~ubuntu-branches/ubuntu/trusty/drizzle/trusty

« back to all changes in this revision

Viewing changes to drizzled/sql_delete.cc

  • Committer: Package Import Robot
  • Author(s): Clint Byrum
  • Date: 2012-06-19 10:46:49 UTC
  • mfrom: (1.1.6)
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20120619104649-e2l0ggd4oz3um0f4
Tags: upstream-7.1.36-stable
ImportĀ upstreamĀ versionĀ 7.1.36-stable

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
#include <drizzled/transaction_services.h>
33
33
#include <drizzled/filesort.h>
34
34
#include <drizzled/sql_lex.h>
 
35
#include <drizzled/diagnostics_area.h>
 
36
#include <drizzled/statistics_variables.h>
 
37
#include <drizzled/session/transactions.h>
35
38
 
36
 
namespace drizzled
37
 
{
 
39
namespace drizzled {
38
40
 
39
41
/**
40
42
  Implement DELETE SQL word.
53
55
  optimizer::SqlSelect *select= NULL;
54
56
  ReadRecord    info;
55
57
  bool          using_limit=limit != HA_POS_ERROR;
56
 
  bool          transactional_table, const_cond;
57
 
  bool          const_cond_result;
58
58
  ha_rows       deleted= 0;
59
59
  uint32_t usable_index= MAX_KEY;
60
60
  Select_Lex   *select_lex= &session->lex().select_lex;
88
88
    tables.table = table;
89
89
    tables.alias = table_list->alias;
90
90
 
91
 
      if (select_lex->setup_ref_array(session, order->elements) ||
92
 
          setup_order(session, select_lex->ref_pointer_array, &tables,
93
 
                    fields, all_fields, (Order*) order->first))
94
 
      {
95
 
        delete select;
96
 
        free_underlaid_joins(session, &session->lex().select_lex);
97
 
        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);
98
97
 
99
 
        return true;
100
 
      }
 
98
      return true;
 
99
    }
101
100
  }
102
101
 
103
 
  const_cond= (!conds || conds->const_item());
 
102
  bool const_cond= not conds || conds->const_item();
104
103
 
105
104
  select_lex->no_error= session->lex().ignore;
106
105
 
107
 
  const_cond_result= const_cond && (!conds || conds->val_int());
 
106
  bool const_cond_result= const_cond && (!conds || conds->val_int());
108
107
  if (session->is_error())
109
108
  {
110
109
    /* Error evaluating val_int(). */
111
 
    return(true);
 
110
    return true;
112
111
  }
113
112
 
114
113
  /*
125
124
    - There is no limit clause
126
125
    - The condition is constant
127
126
    - If there is a condition, then it it produces a non-zero value
128
 
    - If the current command is DELETE FROM with no where clause
129
 
      (i.e., not TRUNCATE) then:
130
 
      - We should not be binlogging this statement row-based, and
131
 
      - there should be no delete triggers associated with the table.
132
127
  */
133
128
  if (!using_limit && const_cond_result)
134
129
  {
181
176
     * Resetting the Diagnostic area to prevent
182
177
     * lp bug# 439719
183
178
     */
184
 
    session->main_da.reset_diagnostics_area();
 
179
    session->main_da().reset_diagnostics_area();
185
180
    session->my_ok((ha_rows) session->rowCount());
186
181
    /*
187
182
      We don't need to call reset_auto_increment in this case, because
199
194
 
200
195
  if (order && order->elements)
201
196
  {
202
 
    uint32_t         length= 0;
203
 
    SortField  *sortorder;
204
 
    ha_rows examined_rows;
205
 
 
206
197
    if ((!select || table->quick_keys.none()) && limit != HA_POS_ERROR)
207
198
      usable_index= optimizer::get_index_for_order(table, (Order*)(order->first), limit);
208
199
 
209
200
    if (usable_index == MAX_KEY)
210
201
    {
211
202
      FileSort filesort(*session);
212
 
      table->sort.io_cache= new internal::IO_CACHE;
213
 
 
214
 
 
215
 
      if (not (sortorder= make_unireg_sortorder((Order*) order->first, &length, NULL)) ||
216
 
          (table->sort.found_records = filesort.run(table, sortorder, length,
217
 
                                                    select, HA_POS_ERROR, 1,
218
 
                                                    examined_rows)) == HA_POS_ERROR)
 
203
      table->sort.io_cache= new internal::io_cache_st;
 
204
      uint32_t length= 0;
 
205
      SortField* sortorder= make_unireg_sortorder((Order*) order->first, &length, NULL);
 
206
      ha_rows examined_rows;
 
207
      if ((table->sort.found_records= filesort.run(table, sortorder, length, select, HA_POS_ERROR, 1, examined_rows)) == HA_POS_ERROR)
219
208
      {
220
209
        delete select;
221
210
        free_underlaid_joins(session, &session->lex().select_lex);
285
274
      else
286
275
      {
287
276
        table->print_error(error,MYF(0));
288
 
        /*
289
 
          In < 4.0.14 we set the error number to 0 here, but that
290
 
          was not sensible, because then MySQL would not roll back the
291
 
          failed DELETE, and also wrote it to the binlog. For MyISAM
292
 
          tables a DELETE probably never should fail (?), but for
293
 
          InnoDB it can fail in a FOREIGN KEY error or an
294
 
          out-of-tablespace error.
295
 
        */
296
277
        error= 1;
297
278
        break;
298
279
      }
325
306
  }
