~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000 MySQL AB
2
3
   This program is free software; you can redistribute it and/or modify
4
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; version 2 of the License.
6
7
   This program is distributed in the hope that it will be useful,
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
   GNU General Public License for more details.
11
12
   You should have received a copy of the GNU General Public License
13
   along with this program; if not, write to the Free Software
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
16
/*
17
  Delete of records and truncate of tables.
18
19
  Multi-table deletes were introduced by Monty and Sinisa
20
*/
243.1.17 by Jay Pipes
FINAL PHASE removal of mysql_priv.h (Bye, bye my friend.)
21
#include <drizzled/server_includes.h>
22
#include <drizzled/sql_select.h>
549 by Monty Taylor
Took gettext.h out of header files.
23
#include <drizzled/error.h>
520.7.1 by Monty Taylor
Moved hash.c to drizzled.
24
#include <drizzled/probes.h>
575.4.7 by Monty Taylor
More header cleanup.
25
#include <drizzled/sql_parse.h>
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
26
#include <drizzled/sql_base.h>
670.2.4 by Monty Taylor
Removed more stuff from the headers.
27
#include <drizzled/lock.h>
1126.10.18 by Padraig O'Sullivan
Various small build fixes for when dtrace is enabled.
28
#include "drizzled/probes.h"
1 by brian
clean slate
29
1130.1.4 by Monty Taylor
Moved StorageEngine into plugin namespace.
30
using namespace drizzled;
31
1 by brian
clean slate
32
/**
33
  Implement DELETE SQL word.
34
35
  @note Like implementations of other DDL/DML in MySQL, this function
36
  relies on the caller to close the thread tables. This is done in the
37
  end of dispatch_command().
38
*/
39
520.1.22 by Brian Aker
Second pass of thd cleanup
40
bool mysql_delete(Session *session, TableList *table_list, COND *conds,
151 by Brian Aker
Ulonglong to uint64_t
41
                  SQL_LIST *order, ha_rows limit, uint64_t options,
1 by brian
clean slate
42
                  bool reset_auto_increment)
43
{
44
  bool          will_batch;
45
  int		error, loc_error;
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
46
  Table		*table;
1 by brian
clean slate
47
  SQL_SELECT	*select=0;
48
  READ_RECORD	info;
49
  bool          using_limit=limit != HA_POS_ERROR;
50
  bool		transactional_table, safe_update, const_cond;
51
  bool          const_cond_result;
52
  ha_rows	deleted= 0;
482 by Brian Aker
Remove uint.
53
  uint32_t usable_index= MAX_KEY;
846 by Brian Aker
Removing on typedeffed class.
54
  Select_Lex   *select_lex= &session->lex->select_lex;
520.1.21 by Brian Aker
THD -> Session rename
55
  Session::killed_state killed_status= Session::NOT_KILLED;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
56
1109.1.3 by Brian Aker
Move names around a bit (to align similar methods)
57
  if (session->openTablesLock(table_list))
1126.10.7 by Padraig O'Sullivan
Added calls to the dtrace delete begin/end probes.
58
  {
59
    DRIZZLE_DELETE_DONE(1, 0);
1109.1.2 by Brian Aker
More from the table patch
60
    return true;
1126.10.7 by Padraig O'Sullivan
Added calls to the dtrace delete begin/end probes.
61
  }
737 by Brian Aker
Updates for dead code removal (and forced assert() in delete).
62
63
  table= table_list->table;
64
  assert(table);
65
520.1.22 by Brian Aker
Second pass of thd cleanup
66
  session->set_proc_info("init");
1 by brian
clean slate
67
  table->map=1;
68
520.1.22 by Brian Aker
Second pass of thd cleanup
69
  if (mysql_prepare_delete(session, table_list, &conds))
1 by brian
clean slate
70
    goto err;
71
72
  /* check ORDER BY even if it can be ignored */
73
  if (order && order->elements)
74
  {
327.2.4 by Brian Aker
Refactoring table.h
75
    TableList   tables;
1 by brian
clean slate
76
    List<Item>   fields;
77
    List<Item>   all_fields;
78
212.6.6 by Mats Kindahl
Removing redundant use of casts in drizzled/ for memcmp(), memcpy(), memset(), and memmove().
79
    memset(&tables, 0, sizeof(tables));
1 by brian
clean slate
80
    tables.table = table;
81
    tables.alias = table_list->alias;
82
520.1.22 by Brian Aker
Second pass of thd cleanup
83
      if (select_lex->setup_ref_array(session, order->elements) ||
84
	  setup_order(session, select_lex->ref_pointer_array, &tables,
327.2.3 by Brian Aker
Refactoring of class Table
85
                    fields, all_fields, (order_st*) order->first))
1 by brian
clean slate
86
    {
87
      delete select;
520.1.22 by Brian Aker
Second pass of thd cleanup
88
      free_underlaid_joins(session, &session->lex->select_lex);
1 by brian
clean slate
89
      goto err;
90
    }
91
  }
92
93
  const_cond= (!conds || conds->const_item());
520.1.22 by Brian Aker
Second pass of thd cleanup
94
  safe_update=test(session->options & OPTION_SAFE_UPDATES);
1 by brian
clean slate
95
  if (safe_update && const_cond)
96
  {
97
    my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
98
               ER(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE), MYF(0));
99
    goto err;
100
  }
