~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_delete.cc

  • Committer: brian
  • Date: 2009-11-11 19:42:27 UTC
  • mfrom: (1211 staging)
  • mto: (1211.1.4 staging)
  • mto: This revision was merged to the branch mainline in revision 1212.
  • Revision ID: brian@orisndriz04-20091111194227-mky4am3ym0dlosaa
Update for Cursor renaming.

Show diffs side-by-side

added added

removed removed

Lines of Context:
129
129
  */
130
130
  if (!using_limit && const_cond_result)
131
131
  {
132
 
    /* Update the table->file->stats.records number */
133
 
    table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
134
 
    ha_rows const maybe_deleted= table->file->stats.records;
135
 
    if (!(error=table->file->ha_delete_all_rows()))
 
132
    /* Update the table->cursor->stats.records number */
 
133
    table->cursor->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
 
134
    ha_rows const maybe_deleted= table->cursor->stats.records;
 
135
    if (!(error=table->cursor->ha_delete_all_rows()))
136
136
    {
137
137
      error= -1;                                // ok
138
138
      deleted= maybe_deleted;
140
140
    }
141
141
    if (error != HA_ERR_WRONG_COMMAND)
142
142
    {
143
 
      table->file->print_error(error,MYF(0));
 
143
      table->cursor->print_error(error,MYF(0));
144
144
      error=0;
145
145
      goto cleanup;
146
146
    }
154
154
      limit= 0;
155
155
  }
156
156
 
157
 
  /* Update the table->file->stats.records number */
158
 
  table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
 
157
  /* Update the table->cursor->stats.records number */
 
158
  table->cursor->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
159
159
 
160
160
  table->covering_keys.reset();
161
161
  table->quick_keys.reset();            // Can't use 'only index'
196
196
    }
197
197
  }
198
198
  if (options & OPTION_QUICK)
199
 
    (void) table->file->extra(HA_EXTRA_QUICK);
 
199
    (void) table->cursor->extra(HA_EXTRA_QUICK);
200
200
 
201
201
  if (order && order->elements)
