~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_update.cc

  • Committer: Brian Aker
  • Date: 2010-12-03 18:46:31 UTC
  • mto: This revision was merged to the branch mainline in revision 1973.
  • Revision ID: brian@tangent.org-20101203184631-rk40syiqbaitq9h8
Remove the use of "using std" from the plugin interface .cc files.

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
 
#include <config.h>
22
 
 
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>
 
21
#include "config.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"
36
34
 
37
35
#include <boost/dynamic_bitset.hpp>
38
36
#include <list>
100
98
  /* Copy the newly read columns into the new record. */
101
99
  for (field_p= table->getFields(); (field= *field_p); field_p++)
102
100
  {
103
 
    if (unique_map.test(field->position()))
 
101
    if (unique_map.test(field->field_index))
104
102
    {
105
103
      field->copy_from_tmp(table->getShare()->rec_buff_length);
106
104
    }
114
112
  Process usual UPDATE
115
113
 
116
114
  SYNOPSIS
117
 
    update_query()
 
115
    mysql_update()
118
116
    session                     thread handler
119
117
    fields              fields for update
120
118
    values              values of fields for update
129
127
    1  - error
130
128
*/
131
129
 
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;
141
 
  int           error= 0;
 
139
  bool          can_compare_record;
 
140
  int           error;
142
141
  uint          used_index= MAX_KEY, dup_key_found;
143
142
  bool          need_sort= true;
144
143
  ha_rows       updated, found;
146
145
  Table         *table;
147
146
  optimizer::SqlSelect *select= NULL;
148
147
  ReadRecord    info;
149
 
  Select_Lex    *select_lex= &session->getLex()->select_lex;
 
148
  Select_Lex    *select_lex= &session->lex->select_lex;
150
149
  uint64_t     id;
151
150
  List<Item> all_fields;
152
151
  Session::killed_state_t killed_status= Session::NOT_KILLED;
165
164
  table->covering_keys= table->getShare()->keys_in_use;
166
165
  table->quick_keys.reset();
167
166
 
168
 
  if (prepare_update(session, table_list, &conds, order_num, order))
 
167
  if (mysql_prepare_update(session, table_list, &conds, order_num, order))
169
168
  {
170
169
    DRIZZLE_UPDATE_DONE(1, 0, 0);
171
170
    return 1;
191
190
      if (table->timestamp_field_type == TIMESTAMP_AUTO_SET_ON_UPDATE ||
192
191
          table->timestamp_field_type == TIMESTAMP_AUTO_SET_ON_BOTH)
193
192
      {
194
 
        table->setWriteSet(table->timestamp_field->position());
 
193
        table->setWriteSet(table->timestamp_field->field_index);
195
194
      }
196
195
    }
197
196
  }
208
207
    fix_inner_refs(session, all_fields, select_lex, select_lex->ref_pointer_array))
209
208
  {
210
209
    DRIZZLE_UPDATE_DONE(1, 0, 0);
211
 
    return 1;
 
210
    return -1;
212
211
  }
213
212
 
214
213
  if (conds)
248
247
     */
249
248
    session->main_da.reset_diagnostics_area();
250
249
    free_underlaid_joins(session, select_lex);
251
 
    if (error || session->is_error())
 
250
    if (error)
252
251
    {
253
252
      DRIZZLE_UPDATE_DONE(1, 0, 0);
254
253
      return 1;
330
329
        Filesort has already found and selected the rows we want to update,
331
330
        so we don't need the where clause
332
331
      */
333
 
      safe_delete(select);
 
332
      delete select;
 
333
      select= 0;
334
334
    }
335
335
    else