101
520.1.22 by Brian Aker
Second pass of thd cleanup
102
  select_lex->no_error= session->lex->ignore;
1 by brian
clean slate
103
104
  const_cond_result= const_cond && (!conds || conds->val_int());
520.1.22 by Brian Aker
Second pass of thd cleanup
105
  if (session->is_error())
1 by brian
clean slate
106
  {
107
    /* Error evaluating val_int(). */
163 by Brian Aker
Merge Monty's code.
108
    return(true);
1 by brian
clean slate
109
  }
110
111
  /*
112
    Test if the user wants to delete all rows and deletion doesn't have
113
    any side-effects (because of triggers), so we can use optimized
114
    handler::delete_all_rows() method.
115
116
    We implement fast TRUNCATE for InnoDB even if triggers are
117
    present.  TRUNCATE ignores triggers.
118
119
    We can use delete_all_rows() if and only if:
120
    - We allow new functions (not using option --skip-new), and are
121
      not in safe mode (not using option --safe-mode)
122
    - There is no limit clause
123
    - The condition is constant
124
    - If there is a condition, then it it produces a non-zero value
125
    - If the current command is DELETE FROM with no where clause
126
      (i.e., not TRUNCATE) then:
127
      - We should not be binlogging this statement row-based, and
128
      - there should be no delete triggers associated with the table.
129
  */
581 by Brian Aker
Second pass through on replication row patch
130
  if (!using_limit && const_cond_result)
1 by brian
clean slate
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()))
136
    {
137
      error= -1;				// ok
138
      deleted= maybe_deleted;
139
      goto cleanup;
140
    }
141
    if (error != HA_ERR_WRONG_COMMAND)
142
    {
143
      table->file->print_error(error,MYF(0));
144
      error=0;
145
      goto cleanup;
146
    }
147
    /* Handler didn't support fast delete; Delete rows one by one */
148
  }
149
  if (conds)
150
  {
151
    Item::cond_result result;
520.1.22 by Brian Aker
Second pass of thd cleanup
152
    conds= remove_eq_conds(session, conds, &result);
1 by brian
clean slate
153
    if (result == Item::COND_FALSE)             // Impossible where
154
      limit= 0;
155
  }
156
157
  /* Update the table->file->stats.records number */
158
  table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
159
1005.2.6 by Monty Taylor
Re-added bitset<> as a replacement for Bitmap<>
160
  table->covering_keys.reset();
