33 #include <sys/types.h>
40 #include <drizzled/error.h>
41 #include <drizzled/gettext.h>
42 #include <drizzled/sql_base.h>
43 #include <drizzled/pthread_globals.h>
44 #include <drizzled/internal/my_pthread.h>
46 #include <drizzled/table.h>
47 #include <drizzled/table/shell.h>
49 #include <drizzled/session.h>
51 #include <drizzled/charset.h>
52 #include <drizzled/internal/m_string.h>
53 #include <drizzled/internal/my_sys.h>
55 #include <drizzled/item/string.h>
56 #include <drizzled/item/int.h>
57 #include <drizzled/item/decimal.h>
58 #include <drizzled/item/float.h>
59 #include <drizzled/item/null.h>
62 #include <drizzled/field.h>
63 #include <drizzled/field/str.h>
64 #include <drizzled/field/num.h>
65 #include <drizzled/field/blob.h>
66 #include <drizzled/field/boolean.h>
67 #include <drizzled/field/enum.h>
68 #include <drizzled/field/null.h>
69 #include <drizzled/field/date.h>
70 #include <drizzled/field/decimal.h>
71 #include <drizzled/field/real.h>
72 #include <drizzled/field/double.h>
73 #include <drizzled/field/int32.h>
74 #include <drizzled/field/int64.h>
75 #include <drizzled/field/size.h>
76 #include <drizzled/field/num.h>
77 #include <drizzled/field/time.h>
78 #include <drizzled/field/epoch.h>
79 #include <drizzled/field/datetime.h>
80 #include <drizzled/field/microtime.h>
81 #include <drizzled/field/varstring.h>
82 #include <drizzled/field/uuid.h>
83 #include <drizzled/field/ipv6.h>
84 #include <drizzled/plugin/storage_engine.h>
85 #include <drizzled/definition/cache.h>
86 #include <drizzled/typelib.h>
87 #include <drizzled/key.h>
88 #include <drizzled/open_tables_state.h>
89 #include <drizzled/catalog/local.h>
95 extern size_t table_def_size;
97 static enum_field_types proto_field_type_to_drizzle_type(
const message::Table::Field &field)
101 case message::Table::Field::INTEGER:
102 return DRIZZLE_TYPE_LONG;
104 case message::Table::Field::DOUBLE:
105 return DRIZZLE_TYPE_DOUBLE;
107 case message::Table::Field::EPOCH:
108 if (field.has_time_options() and field.time_options().microseconds())
109 return DRIZZLE_TYPE_MICROTIME;
111 return DRIZZLE_TYPE_TIMESTAMP;
113 case message::Table::Field::BIGINT:
114 return DRIZZLE_TYPE_LONGLONG;
116 case message::Table::Field::DATETIME:
117 return DRIZZLE_TYPE_DATETIME;
119 case message::Table::Field::DATE:
120 return DRIZZLE_TYPE_DATE;
122 case message::Table::Field::VARCHAR:
123 return DRIZZLE_TYPE_VARCHAR;
125 case message::Table::Field::DECIMAL:
126 return DRIZZLE_TYPE_DECIMAL;
128 case message::Table::Field::ENUM:
129 return DRIZZLE_TYPE_ENUM;
131 case message::Table::Field::BLOB:
132 return DRIZZLE_TYPE_BLOB;
134 case message::Table::Field::UUID:
135 return DRIZZLE_TYPE_UUID;
137 case message::Table::Field::IPV6:
138 return DRIZZLE_TYPE_IPV6;
140 case message::Table::Field::BOOLEAN:
141 return DRIZZLE_TYPE_BOOLEAN;
143 case message::Table::Field::TIME:
144 return DRIZZLE_TYPE_TIME;
150 static Item* default_value_item(enum_field_types field_type,
const charset_info_st& charset,
bool default_null,
151 const string& default_value,
const string& default_bin_value)
154 return new Item_null();
158 case DRIZZLE_TYPE_LONG:
159 case DRIZZLE_TYPE_LONGLONG:
162 Item* default_item=
new Item_int(default_value.c_str(), (int64_t) internal::my_strtoll10(default_value.c_str(), NULL, &error), default_value.length());
164 if (error && error != -1)
171 case DRIZZLE_TYPE_DOUBLE:
172 return new Item_float(default_value.c_str(), default_value.length());
173 case DRIZZLE_TYPE_NULL:
176 case DRIZZLE_TYPE_TIMESTAMP:
177 case DRIZZLE_TYPE_DATETIME:
178 case DRIZZLE_TYPE_TIME:
179 case DRIZZLE_TYPE_DATE:
180 case DRIZZLE_TYPE_ENUM:
181 case DRIZZLE_TYPE_UUID:
182 case DRIZZLE_TYPE_IPV6:
183 case DRIZZLE_TYPE_MICROTIME:
184 case DRIZZLE_TYPE_BOOLEAN:
186 return new Item_string(default_value.data(), default_value.size(), system_charset_info);
187 case DRIZZLE_TYPE_VARCHAR:
188 case DRIZZLE_TYPE_BLOB:
189 return &charset== &my_charset_bin
190 ?
new Item_string(default_bin_value, &my_charset_bin)
191 : new Item_string(default_value, system_charset_info);
192 case DRIZZLE_TYPE_DECIMAL:
193 return new Item_decimal(default_value.c_str(), default_value.length(), system_charset_info);
205 bool TableShare::fieldInPrimaryKey(
Field *in_field)
const
207 assert(getTableMessage());
209 size_t num_indexes= getTableMessage()->indexes_size();
211 for (
size_t x= 0; x < num_indexes; ++x)
214 if (index.is_primary())
216 size_t num_parts= index.index_part_size();
217 for (
size_t y= 0; y < num_parts; ++y)
219 if (index.index_part(y).fieldnr() == in_field->position())
227 TableShare::TableShare(
const identifier::Table::Type type_arg) :
229 found_next_number_field(NULL),
230 timestamp_field(NULL),
232 mem_root(TABLE_ALLOC_BLOCK_SIZE),
234 table_identifier(NULL),
239 stored_rec_length(0),
241 _table_message(NULL),
242 storage_engine(NULL),
246 last_null_bit_pos(0),
252 max_unique_length(0),
257 has_variable_width(false),
258 db_create_options(0),
259 db_options_in_use(0),
261 rowid_field_offset(0),
262 primary_key(MAX_KEY),
263 next_number_index(0),
264 next_number_key_offset(0),
265 next_number_keypart(0),
269 blob_ptr_size(portable_sizeof_char_ptr),
270 db_low_byte_first(false),
274 if (type_arg == message::Table::INTERNAL)
276 string s= identifier::Table::build_tmptable_filename();
277 private_key_for_cache.vectorPtr().assign(s.c_str(), s.c_str() + s.size() + 1);
278 init(private_key_for_cache.vector(), private_key_for_cache.vector());
286 TableShare::TableShare(
const identifier::Table &identifier,
const identifier::Table::Key &key) :
288 found_next_number_field(NULL),
289 timestamp_field(NULL),
291 mem_root(TABLE_ALLOC_BLOCK_SIZE),
294 table_identifier(NULL),
299 stored_rec_length(0),
301 _table_message(NULL),
302 storage_engine(NULL),
303 tmp_table(message::Table::INTERNAL),
306 last_null_bit_pos(0),
312 max_unique_length(0),
317 has_variable_width(false),
318 db_create_options(0),
319 db_options_in_use(0),
321 rowid_field_offset(0),
322 primary_key(MAX_KEY),
323 next_number_index(0),
324 next_number_key_offset(0),
325 next_number_keypart(0),
329 blob_ptr_size(portable_sizeof_char_ptr),
330 db_low_byte_first(false),
334 table_identifier=
new identifier::Table(identifier);
336 assert(identifier.getKey() == key);
338 private_key_for_cache= key;
341 tmp_table= message::Table::INTERNAL;
347 TableShare::TableShare(
const identifier::Table &identifier) :
349 found_next_number_field(NULL),
350 timestamp_field(NULL),
352 mem_root(TABLE_ALLOC_BLOCK_SIZE),
355 table_identifier(NULL),
360 stored_rec_length(0),
362 _table_message(NULL),
363 storage_engine(NULL),
364 tmp_table(identifier.getType()),
367 last_null_bit_pos(0),
373 max_unique_length(0),
378 has_variable_width(false),
379 db_create_options(0),
380 db_options_in_use(0),
382 rowid_field_offset(0),
383 primary_key(MAX_KEY),
384 next_number_index(0),
385 next_number_key_offset(0),
386 next_number_keypart(0),
390 blob_ptr_size(portable_sizeof_char_ptr),
391 db_low_byte_first(false),
395 table_identifier=
new identifier::Table(identifier);
397 private_key_for_cache= identifier.getKey();
398 assert(identifier.getPath().size());
399 private_normalized_path.resize(identifier.getPath().size() + 1);
400 memcpy(&private_normalized_path[0], identifier.getPath().c_str(), identifier.getPath().size());
404 tmp_table= message::Table::INTERNAL;
405 path= private_normalized_path;
406 normalized_path= path;
414 TableShare::TableShare(
const identifier::Table::Type type_arg,
415 const identifier::Table &identifier,
416 const char *path_arg,
417 uint32_t path_length_arg) :
419 found_next_number_field(NULL),
420 timestamp_field(NULL),
422 mem_root(TABLE_ALLOC_BLOCK_SIZE),
425 table_identifier(NULL),
430 stored_rec_length(0),
432 _table_message(NULL),
433 storage_engine(NULL),
437 last_null_bit_pos(0),
443 max_unique_length(0),
448 has_variable_width(false),
449 db_create_options(0),
450 db_options_in_use(0),
452 rowid_field_offset(0),
453 primary_key(MAX_KEY),
454 next_number_index(0),
455 next_number_key_offset(0),
456 next_number_keypart(0),
460 blob_ptr_size(portable_sizeof_char_ptr),
461 db_low_byte_first(false),
465 table_identifier=
new identifier::Table(identifier);
467 private_key_for_cache= identifier.getKey();
472 _path.assign(path_arg, path_length_arg);
476 _path= identifier::Table::build_table_filename(*table_identifier,
false);
479 char* path_buff= mem_root.
strdup(_path);
480 path=
str_ref(path_buff, _path.length());
481 normalized_path=
str_ref(path_buff, _path.length());
483 version= g_refresh_version;
486 void TableShare::init(
const char *new_table_name,
const char *new_path)
489 tmp_table= message::Table::INTERNAL;
492 identifier::Table *n=
new identifier::Table(catalog::local_identifier(),
493 "", new_table_name, new_path);
494 delete table_identifier;
498 normalized_path=
str_ref(new_path);
501 TableShare::~TableShare()
503 storage_engine= NULL;
504 delete table_identifier;
509 void TableShare::setIdentifier(
const identifier::Table &identifier_arg)
511 private_key_for_cache= identifier_arg.getKey();
513 delete table_identifier;
514 table_identifier=
new identifier::Table(identifier_arg);
516 getTableMessage()->set_name(identifier_arg.getTableName());
517 getTableMessage()->set_schema(identifier_arg.getSchemaName());
520 bool TableShare::parse_table_proto(Session& session,
const message::Table &table)
522 drizzled::error_t local_error= EE_OK;
524 if (! table.IsInitialized())
526 my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
527 table.name().empty() ?
" " : table.name().c_str(),
528 table.InitializationErrorString().c_str());
530 return ER_CORRUPT_TABLE_DEFINITION;
533 setTableMessage(table);
535 storage_engine= plugin::StorageEngine::findByName(session, table.engine().name());
536 assert(storage_engine);
538 message::Table::TableOptions table_options;
540 if (table.has_options())
541 table_options= table.options();
543 uint32_t local_db_create_options= 0;
545 if (table_options.pack_record())
546 local_db_create_options|= HA_OPTION_PACK_RECORD;
551 db_create_options= (local_db_create_options & 0x0000FFFF);
552 db_options_in_use= db_create_options;
554 block_size= table_options.has_block_size() ? table_options.block_size() : 0;
556 table_charset= get_charset(table_options.collation_id());
558 if (not table_charset)
560 my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN_COLLATION, MYF(0),
561 table_options.collation().c_str(),
562 table.name().c_str());
564 return ER_CORRUPT_TABLE_DEFINITION;
569 keys= table.indexes_size();
572 for (
int indx= 0; indx < table.indexes_size(); indx++)
573 key_parts+= table.indexes(indx).index_part_size();
575 key_info= (KeyInfo*) mem().
alloc(table.indexes_size() *
sizeof(KeyInfo) +key_parts*
sizeof(KeyPartInfo));
577 KeyPartInfo *key_part;
579 key_part=
reinterpret_cast<KeyPartInfo*
>
580 (key_info+table.indexes_size());
583 ulong *rec_per_key= (ulong*) mem().
alloc(
sizeof(ulong*)*key_parts);
585 KeyInfo* keyinfo= key_info;
586 for (
int keynr= 0; keynr < table.indexes_size(); keynr++, keyinfo++)
588 message::Table::Index indx= table.indexes(keynr);
593 if (indx.is_unique())
594 keyinfo->flags|= HA_NOSAME;
596 if (indx.has_options())
598 message::Table::Index::Options indx_options= indx.options();
599 if (indx_options.pack_key())
600 keyinfo->flags|= HA_PACK_KEY;
602 if (indx_options.var_length_key())
603 keyinfo->flags|= HA_VAR_LENGTH_PART;
605 if (indx_options.null_part_key())
606 keyinfo->flags|= HA_NULL_PART_KEY;
608 if (indx_options.binary_pack_key())
609 keyinfo->flags|= HA_BINARY_PACK_KEY;
611 if (indx_options.has_partial_segments())
612 keyinfo->flags|= HA_KEY_HAS_PART_KEY_SEG;
614 if (indx_options.auto_generated_key())
615 keyinfo->flags|= HA_GENERATED_KEY;
617 if (indx_options.has_key_block_size())
619 keyinfo->flags|= HA_USES_BLOCK_SIZE;
620 keyinfo->block_size= indx_options.key_block_size();
624 keyinfo->block_size= 0;
630 case message::Table::Index::UNKNOWN_INDEX:
631 keyinfo->algorithm= HA_KEY_ALG_UNDEF;
633 case message::Table::Index::BTREE:
634 keyinfo->algorithm= HA_KEY_ALG_BTREE;
636 case message::Table::Index::HASH:
637 keyinfo->algorithm= HA_KEY_ALG_HASH;
642 keyinfo->algorithm= HA_KEY_ALG_UNDEF;
646 keyinfo->key_length= indx.key_length();
648 keyinfo->key_parts= indx.index_part_size();
650 keyinfo->key_part= key_part;
651 keyinfo->rec_per_key= rec_per_key;
653 for (
unsigned int partnr= 0;
654 partnr < keyinfo->key_parts;
655 partnr++, key_part++)
657 message::Table::Index::IndexPart part;
658 part= indx.index_part(partnr);
662 key_part->field= NULL;
663 key_part->fieldnr= part.fieldnr() + 1;
664 key_part->null_bit= 0;
668 key_part->key_part_flag= 0;
669 if (part.has_in_reverse_order())
670 key_part->key_part_flag= part.in_reverse_order()? HA_REVERSE_SORT : 0;
672 key_part->length= part.compare_length();
676 if (table.field(part.fieldnr()).type() == message::Table::Field::VARCHAR
677 || table.field(part.fieldnr()).type() == message::Table::Field::BLOB)
679 uint32_t collation_id;
681 if (table.field(part.fieldnr()).string_options().has_collation_id())
682 collation_id= table.field(part.fieldnr()).string_options().collation_id();
684 collation_id= table.options().collation_id();
686 const charset_info_st *cs= get_charset(collation_id);
688 mbmaxlen= cs->mbmaxlen;
690 key_part->length*= mbmaxlen;
692 key_part->store_length= key_part->length;
695 key_part->key_type= 0;
698 if (not indx.has_comment())
700 keyinfo->comment.clear();
704 keyinfo->flags|= HA_USES_COMMENT;
705 keyinfo->comment.assign(mem().strdup(indx.comment()), indx.comment().length());
708 keyinfo->name= mem().
strdup(indx.name());
710 addKeyName(
string(keyinfo->name, indx.name().length()));
713 keys_for_keyread.reset();
714 set_prefix(keys_in_use, keys);
716 _field_size= table.field_size();
718 setFields(_field_size + 1);
719 _fields[_field_size]= NULL;
721 uint32_t local_null_fields= 0;
724 std::vector<uint32_t> field_offsets;
725 std::vector<uint32_t> field_pack_length;
727 field_offsets.resize(_field_size);
728 field_pack_length.resize(_field_size);
730 uint32_t interval_count= 0;
731 uint32_t interval_parts= 0;
733 uint32_t stored_columns_reclength= 0;
735 for (
unsigned int fieldnr= 0; fieldnr < _field_size; fieldnr++)
737 message::Table::Field pfield= table.field(fieldnr);
738 if (pfield.constraints().is_nullable())
742 else if (not pfield.constraints().is_notnull())
747 enum_field_types drizzle_field_type= proto_field_type_to_drizzle_type(pfield);
749 field_offsets[fieldnr]= stored_columns_reclength;
755 switch(drizzle_field_type)
757 case DRIZZLE_TYPE_BLOB:
758 case DRIZZLE_TYPE_VARCHAR:
760 message::Table::Field::StringFieldOptions field_options= pfield.string_options();
762 const charset_info_st *cs= get_charset(field_options.has_collation_id() ?
763 field_options.collation_id() : 0);
766 cs= default_charset_info;
768 field_pack_length[fieldnr]= calc_pack_length(drizzle_field_type,
769 field_options.length() * cs->mbmaxlen);
772 case DRIZZLE_TYPE_ENUM:
774 message::Table::Field::EnumerationValues field_options= pfield.enumeration_values();
776 field_pack_length[fieldnr]= 4;
779 interval_parts+= field_options.field_value_size();
782 case DRIZZLE_TYPE_DECIMAL:
784 message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
786 field_pack_length[fieldnr]= class_decimal_get_binary_size(fo.precision(), fo.scale());
791 field_pack_length[fieldnr]= calc_pack_length(drizzle_field_type, 0);
794 reclength+= field_pack_length[fieldnr];
795 stored_columns_reclength+= field_pack_length[fieldnr];
799 stored_rec_length= stored_columns_reclength;
801 null_fields= local_null_fields;
803 ulong null_bits= local_null_fields;
804 if (! table_options.pack_record())
806 ulong data_offset= (null_bits + 7)/8;
809 reclength+= data_offset;
810 stored_rec_length+= data_offset;
812 ulong local_rec_buff_length;
814 local_rec_buff_length= ALIGN_SIZE(reclength + 1);
815 rec_buff_length= local_rec_buff_length;
817 resizeDefaultValues(local_rec_buff_length);
818 unsigned char* record= getDefaultValues();
821 if (! table_options.pack_record())
828 intervals.resize(interval_count);
834 uint32_t interval_nr= 0;
836 for (
unsigned int fieldnr= 0; fieldnr < _field_size; fieldnr++)
838 message::Table::Field pfield= table.field(fieldnr);
841 if (pfield.type() != message::Table::Field::ENUM)
844 message::Table::Field::EnumerationValues field_options= pfield.enumeration_values();
846 if (field_options.field_value_size() > Field_enum::max_supported_elements)
848 my_error(ER_CORRUPT_TABLE_DEFINITION_ENUM, MYF(0), table.name().c_str());
850 return ER_CORRUPT_TABLE_DEFINITION_ENUM;
854 const charset_info_st *charset= get_charset(field_options.has_collation_id() ?
855 field_options.collation_id() : 0);
858 charset= default_charset_info;
860 TYPELIB *t= (&intervals[interval_nr]);
862 t->type_names= (
const char**)mem().
alloc((field_options.field_value_size() + 1) *
sizeof(
char*));
863 t->type_lengths= (
unsigned int*)mem().
alloc((field_options.field_value_size() + 1) *
sizeof(
unsigned int));
865 t->type_names[field_options.field_value_size()]= NULL;
866 t->type_lengths[field_options.field_value_size()]= 0;
868 t->count= field_options.field_value_size();
871 for (
int n= 0; n < field_options.field_value_size(); n++)
873 t->type_names[n]= mem().
strdup(field_options.field_value(n));
879 t->type_lengths[n]= charset->cset->lengthsp(charset, t->type_names[n], field_options.field_value(n).length());
888 bool use_hash= _field_size >= MAX_FIELDS_BEFORE_HASH;
890 unsigned char* null_pos= getDefaultValues();
891 int null_bit_pos= (table_options.pack_record()) ? 0 : 1;
893 for (
unsigned int fieldnr= 0; fieldnr < _field_size; fieldnr++)
895 message::Table::Field pfield= table.field(fieldnr);
897 Field::utype unireg_type= Field::NONE;
899 if (pfield.has_numeric_options() &&
900 pfield.numeric_options().is_autoincrement())
902 unireg_type= Field::NEXT_NUMBER;
905 if (pfield.has_options() &&
906 pfield.options().has_default_expression() &&
907 pfield.options().default_expression().compare(
"CURRENT_TIMESTAMP") == 0)
909 if (pfield.options().has_update_expression() &&
910 pfield.options().update_expression().compare(
"CURRENT_TIMESTAMP") == 0)
912 unireg_type= Field::TIMESTAMP_DNUN_FIELD;
914 else if (! pfield.options().has_update_expression())
916 unireg_type= Field::TIMESTAMP_DN_FIELD;
924 else if (pfield.has_options() &&
925 pfield.options().has_update_expression() &&
926 pfield.options().update_expression().compare(
"CURRENT_TIMESTAMP") == 0)
928 unireg_type= Field::TIMESTAMP_UN_FIELD;
932 if (pfield.has_comment())
934 comment.assign(mem().strdup(pfield.comment()), pfield.comment().size());
937 enum_field_types field_type;
939 field_type= proto_field_type_to_drizzle_type(pfield);
941 const charset_info_st *charset= &my_charset_bin;
943 if (field_type == DRIZZLE_TYPE_BLOB || field_type == DRIZZLE_TYPE_VARCHAR)
945 message::Table::Field::StringFieldOptions field_options= pfield.string_options();
947 charset= get_charset(field_options.has_collation_id() ? field_options.collation_id() : 0);
950 charset= default_charset_info;
953 if (field_type == DRIZZLE_TYPE_ENUM)
955 message::Table::Field::EnumerationValues field_options= pfield.enumeration_values();
957 charset= get_charset(field_options.has_collation_id() ? field_options.collation_id() : 0);
960 charset= default_charset_info;
964 if (field_type == DRIZZLE_TYPE_DECIMAL || field_type == DRIZZLE_TYPE_DOUBLE)
966 message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
968 if (not pfield.has_numeric_options() || ! fo.has_scale())
974 decimals= NOT_FIXED_DEC;
978 if (fo.scale() > DECIMAL_MAX_SCALE)
980 local_error= ER_NOT_FORM_FILE;
984 decimals=
static_cast<uint8_t
>(fo.scale());
988 Item *default_value= NULL;
990 if (pfield.options().has_default_value() ||
991 pfield.options().default_null() ||
992 pfield.options().has_default_bin_value())
994 default_value= default_value_item(field_type, *charset, pfield.options().default_null(), pfield.options().default_value(), pfield.options().default_bin_value());
995 if (default_value == NULL)
997 my_error(ER_INVALID_DEFAULT, MYF(0), pfield.name().c_str());
1003 uint32_t field_length= 0;
1008 case DRIZZLE_TYPE_BLOB:
1009 case DRIZZLE_TYPE_VARCHAR:
1011 message::Table::Field::StringFieldOptions field_options= pfield.string_options();
1013 charset= get_charset(field_options.has_collation_id() ?
1014 field_options.collation_id() : 0);
1017 charset= default_charset_info;
1019 field_length= field_options.length() * charset->mbmaxlen;
1022 case DRIZZLE_TYPE_DOUBLE:
1024 message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
1025 if (!fo.has_precision() && !fo.has_scale())
1027 field_length= DBL_DIG+7;
1031 field_length= fo.precision();
1033 if (field_length < decimals &&
1034 decimals != NOT_FIXED_DEC)
1036 my_error(ER_M_BIGGER_THAN_D, MYF(0), pfield.name().c_str());
1037 local_error= ER_M_BIGGER_THAN_D;
1042 case DRIZZLE_TYPE_DECIMAL:
1044 message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
1046 field_length= class_decimal_precision_to_length(fo.precision(), fo.scale(),
1050 case DRIZZLE_TYPE_DATETIME:
1053 case DRIZZLE_TYPE_DATE:
1056 case DRIZZLE_TYPE_ENUM:
1060 message::Table::Field::EnumerationValues fo= pfield.enumeration_values();
1062 for (
int valnr= 0; valnr < fo.field_value_size(); valnr++)
1064 if (fo.field_value(valnr).length() > field_length)
1066 field_length= charset->cset->numchars(charset,
1067 fo.field_value(valnr).c_str(),
1068 fo.field_value(valnr).c_str()
1069 + fo.field_value(valnr).length())
1070 * charset->mbmaxlen;
1075 case DRIZZLE_TYPE_LONG:
1077 uint32_t sign_len= pfield.constraints().is_unsigned() ? 0 : 1;
1078 field_length= MAX_INT_WIDTH+sign_len;
1081 case DRIZZLE_TYPE_LONGLONG:
1083 uint32_t sign_len= pfield.constraints().is_unsigned() ? 0 : 1;
1084 field_length= MAX_BIGINT_WIDTH+sign_len;
1087 case DRIZZLE_TYPE_UUID:
1088 field_length= field::Uuid::max_string_length();
1090 case DRIZZLE_TYPE_IPV6:
1091 field_length= field::IPv6::max_string_length();
1093 case DRIZZLE_TYPE_BOOLEAN:
1094 field_length= field::Boolean::max_string_length();
1096 case DRIZZLE_TYPE_MICROTIME:
1097 field_length= field::Microtime::max_string_length();
1099 case DRIZZLE_TYPE_TIMESTAMP:
1100 field_length= field::Epoch::max_string_length();
1102 case DRIZZLE_TYPE_TIME:
1103 field_length= field::Time::max_string_length();
1105 case DRIZZLE_TYPE_NULL:
1109 bool is_not_null=
false;
1111 if (not pfield.constraints().is_nullable())
1115 else if (pfield.constraints().is_notnull())
1120 Field* f= make_field(pfield,
1121 record + field_offsets[fieldnr] + data_offset,
1129 MTYP_TYPENR(unireg_type),
1130 ((field_type == DRIZZLE_TYPE_ENUM) ? &intervals[interval_nr++] : (TYPELIB*) 0),
1131 getTableMessage()->field(fieldnr).name().c_str());
1133 _fields[fieldnr]= f;
1138 case DRIZZLE_TYPE_BLOB:
1139 case DRIZZLE_TYPE_VARCHAR:
1140 case DRIZZLE_TYPE_DOUBLE:
1141 case DRIZZLE_TYPE_DECIMAL:
1142 case DRIZZLE_TYPE_TIMESTAMP:
1143 case DRIZZLE_TYPE_TIME:
1144 case DRIZZLE_TYPE_DATETIME:
1145 case DRIZZLE_TYPE_MICROTIME:
1146 case DRIZZLE_TYPE_DATE:
1147 case DRIZZLE_TYPE_ENUM:
1148 case DRIZZLE_TYPE_LONG:
1149 case DRIZZLE_TYPE_LONGLONG:
1150 case DRIZZLE_TYPE_NULL:
1151 case DRIZZLE_TYPE_UUID:
1152 case DRIZZLE_TYPE_IPV6:
1153 case DRIZZLE_TYPE_BOOLEAN:
1159 table::Shell temp_table(*
this);
1160 temp_table.in_use= &session;
1162 f->init(&temp_table);
1164 if (! (f->flags & NOT_NULL_FLAG))
1166 *f->null_ptr|= f->null_bit;
1167 if (! (null_bit_pos= (null_bit_pos + 1) & 7))
1174 enum_check_fields old_count_cuted_fields= session.count_cuted_fields;
1175 session.count_cuted_fields= CHECK_FIELD_ERROR_FOR_NULL;
1176 int res= default_value->save_in_field(f, 1);
1177 session.count_cuted_fields= old_count_cuted_fields;
1178 if (res != 0 && res != 3)
1180 my_error(ER_INVALID_DEFAULT, MYF(0), f->field_name);
1181 local_error= ER_INVALID_DEFAULT;
1186 else if (f->real_type() == DRIZZLE_TYPE_ENUM && (f->flags & NOT_NULL_FLAG))
1189 f->store((int64_t) 1,
true);
1198 f->orig_table= NULL;
1200 f->setPosition(fieldnr);
1201 f->comment= comment;
1202 if (not default_value &&
1203 not (f->unireg_check==Field::NEXT_NUMBER) &&
1204 (f->flags & NOT_NULL_FLAG) &&
1205 (not f->is_timestamp()))
1207 f->flags|= NO_DEFAULT_VALUE_FLAG;
1210 if (f->unireg_check == Field::NEXT_NUMBER)
1211 found_next_number_field= &(_fields[fieldnr]);
1215 const char *local_field_name= _fields[fieldnr]->field_name;
1216 name_hash.insert(make_pair(local_field_name, &(_fields[fieldnr])));
1221 for (
unsigned int keynr= 0; keynr < keys; keynr++, keyinfo++)
1223 key_part= keyinfo->key_part;
1225 for (
unsigned int partnr= 0;
1226 partnr < keyinfo->key_parts;
1227 partnr++, key_part++)
1234 key_part->offset= field_offsets[key_part->fieldnr-1] + data_offset;
1243 *(record + null_count / 8)|= ~(((
unsigned char) 1 << (null_count & 7)) - 1);
1245 null_bytes= (null_pos - (
unsigned char*) record + (null_bit_pos + 7) / 8);
1247 last_null_bit_pos= null_bit_pos;
1252 uint32_t local_primary_key= doesKeyNameExist(
"PRIMARY");
1254 key_part= keyinfo->key_part;
1256 for (uint32_t key= 0; key < keys; key++,keyinfo++)
1258 uint32_t usable_parts= 0;
1260 if (local_primary_key >= MAX_KEY && (keyinfo->flags & HA_NOSAME))
1266 local_primary_key=key;
1267 for (uint32_t i= 0; i < keyinfo->key_parts; i++)
1269 uint32_t fieldnr= key_part[i].fieldnr;
1271 _fields[fieldnr-1]->null_ptr ||
1272 _fields[fieldnr-1]->key_length() != key_part[i].length)
1274 local_primary_key= MAX_KEY;
1280 for (uint32_t i= 0 ; i < keyinfo->key_parts ; key_part++,i++)
1283 if (! key_part->fieldnr)
1287 local_field= key_part->field= _fields[key_part->fieldnr-1];
1288 key_part->type= local_field->key_type();
1289 if (local_field->null_ptr)
1291 key_part->null_offset=(uint32_t) ((
unsigned char*) local_field->null_ptr - getDefaultValues());
1292 key_part->null_bit= local_field->null_bit;
1293 key_part->store_length+=HA_KEY_NULL_LENGTH;
1294 keyinfo->flags|=HA_NULL_PART_KEY;
1295 keyinfo->extra_length+= HA_KEY_NULL_LENGTH;
1296 keyinfo->key_length+= HA_KEY_NULL_LENGTH;
1298 if (local_field->type() == DRIZZLE_TYPE_BLOB ||
1299 local_field->real_type() == DRIZZLE_TYPE_VARCHAR)
1301 if (local_field->type() == DRIZZLE_TYPE_BLOB)
1302 key_part->key_part_flag|= HA_BLOB_PART;
1304 key_part->key_part_flag|= HA_VAR_LENGTH_PART;
1305 keyinfo->extra_length+=HA_KEY_BLOB_LENGTH;
1306 key_part->store_length+=HA_KEY_BLOB_LENGTH;
1307 keyinfo->key_length+= HA_KEY_BLOB_LENGTH;
1309 if (i == 0 && key != local_primary_key)
1310 local_field->flags |= (((keyinfo->flags & HA_NOSAME) &&
1311 (keyinfo->key_parts == 1)) ?
1312 UNIQUE_KEY_FLAG : MULTIPLE_KEY_FLAG);
1314 local_field->key_start.set(key);
1315 if (local_field->key_length() == key_part->length &&
1316 !(local_field->flags & BLOB_FLAG))
1318 enum ha_key_alg algo= key_info[key].algorithm;
1319 if (db_type()->index_flags(algo) & HA_KEYREAD_ONLY)
1321 keys_for_keyread.set(key);
1322 local_field->part_of_key.set(key);
1323 local_field->part_of_key_not_clustered.set(key);
1325 if (db_type()->index_flags(algo) & HA_READ_ORDER)
1326 local_field->part_of_sortkey.set(key);
1328 if (!(key_part->key_part_flag & HA_REVERSE_SORT) &&
1331 local_field->flags|= PART_KEY_FLAG;
1332 if (key == local_primary_key)
1334 local_field->flags|= PRI_KEY_FLAG;
1339 if (storage_engine->check_flag(HTON_BIT_PRIMARY_KEY_IN_READ_INDEX))
1341 local_field->part_of_key= keys_in_use;
1342 if (local_field->part_of_sortkey.test(key))
1343 local_field->part_of_sortkey= keys_in_use;
1346 if (local_field->key_length() != key_part->length)
1348 key_part->key_part_flag|= HA_PART_KEY_SEG;
1351 keyinfo->usable_key_parts= usable_parts;
1353 set_if_bigger(max_key_length,keyinfo->key_length+
1354 keyinfo->key_parts);
1355 total_key_length+= keyinfo->key_length;
1357 if (keyinfo->flags & HA_NOSAME)
1359 set_if_bigger(max_unique_length,keyinfo->key_length);
1362 if (local_primary_key < MAX_KEY &&
1363 (keys_in_use.test(local_primary_key)))
1370 if (key_info[local_primary_key].key_parts == 1)
1372 Field *local_field= key_info[local_primary_key].key_part[0].field;
1373 if (local_field && local_field->result_type() == INT_RESULT)
1376 rowid_field_offset= (key_info[local_primary_key].key_part[0].
1383 if (found_next_number_field)
1385 Field *reg_field= *found_next_number_field;
1386 if ((
int) (next_number_index= (uint32_t)
1387 find_ref_key(key_info, keys,
1388 getDefaultValues(), reg_field,
1389 &next_number_key_offset,
1390 &next_number_keypart)) < 0)
1393 local_error= ER_NOT_FORM_FILE;
1399 reg_field->flags |= AUTO_INCREMENT_FLAG;
1406 blob_field.resize(blob_fields);
1407 uint32_t *save= &blob_field[0];
1409 for (Fields::iterator iter= _fields.begin(); iter != _fields.end()-1; iter++, k++)
1411 if ((*iter)->flags & BLOB_FLAG)
1417 all_set.resize(_field_size);
1420 return local_error != EE_OK;
1447 int TableShare::open_table_def(Session& session,
const identifier::Table &identifier)
1449 drizzled::error_t local_error= EE_OK;
1451 message::table::shared_ptr table= plugin::StorageEngine::getTableMessage(session, identifier, local_error);
1453 if (table and table->IsInitialized())
1455 if (parse_table_proto(session, *table))
1457 local_error= ER_CORRUPT_TABLE_DEFINITION_UNKNOWN;
1458 my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN, identifier);
1466 else if (table and not table->IsInitialized())
1468 local_error= ER_CORRUPT_TABLE_DEFINITION_UNKNOWN;
1469 my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN, identifier);
1473 local_error= ER_TABLE_UNKNOWN;
1474 my_error(ER_TABLE_UNKNOWN, identifier);
1477 return static_cast<int>(local_error);
1504 int TableShare::open_table_from_share(Session *session,
1505 const identifier::Table &identifier,
1507 uint32_t db_stat, uint32_t ha_open_flags,
1510 bool error_reported=
false;
1511 int ret= open_table_from_share_inner(session, alias, db_stat, outparam);
1514 ret= open_table_cursor_inner(identifier, db_stat, ha_open_flags, outparam, error_reported);
1519 if (not error_reported)
1520 open_table_error(ret, errno, 0);
1522 boost::checked_delete(outparam.cursor);
1524 outparam.db_stat= 0;
1525 outparam.mem().free_root(MYF(0));
1526 outparam.clearAlias();
1531 int TableShare::open_table_from_share_inner(Session *session,
const char *alias, uint32_t db_stat, Table &outparam)
1534 outparam.resetTable(session,
this, db_stat);
1536 outparam.setAlias(alias);
1539 if (not (outparam.cursor= db_type()->getCursor(outparam)))
1543 uint32_t records= 0;
1544 if (db_stat & HA_OPEN_KEYFILE)
1549 unsigned char* record= outparam.alloc(rec_buff_length * records);
1554 outparam.record[0]= outparam.record[1]= getDefaultValues();
1558 outparam.record[0]= record;
1560 outparam.record[1]= record+ rec_buff_length;
1562 outparam.record[1]= outparam.getInsertRecord();
1565 #ifdef HAVE_VALGRIND
1572 memcpy(outparam.getInsertRecord(), getDefaultValues(), rec_buff_length);
1573 memcpy(outparam.getUpdateRecord(), getDefaultValues(), null_bytes);
1575 memcpy(outparam.getUpdateRecord(), getDefaultValues(), rec_buff_length);
1580 memcpy(outparam.getUpdateRecord(), getDefaultValues(), null_bytes);
1583 Field** field_ptr =
new (outparam.mem()) Field*[_field_size + 1];
1585 outparam.setFields(field_ptr);
1587 record= outparam.getInsertRecord()-1;
1589 outparam.null_flags= (
unsigned char*) record+1;
1592 for (uint32_t i= 0 ; i < _field_size; i++, field_ptr++)
1594 if (!((*field_ptr)= _fields[i]->clone(&outparam.mem(), &outparam)))
1599 if (found_next_number_field)
1600 outparam.found_next_number_field=
1601 outparam.getField(positionFields(found_next_number_field));
1602 if (timestamp_field)
1603 outparam.timestamp_field= (field::Epoch*) outparam.getField(timestamp_field->position());
1608 uint32_t n_length= keys *
sizeof(KeyInfo) + key_parts *
sizeof(KeyPartInfo);
1609 KeyInfo* local_key_info= (KeyInfo*) outparam.alloc(n_length);
1610 outparam.key_info= local_key_info;
1611 KeyPartInfo* key_part=
reinterpret_cast<KeyPartInfo*
>(local_key_info+keys);
1613 memcpy(local_key_info, key_info,
sizeof(*local_key_info)*keys);
1614 memcpy(key_part, key_info[0].key_part,
sizeof(*key_part) * key_parts);
1616 for (KeyInfo* key_info_end= local_key_info + keys; local_key_info < key_info_end; local_key_info++)
1618 local_key_info->table= &outparam;
1619 local_key_info->key_part= key_part;
1621 for (KeyPartInfo* key_part_end= key_part+ local_key_info->key_parts; key_part < key_part_end; key_part++)
1623 Field *local_field= key_part->field= outparam.getField(key_part->fieldnr-1);
1625 if (local_field->key_length() != key_part->length && not (local_field->flags & BLOB_FLAG))
1631 local_field= key_part->field= local_field->new_field(&outparam.mem(), &outparam, 0);
1632 local_field->field_length= key_part->length;
1640 outparam.def_read_set.resize(_field_size);
1641 outparam.def_write_set.resize(_field_size);
1642 outparam.tmp_set.resize(_field_size);
1643 outparam.default_column_bitmaps();
1648 int TableShare::open_table_cursor_inner(
const identifier::Table &identifier,
1649 uint32_t db_stat, uint32_t ha_open_flags,
1651 bool &error_reported)
1657 assert(!(db_stat & HA_WAIT_IF_LOCKED));
1660 if ((ha_err= (outparam.cursor->ha_open(identifier,
1661 (db_stat & HA_READ_ONLY ? O_RDONLY : O_RDWR),
1662 (db_stat & HA_OPEN_TEMPORARY ? HA_OPEN_TMP_TABLE : HA_OPEN_IGNORE_IF_LOCKED) | ha_open_flags))))
1666 case HA_ERR_NO_SUCH_TABLE:
1683 outparam.print_error(ha_err, MYF(0));
1684 error_reported=
true;
1685 if (ha_err == HA_ERR_TABLE_DEF_CHANGED)
1697 void TableShare::open_table_error(
int pass_error,
int db_errno,
int pass_errarg)
1699 char buff[FN_REFLEN];
1700 myf errortype= ME_ERROR+ME_WAITTANG;
1702 switch (pass_error) {
1705 if (db_errno == ENOENT)
1707 my_error(ER_TABLE_UNKNOWN, *table_identifier);
1711 snprintf(buff,
sizeof(buff),
"%s",normalized_path.data());
1712 my_error((db_errno == EMFILE) ? ER_CANT_OPEN_FILE : ER_FILE_NOT_FOUND, errortype, buff, db_errno);
1717 drizzled::error_t err_no= (db_errno == ENOENT) ? ER_FILE_NOT_FOUND : (db_errno == EAGAIN) ? ER_FILE_USED : ER_CANT_OPEN_FILE;
1718 my_error(err_no, errortype, normalized_path.data(), db_errno);
1723 const char *csname= get_charset_name((uint32_t) pass_errarg);
1725 if (!csname || csname[0] ==
'?')
1727 snprintf(tmp,
sizeof(tmp),
"#%d", pass_errarg);
1730 my_printf_error(ER_UNKNOWN_COLLATION, _(
"Unknown collation '%s' in table '%-.64s' definition"), MYF(0), csname, table_identifier->getTableName().c_str());
1734 snprintf(buff,
sizeof(buff),
"%s", normalized_path.data());
1735 my_printf_error(ER_NOT_FORM_FILE, _(
"Table '%-.64s' was created with a different version of Drizzle and cannot be read"), MYF(0), buff);
1741 snprintf(buff,
sizeof(buff),
"%s", normalized_path.data());
1742 my_error(ER_NOT_FORM_FILE, errortype, buff, 0);
1748 Field *TableShare::make_field(
const message::Table::Field &pfield,
1750 uint32_t field_length,
1752 unsigned char *null_pos,
1753 unsigned char null_bit,
1755 enum_field_types field_type,
1756 const charset_info_st * field_charset,
1757 Field::utype unireg_check,
1759 const char *field_name)
1761 return make_field(pfield,
1773 pfield.constraints().is_unsigned());
1776 Field *TableShare::make_field(
const message::Table::Field &,
1778 uint32_t field_length,
1780 unsigned char *null_pos,
1781 unsigned char null_bit,
1783 enum_field_types field_type,
1784 const charset_info_st * field_charset,
1785 Field::utype unireg_check,
1787 const char *field_name,
1797 null_bit= ((
unsigned char) 1) << null_bit;
1802 case DRIZZLE_TYPE_ENUM:
1803 return new (&mem_root) Field_enum(ptr,
1810 case DRIZZLE_TYPE_VARCHAR:
1812 return new (&mem_root) Field_varstring(ptr,field_length,
1813 ha_varchar_packlength(field_length),
1817 case DRIZZLE_TYPE_BLOB:
1818 return new (&mem_root) Field_blob(ptr,
1824 case DRIZZLE_TYPE_DECIMAL:
1825 return new (&mem_root) Field_decimal(ptr,
1832 case DRIZZLE_TYPE_DOUBLE:
1833 return new (&mem_root) Field_double(ptr,
1842 case DRIZZLE_TYPE_UUID:
1843 return new (&mem_root) field::Uuid(ptr,
1848 case DRIZZLE_TYPE_IPV6:
1849 return new (&mem_root) field::IPv6(ptr,
1854 case DRIZZLE_TYPE_BOOLEAN:
1855 return new (&mem_root) field::Boolean(ptr,
1861 case DRIZZLE_TYPE_LONG:
1862 return new (&mem_root) field::Int32(ptr,
1868 case DRIZZLE_TYPE_LONGLONG:
1872 return new (&mem_root) field::Size(ptr,
1880 return new (&mem_root) field::Int64(ptr,
1887 case DRIZZLE_TYPE_MICROTIME:
1888 return new (&mem_root) field::Microtime(ptr,
1894 case DRIZZLE_TYPE_TIMESTAMP:
1895 return new (&mem_root) field::Epoch(ptr,
1901 case DRIZZLE_TYPE_TIME:
1902 return new (&mem_root) field::Time(ptr, field_length, null_pos, null_bit, field_name);
1903 case DRIZZLE_TYPE_DATE:
1904 return new (&mem_root) Field_date(ptr, null_pos, null_bit, field_name);
1905 case DRIZZLE_TYPE_DATETIME:
1906 return new (&mem_root) Field_datetime(ptr, null_pos, null_bit, field_name);
1907 case DRIZZLE_TYPE_NULL:
1908 return new (&mem_root) Field_null(ptr, field_length, field_name);
1914 void TableShare::refreshVersion()
1916 version= g_refresh_version;