~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_delete.cc

  • Committer: Mark Atwood
  • Date: 2011-08-11 03:05:03 UTC
  • mfrom: (2385.1.12 refactor4)
  • Revision ID: me@mark.atwood.name-20110811030503-rp9xjihc5x3y0x4q
mergeĀ lp:~olafvdspek/drizzle/refactor4

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
  Multi-table deletes were introduced by Monty and Sinisa
20
20
*/
21
 
#include "config.h"
22
 
#include "drizzled/sql_select.h"
23
 
#include "drizzled/error.h"
24
 
#include "drizzled/probes.h"
25
 
#include "drizzled/sql_parse.h"
26
 
#include "drizzled/sql_base.h"
27
 
#include "drizzled/lock.h"
28
 
#include "drizzled/probes.h"
29
 
#include "drizzled/optimizer/range.h"
30
 
#include "drizzled/records.h"
31
 
#include "drizzled/internal/iocache.h"
32
 
#include "drizzled/transaction_services.h"
33
 
#include "drizzled/filesort.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_parse.h>
 
26
#include <drizzled/sql_base.h>
 
27
#include <drizzled/lock.h>
 
28
#include <drizzled/probes.h>
 
29
#include <drizzled/optimizer/range.h>
 
30
#include <drizzled/records.h>
 
31
#include <drizzled/internal/iocache.h>
 
32
#include <drizzled/transaction_services.h>
 
33
#include <drizzled/filesort.h>
 
34
#include <drizzled/sql_lex.h>
 
35
#include <drizzled/diagnostics_area.h>
 
36
#include <drizzled/statistics_variables.h>
 
37
#include <drizzled/session/transactions.h>
34
38
 
35
 
namespace drizzled
36
 
{
 
39
namespace drizzled {
37
40
 
38
41
/**
39
42
  Implement DELETE SQL word.
52
55
  optimizer::SqlSelect *select= NULL;
53
56
  ReadRecord    info;
54
57
  bool          using_limit=limit != HA_POS_ERROR;
55
 
  bool          transactional_table, const_cond;
56
 
  bool          const_cond_result;
57
58
  ha_rows       deleted= 0;
58
59
  uint32_t usable_index= MAX_KEY;
59
 
  Select_Lex   *select_lex= &session->lex->select_lex;
 
60
  Select_Lex   *select_lex= &session->lex().select_lex;
60
61
  Session::killed_state_t killed_status= Session::NOT_KILLED;
61
62
 
62
63
  if (session->openTablesLock(table_list))
87
88
    tables.table = table;
88
89
    tables.alias = table_list->alias;
89
90
 
90
 
      if (select_lex->setup_ref_array(session, order->elements) ||
91
 
          setup_order(session, select_lex->ref_pointer_array, &tables,
92
 
                    fields, all_fields, (Order*) order->first))
93
 
      {
94
 
        delete select;
95
 
        free_underlaid_joins(session, &session->lex->select_lex);
96
 
        DRIZZLE_DELETE_DONE(1, 0);
 
91
    select_lex->setup_ref_array(session, order->elements);
 
92
    if (setup_order(session, select_lex->ref_pointer_array, &tables, fields, all_fields, (Order*) order->first))
 
93
    {
 
94
      delete select;
 
95
      free_underlaid_joins(session, &session->lex().select_lex);
 
96
      DRIZZLE_DELETE_DONE(1, 0);
97
97
 
98
 
        return true;
99
 
      }
 
98
      return true;
 
99
    }
100
100
  }
101
101
 
102
 
  const_cond= (!conds || conds->const_item());
103
 
 
104
 
  select_lex->no_error= session->lex->ignore;
105
 
 
106
 
  const_cond_result= const_cond && (!conds || conds->val_int());
 
102
  bool const_cond= not conds || conds->const_item();
 
103
 
 
104
  select_lex->no_error= session->lex().ignore;
 
105
 
 
106
  bool const_cond_result= const_cond && (!conds || conds->val_int());
107
107
  if (session->is_error())
108
108
  {
109
109
    /* Error evaluating val_int(). */
110
 
    return(true);
 
110
    return true;
111
111
  }
112
112
 
113
113
  /*
180
180
     * Resetting the Diagnostic area to prevent
181
181
     * lp bug# 439719
182
182
     */
183
 
    session->main_da.reset_diagnostics_area();
 
183
    session->main_da().reset_diagnostics_area();
184
184
    session->my_ok((ha_rows) session->rowCount());
185
185
    /*
186
186
      We don't need to call reset_auto_increment in this case, because
198
198
 
199
199
  if (order && order->elements)
200
200
  {
201
 
    uint32_t         length= 0;
202
 
    SortField  *sortorder;
203
 
    ha_rows examined_rows;
204
 
 
205
201
    if ((!select || table->quick_keys.none()) && limit != HA_POS_ERROR)
206
202
      usable_index= optimizer::get_index_for_order(table, (Order*)(order->first), limit);
207
203
 
208
204
    if (usable_index == MAX_KEY)
209
205
    {
210
206
      FileSort filesort(*session);
211
 
      table->sort.io_cache= new internal::IO_CACHE;
212
 
 
213
 
 
214
 
      if (not (sortorder= make_unireg_sortorder((Order*) order->first, &length, NULL)) ||
215
 
          (table->sort.found_records = filesort.run(table, sortorder, length,
216
 
                                                    select, HA_POS_ERROR, 1,
217
 
                                                    examined_rows)) == HA_POS_ERROR)
 
207
      table->sort.io_cache= new internal::io_cache_st;
 
208
      uint32_t length= 0;
 
209
      SortField* sortorder= make_unireg_sortorder((Order*) order->first, &length, NULL);
 
210
      ha_rows examined_rows;
 
211
      if ((table->sort.found_records= filesort.run(table, sortorder, length, select, HA_POS_ERROR, 1, examined_rows)) == HA_POS_ERROR)
218
212
      {
219
213
        delete select;
220
 
        free_underlaid_joins(session, &session->lex->select_lex);
 
214
        free_underlaid_joins(session, &session->lex().select_lex);
221
215
 
222
216
        DRIZZLE_DELETE_DONE(1, 0);
223
217
        return true;
324
318
  }
325
319
 
326
320
  delete select;
327
 
  transactional_table= table->cursor->has_transactions();
 
321
  bool transactional_table= table->cursor->has_transactions();
328
322
 
329
323
  if (!transactional_table && deleted > 0)
330
324
    session->transaction.stmt.markModifiedNonTransData();
339
333
  free_underlaid_joins(session, select_lex);
340
334
 
341
335
  DRIZZLE_DELETE_DONE((error >= 0 || session->is_error()), deleted);
342
 
  if (error < 0 || (session->lex->ignore && !session->is_fatal_error))
 
336
  if (error < 0 || (session->lex().ignore && !session->is_fatal_error))
343
337
  {
344
338
    session->row_count_func= deleted;
345
339
    /**
346
340
     * Resetting the Diagnostic area to prevent
347
341
     * lp bug# 439719
348
342
     */
349
 
    session->main_da.reset_diagnostics_area();    
 
343
    session->main_da().reset_diagnostics_area();
350
344
    session->my_ok((ha_rows) session->rowCount());
351
345
  }
