1103
1108
case DRIZZLE_TYPE_BLOB:
1104
1109
case DRIZZLE_TYPE_VARCHAR:
1106
message::Table::Field::StringFieldOptions field_options= pfield.string_options();
1108
charset= get_charset(field_options.has_collation_id() ?
1109
field_options.collation_id() : 0);
1112
charset= default_charset_info;
1114
field_length= field_options.length() * charset->mbmaxlen;
1111
message::Table::Field::StringFieldOptions field_options= pfield.string_options();
1113
charset= get_charset(field_options.has_collation_id() ?
1114
field_options.collation_id() : 0);
1117
charset= default_charset_info;
1119
field_length= field_options.length() * charset->mbmaxlen;
1117
1122
case DRIZZLE_TYPE_DOUBLE:
1119
message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
1120
if (!fo.has_precision() && !fo.has_scale())
1122
field_length= DBL_DIG+7;
1126
field_length= fo.precision();
1128
if (field_length < decimals &&
1129
decimals != NOT_FIXED_DEC)
1131
my_error(ER_M_BIGGER_THAN_D, MYF(0), pfield.name().c_str());
1124
message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
1125
if (!fo.has_precision() && !fo.has_scale())
1127
field_length= DBL_DIG+7;
1131
field_length= fo.precision();
1133
if (field_length < decimals &&
1134
decimals != NOT_FIXED_DEC)
1136
my_error(ER_M_BIGGER_THAN_D, MYF(0), pfield.name().c_str());
1138
1143
case DRIZZLE_TYPE_DECIMAL:
1140
message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
1145
message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
1142
field_length= my_decimal_precision_to_length(fo.precision(), fo.scale(),
1147
field_length= my_decimal_precision_to_length(fo.precision(), fo.scale(),
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;
1153
1158
case DRIZZLE_TYPE_ENUM:
1157
message::Table::Field::EnumerationValues fo= pfield.enumeration_values();
1159
for (int valnr= 0; valnr < fo.field_value_size(); valnr++)
1161
if (fo.field_value(valnr).length() > field_length)
1162
message::Table::Field::EnumerationValues fo= pfield.enumeration_values();
1164
for (int valnr= 0; valnr < fo.field_value_size(); valnr++)
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)
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;
1172
1177
case DRIZZLE_TYPE_LONG:
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;
1178
1183
case DRIZZLE_TYPE_LONGLONG:
1305
1310
if (local_primary_key >= MAX_KEY && (keyinfo->flags & HA_NOSAME))
1308
If the UNIQUE key doesn't have NULL columns and is not a part key
1309
declare this as a primary key.
1311
local_primary_key=key;
1312
for (uint32_t i= 0; i < keyinfo->key_parts; i++)
1314
uint32_t fieldnr= key_part[i].fieldnr;
1316
share->field[fieldnr-1]->null_ptr ||
1317
share->field[fieldnr-1]->key_length() != key_part[i].length)
1319
local_primary_key= MAX_KEY; // Can't be used
1313
If the UNIQUE key doesn't have NULL columns and is not a part key
1314
declare this as a primary key.
1316
local_primary_key=key;
1317
for (uint32_t i= 0; i < keyinfo->key_parts; i++)
1319
uint32_t fieldnr= key_part[i].fieldnr;
1321
share->field[fieldnr-1]->null_ptr ||
1322
share->field[fieldnr-1]->key_length() != key_part[i].length)
1324
local_primary_key= MAX_KEY; // Can't be used
1325
1330
for (uint32_t i= 0 ; i < keyinfo->key_parts ; key_part++,i++)
1328
if (! key_part->fieldnr)
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)
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;
1343
if (local_field->type() == DRIZZLE_TYPE_BLOB ||
1344
local_field->real_type() == DRIZZLE_TYPE_VARCHAR)
1346
if (local_field->type() == DRIZZLE_TYPE_BLOB)
1347
key_part->key_part_flag|= HA_BLOB_PART;
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;
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);
1359
local_field->key_start.set(key);
1360
if (local_field->key_length() == key_part->length &&
1361
!(local_field->flags & BLOB_FLAG))
1363
enum ha_key_alg algo= share->key_info[key].algorithm;
1364
if (share->db_type()->index_flags(algo) & HA_KEYREAD_ONLY)
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);
1370
if (share->db_type()->index_flags(algo) & HA_READ_ORDER)
1371
local_field->part_of_sortkey.set(key);
1373
if (!(key_part->key_part_flag & HA_REVERSE_SORT) &&
1375
usable_parts++; // For FILESORT
1376
local_field->flags|= PART_KEY_FLAG;
1377
if (key == local_primary_key)
1379
local_field->flags|= PRI_KEY_FLAG;
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
1384
if (share->storage_engine->check_flag(HTON_BIT_PRIMARY_KEY_IN_READ_INDEX))
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;
1391
if (local_field->key_length() != key_part->length)
1393
key_part->key_part_flag|= HA_PART_KEY_SEG;
1333
if (! key_part->fieldnr)
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)
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;
1348
if (local_field->type() == DRIZZLE_TYPE_BLOB ||
1349
local_field->real_type() == DRIZZLE_TYPE_VARCHAR)
1351
if (local_field->type() == DRIZZLE_TYPE_BLOB)
1352
key_part->key_part_flag|= HA_BLOB_PART;
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;
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);
1364
local_field->key_start.set(key);
1365
if (local_field->key_length() == key_part->length &&
1366
!(local_field->flags & BLOB_FLAG))
1368
enum ha_key_alg algo= share->key_info[key].algorithm;
1369
if (share->db_type()->index_flags(algo) & HA_KEYREAD_ONLY)
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);
1375
if (share->db_type()->index_flags(algo) & HA_READ_ORDER)
1376
local_field->part_of_sortkey.set(key);
1378
if (!(key_part->key_part_flag & HA_REVERSE_SORT) &&
1380
usable_parts++; // For FILESORT
1381
local_field->flags|= PART_KEY_FLAG;
1382
if (key == local_primary_key)
1384
local_field->flags|= PRI_KEY_FLAG;
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
1389
if (share->storage_engine->check_flag(HTON_BIT_PRIMARY_KEY_IN_READ_INDEX))
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;
1396
if (local_field->key_length() != key_part->length)
1398
key_part->key_part_flag|= HA_PART_KEY_SEG;
1396
1401
keyinfo->usable_key_parts= usable_parts; // Filesort
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;
1402
1407
if (keyinfo->flags & HA_NOSAME)
1404
set_if_bigger(share->max_unique_length,keyinfo->key_length);
1409
set_if_bigger(share->max_unique_length,keyinfo->key_length);
1407
1412
if (local_primary_key < MAX_KEY &&
1578
1583
Open a table based on a TableShare
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
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
1593
ha_open_flags HA_OPEN_ABORT_IF_LOCKED etc..
1594
outparam result table
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
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
1601
1606
int TableShare::open_table_from_share(Session *session, const char *alias,
1835
const char *datext= "";
1840
const char *datext= "";
1837
if (db_type() != NULL)
1839
if ((cursor= db_type()->getCursor(*this, current_session->mem_root)))
1842
if (db_type() != NULL)
1841
if (!(datext= *db_type()->bas_ext()))
1844
if ((cursor= db_type()->getCursor(*this, current_session->mem_root)))
1846
if (!(datext= *db_type()->bas_ext()))
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);
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);
1854
const char *csname= get_charset_name((uint32_t) pass_errarg);
1856
if (!csname || csname[0] =='?')
1858
snprintf(tmp, sizeof(tmp), "#%d", pass_errarg);
1859
const char *csname= get_charset_name((uint32_t) pass_errarg);
1861
if (!csname || csname[0] =='?')
1863
snprintf(tmp, sizeof(tmp), "#%d", pass_errarg);
1866
my_printf_error(ER_UNKNOWN_COLLATION,
1867
_("Unknown collation '%s' in table '%-.64s' definition"),
1868
MYF(0), csname, table_name.str);
1861
my_printf_error(ER_UNKNOWN_COLLATION,
1862
_("Unknown collation '%s' in table '%-.64s' definition"),
1863
MYF(0), csname, table_name.str);
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"),
1882
1887
} /* open_table_error */
1889
Field *TableShare::make_field(memory::Root *root,
1891
uint32_t field_length,
1893
unsigned char *null_pos,
1894
unsigned char null_bit,
1896
enum_field_types field_type,
1897
const CHARSET_INFO * field_charset,
1898
Field::utype unireg_check,
1900
const char *field_name)
1902
TableShare *share= this;
1912
null_bit= ((unsigned char) 1) << null_bit;
1917
case DRIZZLE_TYPE_DATE:
1918
case DRIZZLE_TYPE_DATETIME:
1919
case DRIZZLE_TYPE_TIMESTAMP:
1920
field_charset= &my_charset_bin;
1926
case DRIZZLE_TYPE_ENUM:
1927
return new (root) Field_enum(ptr,
1932
get_enum_pack_length(interval->count),
1935
case DRIZZLE_TYPE_VARCHAR:
1936
return new (root) Field_varstring(ptr,field_length,
1937
HA_VARCHAR_PACKLENGTH(field_length),
1942
case DRIZZLE_TYPE_BLOB:
1943
return new (root) Field_blob(ptr,
1948
calc_pack_length(DRIZZLE_TYPE_LONG, 0),
1950
case DRIZZLE_TYPE_DECIMAL:
1951
return new (root) Field_decimal(ptr,
1959
false /* is_unsigned */);
1960
case DRIZZLE_TYPE_DOUBLE:
1961
return new (root) Field_double(ptr,
1969
false /* is_unsigned */);
1970
case DRIZZLE_TYPE_LONG:
1971
return new (root) Field_long(ptr,
1978
false /* is_unsigned */);
1979
case DRIZZLE_TYPE_LONGLONG:
1980
return new (root) Field_int64_t(ptr,
1987
false /* is_unsigned */);
1988
case DRIZZLE_TYPE_TIMESTAMP:
1989
return new (root) Field_timestamp(ptr,
1997
case DRIZZLE_TYPE_DATE:
1998
return new (root) Field_date(ptr,
2003
case DRIZZLE_TYPE_DATETIME:
2004
return new (root) Field_datetime(ptr,
2009
case DRIZZLE_TYPE_NULL:
2010
return new (root) Field_null(ptr,
2014
default: // Impossible (Wrong version)
1885
2021
} /* namespace drizzled */