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) : malloc(size))
38
#define my_safe_afree(ptr, size, min_length) if (size > min_length) free(ptr)
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, TableList *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
63
Table *table= table_list->table;
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);
118
if (check_unique && thd->dup_field)
112
if (check_unique && session->dup_field)
120
my_error(ER_FIELD_SPECIFIED_TWICE, MYF(0), thd->dup_field->field_name);
114
my_error(ER_FIELD_SPECIFIED_TWICE, MYF(0), session->dup_field->field_name);
123
117
if (table->timestamp_field) // Don't automaticly set timestamp if used
125
if (bitmap_is_set(table->write_set,
126
table->timestamp_field->field_index))
119
if (table->timestamp_field->isWriteSet())
127
120
clear_timestamp_auto_bits(table->timestamp_field_type,
128
121
TIMESTAMP_AUTO_SET_ON_INSERT);
131
bitmap_set_bit(table->write_set,
132
table->timestamp_field->field_index);
124
table->setWriteSet(table->timestamp_field->field_index);
160
static int check_update_fields(THD *thd, TableList *insert_table_list,
152
static int check_update_fields(Session *session, TableList *insert_table_list,
161
153
List<Item> &update_fields,
162
table_map *map __attribute__((unused)))
164
156
Table *table= insert_table_list->table;
165
157
bool timestamp_mark= false;
177
169
/* Check the fields we are going to modify */
178
if (setup_fields(thd, 0, update_fields, MARK_COLUMNS_WRITE, 0, 0))
170
if (setup_fields(session, 0, update_fields, MARK_COLUMNS_WRITE, 0, 0))
181
173
if (table->timestamp_field)
183
175
/* Don't set timestamp column if this is modified. */
184
if (bitmap_is_set(table->write_set,
185
table->timestamp_field->field_index))
176
if (table->timestamp_field->isWriteSet())
186
177
clear_timestamp_auto_bits(table->timestamp_field_type,
187
178
TIMESTAMP_AUTO_SET_ON_UPDATE);
188
179
if (timestamp_mark)
189
bitmap_set_bit(table->write_set,
190
table->timestamp_field->field_index);
180
table->setWriteSet(table->timestamp_field->field_index);
247
236
Name_resolution_context_state ctx_state;
248
237
thr_lock_type lock_type;
249
238
Item *unused_conds= 0;
253
242
Upgrade lock type if the requested lock is incompatible with
254
243
the current connection mode or table operation.
256
upgrade_lock_type(thd, &table_list->lock_type, duplic,
245
upgrade_lock_type(session, &table_list->lock_type, duplic,
257
246
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);
248
if (open_and_lock_tables(session, table_list))
274
if (open_and_lock_tables(thd, table_list))
277
251
lock_type= table_list->lock_type;
279
thd_proc_info(thd, "init");
253
session->set_proc_info("init");
254
session->used_tables=0;
282
256
value_count= values->elements;
284
if (mysql_prepare_insert(thd, table_list, table, fields, values,
258
if (mysql_prepare_insert(session, table_list, table, fields, values,
285
259
update_fields, update_values, duplic, &unused_conds,
287
261
(fields.elements || !value_count ||
319
293
my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), counter);
322
if (setup_fields(thd, 0, *values, MARK_COLUMNS_READ, 0, 0))
296
if (setup_fields(session, 0, *values, MARK_COLUMNS_READ, 0, 0))
327
301
/* Restore the current context. */
328
302
ctx_state.restore_state(context, table_list);
341
315
For single line insert, generate an error if try to set a NOT NULL field
344
thd->count_cuted_fields= ((values_list.elements == 1 &&
318
session->count_cuted_fields= ((values_list.elements == 1 &&
346
320
CHECK_FIELD_ERROR_FOR_NULL :
347
321
CHECK_FIELD_WARN);
348
thd->cuted_fields = 0L;
322
session->cuted_fields = 0L;
349
323
table->next_number_field=table->found_next_number_field;
351
if (thd->slave_thread &&
352
(info.handle_duplicates == DUP_UPDATE) &&
353
(table->next_number_field != NULL) &&
354
rpl_master_has_bug(&active_mi->rli, 24432))
358
thd_proc_info(thd, "update");
326
session->set_proc_info("update");
359
327
if (duplic == DUP_REPLACE)
360
328
table->file->extra(HA_EXTRA_WRITE_CAN_REPLACE);
361
329
if (duplic == DUP_UPDATE)
376
344
if (fields.elements || !value_count)
378
restore_record(table,s->default_values); // Get empty record
379
if (fill_record(thd, fields, *values, 0))
346
table->restoreRecordAsDefault(); // Get empty record
347
if (fill_record(session, fields, *values, 0))
381
if (values_list.elements != 1 && ! thd->is_error())
349
if (values_list.elements != 1 && ! session->is_error())
387
TODO: set thd->abort_on_warning if values_list.elements == 1
355
TODO: set session->abort_on_warning if values_list.elements == 1
388
356
and check that all items return warning in case of problem with
406
374
table->record[0][0]= table->s->default_values[0];
408
if (fill_record(thd, table->field, *values, 0))
376
if (fill_record(session, table->field, *values, 0))
410
if (values_list.elements != 1 && ! thd->is_error())
378
if (values_list.elements != 1 && ! session->is_error())
420
error=write_record(thd, table ,&info);
388
// Release latches in case bulk insert takes a long time
389
ha_release_temporary_latches(session);
391
error=write_record(session, table ,&info);
394
session->row_count++;
426
free_underlaid_joins(thd, &thd->lex->select_lex);
397
free_underlaid_joins(session, &session->lex->select_lex);
427
398
joins_freed= true;
447
418
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);
420
changed= (info.copied || info.deleted || info.updated);
421
if ((changed && error <= 0) || session->transaction.stmt.modified_non_trans_table)
423
if (session->transaction.stmt.modified_non_trans_table)
424
session->transaction.all.modified_non_trans_table= true;
426
assert(transactional_table || !changed || session->transaction.stmt.modified_non_trans_table);
504
thd_proc_info(thd, "end");
429
session->set_proc_info("end");
506
431
We'll report to the client this id:
507
432
- if the table contains an autoincrement column and we successfully
512
437
inserted, the id of the last "inserted" row (if IGNORE, that value may not
513
438
have been really inserted but ignored).
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 :
440
id= (session->first_successful_insert_id_in_cur_stmt > 0) ?
441
session->first_successful_insert_id_in_cur_stmt :
442
(session->arg_of_last_insert_id_function ?
443
session->first_successful_insert_id_in_prev_stmt :
519
444
((table->next_number_field && info.copied) ?
520
445
table->next_number_field->val_int() : 0));
521
446
table->next_number_field=0;
522
thd->count_cuted_fields= CHECK_FIELD_IGNORE;
447
session->count_cuted_fields= CHECK_FIELD_IGNORE;
523
448
table->auto_increment_field_not_null= false;
524
449
if (duplic == DUP_REPLACE)
525
450
table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
529
if (values_list.elements == 1 && (!(thd->options & OPTION_WARNINGS) ||
454
if (values_list.elements == 1 && (!(session->options & OPTION_WARNINGS) ||
455
!session->cuted_fields))
532
thd->row_count_func= info.copied + info.deleted +
533
((thd->client_capabilities & CLIENT_FOUND_ROWS) ?
457
session->row_count_func= info.copied + info.deleted +
458
((session->client_capabilities & CLIENT_FOUND_ROWS) ?
534
459
info.touched : info.updated);
535
my_ok(thd, (ulong) thd->row_count_func, id);
460
session->my_ok((ulong) session->row_count_func, id);
540
ha_rows updated=((thd->client_capabilities & CLIENT_FOUND_ROWS) ?
465
ha_rows updated=((session->client_capabilities & CLIENT_FOUND_ROWS) ?
541
466
info.touched : info.updated);
543
468
sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
544
(ulong) (info.records - info.copied), (ulong) thd->cuted_fields);
469
(ulong) (info.records - info.copied), (ulong) session->cuted_fields);
546
471
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);
472
(ulong) (info.deleted + updated), (ulong) session->cuted_fields);
473
session->row_count_func= info.copied + info.deleted + updated;
474
session->my_ok((ulong) session->row_count_func, id, buff);
551
thd->abort_on_warning= 0;
476
session->abort_on_warning= 0;
552
477
DRIZZLE_INSERT_END();
595
if (setup_tables_and_check_access(thd, &thd->lex->select_lex.context,
596
&thd->lex->select_lex.top_join_list,
520
if (setup_tables_and_check_access(session, &session->lex->select_lex.context,
521
&session->lex->select_lex.top_join_list,
598
&thd->lex->select_lex.leaf_tables,
523
&session->lex->select_lex.leaf_tables,
610
535
mysql_prepare_insert()
536
session Thread handler
612
537
table_list Global/local table list
613
538
table Table to insert into (can be NULL if table should
614
be taken from table_list->table)
539
be taken from table_list->table)
615
540
where Where clause (for insert ... select)
616
541
select_insert true if INSERT ... SELECT statement
617
check_fields true if need to check that all INSERT fields are
542
check_fields true if need to check that all INSERT fields are
619
abort_on_warning whether to report if some INSERT field is not
544
abort_on_warning whether to report if some INSERT field is not
620
545
assigned as an error (true) or as a warning (false).
622
547
TODO (in far future)
629
554
You MUST set table->insert_values to 0 after calling this function
630
555
before releasing the table object.
637
bool mysql_prepare_insert(THD *thd, TableList *table_list,
562
bool mysql_prepare_insert(Session *session, TableList *table_list,
638
563
Table *table, List<Item> &fields, List_item *values,
639
564
List<Item> &update_fields, List<Item> &update_values,
640
565
enum_duplicates duplic,
641
COND **where __attribute__((unused)),
642
567
bool select_insert,
643
568
bool check_fields, bool abort_on_warning)
645
SELECT_LEX *select_lex= &thd->lex->select_lex;
570
Select_Lex *select_lex= &session->lex->select_lex;
646
571
Name_resolution_context *context= &select_lex->context;
647
572
Name_resolution_context_state ctx_state;
648
573
bool insert_into_view= (0 != 0);
650
575
table_map map= 0;
652
577
/* INSERT should have a SELECT or VALUES clause */
653
578
assert (!select_insert || !values);
656
581
For subqueries in VALUES() we should not see the table in which we are
657
582
inserting (for INSERT ... SELECT this is done by changing table_list,
658
because INSERT ... SELECT share SELECT_LEX it with SELECT.
583
because INSERT ... SELECT share Select_Lex it with SELECT.
660
585
if (!select_insert)
662
for (SELECT_LEX_UNIT *un= select_lex->first_inner_unit();
587
for (Select_Lex_Unit *un= select_lex->first_inner_unit();
664
589
un= un->next_unit())
666
for (SELECT_LEX *sl= un->first_select();
591
for (Select_Lex *sl= un->first_select();
668
593
sl= sl->next_select())
675
600
if (duplic == DUP_UPDATE)
677
602
/* it should be allocated before Item::fix_fields() */
678
if (table_list->set_insert_values(thd->mem_root))
603
if (table_list->set_insert_values(session->mem_root))
682
if (mysql_prepare_insert_check_table(thd, table_list, fields, select_insert))
607
if (mysql_prepare_insert_check_table(session, table_list, fields, select_insert))
699
624
table_list->next_local= 0;
700
625
context->resolve_in_table_list_only(table_list);
702
res= check_insert_fields(thd, context->table_list, fields, *values,
627
res= check_insert_fields(session, context->table_list, fields, *values,
703
628
!insert_into_view, &map) ||
704
setup_fields(thd, 0, *values, MARK_COLUMNS_READ, 0, 0);
629
setup_fields(session, 0, *values, MARK_COLUMNS_READ, 0, 0);
706
631
if (!res && check_fields)
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,
633
bool saved_abort_on_warning= session->abort_on_warning;
634
session->abort_on_warning= abort_on_warning;
635
res= check_that_all_fields_are_given_values(session,
712
637
context->table_list->table,
713
638
context->table_list);
714
thd->abort_on_warning= saved_abort_on_warning;
639
session->abort_on_warning= saved_abort_on_warning;
717
642
if (!res && duplic == DUP_UPDATE)
719
res= check_update_fields(thd, context->table_list, update_fields, &map);
644
res= check_update_fields(session, context->table_list, update_fields, &map);
722
647
/* Restore the current context. */
723
648
ctx_state.restore_state(context, table_list);
726
res= setup_fields(thd, 0, update_values, MARK_COLUMNS_READ, 0, 0);
651
res= setup_fields(session, 0, update_values, MARK_COLUMNS_READ, 0, 0);
788
int write_record(THD *thd, Table *table,COPY_INFO *info)
713
int write_record(Session *session, Table *table,COPY_INFO *info)
792
717
MY_BITMAP *save_read_set, *save_write_set;
793
718
uint64_t prev_insert_id= table->file->next_insert_id;
794
719
uint64_t insert_id_for_cur_row= 0;
798
723
save_read_set= table->read_set;
799
724
save_write_set= table->write_set;
801
if (info->handle_duplicates == DUP_REPLACE ||
802
info->handle_duplicates == DUP_UPDATE)
726
if (info->handle_duplicates == DUP_REPLACE || info->handle_duplicates == DUP_UPDATE)
804
728
while ((error=table->file->ha_write_row(table->record[0])))
883
806
an error is returned
885
808
assert(table->insert_values != NULL);
886
store_record(table,insert_values);
887
restore_record(table,record[1]);
809
table->storeRecordAsInsert();
810
table->restoreRecord();
888
811
assert(info->update_fields->elements ==
889
812
info->update_values->elements);
890
if (fill_record(thd, *info->update_fields,
813
if (fill_record(session, *info->update_fields,
891
814
*info->update_values,
942
865
an INSERT or DELETE(s) + INSERT; FOREIGN KEY checks in
943
866
InnoDB do not function in the defined way if we allow MySQL
944
867
to convert the latter operation internally to an UPDATE.
945
We also should not perform this conversion if we have
868
We also should not perform this conversion if we have
946
869
timestamp field with ON UPDATE which is different from DEFAULT.
947
870
Another case when conversion should not be performed is when
948
871
we have ON DELETE trigger on table so user may notice that
1003
926
after_n_copied_inc:
1005
thd->record_first_successful_insert_id_in_cur_stmt(table->file->insert_id_for_cur_row);
928
session->record_first_successful_insert_id_in_cur_stmt(table->file->insert_id_for_cur_row);
1007
930
gok_or_after_err:
1009
my_safe_afree(key,table->s->max_unique_length,MAX_KEY_LENGTH);
1010
933
if (!table->file->has_transactions())
1011
thd->transaction.stmt.modified_non_trans_table= true;
934
session->transaction.stmt.modified_non_trans_table= true;
1015
938
info->last_errno= error;
1016
939
/* current_select is NULL if this is a delayed insert */
1017
if (thd->lex->current_select)
1018
thd->lex->current_select->no_error= 0; // Give error
940
if (session->lex->current_select)
941
session->lex->current_select->no_error= 0; // Give error
1019
942
table->file->print_error(error,MYF(0));
1022
945
table->file->restore_auto_increment(prev_insert_id);
1024
my_safe_afree(key, table->s->max_unique_length, MAX_KEY_LENGTH);
1025
948
table->column_bitmaps_set(save_read_set, save_write_set);
1031
954
Check that all fields with arn't null_fields are used
1032
955
******************************************************************************/
1034
int check_that_all_fields_are_given_values(THD *thd, Table *entry,
1035
TableList *table_list)
957
int check_that_all_fields_are_given_values(Session *session, Table *entry,
1038
MY_BITMAP *write_set= entry->write_set;
1040
962
for (Field **field=entry->field ; *field ; field++)
1042
if (!bitmap_is_set(write_set, (*field)->field_index) &&
1043
((*field)->flags & NO_DEFAULT_VALUE_FLAG) &&
964
if (((*field)->isWriteSet()) == false)
967
* If the field doesn't have any default value
968
* and there is no actual value specified in the
969
* INSERT statement, throw error ER_NO_DEFAULT_FOR_FIELD.
971
if (((*field)->flags & NO_DEFAULT_VALUE_FLAG) &&
1044
972
((*field)->real_type() != DRIZZLE_TYPE_ENUM))
974
my_error(ER_NO_DEFAULT_FOR_FIELD, MYF(0), (*field)->field_name);
1049
table_list= table_list->top_table();
1053
push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1054
ER_NO_DEFAULT_FOR_FIELD,
1055
ER(ER_NO_DEFAULT_FOR_FIELD),
1056
(*field)->field_name);
981
* However, if an actual NULL value was specified
982
* for the field and the field is a NOT NULL field,
983
* throw ER_BAD_NULL_ERROR.
985
* Per the SQL standard, inserting NULL into a NOT NULL
986
* field requires an error to be thrown.
988
if (((*field)->flags & NOT_NULL_FLAG) &&
991
my_error(ER_BAD_NULL_ERROR, MYF(0), (*field)->field_name);
1061
return thd->abort_on_warning ? err : 0;
996
return session->abort_on_warning ? err : 0;
1064
999
/***************************************************************************
1073
1008
mysql_insert_select_prepare()
1009
session thread handler
1081
bool mysql_insert_select_prepare(THD *thd)
1016
bool mysql_insert_select_prepare(Session *session)
1084
SELECT_LEX *select_lex= &lex->select_lex;
1088
Statement-based replication of INSERT ... SELECT ... LIMIT is not safe
1089
as order of rows is not defined, so in mixed mode we go to row-based.
1091
Note that we may consider a statement as safe if ORDER BY primary_key
1092
is present or we SELECT a constant. However it may confuse users to
1093
see very similiar statements replicated differently.
1095
if (lex->current_select->select_limit)
1097
lex->set_stmt_unsafe();
1098
thd->set_current_stmt_binlog_row_based_if_mixed();
1101
SELECT_LEX do not belong to INSERT statement, so we can't add WHERE
1018
LEX *lex= session->lex;
1019
Select_Lex *select_lex= &lex->select_lex;
1022
Select_Lex do not belong to INSERT statement, so we can't add WHERE
1102
1023
clause if table is VIEW
1105
if (mysql_prepare_insert(thd, lex->query_tables,
1026
if (mysql_prepare_insert(session, lex->query_tables,
1106
1027
lex->query_tables->table, lex->field_list, 0,
1107
1028
lex->update_list, lex->value_list,
1108
1029
lex->duplicates,
1156
1077
we are fixing fields from insert list.
1158
1079
lex->current_select= &lex->select_lex;
1159
res= check_insert_fields(thd, table_list, *fields, values,
1080
res= check_insert_fields(session, table_list, *fields, values,
1160
1081
!insert_into_view, &map) ||
1161
setup_fields(thd, 0, values, MARK_COLUMNS_READ, 0, 0);
1082
setup_fields(session, 0, values, MARK_COLUMNS_READ, 0, 0);
1163
1084
if (!res && fields->elements)
1165
bool saved_abort_on_warning= thd->abort_on_warning;
1166
thd->abort_on_warning= !info.ignore;
1167
res= check_that_all_fields_are_given_values(thd, table_list->table,
1086
bool saved_abort_on_warning= session->abort_on_warning;
1087
session->abort_on_warning= !info.ignore;
1088
res= check_that_all_fields_are_given_values(session, table_list->table,
1169
thd->abort_on_warning= saved_abort_on_warning;
1090
session->abort_on_warning= saved_abort_on_warning;
1172
1093
if (info.handle_duplicates == DUP_UPDATE && !res)
1181
1102
table_list->next_local= 0;
1182
1103
context->resolve_in_table_list_only(table_list);
1184
res= res || check_update_fields(thd, context->table_list,
1105
res= res || check_update_fields(session, context->table_list,
1185
1106
*info.update_fields, &map);
1187
When we are not using GROUP BY and there are no ungrouped aggregate functions
1108
When we are not using GROUP BY and there are no ungrouped aggregate functions
1188
1109
we can refer to other tables in the ON DUPLICATE KEY part.
1189
1110
We use next_name_resolution_table descructively, so check it first (views?)
1195
1116
We must make a single context out of the two separate name resolution contexts :
1196
1117
the INSERT table and the tables in the SELECT part of INSERT ... SELECT.
1197
1118
To do that we must concatenate the two lists
1199
table_list->next_name_resolution_table=
1120
table_list->next_name_resolution_table=
1200
1121
ctx_state.get_first_name_resolution_table();
1202
res= res || setup_fields(thd, 0, *info.update_values,
1123
res= res || setup_fields(session, 0, *info.update_values,
1203
1124
MARK_COLUMNS_READ, 0, 0);
1236
1157
Is table which we are changing used somewhere in other parts of
1239
if (unique_table(thd, table_list, table_list->next_global, 0))
1160
if (unique_table(session, table_list, table_list->next_global, 0))
1241
1162
/* Using same table for INSERT and SELECT */
1242
1163
lex->current_select->options|= OPTION_BUFFER_RESULT;
1245
1166
else if (!(lex->current_select->options & OPTION_BUFFER_RESULT))
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
1169
We must not yet prepare the result table if it is the same as one of the
1170
source tables (INSERT SELECT). The preparation may disable
1250
1171
indexes on the result table, which may be used during the select, if it
1251
1172
is the same table (Bug #6034). Do the preparation after the select phase
1252
1173
in select_insert::prepare2().
1256
1177
table->file->ha_start_bulk_insert((ha_rows) 0);
1258
restore_record(table,s->default_values); // Get empty record
1179
table->restoreRecordAsDefault(); // Get empty record
1259
1180
table->next_number_field=table->found_next_number_field;
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;
1182
session->cuted_fields=0;
1268
1183
if (info.ignore || info.handle_duplicates != DUP_ERROR)
1269
1184
table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
1270
1185
if (info.handle_duplicates == DUP_REPLACE)
1271
1186
table->file->extra(HA_EXTRA_WRITE_CAN_REPLACE);
1272
1187
if (info.handle_duplicates == DUP_UPDATE)
1273
1188
table->file->extra(HA_EXTRA_INSERT_WITH_UPDATE);
1274
thd->abort_on_warning= !info.ignore;
1189
session->abort_on_warning= !info.ignore;
1275
1190
table->mark_columns_needed_for_insert();
1290
1205
If the result table is the same as one of the source tables (INSERT SELECT),
1291
1206
the result table is not finally prepared at the join prepair phase.
1292
1207
Do the final preparation now.
1298
1213
int select_insert::prepare2(void)
1301
if (thd->lex->current_select->options & OPTION_BUFFER_RESULT)
1216
if (session->lex->current_select->options & OPTION_BUFFER_RESULT)
1302
1217
table->file->ha_start_bulk_insert((ha_rows) 0);
1313
1228
select_insert::~select_insert()
1318
1233
table->next_number_field=0;
1319
1234
table->auto_increment_field_not_null= false;
1320
1235
table->file->ha_reset();
1322
thd->count_cuted_fields= CHECK_FIELD_IGNORE;
1323
thd->abort_on_warning= 0;
1237
session->count_cuted_fields= CHECK_FIELD_IGNORE;
1238
session->abort_on_warning= 0;
1328
1243
bool select_insert::send_data(List<Item> &values)
1333
1248
if (unit->offset_limit_cnt)
1339
thd->count_cuted_fields= CHECK_FIELD_WARN; // Calculate cuted fields
1254
session->count_cuted_fields= CHECK_FIELD_WARN; // Calculate cuted fields
1340
1255
store_values(values);
1341
thd->count_cuted_fields= CHECK_FIELD_IGNORE;
1342
if (thd->is_error())
1256
session->count_cuted_fields= CHECK_FIELD_IGNORE;
1257
if (session->is_error())
1345
error= write_record(thd, table, &info);
1260
// Release latches in case bulk insert takes a long time
1261
ha_release_temporary_latches(session);
1263
error= write_record(session, table, &info);
1349
1267
if (info.handle_duplicates == DUP_UPDATE)
1352
1270
Restore fields of the record since it is possible that they were
1353
1271
changed by ON DUPLICATE KEY UPDATE clause.
1355
1273
If triggers exist then whey can modify some fields which were not
1356
1274
originally touched by INSERT ... SELECT, so we have to restore
1357
1275
their original values for the next row.
1359
restore_record(table, s->default_values);
1277
table->restoreRecordAsDefault();
1361
1279
if (table->next_number_field)
1364
1282
If no value has been autogenerated so far, we need to remember the
1365
1283
value we just saw, we may need to send it to client in the end.
1367
if (thd->first_successful_insert_id_in_cur_stmt == 0) // optimization
1368
autoinc_value_of_last_inserted_row=
1285
if (session->first_successful_insert_id_in_cur_stmt == 0) // optimization
1286
autoinc_value_of_last_inserted_row=
1369
1287
table->next_number_field->val_int();
1371
1289
Clear auto-increment field for the next record, if triggers are used
1381
1299
void select_insert::store_values(List<Item> &values)
1383
1301
if (fields->elements)
1384
fill_record(thd, *fields, values, 1);
1302
fill_record(session, *fields, values, 1);
1386
fill_record(thd, table->field, values, 1);
1304
fill_record(session, table->field, values, 1);
1389
1307
void select_insert::send_error(uint32_t errcode,const char *err)
1393
1311
my_message(errcode, err, MYF(0));
1414
1331
We must invalidate the table in the query cache before binlog writing
1415
1332
and ha_autocommit_or_rollback.
1417
if (thd->transaction.stmt.modified_non_trans_table)
1418
thd->transaction.all.modified_non_trans_table= true;
1334
if (session->transaction.stmt.modified_non_trans_table)
1335
session->transaction.all.modified_non_trans_table= true;
1420
assert(trans_table || !changed ||
1421
thd->transaction.stmt.modified_non_trans_table);
1337
assert(trans_table || !changed ||
1338
session->transaction.stmt.modified_non_trans_table);
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
1340
table->file->ha_release_auto_increment();
1444
1347
char buff[160];
1445
1348
if (info.ignore)
1446
1349
sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
1447
(ulong) (info.records - info.copied), (ulong) thd->cuted_fields);
1350
(ulong) (info.records - info.copied), (ulong) session->cuted_fields);
1449
1352
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) ?
1353
(ulong) (info.deleted+info.updated), (ulong) session->cuted_fields);
1354
session->row_count_func= info.copied + info.deleted +
1355
((session->client_capabilities & CLIENT_FOUND_ROWS) ?
1453
1356
info.touched : info.updated);
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 :
1358
id= (session->first_successful_insert_id_in_cur_stmt > 0) ?
1359
session->first_successful_insert_id_in_cur_stmt :
1360
(session->arg_of_last_insert_id_function ?
1361
session->first_successful_insert_id_in_prev_stmt :
1459
1362
(info.copied ? autoinc_value_of_last_inserted_row : 0));
1460
::my_ok(thd, (ulong) thd->row_count_func, id, buff);
1363
session->my_ok((ulong) session->row_count_func, id, buff);
1464
1367
void select_insert::abort() {
1468
1371
If the creation of the table failed (due to a syntax error, for
1469
1372
example), no table will have been opened and therefore 'table'
1493
1396
changed= (info.copied || info.deleted || info.updated);
1494
1397
transactional_table= table->file->has_transactions();
1495
if (thd->transaction.stmt.modified_non_trans_table)
1497
if (mysql_bin_log.is_open())
1498
thd->binlog_query(THD::ROW_QUERY_TYPE, thd->query, thd->query_length,
1499
transactional_table, false);
1500
if (!thd->current_stmt_binlog_row_based && !can_rollback_data())
1501
thd->transaction.all.modified_non_trans_table= true;
1503
1398
assert(transactional_table || !changed ||
1504
thd->transaction.stmt.modified_non_trans_table);
1399
session->transaction.stmt.modified_non_trans_table);
1505
1400
table->file->ha_release_auto_increment();
1579
1473
if (create_info->options & HA_LEX_CREATE_IF_NOT_EXISTS)
1581
1475
create_info->table_existed= 1; // Mark that table existed
1582
push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1476
push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1583
1477
ER_TABLE_EXISTS_ERROR, ER(ER_TABLE_EXISTS_ERROR),
1584
1478
create_table->table_name);
1585
1479
return(create_table->table);
1592
1486
tmp_table.alias= 0;
1593
1487
tmp_table.timestamp_field= 0;
1594
1488
tmp_table.s= &share;
1595
init_tmp_table_share(thd, &share, "", 0, "", "");
1597
1490
tmp_table.s->db_create_options=0;
1598
1491
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);
1492
tmp_table.s->db_low_byte_first=
1493
test(create_info->db_type == myisam_engine ||
1494
create_info->db_type == heap_engine);
1602
1495
tmp_table.null_row= false;
1603
1496
tmp_table.maybe_null= false;
1613
1506
field= item->tmp_table_field_from_field_type(&tmp_table, 0);
1615
field= create_tmp_field(thd, &tmp_table, item, item->type(),
1508
field= create_tmp_field(session, &tmp_table, item, item->type(),
1616
1509
(Item ***) 0, &tmp_field, &def_field, 0, 0, 0, 0,
1645
tmp_disable_binlog(thd);
1646
if (!mysql_create_table_no_lock(thd, create_table->db,
1538
if (!mysql_create_table_no_lock(session, create_table->db,
1647
1539
create_table->table_name,
1648
1540
create_info, alter_info, 0,
1649
select_field_count))
1541
select_field_count, true))
1651
1543
if (create_info->table_existed &&
1652
1544
!(create_info->options & HA_LEX_CREATE_TMP_TABLE))
1663
1555
if (!(create_info->options & HA_LEX_CREATE_TMP_TABLE))
1665
1557
pthread_mutex_lock(&LOCK_open);
1666
if (reopen_name_locked_table(thd, create_table, false))
1558
if (reopen_name_locked_table(session, create_table, false))
1668
1560
quick_rm_table(create_info->db_type, create_table->db,
1669
1561
table_case_name(create_info, create_table->table_name),
1684
1576
it preparable for open. But let us do close_temporary_table() here
1687
drop_temporary_table(thd, create_table);
1579
drop_temporary_table(session, create_table);
1691
reenable_binlog(thd);
1692
1583
if (!table) // open failed
1696
1587
table->reginfo.lock_type=TL_WRITE;
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))
1588
if (! ((*lock)= mysql_lock_tables(session, &table, 1,
1589
DRIZZLE_LOCK_IGNORE_FLUSH, ¬_used)))
1704
mysql_unlock_tables(thd, *lock);
1593
mysql_unlock_tables(session, *lock);
1708
1597
if (!create_info->table_existed)
1709
drop_open_table(thd, table, create_table->db, create_table->table_name);
1598
drop_open_table(session, table, create_table->db, create_table->table_name);
1738
1624
slave. Hence, we have to hold on to the CREATE part of the
1739
1625
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);
1778
1632
row-based replication for the statement. If we are creating a
1779
1633
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,
1636
if (!(table= create_table_from_items(session, create_info, create_table,
1788
1637
alter_info, &values,
1789
&extra_lock, hook_ptr)))
1790
1639
return(-1); // abort() deletes table
1792
1641
if (extra_lock)
1813
1662
/* Mark all fields that are given values */
1814
1663
for (Field **f= field ; *f ; f++)
1815
bitmap_set_bit(table->write_set, (*f)->field_index);
1664
table->setWriteSet((*f)->field_index);
1817
1666
/* Don't set timestamp if used */
1818
1667
table->timestamp_field_type= TIMESTAMP_NO_AUTO_SET;
1819
1668
table->next_number_field=table->found_next_number_field;
1821
restore_record(table,s->default_values); // Get empty record
1822
thd->cuted_fields=0;
1670
table->restoreRecordAsDefault(); // Get empty record
1671
session->cuted_fields=0;
1823
1672
if (info.ignore || info.handle_duplicates != DUP_ERROR)
1824
1673
table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
1825
1674
if (info.handle_duplicates == DUP_REPLACE)
1827
1676
if (info.handle_duplicates == DUP_UPDATE)
1828
1677
table->file->extra(HA_EXTRA_INSERT_WITH_UPDATE);
1829
1678
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))
1679
session->abort_on_warning= !info.ignore;
1680
if (check_that_all_fields_are_given_values(session, table, table_list))
1833
1682
table->mark_columns_needed_for_insert();
1834
1683
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);
1879
1687
void select_create::store_values(List<Item> &values)
1881
fill_record(thd, field, values, 1);
1689
fill_record(session, field, values, 1);
1885
1693
void select_create::send_error(uint32_t errcode,const char *err)
1890
1698
This will execute any rollbacks that are necessary before writing
1920
1726
if (!table->s->tmp_table)
1922
ha_autocommit_or_rollback(thd, 0);
1923
end_active_trans(thd);
1728
ha_autocommit_or_rollback(session, 0);
1729
(void) session->endActiveTransaction();
1926
1732
table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
1927
1733
table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
1930
mysql_unlock_tables(thd, *m_plock);
1736
mysql_unlock_tables(session, *m_plock);
1931
1737
*m_plock= NULL;
1973
1777
table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
1974
1778
table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
1975
1779
if (!create_info->table_existed)
1976
drop_open_table(thd, table, create_table->db, create_table->table_name);
1780
drop_open_table(session, table, create_table->db, create_table->table_name);
1977
1781
table=0; // Safety