~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/table_share.cc

  • Committer: Brian Aker
  • Date: 2010-05-14 14:34:40 UTC
  • mfrom: (1530.1.3 staging)
  • Revision ID: brian@gaz-20100514143440-p7yyn7y6yxpqumf1
Rollup merge for typelib/tableshare cleanup.

Show diffs side-by-side

added added

removed removed

Lines of Context:
56
56
#include "drizzled/item/null.h"
57
57
#include "drizzled/temporal.h"
58
58
 
 
59
#include "drizzled/field.h"
 
60
#include "drizzled/field/str.h"
 
61
#include "drizzled/field/num.h"
 
62
#include "drizzled/field/blob.h"
 
63
#include "drizzled/field/enum.h"
 
64
#include "drizzled/field/null.h"
 
65
#include "drizzled/field/date.h"
 
66
#include "drizzled/field/decimal.h"
 
67
#include "drizzled/field/real.h"
 
68
#include "drizzled/field/double.h"
 
69
#include "drizzled/field/long.h"
 
70
#include "drizzled/field/int64_t.h"
 
71
#include "drizzled/field/num.h"
 
72
#include "drizzled/field/timestamp.h"
 
73
#include "drizzled/field/datetime.h"
 
74
#include "drizzled/field/varstring.h"
 
75
 
59
76
using namespace std;
60
77
 
61
78
namespace drizzled
79
96
   * This is going to overalloc a bit - as rehash sets the number of
80
97
   * buckets, not the number of elements. BUT, it'll allow us to not need
81
98
   * to rehash later on as the unordered_map grows.
82
 
   */
 
99
 */
83
100
  table_def_cache.rehash(table_def_size);
84
101
}
85
102
 
237
254
 
238
255
  /**
239
256
   * @TODO: we need to eject something if we exceed table_def_size
240
 
   */
 
257
 */
241
258
  pair<TableDefinitionCache::iterator, bool> ret=
242
259
    table_def_cache.insert(make_pair(key_string, share));
243
260
  if (ret.second == false)
246
263
 
247
264
    return NULL;
248
265
  }
249
 
  
 
266
 
250
267
  TableIdentifier identifier(share->getSchemaName(), share->getTableName());
251
268
  if (share->open_table_def(*session, identifier))
252
269
  {
422
439
bool TableShare::fieldInPrimaryKey(Field *in_field) const
423
440
{
424
441
  assert(table_proto != NULL);
425
 
  
 
442
 
426
443
  size_t num_indexes= table_proto->indexes_size();
427
444
 
428
445
  for (size_t x= 0; x < num_indexes; ++x)
503
520
  newed(true)
504
521
{
505
522
  memset(&name_hash, 0, sizeof(HASH));
506
 
  memset(&fieldnames, 0, sizeof(TYPELIB));
507
523
 
508
524
  table_charset= 0;
509
525
  memset(&all_set, 0, sizeof (MyBitmap));
582
598
    local_db_create_options|= HA_OPTION_PACK_RECORD;
583
599
 
584
600
  /* local_db_create_options was stored as 2 bytes in FRM
585
 
     Any HA_OPTION_ that doesn't fit into 2 bytes was silently truncated away.
586
 
   */
 
601
    Any HA_OPTION_ that doesn't fit into 2 bytes was silently truncated away.
 
602
  */
587
603
  share->db_create_options= (local_db_create_options & 0x0000FFFF);
588
604
  share->db_options_in_use= share->db_create_options;
589
605
 
594
610
    table_options.block_size() : 0;
595
611
 
596
612
  share->table_charset= get_charset(table_options.has_collation_id()?
597
 
                                    table_options.collation_id() : 0);
 
613
                                    table_options.collation_id() : 0);
598
614
 
599
615
  if (!share->table_charset)
600
616
  {
603
619
    {
604
620
      /* Warn that we may be changing the size of character columns */
605
621
      errmsg_printf(ERRMSG_LVL_WARN,
606
 
                    _("'%s' had no or invalid character set, "
607
 
                      "and default character set is multi-byte, "
608
 
                      "so character column sizes may have changed"),
 
622
                    _("'%s' had no or invalid character set, "
 
623
                      "and default character set is multi-byte, "
 
624
                      "so character column sizes may have changed"),
609
625
                    share->getPath());
610
626
    }
611
627
    share->table_charset= default_charset_info;
777
793
    field_offsets[fieldnr]= stored_columns_reclength;
778
794
 
779
795
    /* the below switch is very similar to
780
 
       CreateField::create_length_to_internal_length in field.cc
781
 
       (which should one day be replace by just this code)
 
796
      CreateField::create_length_to_internal_length in field.cc
 
797
      (which should one day be replace by just this code)
782
798
    */
783
799
    switch(drizzle_field_type)
784
800
    {
869
885
    share->intervals= NULL;
870
886
  }
871
887
 
872
 
  share->fieldnames.type_names= (const char **) share->alloc_root((share->fields + 1) * sizeof(char*));
873
 
 
874
 
  share->fieldnames.type_lengths= (unsigned int *) share->alloc_root((share->fields + 1) * sizeof(unsigned int));
875
 
 
876
 
  share->fieldnames.type_names[share->fields]= NULL;
877
 
  share->fieldnames.type_lengths[share->fields]= 0;
878
 
  share->fieldnames.count= share->fields;
879
 
 
880
 
 
881
888
  /* Now fix the TYPELIBs for the intervals (enum values)
882
 
     and field names.
883
 
   */
 
889
    and field names.
 
890
  */
884
891
 
885
892
  uint32_t interval_nr= 0;
886
893
 
889
896
    message::Table::Field pfield= table.field(fieldnr);
890
897
 
891
898
    /* field names */
892
 
    share->fieldnames.type_names[fieldnr]= share->strmake_root(pfield.name().c_str(), pfield.name().length());
893
 
 
894
 
    share->fieldnames.type_lengths[fieldnr]= pfield.name().length();
 
899
    addFieldName(std::string(pfield.name().c_str(), pfield.name().length()));
895
900
 
896
901
    /* enum typelibs */
897
902
    if (pfield.type() != message::Table::Field::ENUM)
924
929
      /* 
925
930
       * Go ask the charset what the length is as for "" length=1
926
931
       * and there's stripping spaces or some other crack going on.
927
 
       */
 
932
     */
928
933
      uint32_t lengthsp;
929
934
      lengthsp= charset->cset->lengthsp(charset,
930
935
                                        t->type_names[n],
993
998
      }
994
999
      else if (! pfield.options().has_update_value())
995
1000
      {
996
 
        unireg_type= Field::TIMESTAMP_DN_FIELD;
 
1001
        unireg_type= Field::TIMESTAMP_DN_FIELD;
997
1002
      }
998
1003
      else
999
 
        assert(1); // Invalid update value.
 
1004
        assert(1); // Invalid update value.
1000
1005
    }
1001
1006
    else if (pfield.has_options() &&
1002
1007
             pfield.options().has_update_value() &&
1035
1040
                           field_options.collation_id() : 0);
1036
1041
 
1037
1042
      if (! charset)
1038
 
        charset= default_charset_info;
 
1043
        charset= default_charset_info;
1039
1044
    }
1040
1045
 
1041
1046
    if (field_type == DRIZZLE_TYPE_ENUM)
1043
1048
      message::Table::Field::EnumerationValues field_options= pfield.enumeration_values();
1044
1049
 
1045
1050
      charset= get_charset(field_options.has_collation_id()?
1046
 
                           field_options.collation_id() : 0);
 
1051
                           field_options.collation_id() : 0);