352
346
  session->status_var.deleted_row_count+= deleted;
370
364
*/
371
365
int prepare_delete(Session *session, TableList *table_list, Item **conds)
372
366
{
373
 
  Select_Lex *select_lex= &session->lex->select_lex;
 
367
  Select_Lex *select_lex= &session->lex().select_lex;
 
368
  session->lex().allow_sum_func= 0;
 
369
  if (setup_tables_and_check_access(session, &session->lex().select_lex.context, &select_lex->top_join_list, 
 
370
    table_list, &select_lex->leaf_tables, false) ||
 
371
      session->setup_conds(table_list, conds))
 
372
    return true;
374
373
 
 
374
  if (unique_table(table_list, table_list->next_global))
 
375
  {
 
376
    my_error(ER_UPDATE_TABLE_USED, MYF(0), table_list->alias);
 
377
    return true;
 
378
  }
375
379
  List<Item> all_fields;
376
 
 
377
 
  session->lex->allow_sum_func= 0;
378
 
  if (setup_tables_and_check_access(session, &session->lex->select_lex.context,
379
 
                                    &session->lex->select_lex.top_join_list,
380
 
                                    table_list,
381
 
                                    &select_lex->leaf_tables, false) ||
382
 
      session->setup_conds(table_list, conds))
383
 
    return(true);
384
 
  {
385
 
    TableList *duplicate;
386
 
    if ((duplicate= unique_table(table_list, table_list->next_global)))
387
 
    {
388
 
      my_error(ER_UPDATE_TABLE_USED, MYF(0), table_list->alias);
389
 
      return(true);
390
 
    }
391
 
  }
392
 
 
393
 
  if (select_lex->inner_refs_list.elements &&
394
 
    fix_inner_refs(session, all_fields, select_lex, select_lex->ref_pointer_array))
395
 
    return(true);
396
 
 
397
 
  return(false);
 
380
  if (select_lex->inner_refs_list.size() && fix_inner_refs(session, all_fields, select_lex, select_lex->ref_pointer_array))
 
381
    return true;
 
382
  return false;
398
383
}
399
384
 
400
385
 
409
394
 
410
395
bool truncate(Session& session, TableList *table_list)
411
396
{
412
 
  bool error;
413
 
  TransactionServices &transaction_services= TransactionServices::singleton();
414
 
 
415
397
  uint64_t save_options= session.options;
416
398
  table_list->lock_type= TL_WRITE;
417
399
  session.options&= ~(OPTION_BEGIN | OPTION_NOT_AUTOCOMMIT);
418
 
  init_select(session.lex);
419
 
  error= delete_query(&session, table_list, (COND*) 0, (SQL_LIST*) 0,
420
 
                      HA_POS_ERROR, 0L, true);
 
400
  init_select(&session.lex());
 
401
  int error= delete_query(&session, table_list, (COND*) 0, (SQL_LIST*) 0, HA_POS_ERROR, 0L, true);
421
402
  /*
422
403
    Safety, in case the engine ignored ha_enable_transaction(false)
423
404
    above. Also clears session->transaction.*.
424
405
  */
425
 
  error= transaction_services.autocommitOrRollback(session, error);
 
406
  error= TransactionServices::autocommitOrRollback(session, error);
426
407
  session.options= save_options;
427
 
 
428
408
  return error;
429
409
}
430
410