~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_update.cc

  • Committer: Brian Aker
  • Date: 2010-10-09 17:44:13 UTC
  • mto: This revision was merged to the branch mainline in revision 1853.
  • Revision ID: brian@tangent.org-20101009174413-4cs0q58kw0fjd45y
First pass through adding back user_locks.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
/*
18
18
  Single table and multi table updates of tables.
 
19
  Multi-table updates were introduced by Sinisa & Monty
19
20
*/
20
 
 
21
21
#include "config.h"
22
 
 
23
22
#include "drizzled/sql_select.h"
24
23
#include "drizzled/error.h"
25
24
#include "drizzled/probes.h"
26
25
#include "drizzled/sql_base.h"
27
 
#include "drizzled/field/epoch.h"
 
26
#include "drizzled/field/timestamp.h"
28
27
#include "drizzled/sql_parse.h"
29
28
#include "drizzled/optimizer/range.h"
30
29
#include "drizzled/records.h"
31
30
#include "drizzled/internal/my_sys.h"
32
31
#include "drizzled/internal/iocache.h"
33
 
#include "drizzled/transaction_services.h"
34
 
#include "drizzled/filesort.h"
35
 
#include "drizzled/plugin/storage_engine.h"
36
32
 
37
33
#include <boost/dynamic_bitset.hpp>
38
34
#include <list>
76
72
    return;
77
73
 
78
74
  /* Create unique_map with all fields used by that index. */
79
 
  boost::dynamic_bitset<> unique_map(table->getShare()->sizeFields()); /* Fields in offended unique. */
 
75
  boost::dynamic_bitset<> unique_map(table->getMutableShare()->sizeFields()); /* Fields in offended unique. */
80
76
  table->mark_columns_used_by_index_no_reset(keynr, unique_map);
81
77
 
82
78
  /* Subtract read_set and write_set. */
100
96
  /* Copy the newly read columns into the new record. */
101
97
  for (field_p= table->getFields(); (field= *field_p); field_p++)
