18
18
Single table and multi table updates of tables.
19
Multi-table updates were introduced by Sinisa & Monty
23
#include <drizzled/sql_select.h>
24
#include <drizzled/error.h>
25
#include <drizzled/probes.h>
26
#include <drizzled/sql_base.h>
27
#include <drizzled/field/epoch.h>
28
#include <drizzled/sql_parse.h>
29
#include <drizzled/optimizer/range.h>
30
#include <drizzled/records.h>
31
#include <drizzled/internal/my_sys.h>
32
#include <drizzled/internal/iocache.h>
33
#include <drizzled/transaction_services.h>
34
#include <drizzled/filesort.h>
35
#include <drizzled/plugin/storage_engine.h>
22
#include "drizzled/sql_select.h"
23
#include "drizzled/error.h"
24
#include "drizzled/probes.h"
25
#include "drizzled/sql_base.h"
26
#include "drizzled/field/timestamp.h"
27
#include "drizzled/sql_parse.h"
28
#include "drizzled/optimizer/range.h"
29
#include "drizzled/records.h"
30
#include "drizzled/internal/my_sys.h"
31
#include "drizzled/internal/iocache.h"
32
#include "drizzled/transaction_services.h"
33
#include "drizzled/filesort.h"
37
35
#include <boost/dynamic_bitset.hpp>
100
98
/* Copy the newly read columns into the new record. */
101
99
for (field_p= table->getFields(); (field= *field_p); field_p++)
103
if (unique_map.test(field->position()))
101
if (unique_map.test(field->field_index))
105
103
field->copy_from_tmp(table->getShare()->rec_buff_length);
132
int update_query(Session *session, TableList *table_list,
130
int mysql_update(Session *session, TableList *table_list,
133
131
List<Item> &fields, List<Item> &values, COND *conds,
134
132
uint32_t order_num, Order *order,
135
133
ha_rows limit, enum enum_duplicates,
138
136
bool using_limit= limit != HA_POS_ERROR;
139
137
bool used_key_is_modified;
140
138
bool transactional_table;
139
bool can_compare_record;
142
141
uint used_index= MAX_KEY, dup_key_found;
143
142
bool need_sort= true;
144
143
ha_rows updated, found;
165
164
table->covering_keys= table->getShare()->keys_in_use;
166
165
table->quick_keys.reset();
168
if (prepare_update(session, table_list, &conds, order_num, order))
167
if (mysql_prepare_update(session, table_list, &conds, order_num, order))
170
169
DRIZZLE_UPDATE_DONE(1, 0, 0);
191
190
if (table->timestamp_field_type == TIMESTAMP_AUTO_SET_ON_UPDATE ||
192
191
table->timestamp_field_type == TIMESTAMP_AUTO_SET_ON_BOTH)
194
table->setWriteSet(table->timestamp_field->position());
193
table->setWriteSet(table->timestamp_field->field_index);
208
207
fix_inner_refs(session, all_fields, select_lex, select_lex->ref_pointer_array))
210
209
DRIZZLE_UPDATE_DONE(1, 0, 0);
249
248
session->main_da.reset_diagnostics_area();
250
249
free_underlaid_joins(session, select_lex);
251
if (error || session->is_error())
253
252
DRIZZLE_UPDATE_DONE(1, 0, 0);
365
365
if (used_index == MAX_KEY || (select && select->quick))
367
if ((error= info.init_read_record(session, table, select, 0, true)))
367
info.init_read_record(session, table, select, 0, true);
372
if ((error= info.init_read_record_idx(session, table, 1, used_index)))
371
info.init_read_record_idx(session, table, 1, used_index);
376
374
session->set_proc_info("Searching rows for update");
408
406
/* Change select to use tempfile */
411
safe_delete(select->quick);
409
delete select->quick;
412
410
if (select->free_cond)
413
411
delete select->cond;
418
select= new optimizer::SqlSelect();
417
select= new optimizer::SqlSelect;
419
418
select->head=table;
421
420
if (tempfile.reinit_io_cache(internal::READ_CACHE,0L,0,0))
435
434
if (select && select->quick && select->quick->reset())
437
436
table->cursor->try_semi_consistent_read(1);
438
if ((error= info.init_read_record(session, table, select, 0, true)))
437
info.init_read_record(session, table, select, 0, true);
443
439
updated= found= 0;
454
450
session->set_proc_info("Updating");
456
452
transactional_table= table->cursor->has_transactions();
457
session->setAbortOnWarning(test(!ignore));
453
session->abort_on_warning= test(!ignore);
460
456
Assure that we can use position()
463
459
if (table->cursor->getEngine()->check_flag(HTON_BIT_PARTIAL_COLUMN_READ))
464
460
table->prepare_for_position();
463
We can use compare_record() to optimize away updates if
464
the table handler is returning all columns OR if
465
if all updated columns are read
467
can_compare_record= (! (table->cursor->getEngine()->check_flag(HTON_BIT_PARTIAL_COLUMN_READ)) ||
468
table->write_set->is_subset_of(*table->read_set));
466
470
while (not (error=info.read_record(&info)) && not session->getKilled())
468
472
if (not (select && select->skip_record()))
473
477
table->storeRecord();
474
478
if (fill_record(session, fields, values))
481
* If we updated some rows before this one failed (updated > 0),
482
* then we will need to undo adding those records to the
483
* replication Statement message.
487
TransactionServices &ts= TransactionServices::singleton();
488
ts.removeStatementRecords(session, updated);
479
if (! table->records_are_comparable() || table->compare_records())
496
if (!can_compare_record || table->compare_record())
481
498
/* Non-batched update */
482
499
error= table->cursor->updateRecord(table->getUpdateRecord(),
578
595
session->main_da.reset_diagnostics_area();
579
session->my_ok((ulong) session->rowCount(), found, id, buff);
580
session->status_var.updated_row_count+= session->rowCount();
596
session->my_ok((ulong) session->row_count_func, found, id, buff);
597
session->status_var.updated_row_count+= session->row_count_func;
582
599
session->count_cuted_fields= CHECK_FIELD_ERROR_FOR_NULL; /* calc cuted fields */
583
session->setAbortOnWarning(false);
600
session->abort_on_warning= 0;
584
601
DRIZZLE_UPDATE_DONE((error >= 0 || session->is_error()), found, updated);
585
602
return ((error >= 0 || session->is_error()) ? 1 : 0);
589
table->print_error(error,MYF(0));
592
606
free_underlaid_joins(session, select_lex);
593
607
if (table->key_read)
595
609
table->key_read=0;
596
610
table->cursor->extra(HA_EXTRA_NO_KEYREAD);
598
session->setAbortOnWarning(false);
612
session->abort_on_warning= 0;
600
614
DRIZZLE_UPDATE_DONE(1, 0, 0);
619
bool prepare_update(Session *session, TableList *table_list,
633
bool mysql_prepare_update(Session *session, TableList *table_list,
620
634
Item **conds, uint32_t order_num, Order *order)
622
636
List<Item> all_fields;
623
Select_Lex *select_lex= &session->getLex()->select_lex;
637
Select_Lex *select_lex= &session->lex->select_lex;
625
session->getLex()->allow_sum_func= 0;
639
session->lex->allow_sum_func= 0;
627
641
if (setup_tables_and_check_access(session, &select_lex->context,
628
642
&select_lex->top_join_list,