1047
1052
 
1048
1053
      if (! charset)
1049
 
              charset= default_charset_info;
 
1054
        charset= default_charset_info;
1050
1055
    }
1051
1056
 
1052
1057
    uint8_t decimals= 0;
1069
1074
        {
1070
1075
          local_error= 4;
1071
1076
 
1072
 
          return local_error;
 
1077
          return local_error;
1073
1078
        }
1074
1079
        decimals= static_cast<uint8_t>(fo.scale());
1075
1080
      }
1102
1107
    {
1103
1108
    case DRIZZLE_TYPE_BLOB:
1104
1109
    case DRIZZLE_TYPE_VARCHAR:
1105
 
    {
1106
 
      message::Table::Field::StringFieldOptions field_options= pfield.string_options();
1107
 
 
1108
 
      charset= get_charset(field_options.has_collation_id() ?
1109
 
                           field_options.collation_id() : 0);
1110
 
 
1111
 
      if (! charset)
1112
 
        charset= default_charset_info;
1113
 
 
1114
 
      field_length= field_options.length() * charset->mbmaxlen;
1115
 
    }
 
1110
      {
 
1111
        message::Table::Field::StringFieldOptions field_options= pfield.string_options();
 
1112
 
 
1113
        charset= get_charset(field_options.has_collation_id() ?
 
1114
                             field_options.collation_id() : 0);
 
1115
 
 
1116
        if (! charset)
 
1117
          charset= default_charset_info;
 
1118
 
 
1119
        field_length= field_options.length() * charset->mbmaxlen;
 
1120
      }
1116
1121
      break;
1117
1122
    case DRIZZLE_TYPE_DOUBLE:
1118
 
    {
1119
 
      message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
1120
 
      if (!fo.has_precision() && !fo.has_scale())
1121
 
      {
1122
 
        field_length= DBL_DIG+7;
1123
 
      }
1124
 
      else
1125
 
      {
1126
 
        field_length= fo.precision();
1127
 
      }
1128
 
      if (field_length < decimals &&
1129
 
          decimals != NOT_FIXED_DEC)
1130
 
      {
1131
 
        my_error(ER_M_BIGGER_THAN_D, MYF(0), pfield.name().c_str());
1132
 
        local_error= 1;
 
1123
      {
 
1124
        message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
 
1125
        if (!fo.has_precision() && !fo.has_scale())
 
1126
        {
 
1127
          field_length= DBL_DIG+7;
 
1128
        }
 
1129
        else
 
1130
        {
 
1131
          field_length= fo.precision();
 
1132
        }
 
1133
        if (field_length < decimals &&
 
1134
            decimals != NOT_FIXED_DEC)
 
1135
        {
 
1136
          my_error(ER_M_BIGGER_THAN_D, MYF(0), pfield.name().c_str());
 
1137
          local_error= 1;
1133
1138
 
1134
 
        return local_error;
 
1139
          return local_error;
 
1140
        }
 
1141
        break;
1135
1142
      }
1136
 
      break;
1137
 
    }
1138
1143
    case DRIZZLE_TYPE_DECIMAL:
1139
 
    {
1140
 
      message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
 
1144
      {
 
1145
        message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
1141
1146
 
1142
 
      field_length= my_decimal_precision_to_length(fo.precision(), fo.scale(),
1143
 
                                                   false);
1144
 
      break;
1145
 
    }
 
1147
        field_length= my_decimal_precision_to_length(fo.precision(), fo.scale(),
 
1148
                                                     false);
 
1149
        break;
 
1150
      }
1146
1151
    case DRIZZLE_TYPE_TIMESTAMP:
1147
1152
    case DRIZZLE_TYPE_DATETIME:
1148
1153
      field_length= DateTime::MAX_STRING_LENGTH;
1151
1156
      field_length= Date::MAX_STRING_LENGTH;
1152
1157
      break;
1153
1158
    case DRIZZLE_TYPE_ENUM:
1154
 
    {
1155
 
      field_length= 0;
1156
 
 
1157
 
      message::Table::Field::EnumerationValues fo= pfield.enumeration_values();
1158
 
 
1159
 
      for (int valnr= 0; valnr < fo.field_value_size(); valnr++)
1160
1159
      {
1161
 
        if (fo.field_value(valnr).length() > field_length)
 
1160
        field_length= 0;
 
1161
 
 
1162
        message::Table::Field::EnumerationValues fo= pfield.enumeration_values();
 
1163
 
 
1164
        for (int valnr= 0; valnr < fo.field_value_size(); valnr++)
1162
1165
        {
1163
 
          field_length= charset->cset->numchars(charset,
1164
 
                                                fo.field_value(valnr).c_str(),
1165
 
                                                fo.field_value(valnr).c_str()
1166
 
                                                + fo.field_value(valnr).length())
1167
 
            * charset->mbmaxlen;
 
1166
          if (fo.field_value(valnr).length() > field_length)
 
1167
          {
 
1168
            field_length= charset->cset->numchars(charset,
 
1169
                                                  fo.field_value(valnr).c_str(),
 
1170
                                                  fo.field_value(valnr).c_str()
 
1171
                                                  + fo.field_value(valnr).length())
 
1172
              * charset->mbmaxlen;
 
1173
          }
1168
1174
        }
1169
1175
      }
1170
 
    }
1171
 
    break;
 
1176
      break;
1172
1177
    case DRIZZLE_TYPE_LONG:
1173
1178
      {
1174
1179
        uint32_t sign_len= pfield.constraints().is_unsigned() ? 0 : 1;
1175
 
          field_length= MAX_INT_WIDTH+sign_len;
 
1180
        field_length= MAX_INT_WIDTH+sign_len;
1176
1181
      }
1177
1182
      break;
1178
1183
    case DRIZZLE_TYPE_LONGLONG:
1194
1199
                                ((field_type == DRIZZLE_TYPE_ENUM) ?
1195
1200
                                 share->intervals + (interval_nr++)
1196
1201
                                 : (TYPELIB*) 0),
1197
 
                                share->fieldnames.type_names[fieldnr]);
 
1202
                                fieldnames[fieldnr].c_str());
