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

« back to all changes in this revision

Viewing changes to drizzled/sql_delete.cc

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2010-11-12 12:26:01 UTC
  • mfrom: (1.1.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20101112122601-myppfj3tfmlkccuq
Tags: upstream-2010.11.03
ImportĀ upstreamĀ versionĀ 2010.11.03

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
13
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
14
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
15
15
 
16
16
/*
17
17
  Delete of records and truncate of tables.
30
30
#include "drizzled/records.h"
31
31
#include "drizzled/internal/iocache.h"
32
32
#include "drizzled/transaction_services.h"
 
33
#include "drizzled/filesort.h"
33
34
 
34
35
namespace drizzled
35
36
{
49
50
  int           error;
50
51
  Table         *table;
51
52
  optimizer::SqlSelect *select= NULL;
52
 
  READ_RECORD   info;
 
53
  ReadRecord    info;
53
54
  bool          using_limit=limit != HA_POS_ERROR;
54
55
  bool          transactional_table, const_cond;
55
56
  bool          const_cond_result;
56
57
  ha_rows       deleted= 0;
57
58
  uint32_t usable_index= MAX_KEY;
58
59
  Select_Lex   *select_lex= &session->lex->select_lex;
59
 
  Session::killed_state killed_status= Session::NOT_KILLED;
 
60
  Session::killed_state_t killed_status= Session::NOT_KILLED;
60
61
 
61
62
  if (session->openTablesLock(table_list))
62
63
  {
71
72
  table->map=1;
72
73
 
73
74
  if (mysql_prepare_delete(session, table_list, &conds))
74
 
    goto err;
 
75
  {
 
76
    DRIZZLE_DELETE_DONE(1, 0);
 
77
    return true;
 
78
  }
75
79
 
76
80
  /* check ORDER BY even if it can be ignored */
77
81
  if (order && order->elements)
80
84
    List<Item>   fields;
81
85
    List<Item>   all_fields;
82
86
 
83
 
    memset(&tables, 0, sizeof(tables));
84
87
    tables.table = table;
85
88
    tables.alias = table_list->alias;
86
89
 
87
90
      if (select_lex->setup_ref_array(session, order->elements) ||
88
91
          setup_order(session, select_lex->ref_pointer_array, &tables,
89
 
                    fields, all_fields, (order_st*) order->first))
90
 
    {
91
 
      delete select;
92
 
      free_underlaid_joins(session, &session->lex->select_lex);
93
 
      goto err;
94
 
    }
 
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);
 
97
 
 
98
        return true;
 
99
      }
95
100
  }
96
101
 
97
102
  const_cond= (!conds || conds->const_item());
158
163
  table->quick_keys.reset();            // Can't use 'only index'
159
164
  select= optimizer::make_select(table, 0, 0, conds, 0, &error);
160
165
  if (error)
161
 
    goto err;
 
166
  {
 
167
    DRIZZLE_DELETE_DONE(1, 0);
 
168
    return true;
 
169
  }
 
170
 
162
171
  if ((select && select->check_quick(session, false, limit)) || !limit)
