~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_update.cc

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