1198
1203
 
1199
1204
    share->field[fieldnr]= f;
1200
1205
 
1219
1224
        my_error(ER_INVALID_DEFAULT, MYF(0), f->field_name);
1220
1225
        local_error= 1;
1221
1226
 
1222
 
        return local_error;
 
1227
        return local_error;
1223
1228
      }
1224
1229
    }
1225
1230
    else if (f->real_type() == DRIZZLE_TYPE_ENUM &&
1255
1260
 
1256
1261
    if (use_hash) /* supposedly this never fails... but comments lie */
1257
1262
      (void) my_hash_insert(&share->name_hash,
1258
 
                            (unsigned char*)&(share->field[fieldnr]));
 
1263
                            (unsigned char*)&(share->field[fieldnr]));
1259
1264
 
1260
1265
  }
1261
1266
 
1272
1277
       * Fix up key_part->offset by adding data_offset.
1273
1278
       * We really should compute offset as well.
1274
1279
       * But at least this way we are a little better.
1275
 
       */
 
1280
     */
1276
1281
      key_part->offset= field_offsets[key_part->fieldnr-1] + data_offset;
1277
1282
    }
1278
1283
  }
1304
1309
 
1305
1310
      if (local_primary_key >= MAX_KEY && (keyinfo->flags & HA_NOSAME))
