~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_update.cc

  • Committer: Brian Aker
  • Date: 2010-12-24 21:27:54 UTC
  • mto: (2035.1.1 clean)
  • mto: This revision was merged to the branch mainline in revision 2037.
  • Revision ID: brian@tangent.org-20101224212754-85xvjullymvhibr9
Merge in cast() for BOOLEAN.

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