326
307
 
327
308
  delete select;
328
 
  transactional_table= table->cursor->has_transactions();
 
309
  bool transactional_table= table->cursor->has_transactions();
329
310
 
330
311
  if (!transactional_table && deleted > 0)
331
312
    session->transaction.stmt.markModifiedNonTransData();
332
313
 
333
 
  /* See similar binlogging code in sql_update.cc, for comments */
334
314
  if ((error < 0) || session->transaction.stmt.hasModifiedNonTransData())
335
315
  {
336
316
    if (session->transaction.stmt.hasModifiedNonTransData())
347
327
     * Resetting the Diagnostic area to prevent
348
328
     * lp bug# 439719
349
329
     */
350
 
    session->main_da.reset_diagnostics_area();
 
330
    session->main_da().reset_diagnostics_area();
351
331
    session->my_ok((ha_rows) session->rowCount());
352
332
  }
353
333
  session->status_var.deleted_row_count+= deleted;
372
352
int prepare_delete(Session *session, TableList *table_list, Item **conds)
373
353
{
374
354
  Select_Lex *select_lex= &session->lex().select_lex;
375
 
 
376
 
  List<Item> all_fields;
377
 
 
378
355
  session->lex().allow_sum_func= 0;
379
 
  if (setup_tables_and_check_access(session, &session->lex().select_lex.context,
380
 
                                    &session->lex().select_lex.top_join_list,
381
 
                                    table_list,
382
 
                                    &select_lex->leaf_tables, false) ||
 
356
  if (setup_tables_and_check_access(session, &session->lex().select_lex.context, &select_lex->top_join_list, 
 
357
    table_list, &select_lex->leaf_tables, false) ||
383
358
      session->setup_conds(table_list, conds))
384
 
    return(true);
 
359
    return true;
 
360
 
 
361
  if (unique_table(table_list, table_list->next_global))
385
362
  {
386
 
    TableList *duplicate;
387
 
    if ((duplicate= unique_table(table_list, table_list->next_global)))
388
 
    {
389
 
      my_error(ER_UPDATE_TABLE_USED, MYF(0), table_list->alias);
390
 
      return(true);
391
 
    }
 
363
    my_error(ER_UPDATE_TABLE_USED, MYF(0), table_list->alias);
 
364
    return true;
392
365
  }
393
 
 
394
 
  if (select_lex->inner_refs_list.size() &&
395
 
    fix_inner_refs(session, all_fields, select_lex, select_lex->ref_pointer_array))
396
 
    return(true);
397
 
 
398
 
  return(false);
 
366
  List<Item> all_fields;
 
367
  if (select_lex->inner_refs_list.size() && fix_inner_refs(session, all_fields, select_lex, select_lex->ref_pointer_array))
 
368
    return true;
 
369
  return false;
399
370
}
400
371
 
401
372
 
410
381
 
411
382
bool truncate(Session& session, TableList *table_list)
412
383
{
413
 
  bool error;
414
 
  TransactionServices &transaction_services= TransactionServices::singleton();
415
 
 
416
384
  uint64_t save_options= session.options;
417
385
  table_list->lock_type= TL_WRITE;
418
386
  session.options&= ~(OPTION_BEGIN | OPTION_NOT_AUTOCOMMIT);
419
387
  init_select(&session.lex());
420
 
  error= delete_query(&session, table_list, (COND*) 0, (SQL_LIST*) 0,
421
 
                      HA_POS_ERROR, 0L, true);
 
388
  int error= delete_query(&session, table_list, (COND*) 0, (SQL_LIST*) 0, HA_POS_ERROR, 0L, true);
422
389
  /*
423
390
    Safety, in case the engine ignored ha_enable_transaction(false)
424
391
    above. Also clears session->transaction.*.
425
392
  */
426
 
  error= transaction_services.autocommitOrRollback(session, error);
 
393
  error= TransactionServices::autocommitOrRollback(session, error);
427
394
  session.options= save_options;
428
 
 
429
395
  return error;
430
396
}
431
397