161
  table->quick_keys.reset();		// Can't use 'only index'
1 by brian
clean slate
162
  select=make_select(table, 0, 0, conds, 0, &error);
163
  if (error)
164
    goto err;
520.1.22 by Brian Aker
Second pass of thd cleanup
165
  if ((select && select->check_quick(session, safe_update, limit)) || !limit)
1 by brian
clean slate
166
  {
167
    delete select;
520.1.22 by Brian Aker
Second pass of thd cleanup
168
    free_underlaid_joins(session, select_lex);
169
    session->row_count_func= 0;
1126.10.18 by Padraig O'Sullivan
Various small build fixes for when dtrace is enabled.
170
    DRIZZLE_DELETE_DONE(0, 0);
836 by Brian Aker
Fixed session call from function to method.
171
    session->my_ok((ha_rows) session->row_count_func);
1 by brian
clean slate
172
    /*
173
      We don't need to call reset_auto_increment in this case, because
174
      mysql_truncate always gives a NULL conds argument, hence we never
175
      get here.
176
    */
1126.10.7 by Padraig O'Sullivan
Added calls to the dtrace delete begin/end probes.
177
    return 0; // Nothing to delete
1 by brian
clean slate
178
  }
179
180
  /* If running in safe sql mode, don't allow updates without keys */
1005.2.6 by Monty Taylor
Re-added bitset<> as a replacement for Bitmap<>
181
  if (table->quick_keys.none())
1 by brian
clean slate
182
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
183
    session->server_status|=SERVER_QUERY_NO_INDEX_USED;
1 by brian
clean slate
184
    if (safe_update && !using_limit)
185
    {
186
      delete select;
520.1.22 by Brian Aker
Second pass of thd cleanup
187
      free_underlaid_joins(session, select_lex);
1 by brian
clean slate
188
      my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
189
                 ER(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE), MYF(0));
190
      goto err;
191
    }
192
  }
193
  if (options & OPTION_QUICK)
194
    (void) table->file->extra(HA_EXTRA_QUICK);
195
196
  if (order && order->elements)
197
  {
482 by Brian Aker
Remove uint.
198
    uint32_t         length= 0;
1 by brian
clean slate
199
    SORT_FIELD  *sortorder;
200
    ha_rows examined_rows;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
201
1005.2.6 by Monty Taylor
Re-added bitset<> as a replacement for Bitmap<>
202
    if ((!select || table->quick_keys.none()) && limit != HA_POS_ERROR)
327.2.3 by Brian Aker
Refactoring of class Table
203
      usable_index= get_index_for_order(table, (order_st*)(order->first), limit);
1 by brian
clean slate
204
205
    if (usable_index == MAX_KEY)
206
    {
684 by Brian Aker
Mass cleanup for casting.
207
      table->sort.io_cache= new IO_CACHE;
641.3.9 by Monty Taylor
More removal of my_malloc.
208
      memset(table->sort.io_cache, 0, sizeof(IO_CACHE));
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
209
210
327.2.3 by Brian Aker
Refactoring of class Table
211
      if (!(sortorder= make_unireg_sortorder((order_st*) order->first,
1 by brian
clean slate
212
                                             &length, NULL)) ||
520.1.22 by Brian Aker
Second pass of thd cleanup
213
	  (table->sort.found_records = filesort(session, table, sortorder, length,
1 by brian
clean slate
214
                                                select, HA_POS_ERROR, 1,
215
                                                &examined_rows))
216
	  == HA_POS_ERROR)
217
      {
218
        delete select;
520.1.22 by Brian Aker
Second pass of thd cleanup
219
        free_underlaid_joins(session, &session->lex->select_lex);
1 by brian
clean slate
220
        goto err;
221
      }
222
      /*
223
        Filesort has already found and selected the rows we want to delete,
224
        so we don't need the where clause
225
      */
226
      delete select;
520.1.22 by Brian Aker
Second pass of thd cleanup
227
      free_underlaid_joins(session, select_lex);
1 by brian
clean slate
228
      select= 0;
229
    }
230
  }