1306
1311
      {
1307
 
        /*
1308
 
          If the UNIQUE key doesn't have NULL columns and is not a part key
1309
 
          declare this as a primary key.
1310
 
        */
1311
 
        local_primary_key=key;
1312
 
        for (uint32_t i= 0; i < keyinfo->key_parts; i++)
1313
 
        {
1314
 
          uint32_t fieldnr= key_part[i].fieldnr;
1315
 
          if (! fieldnr ||
1316
 
              share->field[fieldnr-1]->null_ptr ||
1317
 
              share->field[fieldnr-1]->key_length() != key_part[i].length)
1318
 
          {
1319
 
            local_primary_key= MAX_KEY; // Can't be used
1320
 
            break;
1321
 
          }
1322
 
        }
 
1312
        /*
 
1313
          If the UNIQUE key doesn't have NULL columns and is not a part key
 
1314
          declare this as a primary key.
 
1315
        */
 
1316
        local_primary_key=key;
 
1317
        for (uint32_t i= 0; i < keyinfo->key_parts; i++)
 
1318
        {
 
1319
          uint32_t fieldnr= key_part[i].fieldnr;
 
1320
          if (! fieldnr ||
 
1321
              share->field[fieldnr-1]->null_ptr ||
 
1322
              share->field[fieldnr-1]->key_length() != key_part[i].length)
 
1323
          {
 
1324
            local_primary_key= MAX_KEY; // Can't be used
 
1325
            break;
 
1326
          }
 
1327
        }
1323
1328
      }
1324
1329
 
1325
1330
      for (uint32_t i= 0 ; i < keyinfo->key_parts ; key_part++,i++)
1326
1331
      {
1327
 
        Field *local_field;
1328
 
        if (! key_part->fieldnr)
1329
 
        {
1330
 
          return ENOMEM;
1331
 
        }
1332
 
        local_field= key_part->field= share->field[key_part->fieldnr-1];
1333
 
        key_part->type= local_field->key_type();
1334
 
        if (local_field->null_ptr)
1335
 
        {
1336
 
          key_part->null_offset=(uint32_t) ((unsigned char*) local_field->null_ptr - share->default_values);
1337
 
          key_part->null_bit= local_field->null_bit;
1338
 
          key_part->store_length+=HA_KEY_NULL_LENGTH;
1339
 
          keyinfo->flags|=HA_NULL_PART_KEY;
1340
 
          keyinfo->extra_length+= HA_KEY_NULL_LENGTH;
1341
 
          keyinfo->key_length+= HA_KEY_NULL_LENGTH;
1342
 
        }
1343
 
        if (local_field->type() == DRIZZLE_TYPE_BLOB ||
1344
 
            local_field->real_type() == DRIZZLE_TYPE_VARCHAR)
1345
 
        {
1346
 
          if (local_field->type() == DRIZZLE_TYPE_BLOB)
1347
 
            key_part->key_part_flag|= HA_BLOB_PART;
1348
 
          else
1349
 
            key_part->key_part_flag|= HA_VAR_LENGTH_PART;
1350
 
          keyinfo->extra_length+=HA_KEY_BLOB_LENGTH;
1351
 
          key_part->store_length+=HA_KEY_BLOB_LENGTH;
1352
 
          keyinfo->key_length+= HA_KEY_BLOB_LENGTH;
1353
 
        }
1354
 
        if (i == 0 && key != local_primary_key)
1355
 
          local_field->flags |= (((keyinfo->flags & HA_NOSAME) &&
1356
 
                            (keyinfo->key_parts == 1)) ?
1357
 
                           UNIQUE_KEY_FLAG : MULTIPLE_KEY_FLAG);
1358
 
        if (i == 0)
1359
 
          local_field->key_start.set(key);
1360
 
        if (local_field->key_length() == key_part->length &&
1361
 
            !(local_field->flags & BLOB_FLAG))
1362
 
        {
1363
 
          enum ha_key_alg algo= share->key_info[key].algorithm;
1364
 
          if (share->db_type()->index_flags(algo) & HA_KEYREAD_ONLY)
1365
 
          {
1366
 
            share->keys_for_keyread.set(key);
1367
 
            local_field->part_of_key.set(key);
1368
 
            local_field->part_of_key_not_clustered.set(key);
1369
 
          }
1370
 
          if (share->db_type()->index_flags(algo) & HA_READ_ORDER)
1371
 
            local_field->part_of_sortkey.set(key);
1372
 
        }
1373
 
        if (!(key_part->key_part_flag & HA_REVERSE_SORT) &&
1374
 
            usable_parts == i)
1375
 
          usable_parts++;                       // For FILESORT
1376
 
        local_field->flags|= PART_KEY_FLAG;
1377
 
        if (key == local_primary_key)
1378
 
        {
1379
 
          local_field->flags|= PRI_KEY_FLAG;
1380
 
          /*
1381
 
            If this field is part of the primary key and all keys contains
1382
 
            the primary key, then we can use any key to find this column
1383
 
          */
1384
 
          if (share->storage_engine->check_flag(HTON_BIT_PRIMARY_KEY_IN_READ_INDEX))
1385
 
          {
1386
 
            local_field->part_of_key= share->keys_in_use;
1387
 
            if (local_field->part_of_sortkey.test(key))
1388
 
              local_field->part_of_sortkey= share->keys_in_use;
1389
 
          }
1390
 
        }
1391
 
        if (local_field->key_length() != key_part->length)
1392
 
        {
1393
 
          key_part->key_part_flag|= HA_PART_KEY_SEG;
1394
 
        }
 
1332
        Field *local_field;
 
1333
        if (! key_part->fieldnr)
 
1334
        {
 
1335
          return ENOMEM;
 
1336
        }
 
1337
        local_field= key_part->field= share->field[key_part->fieldnr-1];
 
1338
        key_part->type= local_field->key_type();
 
1339
        if (local_field->null_ptr)
 
1340
        {
 
1341
          key_part->null_offset=(uint32_t) ((unsigned char*) local_field->null_ptr - share->default_values);
 
1342
          key_part->null_bit= local_field->null_bit;
 
1343
          key_part->store_length+=HA_KEY_NULL_LENGTH;
 
1344
          keyinfo->flags|=HA_NULL_PART_KEY;
 
1345
          keyinfo->extra_length+= HA_KEY_NULL_LENGTH;
 
1346
          keyinfo->key_length+= HA_KEY_NULL_LENGTH;
 
1347
        }
 
1348
        if (local_field->type() == DRIZZLE_TYPE_BLOB ||
 
1349
            local_field->real_type() == DRIZZLE_TYPE_VARCHAR)
 
1350
        {
 
1351
          if (local_field->type() == DRIZZLE_TYPE_BLOB)
 
1352
            key_part->key_part_flag|= HA_BLOB_PART;
 
1353
          else
 
1354
            key_part->key_part_flag|= HA_VAR_LENGTH_PART;
 
1355
          keyinfo->extra_length+=HA_KEY_BLOB_LENGTH;
 
1356
          key_part->store_length+=HA_KEY_BLOB_LENGTH;
 
1357
          keyinfo->key_length+= HA_KEY_BLOB_LENGTH;
 
1358
        }
 
1359
        if (i == 0 && key != local_primary_key)
 
1360
          local_field->flags |= (((keyinfo->flags & HA_NOSAME) &&
 
1361
                                  (keyinfo->key_parts == 1)) ?
 
1362
                                 UNIQUE_KEY_FLAG : MULTIPLE_KEY_FLAG);
 
1363
        if (i == 0)
 
1364
          local_field->key_start.set(key);
 
1365
        if (local_field->key_length() == key_part->length &&
 
1366
            !(local_field->flags & BLOB_FLAG))
 
1367
        {
 
1368
          enum ha_key_alg algo= share->key_info[key].algorithm;
 
1369
          if (share->db_type()->index_flags(algo) & HA_KEYREAD_ONLY)
 
1370
          {
 
1371
            share->keys_for_keyread.set(key);
 
1372
            local_field->part_of_key.set(key);
 
1373
            local_field->part_of_key_not_clustered.set(key);
 
1374
          }
 
1375
          if (share->db_type()->index_flags(algo) & HA_READ_ORDER)
 
1376
            local_field->part_of_sortkey.set(key);
 
1377
        }
 
1378
        if (!(key_part->key_part_flag & HA_REVERSE_SORT) &&
 
1379
            usable_parts == i)
 
1380
          usable_parts++;                       // For FILESORT
 
1381
        local_field->flags|= PART_KEY_FLAG;
 
1382
        if (key == local_primary_key)
 
1383
        {
 
1384
          local_field->flags|= PRI_KEY_FLAG;
 
1385
          /*
 
1386
            If this field is part of the primary key and all keys contains
 
1387
            the primary key, then we can use any key to find this column
 
1388
          */
 
1389
          if (share->storage_engine->check_flag(HTON_BIT_PRIMARY_KEY_IN_READ_INDEX))
 
1390
          {
 
1391
            local_field->part_of_key= share->keys_in_use;
 
1392
            if (local_field->part_of_sortkey.test(key))
 
1393
              local_field->part_of_sortkey= share->keys_in_use;
 
1394
          }
 
1395
        }
 
1396
        if (local_field->key_length() != key_part->length)
 
1397
        {
 
1398
          key_part->key_part_flag|= HA_PART_KEY_SEG;
 
1399
        }
1395
1400
      }