163
172
  {
164
173
    delete select;
188
197
  if (order && order->elements)
189
198
  {
190
199
    uint32_t         length= 0;
191
 
    SORT_FIELD  *sortorder;
 
200
    SortField  *sortorder;
192
201
    ha_rows examined_rows;
193
202
 
194
203
    if ((!select || table->quick_keys.none()) && limit != HA_POS_ERROR)
195
 
      usable_index= optimizer::get_index_for_order(table, (order_st*)(order->first), limit);
 
204
      usable_index= optimizer::get_index_for_order(table, (Order*)(order->first), limit);
196
205
 
197
206
    if (usable_index == MAX_KEY)
198
207
    {
 
208
      FileSort filesort(*session);
199
209
      table->sort.io_cache= new internal::IO_CACHE;
200
 
      memset(table->sort.io_cache, 0, sizeof(internal::IO_CACHE));
201
 
 
202
 
 
203
 
      if (!(sortorder= make_unireg_sortorder((order_st*) order->first,
204
 
                                             &length, NULL)) ||
205
 
          (table->sort.found_records = filesort(session, table, sortorder, length,
206
 
                                                select, HA_POS_ERROR, 1,
207
 
                                                &examined_rows))
208
 
          == HA_POS_ERROR)
 
210
 
 
211
 
 
212
      if (not (sortorder= make_unireg_sortorder((Order*) order->first, &length, NULL)) ||
 
213
          (table->sort.found_records = filesort.run(table, sortorder, length,
 
214
                                                    select, HA_POS_ERROR, 1,
 
215
                                                    examined_rows)) == HA_POS_ERROR)
209
216
      {
210
217
        delete select;
211
218
        free_underlaid_joins(session, &session->lex->select_lex);
212
 
        goto err;
 
219
 
 
220
        DRIZZLE_DELETE_DONE(1, 0);
 
221
        return true;
213
222
      }
214
223
      /*
215
224
        Filesort has already found and selected the rows we want to delete,
226
235
  {
227
236
    delete select;
228
237
    free_underlaid_joins(session, select_lex);
229
 
    goto err;
 
238
    DRIZZLE_DELETE_DONE(1, 0);
 
239
    return true;
230
240
  }
231
241
 
232
242
  if (usable_index==MAX_KEY)
233
 
    init_read_record(&info,session,table,select,1,1);
 
243
  {
 
244
    info.init_read_record(session,table,select,1,1);
 
245
  }
234
246
  else
235
 
    init_read_record_idx(&info, session, table, 1, usable_index);
 
247
  {
 
248
    info.init_read_record_idx(session, table, 1, usable_index);
 
249
  }
236
250
 
237
251
  session->set_proc_info("updating");
238
252
 
239
253
  table->mark_columns_needed_for_delete();
240
254
 
241
 
  while (!(error=info.read_record(&info)) && !session->killed &&
 
255
  while (!(error=info.read_record(&info)) && !session->getKilled() &&
242
256
         ! session->is_error())
243
257
  {
244
258
    // session->is_error() is tested to disallow delete row on error
245
259
    if (!(select && select->skip_record())&& ! session->is_error() )
246
260
    {
247
 
      if (!(error= table->cursor->ha_delete_row(table->record[0])))
 
261
      if (!(error= table->cursor->deleteRecord(table->getInsertRecord())))
248
262
      {
249
263
        deleted++;
250
264
        if (!--limit && using_limit)
271
285
    else
272
286
      table->cursor->unlock_row();  // Row failed selection, release lock on it
273
287
  }
274
 
  killed_status= session->killed;
 
288
  killed_status= session->getKilled();
275
289
  if (killed_status != Session::NOT_KILLED || session->is_error())
276
290
    error= 1;                                   // Aborted
277
291
 
278
292
  session->set_proc_info("end");
279
 
  end_read_record(&info);
 
293
  info.end_read_record();
280
294
 
281
295
cleanup:
282
296
 
321
335
    session->main_da.reset_diagnostics_area();    
322
336
    session->my_ok((ha_rows) session->row_count_func);
323
337
  }
 
338
  session->status_var.deleted_row_count+= deleted;
 
339
 
324
340
  return (error >= 0 || session->is_error());
325
 
 
326
 
err:
327
 
  DRIZZLE_DELETE_DONE(1, 0);
328
 
  return true;
329
341
}
330
342
 
331
343
 
396
408
    Safety, in case the engine ignored ha_enable_transaction(false)
397
409
    above. Also clears session->transaction.*.
398
410
  */
399
 
  error= transaction_services.ha_autocommit_or_rollback(&session, error);
 
411
  error= transaction_services.autocommitOrRollback(&session, error);
400
412
  session.options= save_options;
401
413
 
402
414
  return error;