231
232
  /* If quick select is used, initialize it before retrieving rows. */
233
  if (select && select->quick && select->quick->reset())
234
  {
235
    delete select;
520.1.22 by Brian Aker
Second pass of thd cleanup
236
    free_underlaid_joins(session, select_lex);
1 by brian
clean slate
237
    goto err;
238
  }
239
  if (usable_index==MAX_KEY)
520.1.22 by Brian Aker
Second pass of thd cleanup
240
    init_read_record(&info,session,table,select,1,1);
1 by brian
clean slate
241
  else
520.1.22 by Brian Aker
Second pass of thd cleanup
242
    init_read_record_idx(&info, session, table, 1, usable_index);
1 by brian
clean slate
243
520.1.22 by Brian Aker
Second pass of thd cleanup
244
  session->set_proc_info("updating");
1 by brian
clean slate
245
246
  will_batch= !table->file->start_bulk_delete();
247
248
249
  table->mark_columns_needed_for_delete();
250
520.1.22 by Brian Aker
Second pass of thd cleanup
251
  while (!(error=info.read_record(&info)) && !session->killed &&
252
	 ! session->is_error())
1 by brian
clean slate
253
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
254
    // session->is_error() is tested to disallow delete row on error
255
    if (!(select && select->skip_record())&& ! session->is_error() )
1 by brian
clean slate
256
    {
257
      if (!(error= table->file->ha_delete_row(table->record[0])))
258
      {
259
	deleted++;
260
	if (!--limit && using_limit)
261
	{
262
	  error= -1;
263
	  break;
264
	}
265
      }
266
      else
267
      {
268
	table->file->print_error(error,MYF(0));
269
	/*
270
	  In < 4.0.14 we set the error number to 0 here, but that
271
	  was not sensible, because then MySQL would not roll back the
272
	  failed DELETE, and also wrote it to the binlog. For MyISAM
273
	  tables a DELETE probably never should fail (?), but for
274
	  InnoDB it can fail in a FOREIGN KEY error or an
275
	  out-of-tablespace error.
276
	*/
277
 	error= 1;
278
	break;
279
      }
280
    }
281
    else
282
      table->file->unlock_row();  // Row failed selection, release lock on it
283
  }
520.1.22 by Brian Aker
Second pass of thd cleanup
284
  killed_status= session->killed;
285
  if (killed_status != Session::NOT_KILLED || session->is_error())
1 by brian
clean slate
286
    error= 1;					// Aborted
287
  if (will_batch && (loc_error= table->file->end_bulk_delete()))
288
  {
289
    if (error != 1)
290
      table->file->print_error(loc_error,MYF(0));
291
    error=1;
292
  }
520.1.22 by Brian Aker
Second pass of thd cleanup
293
  session->set_proc_info("end");
1 by brian
clean slate
294
  end_read_record(&info);
295
  if (options & OPTION_QUICK)
296
    (void) table->file->extra(HA_EXTRA_NORMAL);
297
298
  if (reset_auto_increment && (error < 0))
299
  {
300
    /*
301
      We're really doing a truncate and need to reset the table's
302
      auto-increment counter.
303
    */
304
    int error2= table->file->ha_reset_auto_increment(0);
305
306
    if (error2 && (error2 != HA_ERR_WRONG_COMMAND))
307
    {
308
      table->file->print_error(error2, MYF(0));
309
      error= 1;
310
    }
311
  }
312
313
cleanup:
314
315
  delete select;
316
  transactional_table= table->file->has_transactions();
317
318
  if (!transactional_table && deleted > 0)
520.1.22 by Brian Aker
Second pass of thd cleanup
319
    session->transaction.stmt.modified_non_trans_table= true;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
320
1 by brian
clean slate
321
  /* See similar binlogging code in sql_update.cc, for comments */
