17#include <drizzled/error.h>
18#include <drizzled/session.h>
22#include <drizzled/plugin/storage_engine.h>
24#include <drizzled/internal/my_sys.h>
25#include <drizzled/typelib.h>
26#include <drizzled/util/test.h>
27#include <drizzled/key_part_info.h>
28#include <drizzled/key_info.h>
34#include <drizzled/message/schema.h>
35#include <drizzled/message/table.h>
36#include <google/protobuf/io/zero_copy_stream.h>
37#include <google/protobuf/io/zero_copy_stream_impl.h>
38#include <google/protobuf/message.h>
40#include <drizzled/table_proto.h>
41#include <drizzled/charset.h>
42#include <drizzled/create_field.h>
43#include <drizzled/function/time/typecast.h>
51 message::Table &table_proto,
53 HA_CREATE_INFO *create_info,
58 List<CreateField>::iterator it(create_fields.begin());
59 message::Table::TableOptions *table_options= table_proto.mutable_options();
61 if (create_fields.size() > MAX_FIELDS)
63 my_error(ER_TOO_MANY_FIELDS, MYF(0), ER(ER_TOO_MANY_FIELDS));
67 assert(strcmp(table_proto.engine().name().c_str(),
68 create_info->db_type->getName().c_str())==0);
70 message::schema::shared_ptr schema_message= plugin::StorageEngine::getSchemaDefinition(identifier);
72 if (schema_message and not message::is_replicated(*schema_message))
74 message::set_is_replicated(table_proto,
false);
78 bool use_existing_fields= table_proto.field_size() > 0;
79 while ((field_arg= it++))
81 message::Table::Field *attribute;
87 if (use_existing_fields)
89 attribute= table_proto.mutable_field(field_number++);
94 attribute= table_proto.add_field();
96 if (field_arg->flags & NOT_NULL_FLAG)
98 attribute->mutable_constraints()->set_is_notnull(
true);
101 if (field_arg->flags & UNSIGNED_FLAG and
102 (field_arg->sql_type == DRIZZLE_TYPE_LONGLONG or field_arg->sql_type == DRIZZLE_TYPE_LONG))
104 field_arg->sql_type= DRIZZLE_TYPE_LONGLONG;
105 attribute->mutable_constraints()->set_is_unsigned(
true);
108 attribute->set_name(field_arg->field_name);
111 assert(((field_arg->flags & NOT_NULL_FLAG)) == attribute->constraints().is_notnull());
112 assert(strcmp(attribute->name().c_str(), field_arg->field_name)==0);
115 message::Table::Field::FieldType parser_type= attribute->type();
117 if (field_arg->sql_type == DRIZZLE_TYPE_NULL)
119 my_error(ER_CANT_CREATE_TABLE, MYF(ME_BELL+ME_WAITTANG), table_proto.name().c_str(), -1);
123 if (field_arg->flags & UNSIGNED_FLAG and
124 (field_arg->sql_type == DRIZZLE_TYPE_LONGLONG or field_arg->sql_type == DRIZZLE_TYPE_LONG))
126 message::Table::Field::FieldConstraints *constraints= attribute->mutable_constraints();
128 field_arg->sql_type= DRIZZLE_TYPE_LONGLONG;
129 constraints->set_is_unsigned(
true);
132 attribute->set_type(message::internalFieldTypeToFieldProtoType(field_arg->sql_type));
134 switch (attribute->type()) {
135 case message::Table::Field::BIGINT:
136 case message::Table::Field::INTEGER:
137 case message::Table::Field::DATE:
138 case message::Table::Field::DATETIME:
139 case message::Table::Field::UUID:
140 case message::Table::Field::IPV6:
141 case message::Table::Field::TIME:
142 case message::Table::Field::BOOLEAN:
144 case message::Table::Field::DOUBLE:
150 if (field_arg->decimals != NOT_FIXED_DEC)
152 message::Table::Field::NumericFieldOptions *numeric_field_options;
154 numeric_field_options= attribute->mutable_numeric_options();
156 numeric_field_options->set_precision(field_arg->length);
157 numeric_field_options->set_scale(field_arg->decimals);
161 case message::Table::Field::VARCHAR:
163 message::Table::Field::StringFieldOptions *string_field_options;
165 string_field_options= attribute->mutable_string_options();
167 if (! use_existing_fields || string_field_options->length()==0)
168 string_field_options->set_length(field_arg->length
169 / field_arg->charset->mbmaxlen);
171 assert((uint32_t)string_field_options->length() == (uint32_t)(field_arg->length / field_arg->charset->mbmaxlen));
173 if (! string_field_options->has_collation())
175 string_field_options->set_collation_id(field_arg->charset->number);
176 string_field_options->set_collation(field_arg->charset->name);
180 case message::Table::Field::DECIMAL:
182 message::Table::Field::NumericFieldOptions *numeric_field_options;
184 numeric_field_options= attribute->mutable_numeric_options();
186 numeric_field_options->set_precision(field_arg->length + ( field_arg->decimals ? -2 : -1));
187 numeric_field_options->set_scale(field_arg->decimals);
190 case message::Table::Field::ENUM:
192 message::Table::Field::EnumerationValues *enumeration_options;
194 assert(field_arg->interval);
196 enumeration_options= attribute->mutable_enumeration_values();
198 for (uint32_t pos= 0; pos < field_arg->interval->count; pos++)
200 const char *src= field_arg->interval->type_names[pos];
202 enumeration_options->add_field_value(src);
204 enumeration_options->set_collation_id(field_arg->charset->number);
205 enumeration_options->set_collation(field_arg->charset->name);
209 case message::Table::Field::BLOB:
211 message::Table::Field::StringFieldOptions *string_field_options;
213 string_field_options= attribute->mutable_string_options();
214 string_field_options->set_collation_id(field_arg->charset->number);
215 string_field_options->set_collation(field_arg->charset->name);
220 case message::Table::Field::EPOCH:
222 if (field_arg->sql_type == DRIZZLE_TYPE_MICROTIME)
223 attribute->mutable_time_options()->set_microseconds(
true);
229 assert (!use_existing_fields || parser_type == attribute->type());
232 field_constraints= attribute->mutable_constraints();
233 constraints->set_is_nullable(field_arg->def->null_value);
236 if (not field_arg->comment.empty())
238 uint32_t tmp_len= system_charset_info->cset->charpos(system_charset_info,
239 field_arg->comment.begin(),
240 field_arg->comment.end(),
241 COLUMN_COMMENT_MAXLEN);
243 if (tmp_len < field_arg->comment.size())
245 my_error(ER_WRONG_STRING_LENGTH, MYF(0), field_arg->comment.data(),
"COLUMN COMMENT", (uint32_t) COLUMN_COMMENT_MAXLEN);
249 if (not use_existing_fields)
250 attribute->set_comment(field_arg->comment.data());
252 assert(strcmp(attribute->comment().c_str(), field_arg->comment.data())==0);
255 if (field_arg->unireg_check == Field::NEXT_NUMBER)
257 message::Table::Field::NumericFieldOptions *field_options;
258 field_options= attribute->mutable_numeric_options();
259 field_options->set_is_autoincrement(
true);
262 if (field_arg->unireg_check == Field::TIMESTAMP_DN_FIELD
263 || field_arg->unireg_check == Field::TIMESTAMP_DNUN_FIELD)
265 message::Table::Field::FieldOptions *field_options;
266 field_options= attribute->mutable_options();
267 field_options->set_default_expression(
"CURRENT_TIMESTAMP");
270 if (field_arg->unireg_check == Field::TIMESTAMP_UN_FIELD
271 || field_arg->unireg_check == Field::TIMESTAMP_DNUN_FIELD)
273 message::Table::Field::FieldOptions *field_options;
274 field_options= attribute->mutable_options();
275 field_options->set_update_expression(
"CURRENT_TIMESTAMP");
278 if (field_arg->def == NULL && not attribute->constraints().is_notnull())
280 message::Table::Field::FieldOptions *field_options;
281 field_options= attribute->mutable_options();
283 field_options->set_default_null(
true);
287 message::Table::Field::FieldOptions *field_options;
288 field_options= attribute->mutable_options();
290 if (field_arg->def->is_null())
292 field_options->set_default_null(
true);
297 String *default_value= field_arg->def->val_str(&d);
299 assert(default_value);
301 if ((field_arg->sql_type==DRIZZLE_TYPE_VARCHAR
302 || field_arg->sql_type==DRIZZLE_TYPE_BLOB)
303 && ((field_arg->length / field_arg->charset->mbmaxlen)
304 < default_value->length()))
306 my_error(ER_INVALID_DEFAULT, MYF(0), field_arg->field_name);
310 if (field::isDateTime(field_arg->sql_type))
314 if (field_arg->def->get_date(ltime, TIME_FUZZY_DATE))
316 my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR),
317 default_value->c_str());
331 if (field_arg->sql_type == DRIZZLE_TYPE_DATETIME)
334 typecast->quick_fix_field();
335 typecast->val_str(default_value);
337 else if (field_arg->sql_type == DRIZZLE_TYPE_DATE)
340 typecast->quick_fix_field();
341 typecast->val_str(default_value);
345 if ((field_arg->sql_type == DRIZZLE_TYPE_VARCHAR && field_arg->charset == &my_charset_bin)
346 || (field_arg->sql_type == DRIZZLE_TYPE_BLOB && field_arg->charset == &my_charset_bin))
348 field_options->set_default_bin_value(
string(default_value->c_ptr(), default_value->length()));
352 field_options->set_default_value(default_value->c_ptr());
357 assert(field_arg->unireg_check == Field::NONE
358 || field_arg->unireg_check == Field::NEXT_NUMBER
359 || field_arg->unireg_check == Field::TIMESTAMP_DN_FIELD
360 || field_arg->unireg_check == Field::TIMESTAMP_UN_FIELD
361 || field_arg->unireg_check == Field::TIMESTAMP_DNUN_FIELD);
365 assert(! use_existing_fields || (field_number == table_proto.field_size()));
367 if (create_info->table_options & HA_OPTION_PACK_RECORD)
368 table_options->set_pack_record(
true);
370 if (table_options->has_comment() && table_options->comment().length() == 0)
371 table_options->clear_comment();
373 if (table_options->has_comment())
376 tmp_len= system_charset_info->cset->charpos(system_charset_info,
377 table_options->comment().c_str(),
378 table_options->comment().c_str() +
379 table_options->comment().length(),
380 TABLE_COMMENT_MAXLEN);
382 if (tmp_len < table_options->comment().length())
384 my_error(ER_WRONG_STRING_LENGTH, MYF(0),
385 table_options->comment().c_str(),
"Table COMMENT",
386 (uint32_t) TABLE_COMMENT_MAXLEN);
391 if (create_info->default_table_charset)
393 table_options->set_collation_id(create_info->default_table_charset->number);
394 table_options->set_collation(create_info->default_table_charset->name);
397 if (create_info->used_fields & HA_CREATE_USED_AUTO)
398 table_options->set_has_user_set_auto_increment_value(
true);
400 table_options->set_has_user_set_auto_increment_value(
false);
402 if (create_info->auto_increment_value)
403 table_options->set_auto_increment_value(create_info->auto_increment_value);
405 for (uint32_t i= 0; i < keys; i++)
407 message::Table::Index *idx= table_proto.add_indexes();
409 assert(test(key_info[i].flags & HA_USES_COMMENT) == (key_info[i].comment.size() > 0));
411 idx->set_name(key_info[i].name);
412 idx->set_key_length(key_info[i].key_length);
413 idx->set_is_primary(is_primary_key(key_info[i].name));
415 if (key_info[i].algorithm == message::Table::Index::UNKNOWN_INDEX)
416 idx->set_type(create_info->db_type->default_index_type());
418 idx->set_type(key_info[i].algorithm);
420 if (key_info[i].flags & HA_NOSAME)
421 idx->set_is_unique(
true);
423 idx->set_is_unique(
false);
425 message::Table::Index::Options *index_options= idx->mutable_options();
427 if (key_info[i].flags & HA_USES_BLOCK_SIZE)
428 index_options->set_key_block_size(key_info[i].block_size);
430 if (key_info[i].flags & HA_PACK_KEY)
431 index_options->set_pack_key(
true);
433 if (key_info[i].flags & HA_BINARY_PACK_KEY)
434 index_options->set_binary_pack_key(
true);
436 if (key_info[i].flags & HA_VAR_LENGTH_PART)
437 index_options->set_var_length_key(
true);
439 if (key_info[i].flags & HA_NULL_PART_KEY)
440 index_options->set_null_part_key(
true);
442 if (key_info[i].flags & HA_KEY_HAS_PART_KEY_SEG)
443 index_options->set_has_partial_segments(
true);
445 if (key_info[i].flags & HA_GENERATED_KEY)
446 index_options->set_auto_generated_key(
true);
448 if (key_info[i].flags & HA_USES_COMMENT)
451 tmp_len= system_charset_info->cset->charpos(system_charset_info,
452 key_info[i].comment.begin(),
453 key_info[i].comment.end(),
454 TABLE_COMMENT_MAXLEN);
456 if (tmp_len < key_info[i].comment.size())
458 my_error(ER_WRONG_STRING_LENGTH, MYF(0),
459 key_info[i].comment.data(),
"Index COMMENT",
460 (uint32_t) TABLE_COMMENT_MAXLEN);
464 idx->set_comment(key_info[i].comment.data());
466 static const uint64_t unknown_index_flag= (HA_NOSAME | HA_PACK_KEY |
471 HA_KEY_HAS_PART_KEY_SEG |
474 if (key_info[i].flags & ~unknown_index_flag)
477 for(
unsigned int j=0; j< key_info[i].key_parts; j++)
479 message::Table::Index::IndexPart *idxpart;
480 const int fieldnr= key_info[i].key_part[j].fieldnr;
483 idxpart= idx->add_index_part();
485 idxpart->set_fieldnr(fieldnr);
487 if (table_proto.field(fieldnr).type() == message::Table::Field::VARCHAR
488 || table_proto.field(fieldnr).type() == message::Table::Field::BLOB)
490 uint32_t collation_id;
492 if (table_proto.field(fieldnr).string_options().has_collation_id())
493 collation_id= table_proto.field(fieldnr).string_options().collation_id();
495 collation_id= table_proto.options().collation_id();
499 mbmaxlen= cs->mbmaxlen;
502 idxpart->set_compare_length(key_info[i].key_part[j].length / mbmaxlen);
506 if (not table_proto.IsInitialized())
508 my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
509 table_proto.name().c_str(),
510 table_proto.InitializationErrorString().c_str());
523 table_proto.SerializeToString(&tmp_string);
528 my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
529 table_proto.name().c_str(),
530 table_proto.InitializationErrorString().c_str());
558bool rea_create_table(
Session *session,
560 message::Table &table_proto,
561 HA_CREATE_INFO *create_info,
563 uint32_t keys,
KeyInfo *key_info)
565 assert(table_proto.has_name());
567 if (fill_table_proto(identifier,
568 table_proto, create_fields, create_info,
574 assert(table_proto.name() == identifier.getTableName());
static bool createTable(Session &session, const identifier::Table &identifier, message::Table &table_message)
TODO: Rename this file - func.h is stupid.