1396
1401
      keyinfo->usable_key_parts= usable_parts; // Filesort
1397
1402
 
1398
1403
      set_if_bigger(share->max_key_length,keyinfo->key_length+
1399
 
                    keyinfo->key_parts);
 
1404
                    keyinfo->key_parts);
1400
1405
      share->total_key_length+= keyinfo->key_length;
1401
1406
 
1402
1407
      if (keyinfo->flags & HA_NOSAME)
1403
1408
      {
1404
 
        set_if_bigger(share->max_unique_length,keyinfo->key_length);
 
1409
        set_if_bigger(share->max_unique_length,keyinfo->key_length);
1405
1410
      }
1406
1411
    }
1407
1412
    if (local_primary_key < MAX_KEY &&
1437
1442
  {
1438
1443
    Field *reg_field= *share->found_next_number_field;
1439
1444
    if ((int) (share->next_number_index= (uint32_t)
1440
 
               find_ref_key(share->key_info, share->keys,
 
1445
               find_ref_key(share->key_info, share->keys,
1441
1446
                            share->default_values, reg_field,
1442
 
                            &share->next_number_key_offset,
 
1447
                            &share->next_number_key_offset,
1443
1448
                            &share->next_number_keypart)) < 0)
1444
1449
    {
1445
1450
      /* Wrong field definition */
1460
1465
 
1461
1466
    /* Store offsets to blob fields to find them fast */
1462
1467
    if (!(share->blob_field= save=
1463
 
          (uint*) share->alloc_root((uint32_t) (share->blob_fields* sizeof(uint32_t)))))
 
1468
          (uint*) share->alloc_root((uint32_t) (share->blob_fields* sizeof(uint32_t)))))
1464
1469
    {
1465
1470
      return local_error;
1466
1471
    }
1515
1520
  share         Fill this with table definition
1516
1521
 
1517
1522
  NOTES
1518
 
    This function is called when the table definition is not cached in
1519
 
    table_def_cache
1520
 
    The data is returned in 'share', which is alloced by
1521
 
    alloc_table_share().. The code assumes that share is initialized.
 
1523
  This function is called when the table definition is not cached in
 
1524
  table_def_cache
 
1525
  The data is returned in 'share', which is alloced by
 
1526
  alloc_table_share().. The code assumes that share is initialized.
1522
1527
 
1523
1528
  RETURN VALUES
1524
 
   0    ok
1525
 
   1    Error (see open_table_error)
1526
 
   2    Error (see open_table_error)
1527
 
   3    Wrong data in .frm cursor
1528
 
   4    Error (see open_table_error)
1529
 
   5    Error (see open_table_error: charset unavailable)
1530
 
   6    Unknown .frm version
 
1529
  0     ok
 
1530
  1     Error (see open_table_error)
 
1531
  2    Error (see open_table_error)
 
1532
  3    Wrong data in .frm cursor
 
1533
  4    Error (see open_table_error)
 
1534
  5    Error (see open_table_error: charset unavailable)
 
1535
  6    Unknown .frm version
1531
1536
*/
1532
1537
 
1533
1538
int TableShare::open_table_def(Session& session, TableIdentifier &identifier)
1553
1558
    {
1554
1559
      if (not table.IsInitialized())
1555
1560
      {
1556
 
        local_error= 4;
 
1561
        local_error= 4;
1557
1562
      }
1558
1563
    }
1559
1564
    goto err_not_open;
1578
1583
  Open a table based on a TableShare
1579
1584
 
1580
1585
  SYNOPSIS
1581
 
    open_table_from_share()
1582
 
    session                     Thread Cursor
1583
 
    share               Table definition
1584
 
    alias               Alias for table
1585
 
    db_stat             open flags (for example HA_OPEN_KEYFILE|
1586
 
                        HA_OPEN_RNDFILE..) can be 0 (example in
1587
 
                        ha_example_table)
1588
 
    ha_open_flags       HA_OPEN_ABORT_IF_LOCKED etc..
1589
 
    outparam            result table
 
1586
  open_table_from_share()
 
1587
  session                       Thread Cursor
 
1588
  share         Table definition
 
1589
  alias         Alias for table
 
1590
  db_stat               open flags (for example HA_OPEN_KEYFILE|
 
1591
  HA_OPEN_RNDFILE..) can be 0 (example in
 
1592
  ha_example_table)
 
1593
  ha_open_flags HA_OPEN_ABORT_IF_LOCKED etc..
 
1594
  outparam              result table
1590
1595
 
1591
1596
  RETURN VALUES
1592
 
   0    ok
1593
 
   1    Error (see open_table_error)
1594
 
   2    Error (see open_table_error)
1595
 
   3    Wrong data in .frm cursor
1596
 
   4    Error (see open_table_error)
1597
 
   5    Error (see open_table_error: charset unavailable)
1598
 
   7    Table definition has changed in engine
 
1597
  0     ok
 
1598
  1     Error (see open_table_error)
 
1599
  2    Error (see open_table_error)
 
1600
  3    Wrong data in .frm cursor
 
1601
  4    Error (see open_table_error)
 
1602
  5    Error (see open_table_error: charset unavailable)
 
1603
  7    Table definition has changed in engine
1599
1604
*/
1600
1605
 
1601
1606
int TableShare::open_table_from_share(Session *session, const char *alias,
1704
1709
 
1705
1710
    memcpy(local_key_info, key_info, sizeof(*local_key_info)*keys);
1706
1711
    memcpy(key_part, key_info[0].key_part, (sizeof(*key_part) *
1707
 
                                                   key_parts));
 
1712
                                            key_parts));
1708
1713
 
1709
1714
    for (key_info_end= local_key_info + keys ;
1710
1715
         local_key_info < key_info_end ;
1763
1768
    {
1764
1769
      switch (ha_err)
1765
1770
      {
1766
 
        case HA_ERR_NO_SUCH_TABLE:
1767
 
          /*
1768
 
            The table did not exists in storage engine, use same error message
1769
 
            as if the .frm cursor didn't exist
1770
 
          */
1771
 
          local_error= 1;
1772
 
          errno= ENOENT;
1773
 
          break;
1774
 
        case EMFILE:
1775
 
          /*
1776
 
            Too many files opened, use same error message as if the .frm
1777
 
            cursor can't open
1778
 
           */
1779
 
          local_error= 1;
1780
 
          errno= EMFILE;
1781
 
          break;
1782
 
        default:
1783
 
          outparam.print_error(ha_err, MYF(0));
1784
 
          error_reported= true;
1785
 
          if (ha_err == HA_ERR_TABLE_DEF_CHANGED)
1786
 
            local_error= 7;
1787
 
          break;
 
1771
      case HA_ERR_NO_SUCH_TABLE:
 
1772
        /*
 
1773
          The table did not exists in storage engine, use same error message
 
1774
          as if the .frm cursor didn't exist
 
1775
        */
 
1776
        local_error= 1;
 
1777
        errno= ENOENT;
 
1778
        break;
 
1779
      case EMFILE:
 
1780
        /*
 
1781
          Too many files opened, use same error message as if the .frm
 
1782
          cursor can't open
 
1783
        */
 
1784
        local_error= 1;
 
1785
        errno= EMFILE;
 
1786
        break;
 
1787
      default:
 
1788
        outparam.print_error(ha_err, MYF(0));
 
1789
        error_reported= true;
 
1790
        if (ha_err == HA_ERR_TABLE_DEF_CHANGED)
 
1791
          local_error= 7;
 
1792
        break;
1788
1793
      }
1789
1794
      goto err;
1790
1795
    }
1796
1801
 
1797
1802
  return 0;
1798
1803
 
1799
 
 err:
 
1804
err:
1800
1805
  if (!error_reported)
1801
1806
    open_table_error(local_error, errno, 0);
1802
1807
 
1830
1835
    }
1831
1836
    break;
1832
1837
  case 2:
1833
 
  {
1834
 
    Cursor *cursor= 0;
1835
 
    const char *datext= "";
 
1838
    {
 
1839
      Cursor *cursor= 0;
 
1840
      const char *datext= "";
1836
1841
 
1837
 
    if (db_type() != NULL)
1838
 
    {
1839
 
      if ((cursor= db_type()->getCursor(*this, current_session->mem_root)))
 
1842
      if (db_type() != NULL)
1840
1843
      {
1841
 
        if (!(datext= *db_type()->bas_ext()))
1842
 
          datext= "";
 
1844
        if ((cursor= db_type()->getCursor(*this, current_session->mem_root)))
 
1845
        {
 
1846
          if (!(datext= *db_type()->bas_ext()))
 
1847
            datext= "";
 
1848
        }
1843
1849
      }
 
1850
      err_no= (db_errno == ENOENT) ? ER_FILE_NOT_FOUND : (db_errno == EAGAIN) ?
 
1851
        ER_FILE_USED : ER_CANT_OPEN_FILE;
 
1852
      snprintf(buff, sizeof(buff), "%s%s", normalized_path.str,datext);
 
1853
      my_error(err_no,errortype, buff, db_errno);
 
1854
      delete cursor;
 
1855
      break;
1844
1856
    }
1845
 
    err_no= (db_errno == ENOENT) ? ER_FILE_NOT_FOUND : (db_errno == EAGAIN) ?
1846
 
      ER_FILE_USED : ER_CANT_OPEN_FILE;
1847
 
    snprintf(buff, sizeof(buff), "%s%s", normalized_path.str,datext);
1848
 
    my_error(err_no,errortype, buff, db_errno);
1849
 
    delete cursor;
1850
 
    break;
1851
 
  }
1852
1857
  case 5:
1853
 
  {
1854
 
    const char *csname= get_charset_name((uint32_t) pass_errarg);
1855
 
    char tmp[10];
1856
 
    if (!csname || csname[0] =='?')
1857
1858
    {
1858
 
      snprintf(tmp, sizeof(tmp), "#%d", pass_errarg);
1859
 
      csname= tmp;
 
1859
      const char *csname= get_charset_name((uint32_t) pass_errarg);
 
1860
      char tmp[10];
 
1861
      if (!csname || csname[0] =='?')
 
1862
      {
 
1863
        snprintf(tmp, sizeof(tmp), "#%d", pass_errarg);
 
1864
        csname= tmp;
 
1865
      }
 
1866
      my_printf_error(ER_UNKNOWN_COLLATION,
 
1867
                      _("Unknown collation '%s' in table '%-.64s' definition"),
 
1868
                      MYF(0), csname, table_name.str);
 
1869
      break;
1860
1870
    }
1861
 
    my_printf_error(ER_UNKNOWN_COLLATION,
1862
 
                    _("Unknown collation '%s' in table '%-.64s' definition"),
1863
 
                    MYF(0), csname, table_name.str);
1864
 
    break;
1865
 
  }
1866
1871
  case 6:
1867
1872
    snprintf(buff, sizeof(buff), "%s", normalized_path.str);
1868
1873
    my_printf_error(ER_NOT_FORM_FILE,
1869
1874
                    _("Table '%-.64s' was created with a different version "
1870
 
                    "of Drizzle and cannot be read"),
 
1875
                      "of Drizzle and cannot be read"),
1871
1876
                    MYF(0), buff);
1872
1877
    break;
1873
1878
  case 8:
1881
1886
  return;
1882
1887
} /* open_table_error */
1883
1888
 
 
1889
Field *TableShare::make_field(memory::Root *root,
 
1890
                              unsigned char *ptr,
 
1891
                              uint32_t field_length,
 
1892
                              bool is_nullable,
 
1893
                              unsigned char *null_pos,
 
1894
                              unsigned char null_bit,
 
1895
                              uint8_t decimals,
 
1896
                              enum_field_types field_type,
 
1897
                              const CHARSET_INFO * field_charset,
 
1898
                              Field::utype unireg_check,
 
1899
                              TYPELIB *interval,
 
1900
                              const char *field_name)
 
