21 #include <drizzled/sql_select.h>
23 #include <drizzled/error.h>
24 #include <drizzled/name_resolution_context_state.h>
25 #include <drizzled/probes.h>
26 #include <drizzled/sql_base.h>
27 #include <drizzled/sql_load.h>
28 #include <drizzled/field/epoch.h>
29 #include <drizzled/lock.h>
31 #include <drizzled/pthread_globals.h>
32 #include <drizzled/transaction_services.h>
33 #include <drizzled/plugin/transactional_storage_engine.h>
34 #include <drizzled/select_insert.h>
35 #include <drizzled/select_create.h>
36 #include <drizzled/table/shell.h>
37 #include <drizzled/alter_info.h>
38 #include <drizzled/sql_parse.h>
39 #include <drizzled/sql_lex.h>
40 #include <drizzled/statistics_variables.h>
41 #include <drizzled/session/transactions.h>
42 #include <drizzled/open_tables_state.h>
43 #include <drizzled/table/cache.h>
44 #include <drizzled/create_field.h>
48 extern plugin::StorageEngine *heap_engine;
49 extern plugin::StorageEngine *myisam_engine;
72 static int check_insert_fields(Session *session, TableList *table_list,
73 List<Item> &fields, List<Item> &values,
77 Table *table= table_list->table;
79 if (fields.size() == 0 && values.size() != 0)
81 if (values.size() != table->getShare()->sizeFields())
83 my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), 1L);
86 clear_timestamp_auto_bits(table->timestamp_field_type,
87 TIMESTAMP_AUTO_SET_ON_INSERT);
96 Select_Lex *select_lex= &session->lex().select_lex;
97 Name_resolution_context *context= &select_lex->context;
98 Name_resolution_context_state ctx_state;
101 if (fields.size() != values.size())
103 my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), 1L);
107 session->dup_field= 0;
110 ctx_state.save_state(context, table_list);
116 table_list->next_local= 0;
117 context->resolve_in_table_list_only(table_list);
118 res= setup_fields(session, 0, fields, MARK_COLUMNS_WRITE, 0, 0);
121 ctx_state.restore_state(context, table_list);
126 if (check_unique && session->dup_field)
128 my_error(ER_FIELD_SPECIFIED_TWICE, MYF(0), session->dup_field->field_name);
131 if (table->timestamp_field)
133 if (table->timestamp_field->isWriteSet())
135 clear_timestamp_auto_bits(table->timestamp_field_type,
136 TIMESTAMP_AUTO_SET_ON_INSERT);
140 table->setWriteSet(table->timestamp_field->position());
168 static int check_update_fields(Session *session, TableList *insert_table_list,
169 List<Item> &update_fields,
172 Table *table= insert_table_list->table;
173 bool timestamp_mark=
false;
175 if (table->timestamp_field)
181 timestamp_mark= table->write_set->test(table->timestamp_field->position());
182 table->write_set->reset(table->timestamp_field->position());
186 if (setup_fields(session, 0, update_fields, MARK_COLUMNS_WRITE, 0, 0))
189 if (table->timestamp_field)
192 if (table->timestamp_field->isWriteSet())
194 clear_timestamp_auto_bits(table->timestamp_field_type,
195 TIMESTAMP_AUTO_SET_ON_UPDATE);
200 table->setWriteSet(table->timestamp_field->position());
217 thr_lock_type *lock_type,
218 enum_duplicates duplic,
221 if (duplic == DUP_UPDATE ||
222 (duplic == DUP_REPLACE && *lock_type == TL_WRITE_CONCURRENT_INSERT))
224 *lock_type= TL_WRITE_DEFAULT;
243 enum_duplicates duplic,
247 bool transactional_table, joins_freed=
false;
249 uint32_t value_count;
258 Item *unused_conds= 0;
266 values_list.size() > 1);
270 DRIZZLE_INSERT_DONE(1, 0);
277 value_count= values->size();
279 if (prepare_insert(session, table_list, table, fields, values,
280 update_fields, update_values, duplic, &unused_conds,
282 (fields.size() || !value_count ||
289 session->setAbortOnWarning(
false);
290 DRIZZLE_INSERT_DONE(1, 0);
295 table= table_list->
table;
297 context= &session->lex().select_lex.context;
308 ctx_state.save_state(context, table_list);
315 context->resolve_in_table_list_only(table_list);
317 while ((values= its++))
320 if (values->size() != value_count)
322 my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), counter);
328 session->setAbortOnWarning(
false);
329 DRIZZLE_INSERT_DONE(1, 0);
333 if (setup_fields(session, 0, *values, MARK_COLUMNS_READ, 0, 0))
339 session->setAbortOnWarning(
false);
340 DRIZZLE_INSERT_DONE(1, 0);
344 its= values_list.begin();
347 ctx_state.restore_state(context, table_list);
353 info.handle_duplicates=duplic;
354 info.update_fields= &update_fields;
355 info.update_values= &update_values;
362 session->count_cuted_fields= ignore ? CHECK_FIELD_WARN : CHECK_FIELD_ERROR_FOR_NULL;
369 if (duplic == DUP_REPLACE)
370 table->
cursor->extra(HA_EXTRA_WRITE_CAN_REPLACE);
371 if (duplic == DUP_UPDATE)
372 table->
cursor->extra(HA_EXTRA_INSERT_WITH_UPDATE);
374 if (duplic != DUP_ERROR || ignore)
375 table->
cursor->extra(HA_EXTRA_IGNORE_DUP_KEY);
376 table->
cursor->ha_start_bulk_insert(values_list.size());
380 session->setAbortOnWarning(not ignore);
382 table->mark_columns_needed_for_insert();
384 while ((values= its++))
386 if (fields.size() || !value_count)
388 table->restoreRecordAsDefault();
389 if (fill_record(session, fields, *values))
391 if (values_list.size() != 1 && ! session->
is_error())
407 table->restoreRecordAsDefault();
409 if (fill_record(session, table->getFields(), *values))
411 if (values_list.size() != 1 && ! session->
is_error())
424 error=write_record(session, table ,&info);
443 if (table->
cursor->ha_end_bulk_insert() && !error)
445 table->print_error(errno,MYF(0));
448 if (duplic != DUP_ERROR || ignore)
449 table->
cursor->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
451 transactional_table= table->
cursor->has_transactions();
459 (void)(transactional_table);
476 (session->arg_of_last_insert_id_function ?
481 session->count_cuted_fields= CHECK_FIELD_IGNORE;
482 table->auto_increment_field_not_null=
false;
483 if (duplic == DUP_REPLACE)
484 table->
cursor->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
492 session->setAbortOnWarning(
false);
493 DRIZZLE_INSERT_DONE(1, 0);
497 if (values_list.size() == 1 && (!(session->
options & OPTION_WARNINGS) ||
501 session->
my_ok((ulong) session->rowCount(),
508 snprintf(buff,
sizeof(buff), ER(ER_INSERT_INFO), (ulong) info.
records,
511 snprintf(buff,
sizeof(buff), ER(ER_INSERT_INFO), (ulong) info.
records,
514 session->
my_ok((ulong) session->rowCount(),
517 session->status_var.inserted_row_count+= session->rowCount();
518 session->setAbortOnWarning(
false);
519 DRIZZLE_INSERT_DONE(0, session->rowCount());
541 static bool prepare_insert_check_table(Session *session, TableList *table_list,
554 return setup_tables_and_check_access(session, &session->lex().select_lex.context,
555 &session->lex().select_lex.top_join_list, table_list, &session->lex().select_lex.leaf_tables, select_insert);
590 bool prepare_insert(Session *session, TableList *table_list,
591 Table *table, List<Item> &fields, List_item *values,
592 List<Item> &update_fields, List<Item> &update_values,
593 enum_duplicates duplic,
596 bool check_fields,
bool abort_on_warning)
598 Select_Lex *select_lex= &session->lex().select_lex;
599 Name_resolution_context *context= &select_lex->context;
600 Name_resolution_context_state ctx_state;
601 bool insert_into_view= (0 != 0);
606 assert (!select_insert || !values);
613 if (not select_insert)
615 for (Select_Lex_Unit *un= select_lex->first_inner_unit();
619 for (Select_Lex *sl= un->first_select();
621 sl= sl->next_select())
623 sl->context.outer_context= 0;
628 if (duplic == DUP_UPDATE)
631 table_list->set_insert_values();
634 if (prepare_insert_check_table(session, table_list, fields, select_insert))
642 assert (!select_lex->group_list.elements);
645 ctx_state.save_state(context, table_list);
651 table_list->next_local= 0;
652 context->resolve_in_table_list_only(table_list);
654 res= check_insert_fields(session, context->table_list, fields, *values,
655 !insert_into_view, &map) ||
656 setup_fields(session, 0, *values, MARK_COLUMNS_READ, 0, 0);
658 if (!res && check_fields)
660 bool saved_abort_on_warning= session->abortOnWarning();
662 session->setAbortOnWarning(abort_on_warning);
663 res= check_that_all_fields_are_given_values(session,
665 context->table_list->table,
666 context->table_list);
667 session->setAbortOnWarning(saved_abort_on_warning);
670 if (!res && duplic == DUP_UPDATE)
672 res= check_update_fields(session, context->table_list, update_fields, &map);
676 ctx_state.restore_state(context, table_list);
679 res= setup_fields(session, 0, update_values, MARK_COLUMNS_READ, 0, 0);
686 table= table_list->table;
688 if (not select_insert && unique_table(table_list, table_list->next_global,
true))
690 my_error(ER_UPDATE_TABLE_USED, MYF(0), table_list->alias);
694 if (duplic == DUP_UPDATE || duplic == DUP_REPLACE)
695 table->prepare_for_position();
703 static int last_uniq_key(Table *table,uint32_t keynr)
705 while (++keynr < table->getShare()->sizeKeys())
706 if (table->key_info[keynr].flags & HA_NOSAME)
739 int write_record(Session *session, Table *table,CopyInfo *info)
742 std::vector<unsigned char> key;
743 boost::dynamic_bitset<> *save_read_set, *save_write_set;
745 uint64_t insert_id_for_cur_row= 0;
749 save_read_set= table->read_set;
750 save_write_set= table->write_set;
752 if (info->handle_duplicates == DUP_REPLACE || info->handle_duplicates == DUP_UPDATE)
754 while ((error=table->cursor->insertRecord(table->getInsertRecord())))
764 if (table->cursor->insert_id_for_cur_row > 0)
765 insert_id_for_cur_row= table->cursor->insert_id_for_cur_row;
767 table->cursor->insert_id_for_cur_row= insert_id_for_cur_row;
768 bool is_duplicate_key_error;
769 if (table->cursor->is_fatal_error(error, HA_CHECK_DUP))
771 is_duplicate_key_error= table->cursor->is_fatal_error(error, 0);
772 if (!is_duplicate_key_error)
780 goto gok_or_after_err;
783 if ((
int) (key_nr = table->get_dup_key(error)) < 0)
785 error= HA_ERR_FOUND_DUPP_KEY;
789 table->use_all_columns();
795 if (info->handle_duplicates == DUP_REPLACE &&
796 table->next_number_field &&
797 key_nr == table->getShare()->next_number_index &&
798 (insert_id_for_cur_row > 0))
800 if (table->cursor->getEngine()->check_flag(HTON_BIT_DUPLICATE_POS))
802 if (table->cursor->rnd_pos(table->getUpdateRecord(),table->cursor->dup_ref))
807 if (table->cursor->extra(HA_EXTRA_FLUSH_CACHE))
815 key.resize(table->getShare()->max_unique_length);
817 key_copy(&key[0], table->getInsertRecord(), table->key_info+key_nr, 0);
818 if ((error=(table->cursor->index_read_idx_map(table->getUpdateRecord(),key_nr,
819 &key[0], HA_WHOLE_KEY,
820 HA_READ_KEY_EXACT))))
823 if (info->handle_duplicates == DUP_UPDATE)
830 assert(table->insert_values.size());
831 table->storeRecordAsInsert();
832 table->restoreRecord();
833 assert(info->update_fields->size() ==
834 info->update_values->size());
835 if (fill_record(session, *info->update_fields,
836 *info->update_values,
840 table->cursor->restore_auto_increment(prev_insert_id);
841 if (table->next_number_field)
842 table->cursor->adjust_next_insert_id_after_explicit_value(
843 table->next_number_field->val_int());
846 if (! table->records_are_comparable() || table->compare_records())
848 if ((error=table->cursor->updateRecord(table->getUpdateRecord(),
849 table->getInsertRecord())) &&
850 error != HA_ERR_RECORD_IS_THE_SAME)
853 !table->cursor->is_fatal_error(error, HA_CHECK_DUP_KEY))
855 goto gok_or_after_err;
860 if (error != HA_ERR_RECORD_IS_THE_SAME)
871 insert_id_for_cur_row= table->cursor->insert_id_for_cur_row= 0;
875 if (table->next_number_field)
876 table->cursor->adjust_next_insert_id_after_explicit_value(
877 table->next_number_field->val_int());
880 goto gok_or_after_err;
898 if (last_uniq_key(table,key_nr) &&
899 !table->cursor->referenced_by_foreign_key() &&
900 (table->timestamp_field_type == TIMESTAMP_NO_AUTO_SET ||
901 table->timestamp_field_type == TIMESTAMP_AUTO_SET_ON_BOTH))
903 if ((error=table->cursor->updateRecord(table->getUpdateRecord(),
904 table->getInsertRecord())) &&
905 error != HA_ERR_RECORD_IS_THE_SAME)
907 if (error != HA_ERR_RECORD_IS_THE_SAME)
911 session->record_first_successful_insert_id_in_cur_stmt(table->cursor->insert_id_for_cur_row);
916 goto after_n_copied_inc;
920 if ((error=table->cursor->deleteRecord(table->getUpdateRecord())))
923 if (!table->cursor->has_transactions())
924 session->transaction.stmt.markModifiedNonTransData();
929 session->record_first_successful_insert_id_in_cur_stmt(table->cursor->insert_id_for_cur_row);
934 if (table->read_set != save_read_set ||
935 table->write_set != save_write_set)
936 table->column_bitmaps_set(*save_read_set, *save_write_set);
938 else if ((error=table->cursor->insertRecord(table->getInsertRecord())))
941 table->cursor->is_fatal_error(error, HA_CHECK_DUP))
943 table->cursor->restore_auto_increment(prev_insert_id);
944 goto gok_or_after_err;
949 session->record_first_successful_insert_id_in_cur_stmt(table->cursor->insert_id_for_cur_row);
952 if (!table->cursor->has_transactions())
953 session->transaction.stmt.markModifiedNonTransData();
957 info->last_errno= error;
959 if (session->lex().current_select)
960 session->lex().current_select->no_error= 0;
961 table->print_error(error,MYF(0));
964 table->cursor->restore_auto_increment(prev_insert_id);
965 table->column_bitmaps_set(*save_read_set, *save_write_set);
974 int check_that_all_fields_are_given_values(Session *session, Table *entry,
979 for (Field **field=entry->getFields() ; *field ; field++)
981 if (not (*field)->isWriteSet())
988 if (((*field)->flags & NO_DEFAULT_VALUE_FLAG) &&
989 ((*field)->real_type() != DRIZZLE_TYPE_ENUM))
991 my_error(ER_NO_DEFAULT_FOR_FIELD, MYF(0), (*field)->field_name);
1005 if (((*field)->flags & NOT_NULL_FLAG) &&
1006 (*field)->is_null())
1008 my_error(ER_BAD_NULL_ERROR, MYF(0), (*field)->field_name);
1013 return session->abortOnWarning() ? err : 0;
1033 bool insert_select_prepare(Session *session)
1035 LEX *lex= &session->lex();
1036 Select_Lex *select_lex= &lex->select_lex;
1043 if (prepare_insert(session, lex->query_tables,
1044 lex->query_tables->table, lex->field_list, 0,
1045 lex->update_list, lex->value_list,
1047 &select_lex->where,
true,
false,
false))
1054 assert(select_lex->leaf_tables != 0);
1055 lex->leaf_tables_insert= select_lex->leaf_tables;
1057 select_lex->leaf_tables= select_lex->leaf_tables->next_leaf;
1062 select_insert::select_insert(TableList *table_list_par, Table *table_par,
1063 List<Item> *fields_par,
1064 List<Item> *update_fields,
1065 List<Item> *update_values,
1066 enum_duplicates duplic,
1067 bool ignore_check_option_errors) :
1068 table_list(table_list_par), table(table_par), fields(fields_par),
1069 autoinc_value_of_last_inserted_row(0),
1070 insert_into_view(table_list_par && 0 != 0)
1072 info.handle_duplicates= duplic;
1073 info.ignore= ignore_check_option_errors;
1074 info.update_fields= update_fields;
1075 info.update_values= update_values;
1080 select_insert::prepare(List<Item> &values, Select_Lex_Unit *u)
1084 Select_Lex *lex_current_select_save= session->lex().current_select;
1094 session->lex().current_select= &session->lex().select_lex;
1095 res= check_insert_fields(session, table_list, *fields, values,
1096 !insert_into_view, &map) ||
1097 setup_fields(session, 0, values, MARK_COLUMNS_READ, 0, 0);
1099 if (!res && fields->size())
1101 bool saved_abort_on_warning= session->abortOnWarning();
1102 session->setAbortOnWarning(not info.ignore);
1103 res= check_that_all_fields_are_given_values(session, table_list->table,
1105 session->setAbortOnWarning(saved_abort_on_warning);
1108 if (info.handle_duplicates == DUP_UPDATE && !res)
1110 Name_resolution_context *context= &session->lex().select_lex.context;
1111 Name_resolution_context_state ctx_state;
1114 ctx_state.save_state(context, table_list);
1117 table_list->next_local= 0;
1118 context->resolve_in_table_list_only(table_list);
1120 res= res || check_update_fields(session, context->table_list,
1121 *info.update_fields, &map);
1127 assert (!table_list->next_name_resolution_table);
1128 if (session->lex().select_lex.group_list.elements == 0 and
1129 not session->lex().select_lex.with_sum_func)
1135 table_list->next_name_resolution_table=
1136 ctx_state.get_first_name_resolution_table();
1138 res= res || setup_fields(session, 0, *info.update_values,
1139 MARK_COLUMNS_READ, 0, 0);
1148 List<Item>::iterator li(info.update_values->begin());
1151 while ((item= li++))
1153 item->transform(&Item::update_value_transformer,
1154 (
unsigned char*)session->lex().current_select);
1159 ctx_state.restore_state(context, table_list);
1162 session->lex().current_select= lex_current_select_save;
1169 table= table_list->table;
1175 if (unique_table(table_list, table_list->next_global))
1178 session->lex().current_select->options|= OPTION_BUFFER_RESULT;
1179 session->lex().current_select->join->select_options|= OPTION_BUFFER_RESULT;
1181 else if (not (session->lex().current_select->options & OPTION_BUFFER_RESULT))
1192 table->cursor->ha_start_bulk_insert((ha_rows) 0);
1194 table->restoreRecordAsDefault();
1195 table->next_number_field=table->found_next_number_field;
1197 session->cuted_fields=0;
1199 if (info.ignore || info.handle_duplicates != DUP_ERROR)
1200 table->cursor->extra(HA_EXTRA_IGNORE_DUP_KEY);
1202 if (info.handle_duplicates == DUP_REPLACE)
1203 table->cursor->extra(HA_EXTRA_WRITE_CAN_REPLACE);
1205 if (info.handle_duplicates == DUP_UPDATE)
1206 table->cursor->extra(HA_EXTRA_INSERT_WITH_UPDATE);
1208 session->setAbortOnWarning(not info.ignore);
1209 table->mark_columns_needed_for_insert();
1232 int select_insert::prepare2(
void)
1234 if (session->lex().current_select->options & OPTION_BUFFER_RESULT)
1235 table->cursor->ha_start_bulk_insert((ha_rows) 0);
1241 void select_insert::cleanup()
1247 select_insert::~select_insert()
1252 table->next_number_field=0;
1253 table->auto_increment_field_not_null=
false;
1254 table->cursor->ha_reset();
1256 session->count_cuted_fields= CHECK_FIELD_IGNORE;
1257 session->setAbortOnWarning(
false);
1262 bool select_insert::send_data(List<Item> &values)
1267 if (unit->offset_limit_cnt)
1269 unit->offset_limit_cnt--;
1273 session->count_cuted_fields= CHECK_FIELD_WARN;
1274 store_values(values);
1275 session->count_cuted_fields= CHECK_FIELD_IGNORE;
1276 if (session->is_error())
1280 plugin::TransactionalStorageEngine::releaseTemporaryLatches(session);
1282 error= write_record(session, table, &info);
1283 table->auto_increment_field_not_null=
false;
1287 if (info.handle_duplicates == DUP_UPDATE)
1297 table->restoreRecordAsDefault();
1299 if (table->next_number_field)
1305 if (session->first_successful_insert_id_in_cur_stmt == 0)
1306 autoinc_value_of_last_inserted_row=
1307 table->next_number_field->val_int();
1312 table->next_number_field->reset();
1319 void select_insert::store_values(List<Item> &values)
1322 fill_record(session, *fields, values,
true);
1324 fill_record(session, table->getFields(), values,
true);
1327 void select_insert::send_error(drizzled::error_t errcode,
const char *err)
1329 my_message(errcode, err, MYF(0));
1333 bool select_insert::send_eof()
1336 bool const trans_table= table->cursor->has_transactions();
1340 error= table->cursor->ha_end_bulk_insert();
1341 table->cursor->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
1342 table->cursor->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
1344 if ((changed= (info.copied || info.deleted || info.updated)))
1346 if (session->transaction.stmt.hasModifiedNonTransData())
1347 session->transaction.all.markModifiedNonTransData();
1349 assert(trans_table || !changed ||
1350 session->transaction.stmt.hasModifiedNonTransData());
1352 table->cursor->ha_release_auto_increment();
1356 table->print_error(error,MYF(0));
1357 DRIZZLE_INSERT_SELECT_DONE(error, 0);
1362 snprintf(buff,
sizeof(buff), ER(ER_INSERT_INFO), (ulong) info.records,
1363 (ulong) (info.records - info.copied), (ulong) session->cuted_fields);
1365 snprintf(buff,
sizeof(buff), ER(ER_INSERT_INFO), (ulong) info.records,
1366 (ulong) (info.deleted+info.updated), (ulong) session->cuted_fields);
1367 session->row_count_func= info.copied + info.deleted + info.updated;
1369 id= (session->first_successful_insert_id_in_cur_stmt > 0) ?
1370 session->first_successful_insert_id_in_cur_stmt :
1371 (session->arg_of_last_insert_id_function ?
1372 session->first_successful_insert_id_in_prev_stmt :
1373 (info.copied ? autoinc_value_of_last_inserted_row : 0));
1374 session->my_ok((ulong) session->rowCount(),
1375 info.copied + info.deleted + info.touched, id, buff);
1376 session->status_var.inserted_row_count+= session->rowCount();
1377 DRIZZLE_INSERT_SELECT_DONE(0, session->rowCount());
1381 void select_insert::abort() {
1392 bool changed, transactional_table;
1394 table->cursor->ha_end_bulk_insert();
1396 changed= (info.copied || info.deleted || info.updated);
1397 transactional_table= table->cursor->has_transactions();
1398 (void)(transactional_table);
1400 assert(transactional_table || !changed ||
1401 session->transaction.stmt.hasModifiedNonTransData());
1402 table->cursor->ha_release_auto_increment();
1405 if (DRIZZLE_INSERT_SELECT_DONE_ENABLED())
1407 DRIZZLE_INSERT_SELECT_DONE(0, info.copied + info.deleted + info.updated);
1460 static Table *create_table_from_items(Session *session, HA_CREATE_INFO *create_info,
1461 TableList *create_table,
1462 message::Table &table_proto,
1463 AlterInfo *alter_info,
1465 bool is_if_not_exists,
1467 const identifier::Table& identifier)
1469 TableShare share(message::Table::INTERNAL);
1470 uint32_t select_field_count= items->size();
1472 List<Item>::iterator it(items->begin());
1476 if (not (identifier.isTmp()) && create_table->table->db_stat)
1479 if (is_if_not_exists)
1481 create_info->table_existed= 1;
1482 push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1483 ER_TABLE_EXISTS_ERROR, ER(ER_TABLE_EXISTS_ERROR),
1484 create_table->getTableName());
1485 return create_table->table;
1488 my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_table->getTableName());
1493 table::Shell tmp_table(share);
1495 if (not table_proto.engine().name().compare(
"MyISAM"))
1496 tmp_table.getMutableShare()->db_low_byte_first=
true;
1497 else if (not table_proto.engine().name().compare(
"MEMORY"))
1498 tmp_table.getMutableShare()->db_low_byte_first=
true;
1500 tmp_table.in_use= session;
1504 CreateField *cr_field;
1505 Field *field, *def_field;
1506 if (item->type() == Item::FUNC_ITEM)
1508 if (item->result_type() != STRING_RESULT)
1510 field= item->tmp_table_field(&tmp_table);
1514 field= item->tmp_table_field_from_field_type(&tmp_table, 0);
1520 (Item ***) 0, &tmp_field, &def_field,
false,
1525 !(cr_field=
new CreateField(field,(item->type() == Item::FIELD_ITEM ?
1526 ((Item_field *)item)->field :
1532 if (item->maybe_null)
1534 cr_field->flags &= ~NOT_NULL_FLAG;
1537 alter_info->create_list.push_back(cr_field);
1550 if (not create_table_no_lock(session,
1559 if (create_info->table_existed && not identifier.isTmp())
1566 my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_table->getTableName());
1570 if (not identifier.isTmp())
1573 boost::mutex::scoped_lock scopedLock(table::Cache::mutex());
1575 if (create_table->table)
1577 table::Concurrent *concurrent_table=
static_cast<table::Concurrent *
>(create_table->table);
1579 if (concurrent_table->reopen_name_locked_table(create_table, session))
1581 (void)plugin::StorageEngine::dropTable(*session, identifier);
1585 table= create_table->table;
1590 (void)plugin::StorageEngine::dropTable(*session, identifier);
1595 if (not (table= session->openTable(create_table, (
bool*) 0,
1596 DRIZZLE_OPEN_TEMPORARY_ONLY)) &&
1597 not create_info->table_existed)
1604 session->open_tables.drop_temporary_table(identifier);
1612 table->reginfo.lock_type=TL_WRITE;
1613 if (not ((*lock)= session->lockTables(&table, 1, DRIZZLE_LOCK_IGNORE_FLUSH)))
1617 session->unlockTables(*lock);
1621 if (not create_info->table_existed)
1622 session->drop_open_table(table, identifier);
1632 select_create::prepare(List<Item> &values, Select_Lex_Unit *u)
1634 DrizzleLock *extra_lock= NULL;
1647 if (not (table= create_table_from_items(session, create_info, create_table,
1649 alter_info, &values,
1651 &extra_lock, identifier)))
1658 assert(m_plock == NULL);
1660 if (identifier.isTmp())
1663 m_plock= &session->open_tables.extra_lock;
1665 *m_plock= extra_lock;
1668 if (table->getShare()->sizeFields() < values.size())
1670 my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), 1);
1675 field= table->getFields() + table->getShare()->sizeFields() - values.size();
1678 for (Field **f= field ; *f ; f++)
1680 table->setWriteSet((*f)->position());
1684 table->timestamp_field_type= TIMESTAMP_NO_AUTO_SET;
1685 table->next_number_field=table->found_next_number_field;
1687 table->restoreRecordAsDefault();
1688 session->cuted_fields=0;
1689 if (info.ignore || info.handle_duplicates != DUP_ERROR)
1690 table->cursor->extra(HA_EXTRA_IGNORE_DUP_KEY);
1692 if (info.handle_duplicates == DUP_REPLACE)
1693 table->cursor->extra(HA_EXTRA_WRITE_CAN_REPLACE);
1695 if (info.handle_duplicates == DUP_UPDATE)
1696 table->cursor->extra(HA_EXTRA_INSERT_WITH_UPDATE);
1698 table->cursor->ha_start_bulk_insert((ha_rows) 0);
1699 session->setAbortOnWarning(not info.ignore);
1700 if (check_that_all_fields_are_given_values(session, table, table_list))
1703 table->mark_columns_needed_for_insert();
1704 table->cursor->extra(HA_EXTRA_WRITE_CACHE);
1708 void select_create::store_values(List<Item> &values)
1710 fill_record(session, field, values,
true);
1714 void select_create::send_error(drizzled::error_t errcode,
const char *err)
1727 select_insert::send_error(errcode, err);
1731 bool select_create::send_eof()
1733 bool tmp=select_insert::send_eof();
1743 if (!table->getShare()->getType())
1745 TransactionServices::autocommitOrRollback(*session, 0);
1746 (void) session->endActiveTransaction();
1749 table->cursor->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
1750 table->cursor->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
1753 session->unlockTables(*m_plock);
1762 void select_create::abort()
1779 select_insert::abort();
1783 session->unlockTables(*m_plock);
1790 table->cursor->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
1791 table->cursor->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
1792 if (not create_info->table_existed)
1793 session->drop_open_table(table, identifier);
void my_ok(ha_rows affected_rows=0, ha_rows found_rows_arg=0, uint64_t passed_id=0, const char *message=NULL)
void ha_release_auto_increment()
void set_proc_info(const char *info)
bool hasModifiedNonTransData() const
static void releaseTemporaryLatches(Session *session)
Table * table
opened table
static void upgrade_lock_type(Session *, thr_lock_type *lock_type, enum_duplicates duplic, bool)
Field * found_next_number_field
uint64_t prev_insert_id(uint64_t nr, drizzle_system_variables *variables)
uint64_t first_successful_insert_id_in_prev_stmt
uint64_t first_successful_insert_id_in_cur_stmt
Field * create_tmp_field(Session *session, Table *table, Item *item, Item::Type type, Item ***copy_func, Field **from_field, Field **default_field, bool group, bool modify_item, bool make_copy_field, uint32_t convert_blob_length)
TableList * next_name_resolution_table
bool insert_query(Session *session, TableList *table, List< Item > &fields, List< List_item > &values, List< Item > &update_fields, List< Item > &update_values, enum_duplicates flag, bool ignore)
void markModifiedNonTransData()
Field * next_number_field
void free_underlaid_joins(Session *session, Select_Lex *select)
TableList * first_name_resolution_table
bool openTablesLock(TableList *)