~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_delete.cc

  • Committer: Stewart Smith
  • Date: 2010-11-03 03:30:27 UTC
  • mto: (1902.1.1 build) (1910.1.2 build)
  • mto: This revision was merged to the branch mainline in revision 1903.
  • Revision ID: stewart@flamingspork.com-20101103033027-lskb6gxwwforfz71
fix docs warning: underline/overline too short for replace.rst

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"
34
33
 
35
34
namespace drizzled
36
35
{
43
42
  end of dispatch_command().
44
43
*/
45
44
 
46
 
bool delete_query(Session *session, TableList *table_list, COND *conds,
 
45
bool mysql_delete(Session *session, TableList *table_list, COND *conds,
47
46
                  SQL_LIST *order, ha_rows limit, uint64_t,
48
47
                  bool reset_auto_increment)
49
48
{
56
55
  bool          const_cond_result;
57
56
  ha_rows       deleted= 0;
58
57
  uint32_t usable_index= MAX_KEY;
59
 
  Select_Lex   *select_lex= &session->getLex()->select_lex;
60
 
  Session::killed_state_t killed_status= Session::NOT_KILLED;
 
58
  Select_Lex   *select_lex= &session->lex->select_lex;
 
59
  Session::killed_state killed_status= Session::NOT_KILLED;
61
60
 
62
61
  if (session->openTablesLock(table_list))
63
62
  {
71
70
  session->set_proc_info("init");
72
71
  table->map=1;
73
72
 
74
 
  if (prepare_delete(session, table_list, &conds))
 
73
  if (mysql_prepare_delete(session, table_list, &conds))
75
74
  {
76
75
    DRIZZLE_DELETE_DONE(1, 0);
77
76
    return true;
92
91
                    fields, all_fields, (Order*) order->first))
93
92
      {
94
93
        delete select;
95
 
        free_underlaid_joins(session, &session->getLex()->select_lex);
 
94
        free_underlaid_joins(session, &session->lex->select_lex);
96
95
        DRIZZLE_DELETE_DONE(1, 0);
97
96
 
98
97
        return true;
101
100
 
102
101
  const_cond= (!conds || conds->const_item());
103
102
 
104
 
  select_lex->no_error= session->getLex()->ignore;
 
103
  select_lex->no_error= session->lex->ignore;
105
104
 
106
105
  const_cond_result= const_cond && (!conds || conds->val_int());
107
106
  if (session->is_error())
173
172
    delete select;
174
173
    free_underlaid_joins(session, select_lex);
175
174
    session->row_count_func= 0;
176
 
    if (session->is_error())
177
 
      return true;
178
175
    DRIZZLE_DELETE_DONE(0, 0);
179
176
    /**
180
177
     * Resetting the Diagnostic area to prevent
181
178
     * lp bug# 439719
182
179
     */
183
180
    session->main_da.reset_diagnostics_area();
184
 
    session->my_ok((ha_rows) session->rowCount());
 
181
    session->my_ok((ha_rows) session->row_count_func);
185
182
    /*
186
183
      We don't need to call reset_auto_increment in this case, because
187
184
      mysql_truncate always gives a NULL conds argument, hence we never
207
204
 
208
205
    if (usable_index == MAX_KEY)
209
206
    {
210
 
      FileSort filesort(*session);
211
207
      table->sort.io_cache= new internal::IO_CACHE;
212
208
 
213
209
 
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)
 
210
      if (!(sortorder= make_unireg_sortorder((Order*) order->first,
 
211
                                             &length, NULL)) ||
 
212
          (table->sort.found_records = filesort(session, table, sortorder, length,
 
213
                                                select, HA_POS_ERROR, 1,
 
214
                                                &examined_rows))
 
215
          == 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");
266
252
 
267
253
  table->mark_columns_needed_for_delete();
268
254
 
269
 
  while (!(error=info.read_record(&info)) && !session->getKilled() &&
 
255
  while (!(error=info.read_record(&info)) && !session->killed &&
270
256
         ! session->is_error())
271
257
  {
272
258
    // session->is_error() is tested to disallow delete row on error
299
285
    else
300
286
      table->cursor->unlock_row();  // Row failed selection, release lock on it
301
287
  }
302
 
  killed_status= session->getKilled();
 
288
  killed_status= session->killed;
303
289
  if (killed_status != Session::NOT_KILLED || session->is_error())
304
290
    error= 1;                                   // Aborted
305
291
 
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;