202
202
  {
248
248
 
249
249
  session->set_proc_info("updating");
250
250
 
251
 
  will_batch= !table->file->start_bulk_delete();
 
251
  will_batch= !table->cursor->start_bulk_delete();
252
252
 
253
253
 
254
254
  table->mark_columns_needed_for_delete();
259
259
    // session->is_error() is tested to disallow delete row on error
260
260
    if (!(select && select->skip_record())&& ! session->is_error() )
261
261
    {
262
 
      if (!(error= table->file->ha_delete_row(table->record[0])))
 
262
      if (!(error= table->cursor->ha_delete_row(table->record[0])))
263
263
      {
264
264
        deleted++;
265
265
        if (!--limit && using_limit)
270
270
      }
271
271
      else
272
272
      {
273
 
        table->file->print_error(error,MYF(0));
 
273
        table->cursor->print_error(error,MYF(0));
274
274
        /*
275
275
          In < 4.0.14 we set the error number to 0 here, but that
276
276
          was not sensible, because then MySQL would not roll back the
284
284
      }
285
285
    }
286
286
    else
287
 
      table->file->unlock_row();  // Row failed selection, release lock on it
 
287
      table->cursor->unlock_row();  // Row failed selection, release lock on it
288
288
  }
289
289
  killed_status= session->killed;
290
290
  if (killed_status != Session::NOT_KILLED || session->is_error())
291
291
    error= 1;                                   // Aborted
292
 
  if (will_batch && (loc_error= table->file->end_bulk_delete()))
 
292
  if (will_batch && (loc_error= table->cursor->end_bulk_delete()))
293
293
  {
294
294
    if (error != 1)
295
 
      table->file->print_error(loc_error,MYF(0));
 
295
      table->cursor->print_error(loc_error,MYF(0));
296
296
    error=1;
297
297
  }
298
298
  session->set_proc_info("end");
299
299
  end_read_record(&info);
300
300
  if (options & OPTION_QUICK)
301
 
    (void) table->file->extra(HA_EXTRA_NORMAL);
 
301
    (void) table->cursor->extra(HA_EXTRA_NORMAL);
 
302
 
 
303
cleanup:
302
304
 
303
305
  if (reset_auto_increment && (error < 0))
304
306
  {
306
308
      We're really doing a truncate and need to reset the table's
307
309
      auto-increment counter.
308
310
    */
309
 
    int error2= table->file->ha_reset_auto_increment(0);
 
311
    int error2= table->cursor->ha_reset_auto_increment(0);
310
312
 
311
313
    if (error2 && (error2 != HA_ERR_WRONG_COMMAND))
312
314
    {
313
 
      table->file->print_error(error2, MYF(0));
 
315
      table->cursor->print_error(error2, MYF(0));
314
316
      error= 1;
315
317
    }
316
318
  }
317
319
 
318
 
cleanup:
319
 
 
320
320
  delete select;
321
 
  transactional_table= table->file->has_transactions();
 
321
  transactional_table= table->cursor->has_transactions();
322
322
 
323
323
  if (!transactional_table && deleted > 0)
324
324
    session->transaction.stmt.modified_non_trans_table= true;
401
401
/*
402
402
  Optimize delete of all rows by doing a full generate of the table
403
403
  This will work even if the .ISM and .ISD tables are destroyed
404
 
 
405
 
  dont_send_ok should be set if:
406
 
  - We should always wants to generate the table (even if the table type
407
 
    normally can't safely do this.
408
 
  - We don't want an ok to be sent to the end user.
409
 
  - We don't want to log the truncate command
410
 
  - If we want to have a name lock on the table on exit without errors.
411
404
*/
412
405
 
413
 
bool mysql_truncate(Session& session, TableList *table_list, bool dont_send_ok)
 
406
bool mysql_truncate(Session& session, TableList *table_list)
414
407
{
415
 
  HA_CREATE_INFO create_info;
416
 
  char path[FN_REFLEN];
417
 
  Table *table;
418
408
  bool error;
419
 
  uint32_t path_length;
420
 
  message::Table tmp_table;
421
 
 
422
 
 
423
 
  memset(&create_info, 0, sizeof(create_info));
424
 
  /* If it is a temporary table, close and regenerate it */
425
 
  if (!dont_send_ok && (table= session.find_temporary_table(table_list)))
426
 
  {
427
 
    message::Table::StorageEngine *engine= tmp_table.mutable_engine();
428
 
    plugin::StorageEngine *table_type= table->s->db_type();
429
 
    TableShare *share= table->s;
430
 
 
431
 
    if (!table_type->check_flag(HTON_BIT_CAN_RECREATE))
432
 
      goto trunc_by_del;
433
 
 
434
 
    table->file->info(HA_STATUS_AUTO | HA_STATUS_NO_LOCK);
435
 
 
436
 
    /*
437
 
      Because we bypass "all" of what it takes to create a Table we are on shakey ground in this
438
 
      execution path. Not everything will be properly created in order to use the table
439
 
      we create here. 
440
 
 
441
 
      Painful.
442
 
    */
443
 
    assert(share->storage_engine);
444
 
    session.close_temporary_table(table, false, false);    // Don't free share
445
 
    assert(share->storage_engine);
446
 
    engine->set_name(share->db_type()->getName());
447
 
    plugin::StorageEngine::createTable(session, share->normalized_path.str,
448
 
                                       share->db.str, share->table_name.str, create_info, 
449
 
                                       true, tmp_table, false);
450
 
    // We don't need to call invalidate() because this table is not in cache
451
 
    if ((error= (int) !(session.open_temporary_table(share->path.str,
452
 
                                                      share->db.str,
453
 
                                                      share->table_name.str, 1,
454
 
                                                      OTM_OPEN))))
455
 
      (void) session.rm_temporary_table(table_type, path);
456
 
    share->free_table_share();
457
 
    free((char*) table);
458
 
    /*
459
 
      If we return here we will not have logged the truncation to the bin log
460
 
      and we will not my_ok() to the client.
461
 
    */
462
 
    goto end;
463
 
  }
464
 
 
465
 
  path_length= build_table_filename(path, sizeof(path), table_list->db,
466
 
                                    table_list->table_name, 0);
467
 
 
468
 
  if (!dont_send_ok)
469
 
    goto trunc_by_del;
470
 
 
471
 
  pthread_mutex_lock(&LOCK_open); /* Recreate table for truncate */
472
 
  error= plugin::StorageEngine::createTable(session, path, table_list->db, table_list->table_name,
473
 
                                            create_info, true, tmp_table, false);
474
 
  pthread_mutex_unlock(&LOCK_open);
475
 
 
476
 
end:
477
 
  if (!dont_send_ok)
478
 
  {
479
 
    if (!error)
480
 
    {
481
 
      /*
482
 
        TRUNCATE must always be statement-based binlogged (not row-based) so
483
 
        we don't test current_stmt_binlog_row_based.
484
 
      */
485
 
      write_bin_log(&session, session.query, session.query_length);
486
 
      session.my_ok();          // This should return record count
487
 
    }
488
 
    pthread_mutex_lock(&LOCK_open); /* For truncate delete from hash when finished */
489
 
    unlock_table_name(table_list);
490
 
    pthread_mutex_unlock(&LOCK_open);
491
 
  }
492
 
  else if (error)
493
 
  {
494
 
    pthread_mutex_lock(&LOCK_open); /* For truncate delete from hash when finished */
495
 
    unlock_table_name(table_list);
496
 
    pthread_mutex_unlock(&LOCK_open);
497
 
  }
498
 
  return(error);
499
 
 
500
 
trunc_by_del:
501
 
  /* Probably InnoDB table */
 
409
 
502
410
  uint64_t save_options= session.options;
503
411
  table_list->lock_type= TL_WRITE;
504
412
  session.options&= ~(OPTION_BEGIN | OPTION_NOT_AUTOCOMMIT);
515
423
  ha_commit(&session);
516
424
  session.options= save_options;
517
425
 
518
 
  return(error);
 
426
  return error;
519
427
}