25
25
#include <drizzled/server_includes.h>
26
26
#include <drizzled/sql_select.h>
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) : my_malloc(size,MYF(0)))
38
#define my_safe_afree(ptr, size, min_length) if (size > min_length) my_free(ptr,MYF(0))
27
#include <drizzled/show.h>
28
#include <drizzled/error.h>
29
#include <drizzled/name_resolution_context_state.h>
30
#include <drizzled/probes.h>
31
#include <drizzled/sql_base.h>
32
#include <drizzled/sql_load.h>
33
#include <drizzled/field/timestamp.h>
34
#include <drizzled/lock.h>
64
static int check_insert_fields(THD *thd, TABLE_LIST *table_list,
58
static int check_insert_fields(Session *session, TableList *table_list,
65
59
List<Item> &fields, List<Item> &values,
67
table_map *map __attribute__((unused)))
69
TABLE *table= table_list->table;
63
Table *table= table_list->table;
71
65
if (fields.elements == 0 && values.elements != 0)
108
102
table_list->next_local= 0;
109
103
context->resolve_in_table_list_only(table_list);
110
res= setup_fields(thd, 0, fields, MARK_COLUMNS_WRITE, 0, 0);
104
res= setup_fields(session, 0, fields, MARK_COLUMNS_WRITE, 0, 0);
112
106
/* Restore the current context. */
113
107
ctx_state.restore_state(context, table_list);
160
static int check_update_fields(THD *thd, TABLE_LIST *insert_table_list,
157
static int check_update_fields(Session *session, TableList *insert_table_list,
161
158
List<Item> &update_fields,
162
table_map *map __attribute__((unused)))
164
TABLE *table= insert_table_list->table;
161
Table *table= insert_table_list->table;
165
162
bool timestamp_mark= false;
167
164
if (table->timestamp_field)
177
174
/* Check the fields we are going to modify */
178
if (setup_fields(thd, 0, update_fields, MARK_COLUMNS_WRITE, 0, 0))
175
if (setup_fields(session, 0, update_fields, MARK_COLUMNS_WRITE, 0, 0))
181
178
if (table->timestamp_field)
236
233
bool transactional_table, joins_freed= false;
238
bool was_insert_delayed= (table_list->lock_type == TL_WRITE_DELAYED);
235
uint32_t value_count;
240
236
ulong counter = 1;
244
240
List_iterator_fast<List_item> its(values_list);
245
241
List_item *values;
246
242
Name_resolution_context *context;
247
243
Name_resolution_context_state ctx_state;
248
244
thr_lock_type lock_type;
249
245
Item *unused_conds= 0;
253
249
Upgrade lock type if the requested lock is incompatible with
254
250
the current connection mode or table operation.
256
upgrade_lock_type(thd, &table_list->lock_type, duplic,
252
upgrade_lock_type(session, &table_list->lock_type, duplic,
257
253
values_list.elements > 1);
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))
268
my_error(ER_DELAYED_INSERT_TABLE_LOCKED, MYF(0),
269
table_list->table_name);
255
if (open_and_lock_tables(session, table_list))
274
if (open_and_lock_tables(thd, table_list))
277
258
lock_type= table_list->lock_type;
279
thd_proc_info(thd, "init");
260
session->set_proc_info("init");
261
session->used_tables=0;
282
263
value_count= values->elements;
284
if (mysql_prepare_insert(thd, table_list, table, fields, values,
265
if (mysql_prepare_insert(session, table_list, table, fields, values,
285
266
update_fields, update_values, duplic, &unused_conds,
287
268
(fields.elements || !value_count ||
319
300
my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), counter);
322
if (setup_fields(thd, 0, *values, MARK_COLUMNS_READ, 0, 0))
303
if (setup_fields(session, 0, *values, MARK_COLUMNS_READ, 0, 0))
327
308
/* Restore the current context. */
328
309
ctx_state.restore_state(context, table_list);
341
322
For single line insert, generate an error if try to set a NOT NULL field
344
thd->count_cuted_fields= ((values_list.elements == 1 &&
325
session->count_cuted_fields= ((values_list.elements == 1 &&
346
327
CHECK_FIELD_ERROR_FOR_NULL :
347
328
CHECK_FIELD_WARN);
348
thd->cuted_fields = 0L;
329
session->cuted_fields = 0L;
349
330
table->next_number_field=table->found_next_number_field;
351
#ifdef HAVE_REPLICATION
352
if (thd->slave_thread &&
353
(info.handle_duplicates == DUP_UPDATE) &&
354
(table->next_number_field != NULL) &&
355
rpl_master_has_bug(&active_mi->rli, 24432))
360
thd_proc_info(thd, "update");
333
session->set_proc_info("update");
361
334
if (duplic == DUP_REPLACE)
362
335
table->file->extra(HA_EXTRA_WRITE_CAN_REPLACE);
363
336
if (duplic == DUP_UPDATE)
378
351
if (fields.elements || !value_count)
380
353
restore_record(table,s->default_values); // Get empty record
381
if (fill_record(thd, fields, *values, 0))
354
if (fill_record(session, fields, *values, 0))
383
if (values_list.elements != 1 && ! thd->is_error())
356
if (values_list.elements != 1 && ! session->is_error())
389
TODO: set thd->abort_on_warning if values_list.elements == 1
362
TODO: set session->abort_on_warning if values_list.elements == 1
390
363
and check that all items return warning in case of problem with
449
422
transactional_table= table->file->has_transactions();
451
if ((changed= (info.copied || info.deleted || info.updated)))
454
Invalidate the table in the query cache if something changed.
455
For the transactional algorithm to work the invalidation must be
456
before binlog writing and ha_autocommit_or_rollback
459
if ((changed && error <= 0) || thd->transaction.stmt.modified_non_trans_table || was_insert_delayed)
461
if (mysql_bin_log.is_open())
466
[Guilhem wrote] Temporary errors may have filled
467
thd->net.last_error/errno. For example if there has
468
been a disk full error when writing the row, and it was
469
MyISAM, then thd->net.last_error/errno will be set to
470
"disk full"... and the my_pwrite() will wait until free
471
space appears, and so when it finishes then the
472
write_row() was entirely successful
474
/* todo: consider removing */
479
A query which per-row-loop can not be interrupted with
480
KILLED, like INSERT, and that does not invoke stored
481
routines can be binlogged with neglecting the KILLED error.
483
If there was no error (error == zero) until after the end of
484
inserting loop the KILLED flag that appeared later can be
485
disregarded since previously possible invocation of stored
486
routines did not result in any error due to the KILLED. In
487
such case the flag is ignored for constructing binlog event.
489
assert(thd->killed != THD::KILL_BAD_DATA || error > 0);
490
if (thd->binlog_query(THD::ROW_QUERY_TYPE,
491
thd->query, thd->query_length,
492
transactional_table, false,
493
(error>0) ? thd->killed : THD::NOT_KILLED) &&
499
if (thd->transaction.stmt.modified_non_trans_table)
500
thd->transaction.all.modified_non_trans_table= true;
502
assert(transactional_table || !changed ||
503
thd->transaction.stmt.modified_non_trans_table);
424
changed= (info.copied || info.deleted || info.updated);
425
if ((changed && error <= 0) || session->transaction.stmt.modified_non_trans_table)
427
if (session->transaction.stmt.modified_non_trans_table)
428
session->transaction.all.modified_non_trans_table= true;
430
assert(transactional_table || !changed || session->transaction.stmt.modified_non_trans_table);
506
thd_proc_info(thd, "end");
433
session->set_proc_info("end");
508
435
We'll report to the client this id:
509
436
- if the table contains an autoincrement column and we successfully
514
441
inserted, the id of the last "inserted" row (if IGNORE, that value may not
515
442
have been really inserted but ignored).
517
id= (thd->first_successful_insert_id_in_cur_stmt > 0) ?
518
thd->first_successful_insert_id_in_cur_stmt :
519
(thd->arg_of_last_insert_id_function ?
520
thd->first_successful_insert_id_in_prev_stmt :
444
id= (session->first_successful_insert_id_in_cur_stmt > 0) ?
445
session->first_successful_insert_id_in_cur_stmt :
446
(session->arg_of_last_insert_id_function ?
447
session->first_successful_insert_id_in_prev_stmt :
521
448
((table->next_number_field && info.copied) ?
522
449
table->next_number_field->val_int() : 0));
523
450
table->next_number_field=0;
524
thd->count_cuted_fields= CHECK_FIELD_IGNORE;
451
session->count_cuted_fields= CHECK_FIELD_IGNORE;
525
452
table->auto_increment_field_not_null= false;
526
453
if (duplic == DUP_REPLACE)
527
454
table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
531
if (values_list.elements == 1 && (!(thd->options & OPTION_WARNINGS) ||
458
if (values_list.elements == 1 && (!(session->options & OPTION_WARNINGS) ||
459
!session->cuted_fields))
534
thd->row_count_func= info.copied + info.deleted +
535
((thd->client_capabilities & CLIENT_FOUND_ROWS) ?
461
session->row_count_func= info.copied + info.deleted +
462
((session->client_capabilities & CLIENT_FOUND_ROWS) ?
536
463
info.touched : info.updated);
537
my_ok(thd, (ulong) thd->row_count_func, id);
464
session->my_ok((ulong) session->row_count_func, id);
542
ha_rows updated=((thd->client_capabilities & CLIENT_FOUND_ROWS) ?
469
ha_rows updated=((session->client_capabilities & CLIENT_FOUND_ROWS) ?
543
470
info.touched : info.updated);
545
472
sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
546
(ulong) (info.records - info.copied), (ulong) thd->cuted_fields);
473
(ulong) (info.records - info.copied), (ulong) session->cuted_fields);
548
475
sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
549
(ulong) (info.deleted + updated), (ulong) thd->cuted_fields);
550
thd->row_count_func= info.copied + info.deleted + updated;
551
::my_ok(thd, (ulong) thd->row_count_func, id, buff);
476
(ulong) (info.deleted + updated), (ulong) session->cuted_fields);
477
session->row_count_func= info.copied + info.deleted + updated;
478
session->my_ok((ulong) session->row_count_func, id, buff);
553
thd->abort_on_warning= 0;
480
session->abort_on_warning= 0;
554
481
DRIZZLE_INSERT_END();
597
if (setup_tables_and_check_access(thd, &thd->lex->select_lex.context,
598
&thd->lex->select_lex.top_join_list,
524
if (setup_tables_and_check_access(session, &session->lex->select_lex.context,
525
&session->lex->select_lex.top_join_list,
600
&thd->lex->select_lex.leaf_tables,
527
&session->lex->select_lex.leaf_tables,
612
539
mysql_prepare_insert()
540
session Thread handler
614
541
table_list Global/local table list
615
542
table Table to insert into (can be NULL if table should
616
be taken from table_list->table)
543
be taken from table_list->table)
617
544
where Where clause (for insert ... select)
618
545
select_insert true if INSERT ... SELECT statement
619
check_fields true if need to check that all INSERT fields are
546
check_fields true if need to check that all INSERT fields are
621
abort_on_warning whether to report if some INSERT field is not
548
abort_on_warning whether to report if some INSERT field is not
622
549
assigned as an error (true) or as a warning (false).
624
551
TODO (in far future)
631
558
You MUST set table->insert_values to 0 after calling this function
632
559
before releasing the table object.
639
bool mysql_prepare_insert(THD *thd, TABLE_LIST *table_list,
640
TABLE *table, List<Item> &fields, List_item *values,
566
bool mysql_prepare_insert(Session *session, TableList *table_list,
567
Table *table, List<Item> &fields, List_item *values,
641
568
List<Item> &update_fields, List<Item> &update_values,
642
569
enum_duplicates duplic,
643
COND **where __attribute__((unused)),
644
571
bool select_insert,
645
572
bool check_fields, bool abort_on_warning)
647
SELECT_LEX *select_lex= &thd->lex->select_lex;
574
SELECT_LEX *select_lex= &session->lex->select_lex;
648
575
Name_resolution_context *context= &select_lex->context;
649
576
Name_resolution_context_state ctx_state;
650
577
bool insert_into_view= (0 != 0);
652
579
table_map map= 0;
654
581
/* INSERT should have a SELECT or VALUES clause */
655
582
assert (!select_insert || !values);
677
604
if (duplic == DUP_UPDATE)
679
606
/* it should be allocated before Item::fix_fields() */
680
if (table_list->set_insert_values(thd->mem_root))
607
if (table_list->set_insert_values(session->mem_root))
684
if (mysql_prepare_insert_check_table(thd, table_list, fields, select_insert))
611
if (mysql_prepare_insert_check_table(session, table_list, fields, select_insert))
701
628
table_list->next_local= 0;
702
629
context->resolve_in_table_list_only(table_list);
704
res= check_insert_fields(thd, context->table_list, fields, *values,
631
res= check_insert_fields(session, context->table_list, fields, *values,
705
632
!insert_into_view, &map) ||
706
setup_fields(thd, 0, *values, MARK_COLUMNS_READ, 0, 0);
633
setup_fields(session, 0, *values, MARK_COLUMNS_READ, 0, 0);
708
635
if (!res && check_fields)
710
bool saved_abort_on_warning= thd->abort_on_warning;
711
thd->abort_on_warning= abort_on_warning;
712
res= check_that_all_fields_are_given_values(thd,
637
bool saved_abort_on_warning= session->abort_on_warning;
638
session->abort_on_warning= abort_on_warning;
639
res= check_that_all_fields_are_given_values(session,
714
641
context->table_list->table,
715
642
context->table_list);
716
thd->abort_on_warning= saved_abort_on_warning;
643
session->abort_on_warning= saved_abort_on_warning;
719
646
if (!res && duplic == DUP_UPDATE)
721
res= check_update_fields(thd, context->table_list, update_fields, &map);
648
res= check_update_fields(session, context->table_list, update_fields, &map);
724
651
/* Restore the current context. */
725
652
ctx_state.restore_state(context, table_list);
728
res= setup_fields(thd, 0, update_values, MARK_COLUMNS_READ, 0, 0);
655
res= setup_fields(session, 0, update_values, MARK_COLUMNS_READ, 0, 0);
737
664
if (!select_insert)
739
TABLE_LIST *duplicate;
740
if ((duplicate= unique_table(thd, table_list, table_list->next_global, 1)))
666
TableList *duplicate;
667
if ((duplicate= unique_table(session, table_list, table_list->next_global, 1)))
742
669
update_non_unique_table_error(table_list, "INSERT", duplicate);
745
select_lex->first_execution= 0;
747
673
if (duplic == DUP_UPDATE || duplic == DUP_REPLACE)
748
674
table->prepare_for_position();
791
int write_record(THD *thd, TABLE *table,COPY_INFO *info)
717
int write_record(Session *session, Table *table,COPY_INFO *info)
795
721
MY_BITMAP *save_read_set, *save_write_set;
796
722
uint64_t prev_insert_id= table->file->next_insert_id;
797
723
uint64_t insert_id_for_cur_row= 0;
801
727
save_read_set= table->read_set;
802
728
save_write_set= table->write_set;
804
if (info->handle_duplicates == DUP_REPLACE ||
805
info->handle_duplicates == DUP_UPDATE)
730
if (info->handle_duplicates == DUP_REPLACE || info->handle_duplicates == DUP_UPDATE)
807
732
while ((error=table->file->ha_write_row(table->record[0])))
811
736
If we do more than one iteration of this loop, from the second one the
812
737
row will have an explicit value in the autoinc field, which was set at
813
738
the first call of handler::update_auto_increment(). So we must save
814
the autogenerated value to avoid thd->insert_id_for_cur_row to become
739
the autogenerated value to avoid session->insert_id_for_cur_row to become
817
742
if (table->file->insert_id_for_cur_row > 0)
868
if (!(key=(char*) my_safe_alloca(table->s->max_unique_length,
793
if (!(key=(char*) malloc(table->s->max_unique_length)))
875
key_copy((uchar*) key,table->record[0],table->key_info+key_nr,0);
799
key_copy((unsigned char*) key,table->record[0],table->key_info+key_nr,0);
876
800
if ((error=(table->file->index_read_idx_map(table->record[1],key_nr,
877
(uchar*) key, HA_WHOLE_KEY,
801
(unsigned char*) key, HA_WHOLE_KEY,
878
802
HA_READ_KEY_EXACT))))
945
869
an INSERT or DELETE(s) + INSERT; FOREIGN KEY checks in
946
870
InnoDB do not function in the defined way if we allow MySQL
947
871
to convert the latter operation internally to an UPDATE.
948
We also should not perform this conversion if we have
872
We also should not perform this conversion if we have
949
873
timestamp field with ON UPDATE which is different from DEFAULT.
950
874
Another case when conversion should not be performed is when
951
875
we have ON DELETE trigger on table so user may notice that
1006
930
after_n_copied_inc:
1008
thd->record_first_successful_insert_id_in_cur_stmt(table->file->insert_id_for_cur_row);
932
session->record_first_successful_insert_id_in_cur_stmt(table->file->insert_id_for_cur_row);
1010
934
gok_or_after_err:
1012
my_safe_afree(key,table->s->max_unique_length,MAX_KEY_LENGTH);
1013
937
if (!table->file->has_transactions())
1014
thd->transaction.stmt.modified_non_trans_table= true;
938
session->transaction.stmt.modified_non_trans_table= true;
1018
942
info->last_errno= error;
1019
943
/* current_select is NULL if this is a delayed insert */
1020
if (thd->lex->current_select)
1021
thd->lex->current_select->no_error= 0; // Give error
944
if (session->lex->current_select)
945
session->lex->current_select->no_error= 0; // Give error
1022
946
table->file->print_error(error,MYF(0));
1025
949
table->file->restore_auto_increment(prev_insert_id);
1027
my_safe_afree(key, table->s->max_unique_length, MAX_KEY_LENGTH);
1028
952
table->column_bitmaps_set(save_read_set, save_write_set);
1034
958
Check that all fields with arn't null_fields are used
1035
959
******************************************************************************/
1037
int check_that_all_fields_are_given_values(THD *thd, TABLE *entry,
1038
TABLE_LIST *table_list)
961
int check_that_all_fields_are_given_values(Session *session, Table *entry,
1041
965
MY_BITMAP *write_set= entry->write_set;
1043
967
for (Field **field=entry->field ; *field ; field++)
1045
if (!bitmap_is_set(write_set, (*field)->field_index) &&
1046
((*field)->flags & NO_DEFAULT_VALUE_FLAG) &&
969
if (!bitmap_is_set(write_set, (*field)->field_index))
972
* If the field doesn't have any default value
973
* and there is no actual value specified in the
974
* INSERT statement, throw error ER_NO_DEFAULT_FOR_FIELD.
976
if (((*field)->flags & NO_DEFAULT_VALUE_FLAG) &&
1047
977
((*field)->real_type() != DRIZZLE_TYPE_ENUM))
979
my_error(ER_NO_DEFAULT_FOR_FIELD, MYF(0), (*field)->field_name);
1052
table_list= table_list->top_table();
1056
push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1057
ER_NO_DEFAULT_FOR_FIELD,
1058
ER(ER_NO_DEFAULT_FOR_FIELD),
1059
(*field)->field_name);
986
* However, if an actual NULL value was specified
987
* for the field and the field is a NOT NULL field,
988
* throw ER_BAD_NULL_ERROR.
990
* Per the SQL standard, inserting NULL into a NOT NULL
991
* field requires an error to be thrown.
993
if (((*field)->flags & NOT_NULL_FLAG) &&
996
my_error(ER_BAD_NULL_ERROR, MYF(0), (*field)->field_name);
1064
return thd->abort_on_warning ? err : 0;
1001
return session->abort_on_warning ? err : 0;
1067
1004
/***************************************************************************
1076
1013
mysql_insert_select_prepare()
1014
session thread handler
1084
bool mysql_insert_select_prepare(THD *thd)
1021
bool mysql_insert_select_prepare(Session *session)
1023
LEX *lex= session->lex;
1087
1024
SELECT_LEX *select_lex= &lex->select_lex;
1088
TABLE_LIST *first_select_leaf_table;
1092
Statement-based replication of INSERT ... SELECT ... LIMIT is not safe
1093
as order of rows is not defined, so in mixed mode we go to row-based.
1095
Note that we may consider a statement as safe if ORDER BY primary_key
1096
is present or we SELECT a constant. However it may confuse users to
1097
see very similiar statements replicated differently.
1099
if (lex->current_select->select_limit)
1101
lex->set_stmt_unsafe();
1102
thd->set_current_stmt_binlog_row_based_if_mixed();
1105
1027
SELECT_LEX do not belong to INSERT statement, so we can't add WHERE
1106
1028
clause if table is VIEW
1109
if (mysql_prepare_insert(thd, lex->query_tables,
1031
if (mysql_prepare_insert(session, lex->query_tables,
1110
1032
lex->query_tables->table, lex->field_list, 0,
1111
1033
lex->update_list, lex->value_list,
1112
1034
lex->duplicates,
1120
1042
assert(select_lex->leaf_tables != 0);
1121
1043
lex->leaf_tables_insert= select_lex->leaf_tables;
1122
1044
/* skip all leaf tables belonged to view where we are insert */
1123
for (first_select_leaf_table= select_lex->leaf_tables->next_leaf;
1124
first_select_leaf_table &&
1125
first_select_leaf_table->belong_to_view &&
1126
first_select_leaf_table->belong_to_view ==
1127
lex->leaf_tables_insert->belong_to_view;
1128
first_select_leaf_table= first_select_leaf_table->next_leaf)
1130
select_lex->leaf_tables= first_select_leaf_table;
1045
select_lex->leaf_tables= select_lex->leaf_tables->next_leaf;
1135
select_insert::select_insert(TABLE_LIST *table_list_par, TABLE *table_par,
1050
select_insert::select_insert(TableList *table_list_par, Table *table_par,
1136
1051
List<Item> *fields_par,
1137
1052
List<Item> *update_fields,
1138
1053
List<Item> *update_values,
1167
1082
we are fixing fields from insert list.
1169
1084
lex->current_select= &lex->select_lex;
1170
res= check_insert_fields(thd, table_list, *fields, values,
1085
res= check_insert_fields(session, table_list, *fields, values,
1171
1086
!insert_into_view, &map) ||
1172
setup_fields(thd, 0, values, MARK_COLUMNS_READ, 0, 0);
1087
setup_fields(session, 0, values, MARK_COLUMNS_READ, 0, 0);
1174
1089
if (!res && fields->elements)
1176
bool saved_abort_on_warning= thd->abort_on_warning;
1177
thd->abort_on_warning= !info.ignore;
1178
res= check_that_all_fields_are_given_values(thd, table_list->table,
1091
bool saved_abort_on_warning= session->abort_on_warning;
1092
session->abort_on_warning= !info.ignore;
1093
res= check_that_all_fields_are_given_values(session, table_list->table,
1180
thd->abort_on_warning= saved_abort_on_warning;
1095
session->abort_on_warning= saved_abort_on_warning;
1183
1098
if (info.handle_duplicates == DUP_UPDATE && !res)
1192
1107
table_list->next_local= 0;
1193
1108
context->resolve_in_table_list_only(table_list);
1195
res= res || check_update_fields(thd, context->table_list,
1110
res= res || check_update_fields(session, context->table_list,
1196
1111
*info.update_fields, &map);
1198
When we are not using GROUP BY and there are no ungrouped aggregate functions
1113
When we are not using GROUP BY and there are no ungrouped aggregate functions
1199
1114
we can refer to other tables in the ON DUPLICATE KEY part.
1200
1115
We use next_name_resolution_table descructively, so check it first (views?)
1206
1121
We must make a single context out of the two separate name resolution contexts :
1207
1122
the INSERT table and the tables in the SELECT part of INSERT ... SELECT.
1208
1123
To do that we must concatenate the two lists
1210
table_list->next_name_resolution_table=
1125
table_list->next_name_resolution_table=
1211
1126
ctx_state.get_first_name_resolution_table();
1213
res= res || setup_fields(thd, 0, *info.update_values,
1128
res= res || setup_fields(session, 0, *info.update_values,
1214
1129
MARK_COLUMNS_READ, 0, 0);
1256
1171
else if (!(lex->current_select->options & OPTION_BUFFER_RESULT))
1259
We must not yet prepare the result table if it is the same as one of the
1260
source tables (INSERT SELECT). The preparation may disable
1174
We must not yet prepare the result table if it is the same as one of the
1175
source tables (INSERT SELECT). The preparation may disable
1261
1176
indexes on the result table, which may be used during the select, if it
1262
1177
is the same table (Bug #6034). Do the preparation after the select phase
1263
1178
in select_insert::prepare2().
1269
1184
restore_record(table,s->default_values); // Get empty record
1270
1185
table->next_number_field=table->found_next_number_field;
1272
#ifdef HAVE_REPLICATION
1273
if (thd->slave_thread &&
1274
(info.handle_duplicates == DUP_UPDATE) &&
1275
(table->next_number_field != NULL) &&
1276
rpl_master_has_bug(&active_mi->rli, 24432))
1280
thd->cuted_fields=0;
1187
session->cuted_fields=0;
1281
1188
if (info.ignore || info.handle_duplicates != DUP_ERROR)
1282
1189
table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
1283
1190
if (info.handle_duplicates == DUP_REPLACE)
1284
1191
table->file->extra(HA_EXTRA_WRITE_CAN_REPLACE);
1285
1192
if (info.handle_duplicates == DUP_UPDATE)
1286
1193
table->file->extra(HA_EXTRA_INSERT_WITH_UPDATE);
1287
thd->abort_on_warning= !info.ignore;
1194
session->abort_on_warning= !info.ignore;
1288
1195
table->mark_columns_needed_for_insert();
1326
1233
select_insert::~select_insert()
1331
1238
table->next_number_field=0;
1332
1239
table->auto_increment_field_not_null= false;
1333
1240
table->file->ha_reset();
1335
thd->count_cuted_fields= CHECK_FIELD_IGNORE;
1336
thd->abort_on_warning= 0;
1242
session->count_cuted_fields= CHECK_FIELD_IGNORE;
1243
session->abort_on_warning= 0;
1341
1248
bool select_insert::send_data(List<Item> &values)
1346
1253
if (unit->offset_limit_cnt)
1352
thd->count_cuted_fields= CHECK_FIELD_WARN; // Calculate cuted fields
1259
session->count_cuted_fields= CHECK_FIELD_WARN; // Calculate cuted fields
1353
1260
store_values(values);
1354
thd->count_cuted_fields= CHECK_FIELD_IGNORE;
1355
if (thd->is_error())
1261
session->count_cuted_fields= CHECK_FIELD_IGNORE;
1262
if (session->is_error())
1358
error= write_record(thd, table, &info);
1265
error= write_record(session, table, &info);
1362
1269
if (info.handle_duplicates == DUP_UPDATE)
1377
1284
If no value has been autogenerated so far, we need to remember the
1378
1285
value we just saw, we may need to send it to client in the end.
1380
if (thd->first_successful_insert_id_in_cur_stmt == 0) // optimization
1381
autoinc_value_of_last_inserted_row=
1287
if (session->first_successful_insert_id_in_cur_stmt == 0) // optimization
1288
autoinc_value_of_last_inserted_row=
1382
1289
table->next_number_field->val_int();
1384
1291
Clear auto-increment field for the next record, if triggers are used
1394
1301
void select_insert::store_values(List<Item> &values)
1396
1303
if (fields->elements)
1397
fill_record(thd, *fields, values, 1);
1304
fill_record(session, *fields, values, 1);
1399
fill_record(thd, table->field, values, 1);
1306
fill_record(session, table->field, values, 1);
1402
void select_insert::send_error(uint errcode,const char *err)
1309
void select_insert::send_error(uint32_t errcode,const char *err)
1406
1313
my_message(errcode, err, MYF(0));
1427
1333
We must invalidate the table in the query cache before binlog writing
1428
1334
and ha_autocommit_or_rollback.
1430
if (thd->transaction.stmt.modified_non_trans_table)
1431
thd->transaction.all.modified_non_trans_table= true;
1336
if (session->transaction.stmt.modified_non_trans_table)
1337
session->transaction.all.modified_non_trans_table= true;
1433
assert(trans_table || !changed ||
1434
thd->transaction.stmt.modified_non_trans_table);
1339
assert(trans_table || !changed ||
1340
session->transaction.stmt.modified_non_trans_table);
1437
Write to binlog before commiting transaction. No statement will
1438
be written by the binlog_query() below in RBR mode. All the
1439
events are in the transaction cache and will be written when
1440
ha_autocommit_or_rollback() is issued below.
1442
if (mysql_bin_log.is_open())
1446
thd->binlog_query(THD::ROW_QUERY_TYPE,
1447
thd->query, thd->query_length,
1448
trans_table, false, killed_status);
1450
1342
table->file->ha_release_auto_increment();
1457
1349
char buff[160];
1458
1350
if (info.ignore)
1459
1351
sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
1460
(ulong) (info.records - info.copied), (ulong) thd->cuted_fields);
1352
(ulong) (info.records - info.copied), (ulong) session->cuted_fields);
1462
1354
sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
1463
(ulong) (info.deleted+info.updated), (ulong) thd->cuted_fields);
1464
thd->row_count_func= info.copied + info.deleted +
1465
((thd->client_capabilities & CLIENT_FOUND_ROWS) ?
1355
(ulong) (info.deleted+info.updated), (ulong) session->cuted_fields);
1356
session->row_count_func= info.copied + info.deleted +
1357
((session->client_capabilities & CLIENT_FOUND_ROWS) ?
1466
1358
info.touched : info.updated);
1468
id= (thd->first_successful_insert_id_in_cur_stmt > 0) ?
1469
thd->first_successful_insert_id_in_cur_stmt :
1470
(thd->arg_of_last_insert_id_function ?
1471
thd->first_successful_insert_id_in_prev_stmt :
1360
id= (session->first_successful_insert_id_in_cur_stmt > 0) ?
1361
session->first_successful_insert_id_in_cur_stmt :
1362
(session->arg_of_last_insert_id_function ?
1363
session->first_successful_insert_id_in_prev_stmt :
1472
1364
(info.copied ? autoinc_value_of_last_inserted_row : 0));
1473
::my_ok(thd, (ulong) thd->row_count_func, id, buff);
1365
session->my_ok((ulong) session->row_count_func, id, buff);
1477
1369
void select_insert::abort() {
1481
1373
If the creation of the table failed (due to a syntax error, for
1482
1374
example), no table will have been opened and therefore 'table'
1506
1398
changed= (info.copied || info.deleted || info.updated);
1507
1399
transactional_table= table->file->has_transactions();
1508
if (thd->transaction.stmt.modified_non_trans_table)
1510
if (mysql_bin_log.is_open())
1511
thd->binlog_query(THD::ROW_QUERY_TYPE, thd->query, thd->query_length,
1512
transactional_table, false);
1513
if (!thd->current_stmt_binlog_row_based && !can_rollback_data())
1514
thd->transaction.all.modified_non_trans_table= true;
1516
1400
assert(transactional_table || !changed ||
1517
thd->transaction.stmt.modified_non_trans_table);
1401
session->transaction.stmt.modified_non_trans_table);
1518
1402
table->file->ha_release_auto_increment();
1527
1411
***************************************************************************/
1530
Create table from lists of fields and items (or just return TABLE
1414
Create table from lists of fields and items (or just return Table
1531
1415
object for pre-opened existing table).
1534
1418
create_table_from_items()
1535
thd in Thread object
1419
session in Thread object
1536
1420
create_info in Create information (like MAX_ROWS, ENGINE or
1537
1421
temporary table flag)
1538
create_table in Pointer to TABLE_LIST object providing database
1422
create_table in Pointer to TableList object providing database
1539
1423
and name for table to be created or to be open
1540
1424
alter_info in/out Initial list of columns and indexes for the table
1553
1437
This function behaves differently for base and temporary tables:
1554
1438
- For base table we assume that either table exists and was pre-opened
1555
1439
and locked at open_and_lock_tables() stage (and in this case we just
1556
emit error or warning and return pre-opened TABLE object) or special
1440
emit error or warning and return pre-opened Table object) or special
1557
1441
placeholder was put in table cache that guarantees that this table
1558
1442
won't be created or opened until the placeholder will be removed
1559
1443
(so there is an exclusive lock on this table).
1564
1448
SELECT it should be changed before it can be used in other contexts.
1567
non-zero Pointer to TABLE object for table created or opened
1451
non-zero Pointer to Table object for table created or opened
1571
static TABLE *create_table_from_items(THD *thd, HA_CREATE_INFO *create_info,
1572
TABLE_LIST *create_table,
1455
static Table *create_table_from_items(Session *session, HA_CREATE_INFO *create_info,
1456
TableList *create_table,
1573
1457
Alter_info *alter_info,
1574
1458
List<Item> *items,
1575
DRIZZLE_LOCK **lock,
1576
TABLEOP_HOOKS *hooks)
1459
DRIZZLE_LOCK **lock)
1578
TABLE tmp_table; // Used during 'Create_field()'
1461
Table tmp_table; // Used during 'Create_field()'
1579
1462
TABLE_SHARE share;
1581
uint select_field_count= items->elements;
1464
uint32_t select_field_count= items->elements;
1582
1465
/* Add selected items to field list */
1583
1466
List_iterator_fast<Item> it(*items);
1592
1475
if (create_info->options & HA_LEX_CREATE_IF_NOT_EXISTS)
1594
1477
create_info->table_existed= 1; // Mark that table existed
1595
push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1478
push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1596
1479
ER_TABLE_EXISTS_ERROR, ER(ER_TABLE_EXISTS_ERROR),
1597
1480
create_table->table_name);
1598
1481
return(create_table->table);
1605
1488
tmp_table.alias= 0;
1606
1489
tmp_table.timestamp_field= 0;
1607
1490
tmp_table.s= &share;
1608
init_tmp_table_share(thd, &share, "", 0, "", "");
1491
init_tmp_table_share(session, &share, "", 0, "", "");
1610
1493
tmp_table.s->db_create_options=0;
1611
1494
tmp_table.s->blob_ptr_size= portable_sizeof_char_ptr;
1612
tmp_table.s->db_low_byte_first=
1495
tmp_table.s->db_low_byte_first=
1613
1496
test(create_info->db_type == myisam_hton ||
1614
1497
create_info->db_type == heap_hton);
1615
1498
tmp_table.null_row= false;
1626
1509
field= item->tmp_table_field_from_field_type(&tmp_table, 0);
1628
field= create_tmp_field(thd, &tmp_table, item, item->type(),
1511
field= create_tmp_field(session, &tmp_table, item, item->type(),
1629
1512
(Item ***) 0, &tmp_field, &def_field, 0, 0, 0, 0,
1651
1534
binlog when a HEAP table is opened for the first time since startup, must
1652
1535
not be written: 1) it would be wrong (imagine we're in CREATE SELECT: we
1653
1536
don't want to delete from it) 2) it would be written before the CREATE
1654
TABLE, which is a wrong order. So we keep binary logging disabled when we
1537
Table, which is a wrong order. So we keep binary logging disabled when we
1658
tmp_disable_binlog(thd);
1659
if (!mysql_create_table_no_lock(thd, create_table->db,
1541
if (!mysql_create_table_no_lock(session, create_table->db,
1660
1542
create_table->table_name,
1661
1543
create_info, alter_info, 0,
1662
select_field_count))
1544
select_field_count, true))
1664
1546
if (create_info->table_existed &&
1665
1547
!(create_info->options & HA_LEX_CREATE_TMP_TABLE))
1676
1558
if (!(create_info->options & HA_LEX_CREATE_TMP_TABLE))
1678
VOID(pthread_mutex_lock(&LOCK_open));
1679
if (reopen_name_locked_table(thd, create_table, false))
1560
pthread_mutex_lock(&LOCK_open);
1561
if (reopen_name_locked_table(session, create_table, false))
1681
1563
quick_rm_table(create_info->db_type, create_table->db,
1682
1564
table_case_name(create_info, create_table->table_name),
1686
1568
table= create_table->table;
1687
VOID(pthread_mutex_unlock(&LOCK_open));
1569
pthread_mutex_unlock(&LOCK_open);
1691
if (!(table= open_table(thd, create_table, thd->mem_root, (bool*) 0,
1573
if (!(table= open_table(session, create_table, (bool*) 0,
1692
1574
DRIZZLE_OPEN_TEMPORARY_ONLY)) &&
1693
1575
!create_info->table_existed)
1697
1579
it preparable for open. But let us do close_temporary_table() here
1700
drop_temporary_table(thd, create_table);
1582
drop_temporary_table(session, create_table);
1704
reenable_binlog(thd);
1705
1586
if (!table) // open failed
1709
1590
table->reginfo.lock_type=TL_WRITE;
1710
hooks->prelock(&table, 1); // Call prelock hooks
1711
if (! ((*lock)= mysql_lock_tables(thd, &table, 1,
1712
DRIZZLE_LOCK_IGNORE_FLUSH, ¬_used)) ||
1713
hooks->postlock(&table, 1))
1591
if (! ((*lock)= mysql_lock_tables(session, &table, 1,
1592
DRIZZLE_LOCK_IGNORE_FLUSH, ¬_used)))
1717
mysql_unlock_tables(thd, *lock);
1596
mysql_unlock_tables(session, *lock);
1721
1600
if (!create_info->table_existed)
1722
drop_open_table(thd, table, create_table->db, create_table->table_name);
1601
drop_open_table(session, table, create_table->db, create_table->table_name);
1751
1627
slave. Hence, we have to hold on to the CREATE part of the
1752
1628
statement until the statement has finished.
1754
class MY_HOOKS : public TABLEOP_HOOKS {
1756
MY_HOOKS(select_create *x, TABLE_LIST *create_table,
1757
TABLE_LIST *select_tables)
1758
: ptr(x), all_tables(*create_table)
1760
all_tables.next_global= select_tables;
1764
virtual int do_postlock(TABLE **tables, uint count)
1766
THD *thd= const_cast<THD*>(ptr->get_thd());
1767
if (int error= decide_logging_format(thd, &all_tables))
1770
TABLE const *const table = *tables;
1771
if (thd->current_stmt_binlog_row_based &&
1772
!table->s->tmp_table &&
1773
!ptr->get_create_info()->table_existed)
1775
ptr->binlog_show_create_table(tables, count);
1781
TABLE_LIST all_tables;
1784
MY_HOOKS hooks(this, create_table, select_tables);
1791
1635
row-based replication for the statement. If we are creating a
1792
1636
temporary table, we need to start a statement transaction.
1794
if ((thd->lex->create_info.options & HA_LEX_CREATE_TMP_TABLE) == 0 &&
1795
thd->current_stmt_binlog_row_based)
1797
thd->binlog_start_trans_and_stmt();
1800
if (!(table= create_table_from_items(thd, create_info, create_table,
1639
if (!(table= create_table_from_items(session, create_info, create_table,
1801
1640
alter_info, &values,
1802
&extra_lock, hook_ptr)))
1803
1642
return(-1); // abort() deletes table
1805
1644
if (extra_lock)
1840
1679
if (info.handle_duplicates == DUP_UPDATE)
1841
1680
table->file->extra(HA_EXTRA_INSERT_WITH_UPDATE);
1842
1681
table->file->ha_start_bulk_insert((ha_rows) 0);
1843
thd->abort_on_warning= !info.ignore;
1844
if (check_that_all_fields_are_given_values(thd, table, table_list))
1682
session->abort_on_warning= !info.ignore;
1683
if (check_that_all_fields_are_given_values(session, table, table_list))
1846
1685
table->mark_columns_needed_for_insert();
1847
1686
table->file->extra(HA_EXTRA_WRITE_CACHE);
1852
select_create::binlog_show_create_table(TABLE **tables, uint count)
1855
Note 1: In RBR mode, we generate a CREATE TABLE statement for the
1856
created table by calling store_create_info() (behaves as SHOW
1857
CREATE TABLE). In the event of an error, nothing should be
1858
written to the binary log, even if the table is non-transactional;
1859
therefore we pretend that the generated CREATE TABLE statement is
1860
for a transactional table. The event will then be put in the
1861
transaction cache, and any subsequent events (e.g., table-map
1862
events and binrow events) will also be put there. We can then use
1863
ha_autocommit_or_rollback() to either throw away the entire
1864
kaboodle of events, or write them to the binary log.
1866
We write the CREATE TABLE statement here and not in prepare()
1867
since there potentially are sub-selects or accesses to information
1868
schema that will do a close_thread_tables(), destroying the
1869
statement transaction cache.
1871
assert(thd->current_stmt_binlog_row_based);
1872
assert(tables && *tables && count > 0);
1875
String query(buf, sizeof(buf), system_charset_info);
1877
TABLE_LIST tmp_table_list;
1879
memset(&tmp_table_list, 0, sizeof(tmp_table_list));
1880
tmp_table_list.table = *tables;
1881
query.length(0); // Have to zero it since constructor doesn't
1883
result= store_create_info(thd, &tmp_table_list, &query, create_info);
1884
assert(result == 0); /* store_create_info() always return 0 */
1886
thd->binlog_query(THD::STMT_QUERY_TYPE,
1887
query.ptr(), query.length(),
1888
/* is_trans */ true,
1889
/* suppress_use */ false);
1892
1690
void select_create::store_values(List<Item> &values)
1894
fill_record(thd, field, values, 1);
1692
fill_record(session, field, values, 1);
1898
void select_create::send_error(uint errcode,const char *err)
1696
void select_create::send_error(uint32_t errcode,const char *err)
1903
1701
This will execute any rollbacks that are necessary before writing
1933
1729
if (!table->s->tmp_table)
1935
ha_autocommit_or_rollback(thd, 0);
1936
end_active_trans(thd);
1731
ha_autocommit_or_rollback(session, 0);
1732
end_active_trans(session);
1939
1735
table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
1940
1736
table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
1943
mysql_unlock_tables(thd, *m_plock);
1739
mysql_unlock_tables(session, *m_plock);
1944
1740
*m_plock= NULL;