~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_delete.cc

  • Committer: Brian Aker
  • Date: 2008-10-20 04:28:21 UTC
  • mto: (492.3.21 drizzle-clean-code)
  • mto: This revision was merged to the branch mainline in revision 530.
  • Revision ID: brian@tangent.org-20081020042821-rqqdrccuu8195k3y
Second pass of thd cleanup

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
  end of dispatch_command().
31
31
*/
32
32
 
33
 
bool mysql_delete(Session *thd, TableList *table_list, COND *conds,
 
33
bool mysql_delete(Session *session, TableList *table_list, COND *conds,
34
34
                  SQL_LIST *order, ha_rows limit, uint64_t options,
35
35
                  bool reset_auto_increment)
36
36
{
44
44
  bool          const_cond_result;
45
45
  ha_rows       deleted= 0;
46
46
  uint32_t usable_index= MAX_KEY;
47
 
  SELECT_LEX   *select_lex= &thd->lex->select_lex;
 
47
  SELECT_LEX   *select_lex= &session->lex->select_lex;
48
48
  Session::killed_state killed_status= Session::NOT_KILLED;
49
49
  
50
50
 
51
 
  if (open_and_lock_tables(thd, table_list))
 
51
  if (open_and_lock_tables(session, table_list))
52
52
    return(true);
53
53
  /* TODO look at this error */
54
54
  if (!(table= table_list->table))
56
56
    my_error(ER_VIEW_DELETE_MERGE_VIEW, MYF(0), "", "");
57
57
    return(true);
58
58
  }
59
 
  thd->set_proc_info("init");
 
59
  session->set_proc_info("init");
60
60
  table->map=1;
61
61
 
62
 
  if (mysql_prepare_delete(thd, table_list, &conds))
 
62
  if (mysql_prepare_delete(session, table_list, &conds))
63
63
    goto err;
64
64
 
65
65
  /* check ORDER BY even if it can be ignored */
73
73
    tables.table = table;
74
74
    tables.alias = table_list->alias;
75
75
 
76
 
      if (select_lex->setup_ref_array(thd, order->elements) ||
77
 
          setup_order(thd, select_lex->ref_pointer_array, &tables,
 
76
      if (select_lex->setup_ref_array(session, order->elements) ||
 
77
          setup_order(session, select_lex->ref_pointer_array, &tables,
78
78
                    fields, all_fields, (order_st*) order->first))
79
79
    {
80
80
      delete select;
81
 
      free_underlaid_joins(thd, &thd->lex->select_lex);
 
81
      free_underlaid_joins(session, &session->lex->select_lex);
82
82
      goto err;
83
83
    }
84
84
  }
85
85
 
86
86
  const_cond= (!conds || conds->const_item());
87
 
  safe_update=test(thd->options & OPTION_SAFE_UPDATES);
 
87
  safe_update=test(session->options & OPTION_SAFE_UPDATES);
88
88
  if (safe_update && const_cond)
89
89
  {
90
90
    my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
92
92
    goto err;
93
93
  }
94
94
 
95
 
  select_lex->no_error= thd->lex->ignore;
 
95
  select_lex->no_error= session->lex->ignore;
96
96
 
97
97
  const_cond_result= const_cond && (!conds || conds->val_int());
98
 
  if (thd->is_error())
 
98
  if (session->is_error())
99
99
  {
100
100
    /* Error evaluating val_int(). */
101
101
    return(true);
121
121
      - there should be no delete triggers associated with the table.
122
122
  */
123
123
  if (!using_limit && const_cond_result &&
124
 
      (thd->lex->sql_command == SQLCOM_TRUNCATE ||
125
 
       (!thd->current_stmt_binlog_row_based)))
 
124
      (session->lex->sql_command == SQLCOM_TRUNCATE ||
 
125
       (!session->current_stmt_binlog_row_based)))
126
126
  {
127
127
    /* Update the table->file->stats.records number */
128
128
    table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
144
144
  if (conds)
145
145
  {
146
146
    Item::cond_result result;
147
 
    conds= remove_eq_conds(thd, conds, &result);
 
147
    conds= remove_eq_conds(session, conds, &result);
148
148
    if (result == Item::COND_FALSE)             // Impossible where
149
149
      limit= 0;
150
150
  }
157
157
  select=make_select(table, 0, 0, conds, 0, &error);
158
158
  if (error)
159
159
    goto err;
160
 
  if ((select && select->check_quick(thd, safe_update, limit)) || !limit)
 
160
  if ((select && select->check_quick(session, safe_update, limit)) || !limit)
161
161
  {
162
162
    delete select;
163
 
    free_underlaid_joins(thd, select_lex);
164
 
    thd->row_count_func= 0;
 
163
    free_underlaid_joins(session, select_lex);
 
164
    session->row_count_func= 0;
165
165
    DRIZZLE_DELETE_END();
166
 
    my_ok(thd, (ha_rows) thd->row_count_func);
 
166
    my_ok(session, (ha_rows) session->row_count_func);
167
167
    /*
168
168
      We don't need to call reset_auto_increment in this case, because
169
169
      mysql_truncate always gives a NULL conds argument, hence we never
175
175
  /* If running in safe sql mode, don't allow updates without keys */
176
176
  if (table->quick_keys.is_clear_all())
177
177
  {
178
 
    thd->server_status|=SERVER_QUERY_NO_INDEX_USED;
 
178
    session->server_status|=SERVER_QUERY_NO_INDEX_USED;
179
179
    if (safe_update && !using_limit)
180
180
    {
181
181
      delete select;
182
 
      free_underlaid_joins(thd, select_lex);
 
182
      free_underlaid_joins(session, select_lex);
183
183
      my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
184
184
                 ER(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE), MYF(0));
185
185
      goto err;
204
204
    
205
205
      if (!(sortorder= make_unireg_sortorder((order_st*) order->first,
206
206
                                             &length, NULL)) ||
207
 
          (table->sort.found_records = filesort(thd, table, sortorder, length,
 
207
          (table->sort.found_records = filesort(session, table, sortorder, length,
208
208
                                                select, HA_POS_ERROR, 1,
209
209
                                                &examined_rows))
210
210
          == HA_POS_ERROR)
211
211
      {
212
212
        delete select;
213
 
        free_underlaid_joins(thd, &thd->lex->select_lex);
 
213
        free_underlaid_joins(session, &session->lex->select_lex);
214
214
        goto err;
215
215
      }
216
216
      /*
218
218
        so we don't need the where clause
219
219
      */
220
220
      delete select;
221
 
      free_underlaid_joins(thd, select_lex);
 
221
      free_underlaid_joins(session, select_lex);
222
222
      select= 0;
223
223
    }
224
224
  }
227
227
  if (select && select->quick && select->quick->reset())
228
228
  {
229
229
    delete select;
230
 
    free_underlaid_joins(thd, select_lex);
 
230
    free_underlaid_joins(session, select_lex);
231
231
    goto err;
232
232
  }
233
233
  if (usable_index==MAX_KEY)
234
 
    init_read_record(&info,thd,table,select,1,1);
 
234
    init_read_record(&info,session,table,select,1,1);
235
235
  else
236
 
    init_read_record_idx(&info, thd, table, 1, usable_index);
 
236
    init_read_record_idx(&info, session, table, 1, usable_index);
237
237
 
238
 
  thd->set_proc_info("updating");
 
238
  session->set_proc_info("updating");
239
239
 
240
240
  will_batch= !table->file->start_bulk_delete();
241
241
 
242
242
 
243
243
  table->mark_columns_needed_for_delete();
244
244
 
245
 
  while (!(error=info.read_record(&info)) && !thd->killed &&
246
 
         ! thd->is_error())
 
245
  while (!(error=info.read_record(&info)) && !session->killed &&
 
246
         ! session->is_error())
247
247
  {
248
 
    // thd->is_error() is tested to disallow delete row on error
249
 
    if (!(select && select->skip_record())&& ! thd->is_error() )
 
248
    // session->is_error() is tested to disallow delete row on error
 
249
    if (!(select && select->skip_record())&& ! session->is_error() )
250
250
    {
251
251
      if (!(error= table->file->ha_delete_row(table->record[0])))
252
252
      {
275
275
    else
276
276
      table->file->unlock_row();  // Row failed selection, release lock on it
277
277
  }
278
 
  killed_status= thd->killed;
279
 
  if (killed_status != Session::NOT_KILLED || thd->is_error())
 
278
  killed_status= session->killed;
 
279
  if (killed_status != Session::NOT_KILLED || session->is_error())
280
280
    error= 1;                                   // Aborted
281
281
  if (will_batch && (loc_error= table->file->end_bulk_delete()))
282
282
  {
284
284
      table->file->print_error(loc_error,MYF(0));
285
285
    error=1;
286
286
  }
287
 
  thd->set_proc_info("end");
 
287
  session->set_proc_info("end");
288
288
  end_read_record(&info);
289
289
  if (options & OPTION_QUICK)
290
290
    (void) table->file->extra(HA_EXTRA_NORMAL);
310
310
  transactional_table= table->file->has_transactions();
311
311
 
312
312
  if (!transactional_table && deleted > 0)
313
 
    thd->transaction.stmt.modified_non_trans_table= true;
 
313
    session->transaction.stmt.modified_non_trans_table= true;
314
314
  
315
315
  /* See similar binlogging code in sql_update.cc, for comments */
316
 
  if ((error < 0) || thd->transaction.stmt.modified_non_trans_table)
 
316
  if ((error < 0) || session->transaction.stmt.modified_non_trans_table)
317
317
  {
318
318
    if (mysql_bin_log.is_open())
319
319
    {
320
320
      if (error < 0)
321
 
        thd->clear_error();
 
321
        session->clear_error();
322
322
      /*
323
323
        [binlog]: If 'handler::delete_all_rows()' was called and the
324
324
        storage engine does not inject the rows itself, we replicate
325
325
        statement-based; otherwise, 'ha_delete_row()' was used to
326
326
        delete specific rows which we might log row-based.
327
327
      */
328
 
      int log_result= thd->binlog_query(Session::ROW_QUERY_TYPE,
329
 
                                        thd->query, thd->query_length,
 
328
      int log_result= session->binlog_query(Session::ROW_QUERY_TYPE,
 
329
                                        session->query, session->query_length,
330
330
                                        transactional_table, false, killed_status);
331
331
 
332
332
      if (log_result && transactional_table)
334
334
        error=1;
335
335
      }
336
336
    }
337
 
    if (thd->transaction.stmt.modified_non_trans_table)
338
 
      thd->transaction.all.modified_non_trans_table= true;
 
337
    if (session->transaction.stmt.modified_non_trans_table)
 
338
      session->transaction.all.modified_non_trans_table= true;
339
339
  }
340
 
  assert(transactional_table || !deleted || thd->transaction.stmt.modified_non_trans_table);
341
 
  free_underlaid_joins(thd, select_lex);
 
340
  assert(transactional_table || !deleted || session->transaction.stmt.modified_non_trans_table);
 
341
  free_underlaid_joins(session, select_lex);
342
342
 
343
343
  DRIZZLE_DELETE_END();
344
 
  if (error < 0 || (thd->lex->ignore && !thd->is_fatal_error))
 
344
  if (error < 0 || (session->lex->ignore && !session->is_fatal_error))
345
345
  {
346
 
    thd->row_count_func= deleted;
347
 
    my_ok(thd, (ha_rows) thd->row_count_func);
 
346
    session->row_count_func= deleted;
 
347
    my_ok(session, (ha_rows) session->row_count_func);
348
348
  }
349
 
  return(error >= 0 || thd->is_error());
 
349
  return(error >= 0 || session->is_error());
350
350
 
351
351
err:
352
352
  DRIZZLE_DELETE_END();
359
359
 
360
360
  SYNOPSIS
361
361
    mysql_prepare_delete()
362
 
    thd                 - thread handler
 
362
    session                     - thread handler
363
363
    table_list          - global/local table list
364
364
    conds               - conditions
365
365
 
367
367
    false OK
368
368
    true  error
369
369
*/
370
 
int mysql_prepare_delete(Session *thd, TableList *table_list, Item **conds)
 
370
int mysql_prepare_delete(Session *session, TableList *table_list, Item **conds)
371
371
{
372
 
  SELECT_LEX *select_lex= &thd->lex->select_lex;
 
372
  SELECT_LEX *select_lex= &session->lex->select_lex;
373
373
  
374
374
  List<Item> all_fields;
375
375
 
381
381
    is present. However it may confuse users to see very similiar statements
382
382
    replicated differently.
383
383
  */
384
 
  if (thd->lex->current_select->select_limit)
 
384
  if (session->lex->current_select->select_limit)
385
385
  {
386
 
    thd->lex->set_stmt_unsafe();
387
 
    thd->set_current_stmt_binlog_row_based_if_mixed();
 
386
    session->lex->set_stmt_unsafe();
 
387
    session->set_current_stmt_binlog_row_based_if_mixed();
388
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,
 
389
  session->lex->allow_sum_func= 0;
 
390
  if (setup_tables_and_check_access(session, &session->lex->select_lex.context,
 
391
                                    &session->lex->select_lex.top_join_list,
392
392
                                    table_list, 
393
393
                                    &select_lex->leaf_tables, false) ||
394
 
      setup_conds(thd, table_list, select_lex->leaf_tables, conds))
 
394
      setup_conds(session, table_list, select_lex->leaf_tables, conds))
395
395
    return(true);
396
396
  {
397
397
    TableList *duplicate;
398
 
    if ((duplicate= unique_table(thd, table_list, table_list->next_global, 0)))
 
398
    if ((duplicate= unique_table(session, table_list, table_list->next_global, 0)))
399
399
    {
400
400
      update_non_unique_table_error(table_list, "DELETE", duplicate);
401
401
      return(true);
403
403
  }
404
404
 
405
405
  if (select_lex->inner_refs_list.elements &&
406
 
    fix_inner_refs(thd, all_fields, select_lex, select_lex->ref_pointer_array))
 
406
    fix_inner_refs(session, all_fields, select_lex, select_lex->ref_pointer_array))
407
407
    return(-1);
408
408
 
409
409
  return(false);
414
414
  Delete multiple tables from join 
415
415
***************************************************************************/
416
416
 
417
 
#define MEM_STRIP_BUF_SIZE current_thd->variables.sortbuff_size
 
417
#define MEM_STRIP_BUF_SIZE current_session->variables.sortbuff_size
418
418
 
419
419
extern "C" int refpos_order_cmp(void* arg, const void *a,const void *b)
420
420
{
427
427
 
428
428
  SYNOPSIS
429
429
    mysql_multi_delete_prepare()
430
 
    thd         thread handler
 
430
    session         thread handler
431
431
 
432
432
  RETURN
433
433
    false OK
434
434
    true  Error
435
435
*/
436
436
 
437
 
int mysql_multi_delete_prepare(Session *thd)
 
437
int mysql_multi_delete_prepare(Session *session)
438
438
{
439
 
  LEX *lex= thd->lex;
 
439
  LEX *lex= session->lex;
440
440
  TableList *aux_tables= (TableList *)lex->auxiliary_table_list.first;
441
441
  TableList *target_tbl;
442
442
  
447
447
 
448
448
    lex->query_tables also point on local list of DELETE SELECT_LEX
449
449
  */
450
 
  if (setup_tables_and_check_access(thd, &thd->lex->select_lex.context,
451
 
                                    &thd->lex->select_lex.top_join_list,
 
450
  if (setup_tables_and_check_access(session, &session->lex->select_lex.context,
 
451
                                    &session->lex->select_lex.top_join_list,
452
452
                                    lex->query_tables,
453
453
                                    &lex->select_lex.leaf_tables, false))
454
454
    return(true);
479
479
    */
480
480
    {
481
481
      TableList *duplicate;
482
 
      if ((duplicate= unique_table(thd, target_tbl->correspondent_table,
 
482
      if ((duplicate= unique_table(session, target_tbl->correspondent_table,
483
483
                                   lex->query_tables, 0)))
484
484
      {
485
485
        update_non_unique_table_error(target_tbl->correspondent_table,
508
508
  
509
509
  unit= u;
510
510
  do_delete= 1;
511
 
  thd->set_proc_info("deleting from main table");
 
511
  session->set_proc_info("deleting from main table");
512
512
  return(0);
513
513
}
514
514
 
520
520
  Unique **tempfiles_ptr;
521
521
  
522
522
 
523
 
  if ((thd->options & OPTION_SAFE_UPDATES) && error_if_full_join(join))
 
523
  if ((session->options & OPTION_SAFE_UPDATES) && error_if_full_join(join))
524
524
    return(1);
525
525
 
526
526
  table_map tables_to_delete_from=0;
576
576
                                  table->file->ref_length,
577
577
                                  MEM_STRIP_BUF_SIZE);
578
578
  }
579
 
  return(thd->is_fatal_error != 0);
 
579
  return(session->is_fatal_error != 0);
580
580
}
581
581
 
582
582
 
626
626
      {
627
627
        deleted++;
628
628
        if (!table->file->has_transactions())
629
 
          thd->transaction.stmt.modified_non_trans_table= true;
 
629
          session->transaction.stmt.modified_non_trans_table= true;
630
630
      }
631
631
      else
632
632
      {
665
665
 
666
666
  /* the error was handled or nothing deleted and no side effects return */
667
667
  if (error_handled ||
668
 
      (!thd->transaction.stmt.modified_non_trans_table && !deleted))
 
668
      (!session->transaction.stmt.modified_non_trans_table && !deleted))
669
669
    return;
670
670
 
671
671
  /*
688
688
    return;
689
689
  }
690
690
  
691
 
  if (thd->transaction.stmt.modified_non_trans_table)
 
691
  if (session->transaction.stmt.modified_non_trans_table)
692
692
  {
693
693
    /* 
694
694
       there is only side effects; to binlog with the error
695
695
    */
696
696
    if (mysql_bin_log.is_open())
697
697
    {
698
 
      thd->binlog_query(Session::ROW_QUERY_TYPE,
699
 
                        thd->query, thd->query_length,
 
698
      session->binlog_query(Session::ROW_QUERY_TYPE,
 
699
                        session->query, session->query_length,
700
700
                        transactional_tables, false);
701
701
    }
702
 
    thd->transaction.all.modified_non_trans_table= true;
 
702
    session->transaction.all.modified_non_trans_table= true;
703
703
  }
704
704
  return;
705
705
}
739
739
    }
740
740
 
741
741
    READ_RECORD info;
742
 
    init_read_record(&info,thd,table,NULL,0,1);
 
742
    init_read_record(&info,session,table,NULL,0,1);
743
743
    /*
744
744
      Ignore any rows not found in reference tables as they may already have
745
745
      been deleted by foreign key handling
746
746
    */
747
747
    info.ignore_not_found_rows= 1;
748
748
    will_batch= !table->file->start_bulk_delete();
749
 
    while (!(local_error=info.read_record(&info)) && !thd->killed)
 
749
    while (!(local_error=info.read_record(&info)) && !session->killed)
750
750
    {
751
751
      if ((local_error=table->file->ha_delete_row(table->record[0])))
752
752
      {
764
764
      }
765
765
    }
766
766
    if (last_deleted != deleted && !table->file->has_transactions())
767
 
      thd->transaction.stmt.modified_non_trans_table= true;
 
767
      session->transaction.stmt.modified_non_trans_table= true;
768
768
    end_read_record(&info);
769
 
    if (thd->killed && !local_error)
 
769
    if (session->killed && !local_error)
770
770
      local_error= 1;
771
771
    if (local_error == -1)                              // End of file
772
772
      local_error = 0;
785
785
bool multi_delete::send_eof()
786
786
{
787
787
  Session::killed_state killed_status= Session::NOT_KILLED;
788
 
  thd->set_proc_info("deleting from reference tables");
 
788
  session->set_proc_info("deleting from reference tables");
789
789
 
790
790
  /* Does deletes for the last n - 1 tables, returns 0 if ok */
791
791
  int local_error= do_deletes();                // returns 0 if success
792
792
 
793
793
  /* compute a total error to know if something failed */
794
794
  local_error= local_error || error;
795
 
  killed_status= (local_error == 0)? Session::NOT_KILLED : thd->killed;
 
795
  killed_status= (local_error == 0)? Session::NOT_KILLED : session->killed;
796
796
  /* reset used flags */
797
 
  thd->set_proc_info("end");
 
797
  session->set_proc_info("end");
798
798
 
799
 
  if ((local_error == 0) || thd->transaction.stmt.modified_non_trans_table)
 
799
  if ((local_error == 0) || session->transaction.stmt.modified_non_trans_table)
800
800
  {
801
801
    if (mysql_bin_log.is_open())
802
802
    {
803
803
      if (local_error == 0)
804
 
        thd->clear_error();
805
 
      if (thd->binlog_query(Session::ROW_QUERY_TYPE,
806
 
                            thd->query, thd->query_length,
 
804
        session->clear_error();
 
805
      if (session->binlog_query(Session::ROW_QUERY_TYPE,
 
806
                            session->query, session->query_length,
807
807
                            transactional_tables, false, killed_status) &&
808
808
          !normal_tables)
809
809
      {
810
810
        local_error=1;  // Log write failed: roll back the SQL statement
811
811
      }
812
812
    }
813
 
    if (thd->transaction.stmt.modified_non_trans_table)
814
 
      thd->transaction.all.modified_non_trans_table= true;
 
813
    if (session->transaction.stmt.modified_non_trans_table)
 
814
      session->transaction.all.modified_non_trans_table= true;
815
815
  }
816
816
  if (local_error != 0)
817
817
    error_handled= true; // to force early leave from ::send_error()
818
818
 
819
819
  if (!local_error)
820
820
  {
821
 
    thd->row_count_func= deleted;
822
 
    ::my_ok(thd, (ha_rows) thd->row_count_func);
 
821
    session->row_count_func= deleted;
 
822
    ::my_ok(session, (ha_rows) session->row_count_func);
823
823
  }
824
824
  return 0;
825
825
}
841
841
  - If we want to have a name lock on the table on exit without errors.
842
842
*/
843
843
 
844
 
bool mysql_truncate(Session *thd, TableList *table_list, bool dont_send_ok)
 
844
bool mysql_truncate(Session *session, TableList *table_list, bool dont_send_ok)
845
845
{
846
846
  HA_CREATE_INFO create_info;
847
847
  char path[FN_REFLEN];
852
852
 
853
853
  memset(&create_info, 0, sizeof(create_info));
854
854
  /* If it is a temporary table, close and regenerate it */
855
 
  if (!dont_send_ok && (table= find_temporary_table(thd, table_list)))
 
855
  if (!dont_send_ok && (table= find_temporary_table(session, table_list)))
856
856
  {
857
857
    handlerton *table_type= table->s->db_type();
858
858
    TABLE_SHARE *share= table->s;
863
863
 
864
864
    table->file->info(HA_STATUS_AUTO | HA_STATUS_NO_LOCK);
865
865
    
866
 
    close_temporary_table(thd, table, 0, 0);    // Don't free share
867
 
    ha_create_table(thd, share->normalized_path.str,
 
866
    close_temporary_table(session, table, 0, 0);    // Don't free share
 
867
    ha_create_table(session, share->normalized_path.str,
868
868
                    share->db.str, share->table_name.str, &create_info, 1);
869
869
    // We don't need to call invalidate() because this table is not in cache
870
 
    if ((error= (int) !(open_temporary_table(thd, share->path.str,
 
870
    if ((error= (int) !(open_temporary_table(session, share->path.str,
871
871
                                             share->db.str,
872
872
                                             share->table_name.str, 1,
873
873
                                             OTM_OPEN))))
892
892
  // '\0';
893
893
  path[path_length - reg_ext_length] = 0;
894
894
  pthread_mutex_lock(&LOCK_open);
895
 
  error= ha_create_table(thd, path, table_list->db, table_list->table_name,
 
895
  error= ha_create_table(session, path, table_list->db, table_list->table_name,
896
896
                         &create_info, 1);
897
897
  pthread_mutex_unlock(&LOCK_open);
898
898
 
905
905
        TRUNCATE must always be statement-based binlogged (not row-based) so
906
906
        we don't test current_stmt_binlog_row_based.
907
907
      */
908
 
      write_bin_log(thd, true, thd->query, thd->query_length);
909
 
      my_ok(thd);               // This should return record count
 
908
      write_bin_log(session, true, session->query, session->query_length);
 
909
      my_ok(session);           // This should return record count
910
910
    }
911
911
    pthread_mutex_lock(&LOCK_open);
912
 
    unlock_table_name(thd, table_list);
 
912
    unlock_table_name(session, table_list);
913
913
    pthread_mutex_unlock(&LOCK_open);
914
914
  }
915
915
  else if (error)
916
916
  {
917
917
    pthread_mutex_lock(&LOCK_open);
918
 
    unlock_table_name(thd, table_list);
 
918
    unlock_table_name(session, table_list);
919
919
    pthread_mutex_unlock(&LOCK_open);
920
920
  }
921
921
  return(error);
922
922
 
923
923
trunc_by_del:
924
924
  /* Probably InnoDB table */
925
 
  uint64_t save_options= thd->options;
 
925
  uint64_t save_options= session->options;
926
926
  table_list->lock_type= TL_WRITE;
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,
 
927
  session->options&= ~(OPTION_BEGIN | OPTION_NOT_AUTOCOMMIT);
 
928
  ha_enable_transaction(session, false);
 
929
  mysql_init_select(session->lex);
 
930
  bool save_binlog_row_based= session->current_stmt_binlog_row_based;
 
931
  session->clear_current_stmt_binlog_row_based();
 
932
  error= mysql_delete(session, table_list, (COND*) 0, (SQL_LIST*) 0,
933
933
                      HA_POS_ERROR, 0L, true);
934
 
  ha_enable_transaction(thd, true);
 
934
  ha_enable_transaction(session, true);
935
935
  /*
936
936
    Safety, in case the engine ignored ha_enable_transaction(false)
937
 
    above. Also clears thd->transaction.*.
 
937
    above. Also clears session->transaction.*.
938
938
  */
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;
 
939
  error= ha_autocommit_or_rollback(session, error);
 
940
  ha_commit(session);
 
941
  session->options= save_options;
 
942
  session->current_stmt_binlog_row_based= save_binlog_row_based;
943
943
  return(error);
944
944
}