~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_update.cc

  • Committer: Monty Taylor
  • Date: 2011-03-08 23:35:47 UTC
  • mfrom: (2224.2.9 statement2)
  • mto: This revision was merged to the branch mainline in revision 2227.
  • Revision ID: mordred@inaugust.com-20110308233547-w2s3tm5svzv339dp
Merged Olaf - Statement refactor.

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