~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_delete.cc

  • Committer: lbieber
  • Date: 2010-09-11 16:33:45 UTC
  • mfrom: (1757.1.2 build)
  • Revision ID: lbieber@orisndriz03-20100911163345-na1t8m18at9thsjl
Merge Vijay - added utf 8 tamil test case suite and test case for creating a database in tamil
Merge Brian - Small set of refactoring (includes one case of memset on a table object).

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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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"
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
{
57
56
  ha_rows       deleted= 0;
58
57
  uint32_t usable_index= MAX_KEY;
59
58
  Select_Lex   *select_lex= &session->lex->select_lex;
60
 
  Session::killed_state_t killed_status= Session::NOT_KILLED;
 
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))
75
 
  {
76
 
    DRIZZLE_DELETE_DONE(1, 0);
77
 
    return true;
78
 
  }
 
73
  if (mysql_prepare_delete(session, table_list, &conds))
 
74
    goto err;
79
75
 
80
76
  /* check ORDER BY even if it can be ignored */
81
77
  if (order && order->elements)
89
85
 
90
86
      if (select_lex->setup_ref_array(session, order->elements) ||
91
87
          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);
97
 
 
98
 
        return true;
99
 
      }
 
88
                    fields, all_fields, (order_st*) order->first))
 
89
    {
 
90
      delete select;
 
91
      free_underlaid_joins(session, &session->lex->select_lex);
 
92
      goto err;
 
93
    }
100
94
  }
101
95
 
102
96
  const_cond= (!conds || conds->const_item());
163
157
  table->quick_keys.reset();            // Can't use 'only index'
164
158
  select= optimizer::make_select(table, 0, 0, conds, 0, &error);
165
159
  if (error)
166
 
  {
167
 
    DRIZZLE_DELETE_DONE(1, 0);
168
 
    return true;
169
 
  }
170
 
 
 
160
    goto err;
171
161
  if ((select && select->check_quick(session, false, limit)) || !limit)
172
162
  {
173
163
    delete select;
174
164
    free_underlaid_joins(session, select_lex);
175
165
    session->row_count_func= 0;
176
 
    if (session->is_error())
177
 
      return true;
178
166
    DRIZZLE_DELETE_DONE(0, 0);
179
167
    /**
180
168
     * Resetting the Diagnostic area to prevent
181
169
     * lp bug# 439719
182
170
     */
183
171
    session->main_da.reset_diagnostics_area();
184
 
    session->my_ok((ha_rows) session->rowCount());
 
172
    session->my_ok((ha_rows) session->row_count_func);
185
173
    /*
186
174
      We don't need to call reset_auto_increment in this case, because
187
175
      mysql_truncate always gives a NULL conds argument, hence we never
203
191
    ha_rows examined_rows;
204
192
 
205
193
    if ((!select || table->quick_keys.none()) && limit != HA_POS_ERROR)
206
 
      usable_index= optimizer::get_index_for_order(table, (Order*)(order->first), limit);
 
194
      usable_index= optimizer::get_index_for_order(table, (order_st*)(order->first), limit);
207
195
 
208
196
    if (usable_index == MAX_KEY)
209
197
    {
210
 
      FileSort filesort(*session);
211
198
      table->sort.io_cache= new internal::IO_CACHE;
212
199
 
213
200
 
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)
 
201
      if (!(sortorder= make_unireg_sortorder((order_st*) order->first,
 
202
                                             &length, NULL)) ||
 
203
          (table->sort.found_records = filesort(session, table, sortorder, length,
 
204
                                                select, HA_POS_ERROR, 1,
 
205
                                                &examined_rows))
 
206
          == HA_POS_ERROR)
218
207
      {
219
208
        delete select;
220
209
        free_underlaid_joins(session, &session->lex->select_lex);
221
 
 
222
 
        DRIZZLE_DELETE_DONE(1, 0);
223
 
        return true;
 
210
        goto err;
224
211
      }
225
212
      /*
226
213
        Filesort has already found and selected the rows we want to delete,
237
224
  {
238
225
    delete select;
239
226
    free_underlaid_joins(session, select_lex);
240
 
    DRIZZLE_DELETE_DONE(1, 0);
241
 
    return true;
 
227
    goto err;
242
228
  }
243
229
 
244
230
  if (usable_index==MAX_KEY)
245
231
  {
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
 
    }
 
232
    info.init_read_record(session,table,select,1,1);
253
233
  }
254
234
  else
255
235
  {
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
 
    }
 
236
    info.init_read_record_idx(session, table, 1, usable_index);
263
237
  }
264
238
 
265
239
  session->set_proc_info("updating");
266
240
 
267
241
  table->mark_columns_needed_for_delete();
268
242
 
269
 
  while (!(error=info.read_record(&info)) && !session->getKilled() &&
 
243
  while (!(error=info.read_record(&info)) && !session->killed &&
270
244
         ! session->is_error())
271
245
  {
272
246
    // session->is_error() is tested to disallow delete row on error
299
273
    else
300
274
      table->cursor->unlock_row();  // Row failed selection, release lock on it
301
275
  }
302
 
  killed_status= session->getKilled();
 
276
  killed_status= session->killed;
303
277
  if (killed_status != Session::NOT_KILLED || session->is_error())
304
278
    error= 1;                                   // Aborted
305
279
 
347
321
     * lp bug# 439719
348
322
     */
349
323
    session->main_da.reset_diagnostics_area();    
350
 
    session->my_ok((ha_rows) session->rowCount());
 
324
    session->my_ok((ha_rows) session->row_count_func);
351
325
  }
