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>
90#include <drizzled/key_part_info.h>
96extern size_t table_def_size;
98static enum_field_types proto_field_type_to_drizzle_type(
const message::Table::Field &field)
102 case message::Table::Field::INTEGER:
103 return DRIZZLE_TYPE_LONG;
105 case message::Table::Field::DOUBLE:
106 return DRIZZLE_TYPE_DOUBLE;
108 case message::Table::Field::EPOCH:
109 if (field.has_time_options() and field.time_options().microseconds())
110 return DRIZZLE_TYPE_MICROTIME;
112 return DRIZZLE_TYPE_TIMESTAMP;
114 case message::Table::Field::BIGINT:
115 return DRIZZLE_TYPE_LONGLONG;
117 case message::Table::Field::DATETIME:
118 return DRIZZLE_TYPE_DATETIME;
120 case message::Table::Field::DATE:
121 return DRIZZLE_TYPE_DATE;
123 case message::Table::Field::VARCHAR:
124 return DRIZZLE_TYPE_VARCHAR;
126 case message::Table::Field::DECIMAL:
127 return DRIZZLE_TYPE_DECIMAL;
129 case message::Table::Field::ENUM:
130 return DRIZZLE_TYPE_ENUM;
132 case message::Table::Field::BLOB:
133 return DRIZZLE_TYPE_BLOB;
135 case message::Table::Field::UUID:
136 return DRIZZLE_TYPE_UUID;
138 case message::Table::Field::IPV6:
139 return DRIZZLE_TYPE_IPV6;
141 case message::Table::Field::BOOLEAN:
142 return DRIZZLE_TYPE_BOOLEAN;
144 case message::Table::Field::TIME:
145 return DRIZZLE_TYPE_TIME;
151static Item* default_value_item(enum_field_types field_type,
const charset_info_st& charset,
bool default_null,
152 const string& default_value,
const string& default_bin_value)
159 case DRIZZLE_TYPE_LONG:
160 case DRIZZLE_TYPE_LONGLONG:
163 int64_t val= (int64_t) internal::my_strtoll10(default_value.c_str(), NULL, &error);
164 if (error && error != -1)
166 return new Item_int(default_value.c_str(), val, default_value.length());
168 case DRIZZLE_TYPE_DOUBLE:
169 return new Item_float(default_value.c_str(), default_value.length());
170 case DRIZZLE_TYPE_NULL:
173 case DRIZZLE_TYPE_TIMESTAMP:
174 case DRIZZLE_TYPE_DATETIME:
175 case DRIZZLE_TYPE_TIME:
176 case DRIZZLE_TYPE_DATE:
177 case DRIZZLE_TYPE_ENUM:
178 case DRIZZLE_TYPE_UUID:
179 case DRIZZLE_TYPE_IPV6:
180 case DRIZZLE_TYPE_MICROTIME:
181 case DRIZZLE_TYPE_BOOLEAN:
183 return new Item_string(default_value.data(), default_value.size(), system_charset_info);
184 case DRIZZLE_TYPE_VARCHAR:
185 case DRIZZLE_TYPE_BLOB:
186 return &charset== &my_charset_bin
187 ?
new Item_string(default_bin_value, &my_charset_bin)
188 : new
Item_string(default_value, system_charset_info);
189 case DRIZZLE_TYPE_DECIMAL:
190 return new Item_decimal(default_value.c_str(), default_value.length(), system_charset_info);
204 assert(getTableMessage());
206 size_t num_indexes= getTableMessage()->indexes_size();
208 for (
size_t x= 0; x < num_indexes; ++x)
210 const message::Table::Index &index= getTableMessage()->indexes(x);
211 if (index.is_primary())
213 size_t num_parts= index.index_part_size();
214 for (
size_t y= 0; y < num_parts; ++y)
216 if (index.index_part(y).fieldnr() == in_field->position())
224TableShare::TableShare(
const identifier::Table::Type type_arg) :
226 found_next_number_field(NULL),
227 timestamp_field(NULL),
229 mem_root(TABLE_ALLOC_BLOCK_SIZE),
231 table_identifier(NULL),
236 stored_rec_length(0),
238 _table_message(NULL),
239 storage_engine(NULL),
243 last_null_bit_pos(0),
249 max_unique_length(0),
254 has_variable_width(false),
255 db_create_options(0),
256 db_options_in_use(0),
258 rowid_field_offset(0),
259 primary_key(MAX_KEY),
260 next_number_index(0),
261 next_number_key_offset(0),
262 next_number_keypart(0),
266 blob_ptr_size(portable_sizeof_char_ptr),
267 db_low_byte_first(false),
271 if (type_arg == message::Table::INTERNAL)
273 string s= identifier::Table::build_tmptable_filename();
274 private_key_for_cache.vectorPtr().assign(s.c_str(), s.c_str() + s.size() + 1);
275 init(private_key_for_cache.vector(), private_key_for_cache.vector());
285 found_next_number_field(NULL),
286 timestamp_field(NULL),
288 mem_root(TABLE_ALLOC_BLOCK_SIZE),
291 table_identifier(NULL),
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 table_identifier=
new identifier::Table(identifier);
333 assert(identifier.getKey() == key);
335 private_key_for_cache= key;
338 tmp_table= message::Table::INTERNAL;
341 normalized_path= str_ref(
"");
346 found_next_number_field(NULL),
347 timestamp_field(NULL),
349 mem_root(TABLE_ALLOC_BLOCK_SIZE),
352 table_identifier(NULL),
357 stored_rec_length(0),
359 _table_message(NULL),
360 storage_engine(NULL),
361 tmp_table(identifier.getType()),
364 last_null_bit_pos(0),
370 max_unique_length(0),
375 has_variable_width(false),
376 db_create_options(0),
377 db_options_in_use(0),
379 rowid_field_offset(0),
380 primary_key(MAX_KEY),
381 next_number_index(0),
382 next_number_key_offset(0),
383 next_number_keypart(0),
387 blob_ptr_size(portable_sizeof_char_ptr),
388 db_low_byte_first(false),
392 table_identifier=
new identifier::Table(identifier);
394 private_key_for_cache= identifier.getKey();
395 assert(identifier.getPath().size());
396 private_normalized_path.resize(identifier.getPath().size() + 1);
397 memcpy(&private_normalized_path[0], identifier.getPath().c_str(), identifier.getPath().size());
401 tmp_table= message::Table::INTERNAL;
402 path= private_normalized_path;
403 normalized_path= path;
411TableShare::TableShare(
const identifier::Table::Type type_arg,
413 const char *path_arg,
414 uint32_t path_length_arg) :
416 found_next_number_field(NULL),
417 timestamp_field(NULL),
419 mem_root(TABLE_ALLOC_BLOCK_SIZE),
422 table_identifier(NULL),
427 stored_rec_length(0),
429 _table_message(NULL),
430 storage_engine(NULL),
434 last_null_bit_pos(0),
440 max_unique_length(0),
445 has_variable_width(false),
446 db_create_options(0),
447 db_options_in_use(0),
449 rowid_field_offset(0),
450 primary_key(MAX_KEY),
451 next_number_index(0),
452 next_number_key_offset(0),
453 next_number_keypart(0),
457 blob_ptr_size(portable_sizeof_char_ptr),
458 db_low_byte_first(false),
462 table_identifier=
new identifier::Table(identifier);
464 private_key_for_cache= identifier.getKey();
469 _path.assign(path_arg, path_length_arg);
473 _path= identifier::Table::build_table_filename(*table_identifier,
false);
476 char* path_buff= mem_root.strdup(_path);
477 path= str_ref(path_buff, _path.length());
478 normalized_path= str_ref(path_buff, _path.length());
480 version= g_refresh_version;
483void TableShare::init(
const char *new_table_name,
const char *new_path)
486 tmp_table= message::Table::INTERNAL;
489 identifier::Table *n=
new identifier::Table(catalog::local_identifier(),
490 "", new_table_name, new_path);
491 delete table_identifier;
494 path= str_ref(new_path);
495 normalized_path= str_ref(new_path);
498TableShare::~TableShare()
500 storage_engine= NULL;
501 delete table_identifier;
503 mem_root.free_root(MYF(0));
508 private_key_for_cache= identifier_arg.getKey();
510 delete table_identifier;
511 table_identifier=
new identifier::Table(identifier_arg);
513 getTableMessage()->set_name(identifier_arg.getTableName());
514 getTableMessage()->set_schema(identifier_arg.getSchemaName());
517bool TableShare::parse_table_proto(
Session& session,
const message::Table &table)
519 drizzled::error_t local_error= EE_OK;
521 if (! table.IsInitialized())
523 my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
524 table.name().empty() ?
" " : table.name().c_str(),
525 table.InitializationErrorString().c_str());
530 setTableMessage(table);
532 storage_engine= plugin::StorageEngine::findByName(session, table.engine().name());
533 assert(storage_engine);
535 message::Table::TableOptions table_options;
537 if (table.has_options())
538 table_options= table.options();
540 uint32_t local_db_create_options= 0;
542 if (table_options.pack_record())
543 local_db_create_options|= HA_OPTION_PACK_RECORD;
548 db_create_options= (local_db_create_options & 0x0000FFFF);
549 db_options_in_use= db_create_options;
551 block_size= table_options.has_block_size() ? table_options.block_size() : 0;
553 table_charset= get_charset(table_options.collation_id());
555 if (not table_charset)
557 my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN_COLLATION, MYF(0),
558 table_options.collation().c_str(),
559 table.name().c_str());
566 keys= table.indexes_size();
569 for (
int indx= 0; indx < table.indexes_size(); indx++)
570 key_parts+= table.indexes(indx).index_part_size();
572 key_info= (KeyInfo*) mem().alloc(table.indexes_size() *
sizeof(KeyInfo) +key_parts*
sizeof(KeyPartInfo));
574 KeyPartInfo *key_part;
576 key_part=
reinterpret_cast<KeyPartInfo*
>
577 (key_info+table.indexes_size());
580 ulong *rec_per_key= (ulong*) mem().alloc(
sizeof(ulong*)*key_parts);
582 KeyInfo* keyinfo= key_info;
583 for (
int keynr= 0; keynr < table.indexes_size(); keynr++, keyinfo++)
585 message::Table::Index indx= table.indexes(keynr);
590 if (indx.is_unique())
591 keyinfo->flags|= HA_NOSAME;
593 if (indx.has_options())
595 message::Table::Index::Options indx_options= indx.options();
596 if (indx_options.pack_key())
597 keyinfo->flags|= HA_PACK_KEY;
599 if (indx_options.var_length_key())
600 keyinfo->flags|= HA_VAR_LENGTH_PART;
602 if (indx_options.null_part_key())
603 keyinfo->flags|= HA_NULL_PART_KEY;
605 if (indx_options.binary_pack_key())
606 keyinfo->flags|= HA_BINARY_PACK_KEY;
608 if (indx_options.has_partial_segments())
609 keyinfo->flags|= HA_KEY_HAS_PART_KEY_SEG;
611 if (indx_options.auto_generated_key())
612 keyinfo->flags|= HA_GENERATED_KEY;
614 if (indx_options.has_key_block_size())
616 keyinfo->flags|= HA_USES_BLOCK_SIZE;
617 keyinfo->block_size= indx_options.key_block_size();
621 keyinfo->block_size= 0;
625 keyinfo->algorithm= indx.type();
627 keyinfo->key_length= indx.key_length();
629 keyinfo->key_parts= indx.index_part_size();
631 keyinfo->key_part= key_part;
632 keyinfo->rec_per_key= rec_per_key;
634 for (
unsigned int partnr= 0;
635 partnr < keyinfo->key_parts;
636 partnr++, key_part++)
638 message::Table::Index::IndexPart part;
639 part= indx.index_part(partnr);
643 key_part->field= NULL;
644 key_part->fieldnr= part.fieldnr() + 1;
645 key_part->null_bit= 0;
649 key_part->key_part_flag= 0;
650 if (part.has_in_reverse_order())
651 key_part->key_part_flag= part.in_reverse_order()? HA_REVERSE_SORT : 0;
653 key_part->length= part.compare_length();
657 if (table.field(part.fieldnr()).type() == message::Table::Field::VARCHAR
658 || table.field(part.fieldnr()).type() == message::Table::Field::BLOB)
660 uint32_t collation_id;
662 if (table.field(part.fieldnr()).string_options().has_collation_id())
663 collation_id= table.field(part.fieldnr()).string_options().collation_id();
665 collation_id= table.options().collation_id();
667 const charset_info_st *cs= get_charset(collation_id);
669 mbmaxlen= cs->mbmaxlen;
671 key_part->length*= mbmaxlen;
673 key_part->store_length= key_part->length;
676 key_part->key_type= 0;
679 if (not indx.has_comment())
681 keyinfo->comment.clear();
685 keyinfo->flags|= HA_USES_COMMENT;
686 keyinfo->comment.assign(mem().strdup(indx.comment()), indx.comment().length());
689 keyinfo->name= mem().strdup(indx.name());
691 addKeyName(
string(keyinfo->name, indx.name().length()));
694 keys_for_keyread.reset();
695 set_prefix(keys_in_use, keys);
697 _field_size= table.field_size();
699 setFields(_field_size + 1);
700 _fields[_field_size]= NULL;
702 uint32_t local_null_fields= 0;
705 std::vector<uint32_t> field_offsets;
706 std::vector<uint32_t> field_pack_length;
708 field_offsets.resize(_field_size);
709 field_pack_length.resize(_field_size);
711 uint32_t interval_count= 0;
713 uint32_t stored_columns_reclength= 0;
715 for (
unsigned int fieldnr= 0; fieldnr < _field_size; fieldnr++)
717 message::Table::Field pfield= table.field(fieldnr);
718 if (pfield.constraints().is_nullable())
722 else if (not pfield.constraints().is_notnull())
727 enum_field_types drizzle_field_type= proto_field_type_to_drizzle_type(pfield);
729 field_offsets[fieldnr]= stored_columns_reclength;
735 switch(drizzle_field_type)
737 case DRIZZLE_TYPE_BLOB:
738 case DRIZZLE_TYPE_VARCHAR:
740 message::Table::Field::StringFieldOptions field_options= pfield.string_options();
742 const charset_info_st *cs= get_charset(field_options.has_collation_id() ?
743 field_options.collation_id() : 0);
746 cs= default_charset_info;
748 field_pack_length[fieldnr]= calc_pack_length(drizzle_field_type,
749 field_options.length() * cs->mbmaxlen);
752 case DRIZZLE_TYPE_ENUM:
754 message::Table::Field::EnumerationValues field_options= pfield.enumeration_values();
756 field_pack_length[fieldnr]= 4;
761 case DRIZZLE_TYPE_DECIMAL:
763 message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
765 field_pack_length[fieldnr]= class_decimal_get_binary_size(fo.precision(), fo.scale());
770 field_pack_length[fieldnr]= calc_pack_length(drizzle_field_type, 0);
773 reclength+= field_pack_length[fieldnr];
774 stored_columns_reclength+= field_pack_length[fieldnr];
778 stored_rec_length= stored_columns_reclength;
780 null_fields= local_null_fields;
782 ulong null_bits= local_null_fields;
783 if (! table_options.pack_record())
785 ulong data_offset= (null_bits + 7)/8;
788 reclength+= data_offset;
789 stored_rec_length+= data_offset;
791 ulong local_rec_buff_length;
793 local_rec_buff_length= ALIGN_SIZE(reclength + 1);
794 rec_buff_length= local_rec_buff_length;
796 resizeDefaultValues(local_rec_buff_length);
797 unsigned char* record= getDefaultValues();
800 if (! table_options.pack_record())
807 intervals.resize(interval_count);
813 uint32_t interval_nr= 0;
815 for (
unsigned int fieldnr= 0; fieldnr < _field_size; fieldnr++)
817 message::Table::Field pfield= table.field(fieldnr);
820 if (pfield.type() != message::Table::Field::ENUM)
823 message::Table::Field::EnumerationValues field_options= pfield.enumeration_values();
825 if (field_options.field_value_size() > Field_enum::max_supported_elements)
827 my_error(ER_CORRUPT_TABLE_DEFINITION_ENUM, MYF(0), table.name().c_str());
833 const charset_info_st *charset= get_charset(field_options.has_collation_id() ?
834 field_options.collation_id() : 0);
837 charset= default_charset_info;
839 TYPELIB *t= (&intervals[interval_nr]);
841 t->type_names= (
const char**)mem().alloc((field_options.field_value_size() + 1) *
sizeof(
char*));
842 t->type_lengths= (
unsigned int*)mem().alloc((field_options.field_value_size() + 1) *
sizeof(
unsigned int));
844 t->type_names[field_options.field_value_size()]= NULL;
845 t->type_lengths[field_options.field_value_size()]= 0;
847 t->count= field_options.field_value_size();
850 for (
int n= 0; n < field_options.field_value_size(); n++)
852 t->type_names[n]= mem().strdup(field_options.field_value(n));
858 t->type_lengths[n]= charset->cset->lengthsp(charset, t->type_names[n], field_options.field_value(n).length());
867 bool use_hash= _field_size >= MAX_FIELDS_BEFORE_HASH;
869 unsigned char* null_pos= getDefaultValues();
870 int null_bit_pos= (table_options.pack_record()) ? 0 : 1;
872 for (
unsigned int fieldnr= 0; fieldnr < _field_size; fieldnr++)
874 message::Table::Field pfield= table.field(fieldnr);
876 Field::utype unireg_type= Field::NONE;
878 if (pfield.has_numeric_options() &&
879 pfield.numeric_options().is_autoincrement())
881 unireg_type= Field::NEXT_NUMBER;
884 if (pfield.has_options() &&
885 pfield.options().has_default_expression() &&
886 pfield.options().default_expression().compare(
"CURRENT_TIMESTAMP") == 0)
888 if (pfield.options().has_update_expression() &&
889 pfield.options().update_expression().compare(
"CURRENT_TIMESTAMP") == 0)
891 unireg_type= Field::TIMESTAMP_DNUN_FIELD;
893 else if (! pfield.options().has_update_expression())
895 unireg_type= Field::TIMESTAMP_DN_FIELD;
903 else if (pfield.has_options() &&
904 pfield.options().has_update_expression() &&
905 pfield.options().update_expression().compare(
"CURRENT_TIMESTAMP") == 0)
907 unireg_type= Field::TIMESTAMP_UN_FIELD;
911 if (pfield.has_comment())
913 comment.assign(mem().strdup(pfield.comment()), pfield.comment().size());
916 enum_field_types field_type;
918 field_type= proto_field_type_to_drizzle_type(pfield);
920 const charset_info_st *charset= &my_charset_bin;
922 if (field_type == DRIZZLE_TYPE_BLOB || field_type == DRIZZLE_TYPE_VARCHAR)
924 message::Table::Field::StringFieldOptions field_options= pfield.string_options();
926 charset= get_charset(field_options.has_collation_id() ? field_options.collation_id() : 0);
929 charset= default_charset_info;
932 if (field_type == DRIZZLE_TYPE_ENUM)
934 message::Table::Field::EnumerationValues field_options= pfield.enumeration_values();
936 charset= get_charset(field_options.has_collation_id() ? field_options.collation_id() : 0);
939 charset= default_charset_info;
943 if (field_type == DRIZZLE_TYPE_DECIMAL || field_type == DRIZZLE_TYPE_DOUBLE)
945 message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
947 if (not pfield.has_numeric_options() || ! fo.has_scale())
953 decimals= NOT_FIXED_DEC;
957 if (fo.scale() > DECIMAL_MAX_SCALE)
959 local_error= ER_NOT_FORM_FILE;
963 decimals=
static_cast<uint8_t
>(fo.scale());
967 Item *default_value= NULL;
969 if (pfield.options().has_default_value() ||
970 pfield.options().default_null() ||
971 pfield.options().has_default_bin_value())
973 default_value= default_value_item(field_type, *charset, pfield.options().default_null(), pfield.options().default_value(), pfield.options().default_bin_value());
974 if (default_value == NULL)
976 my_error(ER_INVALID_DEFAULT, MYF(0), pfield.name().c_str());
982 uint32_t field_length= 0;
987 case DRIZZLE_TYPE_BLOB:
988 case DRIZZLE_TYPE_VARCHAR:
990 message::Table::Field::StringFieldOptions field_options= pfield.string_options();
992 charset= get_charset(field_options.has_collation_id() ?
993 field_options.collation_id() : 0);
996 charset= default_charset_info;
998 field_length= field_options.length() * charset->mbmaxlen;
1001 case DRIZZLE_TYPE_DOUBLE:
1003 message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
1004 if (!fo.has_precision() && !fo.has_scale())
1006 field_length= DBL_DIG+7;
1010 field_length= fo.precision();
1012 if (field_length < decimals &&
1013 decimals != NOT_FIXED_DEC)
1015 my_error(ER_M_BIGGER_THAN_D, MYF(0), pfield.name().c_str());
1016 local_error= ER_M_BIGGER_THAN_D;
1021 case DRIZZLE_TYPE_DECIMAL:
1023 message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
1025 field_length= class_decimal_precision_to_length(fo.precision(), fo.scale(),
1029 case DRIZZLE_TYPE_DATETIME:
1032 case DRIZZLE_TYPE_DATE:
1035 case DRIZZLE_TYPE_ENUM:
1039 message::Table::Field::EnumerationValues fo= pfield.enumeration_values();
1041 for (
int valnr= 0; valnr < fo.field_value_size(); valnr++)
1043 if (fo.field_value(valnr).length() > field_length)
1045 field_length= charset->cset->numchars(charset,
1046 fo.field_value(valnr).c_str(),
1047 fo.field_value(valnr).c_str()
1048 + fo.field_value(valnr).length())
1049 * charset->mbmaxlen;
1054 case DRIZZLE_TYPE_LONG:
1056 uint32_t sign_len= pfield.constraints().is_unsigned() ? 0 : 1;
1057 field_length= MAX_INT_WIDTH+sign_len;
1060 case DRIZZLE_TYPE_LONGLONG:
1062 uint32_t sign_len= pfield.constraints().is_unsigned() ? 0 : 1;
1063 field_length= MAX_BIGINT_WIDTH+sign_len;
1066 case DRIZZLE_TYPE_UUID:
1067 field_length= field::Uuid::max_string_length();
1069 case DRIZZLE_TYPE_IPV6:
1070 field_length= field::IPv6::max_string_length();
1072 case DRIZZLE_TYPE_BOOLEAN:
1073 field_length= field::Boolean::max_string_length();
1075 case DRIZZLE_TYPE_MICROTIME:
1076 field_length= field::Microtime::max_string_length();
1078 case DRIZZLE_TYPE_TIMESTAMP:
1079 field_length= field::Epoch::max_string_length();
1081 case DRIZZLE_TYPE_TIME:
1082 field_length= field::Time::max_string_length();
1084 case DRIZZLE_TYPE_NULL:
1088 bool is_not_null=
false;
1090 if (not pfield.constraints().is_nullable())
1094 else if (pfield.constraints().is_notnull())
1099 Field* f= make_field(pfield,
1100 record + field_offsets[fieldnr] + data_offset,
1108 MTYP_TYPENR(unireg_type),
1109 ((field_type == DRIZZLE_TYPE_ENUM) ? &intervals[interval_nr++] : (TYPELIB*) 0),
1110 getTableMessage()->field(fieldnr).name().c_str());
1112 _fields[fieldnr]= f;
1117 case DRIZZLE_TYPE_BLOB:
1118 case DRIZZLE_TYPE_VARCHAR:
1119 case DRIZZLE_TYPE_DOUBLE:
1120 case DRIZZLE_TYPE_DECIMAL:
1121 case DRIZZLE_TYPE_TIMESTAMP:
1122 case DRIZZLE_TYPE_TIME:
1123 case DRIZZLE_TYPE_DATETIME:
1124 case DRIZZLE_TYPE_MICROTIME:
1125 case DRIZZLE_TYPE_DATE:
1126 case DRIZZLE_TYPE_ENUM:
1127 case DRIZZLE_TYPE_LONG:
1128 case DRIZZLE_TYPE_LONGLONG:
1129 case DRIZZLE_TYPE_NULL:
1130 case DRIZZLE_TYPE_UUID:
1131 case DRIZZLE_TYPE_IPV6:
1132 case DRIZZLE_TYPE_BOOLEAN:
1138 table::Shell temp_table(*
this);
1139 temp_table.in_use= &session;
1141 f->init(&temp_table);
1143 if (! (f->flags & NOT_NULL_FLAG))
1145 *f->null_ptr|= f->null_bit;
1146 if (! (null_bit_pos= (null_bit_pos + 1) & 7))
1153 enum_check_fields old_count_cuted_fields= session.count_cuted_fields;
1154 session.count_cuted_fields= CHECK_FIELD_ERROR_FOR_NULL;
1155 int res= default_value->save_in_field(f, 1);
1156 session.count_cuted_fields= old_count_cuted_fields;
1157 if (res != 0 && res != 3)
1159 my_error(ER_INVALID_DEFAULT, MYF(0), f->field_name);
1160 local_error= ER_INVALID_DEFAULT;
1165 else if (f->real_type() == DRIZZLE_TYPE_ENUM && (f->flags & NOT_NULL_FLAG))
1168 f->store((int64_t) 1,
true);
1177 f->orig_table= NULL;
1179 f->setPosition(fieldnr);
1180 f->comment= comment;
1181 if (not default_value &&
1182 not (f->unireg_check==Field::NEXT_NUMBER) &&
1183 (f->flags & NOT_NULL_FLAG) &&
1184 (not f->is_timestamp()))
1186 f->flags|= NO_DEFAULT_VALUE_FLAG;
1189 if (f->unireg_check == Field::NEXT_NUMBER)
1190 found_next_number_field= &(_fields[fieldnr]);
1194 const char *local_field_name= _fields[fieldnr]->field_name;
1195 name_hash.insert(make_pair(local_field_name, &(_fields[fieldnr])));
1200 for (
unsigned int keynr= 0; keynr < keys; keynr++, keyinfo++)
1202 key_part= keyinfo->key_part;
1204 for (
unsigned int partnr= 0;
1205 partnr < keyinfo->key_parts;
1206 partnr++, key_part++)
1213 key_part->offset= field_offsets[key_part->fieldnr-1] + data_offset;
1222 *(record + null_count / 8)|= ~(((
unsigned char) 1 << (null_count & 7)) - 1);
1224 null_bytes= (null_pos - (
unsigned char*) record + (null_bit_pos + 7) / 8);
1226 last_null_bit_pos= null_bit_pos;
1231 uint32_t local_primary_key= doesKeyNameExist(
"PRIMARY");
1233 key_part= keyinfo->key_part;
1235 for (uint32_t key= 0; key < keys; key++,keyinfo++)
1237 uint32_t usable_parts= 0;
1239 if (local_primary_key >= MAX_KEY && (keyinfo->flags & HA_NOSAME))
1245 local_primary_key=key;
1246 for (uint32_t i= 0; i < keyinfo->key_parts; i++)
1248 uint32_t fieldnr= key_part[i].fieldnr;
1250 _fields[fieldnr-1]->null_ptr ||
1251 _fields[fieldnr-1]->key_length() != key_part[i].length)
1253 local_primary_key= MAX_KEY;
1259 for (uint32_t i= 0 ; i < keyinfo->key_parts ; key_part++,i++)
1262 if (! key_part->fieldnr)
1266 local_field= key_part->field= _fields[key_part->fieldnr-1];
1267 key_part->type= local_field->key_type();
1268 if (local_field->null_ptr)
1270 key_part->null_offset=(uint32_t) ((
unsigned char*) local_field->null_ptr - getDefaultValues());
1271 key_part->null_bit= local_field->null_bit;
1272 key_part->store_length+=HA_KEY_NULL_LENGTH;
1273 keyinfo->flags|=HA_NULL_PART_KEY;
1274 keyinfo->extra_length+= HA_KEY_NULL_LENGTH;
1275 keyinfo->key_length+= HA_KEY_NULL_LENGTH;
1277 if (local_field->type() == DRIZZLE_TYPE_BLOB ||
1278 local_field->real_type() == DRIZZLE_TYPE_VARCHAR)
1280 if (local_field->type() == DRIZZLE_TYPE_BLOB)
1281 key_part->key_part_flag|= HA_BLOB_PART;
1283 key_part->key_part_flag|= HA_VAR_LENGTH_PART;
1284 keyinfo->extra_length+=HA_KEY_BLOB_LENGTH;
1285 key_part->store_length+=HA_KEY_BLOB_LENGTH;
1286 keyinfo->key_length+= HA_KEY_BLOB_LENGTH;
1288 if (i == 0 && key != local_primary_key)
1289 local_field->flags |= (((keyinfo->flags & HA_NOSAME) &&
1290 (keyinfo->key_parts == 1)) ?
1291 UNIQUE_KEY_FLAG : MULTIPLE_KEY_FLAG);
1293 local_field->key_start.set(key);
1294 if (local_field->key_length() == key_part->length &&
1295 !(local_field->flags & BLOB_FLAG))
1297 message::Table::Index::IndexType algo= key_info[key].algorithm;
1298 if (db_type()->index_flags(algo) & HA_KEYREAD_ONLY)
1300 keys_for_keyread.set(key);
1301 local_field->part_of_key.set(key);
1302 local_field->part_of_key_not_clustered.set(key);
1304 if (db_type()->index_flags(algo) & HA_READ_ORDER)
1305 local_field->part_of_sortkey.set(key);
1307 if (!(key_part->key_part_flag & HA_REVERSE_SORT) &&
1310 local_field->flags|= PART_KEY_FLAG;
1311 if (key == local_primary_key)
1313 local_field->flags|= PRI_KEY_FLAG;
1318 if (storage_engine->check_flag(HTON_BIT_PRIMARY_KEY_IN_READ_INDEX))
1320 local_field->part_of_key= keys_in_use;
1321 if (local_field->part_of_sortkey.test(key))
1322 local_field->part_of_sortkey= keys_in_use;
1325 if (local_field->key_length() != key_part->length)
1327 key_part->key_part_flag|= HA_PART_KEY_SEG;
1330 keyinfo->usable_key_parts= usable_parts;
1332 set_if_bigger(max_key_length,keyinfo->key_length+
1333 keyinfo->key_parts);
1334 total_key_length+= keyinfo->key_length;
1336 if (keyinfo->flags & HA_NOSAME)
1338 set_if_bigger(max_unique_length,keyinfo->key_length);
1341 if (local_primary_key < MAX_KEY &&
1342 (keys_in_use.test(local_primary_key)))
1349 if (key_info[local_primary_key].key_parts == 1)
1351 Field *local_field= key_info[local_primary_key].key_part[0].field;
1352 if (local_field && local_field->result_type() == INT_RESULT)
1355 rowid_field_offset= (key_info[local_primary_key].key_part[0].
1362 if (found_next_number_field)
1364 Field *reg_field= *found_next_number_field;
1365 if ((
int) (next_number_index= (uint32_t)
1366 find_ref_key(key_info, keys,
1367 getDefaultValues(), reg_field,
1368 &next_number_key_offset,
1369 &next_number_keypart)) < 0)
1372 local_error= ER_NOT_FORM_FILE;
1378 reg_field->flags |= AUTO_INCREMENT_FLAG;
1385 blob_field.resize(blob_fields);
1386 uint32_t *save= &blob_field[0];
1388 for (Fields::iterator iter= _fields.begin(); iter != _fields.end()-1; iter++, k++)
1390 if ((*iter)->flags & BLOB_FLAG)
1396 all_set.resize(_field_size);
1399 return local_error != EE_OK;
1428 drizzled::error_t local_error= EE_OK;
1430 message::table::shared_ptr table= plugin::StorageEngine::getTableMessage(session, identifier, local_error);
1432 if (table and table->IsInitialized())
1434 if (parse_table_proto(session, *table))
1436 local_error= ER_CORRUPT_TABLE_DEFINITION_UNKNOWN;
1437 my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN, identifier);
1445 else if (table and not table->IsInitialized())
1447 local_error= ER_CORRUPT_TABLE_DEFINITION_UNKNOWN;
1448 my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN, identifier);
1452 local_error= ER_TABLE_UNKNOWN;
1453 my_error(ER_TABLE_UNKNOWN, identifier);
1456 return static_cast<int>(local_error);
1483int TableShare::open_table_from_share(
Session *session,
1486 uint32_t db_stat, uint32_t ha_open_flags,
1489 bool error_reported=
false;
1490 int ret= open_table_from_share_inner(session, alias, db_stat, outparam);
1493 ret= open_table_cursor_inner(identifier, db_stat, ha_open_flags, outparam, error_reported);
1498 if (not error_reported)
1499 open_table_error(ret, errno, 0);
1501 boost::checked_delete(outparam.cursor);
1503 outparam.db_stat= 0;
1504 outparam.mem().free_root(MYF(0));
1505 outparam.clearAlias();
1510int TableShare::open_table_from_share_inner(
Session *session,
const char *alias, uint32_t db_stat,
Table &outparam)
1513 outparam.resetTable(session,
this, db_stat);
1515 outparam.setAlias(alias);
1518 if (not (outparam.cursor= db_type()->getCursor(outparam)))
1522 uint32_t records= 0;
1523 if (db_stat & HA_OPEN_KEYFILE)
1528 unsigned char* record= outparam.alloc(rec_buff_length * records);
1533 outparam.record[0]= outparam.record[1]= getDefaultValues();
1537 outparam.record[0]= record;
1539 outparam.record[1]= record+ rec_buff_length;
1541 outparam.record[1]= outparam.getInsertRecord();
1551 memcpy(outparam.getInsertRecord(), getDefaultValues(), rec_buff_length);
1552 memcpy(outparam.getUpdateRecord(), getDefaultValues(), null_bytes);
1554 memcpy(outparam.getUpdateRecord(), getDefaultValues(), rec_buff_length);
1559 memcpy(outparam.getUpdateRecord(), getDefaultValues(), null_bytes);
1562 Field** field_ptr =
new (outparam.mem()) Field*[_field_size + 1];
1564 outparam.setFields(field_ptr);
1566 record= outparam.getInsertRecord()-1;
1568 outparam.null_flags= (
unsigned char*) record+1;
1571 for (uint32_t i= 0 ; i < _field_size; i++, field_ptr++)
1573 if (!((*field_ptr)= _fields[i]->clone(&outparam.mem(), &outparam)))
1578 if (found_next_number_field)
1579 outparam.found_next_number_field=
1580 outparam.getField(positionFields(found_next_number_field));
1581 if (timestamp_field)
1582 outparam.timestamp_field= (field::Epoch*) outparam.getField(timestamp_field->position());
1587 uint32_t n_length= keys *
sizeof(KeyInfo) + key_parts *
sizeof(KeyPartInfo);
1588 KeyInfo* local_key_info= (KeyInfo*) outparam.alloc(n_length);
1589 outparam.key_info= local_key_info;
1590 KeyPartInfo* key_part=
reinterpret_cast<KeyPartInfo*
>(local_key_info+keys);
1592 memcpy(local_key_info, key_info,
sizeof(*local_key_info)*keys);
1593 memcpy(key_part, key_info[0].key_part,
sizeof(*key_part) * key_parts);
1595 for (KeyInfo* key_info_end= local_key_info + keys; local_key_info < key_info_end; local_key_info++)
1597 local_key_info->table= &outparam;
1598 local_key_info->key_part= key_part;
1600 for (KeyPartInfo* key_part_end= key_part+ local_key_info->key_parts; key_part < key_part_end; key_part++)
1602 Field *local_field= key_part->field= outparam.getField(key_part->fieldnr-1);
1604 if (local_field->key_length() != key_part->length && not (local_field->flags & BLOB_FLAG))
1610 local_field= key_part->field= local_field->new_field(&outparam.mem(), &outparam, 0);
1611 local_field->field_length= key_part->length;
1619 outparam.def_read_set.resize(_field_size);
1620 outparam.def_write_set.resize(_field_size);
1621 outparam.tmp_set.resize(_field_size);
1622 outparam.default_column_bitmaps();
1628 uint32_t db_stat, uint32_t ha_open_flags,
1630 bool &error_reported)
1636 assert(!(db_stat & HA_WAIT_IF_LOCKED));
1639 if ((ha_err= (outparam.cursor->ha_open(identifier,
1640 (db_stat & HA_READ_ONLY ? O_RDONLY : O_RDWR),
1641 (db_stat & HA_OPEN_TEMPORARY ? HA_OPEN_TMP_TABLE : HA_OPEN_IGNORE_IF_LOCKED) | ha_open_flags))))
1645 case HA_ERR_NO_SUCH_TABLE:
1662 outparam.print_error(ha_err, MYF(0));
1663 error_reported=
true;
1664 if (ha_err == HA_ERR_TABLE_DEF_CHANGED)
1676void TableShare::open_table_error(
int pass_error,
int db_errno,
int pass_errarg)
1678 char buff[FN_REFLEN];
1679 myf errortype= ME_ERROR+ME_WAITTANG;
1681 switch (pass_error) {
1684 if (db_errno == ENOENT)
1686 my_error(ER_TABLE_UNKNOWN, *table_identifier);
1690 snprintf(buff,
sizeof(buff),
"%s",normalized_path.data());
1691 my_error((db_errno == EMFILE) ? ER_CANT_OPEN_FILE : ER_FILE_NOT_FOUND, errortype, buff, db_errno);
1696 drizzled::error_t err_no= (db_errno == ENOENT) ? ER_FILE_NOT_FOUND : (db_errno == EAGAIN) ? ER_FILE_USED : ER_CANT_OPEN_FILE;
1697 my_error(err_no, errortype, normalized_path.data(), db_errno);
1702 const char *csname= get_charset_name((uint32_t) pass_errarg);
1704 if (!csname || csname[0] ==
'?')
1706 snprintf(tmp,
sizeof(tmp),
"#%d", pass_errarg);
1709 my_printf_error(ER_UNKNOWN_COLLATION, _(
"Unknown collation '%s' in table '%-.64s' definition"), MYF(0), csname, table_identifier->getTableName().c_str());
1713 snprintf(buff,
sizeof(buff),
"%s", normalized_path.data());
1714 my_printf_error(ER_NOT_FORM_FILE, _(
"Table '%-.64s' was created with a different version of Drizzle and cannot be read"), MYF(0), buff);
1720 snprintf(buff,
sizeof(buff),
"%s", normalized_path.data());
1721 my_error(ER_NOT_FORM_FILE, errortype, buff, 0);
1727Field *TableShare::make_field(
const message::Table::Field &pfield,
1729 uint32_t field_length,
1731 unsigned char *null_pos,
1732 unsigned char null_bit,
1734 enum_field_types field_type,
1736 Field::utype unireg_check,
1738 const char *field_name)
1740 return make_field(pfield,
1752 pfield.constraints().is_unsigned());
1755Field *TableShare::make_field(
const message::Table::Field &,
1757 uint32_t field_length,
1759 unsigned char *null_pos,
1760 unsigned char null_bit,
1762 enum_field_types field_type,
1764 Field::utype unireg_check,
1766 const char *field_name,
1776 null_bit= ((
unsigned char) 1) << null_bit;
1781 case DRIZZLE_TYPE_ENUM:
1782 return new (&mem_root) Field_enum(ptr,
1789 case DRIZZLE_TYPE_VARCHAR:
1791 return new (&mem_root) Field_varstring(ptr,field_length,
1792 ha_varchar_packlength(field_length),
1796 case DRIZZLE_TYPE_BLOB:
1797 return new (&mem_root) Field_blob(ptr,
1803 case DRIZZLE_TYPE_DECIMAL:
1804 return new (&mem_root) Field_decimal(ptr,
1811 case DRIZZLE_TYPE_DOUBLE:
1812 return new (&mem_root) Field_double(ptr,
1821 case DRIZZLE_TYPE_UUID:
1822 return new (&mem_root) field::Uuid(ptr,
1827 case DRIZZLE_TYPE_IPV6:
1828 return new (&mem_root) field::IPv6(ptr,
1833 case DRIZZLE_TYPE_BOOLEAN:
1834 return new (&mem_root) field::Boolean(ptr,
1840 case DRIZZLE_TYPE_LONG:
1841 return new (&mem_root) field::Int32(ptr,
1847 case DRIZZLE_TYPE_LONGLONG:
1851 return new (&mem_root) field::Size(ptr,
1859 return new (&mem_root) field::Int64(ptr,
1866 case DRIZZLE_TYPE_MICROTIME:
1867 return new (&mem_root) field::Microtime(ptr,
1873 case DRIZZLE_TYPE_TIMESTAMP:
1874 return new (&mem_root) field::Epoch(ptr,
1880 case DRIZZLE_TYPE_TIME:
1881 return new (&mem_root) field::Time(ptr, field_length, null_pos, null_bit, field_name);
1882 case DRIZZLE_TYPE_DATE:
1883 return new (&mem_root) Field_date(ptr, null_pos, null_bit, field_name);
1884 case DRIZZLE_TYPE_DATETIME:
1885 return new (&mem_root) Field_datetime(ptr, null_pos, null_bit, field_name);
1886 case DRIZZLE_TYPE_NULL:
1887 return new (&mem_root) Field_null(ptr, field_length, field_name);
1893void TableShare::refreshVersion()
1895 version= g_refresh_version;
static const int MAX_STRING_LENGTH
static const int MAX_STRING_LENGTH
enum_table_category table_category
bool fieldInPrimaryKey(Field *field) const
TODO: Rename this file - func.h is stupid.
@ TABLE_CATEGORY_TEMPORARY