~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_update.cc

  • Committer: Lee Bieber
  • Date: 2011-02-23 23:22:48 UTC
  • mfrom: (2183.2.20 list2)
  • mto: This revision was merged to the branch mainline in revision 2197.
  • Revision ID: kalebral@gmail.com-20110223232248-ev4y8pyt16b806o0
Merge Olaf - Use List::size()

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
20
19
*/
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"
 
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>
34
36
 
35
37
#include <boost/dynamic_bitset.hpp>
36
38
#include <list>
112
114
  Process usual UPDATE
113
115
 
114
116
  SYNOPSIS
115
 
    mysql_update()
 
117
    update_query()
116
118
    session                     thread handler
117
119
    fields              fields for update
118
120
    values              values of fields for update
127
129
    1  - error
128
130
*/
129
131
 
130
 
int mysql_update(Session *session, TableList *table_list,
 
132
int update_query(Session *session, TableList *table_list,
131
133
                 List<Item> &fields, List<Item> &values, COND *conds,
132
134
                 uint32_t order_num, Order *order,
133
135
                 ha_rows limit, enum enum_duplicates,
136
138
  bool          using_limit= limit != HA_POS_ERROR;
137
139
  bool          used_key_is_modified;
138
140
  bool          transactional_table;
139
 
  int           error;
 
141
  int           error= 0;
140
142
  uint          used_index= MAX_KEY, dup_key_found;
141
143
  bool          need_sort= true;
142
144
  ha_rows       updated, found;
144
146
  Table         *table;
145
147
  optimizer::SqlSelect *select= NULL;
146
148
  ReadRecord    info;
147
 
  Select_Lex    *select_lex= &session->lex->select_lex;
 
149
  Select_Lex    *select_lex= &session->getLex()->select_lex;
148
150
  uint64_t     id;
149
151
  List<Item> all_fields;
150
152
  Session::killed_state_t killed_status= Session::NOT_KILLED;
163
165
  table->covering_keys= table->getShare()->keys_in_use;
164
166
  table->quick_keys.reset();
165
167
 
166
 
  if (mysql_prepare_update(session, table_list, &conds, order_num, order))
 
168
  if (prepare_update(session, table_list, &conds, order_num, order))
167
169
  {
168
170
    DRIZZLE_UPDATE_DONE(1, 0, 0);
169
171
    return 1;
202
204
    return 1;
203
205
  }
204
206
 
205
 
  if (select_lex->inner_refs_list.elements &&
 
207
  if (select_lex->inner_refs_list.size() &&
206
208
    fix_inner_refs(session, all_fields, select_lex, select_lex->ref_pointer_array))
207
209
  {
208
210
    DRIZZLE_UPDATE_DONE(1, 0, 0);
209
 
    return -1;
 
211
    return 1;
210
212
  }
211
213
 
212
214
  if (conds)
246
248
     */
247
249
    session->main_da.reset_diagnostics_area();
248
250
    free_underlaid_joins(session, select_lex);
249
 
    if (error)
 
251
    if (error || session->is_error())
250
252
    {
251
253
      DRIZZLE_UPDATE_DONE(1, 0, 0);
252
254
      return 1;
328
330
        Filesort has already found and selected the rows we want to update,
329
331
        so we don't need the where clause
330
332
      */
331
 
      delete select;
332
 
      select= 0;
 
333
      safe_delete(select);
333
334
    }
334
335
    else
335
336
    {
363
364
 
364
365
      if (used_index == MAX_KEY || (select && select->quick))
365
366
      {
366
 
        info.init_read_record(session, table, select, 0, true);
 
367
        if ((error= info.init_read_record(session, table, select, 0, true)))
 
368
          goto err;
367
369
      }
368
370
      else
369
371
      {
370
 
        info.init_read_record_idx(session, table, 1, used_index);
 
372
        if ((error= info.init_read_record_idx(session, table, 1, used_index)))
 
373
          goto err;
371
374
      }
372
375
 
373
376
      session->set_proc_info("Searching rows for update");
405
408
      /* Change select to use tempfile */
406
409
      if (select)
407
410
      {
408
 
        delete select->quick;
 
411
        safe_delete(select->quick);
409
412
        if (select->free_cond)
410
413
          delete select->cond;
411
 
        select->quick=0;
412
414
        select->cond=0;
413
415
      }
414
416
      else
415
417
      {
416
 
        select= new optimizer::SqlSelect;
 
418
        select= new optimizer::SqlSelect();
417
419
        select->head=table;
418
420
      }
419
421
      if (tempfile.reinit_io_cache(internal::READ_CACHE,0L,0,0))
433
435
  if (select && select->quick && select->quick->reset())
434
436
    goto err;
435
437
  table->cursor->try_semi_consistent_read(1);
436
 
  info.init_read_record(session, table, select, 0, true);
 
438
  if ((error= info.init_read_record(session, table, select, 0, true)))
 
439
  {
 
440
    goto err;
 
441
  }
437
442
 
438
443
  updated= found= 0;
439
444
  /*
449
454
  session->set_proc_info("Updating");
450
455
 
451
456
  transactional_table= table->cursor->has_transactions();
452
 
  session->abort_on_warning= test(!ignore);
 
457
  session->setAbortOnWarning(test(!ignore));
453
458
 
454
459
  /*
455
460
    Assure that we can use position()
571
576
     * lp bug# 439719
572
577
     */
573
578
    session->main_da.reset_diagnostics_area();
574
 
    session->my_ok((ulong) session->row_count_func, found, id, buff);
575
 
    session->status_var.updated_row_count+= session->row_count_func;
 
579
    session->my_ok((ulong) session->rowCount(), found, id, buff);
 
580
    session->status_var.updated_row_count+= session->rowCount();
576
581
  }
577
582
  session->count_cuted_fields= CHECK_FIELD_ERROR_FOR_NULL;              /* calc cuted fields */
578
 
  session->abort_on_warning= 0;
 
583
  session->setAbortOnWarning(false);
579
584
  DRIZZLE_UPDATE_DONE((error >= 0 || session->is_error()), found, updated);
580
585
  return ((error >= 0 || session->is_error()) ? 1 : 0);
581
586
 
582
587
err:
 
588
  if (error != 0)
 
589
    table->print_error(error,MYF(0));
 
590
 
583
591
  delete select;
584
592
  free_underlaid_joins(session, select_lex);
585
593
  if (table->key_read)
587
595
    table->key_read=0;
588
596
    table->cursor->extra(HA_EXTRA_NO_KEYREAD);
589
597
  }
590
 
  session->abort_on_warning= 0;
 
598
  session->setAbortOnWarning(false);
591
599
 
592
600
  DRIZZLE_UPDATE_DONE(1, 0, 0);
593
601
  return 1;
597
605
  Prepare items in UPDATE statement
598
606
 
599
607
  SYNOPSIS
600
 
    mysql_prepare_update()
 
608
    prepare_update()
601
609
    session                     - thread handler
602
610
    table_list          - global/local table list
603
611
    conds               - conditions
608
616
    false OK
609
617
    true  error
610
618
*/
611
 
bool mysql_prepare_update(Session *session, TableList *table_list,
 
619
bool prepare_update(Session *session, TableList *table_list,
612
620
                         Item **conds, uint32_t order_num, Order *order)
613
621
{
614
622
  List<Item> all_fields;
615
 
  Select_Lex *select_lex= &session->lex->select_lex;
 
623
  Select_Lex *select_lex= &session->getLex()->select_lex;
616
624
 
617
 
  session->lex->allow_sum_func= 0;
 
625
  session->getLex()->allow_sum_func= 0;
618
626
 
619
627
  if (setup_tables_and_check_access(session, &select_lex->context,
620
628
                                    &select_lex->top_join_list,