19#include <plugin/myisam/myisam.h>
21#include <drizzled/error.h>
22#include <drizzled/gettext.h>
23#include <drizzled/data_home.h>
24#include <drizzled/sql_parse.h>
25#include <drizzled/sql_lex.h>
26#include <drizzled/session.h>
27#include <drizzled/sql_base.h>
28#include <drizzled/lock.h>
29#include <drizzled/item/int.h>
30#include <drizzled/item/empty_string.h>
31#include <drizzled/transaction_services.h>
32#include <drizzled/transaction_services.h>
33#include <drizzled/table_proto.h>
34#include <drizzled/plugin/client.h>
35#include <drizzled/identifier.h>
36#include <drizzled/internal/m_string.h>
37#include <drizzled/charset.h>
38#include <drizzled/definition/cache.h>
39#include <drizzled/system_variables.h>
40#include <drizzled/statement/alter_table.h>
42#include <drizzled/pthread_globals.h>
43#include <drizzled/typelib.h>
44#include <drizzled/plugin/storage_engine.h>
45#include <drizzled/diagnostics_area.h>
46#include <drizzled/open_tables_state.h>
47#include <drizzled/table/cache.h>
48#include <drizzled/create_field.h>
49#include <drizzled/key_part_info.h>
54#include <unordered_set>
60bool is_primary_key(
const char* name)
62 return strcmp(name,
"PRIMARY") == 0;
65static bool check_if_keyname_exists(
const char *name,
KeyInfo *start,
KeyInfo *end);
66static const char *make_unique_key_name(
const char *field_name,
KeyInfo *start,
KeyInfo *end);
69void set_table_default_charset(
const identifier::Catalog &catalog, HA_CREATE_INFO *create_info,
const char *db)
76 if (not create_info->default_table_charset)
77 create_info->default_table_charset= plugin::StorageEngine::getSchemaCollation(
identifier::Schema(catalog, str_ref(db)));
111 util::string::vector wrong_tables;
113 bool foreign_key_error=
false;
117 boost::mutex::scoped_lock scopedLock(table::Cache::mutex());
119 if (not drop_temporary && session->lock_table_names_exclusively(tables))
125 session->no_warnings_for_error= 1;
127 for (table= tables; table; table= table->next_local)
130 table->getSchemaName(),
131 table->getTableName());
133 error= session->open_tables.drop_temporary_table(tmp_identifier);
147 if (drop_temporary ==
false)
149 abort_locked_tables(session, tmp_identifier);
150 table::Cache::removeTable(*session, tmp_identifier, RTFC_WAIT_OTHER_THREAD_FLAG | RTFC_CHECK_KILLED_FLAG);
155 Table *locked_table= drop_locked_tables(session, tmp_identifier);
157 table->table= locked_table;
159 if (session->getKilled())
166 table->getSchemaName(),
167 table->getTableName(),
168 table->getInternalTmpTable() ? message::Table::INTERNAL : message::Table::STANDARD);
170 message::table::shared_ptr message= plugin::StorageEngine::getTableMessage(*session, identifier,
true);
177 push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
178 ER_BAD_TABLE_ERROR, ER(ER_BAD_TABLE_ERROR),
179 table->getTableName());
188 drizzled::error_t local_error;
191 if (plugin::StorageEngine::dropTable(*session, identifier, local_error))
200 if (local_error == HA_ERR_NO_SUCH_TABLE and if_exists)
203 session->clear_error();
206 if (local_error == HA_ERR_ROW_IS_REFERENCED)
209 foreign_key_error=
true;
217 wrong_tables.push_back(table->getTableName());
221 tables->unlock_table_names();
225 if (wrong_tables.size())
227 if (not foreign_key_error)
229 std::string table_error;
231 for (util::string::vector::reference iter : wrong_tables)
236 table_error.resize(table_error.size() -1);
238 my_printf_error(ER_BAD_TABLE_ERROR, ER(ER_BAD_TABLE_ERROR), MYF(0),
239 table_error.c_str());
243 my_message(ER_ROW_IS_REFERENCED, ER(ER_ROW_IS_REFERENCED), MYF(0));
248 session->no_warnings_for_error= 0;
268 ulong a_flags= a->flags, b_flags= b->flags;
270 if (a_flags & HA_NOSAME)
272 if (!(b_flags & HA_NOSAME))
274 if ((a_flags ^ b_flags) & (HA_NULL_PART_KEY))
277 return (a_flags & (HA_NULL_PART_KEY)) ? 1 : -1;
279 if (is_primary_key(a->name))
281 if (is_primary_key(b->name))
284 if ((a_flags ^ b_flags) & HA_KEY_HAS_PART_KEY_SEG)
285 return (a_flags & HA_KEY_HAS_PART_KEY_SEG) ? 1 : -1;
287 else if (b_flags & HA_NOSAME)
294 return ((a->usable_key_parts < b->usable_key_parts) ? -1 :
295 (a->usable_key_parts > b->usable_key_parts) ? 1 :
318class typelib_set_member
324 typelib_set_member(
const char* value,
unsigned int length,
333 return (my_strnncoll(a.cs,
334 (
const unsigned char*)a.s.c_str(), a.s.length(),
335 (
const unsigned char*)b.s.c_str(), b.s.length())==0);
341class typelib_set_member_hasher
343 boost::hash<string> hasher;
345 std::size_t operator()(
const typelib_set_member& t)
const
352static bool check_duplicates_in_interval(
const char *set_or_name,
353 const char *name,
TYPELIB *typelib,
355 unsigned int *dup_val_count)
358 const char **cur_value= typelib->type_names;
359 unsigned int *cur_length= typelib->type_lengths;
362 std::unordered_set<typelib_set_member, typelib_set_member_hasher> interval_set;
364 for ( ; tmp.count > 0; cur_value++, cur_length++)
371 my_error(ER_DUPLICATED_VALUE_IN_TYPE, MYF(0),
372 name,*cur_value,set_or_name);
400static void calculate_interval_lengths(
const charset_info_st *
const cs,
402 uint32_t *max_length,
403 uint32_t *tot_length)
407 *max_length= *tot_length= 0;
408 for (pos= interval->type_names, len= interval->type_lengths;
411 uint32_t length= cs->cset->numchars(cs, *pos, *pos + *len);
412 *tot_length+= length;
413 set_if_bigger(*max_length, (uint32_t)length);
435 uint32_t *blob_columns,
437 int *timestamps_with_niladic)
439 unsigned int dup_val_count;
445 assert(sql_field->charset);
447 switch (sql_field->sql_type) {
448 case DRIZZLE_TYPE_BLOB:
449 sql_field->length= 8;
453 case DRIZZLE_TYPE_ENUM:
455 if (check_duplicates_in_interval(
"ENUM",
456 sql_field->field_name,
466 case DRIZZLE_TYPE_MICROTIME:
467 case DRIZZLE_TYPE_TIMESTAMP:
469 if (sql_field->unireg_check == Field::TIMESTAMP_OLD_FIELD)
473 sql_field->unireg_check= Field::TIMESTAMP_DNUN_FIELD;
474 (*timestamps_with_niladic)++;
478 sql_field->unireg_check= Field::NONE;
481 else if (sql_field->unireg_check != Field::NONE)
483 (*timestamps_with_niladic)++;
490 case DRIZZLE_TYPE_BOOLEAN:
491 case DRIZZLE_TYPE_DATE:
492 case DRIZZLE_TYPE_DATETIME:
493 case DRIZZLE_TYPE_DECIMAL:
494 case DRIZZLE_TYPE_DOUBLE:
495 case DRIZZLE_TYPE_LONG:
496 case DRIZZLE_TYPE_LONGLONG:
497 case DRIZZLE_TYPE_NULL:
498 case DRIZZLE_TYPE_TIME:
499 case DRIZZLE_TYPE_UUID:
500 case DRIZZLE_TYPE_IPV6:
501 case DRIZZLE_TYPE_VARCHAR:
509 HA_CREATE_INFO *create_info,
510 message::Table &create_proto,
513 uint32_t *db_options,
516 int select_field_count)
518 const char *key_name;
520 uint field,blob_columns,max_key_length;
521 ulong record_offset= 0;
524 int timestamps= 0, timestamps_with_niladic= 0;
526 int select_field_pos,auto_increment=0;
527 List<CreateField>::iterator it(alter_info->create_list.begin());
528 List<CreateField>::iterator it2(alter_info->create_list.begin());
532 select_field_pos= alter_info->create_list.size() - select_field_count;
534 max_key_length= engine->max_key_length();
536 for (int32_t field_no=0; (sql_field=it++) ; field_no++)
545 sql_field->
length= sql_field->char_length;
548 sql_field->
charset= create_info->default_table_charset;
556 if (create_info->table_charset && sql_field->
charset != &my_charset_bin)
557 sql_field->
charset= create_info->table_charset;
560 if ((sql_field->flags & BINCMP_FLAG) &&
561 !(sql_field->
charset= get_charset_by_csname(sql_field->
charset->csname, MY_CS_BINSORT)))
564 snprintf(tmp,
sizeof(tmp),
"%.*s_bin", (
int)
sizeof(tmp)-5,
566 my_error(ER_UNKNOWN_COLLATION, MYF(0), tmp);
574 if (sql_field->
def &&
575 save_cs != sql_field->
def->collation.collation &&
576 (sql_field->
sql_type == DRIZZLE_TYPE_ENUM))
588 sql_field->
def= sql_field->
def->safe_charset_converter(save_cs);
590 if (sql_field->
def == NULL)
593 my_error(ER_INVALID_DEFAULT, MYF(0), sql_field->
field_name);
598 if (sql_field->
sql_type == DRIZZLE_TYPE_ENUM)
615 interval= sql_field->
interval= typelib(*session->mem_root, sql_field->interval_list);
617 List<String>::iterator int_it(sql_field->interval_list.begin());
620 int comma_length= cs->cset->wc_mb(cs,
',', (
unsigned char*) comma_buf, (
unsigned char*) comma_buf +
sizeof(comma_buf));
621 assert(comma_length > 0);
623 for (uint32_t i= 0; (tmp= int_it++); i++)
626 if (String::needs_conversion(tmp->length(), tmp->charset(), cs))
628 conv.copy(tmp->ptr(), tmp->length(), cs);
629 interval->type_names[i]= session->mem.strdup(conv);
630 interval->type_lengths[i]= conv.length();
634 lengthsp= cs->cset->lengthsp(cs, interval->type_names[i], interval->type_lengths[i]);
635 interval->type_lengths[i]= lengthsp;
636 ((
unsigned char *)interval->type_names[i])[lengthsp]=
'\0';
638 sql_field->interval_list.clear();
643 uint32_t field_length;
644 assert(sql_field->
sql_type == DRIZZLE_TYPE_ENUM);
645 if (sql_field->
def != NULL)
650 if (sql_field->flags & NOT_NULL_FLAG)
652 my_error(ER_INVALID_DEFAULT, MYF(0), sql_field->
field_name);
660 def->length(cs->cset->lengthsp(cs, def->ptr(), def->length()));
661 if (interval->find_type2(def->ptr(), def->length(), cs) == 0)
663 my_error(ER_INVALID_DEFAULT, MYF(0), sql_field->
field_name);
669 calculate_interval_lengths(cs, interval, &field_length, &new_dummy);
670 sql_field->
length= field_length;
672 set_if_smaller(sql_field->
length, (uint32_t)MAX_FIELD_WIDTH-1);
676 if (prepare_blob_field(session, sql_field))
681 my_error(ER_WRONG_COLUMN_NAME, MYF(0), sql_field->
field_name);
686 for (dup_no=0; (dup_field=it2++) != sql_field; dup_no++)
694 if (field_no < select_field_pos || dup_no >= select_field_pos)
696 my_error(ER_DUP_FIELDNAME, MYF(0), sql_field->
field_name);
702 sql_field->
def= dup_field->
def;
706 create_info->default_table_charset);
707 sql_field->
length= dup_field->char_length;
708 sql_field->pack_length= dup_field->pack_length;
709 sql_field->key_length= dup_field->key_length;
710 sql_field->decimals= dup_field->decimals;
712 sql_field->unireg_check= dup_field->unireg_check;
713 sql_field->flags= dup_field->flags;
723 if (not create_proto.engine().name().compare(
"MyISAM") &&
724 ((sql_field->flags & BLOB_FLAG) ||
725 (sql_field->
sql_type == DRIZZLE_TYPE_VARCHAR)))
727 (*db_options)|= HA_OPTION_PACK_RECORD;
730 it2= alter_info->create_list.begin();
734 it= alter_info->create_list.begin();
735 while ((sql_field=it++))
737 assert(sql_field->
charset != 0);
739 if (prepare_create_field(sql_field, &blob_columns,
740 ×tamps, ×tamps_with_niladic))
742 sql_field->offset= record_offset;
743 if (MTYP_TYPENR(sql_field->unireg_check) == Field::NEXT_NUMBER)
746 if (timestamps_with_niladic > 1)
748 my_message(ER_TOO_MUCH_AUTO_TIMESTAMP_COLS,
749 ER(ER_TOO_MUCH_AUTO_TIMESTAMP_COLS), MYF(0));
752 if (auto_increment > 1)
754 my_message(ER_WRONG_AUTO_KEY, ER(ER_WRONG_AUTO_KEY), MYF(0));
757 if (auto_increment &&
758 (engine->check_flag(HTON_BIT_NO_AUTO_INCREMENT)))
760 my_message(ER_TABLE_CANT_HANDLE_AUTO_INCREMENT,
761 ER(ER_TABLE_CANT_HANDLE_AUTO_INCREMENT), MYF(0));
765 if (blob_columns && (engine->check_flag(HTON_BIT_NO_BLOBS)))
767 my_message(ER_TABLE_CANT_HANDLE_BLOB, ER(ER_TABLE_CANT_HANDLE_BLOB),
774 List<Key>::iterator key_iterator(alter_info->key_list.begin());
775 List<Key>::iterator key_iterator2(alter_info->key_list.begin());
776 uint32_t key_parts=0;
777 bool primary_key=0,unique_key=0;
779 uint32_t tmp, key_number;
781 static char ignore_key[1];
786 while ((key=key_iterator++))
788 if (key->type == Key::FOREIGN_KEY)
790 if (((
Foreign_key *)key)->validate(alter_info->create_list))
795 add_foreign_key_to_table_message(&create_proto,
804 if (fk_key->ref_columns.size() &&
805 fk_key->ref_columns.size() != fk_key->columns.size())
807 my_error(ER_WRONG_FK_DEF, MYF(0),
808 (fk_key->name.data() ? fk_key->name.data() :
"foreign key without name"),
809 ER(ER_KEY_REF_DO_NOT_MATCH_TABLE_REF));
815 tmp= engine->max_key_parts();
816 if (key->columns.size() > tmp)
818 my_error(ER_TOO_MANY_KEY_PARTS,MYF(0),tmp);
821 if (check_identifier_name(key->name, ER_TOO_LONG_IDENT))
823 key_iterator2= alter_info->key_list.begin();
824 if (key->type != Key::FOREIGN_KEY)
826 while ((key2 = key_iterator2++) != key)
833 if ((key2->type != Key::FOREIGN_KEY &&
834 key2->name.data() != ignore_key &&
835 !foreign_key_prefix(key, key2)))
839 if (!key2->generated ||
840 (key->generated && key->columns.size() <
841 key2->columns.size()))
842 key->name.assign(ignore_key, 1);
845 key2->name.assign(ignore_key, 1);
846 key_parts-= key2->columns.size();
853 if (key->name.data() != ignore_key)
854 key_parts+=key->columns.size();
857 if (key->name.data() && !tmp_table && (key->type != Key::PRIMARY) && is_primary_key(key->name.data()))
859 my_error(ER_WRONG_NAME_FOR_INDEX, MYF(0), key->name.data());
863 tmp= engine->max_keys();
864 if (*key_count > tmp)
866 my_error(ER_TOO_MANY_KEYS,MYF(0),tmp);
870 (*key_info_buffer)= key_info= (
KeyInfo*) memory::sql_calloc(
sizeof(
KeyInfo) * (*key_count));
873 key_iterator= alter_info->key_list.begin();
875 for (; (key=key_iterator++) ; key_number++)
877 uint32_t key_length=0;
880 if (key->name.data() == ignore_key)
885 while (key && key->name.data() == ignore_key);
894 case Key::FOREIGN_KEY:
898 key_info->flags = HA_NOSAME;
902 key_info->flags|= HA_GENERATED_KEY;
904 key_info->key_parts=(uint8_t) key->columns.size();
905 key_info->key_part=key_part_info;
906 key_info->usable_key_parts= key_number;
907 key_info->algorithm= key->key_create_info.algorithm;
909 uint32_t tmp_len= system_charset_info->cset->charpos(system_charset_info,
910 key->key_create_info.comment.begin(),
911 key->key_create_info.comment.end(),
912 INDEX_COMMENT_MAXLEN);
914 if (tmp_len < key->key_create_info.comment.size())
916 my_error(ER_WRONG_STRING_LENGTH, MYF(0), key->key_create_info.comment.data(),
"INDEX COMMENT", (uint32_t) INDEX_COMMENT_MAXLEN);
920 key_info->comment.assign(key_info->comment.data(), key->key_create_info.comment.size());
921 if (key_info->comment.size() > 0)
923 key_info->flags|= HA_USES_COMMENT;
924 key_info->comment.assign(key->key_create_info.comment.data(), key_info->comment.size());
927 message::Table::Field *protofield= NULL;
929 List<Key_part_spec>::iterator cols(key->columns.begin());
930 List<Key_part_spec>::iterator cols2(key->columns.begin());
931 for (uint32_t column_nr=0 ; (column=cols++) ; column_nr++)
935 int proto_field_nr= 0;
937 it= alter_info->create_list.begin();
939 while ((sql_field=it++) && ++proto_field_nr &&
940 system_charset_info->strcasecmp(column->field_name.data(), sql_field->
field_name))
947 my_error(ER_KEY_COLUMN_DOES_NOT_EXITS, MYF(0), column->field_name.data());
951 while ((dup_column= cols2++) != column)
953 if (!system_charset_info->strcasecmp(column->field_name.data(), dup_column->field_name.data()))
955 my_printf_error(ER_DUP_FIELDNAME,
956 ER(ER_DUP_FIELDNAME),MYF(0),
957 column->field_name.data());
961 cols2= key->columns.begin();
963 if (create_proto.field_size() > 0)
964 protofield= create_proto.mutable_field(proto_field_nr - 1);
967 column->length*= sql_field->
charset->mbmaxlen;
969 if (sql_field->
sql_type == DRIZZLE_TYPE_BLOB)
971 if (! (engine->check_flag(HTON_BIT_CAN_INDEX_BLOBS)))
973 my_error(ER_BLOB_USED_AS_KEY, MYF(0), column->field_name.data());
976 if (! column->length)
978 my_error(ER_BLOB_KEY_WITHOUT_LENGTH, MYF(0), column->field_name.data());
983 if (! (sql_field->flags & NOT_NULL_FLAG))
985 if (key->type == Key::PRIMARY)
988 sql_field->flags|= NOT_NULL_FLAG;
991 message::Table::Field::FieldConstraints *constraints;
992 constraints= protofield->mutable_constraints();
993 constraints->set_is_notnull(
true);
999 key_info->flags|= HA_NULL_PART_KEY;
1000 if (! (engine->check_flag(HTON_BIT_NULL_IN_KEY)))
1002 my_error(ER_NULL_COLUMN_IN_INDEX, MYF(0), column->field_name.data());
1008 if (MTYP_TYPENR(sql_field->unireg_check) == Field::NEXT_NUMBER)
1010 if (column_nr == 0 || (engine->check_flag(HTON_BIT_AUTO_PART_KEY)))
1015 key_part_info->fieldnr= field;
1016 key_part_info->offset= (uint16_t) sql_field->offset;
1017 key_part_info->key_type= 0;
1018 length= sql_field->key_length;
1022 if (sql_field->
sql_type == DRIZZLE_TYPE_BLOB)
1024 if ((length=column->length) > max_key_length ||
1025 length > engine->max_key_part_length())
1027 length= min(max_key_length, engine->max_key_part_length());
1028 if (key->type == Key::MULTIPLE)
1031 char warn_buff[DRIZZLE_ERRMSG_SIZE];
1032 snprintf(warn_buff,
sizeof(warn_buff), ER(ER_TOO_LONG_KEY),
1034 push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1035 ER_TOO_LONG_KEY, warn_buff);
1037 length-= length % sql_field->
charset->mbmaxlen;
1041 my_error(ER_TOO_LONG_KEY,MYF(0),length);
1046 else if ((column->length > length ||
1049 my_message(ER_WRONG_SUB_KEY, ER(ER_WRONG_SUB_KEY), MYF(0));
1052 else if (! (engine->check_flag(HTON_BIT_NO_PREFIX_CHAR_KEYS)))
1054 length=column->length;
1057 else if (length == 0)
1059 my_error(ER_WRONG_KEY_COLUMN, MYF(0), column->field_name.data());
1062 if (length > engine->max_key_part_length())
1064 length= engine->max_key_part_length();
1065 if (key->type == Key::MULTIPLE)
1068 char warn_buff[DRIZZLE_ERRMSG_SIZE];
1069 snprintf(warn_buff,
sizeof(warn_buff), ER(ER_TOO_LONG_KEY),
1071 push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1072 ER_TOO_LONG_KEY, warn_buff);
1074 length-= length % sql_field->
charset->mbmaxlen;
1078 my_error(ER_TOO_LONG_KEY,MYF(0),length);
1082 key_part_info->length=(uint16_t) length;
1084 if (!((*db_options) & HA_OPTION_NO_PACK_KEYS) &&
1086 (sql_field->
sql_type == DRIZZLE_TYPE_VARCHAR ||
1087 sql_field->
sql_type == DRIZZLE_TYPE_BLOB)))
1089 if ((column_nr == 0 && sql_field->
sql_type == DRIZZLE_TYPE_BLOB) ||
1090 sql_field->
sql_type == DRIZZLE_TYPE_VARCHAR)
1092 key_info->flags|= HA_BINARY_PACK_KEY | HA_VAR_LENGTH_KEY;
1096 key_info->flags|= HA_PACK_KEY;
1100 if (length != sql_field->key_length)
1101 key_info->flags|= HA_KEY_HAS_PART_KEY_SEG;
1109 if (key->type == Key::PRIMARY)
1113 my_message(ER_MULTIPLE_PRI_KEY, ER(ER_MULTIPLE_PRI_KEY),
1117 static const char pkey_name[]=
"PRIMARY";
1121 else if (!(key_name= key->name.data()))
1122 key_name=make_unique_key_name(sql_field->
field_name,
1123 *key_info_buffer, key_info);
1124 if (check_if_keyname_exists(key_name, *key_info_buffer, key_info))
1126 my_error(ER_DUP_KEYNAME, MYF(0), key_name);
1129 key_info->name=(
char*) key_name;
1133 if (!key_info->name || check_column_name(key_info->name))
1135 my_error(ER_WRONG_NAME_FOR_INDEX, MYF(0), key_info->name);
1139 if (!(key_info->flags & HA_NULL_PART_KEY))
1144 key_info->key_length=(uint16_t) key_length;
1146 if (key_length > max_key_length)
1148 my_error(ER_TOO_LONG_KEY,MYF(0),max_key_length);
1155 if (!unique_key && !primary_key &&
1156 (engine->check_flag(HTON_BIT_REQUIRE_PRIMARY_KEY)))
1158 my_message(ER_REQUIRES_PRIMARY_KEY, ER(ER_REQUIRES_PRIMARY_KEY), MYF(0));
1162 if (auto_increment > 0)
1164 my_message(ER_WRONG_AUTO_KEY, ER(ER_WRONG_AUTO_KEY), MYF(0));
1168 internal::my_qsort((
unsigned char*) *key_info_buffer, *key_count,
sizeof(
KeyInfo),
1172 it= alter_info->create_list.begin();
1173 while ((sql_field=it++))
1175 Field::utype
type= (Field::utype) MTYP_TYPENR(sql_field->unireg_check);
1177 if (session->variables.sql_mode & MODE_NO_ZERO_DATE &&
1179 (sql_field->
sql_type == DRIZZLE_TYPE_TIMESTAMP or sql_field->
sql_type == DRIZZLE_TYPE_MICROTIME) &&
1180 (sql_field->flags & NOT_NULL_FLAG) &&
1181 (
type == Field::NONE ||
type == Field::TIMESTAMP_UN_FIELD))
1197 my_error(ER_INVALID_DEFAULT, MYF(0), sql_field->
field_name);
1218static bool prepare_blob_field(Session *,
1219 CreateField *sql_field)
1222 if (sql_field->length > MAX_FIELD_VARCHARLENGTH &&
1223 !(sql_field->flags & BLOB_FLAG))
1225 my_error(ER_TOO_BIG_FIELDLENGTH, MYF(0), sql_field->field_name,
1226 MAX_FIELD_VARCHARLENGTH / sql_field->charset->mbmaxlen);
1230 if ((sql_field->flags & BLOB_FLAG) && sql_field->length)
1232 if (sql_field->sql_type == DRIZZLE_TYPE_BLOB)
1235 sql_field->pack_length= calc_pack_length(sql_field->sql_type, 0);
1237 sql_field->length= 0;
1242static bool locked_create_event(
Session *session,
1244 HA_CREATE_INFO *create_info,
1245 message::Table &table_proto,
1247 bool is_if_not_exists,
1248 bool internal_tmp_table,
1264 identifier.getType() != message::Table::STANDARD );
1268 if (is_if_not_exists)
1271 push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1272 ER_TABLE_EXISTS_ERROR, ER(ER_TABLE_EXISTS_ERROR),
1273 identifier.getTableName().c_str());
1274 create_info->table_existed= 1;
1278 my_error(ER_TABLE_EXISTS_ERROR, identifier);
1283 if (identifier.getType() == message::Table::STANDARD)
1296 if (definition::Cache::find(identifier.getKey()))
1298 my_error(ER_TABLE_EXISTS_ERROR, identifier);
1305 session->set_proc_info(
"creating table");
1306 create_info->table_existed= 0;
1308 create_info->table_options= db_options;
1310 if (not rea_create_table(session, identifier,
1312 create_info, alter_info->create_list,
1313 key_count, key_info_buffer))
1318 if (identifier.getType() == message::Table::TEMPORARY)
1321 if (not (session->open_temporary_table(identifier)))
1323 (void) session->open_tables.rm_temporary_table(identifier);
1334 if (table_proto.type() == message::Table::STANDARD && not internal_tmp_table)
1373bool create_table_no_lock(
Session *session,
1375 HA_CREATE_INFO *create_info,
1376 message::Table &table_proto,
1378 bool internal_tmp_table,
1379 uint32_t select_field_count,
1380 bool is_if_not_exists)
1382 uint db_options, key_count;
1387 if (not alter_info->create_list.size())
1389 my_message(ER_TABLE_MUST_HAVE_COLUMNS, ER(ER_TABLE_MUST_HAVE_COLUMNS),
1393 assert(identifier.getTableName() == table_proto.name());
1394 db_options= create_info->table_options;
1396 set_table_default_charset(session->catalog().identifier(),create_info, identifier.getSchemaName().c_str());
1402 &key_info_buffer, &key_count,
1403 select_field_count))
1405 boost::mutex::scoped_lock lock(table::Cache::mutex());
1406 error= locked_create_event(session,
1413 db_options, key_count,
1417 session->set_proc_info(
"After create");
1427 HA_CREATE_INFO *create_info,
1428 message::Table &table_proto,
1430 bool internal_tmp_table,
1431 uint32_t select_field_count,
1432 bool is_if_not_exists)
1434 Table *name_lock= session->lock_table_name_if_not_cached(identifier);
1436 if (name_lock == NULL)
1438 if (is_if_not_exists)
1440 push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1441 ER_TABLE_EXISTS_ERROR, ER(ER_TABLE_EXISTS_ERROR),
1442 identifier.getTableName().c_str());
1443 create_info->table_existed= 1;
1448 my_error(ER_TABLE_EXISTS_ERROR, identifier);
1454 result= create_table_no_lock(session,
1466 boost::mutex::scoped_lock lock(table::Cache::mutex());
1467 session->unlink_open_table(name_lock);
1477bool create_table(Session *session,
1478 const identifier::Table &identifier,
1479 HA_CREATE_INFO *create_info,
1480 message::Table &table_proto,
1481 AlterInfo *alter_info,
1482 bool internal_tmp_table,
1483 uint32_t select_field_count,
1484 bool is_if_not_exists)
1486 if (identifier.isTmp())
1488 return create_table_no_lock(session,
1514check_if_keyname_exists(
const char *name,
KeyInfo *start,
KeyInfo *end)
1516 for (
KeyInfo *key= start; key != end; key++)
1518 if (!system_charset_info->strcasecmp(name, key->name))
1526make_unique_key_name(
const char *field_name,
KeyInfo *start,
KeyInfo *end)
1528 char buff[MAX_FIELD_NAME],*buff_end;
1530 if (not check_if_keyname_exists(field_name,start,end) && not is_primary_key(field_name))
1535 buff_end= strncpy(buff, field_name,
sizeof(buff)-4);
1536 buff_end+= strlen(buff);
1542 for (uint32_t i=2 ; i< 100; i++)
1545 internal::int10_to_str(i, buff_end+1, 10);
1546 if (!check_if_keyname_exists(buff,start,end))
1547 return memory::sql_strdup(buff);
1549 return (
char*)
"not_specified";
1580 if (not plugin::StorageEngine::doesSchemaExist(to))
1582 my_error(ER_NO_DB_ERROR, MYF(0), to.getSchemaName().c_str());
1586 int error= base->renameTable(session, from, to);
1587 if (error == HA_ERR_WRONG_COMMAND)
1588 my_error(ER_NOT_SUPPORTED_YET, MYF(0),
"ALTER Table");
1591 my_error(ER_ERROR_ON_RENAME, MYF(0),
1592 from.isTmp() ?
"#sql-temporary" : from.getSQLPath().c_str(),
1593 to.isTmp() ?
"#sql-temporary" : to.getSQLPath().c_str(), error);
1618void wait_while_table_is_used(
Session *session,
Table *table,
1619 enum ha_extra_function function)
1622 safe_mutex_assert_owner(table::Cache::mutex().native_handle());
1624 table->cursor->extra(function);
1626 session->abortLock(table);
1629 identifier::Table identifier(session->catalog().identifier(),table->getShare()->getSchemaName(), table->getShare()->getTableName());
1630 table::Cache::removeTable(*session, identifier, RTFC_WAIT_OTHER_THREAD_FLAG);
1650void Session::close_cached_table(
Table *table)
1653 wait_while_table_is_used(
this, table, HA_EXTRA_FORCE_REOPEN);
1655 if (open_tables.lock)
1657 unlockTables(open_tables.lock);
1658 open_tables.lock= NULL;
1674 const char *operator_name,
1675 thr_lock_type lock_type,
1676 bool open_for_modify,
1680 Select_Lex *select= &session->lex().
select_lex;
1686 if (! session->endActiveTransaction())
1689 field_list.push_back(item =
new Item_empty_string(
"Table", NAME_CHAR_LEN * 2, cs));
1690 item->maybe_null = 1;
1692 item->maybe_null = 1;
1694 item->maybe_null = 1;
1696 item->maybe_null = 1;
1697 session->getClient()->sendFields(field_list);
1699 for (table= tables; table; table= table->next_local)
1701 identifier::Table table_identifier(session->catalog().identifier(), table->getSchemaName(), table->getTableName());
1704 std::string table_name = table_identifier.getSQLPath();
1706 table->lock_type= lock_type;
1709 TableList *save_next_global, *save_next_local;
1714 select->table_list.first= (
unsigned char*)table;
1720 session->lex().query_tables= table;
1721 session->lex().query_tables_last= &table->
next_global;
1722 session->lex().query_tables_own_last= 0;
1723 session->no_warnings_for_error= 0;
1725 session->openTablesLock(table);
1726 session->no_warnings_for_error= 0;
1741 if (!session->main_da().m_warn_list.size())
1742 push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
1743 ER_CHECK_NO_SUCH_TABLE, ER(ER_CHECK_NO_SUCH_TABLE));
1744 result_code= HA_ADMIN_CORRUPT;
1748 if ((table->table->db_stat & HA_READ_ONLY) && open_for_modify)
1750 char buff[FN_REFLEN + DRIZZLE_ERRMSG_SIZE];
1752 session->getClient()->store(table_name.c_str());
1753 session->getClient()->store(operator_name);
1754 session->getClient()->store(STRING_WITH_LEN(
"error"));
1755 length= snprintf(buff,
sizeof(buff), ER(ER_OPEN_AS_READONLY),
1756 table_name.c_str());
1757 session->getClient()->store(buff, length);
1759 session->endTransaction(COMMIT);
1760 session->close_thread_tables();
1761 session->lex().reset_query_tables_list(
false);
1763 if (session->getClient()->flush())
1769 if (lock_type == TL_WRITE && table->table->getShare()->getVersion())
1771 table::Cache::mutex().lock();
1772 const char *old_message=session->enter_cond(COND_refresh, table::Cache::mutex(),
1773 "Waiting to get writelock");
1774 session->abortLock(table->table);
1775 identifier::Table identifier(session->catalog().identifier(),table->table->getShare()->getSchemaName(), table->table->getShare()->getTableName());
1776 table::Cache::removeTable(*session, identifier, RTFC_WAIT_OTHER_THREAD_FLAG | RTFC_CHECK_KILLED_FLAG);
1777 session->exit_cond(old_message);
1778 if (session->getKilled())
1783 result_code = (table->table->cursor->*operator_func)(session);
1787 session->lex().cleanup_after_one_table_open();
1788 session->clear_error();
1792 session->getClient()->store(table_name.c_str());
1793 session->getClient()->store(operator_name);
1794 session->getClient()->store(warning_level_names[err->level].data(), warning_level_names[err->level].size());
1795 session->getClient()->store(err->msg);
1796 if (session->getClient()->flush())
1799 drizzle_reset_errors(*session,
true);
1801 session->getClient()->store(table_name.c_str());
1802 session->getClient()->store(operator_name);
1804 switch (result_code) {
1805 case HA_ADMIN_NOT_IMPLEMENTED:
1807 char buf[ERRMSGSIZE+20];
1808 uint32_t length=snprintf(buf, ERRMSGSIZE,
1809 ER(ER_CHECK_NOT_IMPLEMENTED), operator_name);
1810 session->getClient()->store(STRING_WITH_LEN(
"note"));
1811 session->getClient()->store(buf, length);
1816 session->getClient()->store(STRING_WITH_LEN(
"status"));
1817 session->getClient()->store(STRING_WITH_LEN(
"OK"));
1820 case HA_ADMIN_FAILED:
1821 session->getClient()->store(STRING_WITH_LEN(
"status"));
1822 session->getClient()->store(STRING_WITH_LEN(
"Operation failed"));
1825 case HA_ADMIN_REJECT:
1826 session->getClient()->store(STRING_WITH_LEN(
"status"));
1827 session->getClient()->store(STRING_WITH_LEN(
"Operation need committed state"));
1828 open_for_modify=
false;
1831 case HA_ADMIN_ALREADY_DONE:
1832 session->getClient()->store(STRING_WITH_LEN(
"status"));
1833 session->getClient()->store(STRING_WITH_LEN(
"Table is already up to date"));
1836 case HA_ADMIN_CORRUPT:
1837 session->getClient()->store(STRING_WITH_LEN(
"error"));
1838 session->getClient()->store(STRING_WITH_LEN(
"Corrupt"));
1842 case HA_ADMIN_INVALID:
1843 session->getClient()->store(STRING_WITH_LEN(
"error"));
1844 session->getClient()->store(STRING_WITH_LEN(
"Invalid argument"));
1849 char buf[ERRMSGSIZE+20];
1850 uint32_t length=snprintf(buf, ERRMSGSIZE,
1851 _(
"Unknown - internal error %d during operation"),
1853 session->getClient()->store(STRING_WITH_LEN(
"error"));
1854 session->getClient()->store(buf, length);
1863 table->table->getMutableShare()->resetVersion();
1865 else if (open_for_modify)
1867 if (table->table->getShare()->getType())
1869 table->table->cursor->info(HA_STATUS_CONST);
1873 boost::unique_lock<boost::mutex> lock(table::Cache::mutex());
1875 table->table->getShare()->getSchemaName(),
1876 table->table->getShare()->getTableName());
1877 table::Cache::removeTable(*session, identifier, RTFC_NO_FLAG);
1882 session->endTransaction(COMMIT);
1883 session->close_thread_tables();
1885 if (session->getClient()->flush())
1894 session->endTransaction(ROLLBACK);
1895 session->close_thread_tables();
1916static bool create_table_wrapper(
Session &session,
1917 const message::Table& create_table_proto,
1925 message::Table new_table_message;
1927 message::table::shared_ptr source_table_message= plugin::StorageEngine::getTableMessage(session, source_identifier);
1929 if (not source_table_message)
1931 my_error(ER_TABLE_UNKNOWN, source_identifier);
1935 new_table_message.CopyFrom(*source_table_message);
1936 new_table_message.set_type(destination_identifier.isTmp() ? message::Table::TEMPORARY : message::Table::STANDARD);
1940 new_table_message.mutable_engine()->set_name(create_table_proto.engine().name());
1944 new_table_message.set_name(create_table_proto.name());
1945 new_table_message.set_schema(create_table_proto.schema());
1946 new_table_message.set_catalog(create_table_proto.catalog());
1950 for (int32_t j= 0; j < new_table_message.fk_constraint_size(); j++)
1952 if (new_table_message.fk_constraint(j).has_name())
1954 std::string name(new_table_message.name());
1957 name.append(
"_ibfk_");
1958 snprintf(number,
sizeof(number),
"%d", j+1);
1959 name.append(number);
1961 message::Table::ForeignKeyConstraint *pfkey= new_table_message.mutable_fk_constraint(j);
1962 pfkey->set_name(name);
1972 if (success && not destination_identifier.isTmp())
1995bool create_like_table(
Session* session,
1998 message::Table &create_table_proto,
1999 bool is_if_not_exists,
2003 bool table_exists=
false;
2011 if (destination_identifier.isTmp())
2013 if (session->open_tables.find_temporary_table(destination_identifier))
2019 bool was_created= create_table_wrapper(*session,
2021 destination_identifier,
2024 if (not was_created)
2026 (void) session->open_tables.rm_temporary_table(destination_identifier,
true);
2028 else if (not session->open_temporary_table(destination_identifier))
2031 (void) session->open_tables.rm_temporary_table(destination_identifier,
true);
2041 Table *name_lock= session->lock_table_name_if_not_cached(destination_identifier);
2054 boost::mutex::scoped_lock lock(table::Cache::mutex());
2055 was_created= create_table_wrapper(*session, create_table_proto, destination_identifier,
2056 source_identifier, is_engine_set);
2061 if (not was_created)
2063 plugin::StorageEngine::dropTable(*session, destination_identifier);
2073 boost::mutex::scoped_lock lock(table::Cache::mutex());
2074 session->unlink_open_table(name_lock);
2080 if (is_if_not_exists)
2082 char warn_buff[DRIZZLE_ERRMSG_SIZE];
2083 snprintf(warn_buff,
sizeof(warn_buff),
2084 ER(ER_TABLE_EXISTS_ERROR), destination_identifier.getTableName().c_str());
2085 push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
2086 ER_TABLE_EXISTS_ERROR, warn_buff);
2090 my_error(ER_TABLE_EXISTS_ERROR, destination_identifier);
2101 return admin_table(session, tables,
"analyze", TL_READ_NO_INSERT,
true, &
Cursor::ha_analyze);
2107 return admin_table(session, tables,
"check", TL_READ_NO_INSERT,
false, &Cursor::ha_check);
enum_field_types sql_type
const charset_info_st * charset
void create_length_to_internal_length(void)
int ha_analyze(Session *)
static bool type_can_have_key_part(enum_field_types)
virtual String * val_str(String *str)=0
void unlink_open_table(Table *find)
static void createTable(Session &, const message::Table &)
static int autocommitOrRollback(Session &, int error)
static void dropTable(Session &, const identifier::Table &identifier, message::table::const_reference table, bool if_exists)
static bool doesTableExist(Session &session, const drizzled::identifier::Table &identifier, bool include_temporary_tables=true)
static bool createTable(Session &session, const identifier::Table &identifier, message::Table &table_message)
#define KEY_DEFAULT_PACK_LENGTH
void broadcast_refresh(void)
Convert from decimal to binary representation.
TODO: Rename this file - func.h is stupid.
static bool drizzle_create_table(Session *session, const identifier::Table &identifier, HA_CREATE_INFO *create_info, message::Table &table_proto, AlterInfo *alter_info, bool internal_tmp_table, uint32_t select_field_count, bool is_if_not_exists)
int(* qsort_cmp)(const void *, const void *)
static int prepare_create_table(Session *session, HA_CREATE_INFO *create_info, message::Table &create_proto, AlterInfo *alter_info, bool tmp_table, uint32_t *db_options, KeyInfo **key_info_buffer, uint32_t *key_count, int select_field_count)