520.1.22 by Brian Aker
Second pass of thd cleanup
322
  if ((error < 0) || session->transaction.stmt.modified_non_trans_table)
1 by brian
clean slate
323
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
324
    if (session->transaction.stmt.modified_non_trans_table)
325
      session->transaction.all.modified_non_trans_table= true;
1 by brian
clean slate
326
  }
520.1.22 by Brian Aker
Second pass of thd cleanup
327
  assert(transactional_table || !deleted || session->transaction.stmt.modified_non_trans_table);
328
  free_underlaid_joins(session, select_lex);
1 by brian
clean slate
329
1126.10.25 by Padraig O'Sullivan
Updated calls to some dtrace probes to cast the parameter to const char *
330
  DRIZZLE_DELETE_DONE((error >= 0 || session->is_error()), deleted);
520.1.22 by Brian Aker
Second pass of thd cleanup
331
  if (error < 0 || (session->lex->ignore && !session->is_fatal_error))
1 by brian
clean slate
332
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
333
    session->row_count_func= deleted;
836 by Brian Aker
Fixed session call from function to method.
334
    session->my_ok((ha_rows) session->row_count_func);
1 by brian
clean slate
335
  }
1126.10.7 by Padraig O'Sullivan
Added calls to the dtrace delete begin/end probes.
336
  return (error >= 0 || session->is_error());
1 by brian
clean slate
337
338
err:
1126.10.18 by Padraig O'Sullivan
Various small build fixes for when dtrace is enabled.
339
  DRIZZLE_DELETE_DONE(1, 0);
1126.10.7 by Padraig O'Sullivan
Added calls to the dtrace delete begin/end probes.
340
  return true;
