~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_delete.cc

  • Committer: Monty Taylor
  • Date: 2010-11-08 18:26:08 UTC
  • mto: This revision was merged to the branch mainline in revision 1931.
  • Revision ID: mordred@inaugust.com-20101108182608-lci86acl7r53sbi3
Replaced auto_ptr with scoped_ptr.

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
34
 
35
35
namespace drizzled
36
36
{
43
43
  end of dispatch_command().
44
44
*/
45
45
 
46
 
bool delete_query(Session *session, TableList *table_list, COND *conds,
 
46
bool mysql_delete(Session *session, TableList *table_list, COND *conds,
47
47
                  SQL_LIST *order, ha_rows limit, uint64_t,
48
48
                  bool reset_auto_increment)
49
49
{
56
56
  bool          const_cond_result;
57
57
  ha_rows       deleted= 0;
58
58
  uint32_t usable_index= MAX_KEY;
59
 
  Select_Lex   *select_lex= &session->getLex()->select_lex;
 
59
  Select_Lex   *select_lex= &session->lex->select_lex;
60
60
  Session::killed_state_t killed_status= Session::NOT_KILLED;
61
61
 
62
62
  if (session->openTablesLock(table_list))
71
71
  session->set_proc_info("init");
72
72
  table->map=1;
73
73
 
74
 
  if (prepare_delete(session, table_list, &conds))
 
74
  if (mysql_prepare_delete(session, table_list, &conds))
75
75
  {
76
76
    DRIZZLE_DELETE_DONE(1, 0);
77
77
    return true;
92
92
                    fields, all_fields, (Order*) order->first))
93
93
      {
94
94
        delete select;
95
 
        free_underlaid_joins(session, &session->getLex()->select_lex);
 
95
        free_underlaid_joins(session, &session->lex->select_lex);
96
96
        DRIZZLE_DELETE_DONE(1, 0);
97
97
 
98
98
        return true;
101
101
 
102
102
  const_cond= (!conds || conds->const_item());
103
103
 
104
 
  select_lex->no_error= session->getLex()->ignore;
 
104
  select_lex->no_error= session->lex->ignore;
105
105
 
106
106
  const_cond_result= const_cond && (!conds || conds->val_int());
107
107
  if (session->is_error())
173
173
    delete select;
174
174
    free_underlaid_joins(session, select_lex);
175
175
    session->row_count_func= 0;
176
 
    if (session->is_error())
177
 
      return true;
178
176
    DRIZZLE_DELETE_DONE(0, 0);
179
177
    /**
180
178
     * Resetting the Diagnostic area to prevent
181
179
     * lp bug# 439719
182
180
     */
183
181
    session->main_da.reset_diagnostics_area();
184
 
    session->my_ok((ha_rows) session->rowCount());
 
182
    session->my_ok((ha_rows) session->row_count_func);
185
183
    /*
186
184
      We don't need to call reset_auto_increment in this case, because
187
185
      mysql_truncate always gives a NULL conds argument, hence we never
217
215
                                                    examined_rows)) == HA_POS_ERROR)
218
216
      {
219
217
        delete select;
220
 
        free_underlaid_joins(session, &session->getLex()->select_lex);
 
218
        free_underlaid_joins(session, &session->lex->select_lex);
221
219
 
222
220
        DRIZZLE_DELETE_DONE(1, 0);
223
221
        return true;
243
241
 
244
242
  if (usable_index==MAX_KEY)
245
243
  {
246
 
    if ((error= info.init_read_record(session,table,select,1,1)))
247
 
    {
248
 
      table->print_error(error, MYF(0));
249
 
      delete select;
250
 
      free_underlaid_joins(session, select_lex);
251
 
      return true;
252
 
    }
 
244
    info.init_read_record(session,table,select,1,1);
253
245
  }
254
246
  else
255
247
  {
256
 
    if ((error= info.init_read_record_idx(session, table, 1, usable_index)))
257
 
    {
258
 
      table->print_error(error, MYF(0));
259
 
      delete select;
260
 
      free_underlaid_joins(session, select_lex);
261
 
      return true;
262
 
    }
 
248
    info.init_read_record_idx(session, table, 1, usable_index);
263
249
  }
264
250
 
265
251
  session->set_proc_info("updating");
339
325
  free_underlaid_joins(session, select_lex);
340
326
 
341
327
  DRIZZLE_DELETE_DONE((error >= 0 || session->is_error()), deleted);
342
 
  if (error < 0 || (session->getLex()->ignore && !session->is_fatal_error))
 
328
  if (error < 0 || (session->lex->ignore && !session->is_fatal_error))
343
329
  {
344
330
    session->row_count_func= deleted;
345
331
    /**
347
333
     * lp bug# 439719
348
334
     */
349
335
    session->main_da.reset_diagnostics_area();    
350
 
    session->my_ok((ha_rows) session->rowCount());
 
336
    session->my_ok((ha_rows) session->row_count_func);
351
337
  }
352
338
  session->status_var.deleted_row_count+= deleted;
353
339
 
359
345
  Prepare items in DELETE statement
360
346
 
361
347
  SYNOPSIS
362
 
    prepare_delete()
 
348
    mysql_prepare_delete()
363
349
    session                     - thread handler
364
350
    table_list          - global/local table list
365
351
    conds               - conditions
368
354
    false OK
369
355
    true  error
370
356
*/
371
 
int prepare_delete(Session *session, TableList *table_list, Item **conds)
 
357
int mysql_prepare_delete(Session *session, TableList *table_list, Item **conds)
372
358
{
373
 
  Select_Lex *select_lex= &session->getLex()->select_lex;
 
359
  Select_Lex *select_lex= &session->lex->select_lex;
374
360
 
375
361
  List<Item> all_fields;
376
362
 
377
 
  session->getLex()->allow_sum_func= 0;
378
 
  if (setup_tables_and_check_access(session, &session->getLex()->select_lex.context,
379
 
                                    &session->getLex()->select_lex.top_join_list,
 
363
  session->lex->allow_sum_func= 0;
 
364
  if (setup_tables_and_check_access(session, &session->lex->select_lex.context,
 
365
                                    &session->lex->select_lex.top_join_list,
380
366
                                    table_list,
381
367
                                    &select_lex->leaf_tables, false) ||
382
368
      session->setup_conds(table_list, conds))
392
378
 
393
379
  if (select_lex->inner_refs_list.elements &&
394
380
    fix_inner_refs(session, all_fields, select_lex, select_lex->ref_pointer_array))
395
 
    return(true);
 
381
    return(-1);
396
382
 
397
383
  return(false);
398
384
}
407
393
  This will work even if the .ISM and .ISD tables are destroyed
408
394
*/
409
395
 
410
 
bool truncate(Session& session, TableList *table_list)
 
396
bool mysql_truncate(Session& session, TableList *table_list)
411
397
{
412
398
  bool error;
413
399
  TransactionServices &transaction_services= TransactionServices::singleton();
415
401
  uint64_t save_options= session.options;
416
402
  table_list->lock_type= TL_WRITE;
417
403
  session.options&= ~(OPTION_BEGIN | OPTION_NOT_AUTOCOMMIT);
418
 
  init_select(session.getLex());
419
 
  error= delete_query(&session, table_list, (COND*) 0, (SQL_LIST*) 0,
 
404
  mysql_init_select(session.lex);
 
405
  error= mysql_delete(&session, table_list, (COND*) 0, (SQL_LIST*) 0,
420
406
                      HA_POS_ERROR, 0L, true);
421
407
  /*
422
408
    Safety, in case the engine ignored ha_enable_transaction(false)
423
409
    above. Also clears session->transaction.*.
424
410
  */
425
 
  error= transaction_services.autocommitOrRollback(session, error);
 
411
  error= transaction_services.autocommitOrRollback(&session, error);
426
412
  session.options= save_options;
427
413
 
428
414
  return error;