352
326
  session->status_var.deleted_row_count+= deleted;
353
 
 
354
327
  return (error >= 0 || session->is_error());
 
328
 
 
329
err:
 
330
  DRIZZLE_DELETE_DONE(1, 0);
 
331
  return true;
355
332
}
356
333
 
357
334
 
359
336
  Prepare items in DELETE statement
360
337
 
361
338
  SYNOPSIS
362
 
    prepare_delete()
 
339
    mysql_prepare_delete()
363
340
    session                     - thread handler
364
341
    table_list          - global/local table list
365
342
    conds               - conditions
368
345
    false OK
369
346
    true  error
370
347
*/
371
 
int prepare_delete(Session *session, TableList *table_list, Item **conds)
 
348
int mysql_prepare_delete(Session *session, TableList *table_list, Item **conds)
372
349
{
373
350
  Select_Lex *select_lex= &session->lex->select_lex;
374
351
 
392
369
 
393
370
  if (select_lex->inner_refs_list.elements &&
394
371
    fix_inner_refs(session, all_fields, select_lex, select_lex->ref_pointer_array))
395
 
    return(true);
 
372
    return(-1);
396
373
 
397
374
  return(false);
398
375
}
407
384
  This will work even if the .ISM and .ISD tables are destroyed
408
385
*/
409
386
 
410
 
bool truncate(Session& session, TableList *table_list)
 
387
bool mysql_truncate(Session& session, TableList *table_list)
411
388
{
412
389
  bool error;
413
390
  TransactionServices &transaction_services= TransactionServices::singleton();
415
392
  uint64_t save_options= session.options;
416
393
  table_list->lock_type= TL_WRITE;
417
394
  session.options&= ~(OPTION_BEGIN | OPTION_NOT_AUTOCOMMIT);
418
 
  init_select(session.lex);
419
 
  error= delete_query(&session, table_list, (COND*) 0, (SQL_LIST*) 0,
 
395
  mysql_init_select(session.lex);
 
396
  error= mysql_delete(&session, table_list, (COND*) 0, (SQL_LIST*) 0,
420
397
                      HA_POS_ERROR, 0L, true);
421
398
  /*
422
399
    Safety, in case the engine ignored ha_enable_transaction(false)
423
400
    above. Also clears session->transaction.*.
424
401
  */
425
 
  error= transaction_services.autocommitOrRollback(session, error);
 
402
  error= transaction_services.autocommitOrRollback(&session, error);
426
403
  session.options= save_options;
427
404
 
428
405
  return error;