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>
94 extern size_t table_def_size;
96 static enum_field_types proto_field_type_to_drizzle_type(
const message::Table::Field &field)
100 case message::Table::Field::INTEGER:
101 return DRIZZLE_TYPE_LONG;
103 case message::Table::Field::DOUBLE:
104 return DRIZZLE_TYPE_DOUBLE;
106 case message::Table::Field::EPOCH:
107 if (field.has_time_options() and field.time_options().microseconds())
108 return DRIZZLE_TYPE_MICROTIME;
110 return DRIZZLE_TYPE_TIMESTAMP;
112 case message::Table::Field::BIGINT:
113 return DRIZZLE_TYPE_LONGLONG;
115 case message::Table::Field::DATETIME:
116 return DRIZZLE_TYPE_DATETIME;
118 case message::Table::Field::DATE:
119 return DRIZZLE_TYPE_DATE;
121 case message::Table::Field::VARCHAR:
122 return DRIZZLE_TYPE_VARCHAR;
124 case message::Table::Field::DECIMAL:
125 return DRIZZLE_TYPE_DECIMAL;
127 case message::Table::Field::ENUM:
128 return DRIZZLE_TYPE_ENUM;
130 case message::Table::Field::BLOB:
131 return DRIZZLE_TYPE_BLOB;
133 case message::Table::Field::UUID:
134 return DRIZZLE_TYPE_UUID;
136 case message::Table::Field::IPV6:
137 return DRIZZLE_TYPE_IPV6;
139 case message::Table::Field::BOOLEAN:
140 return DRIZZLE_TYPE_BOOLEAN;
142 case message::Table::Field::TIME:
143 return DRIZZLE_TYPE_TIME;
149 static Item* default_value_item(enum_field_types field_type,
const charset_info_st& charset,
bool default_null,
150 const string& default_value,
const string& default_bin_value)
153 return new Item_null();
157 case DRIZZLE_TYPE_LONG:
158 case DRIZZLE_TYPE_LONGLONG:
161 Item* default_item=
new Item_int(default_value.c_str(), (int64_t) internal::my_strtoll10(default_value.c_str(), NULL, &error), default_value.length());
163 if (error && error != -1)
170 case DRIZZLE_TYPE_DOUBLE:
171 return new Item_float(default_value.c_str(), default_value.length());
172 case DRIZZLE_TYPE_NULL:
175 case DRIZZLE_TYPE_TIMESTAMP:
176 case DRIZZLE_TYPE_DATETIME:
177 case DRIZZLE_TYPE_TIME:
178 case DRIZZLE_TYPE_DATE:
179 case DRIZZLE_TYPE_ENUM:
180 case DRIZZLE_TYPE_UUID:
181 case DRIZZLE_TYPE_IPV6:
182 case DRIZZLE_TYPE_MICROTIME:
183 case DRIZZLE_TYPE_BOOLEAN:
185 return new Item_string(default_value.data(), default_value.size(), system_charset_info);
186 case DRIZZLE_TYPE_VARCHAR:
187 case DRIZZLE_TYPE_BLOB:
188 return &charset== &my_charset_bin
189 ?
new Item_string(default_bin_value, &my_charset_bin)
190 : new Item_string(default_value, system_charset_info);
191 case DRIZZLE_TYPE_DECIMAL:
192 return new Item_decimal(default_value.c_str(), default_value.length(), system_charset_info);
204 bool TableShare::fieldInPrimaryKey(
Field *in_field)
const
206 assert(getTableMessage());
208 size_t num_indexes= getTableMessage()->indexes_size();
210 for (
size_t x= 0; x < num_indexes; ++x)
213 if (index.is_primary())
215 size_t num_parts= index.index_part_size();
216 for (
size_t y= 0; y < num_parts; ++y)
218 if (index.index_part(y).fieldnr() == in_field->position())
226 TableShare::TableShare(
const identifier::Table::Type type_arg) :
228 found_next_number_field(NULL),
229 timestamp_field(NULL),
231 mem_root(TABLE_ALLOC_BLOCK_SIZE),
237 stored_rec_length(0),
239 _table_message(NULL),
240 storage_engine(NULL),
244 last_null_bit_pos(0),
250 max_unique_length(0),
255 has_variable_width(false),
256 db_create_options(0),
257 db_options_in_use(0),
259 rowid_field_offset(0),
260 primary_key(MAX_KEY),
261 next_number_index(0),
262 next_number_key_offset(0),
263 next_number_keypart(0),
267 blob_ptr_size(portable_sizeof_char_ptr),
268 db_low_byte_first(false),
272 if (type_arg == message::Table::INTERNAL)
274 string s= identifier::Table::build_tmptable_filename();
275 private_key_for_cache.vectorPtr().assign(s.c_str(), s.c_str() + s.size() + 1);
276 init(private_key_for_cache.vector(), private_key_for_cache.vector());
284 TableShare::TableShare(
const identifier::Table &identifier,
const identifier::Table::Key &key) :
286 found_next_number_field(NULL),
287 timestamp_field(NULL),
289 mem_root(TABLE_ALLOC_BLOCK_SIZE),
296 stored_rec_length(0),
298 _table_message(NULL),
299 storage_engine(NULL),
300 tmp_table(message::Table::INTERNAL),
303 last_null_bit_pos(0),
309 max_unique_length(0),
314 has_variable_width(false),
315 db_create_options(0),
316 db_options_in_use(0),
318 rowid_field_offset(0),
319 primary_key(MAX_KEY),
320 next_number_index(0),
321 next_number_key_offset(0),
322 next_number_keypart(0),
326 blob_ptr_size(portable_sizeof_char_ptr),
327 db_low_byte_first(false),
331 assert(identifier.getKey() == key);
333 private_key_for_cache= key;
336 tmp_table= message::Table::INTERNAL;
338 db=
str_ref(private_key_for_cache.schema_name());
339 table_name=
str_ref(private_key_for_cache.table_name());
343 std::string tb_name(identifier.getTableName());
344 boost::to_lower(tb_name);
345 assert(strcmp(tb_name.c_str(), table_name.data()) == 0);
348 TableShare::TableShare(
const identifier::Table &identifier) :
350 found_next_number_field(NULL),
351 timestamp_field(NULL),
353 mem_root(TABLE_ALLOC_BLOCK_SIZE),
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 private_key_for_cache= identifier.getKey();
396 assert(identifier.getPath().size());
397 private_normalized_path.resize(identifier.getPath().size() + 1);
398 memcpy(&private_normalized_path[0], identifier.getPath().c_str(), identifier.getPath().size());
402 tmp_table= message::Table::INTERNAL;
403 db=
str_ref(private_key_for_cache.vector());
404 table_name=
str_ref(db.data() + 1);
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),
429 stored_rec_length(0),
431 _table_message(NULL),
432 storage_engine(NULL),
436 last_null_bit_pos(0),
442 max_unique_length(0),
447 has_variable_width(false),
448 db_create_options(0),
449 db_options_in_use(0),
451 rowid_field_offset(0),
452 primary_key(MAX_KEY),
453 next_number_index(0),
454 next_number_key_offset(0),
455 next_number_keypart(0),
459 blob_ptr_size(portable_sizeof_char_ptr),
460 db_low_byte_first(false),
464 private_key_for_cache= identifier.getKey();
469 db=
str_ref(private_key_for_cache.schema_name());
470 table_name=
str_ref(private_key_for_cache.table_name());
475 _path.assign(path_arg, path_length_arg);
479 _path= identifier::Table::build_table_filename(db.data(), table_name.data(),
false);
482 char* path_buff= mem_root.
strdup(_path);
483 path=
str_ref(path_buff, _path.length());
484 normalized_path=
str_ref(path_buff, _path.length());
486 version= g_refresh_version;
489 void TableShare::init(
const char *new_table_name,
const char *new_path)
492 tmp_table= message::Table::INTERNAL;
494 table_name=
str_ref(new_table_name);
496 normalized_path=
str_ref(new_path);
499 TableShare::~TableShare()
501 storage_engine= NULL;
506 void TableShare::setIdentifier(
const identifier::Table &identifier_arg)
508 private_key_for_cache= identifier_arg.getKey();
514 db=
str_ref(private_key_for_cache.schema_name());
515 table_name=
str_ref(private_key_for_cache.table_name());
517 getTableMessage()->set_name(identifier_arg.getTableName());
518 getTableMessage()->set_schema(identifier_arg.getSchemaName());
521 bool TableShare::parse_table_proto(Session& session,
const message::Table &table)
523 drizzled::error_t local_error= EE_OK;
525 if (! table.IsInitialized())
527 my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
528 table.name().empty() ?
" " : table.name().c_str(),
529 table.InitializationErrorString().c_str());
531 return ER_CORRUPT_TABLE_DEFINITION;
534 setTableMessage(table);
536 storage_engine= plugin::StorageEngine::findByName(session, table.engine().name());
537 assert(storage_engine);
539 message::Table::TableOptions table_options;
541 if (table.has_options())
542 table_options= table.options();
544 uint32_t local_db_create_options= 0;
546 if (table_options.pack_record())
547 local_db_create_options|= HA_OPTION_PACK_RECORD;
552 db_create_options= (local_db_create_options & 0x0000FFFF);
553 db_options_in_use= db_create_options;
555 block_size= table_options.has_block_size() ? table_options.block_size() : 0;
557 table_charset= get_charset(table_options.collation_id());
559 if (not table_charset)
561 my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN_COLLATION, MYF(0),
562 table_options.collation().c_str(),
563 table.name().c_str());
565 return ER_CORRUPT_TABLE_DEFINITION;
570 keys= table.indexes_size();
573 for (
int indx= 0; indx < table.indexes_size(); indx++)
574 key_parts+= table.indexes(indx).index_part_size();
576 key_info= (KeyInfo*) mem().
alloc(table.indexes_size() *
sizeof(KeyInfo) +key_parts*
sizeof(KeyPartInfo));
578 KeyPartInfo *key_part;
580 key_part=
reinterpret_cast<KeyPartInfo*
>
581 (key_info+table.indexes_size());
584 ulong *rec_per_key= (ulong*) mem().
alloc(
sizeof(ulong*)*key_parts);
586 KeyInfo* keyinfo= key_info;
587 for (
int keynr= 0; keynr < table.indexes_size(); keynr++, keyinfo++)
589 message::Table::Index indx= table.indexes(keynr);
594 if (indx.is_unique())
595 keyinfo->flags|= HA_NOSAME;
597 if (indx.has_options())
599 message::Table::Index::Options indx_options= indx.options();
600 if (indx_options.pack_key())
601 keyinfo->flags|= HA_PACK_KEY;
603 if (indx_options.var_length_key())
604 keyinfo->flags|= HA_VAR_LENGTH_PART;
606 if (indx_options.null_part_key())
607 keyinfo->flags|= HA_NULL_PART_KEY;
609 if (indx_options.binary_pack_key())
610 keyinfo->flags|= HA_BINARY_PACK_KEY;
612 if (indx_options.has_partial_segments())
613 keyinfo->flags|= HA_KEY_HAS_PART_KEY_SEG;
615 if (indx_options.auto_generated_key())
616 keyinfo->flags|= HA_GENERATED_KEY;
618 if (indx_options.has_key_block_size())
620 keyinfo->flags|= HA_USES_BLOCK_SIZE;
621 keyinfo->block_size= indx_options.key_block_size();
625 keyinfo->block_size= 0;
631 case message::Table::Index::UNKNOWN_INDEX:
632 keyinfo->algorithm= HA_KEY_ALG_UNDEF;
634 case message::Table::Index::BTREE:
635 keyinfo->algorithm= HA_KEY_ALG_BTREE;
637 case message::Table::Index::HASH:
638 keyinfo->algorithm= HA_KEY_ALG_HASH;
643 keyinfo->algorithm= HA_KEY_ALG_UNDEF;
647 keyinfo->key_length= indx.key_length();
649 keyinfo->key_parts= indx.index_part_size();
651 keyinfo->key_part= key_part;
652 keyinfo->rec_per_key= rec_per_key;
654 for (
unsigned int partnr= 0;
655 partnr < keyinfo->key_parts;
656 partnr++, key_part++)
658 message::Table::Index::IndexPart part;
659 part= indx.index_part(partnr);
663 key_part->field= NULL;
664 key_part->fieldnr= part.fieldnr() + 1;
665 key_part->null_bit= 0;
669 key_part->key_part_flag= 0;
670 if (part.has_in_reverse_order())
671 key_part->key_part_flag= part.in_reverse_order()? HA_REVERSE_SORT : 0;
673 key_part->length= part.compare_length();
677 if (table.field(part.fieldnr()).type() == message::Table::Field::VARCHAR
678 || table.field(part.fieldnr()).type() == message::Table::Field::BLOB)
680 uint32_t collation_id;
682 if (table.field(part.fieldnr()).string_options().has_collation_id())
683 collation_id= table.field(part.fieldnr()).string_options().collation_id();
685 collation_id= table.options().collation_id();
687 const charset_info_st *cs= get_charset(collation_id);
689 mbmaxlen= cs->mbmaxlen;
691 key_part->length*= mbmaxlen;
693 key_part->store_length= key_part->length;
696 key_part->key_type= 0;
699 if (not indx.has_comment())
701 keyinfo->comment.clear();
705 keyinfo->flags|= HA_USES_COMMENT;
706 keyinfo->comment.assign(mem().strdup(indx.comment()), indx.comment().length());
709 keyinfo->name= mem().
strdup(indx.name());
711 addKeyName(
string(keyinfo->name, indx.name().length()));
714 keys_for_keyread.reset();
715 set_prefix(keys_in_use, keys);
717 _field_size= table.field_size();
719 setFields(_field_size + 1);
720 _fields[_field_size]= NULL;
722 uint32_t local_null_fields= 0;
725 std::vector<uint32_t> field_offsets;
726 std::vector<uint32_t> field_pack_length;
728 field_offsets.resize(_field_size);
729 field_pack_length.resize(_field_size);
731 uint32_t interval_count= 0;
732 uint32_t interval_parts= 0;
734 uint32_t stored_columns_reclength= 0;
736 for (
unsigned int fieldnr= 0; fieldnr < _field_size; fieldnr++)
738 message::Table::Field pfield= table.field(fieldnr);
739 if (pfield.constraints().is_nullable())
743 else if (not pfield.constraints().is_notnull())
748 enum_field_types drizzle_field_type= proto_field_type_to_drizzle_type(pfield);
750 field_offsets[fieldnr]= stored_columns_reclength;
756 switch(drizzle_field_type)
758 case DRIZZLE_TYPE_BLOB:
759 case DRIZZLE_TYPE_VARCHAR:
761 message::Table::Field::StringFieldOptions field_options= pfield.string_options();
763 const charset_info_st *cs= get_charset(field_options.has_collation_id() ?
764 field_options.collation_id() : 0);
767 cs= default_charset_info;
769 field_pack_length[fieldnr]= calc_pack_length(drizzle_field_type,
770 field_options.length() * cs->mbmaxlen);
773 case DRIZZLE_TYPE_ENUM:
775 message::Table::Field::EnumerationValues field_options= pfield.enumeration_values();
777 field_pack_length[fieldnr]= 4;
780 interval_parts+= field_options.field_value_size();
783 case DRIZZLE_TYPE_DECIMAL:
785 message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
787 field_pack_length[fieldnr]= class_decimal_get_binary_size(fo.precision(), fo.scale());
792 field_pack_length[fieldnr]= calc_pack_length(drizzle_field_type, 0);
795 reclength+= field_pack_length[fieldnr];
796 stored_columns_reclength+= field_pack_length[fieldnr];
800 stored_rec_length= stored_columns_reclength;
802 null_fields= local_null_fields;
804 ulong null_bits= local_null_fields;
805 if (! table_options.pack_record())
807 ulong data_offset= (null_bits + 7)/8;
810 reclength+= data_offset;
811 stored_rec_length+= data_offset;
813 ulong local_rec_buff_length;
815 local_rec_buff_length= ALIGN_SIZE(reclength + 1);
816 rec_buff_length= local_rec_buff_length;
818 resizeDefaultValues(local_rec_buff_length);
819 unsigned char* record= getDefaultValues();
822 if (! table_options.pack_record())
829 intervals.resize(interval_count);
835 uint32_t interval_nr= 0;
837 for (
unsigned int fieldnr= 0; fieldnr < _field_size; fieldnr++)
839 message::Table::Field pfield= table.field(fieldnr);
842 if (pfield.type() != message::Table::Field::ENUM)
845 message::Table::Field::EnumerationValues field_options= pfield.enumeration_values();
847 if (field_options.field_value_size() > Field_enum::max_supported_elements)
849 my_error(ER_CORRUPT_TABLE_DEFINITION_ENUM, MYF(0), table.name().c_str());
851 return ER_CORRUPT_TABLE_DEFINITION_ENUM;
855 const charset_info_st *charset= get_charset(field_options.has_collation_id() ?
856 field_options.collation_id() : 0);
859 charset= default_charset_info;
861 TYPELIB *t= (&intervals[interval_nr]);
863 t->type_names= (
const char**)mem().
alloc((field_options.field_value_size() + 1) *
sizeof(
char*));
864 t->type_lengths= (
unsigned int*)mem().
alloc((field_options.field_value_size() + 1) *
sizeof(
unsigned int));
866 t->type_names[field_options.field_value_size()]= NULL;
867 t->type_lengths[field_options.field_value_size()]= 0;
869 t->count= field_options.field_value_size();
872 for (
int n= 0; n < field_options.field_value_size(); n++)
874 t->type_names[n]= mem().
strdup(field_options.field_value(n));
880 t->type_lengths[n]= charset->cset->lengthsp(charset, t->type_names[n], field_options.field_value(n).length());
889 bool use_hash= _field_size >= MAX_FIELDS_BEFORE_HASH;
891 unsigned char* null_pos= getDefaultValues();
892 int null_bit_pos= (table_options.pack_record()) ? 0 : 1;
894 for (
unsigned int fieldnr= 0; fieldnr < _field_size; fieldnr++)
896 message::Table::Field pfield= table.field(fieldnr);
898 Field::utype unireg_type= Field::NONE;
900 if (pfield.has_numeric_options() &&
901 pfield.numeric_options().is_autoincrement())
903 unireg_type= Field::NEXT_NUMBER;
906 if (pfield.has_options() &&
907 pfield.options().has_default_expression() &&
908 pfield.options().default_expression().compare(
"CURRENT_TIMESTAMP") == 0)
910 if (pfield.options().has_update_expression() &&
911 pfield.options().update_expression().compare(
"CURRENT_TIMESTAMP") == 0)
913 unireg_type= Field::TIMESTAMP_DNUN_FIELD;
915 else if (! pfield.options().has_update_expression())
917 unireg_type= Field::TIMESTAMP_DN_FIELD;
925 else if (pfield.has_options() &&
926 pfield.options().has_update_expression() &&
927 pfield.options().update_expression().compare(
"CURRENT_TIMESTAMP") == 0)
929 unireg_type= Field::TIMESTAMP_UN_FIELD;
933 if (pfield.has_comment())
935 comment.assign(mem().strdup(pfield.comment()), pfield.comment().size());
938 enum_field_types field_type;
940 field_type= proto_field_type_to_drizzle_type(pfield);
942 const charset_info_st *charset= &my_charset_bin;
944 if (field_type == DRIZZLE_TYPE_BLOB || field_type == DRIZZLE_TYPE_VARCHAR)
946 message::Table::Field::StringFieldOptions field_options= pfield.string_options();
948 charset= get_charset(field_options.has_collation_id() ? field_options.collation_id() : 0);
951 charset= default_charset_info;
954 if (field_type == DRIZZLE_TYPE_ENUM)
956 message::Table::Field::EnumerationValues field_options= pfield.enumeration_values();
958 charset= get_charset(field_options.has_collation_id() ? field_options.collation_id() : 0);
961 charset= default_charset_info;
965 if (field_type == DRIZZLE_TYPE_DECIMAL || field_type == DRIZZLE_TYPE_DOUBLE)
967 message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
969 if (not pfield.has_numeric_options() || ! fo.has_scale())
975 decimals= NOT_FIXED_DEC;
979 if (fo.scale() > DECIMAL_MAX_SCALE)
981 local_error= ER_NOT_FORM_FILE;
985 decimals=
static_cast<uint8_t
>(fo.scale());
989 Item *default_value= NULL;
991 if (pfield.options().has_default_value() ||
992 pfield.options().default_null() ||
993 pfield.options().has_default_bin_value())
995 default_value= default_value_item(field_type, *charset, pfield.options().default_null(), pfield.options().default_value(), pfield.options().default_bin_value());
996 if (default_value == NULL)
998 my_error(ER_INVALID_DEFAULT, MYF(0), pfield.name().c_str());
1004 uint32_t field_length= 0;
1009 case DRIZZLE_TYPE_BLOB:
1010 case DRIZZLE_TYPE_VARCHAR:
1012 message::Table::Field::StringFieldOptions field_options= pfield.string_options();
1014 charset= get_charset(field_options.has_collation_id() ?
1015 field_options.collation_id() : 0);
1018 charset= default_charset_info;
1020 field_length= field_options.length() * charset->mbmaxlen;
1023 case DRIZZLE_TYPE_DOUBLE:
1025 message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
1026 if (!fo.has_precision() && !fo.has_scale())
1028 field_length= DBL_DIG+7;
1032 field_length= fo.precision();
1034 if (field_length < decimals &&
1035 decimals != NOT_FIXED_DEC)
1037 my_error(ER_M_BIGGER_THAN_D, MYF(0), pfield.name().c_str());
1038 local_error= ER_M_BIGGER_THAN_D;
1043 case DRIZZLE_TYPE_DECIMAL:
1045 message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
1047 field_length= class_decimal_precision_to_length(fo.precision(), fo.scale(),
1051 case DRIZZLE_TYPE_DATETIME:
1054 case DRIZZLE_TYPE_DATE:
1057 case DRIZZLE_TYPE_ENUM:
1061 message::Table::Field::EnumerationValues fo= pfield.enumeration_values();
1063 for (
int valnr= 0; valnr < fo.field_value_size(); valnr++)
1065 if (fo.field_value(valnr).length() > field_length)
1067 field_length= charset->cset->numchars(charset,
1068 fo.field_value(valnr).c_str(),
1069 fo.field_value(valnr).c_str()
1070 + fo.field_value(valnr).length())
1071 * charset->mbmaxlen;
1076 case DRIZZLE_TYPE_LONG:
1078 uint32_t sign_len= pfield.constraints().is_unsigned() ? 0 : 1;
1079 field_length= MAX_INT_WIDTH+sign_len;
1082 case DRIZZLE_TYPE_LONGLONG:
1084 uint32_t sign_len= pfield.constraints().is_unsigned() ? 0 : 1;
1085 field_length= MAX_BIGINT_WIDTH+sign_len;
1088 case DRIZZLE_TYPE_UUID:
1089 field_length= field::Uuid::max_string_length();
1091 case DRIZZLE_TYPE_IPV6:
1092 field_length= field::IPv6::max_string_length();
1094 case DRIZZLE_TYPE_BOOLEAN:
1095 field_length= field::Boolean::max_string_length();
1097 case DRIZZLE_TYPE_MICROTIME:
1098 field_length= field::Microtime::max_string_length();
1100 case DRIZZLE_TYPE_TIMESTAMP:
1101 field_length= field::Epoch::max_string_length();
1103 case DRIZZLE_TYPE_TIME:
1104 field_length= field::Time::max_string_length();
1106 case DRIZZLE_TYPE_NULL:
1110 bool is_not_null=
false;
1112 if (not pfield.constraints().is_nullable())
1116 else if (pfield.constraints().is_notnull())
1121 Field* f= make_field(pfield,
1122 record + field_offsets[fieldnr] + data_offset,
1130 MTYP_TYPENR(unireg_type),
1131 ((field_type == DRIZZLE_TYPE_ENUM) ? &intervals[interval_nr++] : (TYPELIB*) 0),
1132 getTableMessage()->field(fieldnr).name().c_str());
1134 _fields[fieldnr]= f;
1139 case DRIZZLE_TYPE_BLOB:
1140 case DRIZZLE_TYPE_VARCHAR:
1141 case DRIZZLE_TYPE_DOUBLE:
1142 case DRIZZLE_TYPE_DECIMAL:
1143 case DRIZZLE_TYPE_TIMESTAMP:
1144 case DRIZZLE_TYPE_TIME:
1145 case DRIZZLE_TYPE_DATETIME:
1146 case DRIZZLE_TYPE_MICROTIME:
1147 case DRIZZLE_TYPE_DATE:
1148 case DRIZZLE_TYPE_ENUM:
1149 case DRIZZLE_TYPE_LONG:
1150 case DRIZZLE_TYPE_LONGLONG:
1151 case DRIZZLE_TYPE_NULL:
1152 case DRIZZLE_TYPE_UUID:
1153 case DRIZZLE_TYPE_IPV6:
1154 case DRIZZLE_TYPE_BOOLEAN:
1160 table::Shell temp_table(*
this);
1161 temp_table.in_use= &session;
1163 f->init(&temp_table);
1165 if (! (f->flags & NOT_NULL_FLAG))
1167 *f->null_ptr|= f->null_bit;
1168 if (! (null_bit_pos= (null_bit_pos + 1) & 7))
1175 enum_check_fields old_count_cuted_fields= session.count_cuted_fields;
1176 session.count_cuted_fields= CHECK_FIELD_ERROR_FOR_NULL;
1177 int res= default_value->save_in_field(f, 1);
1178 session.count_cuted_fields= old_count_cuted_fields;
1179 if (res != 0 && res != 3)
1181 my_error(ER_INVALID_DEFAULT, MYF(0), f->field_name);
1182 local_error= ER_INVALID_DEFAULT;
1187 else if (f->real_type() == DRIZZLE_TYPE_ENUM && (f->flags & NOT_NULL_FLAG))
1190 f->store((int64_t) 1,
true);
1199 f->orig_table= NULL;
1201 f->setPosition(fieldnr);
1202 f->comment= comment;
1203 if (not default_value &&
1204 not (f->unireg_check==Field::NEXT_NUMBER) &&
1205 (f->flags & NOT_NULL_FLAG) &&
1206 (not f->is_timestamp()))
1208 f->flags|= NO_DEFAULT_VALUE_FLAG;
1211 if (f->unireg_check == Field::NEXT_NUMBER)
1212 found_next_number_field= &(_fields[fieldnr]);
1216 const char *local_field_name= _fields[fieldnr]->field_name;
1217 name_hash.insert(make_pair(local_field_name, &(_fields[fieldnr])));
1222 for (
unsigned int keynr= 0; keynr < keys; keynr++, keyinfo++)
1224 key_part= keyinfo->key_part;
1226 for (
unsigned int partnr= 0;
1227 partnr < keyinfo->key_parts;
1228 partnr++, key_part++)
1235 key_part->offset= field_offsets[key_part->fieldnr-1] + data_offset;
1244 *(record + null_count / 8)|= ~(((
unsigned char) 1 << (null_count & 7)) - 1);
1246 null_bytes= (null_pos - (
unsigned char*) record + (null_bit_pos + 7) / 8);
1248 last_null_bit_pos= null_bit_pos;
1253 uint32_t local_primary_key= doesKeyNameExist(
"PRIMARY");
1255 key_part= keyinfo->key_part;
1257 for (uint32_t key= 0; key < keys; key++,keyinfo++)
1259 uint32_t usable_parts= 0;
1261 if (local_primary_key >= MAX_KEY && (keyinfo->flags & HA_NOSAME))
1267 local_primary_key=key;
1268 for (uint32_t i= 0; i < keyinfo->key_parts; i++)
1270 uint32_t fieldnr= key_part[i].fieldnr;
1272 _fields[fieldnr-1]->null_ptr ||
1273 _fields[fieldnr-1]->key_length() != key_part[i].length)
1275 local_primary_key= MAX_KEY;
1281 for (uint32_t i= 0 ; i < keyinfo->key_parts ; key_part++,i++)
1284 if (! key_part->fieldnr)
1288 local_field= key_part->field= _fields[key_part->fieldnr-1];
1289 key_part->type= local_field->key_type();
1290 if (local_field->null_ptr)
1292 key_part->null_offset=(uint32_t) ((
unsigned char*) local_field->null_ptr - getDefaultValues());
1293 key_part->null_bit= local_field->null_bit;
1294 key_part->store_length+=HA_KEY_NULL_LENGTH;
1295 keyinfo->flags|=HA_NULL_PART_KEY;
1296 keyinfo->extra_length+= HA_KEY_NULL_LENGTH;
1297 keyinfo->key_length+= HA_KEY_NULL_LENGTH;
1299 if (local_field->type() == DRIZZLE_TYPE_BLOB ||
1300 local_field->real_type() == DRIZZLE_TYPE_VARCHAR)
1302 if (local_field->type() == DRIZZLE_TYPE_BLOB)
1303 key_part->key_part_flag|= HA_BLOB_PART;
1305 key_part->key_part_flag|= HA_VAR_LENGTH_PART;
1306 keyinfo->extra_length+=HA_KEY_BLOB_LENGTH;
1307 key_part->store_length+=HA_KEY_BLOB_LENGTH;
1308 keyinfo->key_length+= HA_KEY_BLOB_LENGTH;
1310 if (i == 0 && key != local_primary_key)
1311 local_field->flags |= (((keyinfo->flags & HA_NOSAME) &&
1312 (keyinfo->key_parts == 1)) ?
1313 UNIQUE_KEY_FLAG : MULTIPLE_KEY_FLAG);
1315 local_field->key_start.set(key);
1316 if (local_field->key_length() == key_part->length &&
1317 !(local_field->flags & BLOB_FLAG))
1319 enum ha_key_alg algo= key_info[key].algorithm;
1320 if (db_type()->index_flags(algo) & HA_KEYREAD_ONLY)
1322 keys_for_keyread.set(key);
1323 local_field->part_of_key.set(key);
1324 local_field->part_of_key_not_clustered.set(key);
1326 if (db_type()->index_flags(algo) & HA_READ_ORDER)
1327 local_field->part_of_sortkey.set(key);
1329 if (!(key_part->key_part_flag & HA_REVERSE_SORT) &&
1332 local_field->flags|= PART_KEY_FLAG;
1333 if (key == local_primary_key)
1335 local_field->flags|= PRI_KEY_FLAG;
1340 if (storage_engine->check_flag(HTON_BIT_PRIMARY_KEY_IN_READ_INDEX))
1342 local_field->part_of_key= keys_in_use;
1343 if (local_field->part_of_sortkey.test(key))
1344 local_field->part_of_sortkey= keys_in_use;
1347 if (local_field->key_length() != key_part->length)
1349 key_part->key_part_flag|= HA_PART_KEY_SEG;
1352 keyinfo->usable_key_parts= usable_parts;
1354 set_if_bigger(max_key_length,keyinfo->key_length+
1355 keyinfo->key_parts);
1356 total_key_length+= keyinfo->key_length;
1358 if (keyinfo->flags & HA_NOSAME)
1360 set_if_bigger(max_unique_length,keyinfo->key_length);
1363 if (local_primary_key < MAX_KEY &&
1364 (keys_in_use.test(local_primary_key)))
1371 if (key_info[local_primary_key].key_parts == 1)
1373 Field *local_field= key_info[local_primary_key].key_part[0].field;
1374 if (local_field && local_field->result_type() == INT_RESULT)
1377 rowid_field_offset= (key_info[local_primary_key].key_part[0].
1384 if (found_next_number_field)
1386 Field *reg_field= *found_next_number_field;
1387 if ((
int) (next_number_index= (uint32_t)
1388 find_ref_key(key_info, keys,
1389 getDefaultValues(), reg_field,
1390 &next_number_key_offset,
1391 &next_number_keypart)) < 0)
1394 local_error= ER_NOT_FORM_FILE;
1400 reg_field->flags |= AUTO_INCREMENT_FLAG;
1407 blob_field.resize(blob_fields);
1408 uint32_t *save= &blob_field[0];
1410 for (Fields::iterator iter= _fields.begin(); iter != _fields.end()-1; iter++, k++)
1412 if ((*iter)->flags & BLOB_FLAG)
1418 all_set.resize(_field_size);
1421 return local_error != EE_OK;
1448 int TableShare::open_table_def(Session& session,
const identifier::Table &identifier)
1450 drizzled::error_t local_error= EE_OK;
1452 message::table::shared_ptr table= plugin::StorageEngine::getTableMessage(session, identifier, local_error);
1454 if (table and table->IsInitialized())
1456 if (parse_table_proto(session, *table))
1458 local_error= ER_CORRUPT_TABLE_DEFINITION_UNKNOWN;
1459 my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN, identifier);
1467 else if (table and not table->IsInitialized())
1469 local_error= ER_CORRUPT_TABLE_DEFINITION_UNKNOWN;
1470 my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN, identifier);
1474 local_error= ER_TABLE_UNKNOWN;
1475 my_error(ER_TABLE_UNKNOWN, identifier);
1478 return static_cast<int>(local_error);
1505 int TableShare::open_table_from_share(Session *session,
1506 const identifier::Table &identifier,
1508 uint32_t db_stat, uint32_t ha_open_flags,
1511 bool error_reported=
false;
1512 int ret= open_table_from_share_inner(session, alias, db_stat, outparam);
1515 ret= open_table_cursor_inner(identifier, db_stat, ha_open_flags, outparam, error_reported);
1520 if (not error_reported)
1521 open_table_error(ret, errno, 0);
1523 boost::checked_delete(outparam.cursor);
1525 outparam.db_stat= 0;
1526 outparam.mem().free_root(MYF(0));
1527 outparam.clearAlias();
1532 int TableShare::open_table_from_share_inner(Session *session,
const char *alias, uint32_t db_stat, Table &outparam)
1535 outparam.resetTable(session,
this, db_stat);
1537 outparam.setAlias(alias);
1540 if (not (outparam.cursor= db_type()->getCursor(outparam)))
1544 uint32_t records= 0;
1545 if (db_stat & HA_OPEN_KEYFILE)
1550 unsigned char* record= outparam.alloc(rec_buff_length * records);
1555 outparam.record[0]= outparam.record[1]= getDefaultValues();
1559 outparam.record[0]= record;
1561 outparam.record[1]= record+ rec_buff_length;
1563 outparam.record[1]= outparam.getInsertRecord();
1566 #ifdef HAVE_VALGRIND
1573 memcpy(outparam.getInsertRecord(), getDefaultValues(), rec_buff_length);
1574 memcpy(outparam.getUpdateRecord(), getDefaultValues(), null_bytes);
1576 memcpy(outparam.getUpdateRecord(), getDefaultValues(), rec_buff_length);
1581 memcpy(outparam.getUpdateRecord(), getDefaultValues(), null_bytes);
1584 Field** field_ptr =
new (outparam.mem()) Field*[_field_size + 1];
1586 outparam.setFields(field_ptr);
1588 record= outparam.getInsertRecord()-1;
1590 outparam.null_flags= (
unsigned char*) record+1;
1593 for (uint32_t i= 0 ; i < _field_size; i++, field_ptr++)
1595 if (!((*field_ptr)= _fields[i]->clone(&outparam.mem(), &outparam)))
1600 if (found_next_number_field)
1601 outparam.found_next_number_field=
1602 outparam.getField(positionFields(found_next_number_field));
1603 if (timestamp_field)
1604 outparam.timestamp_field= (field::Epoch*) outparam.getField(timestamp_field->position());
1609 uint32_t n_length= keys *
sizeof(KeyInfo) + key_parts *
sizeof(KeyPartInfo);
1610 KeyInfo* local_key_info= (KeyInfo*) outparam.alloc(n_length);
1611 outparam.key_info= local_key_info;
1612 KeyPartInfo* key_part=
reinterpret_cast<KeyPartInfo*
>(local_key_info+keys);
1614 memcpy(local_key_info, key_info,
sizeof(*local_key_info)*keys);
1615 memcpy(key_part, key_info[0].key_part,
sizeof(*key_part) * key_parts);
1617 for (KeyInfo* key_info_end= local_key_info + keys; local_key_info < key_info_end; local_key_info++)
1619 local_key_info->table= &outparam;
1620 local_key_info->key_part= key_part;
1622 for (KeyPartInfo* key_part_end= key_part+ local_key_info->key_parts; key_part < key_part_end; key_part++)
1624 Field *local_field= key_part->field= outparam.getField(key_part->fieldnr-1);
1626 if (local_field->key_length() != key_part->length && not (local_field->flags & BLOB_FLAG))
1632 local_field= key_part->field= local_field->new_field(&outparam.mem(), &outparam, 0);
1633 local_field->field_length= key_part->length;
1641 outparam.def_read_set.resize(_field_size);
1642 outparam.def_write_set.resize(_field_size);
1643 outparam.tmp_set.resize(_field_size);
1644 outparam.default_column_bitmaps();
1649 int TableShare::open_table_cursor_inner(
const identifier::Table &identifier,
1650 uint32_t db_stat, uint32_t ha_open_flags,
1652 bool &error_reported)
1658 assert(!(db_stat & HA_WAIT_IF_LOCKED));
1661 if ((ha_err= (outparam.cursor->ha_open(identifier,
1662 (db_stat & HA_READ_ONLY ? O_RDONLY : O_RDWR),
1663 (db_stat & HA_OPEN_TEMPORARY ? HA_OPEN_TMP_TABLE : HA_OPEN_IGNORE_IF_LOCKED) | ha_open_flags))))
1667 case HA_ERR_NO_SUCH_TABLE:
1684 outparam.print_error(ha_err, MYF(0));
1685 error_reported=
true;
1686 if (ha_err == HA_ERR_TABLE_DEF_CHANGED)
1698 void TableShare::open_table_error(
int pass_error,
int db_errno,
int pass_errarg)
1700 char buff[FN_REFLEN];
1701 myf errortype= ME_ERROR+ME_WAITTANG;
1703 switch (pass_error) {
1706 if (db_errno == ENOENT)
1708 identifier::Table identifier(db.data(), table_name.data());
1709 my_error(ER_TABLE_UNKNOWN, identifier);
1713 snprintf(buff,
sizeof(buff),
"%s",normalized_path.data());
1714 my_error((db_errno == EMFILE) ? ER_CANT_OPEN_FILE : ER_FILE_NOT_FOUND, errortype, buff, db_errno);
1719 drizzled::error_t err_no= (db_errno == ENOENT) ? ER_FILE_NOT_FOUND : (db_errno == EAGAIN) ? ER_FILE_USED : ER_CANT_OPEN_FILE;
1720 my_error(err_no, errortype, normalized_path.data(), db_errno);
1725 const char *csname= get_charset_name((uint32_t) pass_errarg);
1727 if (!csname || csname[0] ==
'?')
1729 snprintf(tmp,
sizeof(tmp),
"#%d", pass_errarg);
1732 my_printf_error(ER_UNKNOWN_COLLATION, _(
"Unknown collation '%s' in table '%-.64s' definition"), MYF(0), csname, table_name.data());
1736 snprintf(buff,
sizeof(buff),
"%s", normalized_path.data());
1737 my_printf_error(ER_NOT_FORM_FILE, _(
"Table '%-.64s' was created with a different version of Drizzle and cannot be read"), MYF(0), buff);
1743 snprintf(buff,
sizeof(buff),
"%s", normalized_path.data());
1744 my_error(ER_NOT_FORM_FILE, errortype, buff, 0);
1750 Field *TableShare::make_field(
const message::Table::Field &pfield,
1752 uint32_t field_length,
1754 unsigned char *null_pos,
1755 unsigned char null_bit,
1757 enum_field_types field_type,
1758 const charset_info_st * field_charset,
1759 Field::utype unireg_check,
1761 const char *field_name)
1763 return make_field(pfield,
1775 pfield.constraints().is_unsigned());
1778 Field *TableShare::make_field(
const message::Table::Field &,
1780 uint32_t field_length,
1782 unsigned char *null_pos,
1783 unsigned char null_bit,
1785 enum_field_types field_type,
1786 const charset_info_st * field_charset,
1787 Field::utype unireg_check,
1789 const char *field_name,
1799 null_bit= ((
unsigned char) 1) << null_bit;
1804 case DRIZZLE_TYPE_ENUM:
1805 return new (&mem_root) Field_enum(ptr,
1812 case DRIZZLE_TYPE_VARCHAR:
1814 return new (&mem_root) Field_varstring(ptr,field_length,
1815 ha_varchar_packlength(field_length),
1819 case DRIZZLE_TYPE_BLOB:
1820 return new (&mem_root) Field_blob(ptr,
1826 case DRIZZLE_TYPE_DECIMAL:
1827 return new (&mem_root) Field_decimal(ptr,
1834 case DRIZZLE_TYPE_DOUBLE:
1835 return new (&mem_root) Field_double(ptr,
1844 case DRIZZLE_TYPE_UUID:
1845 return new (&mem_root) field::Uuid(ptr,
1850 case DRIZZLE_TYPE_IPV6:
1851 return new (&mem_root) field::IPv6(ptr,
1856 case DRIZZLE_TYPE_BOOLEAN:
1857 return new (&mem_root) field::Boolean(ptr,
1863 case DRIZZLE_TYPE_LONG:
1864 return new (&mem_root) field::Int32(ptr,
1870 case DRIZZLE_TYPE_LONGLONG:
1874 return new (&mem_root) field::Size(ptr,
1882 return new (&mem_root) field::Int64(ptr,
1889 case DRIZZLE_TYPE_MICROTIME:
1890 return new (&mem_root) field::Microtime(ptr,
1896 case DRIZZLE_TYPE_TIMESTAMP:
1897 return new (&mem_root) field::Epoch(ptr,
1903 case DRIZZLE_TYPE_TIME:
1904 return new (&mem_root) field::Time(ptr, field_length, null_pos, null_bit, field_name);
1905 case DRIZZLE_TYPE_DATE:
1906 return new (&mem_root) Field_date(ptr, null_pos, null_bit, field_name);
1907 case DRIZZLE_TYPE_DATETIME:
1908 return new (&mem_root) Field_datetime(ptr, null_pos, null_bit, field_name);
1909 case DRIZZLE_TYPE_NULL:
1910 return new (&mem_root) Field_null(ptr, field_length, field_name);
1916 void TableShare::refreshVersion()
1918 version= g_refresh_version;