~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_delete.cc

  • Committer: Brian Aker
  • Date: 2009-11-10 19:12:28 UTC
  • mto: This revision was merged to the branch mainline in revision 1210.
  • Revision ID: brian@gaz-20091110191228-xza6ainzo0yxtzvw
Merge Truncate patch. This fixes all of the "half setup" of Truncate. Still
leaves open the question of what we should be doing in replication.

Show diffs side-by-side

added added

removed removed

Lines of Context:
300
300
  if (options & OPTION_QUICK)
301
301
    (void) table->file->extra(HA_EXTRA_NORMAL);
302
302
 
 
303
cleanup:
 
304
 
303
305
  if (reset_auto_increment && (error < 0))
304
306
  {
305
307
    /*
315
317
    }
316
318
  }
317
319
 
318
 
cleanup:
319
 
 
320
320
  delete select;
321
321
  transactional_table= table->file->has_transactions();
322
322
 
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
}