1 by brian
clean slate
341
}
342
343
344
/*
345
  Prepare items in DELETE statement
346
347
  SYNOPSIS
348
    mysql_prepare_delete()
520.1.22 by Brian Aker
Second pass of thd cleanup
349
    session			- thread handler
1 by brian
clean slate
350
    table_list		- global/local table list
351
    conds		- conditions
352
353
  RETURN VALUE
163 by Brian Aker
Merge Monty's code.
354
    false OK
355
    true  error
1 by brian
clean slate
356
*/
520.1.22 by Brian Aker
Second pass of thd cleanup
357
int mysql_prepare_delete(Session *session, TableList *table_list, Item **conds)
1 by brian
clean slate
358
{
846 by Brian Aker
Removing on typedeffed class.
359
  Select_Lex *select_lex= &session->lex->select_lex;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
360
1 by brian
clean slate
361
  List<Item> all_fields;
362
520.1.22 by Brian Aker
Second pass of thd cleanup
363
  session->lex->allow_sum_func= 0;
364
  if (setup_tables_and_check_access(session, &session->lex->select_lex.context,
365
                                    &session->lex->select_lex.top_join_list,
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
366
                                    table_list,
1 by brian
clean slate
367
                                    &select_lex->leaf_tables, false) ||
1109.1.5 by Brian Aker
More extraction from sql_base
368
      session->setup_conds(table_list, conds))
163 by Brian Aker
Merge Monty's code.
369
    return(true);
1 by brian
clean slate
370
  {
327.2.4 by Brian Aker
Refactoring table.h
371
    TableList *duplicate;
520.1.22 by Brian Aker
Second pass of thd cleanup
372
    if ((duplicate= unique_table(session, table_list, table_list->next_global, 0)))
1 by brian
clean slate
373
    {
1054.1.9 by Brian Aker
This is a large number of refactors against the Session class for its
374
      my_error(ER_UPDATE_TABLE_USED, MYF(0), table_list->alias);
163 by Brian Aker
Merge Monty's code.
375
      return(true);
1 by brian
clean slate
376
    }
377
  }
378
379
  if (select_lex->inner_refs_list.elements &&
520.1.22 by Brian Aker
Second pass of thd cleanup
380
    fix_inner_refs(session, all_fields, select_lex, select_lex->ref_pointer_array))
51.2.2 by Patrick Galbraith
Removed DBUGs from
381
    return(-1);
1 by brian
clean slate
382
163 by Brian Aker
Merge Monty's code.
383
  return(false);
1 by brian
clean slate
384
}
385
386
387
/***************************************************************************
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
388
  TRUNCATE Table
1 by brian
clean slate
389
****************************************************************************/
390
391
/*
392
  Optimize delete of all rows by doing a full generate of the table
393
  This will work even if the .ISM and .ISD tables are destroyed
394
395
  dont_send_ok should be set if:
396
  - We should always wants to generate the table (even if the table type
397
    normally can't safely do this.
398
  - We don't want an ok to be sent to the end user.
399
  - We don't want to log the truncate command
400
  - If we want to have a name lock on the table on exit without errors.
401
*/
402
520.1.22 by Brian Aker
Second pass of thd cleanup
403
bool mysql_truncate(Session *session, TableList *table_list, bool dont_send_ok)
1 by brian
clean slate
404
{
405
  HA_CREATE_INFO create_info;
406
  char path[FN_REFLEN];
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
407
  Table *table;
1 by brian
clean slate
408
  bool error;
482 by Brian Aker
Remove uint.
409
  uint32_t path_length;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
410
1 by brian
clean slate
411
212.6.6 by Mats Kindahl
Removing redundant use of casts in drizzled/ for memcmp(), memcpy(), memset(), and memmove().
412
  memset(&create_info, 0, sizeof(create_info));
1 by brian
clean slate
413
  /* If it is a temporary table, close and regenerate it */
1054.1.9 by Brian Aker
This is a large number of refactors against the Session class for its
414
  if (!dont_send_ok && (table= session->find_temporary_table(table_list)))
1 by brian
clean slate
415
  {
1130.1.4 by Monty Taylor
Moved StorageEngine into plugin namespace.
416
    plugin::StorageEngine *table_type= table->s->db_type();
1000.1.3 by Brian Aker
Renamed TABLE_SHARE to TableShare
417
    TableShare *share= table->s;
1 by brian
clean slate
418
964.1.4 by Monty Taylor
Moved flags into private area.
419
    if (!table_type->check_flag(HTON_BIT_CAN_RECREATE))
1 by brian
clean slate
420
      goto trunc_by_del;
421
422
    table->file->info(HA_STATUS_AUTO | HA_STATUS_NO_LOCK);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
423
1054.1.9 by Brian Aker
This is a large number of refactors against the Session class for its
424
    session->close_temporary_table(table, false, false);    // Don't free share
1160.1.1 by Brian Aker
Refactor SE createTable back to engine class.
425
    plugin::StorageEngine::createTable(session, share->normalized_path.str,
426
                                       share->db.str, share->table_name.str, &create_info,
427
                                       true, NULL);
1 by brian
clean slate
428
    // We don't need to call invalidate() because this table is not in cache
1109.1.1 by Brian Aker
Applying refactor of tmp table bits back to session. (this all needs to be
429
    if ((error= (int) !(session->open_temporary_table(share->path.str,
430
                                                      share->db.str,
431
                                                      share->table_name.str, 1,
432
                                                      OTM_OPEN))))
433
      (void) session->rm_temporary_table(table_type, path);
1000.1.5 by Brian Aker
More refactoring back to TableShare object.
434
    share->free_table_share();
477 by Monty Taylor
Removed my_free(). It turns out that it had been def'd to ignore the flags passed to it in the second arg anyway. Gotta love that.
435
    free((char*) table);
1 by brian
clean slate
436
    /*
437
      If we return here we will not have logged the truncation to the bin log
438
      and we will not my_ok() to the client.
439
    */
440
    goto end;
441
  }
442
443
  path_length= build_table_filename(path, sizeof(path), table_list->db,
1039.1.6 by Brian Aker
Refactor for build_table_filename()
444
                                    table_list->table_name, 0);
1 by brian
clean slate
445
446
  if (!dont_send_ok)
410 by Brian Aker
Removed legacy type, but truncate is no longer doing recreate. This should
447
    goto trunc_by_del;
1 by brian
clean slate
448
1046.1.2 by Brian Aker
Comments on LOCK_open
449
  pthread_mutex_lock(&LOCK_open); /* Recreate table for truncate */
1160.1.1 by Brian Aker
Refactor SE createTable back to engine class.
450
  error= plugin::StorageEngine::createTable(session, path, table_list->db, table_list->table_name,
451
                                            &create_info, true, NULL);
398.1.10 by Monty Taylor
Actually removed VOID() this time.
452
  pthread_mutex_unlock(&LOCK_open);
1 by brian
clean slate
453
454
end:
455
  if (!dont_send_ok)
456
  {
457
    if (!error)
458
    {
459
      /*
460
        TRUNCATE must always be statement-based binlogged (not row-based) so
461
        we don't test current_stmt_binlog_row_based.
462
      */
520.1.22 by Brian Aker
Second pass of thd cleanup
463
      write_bin_log(session, true, session->query, session->query_length);
836 by Brian Aker
Fixed session call from function to method.
464
      session->my_ok();		// This should return record count
1 by brian
clean slate
465
    }
1046.1.2 by Brian Aker
Comments on LOCK_open
466
    pthread_mutex_lock(&LOCK_open); /* For truncate delete from hash when finished */
1017 by Brian Aker
Drop dead session pass
467
    unlock_table_name(table_list);
398.1.10 by Monty Taylor
Actually removed VOID() this time.
468
    pthread_mutex_unlock(&LOCK_open);
1 by brian
clean slate
469
  }
470
  else if (error)
471
  {
1046.1.2 by Brian Aker
Comments on LOCK_open
472
    pthread_mutex_lock(&LOCK_open); /* For truncate delete from hash when finished */
1017 by Brian Aker
Drop dead session pass
473
    unlock_table_name(table_list);
398.1.10 by Monty Taylor
Actually removed VOID() this time.
474
    pthread_mutex_unlock(&LOCK_open);
1 by brian
clean slate
475
  }
51.2.2 by Patrick Galbraith
Removed DBUGs from
476
  return(error);
1 by brian
clean slate
477
478
trunc_by_del:
479
  /* Probably InnoDB table */
520.1.22 by Brian Aker
Second pass of thd cleanup
480
  uint64_t save_options= session->options;
1 by brian
clean slate
481
  table_list->lock_type= TL_WRITE;
520.1.22 by Brian Aker
Second pass of thd cleanup
482
  session->options&= ~(OPTION_BEGIN | OPTION_NOT_AUTOCOMMIT);
483
  ha_enable_transaction(session, false);
484
  mysql_init_select(session->lex);
485
  error= mysql_delete(session, table_list, (COND*) 0, (SQL_LIST*) 0,
398.1.8 by Monty Taylor
Enabled -Wlong-long.
486
                      HA_POS_ERROR, 0L, true);
520.1.22 by Brian Aker
Second pass of thd cleanup
487
  ha_enable_transaction(session, true);
1 by brian
clean slate
488
  /*
163 by Brian Aker
Merge Monty's code.
489
    Safety, in case the engine ignored ha_enable_transaction(false)
520.1.22 by Brian Aker
Second pass of thd cleanup
490
    above. Also clears session->transaction.*.
1 by brian
clean slate
491
  */
520.1.22 by Brian Aker
Second pass of thd cleanup
492
  error= ha_autocommit_or_rollback(session, error);
493
  ha_commit(session);
494
  session->options= save_options;
51.2.2 by Patrick Galbraith
Removed DBUGs from
495
  return(error);
1 by brian
clean slate
496
}