12
12
You should have received a copy of the GNU General Public License
13
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
17
/* Insert of records */
22
Drizzle has a different form of DELAYED then MySQL. DELAYED is just
23
a hint to the the sorage engine (which can then do whatever it likes.
25
#include <drizzled/server_includes.h>
21
26
#include <drizzled/sql_select.h>
22
#include <drizzled/show.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>
30
#include <drizzled/sql_table.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>
37
#include <drizzled/table/shell.h>
42
extern plugin::StorageEngine *heap_engine;
43
extern plugin::StorageEngine *myisam_engine;
27
#include <drizzled/sql_show.h>
29
#include <drizzled/drizzled_error_messages.h>
31
/* Define to force use of my_malloc() if the allocated memory block is big */
34
#define my_safe_alloca(size, min_length) my_alloca(size)
35
#define my_safe_afree(ptr, size, min_length) my_afree(ptr)
37
#define my_safe_alloca(size, min_length) ((size <= min_length) ? my_alloca(size) : malloc(size))
38
#define my_safe_afree(ptr, size, min_length) if (size > min_length) free(ptr)
46
44
Check if insert fields are correct.
49
47
check_insert_fields()
50
session The current thread.
48
thd The current thread.
51
49
table The table for insert.
52
50
fields The insert fields.
53
51
values The insert values.
241
236
bool transactional_table, joins_freed= false;
238
bool was_insert_delayed= (table_list->lock_type == TL_WRITE_DELAYED);
243
239
uint32_t value_count;
244
240
ulong counter = 1;
248
List<List_item>::iterator its(values_list.begin());
244
List_iterator_fast<List_item> its(values_list);
249
245
List_item *values;
250
246
Name_resolution_context *context;
251
247
Name_resolution_context_state ctx_state;
252
248
thr_lock_type lock_type;
253
249
Item *unused_conds= 0;
257
253
Upgrade lock type if the requested lock is incompatible with
258
254
the current connection mode or table operation.
260
upgrade_lock_type(session, &table_list->lock_type, duplic,
256
upgrade_lock_type(thd, &table_list->lock_type, duplic,
261
257
values_list.elements > 1);
263
if (session->openTablesLock(table_list))
260
We can't write-delayed into a table locked with LOCK TABLES:
261
this will lead to a deadlock, since the delayed thread will
262
never be able to get a lock on the table. QQQ: why not
263
upgrade the lock here instead?
265
if (table_list->lock_type == TL_WRITE_DELAYED && thd->locked_tables &&
266
find_locked_table(thd, table_list->db, table_list->table_name))
265
DRIZZLE_INSERT_DONE(1, 0);
268
my_error(ER_DELAYED_INSERT_TABLE_LOCKED, MYF(0),
269
table_list->table_name);
274
if (open_and_lock_tables(thd, table_list))
269
277
lock_type= table_list->lock_type;
271
session->set_proc_info("init");
272
session->used_tables=0;
279
thd_proc_info(thd, "init");
274
282
value_count= values->elements;
276
if (prepare_insert(session, table_list, table, fields, values,
284
if (mysql_prepare_insert(thd, table_list, table, fields, values,
277
285
update_fields, update_values, duplic, &unused_conds,
279
287
(fields.elements || !value_count ||
280
288
(0) != 0), !ignore))
283
table->cursor->ha_release_auto_increment();
285
free_underlaid_joins(session, &session->getLex()->select_lex);
286
session->setAbortOnWarning(false);
287
DRIZZLE_INSERT_DONE(1, 0);
291
291
/* mysql_prepare_insert set table_list->table if it was not set */
292
292
table= table_list->table;
294
context= &session->getLex()->select_lex.context;
294
context= &thd->lex->select_lex.context;
296
296
These three asserts test the hypothesis that the resetting of the name
297
297
resolution context below is not necessary at all since the list of local
436
435
Do not do this release if this is a delayed insert, it would steal
437
436
auto_inc values from the delayed_insert thread as they share Table.
439
table->cursor->ha_release_auto_increment();
440
if (table->cursor->ha_end_bulk_insert() && !error)
438
table->file->ha_release_auto_increment();
439
if (table->file->ha_end_bulk_insert() && !error)
442
table->print_error(errno,MYF(0));
441
table->file->print_error(my_errno,MYF(0));
445
444
if (duplic != DUP_ERROR || ignore)
446
table->cursor->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
448
transactional_table= table->cursor->has_transactions();
450
changed= (info.copied || info.deleted || info.updated);
451
if ((changed && error <= 0) || session->transaction.stmt.hasModifiedNonTransData())
453
if (session->transaction.stmt.hasModifiedNonTransData())
454
session->transaction.all.markModifiedNonTransData();
456
assert(transactional_table || !changed || session->transaction.stmt.hasModifiedNonTransData());
445
table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
447
transactional_table= table->file->has_transactions();
449
if ((changed= (info.copied || info.deleted || info.updated)))
452
Invalidate the table in the query cache if something changed.
453
For the transactional algorithm to work the invalidation must be
454
before binlog writing and ha_autocommit_or_rollback
457
if ((changed && error <= 0) || thd->transaction.stmt.modified_non_trans_table || was_insert_delayed)
459
if (mysql_bin_log.is_open())
464
[Guilhem wrote] Temporary errors may have filled
465
thd->net.last_error/errno. For example if there has
466
been a disk full error when writing the row, and it was
467
MyISAM, then thd->net.last_error/errno will be set to
468
"disk full"... and the my_pwrite() will wait until free
469
space appears, and so when it finishes then the
470
write_row() was entirely successful
472
/* todo: consider removing */
477
A query which per-row-loop can not be interrupted with
478
KILLED, like INSERT, and that does not invoke stored
479
routines can be binlogged with neglecting the KILLED error.
481
If there was no error (error == zero) until after the end of
482
inserting loop the KILLED flag that appeared later can be
483
disregarded since previously possible invocation of stored
484
routines did not result in any error due to the KILLED. In
485
such case the flag is ignored for constructing binlog event.
487
assert(thd->killed != THD::KILL_BAD_DATA || error > 0);
488
if (thd->binlog_query(THD::ROW_QUERY_TYPE,
489
thd->query, thd->query_length,
490
transactional_table, false,
491
(error>0) ? thd->killed : THD::NOT_KILLED) &&
497
if (thd->transaction.stmt.modified_non_trans_table)
498
thd->transaction.all.modified_non_trans_table= true;
500
assert(transactional_table || !changed ||
501
thd->transaction.stmt.modified_non_trans_table);
459
session->set_proc_info("end");
504
thd_proc_info(thd, "end");
461
506
We'll report to the client this id:
462
507
- if the table contains an autoincrement column and we successfully
467
512
inserted, the id of the last "inserted" row (if IGNORE, that value may not
468
513
have been really inserted but ignored).
470
id= (session->first_successful_insert_id_in_cur_stmt > 0) ?
471
session->first_successful_insert_id_in_cur_stmt :
472
(session->arg_of_last_insert_id_function ?
473
session->first_successful_insert_id_in_prev_stmt :
515
id= (thd->first_successful_insert_id_in_cur_stmt > 0) ?
516
thd->first_successful_insert_id_in_cur_stmt :
517
(thd->arg_of_last_insert_id_function ?
518
thd->first_successful_insert_id_in_prev_stmt :
474
519
((table->next_number_field && info.copied) ?
475
520
table->next_number_field->val_int() : 0));
476
521
table->next_number_field=0;
477
session->count_cuted_fields= CHECK_FIELD_IGNORE;
522
thd->count_cuted_fields= CHECK_FIELD_IGNORE;
478
523
table->auto_increment_field_not_null= false;
479
524
if (duplic == DUP_REPLACE)
480
table->cursor->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
525
table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
485
table->cursor->ha_release_auto_increment();
487
free_underlaid_joins(session, &session->getLex()->select_lex);
488
session->setAbortOnWarning(false);
489
DRIZZLE_INSERT_DONE(1, 0);
493
if (values_list.elements == 1 && (!(session->options & OPTION_WARNINGS) ||
494
!session->cuted_fields))
496
session->row_count_func= info.copied + info.deleted + info.updated;
497
session->my_ok((ulong) session->rowCount(),
498
info.copied + info.deleted + info.touched, id);
529
if (values_list.elements == 1 && (!(thd->options & OPTION_WARNINGS) ||
532
thd->row_count_func= info.copied + info.deleted +
533
((thd->client_capabilities & CLIENT_FOUND_ROWS) ?
534
info.touched : info.updated);
535
my_ok(thd, (ulong) thd->row_count_func, id);
540
ha_rows updated=((thd->client_capabilities & CLIENT_FOUND_ROWS) ?
541
info.touched : info.updated);
504
snprintf(buff, sizeof(buff), ER(ER_INSERT_INFO), (ulong) info.records,
505
(ulong) (info.records - info.copied), (ulong) session->cuted_fields);
543
sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
544
(ulong) (info.records - info.copied), (ulong) thd->cuted_fields);
507
snprintf(buff, sizeof(buff), ER(ER_INSERT_INFO), (ulong) info.records,
508
(ulong) (info.deleted + info.updated), (ulong) session->cuted_fields);
509
session->row_count_func= info.copied + info.deleted + info.updated;
510
session->my_ok((ulong) session->rowCount(),
511
info.copied + info.deleted + info.touched, id, buff);
546
sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
547
(ulong) (info.deleted + updated), (ulong) thd->cuted_fields);
548
thd->row_count_func= info.copied + info.deleted + updated;
549
::my_ok(thd, (ulong) thd->row_count_func, id, buff);
513
session->status_var.inserted_row_count+= session->rowCount();
514
session->setAbortOnWarning(false);
515
DRIZZLE_INSERT_DONE(0, session->rowCount());
551
thd->abort_on_warning= 0;
552
DRIZZLE_INSERT_END();
557
table->file->ha_release_auto_increment();
559
free_underlaid_joins(thd, &thd->lex->select_lex);
560
thd->abort_on_warning= 0;
561
DRIZZLE_INSERT_END();
654
699
table_list->next_local= 0;
655
700
context->resolve_in_table_list_only(table_list);
657
res= check_insert_fields(session, context->table_list, fields, *values,
702
res= check_insert_fields(thd, context->table_list, fields, *values,
658
703
!insert_into_view, &map) ||
659
setup_fields(session, 0, *values, MARK_COLUMNS_READ, 0, 0);
704
setup_fields(thd, 0, *values, MARK_COLUMNS_READ, 0, 0);
661
706
if (!res && check_fields)
663
bool saved_abort_on_warning= session->abortOnWarning();
665
session->setAbortOnWarning(abort_on_warning);
666
res= check_that_all_fields_are_given_values(session,
708
bool saved_abort_on_warning= thd->abort_on_warning;
709
thd->abort_on_warning= abort_on_warning;
710
res= check_that_all_fields_are_given_values(thd,
668
712
context->table_list->table,
669
713
context->table_list);
670
session->setAbortOnWarning(saved_abort_on_warning);
714
thd->abort_on_warning= saved_abort_on_warning;
673
717
if (!res && duplic == DUP_UPDATE)
675
res= check_update_fields(session, context->table_list, update_fields, &map);
719
res= check_update_fields(thd, context->table_list, update_fields, &map);
678
722
/* Restore the current context. */
679
723
ctx_state.restore_state(context, table_list);
682
res= setup_fields(session, 0, update_values, MARK_COLUMNS_READ, 0, 0);
726
res= setup_fields(thd, 0, update_values, MARK_COLUMNS_READ, 0, 0);
689
733
table= table_list->table;
691
if (not select_insert)
693
737
TableList *duplicate;
694
if ((duplicate= unique_table(table_list, table_list->next_global, true)))
738
if ((duplicate= unique_table(thd, table_list, table_list->next_global, 1)))
696
my_error(ER_UPDATE_TABLE_USED, MYF(0), table_list->alias);
740
update_non_unique_table_error(table_list, "INSERT", duplicate);
702
744
if (duplic == DUP_UPDATE || duplic == DUP_REPLACE)
703
745
table->prepare_for_position();
928
if ((error=table->cursor->deleteRecord(table->getUpdateRecord())))
976
if ((error=table->file->ha_delete_row(table->record[1])))
931
if (!table->cursor->has_transactions())
932
session->transaction.stmt.markModifiedNonTransData();
979
if (!table->file->has_transactions())
980
thd->transaction.stmt.modified_non_trans_table= true;
933
981
/* Let us attempt do write_row() once more */
937
session->record_first_successful_insert_id_in_cur_stmt(table->cursor->insert_id_for_cur_row);
985
thd->record_first_successful_insert_id_in_cur_stmt(table->file->insert_id_for_cur_row);
939
987
Restore column maps if they where replaced during an duplicate key
942
990
if (table->read_set != save_read_set ||
943
991
table->write_set != save_write_set)
944
table->column_bitmaps_set(*save_read_set, *save_write_set);
992
table->column_bitmaps_set(save_read_set, save_write_set);
946
else if ((error=table->cursor->insertRecord(table->getInsertRecord())))
994
else if ((error=table->file->ha_write_row(table->record[0])))
948
996
if (!info->ignore ||
949
table->cursor->is_fatal_error(error, HA_CHECK_DUP))
997
table->file->is_fatal_error(error, HA_CHECK_DUP))
951
table->cursor->restore_auto_increment(prev_insert_id);
999
table->file->restore_auto_increment(prev_insert_id);
952
1000
goto gok_or_after_err;
955
1003
after_n_copied_inc:
957
session->record_first_successful_insert_id_in_cur_stmt(table->cursor->insert_id_for_cur_row);
1005
thd->record_first_successful_insert_id_in_cur_stmt(table->file->insert_id_for_cur_row);
959
1007
gok_or_after_err:
960
if (!table->cursor->has_transactions())
961
session->transaction.stmt.markModifiedNonTransData();
1009
my_safe_afree(key,table->s->max_unique_length,MAX_KEY_LENGTH);
1010
if (!table->file->has_transactions())
1011
thd->transaction.stmt.modified_non_trans_table= true;
965
1015
info->last_errno= error;
966
1016
/* current_select is NULL if this is a delayed insert */
967
if (session->getLex()->current_select)
968
session->getLex()->current_select->no_error= 0; // Give error
969
table->print_error(error,MYF(0));
1017
if (thd->lex->current_select)
1018
thd->lex->current_select->no_error= 0; // Give error
1019
table->file->print_error(error,MYF(0));
972
table->cursor->restore_auto_increment(prev_insert_id);
973
table->column_bitmaps_set(*save_read_set, *save_write_set);
1022
table->file->restore_auto_increment(prev_insert_id);
1024
my_safe_afree(key, table->s->max_unique_length, MAX_KEY_LENGTH);
1025
table->column_bitmaps_set(save_read_set, save_write_set);
1180
1236
Is table which we are changing used somewhere in other parts of
1183
if (unique_table(table_list, table_list->next_global))
1239
if (unique_table(thd, table_list, table_list->next_global, 0))
1185
1241
/* Using same table for INSERT and SELECT */
1186
session->getLex()->current_select->options|= OPTION_BUFFER_RESULT;
1187
session->getLex()->current_select->join->select_options|= OPTION_BUFFER_RESULT;
1242
lex->current_select->options|= OPTION_BUFFER_RESULT;
1243
lex->current_select->join->select_options|= OPTION_BUFFER_RESULT;
1189
else if (not (session->getLex()->current_select->options & OPTION_BUFFER_RESULT))
1245
else if (!(lex->current_select->options & OPTION_BUFFER_RESULT))
1192
We must not yet prepare the result table if it is the same as one of the
1193
source tables (INSERT SELECT). The preparation may disable
1248
We must not yet prepare the result table if it is the same as one of the
1249
source tables (INSERT SELECT). The preparation may disable
1194
1250
indexes on the result table, which may be used during the select, if it
1195
1251
is the same table (Bug #6034). Do the preparation after the select phase
1196
1252
in select_insert::prepare2().
1197
1253
We won't start bulk inserts at all if this statement uses functions or
1198
1254
should invoke triggers since they may access to the same table too.
1200
table->cursor->ha_start_bulk_insert((ha_rows) 0);
1256
table->file->ha_start_bulk_insert((ha_rows) 0);
1202
table->restoreRecordAsDefault(); // Get empty record
1258
restore_record(table,s->default_values); // Get empty record
1203
1259
table->next_number_field=table->found_next_number_field;
1205
session->cuted_fields=0;
1261
if (thd->slave_thread &&
1262
(info.handle_duplicates == DUP_UPDATE) &&
1263
(table->next_number_field != NULL) &&
1264
rpl_master_has_bug(&active_mi->rli, 24432))
1267
thd->cuted_fields=0;
1207
1268
if (info.ignore || info.handle_duplicates != DUP_ERROR)
1208
table->cursor->extra(HA_EXTRA_IGNORE_DUP_KEY);
1269
table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
1210
1270
if (info.handle_duplicates == DUP_REPLACE)
1211
table->cursor->extra(HA_EXTRA_WRITE_CAN_REPLACE);
1271
table->file->extra(HA_EXTRA_WRITE_CAN_REPLACE);
1213
1272
if (info.handle_duplicates == DUP_UPDATE)
1214
table->cursor->extra(HA_EXTRA_INSERT_WITH_UPDATE);
1216
session->setAbortOnWarning(not info.ignore);
1273
table->file->extra(HA_EXTRA_INSERT_WITH_UPDATE);
1274
thd->abort_on_warning= !info.ignore;
1217
1275
table->mark_columns_needed_for_insert();
1327
1381
void select_insert::store_values(List<Item> &values)
1329
1383
if (fields->elements)
1330
fill_record(session, *fields, values, true);
1384
fill_record(thd, *fields, values, 1);
1332
fill_record(session, table->getFields(), values, true);
1386
fill_record(thd, table->field, values, 1);
1335
void select_insert::send_error(drizzled::error_t errcode,const char *err)
1389
void select_insert::send_error(uint32_t errcode,const char *err)
1337
1393
my_message(errcode, err, MYF(0));
1341
1399
bool select_insert::send_eof()
1344
bool const trans_table= table->cursor->has_transactions();
1402
bool const trans_table= table->file->has_transactions();
1348
error= table->cursor->ha_end_bulk_insert();
1349
table->cursor->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
1350
table->cursor->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
1405
THD::killed_state killed_status= thd->killed;
1407
error= table->file->ha_end_bulk_insert();
1408
table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
1409
table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
1352
1411
if ((changed= (info.copied || info.deleted || info.updated)))
1355
1414
We must invalidate the table in the query cache before binlog writing
1356
and autocommitOrRollback.
1415
and ha_autocommit_or_rollback.
1358
if (session->transaction.stmt.hasModifiedNonTransData())
1359
session->transaction.all.markModifiedNonTransData();
1417
if (thd->transaction.stmt.modified_non_trans_table)
1418
thd->transaction.all.modified_non_trans_table= true;
1361
assert(trans_table || !changed ||
1362
session->transaction.stmt.hasModifiedNonTransData());
1420
assert(trans_table || !changed ||
1421
thd->transaction.stmt.modified_non_trans_table);
1364
table->cursor->ha_release_auto_increment();
1424
Write to binlog before commiting transaction. No statement will
1425
be written by the binlog_query() below in RBR mode. All the
1426
events are in the transaction cache and will be written when
1427
ha_autocommit_or_rollback() is issued below.
1429
if (mysql_bin_log.is_open())
1433
thd->binlog_query(THD::ROW_QUERY_TYPE,
1434
thd->query, thd->query_length,
1435
trans_table, false, killed_status);
1437
table->file->ha_release_auto_increment();
1368
table->print_error(error,MYF(0));
1369
DRIZZLE_INSERT_SELECT_DONE(error, 0);
1441
table->file->print_error(error,MYF(0));
1372
1444
char buff[160];
1373
1445
if (info.ignore)
1374
snprintf(buff, sizeof(buff), ER(ER_INSERT_INFO), (ulong) info.records,
1375
(ulong) (info.records - info.copied), (ulong) session->cuted_fields);
1446
sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
1447
(ulong) (info.records - info.copied), (ulong) thd->cuted_fields);
1377
snprintf(buff, sizeof(buff), ER(ER_INSERT_INFO), (ulong) info.records,
1378
(ulong) (info.deleted+info.updated), (ulong) session->cuted_fields);
1379
session->row_count_func= info.copied + info.deleted + info.updated;
1449
sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
1450
(ulong) (info.deleted+info.updated), (ulong) thd->cuted_fields);
1451
thd->row_count_func= info.copied + info.deleted +
1452
((thd->client_capabilities & CLIENT_FOUND_ROWS) ?
1453
info.touched : info.updated);
1381
id= (session->first_successful_insert_id_in_cur_stmt > 0) ?
1382
session->first_successful_insert_id_in_cur_stmt :
1383
(session->arg_of_last_insert_id_function ?
1384
session->first_successful_insert_id_in_prev_stmt :
1455
id= (thd->first_successful_insert_id_in_cur_stmt > 0) ?
1456
thd->first_successful_insert_id_in_cur_stmt :
1457
(thd->arg_of_last_insert_id_function ?
1458
thd->first_successful_insert_id_in_prev_stmt :
1385
1459
(info.copied ? autoinc_value_of_last_inserted_row : 0));
1386
session->my_ok((ulong) session->rowCount(),
1387
info.copied + info.deleted + info.touched, id, buff);
1388
session->status_var.inserted_row_count+= session->rowCount();
1389
DRIZZLE_INSERT_SELECT_DONE(0, session->rowCount());
1460
::my_ok(thd, (ulong) thd->row_count_func, id, buff);
1393
1464
void select_insert::abort() {
1397
1468
If the creation of the table failed (due to a syntax error, for
1398
1469
example), no table will have been opened and therefore 'table'
1484
static Table *create_table_from_items(Session *session, HA_CREATE_INFO *create_info,
1558
static Table *create_table_from_items(THD *thd, HA_CREATE_INFO *create_info,
1485
1559
TableList *create_table,
1486
message::Table &table_proto,
1487
AlterInfo *alter_info,
1560
Alter_info *alter_info,
1488
1561
List<Item> *items,
1489
bool is_if_not_exists,
1491
identifier::Table::const_reference identifier)
1562
DRIZZLE_LOCK **lock,
1563
TABLEOP_HOOKS *hooks)
1493
TableShare share(message::Table::INTERNAL);
1565
Table tmp_table; // Used during 'Create_field()'
1494
1568
uint32_t select_field_count= items->elements;
1495
1569
/* Add selected items to field list */
1496
List<Item>::iterator it(items->begin());
1570
List_iterator_fast<Item> it(*items);
1498
1572
Field *tmp_field;
1500
if (not (identifier.isTmp()) && create_table->table->db_stat)
1575
if (!(create_info->options & HA_LEX_CREATE_TMP_TABLE) &&
1576
create_table->table->db_stat)
1502
/* Table already exists and was open at openTablesLock() stage. */
1503
if (is_if_not_exists)
1578
/* Table already exists and was open at open_and_lock_tables() stage. */
1579
if (create_info->options & HA_LEX_CREATE_IF_NOT_EXISTS)
1505
1581
create_info->table_existed= 1; // Mark that table existed
1506
push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1582
push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1507
1583
ER_TABLE_EXISTS_ERROR, ER(ER_TABLE_EXISTS_ERROR),
1508
create_table->getTableName());
1509
return create_table->table;
1584
create_table->table_name);
1585
return(create_table->table);
1512
my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_table->getTableName());
1588
my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_table->table_name);
1593
tmp_table.timestamp_field= 0;
1594
tmp_table.s= &share;
1595
init_tmp_table_share(thd, &share, "", 0, "", "");
1597
tmp_table.s->db_create_options=0;
1598
tmp_table.s->blob_ptr_size= portable_sizeof_char_ptr;
1599
tmp_table.s->db_low_byte_first=
1600
test(create_info->db_type == myisam_hton ||
1601
create_info->db_type == heap_hton);
1602
tmp_table.null_row= false;
1603
tmp_table.maybe_null= false;
1517
table::Shell tmp_table(share); // Used during 'CreateField()'
1519
if (not table_proto.engine().name().compare("MyISAM"))
1520
tmp_table.getMutableShare()->db_low_byte_first= true;
1521
else if (not table_proto.engine().name().compare("MEMORY"))
1522
tmp_table.getMutableShare()->db_low_byte_first= true;
1524
tmp_table.in_use= session;
1528
CreateField *cr_field;
1529
Field *field, *def_field;
1530
if (item->type() == Item::FUNC_ITEM)
1532
if (item->result_type() != STRING_RESULT)
1534
field= item->tmp_table_field(&tmp_table);
1538
field= item->tmp_table_field_from_field_type(&tmp_table, 0);
1607
Create_field *cr_field;
1608
Field *field, *def_field;
1609
if (item->type() == Item::FUNC_ITEM)
1610
if (item->result_type() != STRING_RESULT)
1611
field= item->tmp_table_field(&tmp_table);
1543
field= create_tmp_field(session, &tmp_table, item, item->type(),
1544
(Item ***) 0, &tmp_field, &def_field, false,
1549
!(cr_field=new CreateField(field,(item->type() == Item::FIELD_ITEM ?
1550
((Item_field *)item)->field :
1556
if (item->maybe_null)
1558
cr_field->flags &= ~NOT_NULL_FLAG;
1561
alter_info->create_list.push_back(cr_field);
1613
field= item->tmp_table_field_from_field_type(&tmp_table, 0);
1615
field= create_tmp_field(thd, &tmp_table, item, item->type(),
1616
(Item ***) 0, &tmp_field, &def_field, 0, 0, 0, 0,
1619
!(cr_field=new Create_field(field,(item->type() == Item::FIELD_ITEM ?
1620
((Item_field *)item)->field :
1623
if (item->maybe_null)
1624
cr_field->flags &= ~NOT_NULL_FLAG;
1625
alter_info->create_list.push_back(cr_field);
1568
1631
Note that we either creating (or opening existing) temporary table or
1569
1632
creating base table on which name we have exclusive lock. So code below
1570
1633
should not cause deadlocks or races.
1635
We don't log the statement, it will be logged later.
1637
If this is a HEAP table, the automatic DELETE FROM which is written to the
1638
binlog when a HEAP table is opened for the first time since startup, must
1639
not be written: 1) it would be wrong (imagine we're in CREATE SELECT: we
1640
don't want to delete from it) 2) it would be written before the CREATE
1641
Table, which is a wrong order. So we keep binary logging disabled when we
1574
if (not create_table_no_lock(session,
1645
tmp_disable_binlog(thd);
1646
if (!mysql_create_table_no_lock(thd, create_table->db,
1647
create_table->table_name,
1648
create_info, alter_info, 0,
1649
select_field_count))
1583
if (create_info->table_existed && not identifier.isTmp())
1651
if (create_info->table_existed &&
1652
!(create_info->options & HA_LEX_CREATE_TMP_TABLE))
1586
1655
This means that someone created table underneath server
1587
1656
or it was created via different mysqld front-end to the
1588
1657
cluster. We don't have much options but throw an error.
1590
my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_table->getTableName());
1659
my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_table->table_name);
1594
if (not identifier.isTmp())
1663
if (!(create_info->options & HA_LEX_CREATE_TMP_TABLE))
1596
/* CREATE TABLE... has found that the table already exists for insert and is adapting to use it */
1597
boost::mutex::scoped_lock scopedLock(table::Cache::singleton().mutex());
1599
if (create_table->table)
1665
pthread_mutex_lock(&LOCK_open);
1666
if (reopen_name_locked_table(thd, create_table, false))
1601
table::Concurrent *concurrent_table= static_cast<table::Concurrent *>(create_table->table);
1603
if (concurrent_table->reopen_name_locked_table(create_table, session))
1605
(void)plugin::StorageEngine::dropTable(*session, identifier);
1609
table= create_table->table;
1668
quick_rm_table(create_info->db_type, create_table->db,
1669
table_case_name(create_info, create_table->table_name),
1614
(void)plugin::StorageEngine::dropTable(*session, identifier);
1673
table= create_table->table;
1674
pthread_mutex_unlock(&LOCK_open);
1619
if (not (table= session->openTable(create_table, (bool*) 0,
1620
DRIZZLE_OPEN_TEMPORARY_ONLY)) &&
1621
not create_info->table_existed)
1678
if (!(table= open_table(thd, create_table, (bool*) 0,
1679
DRIZZLE_OPEN_TEMPORARY_ONLY)) &&
1680
!create_info->table_existed)
1624
1683
This shouldn't happen as creation of temporary table should make
1625
1684
it preparable for open. But let us do close_temporary_table() here
1628
session->drop_temporary_table(identifier);
1687
drop_temporary_table(thd, create_table);
1632
if (not table) // open failed
1691
reenable_binlog(thd);
1692
if (!table) // open failed
1636
1696
table->reginfo.lock_type=TL_WRITE;
1637
if (not ((*lock)= session->lockTables(&table, 1, DRIZZLE_LOCK_IGNORE_FLUSH)))
1697
hooks->prelock(&table, 1); // Call prelock hooks
1698
if (! ((*lock)= mysql_lock_tables(thd, &table, 1,
1699
DRIZZLE_LOCK_IGNORE_FLUSH, ¬_used)) ||
1700
hooks->postlock(&table, 1))
1641
session->unlockTables(*lock);
1704
mysql_unlock_tables(thd, *lock);
1645
if (not create_info->table_existed)
1646
session->drop_open_table(table, identifier);
1708
if (!create_info->table_existed)
1709
drop_open_table(thd, table, create_table->db, create_table->table_name);
1656
select_create::prepare(List<Item> &values, Select_Lex_Unit *u)
1717
select_create::prepare(List<Item> &values, SELECT_LEX_UNIT *u)
1658
DrizzleLock *extra_lock= NULL;
1719
DRIZZLE_LOCK *extra_lock= NULL;
1722
TABLEOP_HOOKS *hook_ptr= NULL;
1660
For replication, the CREATE-SELECT statement is written
1661
in two pieces: the first transaction messsage contains
1662
the CREATE TABLE statement as a CreateTableStatement message
1663
necessary to create the table.
1665
The second transaction message contains all the InsertStatement
1666
and associated InsertRecords that should go into the table.
1724
For row-based replication, the CREATE-SELECT statement is written
1725
in two pieces: the first one contain the CREATE TABLE statement
1726
necessary to create the table and the second part contain the rows
1727
that should go into the table.
1729
For non-temporary tables, the start of the CREATE-SELECT
1730
implicitly commits the previous transaction, and all events
1731
forming the statement will be stored the transaction cache. At end
1732
of the statement, the entire statement is committed as a
1733
transaction, and all events are written to the binary log.
1735
On the master, the table is locked for the duration of the
1736
statement, but since the CREATE part is replicated as a simple
1737
statement, there is no way to lock the table for accesses on the
1738
slave. Hence, we have to hold on to the CREATE part of the
1739
statement until the statement has finished.
1741
class MY_HOOKS : public TABLEOP_HOOKS {
1743
MY_HOOKS(select_create *x, TableList *create_table,
1744
TableList *select_tables)
1745
: ptr(x), all_tables(*create_table)
1747
all_tables.next_global= select_tables;
1751
virtual int do_postlock(Table **tables, uint32_t count)
1753
THD *thd= const_cast<THD*>(ptr->get_thd());
1754
if (int error= decide_logging_format(thd, &all_tables))
1757
Table const *const table = *tables;
1758
if (thd->current_stmt_binlog_row_based &&
1759
!table->s->tmp_table &&
1760
!ptr->get_create_info()->table_existed)
1762
ptr->binlog_show_create_table(tables, count);
1768
TableList all_tables;
1771
MY_HOOKS hooks(this, create_table, select_tables);
1671
if (not (table= create_table_from_items(session, create_info, create_table,
1673
alter_info, &values,
1675
&extra_lock, identifier)))
1777
Start a statement transaction before the create if we are using
1778
row-based replication for the statement. If we are creating a
1779
temporary table, we need to start a statement transaction.
1781
if ((thd->lex->create_info.options & HA_LEX_CREATE_TMP_TABLE) == 0 &&
1782
thd->current_stmt_binlog_row_based)
1784
thd->binlog_start_trans_and_stmt();
1787
if (!(table= create_table_from_items(thd, create_info, create_table,
1788
alter_info, &values,
1789
&extra_lock, hook_ptr)))
1677
1790
return(-1); // abort() deletes table
1680
1792
if (extra_lock)
1682
1794
assert(m_plock == NULL);
1684
if (identifier.isTmp())
1796
if (create_info->options & HA_LEX_CREATE_TMP_TABLE)
1685
1797
m_plock= &m_lock;
1687
m_plock= &session->extra_lock;
1799
m_plock= &thd->extra_lock;
1689
1801
*m_plock= extra_lock;
1692
if (table->getShare()->sizeFields() < values.elements)
1804
if (table->s->fields < values.elements)
1694
1806
my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), 1);
1698
1810
/* First field to copy */
1699
field= table->getFields() + table->getShare()->sizeFields() - values.elements;
1811
field= table->field+table->s->fields - values.elements;
1701
1813
/* Mark all fields that are given values */
1702
1814
for (Field **f= field ; *f ; f++)
1704
table->setWriteSet((*f)->position());
1815
bitmap_set_bit(table->write_set, (*f)->field_index);
1707
1817
/* Don't set timestamp if used */
1708
1818
table->timestamp_field_type= TIMESTAMP_NO_AUTO_SET;
1709
1819
table->next_number_field=table->found_next_number_field;
1711
table->restoreRecordAsDefault(); // Get empty record
1712
session->cuted_fields=0;
1821
restore_record(table,s->default_values); // Get empty record
1822
thd->cuted_fields=0;
1713
1823
if (info.ignore || info.handle_duplicates != DUP_ERROR)
1714
table->cursor->extra(HA_EXTRA_IGNORE_DUP_KEY);
1824
table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
1716
1825
if (info.handle_duplicates == DUP_REPLACE)
1717
table->cursor->extra(HA_EXTRA_WRITE_CAN_REPLACE);
1826
table->file->extra(HA_EXTRA_WRITE_CAN_REPLACE);
1719
1827
if (info.handle_duplicates == DUP_UPDATE)
1720
table->cursor->extra(HA_EXTRA_INSERT_WITH_UPDATE);
1722
table->cursor->ha_start_bulk_insert((ha_rows) 0);
1723
session->setAbortOnWarning(not info.ignore);
1724
if (check_that_all_fields_are_given_values(session, table, table_list))
1828
table->file->extra(HA_EXTRA_INSERT_WITH_UPDATE);
1829
table->file->ha_start_bulk_insert((ha_rows) 0);
1830
thd->abort_on_warning= !info.ignore;
1831
if (check_that_all_fields_are_given_values(thd, table, table_list))
1727
1833
table->mark_columns_needed_for_insert();
1728
table->cursor->extra(HA_EXTRA_WRITE_CACHE);
1834
table->file->extra(HA_EXTRA_WRITE_CACHE);
1839
select_create::binlog_show_create_table(Table **tables, uint32_t count)
1842
Note 1: In RBR mode, we generate a CREATE TABLE statement for the
1843
created table by calling store_create_info() (behaves as SHOW
1844
CREATE TABLE). In the event of an error, nothing should be
1845
written to the binary log, even if the table is non-transactional;
1846
therefore we pretend that the generated CREATE TABLE statement is
1847
for a transactional table. The event will then be put in the
1848
transaction cache, and any subsequent events (e.g., table-map
1849
events and binrow events) will also be put there. We can then use
1850
ha_autocommit_or_rollback() to either throw away the entire
1851
kaboodle of events, or write them to the binary log.
1853
We write the CREATE TABLE statement here and not in prepare()
1854
since there potentially are sub-selects or accesses to information
1855
schema that will do a close_thread_tables(), destroying the
1856
statement transaction cache.
1858
assert(thd->current_stmt_binlog_row_based);
1859
assert(tables && *tables && count > 0);
1862
String query(buf, sizeof(buf), system_charset_info);
1864
TableList tmp_table_list;
1866
memset(&tmp_table_list, 0, sizeof(tmp_table_list));
1867
tmp_table_list.table = *tables;
1868
query.length(0); // Have to zero it since constructor doesn't
1870
result= store_create_info(thd, &tmp_table_list, &query, create_info);
1871
assert(result == 0); /* store_create_info() always return 0 */
1873
thd->binlog_query(THD::STMT_QUERY_TYPE,
1874
query.ptr(), query.length(),
1875
/* is_trans */ true,
1876
/* suppress_use */ false);
1732
1879
void select_create::store_values(List<Item> &values)
1734
fill_record(session, field, values, true);
1881
fill_record(thd, field, values, 1);
1738
void select_create::send_error(drizzled::error_t errcode,const char *err)
1885
void select_create::send_error(uint32_t errcode,const char *err)
1741
1890
This will execute any rollbacks that are necessary before writing
1742
1891
the transcation cache.