102
98
  {
103
 
    if (unique_map.test(field->position()))
 
99
    if (unique_map.test(field->field_index))
104
100
    {
105
101
      field->copy_from_tmp(table->getShare()->rec_buff_length);
106
102
    }
114
110
  Process usual UPDATE
115
111
 
116
112
  SYNOPSIS
117
 
    update_query()
 
113
    mysql_update()
118
114
    session                     thread handler
119
115
    fields              fields for update
120
116
    values              values of fields for update
129
125
    1  - error
130
126
*/
131
127
 
132
 
int update_query(Session *session, TableList *table_list,
 
128
int mysql_update(Session *session, TableList *table_list,
133
129
                 List<Item> &fields, List<Item> &values, COND *conds,
134
 
                 uint32_t order_num, Order *order,
 
130
                 uint32_t order_num, order_st *order,
135
131
                 ha_rows limit, enum enum_duplicates,
136
132
                 bool ignore)
137
133
{
138
134
  bool          using_limit= limit != HA_POS_ERROR;
139
135
  bool          used_key_is_modified;
140
136
  bool          transactional_table;
141
 
  int           error= 0;
 
137
  bool          can_compare_record;
 
138
  int           error;
142
139
  uint          used_index= MAX_KEY, dup_key_found;
143
140
  bool          need_sort= true;
144
141
  ha_rows       updated, found;
149
146
  Select_Lex    *select_lex= &session->lex->select_lex;
150
147
  uint64_t     id;
151
148
  List<Item> all_fields;
152
 
  Session::killed_state_t killed_status= Session::NOT_KILLED;
 
149
  Session::killed_state killed_status= Session::NOT_KILLED;
153
150
 
154
 
  DRIZZLE_UPDATE_START(session->getQueryString()->c_str());
 
151
  DRIZZLE_UPDATE_START(session->query.c_str());
155
152
  if (session->openTablesLock(table_list))
156
153
  {
157
154
    DRIZZLE_UPDATE_DONE(1, 0, 0);
165
162
  table->covering_keys= table->getShare()->keys_in_use;
166
163
  table->quick_keys.reset();
167
164
 
168
 
  if (prepare_update(session, table_list, &conds, order_num, order))
169
 
  {
170
 
    DRIZZLE_UPDATE_DONE(1, 0, 0);
171
 
    return 1;
172
 
  }
 
165
  if (mysql_prepare_update(session, table_list, &conds, order_num, order))
 
166
    goto abort;
173
167
 
174
168
  old_covering_keys= table->covering_keys;              // Keys used in WHERE
175
169
  /* Check the fields we are going to modify */
176
170
  if (setup_fields_with_no_wrap(session, 0, fields, MARK_COLUMNS_WRITE, 0, 0))
177
 
  {
178
 
    DRIZZLE_UPDATE_DONE(1, 0, 0);
179
 
    return 1;
180
 
  }
181
 
 
 
171
    goto abort;
182
172
  if (table->timestamp_field)
183
173
  {
184
174
    // Don't set timestamp column if this is modified
191
181
      if (table->timestamp_field_type == TIMESTAMP_AUTO_SET_ON_UPDATE ||
192
182
          table->timestamp_field_type == TIMESTAMP_AUTO_SET_ON_BOTH)
193
183
      {
194
 
        table->setWriteSet(table->timestamp_field->position());
 
184
        table->setWriteSet(table->timestamp_field->field_index);
195
185
      }
196
186
    }
197
187
  }
199
189
  if (setup_fields(session, 0, values, MARK_COLUMNS_READ, 0, 0))
200
190
  {
201
191
    free_underlaid_joins(session, select_lex);
202
 
    DRIZZLE_UPDATE_DONE(1, 0, 0);
203
 
 
204
 
    return 1;
 
192
    goto abort;
205
193
  }
206
194
 
207
195
  if (select_lex->inner_refs_list.elements &&
208
196
    fix_inner_refs(session, all_fields, select_lex, select_lex->ref_pointer_array))
209
197
  {
210
198
    DRIZZLE_UPDATE_DONE(1, 0, 0);
211
 
    return 1;
 
199
    return -1;
212
200
  }
213
201
 
214
202
  if (conds)
248
236
     */
249
237
    session->main_da.reset_diagnostics_area();
250
238
    free_underlaid_joins(session, select_lex);
251
 
    if (error || session->is_error())
252
 
    {
253
 
      DRIZZLE_UPDATE_DONE(1, 0, 0);
254
 
      return 1;
255
 
    }
 
239
    if (error)
 
240
      goto abort;                               // Error in where
256
241
    DRIZZLE_UPDATE_DONE(0, 0, 0);
257
242
    session->my_ok();                           // No matching records
258
243
    return 0;
315
300
      uint32_t         length= 0;
316
301
      SortField  *sortorder;
317
302
      ha_rows examined_rows;
318
 
      FileSort filesort(*session);
319
303
 
320
 
      table->sort.io_cache= new internal::IO_CACHE;
 
304
      table->sort.io_cache = new internal::IO_CACHE;
321
305
 
322
306
      if (!(sortorder=make_unireg_sortorder(order, &length, NULL)) ||
323
 
          (table->sort.found_records= filesort.run(table, sortorder, length,
324
 
                                                   select, limit, 1,
325
 
                                                   examined_rows)) == HA_POS_ERROR)
 
307
          (table->sort.found_records= filesort(session, table, sortorder, length,
 
308
                                               select, limit, 1,
 
309
                                               &examined_rows))
 
310
          == HA_POS_ERROR)
326
311
      {
327
312
        goto err;
328
313
      }
342
327
      */
343
328
 
344
329
      internal::IO_CACHE tempfile;
345
 
      if (tempfile.open_cached_file(drizzle_tmpdir.c_str(),TEMP_PREFIX, DISK_BUFFER_SIZE, MYF(MY_WME)))
346
 
      {
 
330
      if (open_cached_file(&tempfile, drizzle_tmpdir.c_str(),TEMP_PREFIX,
 
331
                           DISK_BUFFER_SIZE, MYF(MY_WME)))
347
332
        goto err;
348
 
      }
349
333
 
350
334
      /* If quick select is used, initialize it before retrieving rows. */
351
335
      if (select && select->quick && select->quick->reset())
365
349
 
366
350
      if (used_index == MAX_KEY || (select && select->quick))
367
351
      {
368
 
        if ((error= info.init_read_record(session, table, select, 0, true)))
369
 
          goto err;
 
352
        info.init_read_record(session, table, select, 0, true);
370
353
      }
371
354
      else
372
355
      {
373
 
        if ((error= info.init_read_record_idx(session, table, 1, used_index)))
374
 
          goto err;
 
356
        info.init_read_record_idx(session, table, 1, used_index);
375
357
      }
376
358
 
377
359
      session->set_proc_info("Searching rows for update");
378
360
      ha_rows tmp_limit= limit;
379
361
 
380
 
      while (not(error= info.read_record(&info)) && not session->getKilled())
 
362
      while (!(error=info.read_record(&info)) && !session->killed)
381
363
      {
382
364
        if (!(select && select->skip_record()))
383
365
        {
400
382
        else
401
383
          table->cursor->unlock_row();
402
384
      }
403
 
      if (session->getKilled() && not error)
 
385
      if (session->killed && !error)
404
386
        error= 1;                               // Aborted
405
387
      limit= tmp_limit;
406
388
      table->cursor->try_semi_consistent_read(0);
417
399
      }
418
400
      else
419
401
      {
420
 
        select= new optimizer::SqlSelect();
 
402
        select= new optimizer::SqlSelect;
421
403
        select->head=table;
422
404
      }
423
 
      if (tempfile.reinit_io_cache(internal::READ_CACHE,0L,0,0))
 
405
      if (reinit_io_cache(&tempfile,internal::READ_CACHE,0L,0,0))
424
406
        error=1;
425
407
      // Read row ptrs from this cursor
426
408
      memcpy(select->file, &tempfile, sizeof(tempfile));
437
419
  if (select && select->quick && select->quick->reset())
438
420
    goto err;
439
421
  table->cursor->try_semi_consistent_read(1);
440
 
  if ((error= info.init_read_record(session, table, select, 0, true)))
441
 
  {
442
 
    goto err;
443
 
  }
 
422
  info.init_read_record(session, table, select, 0, true);
444
423
 
445
424
  updated= found= 0;
446
425
  /*
456
435
  session->set_proc_info("Updating");
457
436
 
458
437
  transactional_table= table->cursor->has_transactions();
459
 
  session->setAbortOnWarning(test(!ignore));
 
438
  session->abort_on_warning= test(!ignore);
460
439
 
461
440
  /*
462
441
    Assure that we can use position()
465
444
  if (table->cursor->getEngine()->check_flag(HTON_BIT_PARTIAL_COLUMN_READ))
466
445
    table->prepare_for_position();
467
446
 
468
 
  while (not (error=info.read_record(&info)) && not session->getKilled())
 
447
  /*
 
448
    We can use compare_record() to optimize away updates if
 
449
    the table handler is returning all columns OR if
 
450
    if all updated columns are read
 
451
  */
 
452
  can_compare_record= (! (table->cursor->getEngine()->check_flag(HTON_BIT_PARTIAL_COLUMN_READ)) ||
 
453
                       table->write_set->is_subset_of(*table->read_set));
 
454
 
 
455
  while (! (error=info.read_record(&info)) && !session->killed)
469
456
  {
470
 
    if (not (select && select->skip_record()))
 
457
    if (! (select && select->skip_record()))
471
458
    {
472
459
      if (table->cursor->was_semi_consistent_read())
473
460
        continue;  /* repeat the read of the same row if it still exists */
478
465
 
479
466
      found++;
480
467
 
481
 
      if (! table->records_are_comparable() || table->compare_records())
 
468
      if (!can_compare_record || table->compare_record())
482
469
      {
483
470
        /* Non-batched update */
484
471
        error= table->cursor->updateRecord(table->getUpdateRecord(),
487
474
        table->auto_increment_field_not_null= false;
488
475
 
489
476
        if (!error || error == HA_ERR_RECORD_IS_THE_SAME)
490
 
        {
 
477
        {
491
478
          if (error != HA_ERR_RECORD_IS_THE_SAME)
492
479
            updated++;
493
480
          else
494
481
            error= 0;
495
 
        }
496
 
        else if (! ignore ||
 
482
        }
 
483
        else if (! ignore ||
497
484
                 table->cursor->is_fatal_error(error, HA_CHECK_DUP_KEY))
498
 
        {
 
485
        {
499
486
          /*
500
487
            If (ignore && error is ignorable) we don't have to
501
488
            do anything; otherwise...
506
493
            flags|= ME_FATALERROR; /* Other handler errors are fatal */
507
494
 
508
495
          prepare_record_for_error_message(error, table);
509
 
          table->print_error(error,MYF(flags));
510
 
          error= 1;
511
 
          break;
512
 
        }
 
496
          table->print_error(error,MYF(flags));
 
497
          error= 1;
 
498
          break;
 
499
        }
513
500
      }
514
501
 
515
502
      if (!--limit && using_limit)
531
518
    It's assumed that if an error was set in combination with an effective
532
519
    killed status then the error is due to killing.
533
520
  */
534
 
  killed_status= session->getKilled(); // get the status of the volatile
 
521
  killed_status= session->killed; // get the status of the volatile
535
522
  // simulated killing after the loop must be ineffective for binlogging
536
523
  error= (killed_status == Session::NOT_KILLED)?  error : 1;
537
524
 
578
565
     * lp bug# 439719
579
566
     */
580
567
    session->main_da.reset_diagnostics_area();
581
 
    session->my_ok((ulong) session->rowCount(), found, id, buff);
582
 
    session->status_var.updated_row_count+= session->rowCount();
 
568
    session->my_ok((ulong) session->row_count_func, found, id, buff);
 
569
    session->status_var.updated_row_count+= session->row_count_func;
583
570
  }
584
571
  session->count_cuted_fields= CHECK_FIELD_ERROR_FOR_NULL;              /* calc cuted fields */
585
 
  session->setAbortOnWarning(false);
 
572
  session->abort_on_warning= 0;
586
573
  DRIZZLE_UPDATE_DONE((error >= 0 || session->is_error()), found, updated);
587
574
  return ((error >= 0 || session->is_error()) ? 1 : 0);
588
575
 
589
576
err:
590
 
  if (error != 0)
591
 
    table->print_error(error,MYF(0));
592
 
 
593
577
  delete select;
594
578
  free_underlaid_joins(session, select_lex);
595
579
  if (table->key_read)
597
581
    table->key_read=0;
598
582
    table->cursor->extra(HA_EXTRA_NO_KEYREAD);
599
583
  }
600
 
  session->setAbortOnWarning(false);
 
584
  session->abort_on_warning= 0;
601
585
 
 
586
abort:
602
587
  DRIZZLE_UPDATE_DONE(1, 0, 0);
603
588
  return 1;
604
589
}
607
592
  Prepare items in UPDATE statement
608
593
 
609
594
  SYNOPSIS
610
 
    prepare_update()
 
595
    mysql_prepare_update()
611
596
    session                     - thread handler
612
597
    table_list          - global/local table list
613
598
    conds               - conditions
618
603
    false OK
619
604
    true  error
620
605
*/
621
 
bool prepare_update(Session *session, TableList *table_list,
622
 
                         Item **conds, uint32_t order_num, Order *order)
 
606
bool mysql_prepare_update(Session *session, TableList *table_list,
 
607
                         Item **conds, uint32_t order_num, order_st *order)
623
608
{
624
609
  List<Item> all_fields;
625
610
  Select_Lex *select_lex= &session->lex->select_lex;
642
627
    TableList *duplicate;
643
628
    if ((duplicate= unique_table(table_list, table_list->next_global)))
644
629
    {
645
 
      my_error(ER_UPDATE_TABLE_USED, MYF(0), table_list->getTableName());
 
630
      my_error(ER_UPDATE_TABLE_USED, MYF(0), table_list->table_name);
646
631
      return true;
647
632
    }
648
633
  }