18
18
Single table and multi table updates of tables.
19
19
Multi-table updates were introduced by Sinisa & Monty
21
#include <drizzled/server_includes.h>
22
#include <drizzled/sql_select.h>
23
#include <drizzled/drizzled_error_messages.h>
22
#include "mysql_priv.h"
23
#include "sql_select.h"
25
/* Return 0 if row hasn't changed */
27
bool compare_record(TABLE *table)
29
if (table->s->blob_fields + table->s->varchar_fields == 0)
30
return cmp_record(table,record[1]);
31
/* Compare null bits */
32
if (memcmp(table->null_flags,
33
table->null_flags+table->s->rec_buff_length,
34
table->s->null_bytes))
35
return TRUE; // Diff in NULL value
36
/* Compare updated fields */
37
for (Field **ptr= table->field ; *ptr ; ptr++)
39
if (bitmap_is_set(table->write_set, (*ptr)->field_index) &&
40
(*ptr)->cmp_binary_offset(table->s->rec_buff_length))
26
48
check that all fields are real fields
48
70
/* item has name, because it comes from VIEW SELECT list */
49
71
my_error(ER_NONUPDATEABLE_COLUMN, MYF(0), item->name);
53
75
we make temporary copy of Item_field, to avoid influence of changing
71
93
@param[in] table table
74
static void prepare_record_for_error_message(int error, Table *table)
96
static void prepare_record_for_error_message(int error, TABLE *table)
79
101
MY_BITMAP unique_map; /* Fields in offended unique. */
80
102
my_bitmap_map unique_map_buf[bitmap_buffer_size(MAX_FIELDS)];
103
DBUG_ENTER("prepare_record_for_error_message");
83
106
Only duplicate key errors print the key value.
84
107
If storage engine does always read all columns, we have the value alraedy.
86
109
if ((error != HA_ERR_FOUND_DUPP_KEY) ||
87
110
!(table->file->ha_table_flags() & HA_PARTIAL_COLUMN_READ))
91
114
Get the number of the offended index.
92
115
We will see MAX_KEY if the engine cannot determine the affected index.
94
117
if ((keynr= table->file->get_dup_key(error)) >= MAX_KEY)
97
120
/* Create unique_map with all fields used by that index. */
98
bitmap_init(&unique_map, unique_map_buf, table->s->fields, false);
121
bitmap_init(&unique_map, unique_map_buf, table->s->fields, FALSE);
99
122
table->mark_columns_used_by_index_no_reset(keynr, &unique_map);
101
124
/* Subtract read_set and write_set. */
136
159
fields fields for update
137
160
values values of fields for update
138
161
conds WHERE clause expression
139
order_num number of elemen in order_st BY clause
140
order order_st BY clause list
162
order_num number of elemen in ORDER BY clause
163
order ORDER BY clause list
141
164
limit limit clause
142
165
handle_duplicates how to handle duplicates
151
174
int mysql_update(THD *thd,
152
TableList *table_list,
175
TABLE_LIST *table_list,
153
176
List<Item> &fields,
156
uint32_t order_num, order_st *order,
158
enum enum_duplicates handle_duplicates __attribute__((unused)),
179
uint order_num, ORDER *order,
181
enum enum_duplicates handle_duplicates, bool ignore)
161
183
bool using_limit= limit != HA_POS_ERROR;
162
184
bool safe_update= test(thd->options & OPTION_SAFE_UPDATES);
164
186
bool can_compare_record;
165
187
int error, loc_error;
166
188
uint used_index= MAX_KEY, dup_key_found;
167
bool need_sort= true;
168
uint32_t table_count= 0;
189
bool need_sort= TRUE;
169
191
ha_rows updated, found;
170
192
key_map old_covering_keys;
172
194
SQL_SELECT *select;
173
195
READ_RECORD info;
174
196
SELECT_LEX *select_lex= &thd->lex->select_lex;
175
197
bool need_reopen;
177
199
List<Item> all_fields;
178
200
THD::killed_state killed_status= THD::NOT_KILLED;
201
DBUG_ENTER("mysql_update");
182
205
if (open_tables(thd, &table_list, &table_count, 0))
185
208
if (!lock_tables(thd, table_list, table_count, &need_reopen))
187
210
if (!need_reopen)
189
212
close_tables_for_reopen(thd, &table_list);
192
215
if (mysql_handle_derived(thd->lex, &mysql_derived_prepare) ||
193
216
(thd->fill_derived_tables() &&
194
217
mysql_handle_derived(thd->lex, &mysql_derived_filling)))
197
DRIZZLE_UPDATE_START();
198
thd->set_proc_info("init");
220
MYSQL_UPDATE_START();
221
thd_proc_info(thd, "init");
199
222
table= table_list->table;
201
224
/* Calculate "table->covering_keys" based on the WHERE */
269
292
free_underlaid_joins(thd, select_lex);
271
294
goto abort; // Error in where
272
DRIZZLE_UPDATE_END();
273
296
my_ok(thd); // No matching records
276
299
if (!select && limit != HA_POS_ERROR)
278
301
if ((used_index= get_index_for_order(table, order, limit)) != MAX_KEY)
281
304
/* If running in safe sql mode, don't allow updates without keys */
282
305
if (table->quick_keys.is_clear_all())
330
353
if (order && (need_sort || used_key_is_modified))
333
Doing an order_st BY; Let filesort find and sort the rows we are going
356
Doing an ORDER BY; Let filesort find and sort the rows we are going
335
358
NOTE: filesort will call table->prepare_for_position()
338
361
SORT_FIELD *sortorder;
339
362
ha_rows examined_rows;
390
413
init_read_record_idx(&info, thd, table, 1, used_index);
392
thd->set_proc_info("Searching rows for update");
415
thd_proc_info(thd, "Searching rows for update");
393
416
ha_rows tmp_limit= limit;
395
418
while (!(error=info.read_record(&info)) && !thd->killed)
458
481
thd->count_cuted_fields= ignore ? CHECK_FIELD_WARN
459
482
: CHECK_FIELD_ERROR_FOR_NULL;
460
483
thd->cuted_fields=0L;
461
thd->set_proc_info("Updating");
484
thd_proc_info(thd, "Updating");
463
486
transactional_table= table->file->has_transactions();
464
thd->abort_on_warning= test(!ignore);
487
thd->abort_on_warning= test(!ignore &&
488
(thd->variables.sql_mode &
489
(MODE_STRICT_TRANS_TABLES |
490
MODE_STRICT_ALL_TABLES)));
465
491
will_batch= !table->file->start_bulk_update();
614
640
The cached value can not change whereas the killed status can
615
641
(externally) since this point and change of the latter won't affect
617
It's assumed that if an error was set in combination with an effective
643
It's assumed that if an error was set in combination with an effective
618
644
killed status then the error is due to killing.
620
killed_status= thd->killed; // get the status of the volatile
646
killed_status= thd->killed; // get the status of the volatile
621
647
// simulated killing after the loop must be ineffective for binlogging
648
DBUG_EXECUTE_IF("simulate_kill_bug27571",
650
thd->killed= THD::KILL_QUERY;
622
652
error= (killed_status == THD::NOT_KILLED)? error : 1;
626
656
(loc_error= table->file->exec_bulk_update(&dup_key_found)))
646
676
table->file->try_semi_consistent_read(0);
648
678
if (!transactional_table && updated > 0)
649
thd->transaction.stmt.modified_non_trans_table= true;
679
thd->transaction.stmt.modified_non_trans_table= TRUE;
651
681
end_read_record(&info);
653
thd->set_proc_info("end");
654
table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
683
thd_proc_info(thd, "end");
684
VOID(table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY));
657
687
error < 0 means really no error at all: we processed all rows until the
658
688
last one without error. error > 0 means an error (e.g. unique key
659
689
violation and no IGNORE or REPLACE). error == 0 is also an error (if
660
690
preparing the record or invoking before triggers fails). See
661
ha_autocommit_or_rollback(error>=0) and return(error>=0) below.
691
ha_autocommit_or_rollback(error>=0) and DBUG_RETURN(error>=0) below.
662
692
Sometimes we want to binlog even if we updated no rows, in case user used
663
693
it to be sure master and slave are in same state.
670
700
thd->clear_error();
671
701
if (thd->binlog_query(THD::ROW_QUERY_TYPE,
672
702
thd->query, thd->query_length,
673
transactional_table, false, killed_status) &&
703
transactional_table, FALSE, killed_status) &&
674
704
transactional_table)
676
706
error=1; // Rollback update
679
709
if (thd->transaction.stmt.modified_non_trans_table)
680
thd->transaction.all.modified_non_trans_table= true;
710
thd->transaction.all.modified_non_trans_table= TRUE;
682
assert(transactional_table || !updated || thd->transaction.stmt.modified_non_trans_table);
712
DBUG_ASSERT(transactional_table || !updated || thd->transaction.stmt.modified_non_trans_table);
683
713
free_underlaid_joins(thd, select_lex);
685
715
/* If LAST_INSERT_ID(X) was used, report X */
686
716
id= thd->arg_of_last_insert_id_function ?
687
717
thd->first_successful_insert_id_in_prev_stmt : 0;
689
DRIZZLE_UPDATE_END();
692
722
char buff[STRING_BUFFER_USUAL_SIZE];
695
725
thd->row_count_func=
696
726
(thd->client_capabilities & CLIENT_FOUND_ROWS) ? found : updated;
697
727
my_ok(thd, (ulong) thd->row_count_func, id, buff);
728
DBUG_PRINT("info",("%ld records updated", (long) updated));
699
730
thd->count_cuted_fields= CHECK_FIELD_IGNORE; /* calc cuted fields */
700
731
thd->abort_on_warning= 0;
701
return((error >= 0 || thd->is_error()) ? 1 : 0);
732
DBUG_RETURN((error >= 0 || thd->is_error()) ? 1 : 0);
723
754
thd - thread handler
724
755
table_list - global/local table list
725
756
conds - conditions
726
order_num - number of order_st BY list entries
727
order - order_st BY clause list
757
order_num - number of ORDER BY list entries
758
order - ORDER BY clause list
733
bool mysql_prepare_update(THD *thd, TableList *table_list,
734
Item **conds, uint32_t order_num, order_st *order)
764
bool mysql_prepare_update(THD *thd, TABLE_LIST *table_list,
765
Item **conds, uint order_num, ORDER *order)
736
768
List<Item> all_fields;
737
769
SELECT_LEX *select_lex= &thd->lex->select_lex;
770
DBUG_ENTER("mysql_prepare_update");
740
773
Statement-based replication of UPDATE ... LIMIT is not safe as order of
741
774
rows is not defined, so in mixed mode we go to row-based.
743
Note that we may consider a statement as safe if order_st BY primary_key
776
Note that we may consider a statement as safe if ORDER BY primary_key
744
777
is present. However it may confuse users to see very similiar statements
745
778
replicated differently.
761
794
select_lex->setup_ref_array(thd, order_num) ||
762
795
setup_order(thd, select_lex->ref_pointer_array,
763
796
table_list, all_fields, all_fields, order))
766
799
/* Check that we are not using table that we are updating in a sub select */
768
TableList *duplicate;
801
TABLE_LIST *duplicate;
769
802
if ((duplicate= unique_table(thd, table_list, table_list->next_global, 0)))
771
804
update_non_unique_table_error(table_list, "UPDATE", duplicate);
772
805
my_error(ER_UPDATE_TABLE_USED, MYF(0), table_list->table_name);
809
select_lex->fix_prepare_information(thd, conds, &fake_conds);
806
840
thd thread handler
813
847
int mysql_multi_update_prepare(THD *thd)
815
849
LEX *lex= thd->lex;
816
TableList *table_list= lex->query_tables;
817
TableList *tl, *leaves;
850
TABLE_LIST *table_list= lex->query_tables;
851
TABLE_LIST *tl, *leaves;
818
852
List<Item> *fields= &lex->select_lex.item_list;
819
853
table_map tables_for_update;
820
854
bool update_view= 0;
823
857
counter else junk will be assigned here, but then replaced with real
824
858
count in open_tables()
826
uint32_t table_count= lex->table_count;
860
uint table_count= lex->table_count;
827
861
const bool using_lock_tables= thd->locked_tables != 0;
828
862
bool original_multiupdate= (thd->lex->sql_command == SQLCOM_UPDATE_MULTI);
829
bool need_reopen= false;
863
bool need_reopen= FALSE;
864
DBUG_ENTER("mysql_multi_update_prepare");
832
866
/* following need for prepared statements, to run next time multi-update */
833
867
thd->lex->sql_command= SQLCOM_UPDATE_MULTI;
838
872
if (((original_multiupdate || need_reopen) &&
839
873
open_tables(thd, &table_list, &table_count, 0)) ||
840
874
mysql_handle_derived(lex, &mysql_derived_prepare))
843
877
setup_tables() need for VIEWs. JOIN::prepare() will call setup_tables()
844
878
second time, but this call will do nothing (there are check for second
849
883
&lex->select_lex.top_join_list,
851
885
&lex->select_lex.leaf_tables, false))
854
888
if (setup_fields_with_no_wrap(thd, 0, *fields, MARK_COLUMNS_WRITE, 0, 0))
857
891
if (update_view && check_fields(thd, *fields))
862
896
tables_for_update= get_table_map(fields);
867
901
leaves= lex->select_lex.leaf_tables;
868
902
for (tl= leaves; tl; tl= tl->next_leaf)
870
Table *table= tl->table;
904
TABLE *table= tl->table;
871
905
/* Only set timestamp column if this is not modified */
872
906
if (table->timestamp_field &&
873
907
bitmap_is_set(table->write_set,
878
912
if (table->map & tables_for_update)
880
914
table->mark_columns_needed_for_update();
915
DBUG_PRINT("info",("setting table `%s` for update", tl->alias));
882
917
If table will be updated we should not downgrade lock for it and
923
DBUG_PRINT("info",("setting table `%s` for read-only", tl->alias));
889
925
If we are using the binary log, we need TL_READ_NO_INSERT to get
890
926
correct order of statements. Otherwise, we use a TL_READ lock to
893
929
tl->lock_type= using_update_log ? TL_READ_NO_INSERT : TL_READ;
895
/* Update Table::lock_type accordingly. */
931
/* Update TABLE::lock_type accordingly. */
896
932
if (!tl->placeholder() && !using_lock_tables)
897
933
tl->table->reginfo.lock_type= tl->lock_type;
918
954
/* We have to cleanup translation tables of views. */
919
for (TableList *tbl= table_list; tbl; tbl= tbl->next_global)
955
for (TABLE_LIST *tbl= table_list; tbl; tbl= tbl->next_global)
920
956
tbl->cleanup_items();
922
958
close_tables_for_reopen(thd, &table_list);
927
963
Check that we are not using table that we are updating, but we should
928
964
skip all tables of UPDATE SELECT itself
930
lex->select_lex.exclude_from_table_unique_test= true;
966
lex->select_lex.exclude_from_table_unique_test= TRUE;
931
967
/* We only need SELECT privilege for columns in the values list */
932
968
for (tl= leaves; tl; tl= tl->next_leaf)
934
970
if (tl->lock_type != TL_READ &&
935
971
tl->lock_type != TL_READ_NO_INSERT)
937
TableList *duplicate;
973
TABLE_LIST *duplicate;
938
974
if ((duplicate= unique_table(thd, tl, table_list, 0)))
940
976
update_non_unique_table_error(table_list, "UPDATE", duplicate);
946
Set exclude_from_table_unique_test value back to false. It is needed for
982
Set exclude_from_table_unique_test value back to FALSE. It is needed for
947
983
further check in multi_update::prepare whether to use record cache.
949
lex->select_lex.exclude_from_table_unique_test= false;
985
lex->select_lex.exclude_from_table_unique_test= FALSE;
951
987
if (thd->fill_derived_tables() &&
952
988
mysql_handle_derived(lex, &mysql_derived_filling))
963
999
bool mysql_multi_update(THD *thd,
964
TableList *table_list,
1000
TABLE_LIST *table_list,
965
1001
List<Item> *fields,
966
1002
List<Item> *values,
969
1005
enum enum_duplicates handle_duplicates, bool ignore,
970
1006
SELECT_LEX_UNIT *unit, SELECT_LEX *select_lex)
972
1008
multi_update *result;
1010
DBUG_ENTER("mysql_multi_update");
975
1012
if (!(result= new multi_update(table_list,
976
1013
thd->lex->select_lex.leaf_tables,
978
1015
handle_duplicates, ignore)))
981
thd->abort_on_warning= true;
1018
thd->abort_on_warning= test(thd->variables.sql_mode &
1019
(MODE_STRICT_TRANS_TABLES |
1020
MODE_STRICT_ALL_TABLES));
983
1022
List<Item> total_list;
984
1023
res= mysql_select(thd, &select_lex->ref_pointer_array,
985
1024
table_list, select_lex->with_wild,
987
conds, 0, (order_st *) NULL, (order_st *)NULL, (Item *) NULL,
1026
conds, 0, (ORDER *) NULL, (ORDER *)NULL, (Item *) NULL,
989
1028
options | SELECT_NO_JOIN_CACHE | SELECT_NO_UNLOCK |
990
1029
OPTION_SETUP_TABLES_DONE,
991
1030
result, unit, select_lex);
1031
DBUG_PRINT("info",("res: %d report_error: %d", res,
1032
(int) thd->is_error()));
992
1033
res|= thd->is_error();
993
1034
if (unlikely(res))
1000
1041
thd->abort_on_warning= 0;
1005
multi_update::multi_update(TableList *table_list,
1006
TableList *leaves_list,
1046
multi_update::multi_update(TABLE_LIST *table_list,
1047
TABLE_LIST *leaves_list,
1007
1048
List<Item> *field_list, List<Item> *value_list,
1008
1049
enum enum_duplicates handle_duplicates_arg,
1009
1050
bool ignore_arg)
1019
1060
Connect fields with tables and create list of tables that are updated
1022
int multi_update::prepare(List<Item> ¬_used_values __attribute__((unused)),
1023
SELECT_LEX_UNIT *lex_unit __attribute__((unused)))
1063
int multi_update::prepare(List<Item> ¬_used_values,
1064
SELECT_LEX_UNIT *lex_unit)
1025
TableList *table_ref;
1066
TABLE_LIST *table_ref;
1026
1067
SQL_LIST update;
1027
1068
table_map tables_to_update;
1028
1069
Item_field *item;
1029
1070
List_iterator_fast<Item> field_it(*fields);
1030
1071
List_iterator_fast<Item> value_it(*values);
1031
uint32_t i, max_fields;
1032
uint32_t leaf_table_count= 0;
1073
uint leaf_table_count= 0;
1074
DBUG_ENTER("multi_update::prepare");
1034
1076
thd->count_cuted_fields= CHECK_FIELD_WARN;
1035
1077
thd->cuted_fields=0L;
1036
thd->set_proc_info("updating main table");
1078
thd_proc_info(thd, "updating main table");
1038
1080
tables_to_update= get_table_map(fields);
1040
1082
if (!tables_to_update)
1042
1084
my_message(ER_NO_TABLES_USED, ER(ER_NO_TABLES_USED), MYF(0));
1051
1093
if (setup_fields(thd, 0, *values, MARK_COLUMNS_READ, 0, 0))
1055
1097
Save tables beeing updated in update_tables
1061
1103
for (table_ref= leaves; table_ref; table_ref= table_ref->next_leaf)
1063
1105
/* TODO: add support of view of join support */
1064
Table *table=table_ref->table;
1106
TABLE *table=table_ref->table;
1065
1107
leaf_table_count++;
1066
1108
if (tables_to_update & table->map)
1068
TableList *tl= (TableList*) thd->memdup((char*) table_ref,
1110
TABLE_LIST *tl= (TABLE_LIST*) thd->memdup((char*) table_ref,
1072
update.link_in_list((unsigned char*) tl, (unsigned char**) &tl->next_local);
1114
update.link_in_list((uchar*) tl, (uchar**) &tl->next_local);
1073
1115
tl->shared= table_count++;
1074
1116
table->no_keyread=1;
1075
1117
table->covering_keys.clear_all();
1081
1123
table_count= update.elements;
1082
update_tables= (TableList*) update.first;
1124
update_tables= (TABLE_LIST*) update.first;
1084
tmp_tables = (Table**) thd->calloc(sizeof(Table *) * table_count);
1126
tmp_tables = (TABLE**) thd->calloc(sizeof(TABLE *) * table_count);
1085
1127
tmp_table_param = (TMP_TABLE_PARAM*) thd->calloc(sizeof(TMP_TABLE_PARAM) *
1087
1129
fields_for_table= (List_item **) thd->alloc(sizeof(List_item *) *
1089
1131
values_for_table= (List_item **) thd->alloc(sizeof(List_item *) *
1091
1133
if (thd->is_fatal_error)
1093
1135
for (i=0 ; i < table_count ; i++)
1095
1137
fields_for_table[i]= new List_item;
1096
1138
values_for_table[i]= new List_item;
1098
1140
if (thd->is_fatal_error)
1101
1143
/* Split fields into fields_for_table[] and values_by_table[] */
1103
1145
while ((item= (Item_field *) field_it++))
1105
1147
Item *value= value_it++;
1106
uint32_t offset= item->field->table->pos_in_table_list->shared;
1148
uint offset= item->field->table->pos_in_table_list->shared;
1107
1149
fields_for_table[offset]->push_back(item);
1108
1150
values_for_table[offset]->push_back(value);
1110
1152
if (thd->is_fatal_error)
1113
1155
/* Allocate copy fields */
1115
1157
for (i=0 ; i < table_count ; i++)
1116
1158
set_if_bigger(max_fields, fields_for_table[i]->elements + leaf_table_count);
1117
1159
copy_field= new Copy_field[max_fields];
1118
return(thd->is_fatal_error != 0);
1160
DBUG_RETURN(thd->is_fatal_error != 0);
1142
1184
- Table is not joined to itself.
1144
1186
This function gets information about fields to be updated from
1145
the Table::write_set bitmap.
1187
the TABLE::write_set bitmap.
1148
1190
This code is a bit dependent of how make_join_readinfo() works.
1155
1197
static bool safe_update_on_fly(THD *thd, JOIN_TAB *join_tab,
1156
TableList *table_ref, TableList *all_tables)
1198
TABLE_LIST *table_ref, TABLE_LIST *all_tables)
1158
Table *table= join_tab->table;
1200
TABLE *table= join_tab->table;
1159
1201
if (unique_table(thd, table_ref, all_tables, 0))
1161
1203
switch (join_tab->type) {
1162
1204
case JT_SYSTEM:
1164
1206
case JT_EQ_REF:
1165
return true; // At most one matching row
1207
return TRUE; // At most one matching row
1167
1209
case JT_REF_OR_NULL:
1168
1210
return !is_key_used(table, join_tab->ref.key, table->write_set);
1174
1216
if ((table->file->ha_table_flags() & HA_PRIMARY_KEY_IN_READ_INDEX) &&
1175
1217
table->s->primary_key < MAX_KEY)
1176
1218
return !is_key_used(table, table->s->primary_key, table->write_set);
1179
1221
break; // Avoid compler warning
1196
1238
multi_update::initialize_tables(JOIN *join)
1198
TableList *table_ref;
1240
TABLE_LIST *table_ref;
1241
DBUG_ENTER("initialize_tables");
1200
1243
if ((thd->options & OPTION_SAFE_UPDATES) && error_if_full_join(join))
1202
1245
main_table=join->join_tab->table;
1203
1246
table_to_update= 0;
1205
1248
/* Any update has at least one pair (field, value) */
1206
assert(fields->elements);
1249
DBUG_ASSERT(fields->elements);
1208
1251
/* Create a temporary table for keys to all tables, except main table */
1209
1252
for (table_ref= update_tables; table_ref; table_ref= table_ref->next_local)
1211
Table *table=table_ref->table;
1212
uint32_t cnt= table_ref->shared;
1254
TABLE *table=table_ref->table;
1255
uint cnt= table_ref->shared;
1213
1256
List<Item> temp_fields;
1215
1258
TMP_TABLE_PARAM *tmp_param;
1217
1260
table->mark_columns_needed_for_update();
1237
1280
OPTION condition.
1240
List_iterator_fast<Table> tbl_it(unupdated_check_opt_tables);
1283
List_iterator_fast<TABLE> tbl_it(unupdated_check_opt_tables);
1244
1287
Field_string *field= new Field_string(tbl->file->ref_length, 0,
1245
1288
tbl->alias, &my_charset_bin);
1247
Field_varstring *field= new Field_varstring(tbl->file->ref_length, 0,
1248
tbl->alias, tbl->s, &my_charset_bin);
1252
1291
field->init(tbl);
1254
1293
The field will be converted to varstring when creating tmp table if
1255
1294
table to be updated was created by mysql 4.1. Deny this.
1296
field->can_alter_field_type= 0;
1257
1297
Item_field *ifield= new Item_field((Field *) field);
1260
1300
ifield->maybe_null= 0;
1261
1301
if (temp_fields.push_back(ifield))
1263
1303
} while ((tbl= tbl_it++));
1265
1305
temp_fields.concat(fields_for_table[cnt]);
1267
1307
/* Make an unique key over the first field to avoid duplicated updates */
1268
memset(&group, 0, sizeof(group));
1308
bzero((char*) &group, sizeof(group));
1270
1310
group.item= (Item**) temp_fields.head_ref();
1276
1316
if (!(tmp_tables[cnt]=create_tmp_table(thd,
1279
(order_st*) &group, 0, 0,
1319
(ORDER*) &group, 0, 0,
1280
1320
TMP_TABLE_ALL_COLUMNS,
1284
1324
tmp_tables[cnt]->file->extra(HA_EXTRA_WRITE_CACHE);
1290
1330
multi_update::~multi_update()
1293
1333
for (table= update_tables ; table; table= table->next_local)
1295
1335
table->table->no_keyread= table->table->no_cache= 0;
1300
1340
if (tmp_tables)
1302
for (uint32_t cnt = 0; cnt < table_count; cnt++)
1342
for (uint cnt = 0; cnt < table_count; cnt++)
1304
1344
if (tmp_tables[cnt])
1306
tmp_tables[cnt]->free_tmp_table(thd);
1346
free_tmp_table(thd, tmp_tables[cnt]);
1307
1347
tmp_table_param[cnt].cleanup();
1311
1351
if (copy_field)
1312
1352
delete [] copy_field;
1313
1353
thd->count_cuted_fields= CHECK_FIELD_IGNORE; // Restore this setting
1314
assert(trans_safe || !updated ||
1354
DBUG_ASSERT(trans_safe || !updated ||
1315
1355
thd->transaction.all.modified_non_trans_table);
1319
bool multi_update::send_data(List<Item> ¬_used_values __attribute__((unused)))
1359
bool multi_update::send_data(List<Item> ¬_used_values)
1321
TableList *cur_table;
1361
TABLE_LIST *cur_table;
1362
DBUG_ENTER("multi_update::send_data");
1323
1364
for (cur_table= update_tables; cur_table; cur_table= cur_table->next_local)
1325
Table *table= cur_table->table;
1326
uint32_t offset= cur_table->shared;
1366
TABLE *table= cur_table->table;
1367
uint offset= cur_table->shared;
1328
1369
Check if we are using outer join and we didn't find the row
1329
1370
or if we have already updated this row in the previous call to this
1355
1396
store_record(table,record[1]);
1356
1397
if (fill_record(thd, *fields_for_table[offset],
1357
1398
*values_for_table[offset], 0))
1361
if (!can_compare_record || table->compare_record())
1402
if (!can_compare_record || compare_record(table))
1364
1405
if (!updated++)
1417
Table *tmp_table= tmp_tables[offset];
1458
TABLE *tmp_table= tmp_tables[offset];
1419
1460
For updatable VIEW store rowid of the updated table and
1420
1461
rowids of tables used in the CHECK OPTION condition.
1422
uint32_t field_num= 0;
1423
List_iterator_fast<Table> tbl_it(unupdated_check_opt_tables);
1464
List_iterator_fast<TABLE> tbl_it(unupdated_check_opt_tables);
1427
1468
tbl->file->position(tbl->record[0]);
1428
memcpy(tmp_table->field[field_num]->ptr,
1429
tbl->file->ref, tbl->file->ref_length);
1469
memcpy((char*) tmp_table->field[field_num]->ptr,
1470
(char*) tbl->file->ref, tbl->file->ref_length);
1431
1472
} while ((tbl= tbl_it++));
1449
return(1); // Not a table_is_full error
1490
DBUG_RETURN(1); // Not a table_is_full error
1459
void multi_update::send_error(uint32_t errcode,const char *err)
1500
void multi_update::send_error(uint errcode,const char *err)
1461
1502
/* First send error what ever it is ... */
1462
1503
my_error(errcode, MYF(0), err);
1503
1544
thd->binlog_query(THD::ROW_QUERY_TYPE,
1504
1545
thd->query, thd->query_length,
1505
transactional_tables, false);
1546
transactional_tables, FALSE);
1507
thd->transaction.all.modified_non_trans_table= true;
1548
thd->transaction.all.modified_non_trans_table= TRUE;
1509
assert(trans_safe || !updated || thd->transaction.stmt.modified_non_trans_table);
1550
DBUG_ASSERT(trans_safe || !updated || thd->transaction.stmt.modified_non_trans_table);
1513
1554
int multi_update::do_updates()
1515
TableList *cur_table;
1556
TABLE_LIST *cur_table;
1516
1557
int local_error= 0;
1517
1558
ha_rows org_updated;
1518
Table *table, *tmp_table;
1519
List_iterator_fast<Table> check_opt_it(unupdated_check_opt_tables);
1559
TABLE *table, *tmp_table;
1560
List_iterator_fast<TABLE> check_opt_it(unupdated_check_opt_tables);
1561
DBUG_ENTER("multi_update::do_updates");
1521
1563
do_update= 0; // Don't retry this function
1524
1566
for (cur_table= update_tables; cur_table; cur_table= cur_table->next_local)
1526
1568
bool can_compare_record;
1527
uint32_t offset= cur_table->shared;
1569
uint offset= cur_table->shared;
1529
1571
table = cur_table->table;
1530
1572
if (table == table_to_update)
1581
1623
/* call rnd_pos() using rowids from temporary table */
1582
1624
check_opt_it.rewind();
1584
uint32_t field_num= 0;
1587
1629
if((local_error=
1588
1630
tbl->file->rnd_pos(tbl->record[0],
1589
(unsigned char *) tmp_table->field[field_num]->ptr)))
1631
(uchar *) tmp_table->field[field_num]->ptr)))
1592
1634
} while((tbl= check_opt_it++));
1600
1642
copy_field_ptr++)
1601
1643
(*copy_field_ptr->do_copy)(copy_field_ptr);
1603
if (!can_compare_record || table->compare_record())
1645
if (!can_compare_record || compare_record(table))
1605
1647
if ((local_error=table->file->ha_update_row(table->record[1],
1606
1648
table->record[0])) &&
1626
1668
trans_safe= 0; // Can't do safe rollback
1627
thd->transaction.stmt.modified_non_trans_table= true;
1669
thd->transaction.stmt.modified_non_trans_table= TRUE;
1630
1672
(void) table->file->ha_rnd_end();
1631
1673
(void) tmp_table->file->ha_rnd_end();
1632
1674
check_opt_it.rewind();
1633
while (Table *tbl= check_opt_it++)
1675
while (TABLE *tbl= check_opt_it++)
1634
1676
tbl->file->ha_rnd_end();
1645
1687
(void) table->file->ha_rnd_end();
1646
1688
(void) tmp_table->file->ha_rnd_end();
1647
1689
check_opt_it.rewind();
1648
while (Table *tbl= check_opt_it++)
1690
while (TABLE *tbl= check_opt_it++)
1649
1691
tbl->file->ha_rnd_end();
1651
1693
if (updated != org_updated)
1667
1709
bool multi_update::send_eof()
1669
1711
char buff[STRING_BUFFER_USUAL_SIZE];
1671
1713
THD::killed_state killed_status= THD::NOT_KILLED;
1673
thd->set_proc_info("updating reference tables");
1714
DBUG_ENTER("multi_update::send_eof");
1715
thd_proc_info(thd, "updating reference tables");
1676
1718
Does updates for the last n - 1 tables, returns 0 if ok;
1682
1724
later carried out killing should not affect binlogging.
1684
1726
killed_status= (local_error == 0)? THD::NOT_KILLED : thd->killed;
1685
thd->set_proc_info("end");
1727
thd_proc_info(thd, "end");
1688
1730
Write the SQL statement to the binlog if we updated
1693
1735
either from the query's list or via a stored routine: bug#13270,23333
1696
assert(trans_safe || !updated ||
1738
DBUG_ASSERT(trans_safe || !updated ||
1697
1739
thd->transaction.stmt.modified_non_trans_table);
1698
1740
if (local_error == 0 || thd->transaction.stmt.modified_non_trans_table)
1703
1745
thd->clear_error();
1704
1746
if (thd->binlog_query(THD::ROW_QUERY_TYPE,
1705
1747
thd->query, thd->query_length,
1706
transactional_tables, false, killed_status) &&
1748
transactional_tables, FALSE, killed_status) &&
1709
local_error= 1; // Rollback update
1751
local_error= 1; // Rollback update
1712
1754
if (thd->transaction.stmt.modified_non_trans_table)
1713
thd->transaction.all.modified_non_trans_table= true;
1755
thd->transaction.all.modified_non_trans_table= TRUE;
1715
1757
if (local_error != 0)
1716
error_handled= true; // to force early leave from ::send_error()
1758
error_handled= TRUE; // to force early leave from ::send_error()
1718
1760
if (local_error > 0) // if the above log write did not fail ...
1720
1762
/* Safety: If we haven't got an error before (can happen in do_updates) */
1721
1763
my_message(ER_UNKNOWN_ERROR, "An error occured in multi-table update",
1726
1768
id= thd->arg_of_last_insert_id_function ?