~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_delete.cc

  • Committer: Mark Atwood
  • Date: 2008-10-16 11:33:16 UTC
  • mto: (520.1.13 drizzle)
  • mto: This revision was merged to the branch mainline in revision 530.
  • Revision ID: mark@fallenpegasus.com-20081016113316-ff6jdt31ck90sjdh
an implemention of the errmsg plugin

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
*/
21
21
#include <drizzled/server_includes.h>
22
22
#include <drizzled/sql_select.h>
23
 
#include <drizzled/error.h>
24
 
#include <drizzled/probes.h>
25
 
#include <drizzled/sql_parse.h>
26
 
#include <drizzled/sql_base.h>
 
23
#include <drizzled/drizzled_error_messages.h>
27
24
 
28
25
/**
29
26
  Implement DELETE SQL word.
33
30
  end of dispatch_command().
34
31
*/
35
32
 
36
 
bool mysql_delete(Session *session, TableList *table_list, COND *conds,
 
33
bool mysql_delete(THD *thd, TableList *table_list, COND *conds,
37
34
                  SQL_LIST *order, ha_rows limit, uint64_t options,
38
35
                  bool reset_auto_increment)
39
36
{
47
44
  bool          const_cond_result;
48
45
  ha_rows       deleted= 0;
49
46
  uint32_t usable_index= MAX_KEY;
50
 
  SELECT_LEX   *select_lex= &session->lex->select_lex;
51
 
  Session::killed_state killed_status= Session::NOT_KILLED;
 
47
  SELECT_LEX   *select_lex= &thd->lex->select_lex;
 
48
  THD::killed_state killed_status= THD::NOT_KILLED;
52
49
  
53
50
 
54
 
  if (open_and_lock_tables(session, table_list))
 
51
  if (open_and_lock_tables(thd, table_list))
55
52
    return(true);
56
53
  /* TODO look at this error */
57
54
  if (!(table= table_list->table))
59
56
    my_error(ER_VIEW_DELETE_MERGE_VIEW, MYF(0), "", "");
60
57
    return(true);
61
58
  }
62
 
  session->set_proc_info("init");
 
59
  thd->set_proc_info("init");
63
60
  table->map=1;
64
61
 
65
 
  if (mysql_prepare_delete(session, table_list, &conds))
 
62
  if (mysql_prepare_delete(thd, table_list, &conds))
66
63
    goto err;
67
64
 
68
65
  /* check ORDER BY even if it can be ignored */
76
73
    tables.table = table;
77
74
    tables.alias = table_list->alias;
78
75
 
79
 
      if (select_lex->setup_ref_array(session, order->elements) ||
80
 
          setup_order(session, select_lex->ref_pointer_array, &tables,
 
76
      if (select_lex->setup_ref_array(thd, order->elements) ||
 
77
          setup_order(thd, select_lex->ref_pointer_array, &tables,
81
78
                    fields, all_fields, (order_st*) order->first))
82
79
    {
83
80
      delete select;
84
 
      free_underlaid_joins(session, &session->lex->select_lex);
 
81
      free_underlaid_joins(thd, &thd->lex->select_lex);
85
82
      goto err;
86
83
    }
87
84
  }
88
85
 
89
86
  const_cond= (!conds || conds->const_item());
90
 
  safe_update=test(session->options & OPTION_SAFE_UPDATES);
 
87
  safe_update=test(thd->options & OPTION_SAFE_UPDATES);
91
88
  if (safe_update && const_cond)
92
89
  {
93
90
    my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
95
92
    goto err;
96
93
  }
97
94
 
98
 
  select_lex->no_error= session->lex->ignore;
 
95
  select_lex->no_error= thd->lex->ignore;
99
96
 
100
97
  const_cond_result= const_cond && (!conds || conds->val_int());
101
 
  if (session->is_error())
 
98
  if (thd->is_error())
102
99
  {
103
100
    /* Error evaluating val_int(). */
104
101
    return(true);
123
120
      - We should not be binlogging this statement row-based, and
124
121
      - there should be no delete triggers associated with the table.
125
122
  */
126
 
  if (!using_limit && const_cond_result)
 
123
  if (!using_limit && const_cond_result &&
 
124
      (thd->lex->sql_command == SQLCOM_TRUNCATE ||
 
125
       (!thd->current_stmt_binlog_row_based)))
127
126
  {
128
127
    /* Update the table->file->stats.records number */
129
128
    table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
145
144
  if (conds)
146
145
  {
147
146
    Item::cond_result result;
148
 
    conds= remove_eq_conds(session, conds, &result);
 
147
    conds= remove_eq_conds(thd, conds, &result);
149
148
    if (result == Item::COND_FALSE)             // Impossible where
150
149
      limit= 0;
151
150
  }
158
157
  select=make_select(table, 0, 0, conds, 0, &error);
159
158
  if (error)
160
159
    goto err;
161
 
  if ((select && select->check_quick(session, safe_update, limit)) || !limit)
 
160
  if ((select && select->check_quick(thd, safe_update, limit)) || !limit)
162
161
  {
163
162
    delete select;
164
 
    free_underlaid_joins(session, select_lex);
165
 
    session->row_count_func= 0;
 
163
    free_underlaid_joins(thd, select_lex);
 
164
    thd->row_count_func= 0;
166
165
    DRIZZLE_DELETE_END();
167
 
    my_ok(session, (ha_rows) session->row_count_func);
 
166
    my_ok(thd, (ha_rows) thd->row_count_func);
168
167
    /*
169
168
      We don't need to call reset_auto_increment in this case, because
170
169
      mysql_truncate always gives a NULL conds argument, hence we never
176
175
  /* If running in safe sql mode, don't allow updates without keys */
177
176
  if (table->quick_keys.is_clear_all())
178
177
  {
179
 
    session->server_status|=SERVER_QUERY_NO_INDEX_USED;
 
178
    thd->server_status|=SERVER_QUERY_NO_INDEX_USED;
180
179
    if (safe_update && !using_limit)
181
180
    {
182
181
      delete select;
183
 
      free_underlaid_joins(session, select_lex);
 
182
      free_underlaid_joins(thd, select_lex);
184
183
      my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
185
184
                 ER(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE), MYF(0));
186
185
      goto err;
205
204
    
206
205
      if (!(sortorder= make_unireg_sortorder((order_st*) order->first,
207
206
                                             &length, NULL)) ||
208
 
          (table->sort.found_records = filesort(session, table, sortorder, length,
 
207
          (table->sort.found_records = filesort(thd, table, sortorder, length,
209
208
                                                select, HA_POS_ERROR, 1,
210
209
                                                &examined_rows))
211
210
          == HA_POS_ERROR)
212
211
      {
213
212
        delete select;
214
 
        free_underlaid_joins(session, &session->lex->select_lex);
 
213
        free_underlaid_joins(thd, &thd->lex->select_lex);
215
214
        goto err;
216
215
      }
217
216
      /*
219
218
        so we don't need the where clause
220
219
      */
221
220
      delete select;
222
 
      free_underlaid_joins(session, select_lex);
 
221
      free_underlaid_joins(thd, select_lex);
223
222
      select= 0;
224
223
    }
225
224
  }
228
227
  if (select && select->quick && select->quick->reset())
229
228
  {
230
229
    delete select;
231
 
    free_underlaid_joins(session, select_lex);
 
230
    free_underlaid_joins(thd, select_lex);
232
231
    goto err;
233
232
  }
234
233
  if (usable_index==MAX_KEY)
235
 
    init_read_record(&info,session,table,select,1,1);
 
234
    init_read_record(&info,thd,table,select,1,1);
236
235
  else
237
 
    init_read_record_idx(&info, session, table, 1, usable_index);
 
236
    init_read_record_idx(&info, thd, table, 1, usable_index);
238
237
 
239
 
  session->set_proc_info("updating");
 
238
  thd->set_proc_info("updating");
240
239
 
241
240
  will_batch= !table->file->start_bulk_delete();
242
241
 
243
242
 
244
243
  table->mark_columns_needed_for_delete();
245
244
 
246
 
  while (!(error=info.read_record(&info)) && !session->killed &&
247
 
         ! session->is_error())
 
245
  while (!(error=info.read_record(&info)) && !thd->killed &&
 
246
         ! thd->is_error())
248
247
  {
249
 
    // session->is_error() is tested to disallow delete row on error
250
 
    if (!(select && select->skip_record())&& ! session->is_error() )
 
248
    // thd->is_error() is tested to disallow delete row on error
 
249
    if (!(select && select->skip_record())&& ! thd->is_error() )
251
250
    {
252
251
      if (!(error= table->file->ha_delete_row(table->record[0])))
253
252
      {
276
275
    else
277
276
      table->file->unlock_row();  // Row failed selection, release lock on it
278
277
  }
279
 
  killed_status= session->killed;
280
 
  if (killed_status != Session::NOT_KILLED || session->is_error())
 
278
  killed_status= thd->killed;
 
279
  if (killed_status != THD::NOT_KILLED || thd->is_error())
281
280
    error= 1;                                   // Aborted
282
281
  if (will_batch && (loc_error= table->file->end_bulk_delete()))
283
282
  {
285
284
      table->file->print_error(loc_error,MYF(0));
286
285
    error=1;
287
286
  }
288
 
  session->set_proc_info("end");
 
287
  thd->set_proc_info("end");
289
288
  end_read_record(&info);
290
289
  if (options & OPTION_QUICK)
291
290
    (void) table->file->extra(HA_EXTRA_NORMAL);
311
310
  transactional_table= table->file->has_transactions();
312
311
 
313
312
  if (!transactional_table && deleted > 0)
314
 
    session->transaction.stmt.modified_non_trans_table= true;
 
313
    thd->transaction.stmt.modified_non_trans_table= true;
315
314
  
316
315
  /* See similar binlogging code in sql_update.cc, for comments */
317
 
  if ((error < 0) || session->transaction.stmt.modified_non_trans_table)
 
316
  if ((error < 0) || thd->transaction.stmt.modified_non_trans_table)
318
317
  {
319
 
    if (drizzle_bin_log.is_open())
 
318
    if (mysql_bin_log.is_open())
320
319
    {
321
320
      if (error < 0)
322
 
        session->clear_error();
 
321
        thd->clear_error();
323
322
      /*
324
323
        [binlog]: If 'handler::delete_all_rows()' was called and the
325
324
        storage engine does not inject the rows itself, we replicate
326
325
        statement-based; otherwise, 'ha_delete_row()' was used to
327
326
        delete specific rows which we might log row-based.
328
327
      */
329
 
      int log_result= session->binlog_query(Session::ROW_QUERY_TYPE,
330
 
                                        session->query, session->query_length,
 
328
      int log_result= thd->binlog_query(THD::ROW_QUERY_TYPE,
 
329
                                        thd->query, thd->query_length,
331
330
                                        transactional_table, false, killed_status);
332
331
 
333
332
      if (log_result && transactional_table)
335
334
        error=1;
336
335
      }
337
336
    }
338
 
    if (session->transaction.stmt.modified_non_trans_table)
339
 
      session->transaction.all.modified_non_trans_table= true;
 
337
    if (thd->transaction.stmt.modified_non_trans_table)
 
338
      thd->transaction.all.modified_non_trans_table= true;
340
339
  }
341
 
  assert(transactional_table || !deleted || session->transaction.stmt.modified_non_trans_table);
342
 
  free_underlaid_joins(session, select_lex);
 
340
  assert(transactional_table || !deleted || thd->transaction.stmt.modified_non_trans_table);
 
341
  free_underlaid_joins(thd, select_lex);
343
342
 
344
343
  DRIZZLE_DELETE_END();
345
 
  if (error < 0 || (session->lex->ignore && !session->is_fatal_error))
 
344
  if (error < 0 || (thd->lex->ignore && !thd->is_fatal_error))
346
345
  {
347
 
    session->row_count_func= deleted;
348
 
    my_ok(session, (ha_rows) session->row_count_func);
 
346
    thd->row_count_func= deleted;
 
347
    my_ok(thd, (ha_rows) thd->row_count_func);
349
348
  }
350
 
  return(error >= 0 || session->is_error());
 
349
  return(error >= 0 || thd->is_error());
351
350
 
352
351
err:
353
352
  DRIZZLE_DELETE_END();
360
359
 
361
360
  SYNOPSIS
362
361
    mysql_prepare_delete()
363
 
    session                     - thread handler
 
362
    thd                 - thread handler
364
363
    table_list          - global/local table list
365
364
    conds               - conditions
366
365
 
368
367
    false OK
369
368
    true  error
370
369
*/
371
 
int mysql_prepare_delete(Session *session, TableList *table_list, Item **conds)
 
370
int mysql_prepare_delete(THD *thd, TableList *table_list, Item **conds)
372
371
{
373
 
  SELECT_LEX *select_lex= &session->lex->select_lex;
 
372
  SELECT_LEX *select_lex= &thd->lex->select_lex;
374
373
  
375
374
  List<Item> all_fields;
376
375
 
377
 
  session->lex->allow_sum_func= 0;
378
 
  if (setup_tables_and_check_access(session, &session->lex->select_lex.context,
379
 
                                    &session->lex->select_lex.top_join_list,
 
376
  /*
 
377
    Statement-based replication of DELETE ... LIMIT is not safe as order of
 
378
    rows is not defined, so in mixed mode we go to row-based.
 
379
 
 
380
    Note that we may consider a statement as safe if ORDER BY primary_key
 
381
    is present. However it may confuse users to see very similiar statements
 
382
    replicated differently.
 
383
  */
 
384
  if (thd->lex->current_select->select_limit)
 
385
  {
 
386
    thd->lex->set_stmt_unsafe();
 
387
    thd->set_current_stmt_binlog_row_based_if_mixed();
 
388
  }
 
389
  thd->lex->allow_sum_func= 0;
 
390
  if (setup_tables_and_check_access(thd, &thd->lex->select_lex.context,
 
391
                                    &thd->lex->select_lex.top_join_list,
380
392
                                    table_list, 
381
393
                                    &select_lex->leaf_tables, false) ||
382
 
      setup_conds(session, table_list, select_lex->leaf_tables, conds))
 
394
      setup_conds(thd, table_list, select_lex->leaf_tables, conds))
383
395
    return(true);
384
396
  {
385
397
    TableList *duplicate;
386
 
    if ((duplicate= unique_table(session, table_list, table_list->next_global, 0)))
 
398
    if ((duplicate= unique_table(thd, table_list, table_list->next_global, 0)))
387
399
    {
388
400
      update_non_unique_table_error(table_list, "DELETE", duplicate);
389
401
      return(true);
391
403
  }
392
404
 
393
405
  if (select_lex->inner_refs_list.elements &&
394
 
    fix_inner_refs(session, all_fields, select_lex, select_lex->ref_pointer_array))
 
406
    fix_inner_refs(thd, all_fields, select_lex, select_lex->ref_pointer_array))
395
407
    return(-1);
396
408
 
397
409
  return(false);
402
414
  Delete multiple tables from join 
403
415
***************************************************************************/
404
416
 
405
 
#define MEM_STRIP_BUF_SIZE current_session->variables.sortbuff_size
 
417
#define MEM_STRIP_BUF_SIZE current_thd->variables.sortbuff_size
406
418
 
407
419
extern "C" int refpos_order_cmp(void* arg, const void *a,const void *b)
408
420
{
415
427
 
416
428
  SYNOPSIS
417
429
    mysql_multi_delete_prepare()
418
 
    session         thread handler
 
430
    thd         thread handler
419
431
 
420
432
  RETURN
421
433
    false OK
422
434
    true  Error
423
435
*/
424
436
 
425
 
int mysql_multi_delete_prepare(Session *session)
 
437
int mysql_multi_delete_prepare(THD *thd)
426
438
{
427
 
  LEX *lex= session->lex;
 
439
  LEX *lex= thd->lex;
428
440
  TableList *aux_tables= (TableList *)lex->auxiliary_table_list.first;
429
441
  TableList *target_tbl;
430
442
  
435
447
 
436
448
    lex->query_tables also point on local list of DELETE SELECT_LEX
437
449
  */
438
 
  if (setup_tables_and_check_access(session, &session->lex->select_lex.context,
439
 
                                    &session->lex->select_lex.top_join_list,
 
450
  if (setup_tables_and_check_access(thd, &thd->lex->select_lex.context,
 
451
                                    &thd->lex->select_lex.top_join_list,
440
452
                                    lex->query_tables,
441
453
                                    &lex->select_lex.leaf_tables, false))
442
454
    return(true);
467
479
    */
468
480
    {
469
481
      TableList *duplicate;
470
 
      if ((duplicate= unique_table(session, target_tbl->correspondent_table,
 
482
      if ((duplicate= unique_table(thd, target_tbl->correspondent_table,
471
483
                                   lex->query_tables, 0)))
472
484
      {
473
485
        update_non_unique_table_error(target_tbl->correspondent_table,
490
502
 
491
503
 
492
504
int
493
 
multi_delete::prepare(List<Item> &, SELECT_LEX_UNIT *u)
 
505
multi_delete::prepare(List<Item> &values __attribute__((unused)),
 
506
                      SELECT_LEX_UNIT *u)
494
507
{
495
508
  
496
509
  unit= u;
497
510
  do_delete= 1;
498
 
  session->set_proc_info("deleting from main table");
 
511
  thd->set_proc_info("deleting from main table");
499
512
  return(0);
500
513
}
501
514
 
507
520
  Unique **tempfiles_ptr;
508
521
  
509
522
 
510
 
  if ((session->options & OPTION_SAFE_UPDATES) && error_if_full_join(join))
 
523
  if ((thd->options & OPTION_SAFE_UPDATES) && error_if_full_join(join))
511
524
    return(1);
512
525
 
513
526
  table_map tables_to_delete_from=0;
563
576
                                  table->file->ref_length,
564
577
                                  MEM_STRIP_BUF_SIZE);
565
578
  }
566
 
  return(session->is_fatal_error != 0);
 
579
  return(thd->is_fatal_error != 0);
567
580
}
568
581
 
569
582
 
585
598
}
586
599
 
587
600
 
588
 
bool multi_delete::send_data(List<Item> &)
 
601
bool multi_delete::send_data(List<Item> &values __attribute__((unused)))
589
602
{
590
603
  int secure_counter= delete_while_scanning ? -1 : 0;
591
604
  TableList *del_table;
592
 
 
 
605
  
593
606
 
594
607
  for (del_table= delete_tables;
595
608
       del_table;
613
626
      {
614
627
        deleted++;
615
628
        if (!table->file->has_transactions())
616
 
          session->transaction.stmt.modified_non_trans_table= true;
 
629
          thd->transaction.stmt.modified_non_trans_table= true;
617
630
      }
618
631
      else
619
632
      {
652
665
 
653
666
  /* the error was handled or nothing deleted and no side effects return */
654
667
  if (error_handled ||
655
 
      (!session->transaction.stmt.modified_non_trans_table && !deleted))
 
668
      (!thd->transaction.stmt.modified_non_trans_table && !deleted))
656
669
    return;
657
670
 
658
671
  /*
675
688
    return;
676
689
  }
677
690
  
678
 
  if (session->transaction.stmt.modified_non_trans_table)
 
691
  if (thd->transaction.stmt.modified_non_trans_table)
679
692
  {
680
693
    /* 
681
694
       there is only side effects; to binlog with the error
682
695
    */
683
 
    if (drizzle_bin_log.is_open())
 
696
    if (mysql_bin_log.is_open())
684
697
    {
685
 
      session->binlog_query(Session::ROW_QUERY_TYPE,
686
 
                        session->query, session->query_length,
 
698
      thd->binlog_query(THD::ROW_QUERY_TYPE,
 
699
                        thd->query, thd->query_length,
687
700
                        transactional_tables, false);
688
701
    }
689
 
    session->transaction.all.modified_non_trans_table= true;
 
702
    thd->transaction.all.modified_non_trans_table= true;
690
703
  }
691
704
  return;
692
705
}
726
739
    }
727
740
 
728
741
    READ_RECORD info;
729
 
    init_read_record(&info,session,table,NULL,0,1);
 
742
    init_read_record(&info,thd,table,NULL,0,1);
730
743
    /*
731
744
      Ignore any rows not found in reference tables as they may already have
732
745
      been deleted by foreign key handling
733
746
    */
734
747
    info.ignore_not_found_rows= 1;
735
748
    will_batch= !table->file->start_bulk_delete();
736
 
    while (!(local_error=info.read_record(&info)) && !session->killed)
 
749
    while (!(local_error=info.read_record(&info)) && !thd->killed)
737
750
    {
738
751
      if ((local_error=table->file->ha_delete_row(table->record[0])))
739
752
      {
751
764
      }
752
765
    }
753
766
    if (last_deleted != deleted && !table->file->has_transactions())
754
 
      session->transaction.stmt.modified_non_trans_table= true;
 
767
      thd->transaction.stmt.modified_non_trans_table= true;
755
768
    end_read_record(&info);
756
 
    if (session->killed && !local_error)
 
769
    if (thd->killed && !local_error)
757
770
      local_error= 1;
758
771
    if (local_error == -1)                              // End of file
759
772
      local_error = 0;
771
784
 
772
785
bool multi_delete::send_eof()
773
786
{
774
 
  Session::killed_state killed_status= Session::NOT_KILLED;
775
 
  session->set_proc_info("deleting from reference tables");
 
787
  THD::killed_state killed_status= THD::NOT_KILLED;
 
788
  thd->set_proc_info("deleting from reference tables");
776
789
 
777
790
  /* Does deletes for the last n - 1 tables, returns 0 if ok */
778
791
  int local_error= do_deletes();                // returns 0 if success
779
792
 
780
793
  /* compute a total error to know if something failed */
781
794
  local_error= local_error || error;
782
 
  killed_status= (local_error == 0)? Session::NOT_KILLED : session->killed;
 
795
  killed_status= (local_error == 0)? THD::NOT_KILLED : thd->killed;
783
796
  /* reset used flags */
784
 
  session->set_proc_info("end");
 
797
  thd->set_proc_info("end");
785
798
 
786
 
  if ((local_error == 0) || session->transaction.stmt.modified_non_trans_table)
 
799
  if ((local_error == 0) || thd->transaction.stmt.modified_non_trans_table)
787
800
  {
788
 
    if (drizzle_bin_log.is_open())
 
801
    if (mysql_bin_log.is_open())
789
802
    {
790
803
      if (local_error == 0)
791
 
        session->clear_error();
792
 
      if (session->binlog_query(Session::ROW_QUERY_TYPE,
793
 
                            session->query, session->query_length,
 
804
        thd->clear_error();
 
805
      if (thd->binlog_query(THD::ROW_QUERY_TYPE,
 
806
                            thd->query, thd->query_length,
794
807
                            transactional_tables, false, killed_status) &&
795
808
          !normal_tables)
796
809
      {
797
810
        local_error=1;  // Log write failed: roll back the SQL statement
798
811
      }
799
812
    }
800
 
    if (session->transaction.stmt.modified_non_trans_table)
801
 
      session->transaction.all.modified_non_trans_table= true;
 
813
    if (thd->transaction.stmt.modified_non_trans_table)
 
814
      thd->transaction.all.modified_non_trans_table= true;
802
815
  }
803
816
  if (local_error != 0)
804
817
    error_handled= true; // to force early leave from ::send_error()
805
818
 
806
819
  if (!local_error)
807
820
  {
808
 
    session->row_count_func= deleted;
809
 
    ::my_ok(session, (ha_rows) session->row_count_func);
 
821
    thd->row_count_func= deleted;
 
822
    ::my_ok(thd, (ha_rows) thd->row_count_func);
810
823
  }
811
824
  return 0;
812
825
}
828
841
  - If we want to have a name lock on the table on exit without errors.
829
842
*/
830
843
 
831
 
bool mysql_truncate(Session *session, TableList *table_list, bool dont_send_ok)
 
844
bool mysql_truncate(THD *thd, TableList *table_list, bool dont_send_ok)
832
845
{
833
846
  HA_CREATE_INFO create_info;
834
847
  char path[FN_REFLEN];
839
852
 
840
853
  memset(&create_info, 0, sizeof(create_info));
841
854
  /* If it is a temporary table, close and regenerate it */
842
 
  if (!dont_send_ok && (table= find_temporary_table(session, table_list)))
 
855
  if (!dont_send_ok && (table= find_temporary_table(thd, table_list)))
843
856
  {
844
857
    handlerton *table_type= table->s->db_type();
845
858
    TABLE_SHARE *share= table->s;
 
859
    bool frm_only= (share->tmp_table == TMP_TABLE_FRM_FILE_ONLY);
846
860
 
847
861
    if (!ha_check_storage_engine_flag(table_type, HTON_CAN_RECREATE))
848
862
      goto trunc_by_del;
849
863
 
850
864
    table->file->info(HA_STATUS_AUTO | HA_STATUS_NO_LOCK);
851
865
    
852
 
    close_temporary_table(session, table, 0, 0);    // Don't free share
853
 
    ha_create_table(session, share->normalized_path.str,
 
866
    close_temporary_table(thd, table, 0, 0);    // Don't free share
 
867
    ha_create_table(thd, share->normalized_path.str,
854
868
                    share->db.str, share->table_name.str, &create_info, 1);
855
869
    // We don't need to call invalidate() because this table is not in cache
856
 
    if ((error= (int) !(open_temporary_table(session, share->path.str,
 
870
    if ((error= (int) !(open_temporary_table(thd, share->path.str,
857
871
                                             share->db.str,
858
872
                                             share->table_name.str, 1,
859
873
                                             OTM_OPEN))))
860
 
      (void) rm_temporary_table(table_type, path);
 
874
      (void) rm_temporary_table(table_type, path, frm_only);
861
875
    free_table_share(share);
862
876
    free((char*) table);
863
877
    /*
878
892
  // '\0';
879
893
  path[path_length - reg_ext_length] = 0;
880
894
  pthread_mutex_lock(&LOCK_open);
881
 
  error= ha_create_table(session, path, table_list->db, table_list->table_name,
 
895
  error= ha_create_table(thd, path, table_list->db, table_list->table_name,
882
896
                         &create_info, 1);
883
897
  pthread_mutex_unlock(&LOCK_open);
884
898
 
891
905
        TRUNCATE must always be statement-based binlogged (not row-based) so
892
906
        we don't test current_stmt_binlog_row_based.
893
907
      */
894
 
      write_bin_log(session, true, session->query, session->query_length);
895
 
      my_ok(session);           // This should return record count
 
908
      write_bin_log(thd, true, thd->query, thd->query_length);
 
909
      my_ok(thd);               // This should return record count
896
910
    }
897
911
    pthread_mutex_lock(&LOCK_open);
898
 
    unlock_table_name(session, table_list);
 
912
    unlock_table_name(thd, table_list);
899
913
    pthread_mutex_unlock(&LOCK_open);
900
914
  }
901
915
  else if (error)
902
916
  {
903
917
    pthread_mutex_lock(&LOCK_open);
904
 
    unlock_table_name(session, table_list);
 
918
    unlock_table_name(thd, table_list);
905
919
    pthread_mutex_unlock(&LOCK_open);
906
920
  }
907
921
  return(error);
908
922
 
909
923
trunc_by_del:
910
924
  /* Probably InnoDB table */
911
 
  uint64_t save_options= session->options;
 
925
  uint64_t save_options= thd->options;
912
926
  table_list->lock_type= TL_WRITE;
913
 
  session->options&= ~(OPTION_BEGIN | OPTION_NOT_AUTOCOMMIT);
914
 
  ha_enable_transaction(session, false);
915
 
  mysql_init_select(session->lex);
916
 
  error= mysql_delete(session, table_list, (COND*) 0, (SQL_LIST*) 0,
 
927
  thd->options&= ~(OPTION_BEGIN | OPTION_NOT_AUTOCOMMIT);
 
928
  ha_enable_transaction(thd, false);
 
929
  mysql_init_select(thd->lex);
 
930
  bool save_binlog_row_based= thd->current_stmt_binlog_row_based;
 
931
  thd->clear_current_stmt_binlog_row_based();
 
932
  error= mysql_delete(thd, table_list, (COND*) 0, (SQL_LIST*) 0,
917
933
                      HA_POS_ERROR, 0L, true);
918
 
  ha_enable_transaction(session, true);
 
934
  ha_enable_transaction(thd, true);
919
935
  /*
920
936
    Safety, in case the engine ignored ha_enable_transaction(false)
921
 
    above. Also clears session->transaction.*.
 
937
    above. Also clears thd->transaction.*.
922
938
  */
923
 
  error= ha_autocommit_or_rollback(session, error);
924
 
  ha_commit(session);
925
 
  session->options= save_options;
 
939
  error= ha_autocommit_or_rollback(thd, error);
 
940
  ha_commit(thd);
 
941
  thd->options= save_options;
 
942
  thd->current_stmt_binlog_row_based= save_binlog_row_based;
926
943
  return(error);
927
944
}