333
Prepare triggers for INSERT-like statement.
336
prepare_triggers_for_insert_stmt()
337
thd The current thread
338
table Table to which insert will happen
339
duplic Type of duplicate handling for insert which will happen
342
Prepare triggers for INSERT-like statement by marking fields
343
used by triggers and inform handlers that batching of UPDATE/DELETE
344
cannot be done if there are BEFORE UPDATE/DELETE triggers.
347
void prepare_triggers_for_insert_stmt(THD *thd, TABLE *table,
348
enum_duplicates duplic)
352
if (table->triggers->has_triggers(TRG_EVENT_DELETE,
356
The table has AFTER DELETE triggers that might access to
357
subject table and therefore might need delete to be done
358
immediately. So we turn-off the batching.
360
(void) table->file->extra(HA_EXTRA_DELETE_CANNOT_BATCH);
362
if (table->triggers->has_triggers(TRG_EVENT_UPDATE,
366
The table has AFTER UPDATE triggers that might access to subject
367
table and therefore might need update to be done immediately.
368
So we turn-off the batching.
370
(void) table->file->extra(HA_EXTRA_UPDATE_CANNOT_BATCH);
372
mark_fields_used_by_triggers_for_insert_stmt(thd, table, duplic);
333
378
Mark fields used by triggers for INSERT-like statement.
470
515
if (res || thd->is_fatal_error)
471
516
DBUG_RETURN(TRUE);
473
thd->proc_info="init";
518
thd_proc_info(thd, "init");
474
519
thd->used_tables=0;
521
value_count= values->elements;
477
523
if (mysql_prepare_insert(thd, table_list, table, fields, values,
478
524
update_fields, update_values, duplic, &unused_conds,
526
(fields.elements || !value_count),
527
!ignore && (thd->variables.sql_mode &
528
(MODE_STRICT_TRANS_TABLES |
529
MODE_STRICT_ALL_TABLES))))
482
532
/* mysql_prepare_insert set table_list->table if it was not set */
523
572
Fill in the given fields and dump it to the table file
525
info.records= info.deleted= info.copied= info.updated= 0;
574
info.records= info.deleted= info.copied= info.updated= info.touched= 0;
526
575
info.ignore= ignore;
527
576
info.handle_duplicates=duplic;
528
577
info.update_fields= &update_fields;
554
thd->proc_info="update";
603
thd_proc_info(thd, "update");
555
604
if (duplic != DUP_ERROR || ignore)
556
605
table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
557
606
if (duplic == DUP_REPLACE)
581
630
table->file->start_bulk_insert(values_list.elements);
583
632
thd->no_trans_update= 0;
584
thd->abort_on_warning= (!ignore &&
585
(thd->variables.sql_mode &
586
(MODE_STRICT_TRANS_TABLES |
587
MODE_STRICT_ALL_TABLES)));
589
if ((fields.elements || !value_count) &&
590
check_that_all_fields_are_given_values(thd, table, table_list))
592
/* thd->net.report_error is now set, which will abort the next loop */
596
mark_fields_used_by_triggers_for_insert_stmt(thd, table, duplic);
633
thd->abort_on_warning= (!ignore && (thd->variables.sql_mode &
634
(MODE_STRICT_TRANS_TABLES |
635
MODE_STRICT_ALL_TABLES)));
637
prepare_triggers_for_insert_stmt(thd, table, duplic);
598
639
if (table_list->prepare_where(thd, 0, TRUE) ||
599
640
table_list->prepare_check_option(thd))
760
thd->proc_info="end";
801
thd_proc_info(thd, "end");
761
802
table->next_number_field=0;
762
803
thd->count_cuted_fields= CHECK_FIELD_IGNORE;
763
804
thd->next_insert_id=0; // Reset this if wrongly used
805
table->auto_increment_field_not_null= FALSE;
764
806
if (duplic != DUP_ERROR || ignore)
765
807
table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
766
808
if (duplic == DUP_REPLACE &&
767
809
(!table->triggers || !table->triggers->has_delete_triggers()))
768
810
table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
770
/* Reset value of LAST_INSERT_ID if no rows where inserted */
771
if (!info.copied && thd->insert_id_used)
812
/* Reset value of LAST_INSERT_ID if no rows were inserted or touched */
813
if (!info.copied && !info.touched && thd->insert_id_used)
773
815
thd->insert_id(0);
963
1005
be taken from table_list->table)
964
1006
where Where clause (for insert ... select)
965
1007
select_insert TRUE if INSERT ... SELECT statement
1008
check_fields TRUE if need to check that all INSERT fields are
1010
abort_on_warning whether to report if some INSERT field is not
1011
assigned as an error (TRUE) or as a warning (FALSE).
967
1013
TODO (in far future)
983
1029
TABLE *table, List<Item> &fields, List_item *values,
984
1030
List<Item> &update_fields, List<Item> &update_values,
985
1031
enum_duplicates duplic,
986
COND **where, bool select_insert)
1032
COND **where, bool select_insert,
1033
bool check_fields, bool abort_on_warning)
988
1035
SELECT_LEX *select_lex= &thd->lex->select_lex;
989
1036
Name_resolution_context *context= &select_lex->context;
1046
1093
table_list->next_local= 0;
1047
1094
context->resolve_in_table_list_only(table_list);
1049
if (!(res= check_insert_fields(thd, context->table_list, fields, *values,
1050
!insert_into_view, &map) ||
1051
setup_fields(thd, 0, *values, 0, 0, 0))
1052
&& duplic == DUP_UPDATE)
1096
res= check_insert_fields(thd, context->table_list, fields, *values,
1097
!insert_into_view, &map) ||
1098
setup_fields(thd, 0, *values, 0, 0, 0);
1100
if (!res && check_fields)
1102
bool saved_abort_on_warning= thd->abort_on_warning;
1103
thd->abort_on_warning= abort_on_warning;
1104
res= check_that_all_fields_are_given_values(thd,
1106
context->table_list->table,
1107
context->table_list);
1108
thd->abort_on_warning= saved_abort_on_warning;
1111
if (!res && duplic == DUP_UPDATE)
1054
1113
select_lex->no_wrap_view_item= TRUE;
1055
1114
res= check_update_fields(thd, context->table_list, update_fields, &map);
1284
if (table->next_number_field)
1285
table->file->adjust_next_insert_id_after_explicit_value(
1286
table->next_number_field->val_int());
1224
1289
if ((table->file->table_flags() & HA_PARTIAL_COLUMN_READ) ||
1225
1290
compare_record(table, thd->query_id))
1227
1292
info->updated++;
1229
if (table->next_number_field)
1230
table->file->adjust_next_insert_id_after_explicit_value(
1231
table->next_number_field->val_int());
1233
1293
trg_error= (table->triggers &&
1234
1294
table->triggers->process_triggers(thd, TRG_EVENT_UPDATE,
1235
1295
TRG_ACTION_AFTER,
1237
1297
info->copied++;
1239
1300
goto ok_or_after_trg_err;
1241
1302
else /* DUP_REPLACE */
1493
1554
delayed_insert *find_handler(THD *thd, TABLE_LIST *table_list)
1495
thd->proc_info="waiting for delay_list";
1556
thd_proc_info(thd, "waiting for delay_list");
1496
1557
pthread_mutex_lock(&LOCK_delayed_insert); // Protect master list
1497
1558
I_List_iterator<delayed_insert> it(delayed_threads);
1498
1559
delayed_insert *tmp;
1530
1591
if (delayed_insert_threads >= thd->variables.max_insert_delayed_threads)
1531
1592
DBUG_RETURN(0);
1532
thd->proc_info="Creating delayed handler";
1593
thd_proc_info(thd, "Creating delayed handler");
1533
1594
pthread_mutex_lock(&LOCK_delayed_create);
1535
1596
The first search above was done without LOCK_delayed_create.
1573
1634
/* Wait until table is open */
1574
thd->proc_info="waiting for handler open";
1635
thd_proc_info(thd, "waiting for handler open");
1575
1636
while (!tmp->thd.killed && !tmp->table && !thd->killed)
1577
1638
pthread_cond_wait(&tmp->cond_client,&tmp->mutex);
1579
1640
pthread_mutex_unlock(&tmp->mutex);
1580
thd->proc_info="got old table";
1641
thd_proc_info(thd, "got old table");
1581
1642
if (tmp->thd.killed)
1583
1644
if (tmp->thd.is_fatal_error)
1637
1698
tables_in_use++;
1638
1699
if (!thd.lock) // Table is not locked
1640
client_thd->proc_info="waiting for handler lock";
1701
thd_proc_info(client_thd, "waiting for handler lock");
1641
1702
pthread_cond_signal(&cond); // Tell handler to lock table
1642
1703
while (!dead && !thd.lock && ! client_thd->killed)
1644
1705
pthread_cond_wait(&cond_client,&mutex);
1646
client_thd->proc_info="got handler lock";
1707
thd_proc_info(client_thd, "got handler lock");
1647
1708
if (client_thd->killed)
1661
1722
bytes. Since the table copy is used for creating one record only,
1662
1723
the other record buffers and alignment are unnecessary.
1664
client_thd->proc_info="allocating local table";
1725
thd_proc_info(client_thd, "allocating local table");
1665
1726
copy= (TABLE*) client_thd->alloc(sizeof(*copy)+
1666
1727
(table->s->fields+1)*sizeof(Field**)+
1667
1728
table->s->reclength);
1741
1802
delayed_insert *di=thd->di;
1742
1803
DBUG_ENTER("write_delayed");
1744
thd->proc_info="waiting for handler insert";
1805
thd_proc_info(thd, "waiting for handler insert");
1745
1806
pthread_mutex_lock(&di->mutex);
1746
1807
while (di->stacked_inserts >= delayed_queue_size && !thd->killed)
1747
1808
pthread_cond_wait(&di->cond_client,&di->mutex);
1748
thd->proc_info="storing row into queue";
1809
thd_proc_info(thd, "storing row into queue");
1750
1811
if (thd->killed || !(row= new delayed_row(duplic, ignore, log_on)))
1954
2015
/* Information for pthread_kill */
1955
2016
di->thd.mysys_var->current_mutex= &di->mutex;
1956
2017
di->thd.mysys_var->current_cond= &di->cond;
1957
di->thd.proc_info="Waiting for INSERT";
2018
thd_proc_info(&(di->thd), "Waiting for INSERT");
1959
2020
DBUG_PRINT("info",("Waiting for someone to insert rows"));
1960
2021
while (!thd->killed)
1989
2050
pthread_mutex_unlock(&di->thd.mysys_var->mutex);
1990
2051
pthread_mutex_lock(&di->mutex);
1992
di->thd.proc_info=0;
2053
thd_proc_info(&(di->thd), 0);
1994
2055
if (di->tables_in_use && ! thd->lock)
2109
2170
table->next_number_field=table->found_next_number_field;
2111
thd.proc_info="upgrading lock";
2172
thd_proc_info(&thd, "upgrading lock");
2112
2173
if (thr_upgrade_write_delay_lock(*thd.lock->locks))
2114
2175
/* This can only happen if thread is killed by shutdown */
2235
2296
if (tables_in_use)
2236
2297
pthread_cond_broadcast(&cond_client); // If waiting clients
2237
thd.proc_info="reschedule";
2298
thd_proc_info(&thd, "reschedule");
2238
2299
pthread_mutex_unlock(&mutex);
2239
2300
if ((error=table->file->extra(HA_EXTRA_NO_CACHE)))
2253
2314
if (!using_bin_log)
2254
2315
table->file->extra(HA_EXTRA_WRITE_CACHE);
2255
2316
pthread_mutex_lock(&mutex);
2256
thd.proc_info="insert";
2317
thd_proc_info(&thd, "insert");
2258
2319
if (tables_in_use)
2259
2320
pthread_cond_broadcast(&cond_client); // If waiting clients
2324
thd_proc_info(&thd, 0);
2264
2325
table->next_number_field=0;
2265
2326
pthread_mutex_unlock(&mutex);
2266
2327
if ((error=table->file->extra(HA_EXTRA_NO_CACHE)))
2324
2385
lex->query_tables->table, lex->field_list, 0,
2325
2386
lex->update_list, lex->value_list,
2326
2387
lex->duplicates,
2327
&select_lex->where, TRUE))
2388
&select_lex->where, TRUE, FALSE, FALSE))
2328
2389
DBUG_RETURN(TRUE);
2386
2447
!insert_into_view, &map) ||
2387
2448
setup_fields(thd, 0, values, 0, 0, 0);
2389
if (info.handle_duplicates == DUP_UPDATE)
2450
if (!res && fields->elements)
2452
bool saved_abort_on_warning= thd->abort_on_warning;
2453
thd->abort_on_warning= !info.ignore && (thd->variables.sql_mode &
2454
(MODE_STRICT_TRANS_TABLES |
2455
MODE_STRICT_ALL_TABLES));
2456
res= check_that_all_fields_are_given_values(thd, table_list->table,
2458
thd->abort_on_warning= saved_abort_on_warning;
2461
if (info.handle_duplicates == DUP_UPDATE && !res)
2391
2463
Name_resolution_context *context= &lex->select_lex.context;
2392
2464
Name_resolution_context_state ctx_state;
2497
2569
(thd->variables.sql_mode &
2498
2570
(MODE_STRICT_TRANS_TABLES |
2499
2571
MODE_STRICT_ALL_TABLES)));
2500
res= ((fields->elements &&
2501
check_that_all_fields_are_given_values(thd, table, table_list)) ||
2502
table_list->prepare_where(thd, 0, TRUE) ||
2572
res= (table_list->prepare_where(thd, 0, TRUE) ||
2503
2573
table_list->prepare_check_option(thd));
2506
mark_fields_used_by_triggers_for_insert_stmt(thd, table,
2507
info.handle_duplicates);
2576
prepare_triggers_for_insert_stmt(thd, table,
2577
info.handle_duplicates);
2508
2578
DBUG_RETURN(res);