1901
{
 
1902
  TableShare *share= this;
 
1903
  assert(root);
 
1904
 
 
1905
  if (! is_nullable)
 
1906
  {
 
1907
    null_pos=0;
 
1908
    null_bit=0;
 
1909
  }
 
1910
  else
 
1911
  {
 
1912
    null_bit= ((unsigned char) 1) << null_bit;
 
1913
  }
 
1914
 
 
1915
  switch (field_type) 
 
1916
  {
 
1917
  case DRIZZLE_TYPE_DATE:
 
1918
  case DRIZZLE_TYPE_DATETIME:
 
1919
  case DRIZZLE_TYPE_TIMESTAMP:
 
1920
    field_charset= &my_charset_bin;
 
1921
  default: break;
 
1922
  }
 
1923
 
 
1924
  switch (field_type)
 
1925
  {
 
1926
  case DRIZZLE_TYPE_ENUM:
 
1927
    return new (root) Field_enum(ptr,
 
1928
                                 field_length,
 
1929
                                 null_pos,
 
1930
                                 null_bit,
 
1931
                                 field_name,
 
1932
                                 get_enum_pack_length(interval->count),
 
1933
                                 interval,
 
1934
                                 field_charset);
 
1935
  case DRIZZLE_TYPE_VARCHAR:
 
1936
    return new (root) Field_varstring(ptr,field_length,
 
1937
                                      HA_VARCHAR_PACKLENGTH(field_length),
 
1938
                                      null_pos,null_bit,
 
1939
                                      field_name,
 
1940
                                      share,
 
1941
                                      field_charset);
 
1942
  case DRIZZLE_TYPE_BLOB:
 
1943
    return new (root) Field_blob(ptr,
 
1944
                                 null_pos,
 
1945
                                 null_bit,
 
1946
                                 field_name,
 
1947
                                 share,
 
1948
                                 calc_pack_length(DRIZZLE_TYPE_LONG, 0),
 
1949
                                 field_charset);
 
1950
  case DRIZZLE_TYPE_DECIMAL:
 
1951
    return new (root) Field_decimal(ptr,
 
1952
                                    field_length,
 
1953
                                    null_pos,
 
1954
                                    null_bit,
 
1955
                                    unireg_check,
 
1956
                                    field_name,
 
1957
                                    decimals,
 
1958
                                    false,
 
1959
                                    false /* is_unsigned */);
 
1960
  case DRIZZLE_TYPE_DOUBLE:
 
1961
    return new (root) Field_double(ptr,
 
1962
                                   field_length,
 
1963
                                   null_pos,
 
1964
                                   null_bit,
 
1965
                                   unireg_check,
 
1966
                                   field_name,
 
1967
                                   decimals,
 
1968
                                   false,
 
1969
                                   false /* is_unsigned */);
 
1970
  case DRIZZLE_TYPE_LONG:
 
1971
    return new (root) Field_long(ptr,
 
1972
                                 field_length,
 
1973
                                 null_pos,
 
1974
                                 null_bit,
 
1975
                                 unireg_check,
 
1976
                                 field_name,
 
1977
                                 false,
 
1978
                                 false /* is_unsigned */);
 
1979
  case DRIZZLE_TYPE_LONGLONG:
 
1980
    return new (root) Field_int64_t(ptr,
 
1981
                                    field_length,
 
1982
                                    null_pos,
 
1983
                                    null_bit,
 
1984
                                    unireg_check,
 
1985
                                    field_name,
 
1986
                                    false,
 
1987
                                    false /* is_unsigned */);
 
1988
  case DRIZZLE_TYPE_TIMESTAMP:
 
1989
    return new (root) Field_timestamp(ptr,
 
1990
                                      field_length,
 
1991
                                      null_pos,
 
1992
                                      null_bit,
 
1993
                                      unireg_check,
 
1994
                                      field_name,
 
1995
                                      share,
 
1996
                                      field_charset);
 
1997
  case DRIZZLE_TYPE_DATE:
 
1998
    return new (root) Field_date(ptr,
 
1999
                                 null_pos,
 
2000
                                 null_bit,
 
2001
                                 field_name,
 
2002
                                 field_charset);
 
2003
  case DRIZZLE_TYPE_DATETIME:
 
2004
    return new (root) Field_datetime(ptr,
 
2005
                                     null_pos,
 
2006
                                     null_bit,
 
2007
                                     field_name,
 
2008
                                     field_charset);
 
2009
  case DRIZZLE_TYPE_NULL:
 
2010
    return new (root) Field_null(ptr,
 
2011
                                 field_length,
 
2012
                                 field_name,
 
2013
                                 field_charset);
 
2014
  default: // Impossible (Wrong version)
 
2015
    break;
 
2016
  }
 
2017
  return 0;
 
2018
}
 
2019
 
1884
2020
 
1885
2021
} /* namespace drizzled */