336
336
    {
364
364
 
365
365
      if (used_index == MAX_KEY || (select && select->quick))
366
366
      {
367
 
        if ((error= info.init_read_record(session, table, select, 0, true)))
368
 
          goto err;
 
367
        info.init_read_record(session, table, select, 0, true);
369
368
      }
370
369
      else
371
370
      {
372
 
        if ((error= info.init_read_record_idx(session, table, 1, used_index)))
373
 
          goto err;
 
371
        info.init_read_record_idx(session, table, 1, used_index);
374
372
      }
375
373
 
376
374
      session->set_proc_info("Searching rows for update");
408
406
      /* Change select to use tempfile */
409
407
      if (select)
410
408
      {
411
 
        safe_delete(select->quick);
 
409
        delete select->quick;
412
410
        if (select->free_cond)
413
411
          delete select->cond;
 
412
        select->quick=0;
414
413
        select->cond=0;
415
414
      }
416
415
      else
417
416
      {
418
 
        select= new optimizer::SqlSelect();
 
417
        select= new optimizer::SqlSelect;
419
418
        select->head=table;
420
419
      }
421
420
      if (tempfile.reinit_io_cache(internal::READ_CACHE,0L,0,0))
435
434
  if (select && select->quick && select->quick->reset())
436
435
    goto err;
437
436
  table->cursor->try_semi_consistent_read(1);
438
 
  if ((error= info.init_read_record(session, table, select, 0, true)))
439
 
  {
440
 
    goto err;
441
 
  }
 
437
  info.init_read_record(session, table, select, 0, true);
442
438
 
443
439
  updated= found= 0;
444
440
  /*
454
450
  session->set_proc_info("Updating");
455
451
 
456
452
  transactional_table= table->cursor->has_transactions();
457
 
  session->setAbortOnWarning(test(!ignore));
 
453
  session->abort_on_warning= test(!ignore);
458
454
 
459
455
  /*
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();
465
461
 
 
462
  /*
 
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
 
466
  */
 
467
  can_compare_record= (! (table->cursor->getEngine()->check_flag(HTON_BIT_PARTIAL_COLUMN_READ)) ||
 
468
                       table->write_set->is_subset_of(*table->read_set));
 
469
 
466
470
  while (not (error=info.read_record(&info)) && not session->getKilled())
467
471
  {
468
472
    if (not (select && select->skip_record()))
472
476
 
473
477
      table->storeRecord();
474
478
      if (fill_record(session, fields, values))
 
479
      {
 
480
        /*
 
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.
 
484
         */
 
485
        if (updated > 0)
 
486
        {
 
487
          TransactionServices &ts= TransactionServices::singleton();
 
488
          ts.removeStatementRecords(session, updated);
 
489
        }
 
490
 
475
491
        break;
 
492
      }
476
493
 
477
494
      found++;
478
495
 
479
 
      if (! table->records_are_comparable() || table->compare_records())
 
496
      if (!can_compare_record || table->compare_record())
480
497
      {
481
498
        /* Non-batched update */
482
499
        error= table->cursor->updateRecord(table->getUpdateRecord(),
576
593
     * lp bug# 439719
577
594
     */
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;
581
598
  }
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);
586
603
 
587
604
err:
588
 
  if (error != 0)
589
 
    table->print_error(error,MYF(0));
590
 
 
591
605
  delete select;
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);
597
611
  }
598
 
  session->setAbortOnWarning(false);
 
612
  session->abort_on_warning= 0;
599
613
 
600
614
  DRIZZLE_UPDATE_DONE(1, 0, 0);
601
615
  return 1;
605
619
  Prepare items in UPDATE statement
606
620
 
607
621
  SYNOPSIS
608
 
    prepare_update()
 
622
    mysql_prepare_update()
609
623
    session                     - thread handler
610
624
    table_list          - global/local table list
611
625
    conds               - conditions
616
630
    false OK
617
631
    true  error
618
632
*/
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)
621
635
{
622
636
  List<Item> all_fields;
623
 
  Select_Lex *select_lex= &session->getLex()->select_lex;
 
637
  Select_Lex *select_lex= &session->lex->select_lex;
624
638
 
625
 
  session->getLex()->allow_sum_func= 0;
 
639
  session->lex->allow_sum_func= 0;
626
640
 
627
641
  if (setup_tables_and_check_access(session, &select_lex->context,
628
642
                                    &select_lex->top_join_list,