~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000-2006 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
/* Insert of records */
18
1241.9.36 by Monty Taylor
ZOMG. I deleted drizzled/server_includes.h.
19
#include "config.h"
243.1.17 by Jay Pipes
FINAL PHASE removal of mysql_priv.h (Bye, bye my friend.)
20
#include <drizzled/sql_select.h>
575.4.7 by Monty Taylor
More header cleanup.
21
#include <drizzled/show.h>
549 by Monty Taylor
Took gettext.h out of header files.
22
#include <drizzled/error.h>
584.4.4 by Monty Taylor
Split out Name_resolution_context.
23
#include <drizzled/name_resolution_context_state.h>
520.7.1 by Monty Taylor
Moved hash.c to drizzled.
24
#include <drizzled/probes.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.
25
#include <drizzled/sql_base.h>
26
#include <drizzled/sql_load.h>
584.5.1 by Monty Taylor
Removed field includes from field.h.
27
#include <drizzled/field/timestamp.h>
670.2.4 by Monty Taylor
Removed more stuff from the headers.
28
#include <drizzled/lock.h>
1241.9.23 by Monty Taylor
Removed sql_table.h from server_includes.h.
29
#include "drizzled/sql_table.h"
1241.9.31 by Monty Taylor
Moved global pthread variables into their own header.
30
#include "drizzled/pthread_globals.h"
1273.1.2 by Jay Pipes
This patch does not change any algorithms or code paths,
31
#include "drizzled/transaction_services.h"
1 by brian
clean slate
32
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
33
namespace drizzled
34
{
1 by brian
clean slate
35
1241.9.32 by Monty Taylor
Moved global myisam and heap pointers out of server_includes.
36
extern plugin::StorageEngine *heap_engine;
37
extern plugin::StorageEngine *myisam_engine;
38
1 by brian
clean slate
39
/*
40
  Check if insert fields are correct.
41
42
  SYNOPSIS
43
    check_insert_fields()
520.1.22 by Brian Aker
Second pass of thd cleanup
44
    session                         The current thread.
1 by brian
clean slate
45
    table                       The table for insert.
46
    fields                      The insert fields.
47
    values                      The insert values.
48
    check_unique                If duplicate values should be rejected.
49
50
  NOTE
51
    Clears TIMESTAMP_AUTO_SET_ON_INSERT from table->timestamp_field_type
52
    or leaves it as is, depending on if timestamp should be updated or
53
    not.
54
55
  RETURN
56
    0           OK
57
    -1          Error
58
*/
59
520.1.22 by Brian Aker
Second pass of thd cleanup
60
static int check_insert_fields(Session *session, TableList *table_list,
1 by brian
clean slate
61
                               List<Item> &fields, List<Item> &values,
77.1.45 by Monty Taylor
Warning fixes.
62
                               bool check_unique,
779.1.27 by Monty Taylor
Got rid of __attribute__((unused)) and the like from the .cc files.
63
                               table_map *)
1 by brian
clean slate
64
{
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
65
  Table *table= table_list->table;
1 by brian
clean slate
66
67
  if (fields.elements == 0 && values.elements != 0)
68
  {
69
    if (values.elements != table->s->fields)
70
    {
71
      my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), 1L);
72
      return -1;
73
    }
74
    clear_timestamp_auto_bits(table->timestamp_field_type,
75
                              TIMESTAMP_AUTO_SET_ON_INSERT);
76
    /*
77
      No fields are provided so all fields must be provided in the values.
78
      Thus we set all bits in the write set.
79
    */
1005.2.12 by Monty Taylor
Moved some things to the API.
80
    table->setWriteSet();
1 by brian
clean slate
81
  }
82
  else
83
  {						// Part field list
846 by Brian Aker
Removing on typedeffed class.
84
    Select_Lex *select_lex= &session->lex->select_lex;
1 by brian
clean slate
85
    Name_resolution_context *context= &select_lex->context;
86
    Name_resolution_context_state ctx_state;
87
    int res;
88
89
    if (fields.elements != values.elements)
90
    {
91
      my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), 1L);
92
      return -1;
93
    }
94
520.1.22 by Brian Aker
Second pass of thd cleanup
95
    session->dup_field= 0;
1 by brian
clean slate
96
97
    /* Save the state of the current name resolution context. */
98
    ctx_state.save_state(context, table_list);
99
100
    /*
101
      Perform name resolution only in the first table - 'table_list',
102
      which is the table that is inserted into.
103
    */
104
    table_list->next_local= 0;
105
    context->resolve_in_table_list_only(table_list);
520.1.22 by Brian Aker
Second pass of thd cleanup
106
    res= setup_fields(session, 0, fields, MARK_COLUMNS_WRITE, 0, 0);
1 by brian
clean slate
107
108
    /* Restore the current context. */
109
    ctx_state.restore_state(context, table_list);
110
111
    if (res)
112
      return -1;
113
520.1.22 by Brian Aker
Second pass of thd cleanup
114
    if (check_unique && session->dup_field)
1 by brian
clean slate
115
    {
520.1.22 by Brian Aker
Second pass of thd cleanup
116
      my_error(ER_FIELD_SPECIFIED_TWICE, MYF(0), session->dup_field->field_name);
1 by brian
clean slate
117
      return -1;
118
    }
119
    if (table->timestamp_field)	// Don't automaticly set timestamp if used
120
    {
1009 by Brian Aker
Merge of Monty
121
      if (table->timestamp_field->isWriteSet())
1 by brian
clean slate
122
        clear_timestamp_auto_bits(table->timestamp_field_type,
123
                                  TIMESTAMP_AUTO_SET_ON_INSERT);
124
      else
125
      {
1005.2.12 by Monty Taylor
Moved some things to the API.
126
        table->setWriteSet(table->timestamp_field->field_index);
1 by brian
clean slate
127
      }
128
    }
129
  }
130
131
  return 0;
132
}
133
134
135
/*
136
  Check update fields for the timestamp field.
137
138
  SYNOPSIS
139
    check_update_fields()
520.1.22 by Brian Aker
Second pass of thd cleanup
140
    session                         The current thread.
1 by brian
clean slate
141
    insert_table_list           The insert table list.
142
    table                       The table for update.
143
    update_fields               The update fields.
144
145
  NOTE
146
    If the update fields include the timestamp field,
147
    remove TIMESTAMP_AUTO_SET_ON_UPDATE from table->timestamp_field_type.
148
149
  RETURN
150
    0           OK
151
    -1          Error
152
*/
153
520.1.22 by Brian Aker
Second pass of thd cleanup
154
static int check_update_fields(Session *session, TableList *insert_table_list,
77.1.45 by Monty Taylor
Warning fixes.
155
                               List<Item> &update_fields,
779.1.27 by Monty Taylor
Got rid of __attribute__((unused)) and the like from the .cc files.
156
                               table_map *)
1 by brian
clean slate
157
{
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
158
  Table *table= insert_table_list->table;
199 by Brian Aker
my_bool...
159
  bool timestamp_mark= false;
1 by brian
clean slate
160
161
  if (table->timestamp_field)
162
  {
163
    /*
164
      Unmark the timestamp field so that we can check if this is modified
165
      by update_fields
166
    */
1103.6.4 by Padraig O'Sullivan
Correct all remaining references to the old bitmap interface in the code
167
    timestamp_mark= table->write_set->testAndClear(table->timestamp_field->field_index);
1 by brian
clean slate
168
  }
169
170
  /* Check the fields we are going to modify */
520.1.22 by Brian Aker
Second pass of thd cleanup
171
  if (setup_fields(session, 0, update_fields, MARK_COLUMNS_WRITE, 0, 0))
1 by brian
clean slate
172
    return -1;
173
174
  if (table->timestamp_field)
175
  {
176
    /* Don't set timestamp column if this is modified. */
1009 by Brian Aker
Merge of Monty
177
    if (table->timestamp_field->isWriteSet())
1 by brian
clean slate
178
      clear_timestamp_auto_bits(table->timestamp_field_type,
179
                                TIMESTAMP_AUTO_SET_ON_UPDATE);
180
    if (timestamp_mark)
1005.2.12 by Monty Taylor
Moved some things to the API.
181
      table->setWriteSet(table->timestamp_field->field_index);
1 by brian
clean slate
182
  }
183
  return 0;
184
}
185
186
187
/**
188
  Upgrade table-level lock of INSERT statement to TL_WRITE if
189
  a more concurrent lock is infeasible for some reason. This is
190
  necessary for engines without internal locking support (MyISAM).
191
  An engine with internal locking implementation might later
192
  downgrade the lock in handler::store_lock() method.
193
*/
194
195
static
779.1.27 by Monty Taylor
Got rid of __attribute__((unused)) and the like from the .cc files.
196
void upgrade_lock_type(Session *,
77.1.45 by Monty Taylor
Warning fixes.
197
                       thr_lock_type *lock_type,
1 by brian
clean slate
198
                       enum_duplicates duplic,
779.1.27 by Monty Taylor
Got rid of __attribute__((unused)) and the like from the .cc files.
199
                       bool )
1 by brian
clean slate
200
{
201
  if (duplic == DUP_UPDATE ||
202
      (duplic == DUP_REPLACE && *lock_type == TL_WRITE_CONCURRENT_INSERT))
203
  {
204
    *lock_type= TL_WRITE_DEFAULT;
205
    return;
206
  }
207
}
208
209
210
/**
211
  INSERT statement implementation
212
213
  @note Like implementations of other DDL/DML in MySQL, this function
214
  relies on the caller to close the thread tables. This is done in the
215
  end of dispatch_command().
216
*/
217
520.1.22 by Brian Aker
Second pass of thd cleanup
218
bool mysql_insert(Session *session,TableList *table_list,
1 by brian
clean slate
219
                  List<Item> &fields,
220
                  List<List_item> &values_list,
221
                  List<Item> &update_fields,
222
                  List<Item> &update_values,
223
                  enum_duplicates duplic,
224
		  bool ignore)
225
{
226
  int error;
163 by Brian Aker
Merge Monty's code.
227
  bool transactional_table, joins_freed= false;
1 by brian
clean slate
228
  bool changed;
482 by Brian Aker
Remove uint.
229
  uint32_t value_count;
1 by brian
clean slate
230
  ulong counter = 1;
151 by Brian Aker
Ulonglong to uint64_t
231
  uint64_t id;
1 by brian
clean slate
232
  COPY_INFO info;
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
233
  Table *table= 0;
1 by brian
clean slate
234
  List_iterator_fast<List_item> its(values_list);
235
  List_item *values;
236
  Name_resolution_context *context;
237
  Name_resolution_context_state ctx_state;
238
  thr_lock_type lock_type;
239
  Item *unused_conds= 0;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
240
1 by brian
clean slate
241
242
  /*
243
    Upgrade lock type if the requested lock is incompatible with
244
    the current connection mode or table operation.
245
  */
520.1.22 by Brian Aker
Second pass of thd cleanup
246
  upgrade_lock_type(session, &table_list->lock_type, duplic,
1 by brian
clean slate
247
                    values_list.elements > 1);
248
1109.1.3 by Brian Aker
Move names around a bit (to align similar methods)
249
  if (session->openTablesLock(table_list))
1126.10.12 by Padraig O'Sullivan
Added calls to the dtrace probes related to insertion.
250
  {
251
    DRIZZLE_INSERT_DONE(1, 0);
1109.1.2 by Brian Aker
More from the table patch
252
    return true;
1126.10.12 by Padraig O'Sullivan
Added calls to the dtrace probes related to insertion.
253
  }
1 by brian
clean slate
254
255
  lock_type= table_list->lock_type;
256
520.1.22 by Brian Aker
Second pass of thd cleanup
257
  session->set_proc_info("init");
258
  session->used_tables=0;
1 by brian
clean slate
259
  values= its++;
260
  value_count= values->elements;
261
520.1.22 by Brian Aker
Second pass of thd cleanup
262
  if (mysql_prepare_insert(session, table_list, table, fields, values,
1 by brian
clean slate
263
			   update_fields, update_values, duplic, &unused_conds,
163 by Brian Aker
Merge Monty's code.
264
                           false,
1 by brian
clean slate
265
                           (fields.elements || !value_count ||
266
                            (0) != 0), !ignore))
267
    goto abort;
268
269
  /* mysql_prepare_insert set table_list->table if it was not set */
270
  table= table_list->table;
271
520.1.22 by Brian Aker
Second pass of thd cleanup
272
  context= &session->lex->select_lex.context;
1 by brian
clean slate
273
  /*
274
    These three asserts test the hypothesis that the resetting of the name
275
    resolution context below is not necessary at all since the list of local
276
    tables for INSERT always consists of one table.
277
  */
51.2.2 by Patrick Galbraith
Removed DBUGs from
278
  assert(!table_list->next_local);
279
  assert(!context->table_list->next_local);
280
  assert(!context->first_name_resolution_table->next_name_resolution_table);
1 by brian
clean slate
281
282
  /* Save the state of the current name resolution context. */
283
  ctx_state.save_state(context, table_list);
284
285
  /*
286
    Perform name resolution only in the first table - 'table_list',
287
    which is the table that is inserted into.
288
  */
289
  table_list->next_local= 0;
290
  context->resolve_in_table_list_only(table_list);
291
292
  while ((values= its++))
293
  {
294
    counter++;
295
    if (values->elements != value_count)
296
    {
297
      my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), counter);
298
      goto abort;
299
    }
520.1.22 by Brian Aker
Second pass of thd cleanup
300
    if (setup_fields(session, 0, *values, MARK_COLUMNS_READ, 0, 0))
1 by brian
clean slate
301
      goto abort;
302
  }
303
  its.rewind ();
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
304
1 by brian
clean slate
305
  /* Restore the current context. */
306
  ctx_state.restore_state(context, table_list);
307
308
  /*
1208.3.2 by brian
Update for Cursor renaming.
309
    Fill in the given fields and dump it to the table cursor
1 by brian
clean slate
310
  */
212.6.6 by Mats Kindahl
Removing redundant use of casts in drizzled/ for memcmp(), memcpy(), memset(), and memmove().
311
  memset(&info, 0, sizeof(info));
1 by brian
clean slate
312
  info.ignore= ignore;
313
  info.handle_duplicates=duplic;
314
  info.update_fields= &update_fields;
315
  info.update_values= &update_values;
316
317
  /*
318
    Count warnings for all inserts.
319
    For single line insert, generate an error if try to set a NOT NULL field
320
    to NULL.
321
  */
520.1.22 by Brian Aker
Second pass of thd cleanup
322
  session->count_cuted_fields= ((values_list.elements == 1 &&
1235.1.11 by Brian Aker
Small cleanups, did in MERGE table only engine flag.
323
                                 !ignore) ?
324
                                CHECK_FIELD_ERROR_FOR_NULL :
325
                                CHECK_FIELD_WARN);
520.1.22 by Brian Aker
Second pass of thd cleanup
326
  session->cuted_fields = 0L;
1 by brian
clean slate
327
  table->next_number_field=table->found_next_number_field;
328
329
  error=0;
520.1.22 by Brian Aker
Second pass of thd cleanup
330
  session->set_proc_info("update");
1 by brian
clean slate
331
  if (duplic == DUP_REPLACE)
1208.3.2 by brian
Update for Cursor renaming.
332
    table->cursor->extra(HA_EXTRA_WRITE_CAN_REPLACE);
1 by brian
clean slate
333
  if (duplic == DUP_UPDATE)
1208.3.2 by brian
Update for Cursor renaming.
334
    table->cursor->extra(HA_EXTRA_INSERT_WITH_UPDATE);
1 by brian
clean slate
335
  {
336
    if (duplic != DUP_ERROR || ignore)
1208.3.2 by brian
Update for Cursor renaming.
337
      table->cursor->extra(HA_EXTRA_IGNORE_DUP_KEY);
338
    table->cursor->ha_start_bulk_insert(values_list.elements);
1 by brian
clean slate
339
  }
340
341
520.1.22 by Brian Aker
Second pass of thd cleanup
342
  session->abort_on_warning= !ignore;
1 by brian
clean slate
343
344
  table->mark_columns_needed_for_insert();
345
346
  while ((values= its++))
347
  {
348
    if (fields.elements || !value_count)
349
    {
997.5.1 by chris
Replace macros around unireg.h, store_record,restore_record,cmp_record,empty_record
350
      table->restoreRecordAsDefault();	// Get empty record
1235.1.11 by Brian Aker
Small cleanups, did in MERGE table only engine flag.
351
      if (fill_record(session, fields, *values))
1 by brian
clean slate
352
      {
520.1.22 by Brian Aker
Second pass of thd cleanup
353
	if (values_list.elements != 1 && ! session->is_error())
1 by brian
clean slate
354
	{
355
	  info.records++;
356
	  continue;
357
	}
358
	/*
520.1.22 by Brian Aker
Second pass of thd cleanup
359
	  TODO: set session->abort_on_warning if values_list.elements == 1
1 by brian
clean slate
360
	  and check that all items return warning in case of problem with
361
	  storing field.
362
        */
363
	error=1;
364
	break;
365
      }
366
    }
367
    else
368
    {
1222.3.1 by Stewart Smith
mi_write valgrind warning in alias test
369
      table->restoreRecordAsDefault();	// Get empty record
370
1235.1.11 by Brian Aker
Small cleanups, did in MERGE table only engine flag.
371
      if (fill_record(session, table->field, *values))
1 by brian
clean slate
372
      {
520.1.22 by Brian Aker
Second pass of thd cleanup
373
	if (values_list.elements != 1 && ! session->is_error())
1 by brian
clean slate
374
	{
375
	  info.records++;
376
	  continue;
377
	}
378
	error=1;
379
	break;
380
      }
381
    }
382
873.2.11 by Monty Taylor
call ha_release_temporary_latches
383
    // Release latches in case bulk insert takes a long time
1152.1.4 by Brian Aker
Remove wrappers from SE
384
    plugin::StorageEngine::releaseTemporaryLatches(session);
873.2.11 by Monty Taylor
call ha_release_temporary_latches
385
520.1.22 by Brian Aker
Second pass of thd cleanup
386
    error=write_record(session, table ,&info);
1 by brian
clean slate
387
    if (error)
388
      break;
520.1.22 by Brian Aker
Second pass of thd cleanup
389
    session->row_count++;
1 by brian
clean slate
390
  }
391
520.1.22 by Brian Aker
Second pass of thd cleanup
392
  free_underlaid_joins(session, &session->lex->select_lex);
163 by Brian Aker
Merge Monty's code.
393
  joins_freed= true;
1 by brian
clean slate
394
395
  /*
396
    Now all rows are inserted.  Time to update logs and sends response to
397
    user
398
  */
399
  {
400
    /*
401
      Do not do this release if this is a delayed insert, it would steal
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
402
      auto_inc values from the delayed_insert thread as they share Table.
1 by brian
clean slate
403
    */
1208.3.2 by brian
Update for Cursor renaming.
404
    table->cursor->ha_release_auto_increment();
405
    if (table->cursor->ha_end_bulk_insert() && !error)
1 by brian
clean slate
406
    {
1241.9.57 by Monty Taylor
Oy. Bigger change than I normally like - but this stuff is all intertwined.
407
      table->print_error(errno,MYF(0));
1 by brian
clean slate
408
      error=1;
409
    }
410
    if (duplic != DUP_ERROR || ignore)
1208.3.2 by brian
Update for Cursor renaming.
411
      table->cursor->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
1 by brian
clean slate
412
1208.3.2 by brian
Update for Cursor renaming.
413
    transactional_table= table->cursor->has_transactions();
1 by brian
clean slate
414
831.1.1 by Brian Aker
Cleanup of DELAYED INSERT bits (aka... it is just now a hint to the engine).
415
    changed= (info.copied || info.deleted || info.updated);
416
    if ((changed && error <= 0) || session->transaction.stmt.modified_non_trans_table)
1 by brian
clean slate
417
    {
520.1.22 by Brian Aker
Second pass of thd cleanup
418
      if (session->transaction.stmt.modified_non_trans_table)
419
	session->transaction.all.modified_non_trans_table= true;
1 by brian
clean slate
420
    }
831.1.1 by Brian Aker
Cleanup of DELAYED INSERT bits (aka... it is just now a hint to the engine).
421
    assert(transactional_table || !changed || session->transaction.stmt.modified_non_trans_table);
1 by brian
clean slate
422
423
  }
520.1.22 by Brian Aker
Second pass of thd cleanup
424
  session->set_proc_info("end");
1 by brian
clean slate
425
  /*
426
    We'll report to the client this id:
427
    - if the table contains an autoincrement column and we successfully
428
    inserted an autogenerated value, the autogenerated value.
429
    - if the table contains no autoincrement column and LAST_INSERT_ID(X) was
430
    called, X.
431
    - if the table contains an autoincrement column, and some rows were
432
    inserted, the id of the last "inserted" row (if IGNORE, that value may not
433
    have been really inserted but ignored).
434
  */
520.1.22 by Brian Aker
Second pass of thd cleanup
435
  id= (session->first_successful_insert_id_in_cur_stmt > 0) ?
436
    session->first_successful_insert_id_in_cur_stmt :
437
    (session->arg_of_last_insert_id_function ?
438
     session->first_successful_insert_id_in_prev_stmt :
1 by brian
clean slate
439
     ((table->next_number_field && info.copied) ?
440
     table->next_number_field->val_int() : 0));
441
  table->next_number_field=0;
520.1.22 by Brian Aker
Second pass of thd cleanup
442
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
163 by Brian Aker
Merge Monty's code.
443
  table->auto_increment_field_not_null= false;
1 by brian
clean slate
444
  if (duplic == DUP_REPLACE)
1208.3.2 by brian
Update for Cursor renaming.
445
    table->cursor->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
1 by brian
clean slate
446
447
  if (error)
448
    goto abort;
520.1.22 by Brian Aker
Second pass of thd cleanup
449
  if (values_list.elements == 1 && (!(session->options & OPTION_WARNINGS) ||
450
				    !session->cuted_fields))
1 by brian
clean slate
451
  {
971.3.59 by Eric Day
Removed client_capabilities from session and pushed functionality into protocol plugin.
452
    session->row_count_func= info.copied + info.deleted + info.updated;
453
    session->my_ok((ulong) session->row_count_func,
454
                   info.copied + info.deleted + info.touched, id);
1 by brian
clean slate
455
  }
456
  else
457
  {
458
    char buff[160];
459
    if (ignore)
460
      sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
520.1.22 by Brian Aker
Second pass of thd cleanup
461
              (ulong) (info.records - info.copied), (ulong) session->cuted_fields);
1 by brian
clean slate
462
    else
463
      sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
971.3.59 by Eric Day
Removed client_capabilities from session and pushed functionality into protocol plugin.
464
	      (ulong) (info.deleted + info.updated), (ulong) session->cuted_fields);
465
    session->row_count_func= info.copied + info.deleted + info.updated;
466
    session->my_ok((ulong) session->row_count_func,
467
                   info.copied + info.deleted + info.touched, id, buff);
1 by brian
clean slate
468
  }
520.1.22 by Brian Aker
Second pass of thd cleanup
469
  session->abort_on_warning= 0;
1126.10.12 by Padraig O'Sullivan
Added calls to the dtrace probes related to insertion.
470
  DRIZZLE_INSERT_DONE(0, session->row_count_func);
471
  return false;
1 by brian
clean slate
472
473
abort:
474
  if (table != NULL)
1208.3.2 by brian
Update for Cursor renaming.
475
    table->cursor->ha_release_auto_increment();
1 by brian
clean slate
476
  if (!joins_freed)
520.1.22 by Brian Aker
Second pass of thd cleanup
477
    free_underlaid_joins(session, &session->lex->select_lex);
478
  session->abort_on_warning= 0;
1126.10.18 by Padraig O'Sullivan
Various small build fixes for when dtrace is enabled.
479
  DRIZZLE_INSERT_DONE(1, 0);
1126.10.12 by Padraig O'Sullivan
Added calls to the dtrace probes related to insertion.
480
  return true;
1 by brian
clean slate
481
}
482
483
484
/*
485
  Check if table can be updated
486
487
  SYNOPSIS
488
     mysql_prepare_insert_check_table()
520.1.22 by Brian Aker
Second pass of thd cleanup
489
     session		Thread handle
1 by brian
clean slate
490
     table_list		Table list
491
     fields		List of fields to be updated
492
     where		Pointer to where clause
493
     select_insert      Check is making for SELECT ... INSERT
494
495
   RETURN
163 by Brian Aker
Merge Monty's code.
496
     false ok
497
     true  ERROR
1 by brian
clean slate
498
*/
499
520.1.22 by Brian Aker
Second pass of thd cleanup
500
static bool mysql_prepare_insert_check_table(Session *session, TableList *table_list,
779.1.27 by Monty Taylor
Got rid of __attribute__((unused)) and the like from the .cc files.
501
                                             List<Item> &,
1 by brian
clean slate
502
                                             bool select_insert)
503
{
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
504
1 by brian
clean slate
505
506
  /*
507
     first table in list is the one we'll INSERT into, requires INSERT_ACL.
508
     all others require SELECT_ACL only. the ACL requirement below is for
509
     new leaves only anyway (view-constituents), so check for SELECT rather
510
     than INSERT.
511
  */
512
520.1.22 by Brian Aker
Second pass of thd cleanup
513
  if (setup_tables_and_check_access(session, &session->lex->select_lex.context,
514
                                    &session->lex->select_lex.top_join_list,
1 by brian
clean slate
515
                                    table_list,
520.1.22 by Brian Aker
Second pass of thd cleanup
516
                                    &session->lex->select_lex.leaf_tables,
1 by brian
clean slate
517
                                    select_insert))
163 by Brian Aker
Merge Monty's code.
518
    return(true);
1 by brian
clean slate
519
163 by Brian Aker
Merge Monty's code.
520
  return(false);
1 by brian
clean slate
521
}
522
523
524
/*
525
  Prepare items in INSERT statement
526
527
  SYNOPSIS
528
    mysql_prepare_insert()
520.1.22 by Brian Aker
Second pass of thd cleanup
529
    session			Thread handler
1 by brian
clean slate
530
    table_list	        Global/local table list
531
    table		Table to insert into (can be NULL if table should
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
532
			be taken from table_list->table)
1 by brian
clean slate
533
    where		Where clause (for insert ... select)
163 by Brian Aker
Merge Monty's code.
534
    select_insert	true if INSERT ... SELECT statement
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
535
    check_fields        true if need to check that all INSERT fields are
1 by brian
clean slate
536
                        given values.
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
537
    abort_on_warning    whether to report if some INSERT field is not
163 by Brian Aker
Merge Monty's code.
538
                        assigned as an error (true) or as a warning (false).
1 by brian
clean slate
539
540
  TODO (in far future)
541
    In cases of:
542
    INSERT INTO t1 SELECT a, sum(a) as sum1 from t2 GROUP BY a
543
    ON DUPLICATE KEY ...
544
    we should be able to refer to sum1 in the ON DUPLICATE KEY part
545
546
  WARNING
547
    You MUST set table->insert_values to 0 after calling this function
548
    before releasing the table object.
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
549
1 by brian
clean slate
550
  RETURN VALUE
163 by Brian Aker
Merge Monty's code.
551
    false OK
552
    true  error
1 by brian
clean slate
553
*/
554
520.1.22 by Brian Aker
Second pass of thd cleanup
555
bool mysql_prepare_insert(Session *session, TableList *table_list,
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
556
                          Table *table, List<Item> &fields, List_item *values,
1 by brian
clean slate
557
                          List<Item> &update_fields, List<Item> &update_values,
558
                          enum_duplicates duplic,
779.1.27 by Monty Taylor
Got rid of __attribute__((unused)) and the like from the .cc files.
559
                          COND **,
77.1.45 by Monty Taylor
Warning fixes.
560
                          bool select_insert,
1 by brian
clean slate
561
                          bool check_fields, bool abort_on_warning)
562
{
846 by Brian Aker
Removing on typedeffed class.
563
  Select_Lex *select_lex= &session->lex->select_lex;
1 by brian
clean slate
564
  Name_resolution_context *context= &select_lex->context;
565
  Name_resolution_context_state ctx_state;
566
  bool insert_into_view= (0 != 0);
567
  bool res= 0;
568
  table_map map= 0;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
569
1 by brian
clean slate
570
  /* INSERT should have a SELECT or VALUES clause */
51.2.2 by Patrick Galbraith
Removed DBUGs from
571
  assert (!select_insert || !values);
1 by brian
clean slate
572
573
  /*
574
    For subqueries in VALUES() we should not see the table in which we are
575
    inserting (for INSERT ... SELECT this is done by changing table_list,
846 by Brian Aker
Removing on typedeffed class.
576
    because INSERT ... SELECT share Select_Lex it with SELECT.
1 by brian
clean slate
577
  */
578
  if (!select_insert)
579
  {
848 by Brian Aker
typdef class removal (just... use the name of the class).
580
    for (Select_Lex_Unit *un= select_lex->first_inner_unit();
1 by brian
clean slate
581
         un;
582
         un= un->next_unit())
583
    {
846 by Brian Aker
Removing on typedeffed class.
584
      for (Select_Lex *sl= un->first_select();
1 by brian
clean slate
585
           sl;
586
           sl= sl->next_select())
587
      {
588
        sl->context.outer_context= 0;
589
      }
590
    }
591
  }
592
593
  if (duplic == DUP_UPDATE)
594
  {
595
    /* it should be allocated before Item::fix_fields() */
520.1.22 by Brian Aker
Second pass of thd cleanup
596
    if (table_list->set_insert_values(session->mem_root))
163 by Brian Aker
Merge Monty's code.
597
      return(true);
1 by brian
clean slate
598
  }
599
520.1.22 by Brian Aker
Second pass of thd cleanup
600
  if (mysql_prepare_insert_check_table(session, table_list, fields, select_insert))
163 by Brian Aker
Merge Monty's code.
601
    return(true);
1 by brian
clean slate
602
603
604
  /* Prepare the fields in the statement. */
605
  if (values)
606
  {
607
    /* if we have INSERT ... VALUES () we cannot have a GROUP BY clause */
51.2.2 by Patrick Galbraith
Removed DBUGs from
608
    assert (!select_lex->group_list.elements);
1 by brian
clean slate
609
610
    /* Save the state of the current name resolution context. */
611
    ctx_state.save_state(context, table_list);
612
613
    /*
614
      Perform name resolution only in the first table - 'table_list',
615
      which is the table that is inserted into.
616
     */
617
    table_list->next_local= 0;
618
    context->resolve_in_table_list_only(table_list);
619
520.1.22 by Brian Aker
Second pass of thd cleanup
620
    res= check_insert_fields(session, context->table_list, fields, *values,
1 by brian
clean slate
621
                             !insert_into_view, &map) ||
520.1.22 by Brian Aker
Second pass of thd cleanup
622
      setup_fields(session, 0, *values, MARK_COLUMNS_READ, 0, 0);
1 by brian
clean slate
623
624
    if (!res && check_fields)
625
    {
520.1.22 by Brian Aker
Second pass of thd cleanup
626
      bool saved_abort_on_warning= session->abort_on_warning;
627
      session->abort_on_warning= abort_on_warning;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
628
      res= check_that_all_fields_are_given_values(session,
629
                                                  table ? table :
1 by brian
clean slate
630
                                                  context->table_list->table,
631
                                                  context->table_list);
520.1.22 by Brian Aker
Second pass of thd cleanup
632
      session->abort_on_warning= saved_abort_on_warning;
1 by brian
clean slate
633
    }
634
635
    if (!res && duplic == DUP_UPDATE)
636
    {
520.1.22 by Brian Aker
Second pass of thd cleanup
637
      res= check_update_fields(session, context->table_list, update_fields, &map);
1 by brian
clean slate
638
    }
639
640
    /* Restore the current context. */
641
    ctx_state.restore_state(context, table_list);
642
643
    if (!res)
520.1.22 by Brian Aker
Second pass of thd cleanup
644
      res= setup_fields(session, 0, update_values, MARK_COLUMNS_READ, 0, 0);
1 by brian
clean slate
645
  }
646
647
  if (res)
51.2.2 by Patrick Galbraith
Removed DBUGs from
648
    return(res);
1 by brian
clean slate
649
650
  if (!table)
651
    table= table_list->table;
652
653
  if (!select_insert)
654
  {
327.2.4 by Brian Aker
Refactoring table.h
655
    TableList *duplicate;
1220.1.13 by Brian Aker
Remove mysql_lock_have_duplicate() (we don't have merge, and our partition
656
    if ((duplicate= unique_table(table_list, table_list->next_global, true)))
1 by brian
clean slate
657
    {
1054.1.9 by Brian Aker
This is a large number of refactors against the Session class for its
658
      my_error(ER_UPDATE_TABLE_USED, MYF(0), table_list->alias);
659
660
      return true;
1 by brian
clean slate
661
    }
662
  }
663
  if (duplic == DUP_UPDATE || duplic == DUP_REPLACE)
664
    table->prepare_for_position();
1054.1.9 by Brian Aker
This is a large number of refactors against the Session class for its
665
666
  return false;
1 by brian
clean slate
667
}
668
669
670
	/* Check if there is more uniq keys after field */
671
482 by Brian Aker
Remove uint.
672
static int last_uniq_key(Table *table,uint32_t keynr)
1 by brian
clean slate
673
{
674
  while (++keynr < table->s->keys)
675
    if (table->key_info[keynr].flags & HA_NOSAME)
676
      return 0;
677
  return 1;
678
}
679
680
681
/*
682
  Write a record to table with optional deleting of conflicting records,
683
  invoke proper triggers if needed.
684
685
  SYNOPSIS
686
     write_record()
520.1.22 by Brian Aker
Second pass of thd cleanup
687
      session   - thread context
1 by brian
clean slate
688
      table - table to which record should be written
689
      info  - COPY_INFO structure describing handling of duplicates
690
              and which is used for counting number of records inserted
691
              and deleted.
692
693
  NOTE
694
    Once this record will be written to table after insert trigger will
695
    be invoked. If instead of inserting new record we will update old one
696
    then both on update triggers will work instead. Similarly both on
697
    delete triggers will be invoked if we will delete conflicting records.
698
520.1.22 by Brian Aker
Second pass of thd cleanup
699
    Sets session->transaction.stmt.modified_non_trans_table to true if table which is updated didn't have
1 by brian
clean slate
700
    transactions.
701
702
  RETURN VALUE
703
    0     - success
704
    non-0 - error
705
*/
706
707
520.1.22 by Brian Aker
Second pass of thd cleanup
708
int write_record(Session *session, Table *table,COPY_INFO *info)
1 by brian
clean slate
709
{
710
  int error;
711
  char *key=0;
1103.6.2 by Padraig O'Sullivan
Removing references to MY_BITMAP throughout the code base and updating calls
712
  MyBitmap *save_read_set, *save_write_set;
1208.3.2 by brian
Update for Cursor renaming.
713
  uint64_t prev_insert_id= table->cursor->next_insert_id;
151 by Brian Aker
Ulonglong to uint64_t
714
  uint64_t insert_id_for_cur_row= 0;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
715
1 by brian
clean slate
716
717
  info->records++;
718
  save_read_set=  table->read_set;
719
  save_write_set= table->write_set;
720
831.1.1 by Brian Aker
Cleanup of DELAYED INSERT bits (aka... it is just now a hint to the engine).
721
  if (info->handle_duplicates == DUP_REPLACE || info->handle_duplicates == DUP_UPDATE)
1 by brian
clean slate
722
  {
1208.3.2 by brian
Update for Cursor renaming.
723
    while ((error=table->cursor->ha_write_row(table->record[0])))
1 by brian
clean slate
724
    {
482 by Brian Aker
Remove uint.
725
      uint32_t key_nr;
1 by brian
clean slate
726
      /*
727
        If we do more than one iteration of this loop, from the second one the
728
        row will have an explicit value in the autoinc field, which was set at
729
        the first call of handler::update_auto_increment(). So we must save
520.1.22 by Brian Aker
Second pass of thd cleanup
730
        the autogenerated value to avoid session->insert_id_for_cur_row to become
1 by brian
clean slate
731
        0.
732
      */
1208.3.2 by brian
Update for Cursor renaming.
733
      if (table->cursor->insert_id_for_cur_row > 0)
734
        insert_id_for_cur_row= table->cursor->insert_id_for_cur_row;
1 by brian
clean slate
735
      else
1208.3.2 by brian
Update for Cursor renaming.
736
        table->cursor->insert_id_for_cur_row= insert_id_for_cur_row;
1 by brian
clean slate
737
      bool is_duplicate_key_error;
1208.3.2 by brian
Update for Cursor renaming.
738
      if (table->cursor->is_fatal_error(error, HA_CHECK_DUP))
1 by brian
clean slate
739
	goto err;
1208.3.2 by brian
Update for Cursor renaming.
740
      is_duplicate_key_error= table->cursor->is_fatal_error(error, 0);
1 by brian
clean slate
741
      if (!is_duplicate_key_error)
742
      {
743
        /*
744
          We come here when we had an ignorable error which is not a duplicate
745
          key error. In this we ignore error if ignore flag is set, otherwise
746
          report error as usual. We will not do any duplicate key processing.
747
        */
748
        if (info->ignore)
749
          goto gok_or_after_err; /* Ignoring a not fatal error, return 0 */
750
        goto err;
751
      }
1216.1.1 by Brian Aker
Move print_error up to Engine.
752
      if ((int) (key_nr = table->get_dup_key(error)) < 0)
1 by brian
clean slate
753
      {
754
	error= HA_ERR_FOUND_DUPP_KEY;         /* Database can't find key */
755
	goto err;
756
      }
757
      /* Read all columns for the row we are going to replace */
758
      table->use_all_columns();
759
      /*
760
	Don't allow REPLACE to replace a row when a auto_increment column
761
	was used.  This ensures that we don't get a problem when the
762
	whole range of the key has been used.
763
      */
764
      if (info->handle_duplicates == DUP_REPLACE &&
765
          table->next_number_field &&
766
          key_nr == table->s->next_number_index &&
767
	  (insert_id_for_cur_row > 0))
768
	goto err;
1233.1.6 by Brian Aker
Remove table_flags from MyISAM.
769
      if (table->cursor->getEngine()->check_flag(HTON_BIT_DUPLICATE_POS))
1 by brian
clean slate
770
      {
1208.3.2 by brian
Update for Cursor renaming.
771
	if (table->cursor->rnd_pos(table->record[1],table->cursor->dup_ref))
1 by brian
clean slate
772
	  goto err;
773
      }
774
      else
775
      {
1208.3.2 by brian
Update for Cursor renaming.
776
	if (table->cursor->extra(HA_EXTRA_FLUSH_CACHE)) /* Not needed with NISAM */
1 by brian
clean slate
777
	{
1241.9.57 by Monty Taylor
Oy. Bigger change than I normally like - but this stuff is all intertwined.
778
	  error=errno;
1 by brian
clean slate
779
	  goto err;
780
	}
781
782
	if (!key)
783
	{
656.1.1 by Monty Taylor
OOOh doggie. Got rid of my_alloca.
784
	  if (!(key=(char*) malloc(table->s->max_unique_length)))
1 by brian
clean slate
785
	  {
786
	    error=ENOMEM;
787
	    goto err;
788
	  }
789
	}
481 by Brian Aker
Remove all of uchar.
790
	key_copy((unsigned char*) key,table->record[0],table->key_info+key_nr,0);
1208.3.2 by brian
Update for Cursor renaming.
791
	if ((error=(table->cursor->index_read_idx_map(table->record[1],key_nr,
481 by Brian Aker
Remove all of uchar.
792
                                                    (unsigned char*) key, HA_WHOLE_KEY,
1 by brian
clean slate
793
                                                    HA_READ_KEY_EXACT))))
794
	  goto err;
795
      }
796
      if (info->handle_duplicates == DUP_UPDATE)
797
      {
798
        /*
799
          We don't check for other UNIQUE keys - the first row
800
          that matches, is updated. If update causes a conflict again,
801
          an error is returned
802
        */
51.2.2 by Patrick Galbraith
Removed DBUGs from
803
	assert(table->insert_values != NULL);
997.5.1 by chris
Replace macros around unireg.h, store_record,restore_record,cmp_record,empty_record
804
        table->storeRecordAsInsert();
805
        table->restoreRecord();
51.2.2 by Patrick Galbraith
Removed DBUGs from
806
        assert(info->update_fields->elements ==
1 by brian
clean slate
807
                    info->update_values->elements);
520.1.22 by Brian Aker
Second pass of thd cleanup
808
        if (fill_record(session, *info->update_fields,
1 by brian
clean slate
809
                                                 *info->update_values,
810
                                                 info->ignore))
811
          goto before_err;
812
1208.3.2 by brian
Update for Cursor renaming.
813
        table->cursor->restore_auto_increment(prev_insert_id);
1 by brian
clean slate
814
        if (table->next_number_field)
1208.3.2 by brian
Update for Cursor renaming.
815
          table->cursor->adjust_next_insert_id_after_explicit_value(
1 by brian
clean slate
816
            table->next_number_field->val_int());
817
        info->touched++;
1233.1.4 by Brian Aker
Added:
818
        if ((table->cursor->getEngine()->check_flag(HTON_BIT_PARTIAL_COLUMN_READ) &&
1005.2.3 by Monty Taylor
Further reversion of P.
819
             !bitmap_is_subset(table->write_set, table->read_set)) ||
355 by Brian Aker
More Table cleanup
820
            table->compare_record())
1 by brian
clean slate
821
        {
1208.3.2 by brian
Update for Cursor renaming.
822
          if ((error=table->cursor->ha_update_row(table->record[1],
1 by brian
clean slate
823
                                                table->record[0])) &&
824
              error != HA_ERR_RECORD_IS_THE_SAME)
825
          {
826
            if (info->ignore &&
1208.3.2 by brian
Update for Cursor renaming.
827
                !table->cursor->is_fatal_error(error, HA_CHECK_DUP_KEY))
1 by brian
clean slate
828
            {
829
              goto gok_or_after_err;
830
            }
831
            goto err;
832
          }
833
834
          if (error != HA_ERR_RECORD_IS_THE_SAME)
835
            info->updated++;
836
          else
837
            error= 0;
838
          /*
839
            If ON DUP KEY UPDATE updates a row instead of inserting one, it's
840
            like a regular UPDATE statement: it should not affect the value of a
841
            next SELECT LAST_INSERT_ID() or mysql_insert_id().
842
            Except if LAST_INSERT_ID(#) was in the INSERT query, which is
520.1.21 by Brian Aker
THD -> Session rename
843
            handled separately by Session::arg_of_last_insert_id_function.
1 by brian
clean slate
844
          */
1208.3.2 by brian
Update for Cursor renaming.
845
          insert_id_for_cur_row= table->cursor->insert_id_for_cur_row= 0;
1 by brian
clean slate
846
          info->copied++;
847
        }
848
849
        if (table->next_number_field)
1208.3.2 by brian
Update for Cursor renaming.
850
          table->cursor->adjust_next_insert_id_after_explicit_value(
1 by brian
clean slate
851
            table->next_number_field->val_int());
852
        info->touched++;
853
854
        goto gok_or_after_err;
855
      }
856
      else /* DUP_REPLACE */
857
      {
858
	/*
859
	  The manual defines the REPLACE semantics that it is either
860
	  an INSERT or DELETE(s) + INSERT; FOREIGN KEY checks in
861
	  InnoDB do not function in the defined way if we allow MySQL
862
	  to convert the latter operation internally to an UPDATE.
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
863
          We also should not perform this conversion if we have
1 by brian
clean slate
864
          timestamp field with ON UPDATE which is different from DEFAULT.
865
          Another case when conversion should not be performed is when
866
          we have ON DELETE trigger on table so user may notice that
867
          we cheat here. Note that it is ok to do such conversion for
868
          tables which have ON UPDATE but have no ON DELETE triggers,
869
          we just should not expose this fact to users by invoking
870
          ON UPDATE triggers.
871
	*/
872
	if (last_uniq_key(table,key_nr) &&
1208.3.2 by brian
Update for Cursor renaming.
873
	    !table->cursor->referenced_by_foreign_key() &&
1 by brian
clean slate
874
            (table->timestamp_field_type == TIMESTAMP_NO_AUTO_SET ||
875
             table->timestamp_field_type == TIMESTAMP_AUTO_SET_ON_BOTH))
876
        {
1208.3.2 by brian
Update for Cursor renaming.
877
          if ((error=table->cursor->ha_update_row(table->record[1],
1 by brian
clean slate
878
					        table->record[0])) &&
879
              error != HA_ERR_RECORD_IS_THE_SAME)
880
            goto err;
881
          if (error != HA_ERR_RECORD_IS_THE_SAME)
882
            info->deleted++;
883
          else
884
            error= 0;
1208.3.2 by brian
Update for Cursor renaming.
885
          session->record_first_successful_insert_id_in_cur_stmt(table->cursor->insert_id_for_cur_row);
1 by brian
clean slate
886
          /*
887
            Since we pretend that we have done insert we should call
888
            its after triggers.
889
          */
890
          goto after_n_copied_inc;
891
        }
892
        else
893
        {
1208.3.2 by brian
Update for Cursor renaming.
894
          if ((error=table->cursor->ha_delete_row(table->record[1])))
1 by brian
clean slate
895
            goto err;
896
          info->deleted++;
1208.3.2 by brian
Update for Cursor renaming.
897
          if (!table->cursor->has_transactions())
520.1.22 by Brian Aker
Second pass of thd cleanup
898
            session->transaction.stmt.modified_non_trans_table= true;
1 by brian
clean slate
899
          /* Let us attempt do write_row() once more */
900
        }
901
      }
902
    }
1208.3.2 by brian
Update for Cursor renaming.
903
    session->record_first_successful_insert_id_in_cur_stmt(table->cursor->insert_id_for_cur_row);
1 by brian
clean slate
904
    /*
905
      Restore column maps if they where replaced during an duplicate key
906
      problem.
907
    */
908
    if (table->read_set != save_read_set ||
909
        table->write_set != save_write_set)
910
      table->column_bitmaps_set(save_read_set, save_write_set);
911
  }
1208.3.2 by brian
Update for Cursor renaming.
912
  else if ((error=table->cursor->ha_write_row(table->record[0])))
1 by brian
clean slate
913
  {
914
    if (!info->ignore ||
1208.3.2 by brian
Update for Cursor renaming.
915
        table->cursor->is_fatal_error(error, HA_CHECK_DUP))
1 by brian
clean slate
916
      goto err;
1208.3.2 by brian
Update for Cursor renaming.
917
    table->cursor->restore_auto_increment(prev_insert_id);
1 by brian
clean slate
918
    goto gok_or_after_err;
919
  }
920
921
after_n_copied_inc:
922
  info->copied++;
1208.3.2 by brian
Update for Cursor renaming.
923
  session->record_first_successful_insert_id_in_cur_stmt(table->cursor->insert_id_for_cur_row);
1 by brian
clean slate
924
925
gok_or_after_err:
926
  if (key)
656.1.1 by Monty Taylor
OOOh doggie. Got rid of my_alloca.
927
    free(key);
1208.3.2 by brian
Update for Cursor renaming.
928
  if (!table->cursor->has_transactions())
520.1.22 by Brian Aker
Second pass of thd cleanup
929
    session->transaction.stmt.modified_non_trans_table= true;
51.2.2 by Patrick Galbraith
Removed DBUGs from
930
  return(0);
1 by brian
clean slate
931
932
err:
933
  info->last_errno= error;
934
  /* current_select is NULL if this is a delayed insert */
520.1.22 by Brian Aker
Second pass of thd cleanup
935
  if (session->lex->current_select)
936
    session->lex->current_select->no_error= 0;        // Give error
1216.1.1 by Brian Aker
Move print_error up to Engine.
937
  table->print_error(error,MYF(0));
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
938
1 by brian
clean slate
939
before_err:
1208.3.2 by brian
Update for Cursor renaming.
940
  table->cursor->restore_auto_increment(prev_insert_id);
1 by brian
clean slate
941
  if (key)
656.1.1 by Monty Taylor
OOOh doggie. Got rid of my_alloca.
942
    free(key);
1 by brian
clean slate
943
  table->column_bitmaps_set(save_read_set, save_write_set);
51.2.2 by Patrick Galbraith
Removed DBUGs from
944
  return(1);
1 by brian
clean slate
945
}
946
947
948
/******************************************************************************
949
  Check that all fields with arn't null_fields are used
950
******************************************************************************/
951
520.1.22 by Brian Aker
Second pass of thd cleanup
952
int check_that_all_fields_are_given_values(Session *session, Table *entry,
685.4.10 by Jay Pipes
Fixed bug where error 1364 was not being thrown in an appropriate place. Now, check_that_all_fields_have_given_values() method does two separate checks, one for not being in the write_set() and not having a default value (raise 1364) and another for being in the write_set() but having a NULL value set
953
                                           TableList *)
1 by brian
clean slate
954
{
955
  int err= 0;
956
957
  for (Field **field=entry->field ; *field ; field++)
958
  {
1009 by Brian Aker
Merge of Monty
959
    if (((*field)->isWriteSet()) == false)
685.4.10 by Jay Pipes
Fixed bug where error 1364 was not being thrown in an appropriate place. Now, check_that_all_fields_have_given_values() method does two separate checks, one for not being in the write_set() and not having a default value (raise 1364) and another for being in the write_set() but having a NULL value set
960
    {
961
      /*
962
       * If the field doesn't have any default value
963
       * and there is no actual value specified in the
964
       * INSERT statement, throw error ER_NO_DEFAULT_FOR_FIELD.
965
       */
966
      if (((*field)->flags & NO_DEFAULT_VALUE_FLAG) &&
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
967
        ((*field)->real_type() != DRIZZLE_TYPE_ENUM))
1 by brian
clean slate
968
      {
685.4.10 by Jay Pipes
Fixed bug where error 1364 was not being thrown in an appropriate place. Now, check_that_all_fields_have_given_values() method does two separate checks, one for not being in the write_set() and not having a default value (raise 1364) and another for being in the write_set() but having a NULL value set
969
        my_error(ER_NO_DEFAULT_FOR_FIELD, MYF(0), (*field)->field_name);
970
        err= 1;
1 by brian
clean slate
971
      }
685.4.10 by Jay Pipes
Fixed bug where error 1364 was not being thrown in an appropriate place. Now, check_that_all_fields_have_given_values() method does two separate checks, one for not being in the write_set() and not having a default value (raise 1364) and another for being in the write_set() but having a NULL value set
972
    }
973
    else
974
    {
685.4.1 by Jay Pipes
Enabled the null.test.
975
      /*
685.4.10 by Jay Pipes
Fixed bug where error 1364 was not being thrown in an appropriate place. Now, check_that_all_fields_have_given_values() method does two separate checks, one for not being in the write_set() and not having a default value (raise 1364) and another for being in the write_set() but having a NULL value set
976
       * However, if an actual NULL value was specified
977
       * for the field and the field is a NOT NULL field, 
978
       * throw ER_BAD_NULL_ERROR.
979
       *
685.4.1 by Jay Pipes
Enabled the null.test.
980
       * Per the SQL standard, inserting NULL into a NOT NULL
981
       * field requires an error to be thrown.
982
       */
685.4.10 by Jay Pipes
Fixed bug where error 1364 was not being thrown in an appropriate place. Now, check_that_all_fields_have_given_values() method does two separate checks, one for not being in the write_set() and not having a default value (raise 1364) and another for being in the write_set() but having a NULL value set
983
      if (((*field)->flags & NOT_NULL_FLAG) &&
984
          (*field)->is_null())
985
      {
986
        my_error(ER_BAD_NULL_ERROR, MYF(0), (*field)->field_name);
987
        err= 1;
988
      }
1 by brian
clean slate
989
    }
990
  }
520.1.22 by Brian Aker
Second pass of thd cleanup
991
  return session->abort_on_warning ? err : 0;
1 by brian
clean slate
992
}
993
994
/***************************************************************************
995
  Store records in INSERT ... SELECT *
996
***************************************************************************/
997
998
999
/*
1000
  make insert specific preparation and checks after opening tables
1001
1002
  SYNOPSIS
1003
    mysql_insert_select_prepare()
520.1.22 by Brian Aker
Second pass of thd cleanup
1004
    session         thread handler
1 by brian
clean slate
1005
1006
  RETURN
163 by Brian Aker
Merge Monty's code.
1007
    false OK
1008
    true  Error
1 by brian
clean slate
1009
*/
1010
520.1.22 by Brian Aker
Second pass of thd cleanup
1011
bool mysql_insert_select_prepare(Session *session)
1 by brian
clean slate
1012
{
520.1.22 by Brian Aker
Second pass of thd cleanup
1013
  LEX *lex= session->lex;
846 by Brian Aker
Removing on typedeffed class.
1014
  Select_Lex *select_lex= &lex->select_lex;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1015
1 by brian
clean slate
1016
  /*
846 by Brian Aker
Removing on typedeffed class.
1017
    Select_Lex do not belong to INSERT statement, so we can't add WHERE
1 by brian
clean slate
1018
    clause if table is VIEW
1019
  */
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1020
520.1.22 by Brian Aker
Second pass of thd cleanup
1021
  if (mysql_prepare_insert(session, lex->query_tables,
1 by brian
clean slate
1022
                           lex->query_tables->table, lex->field_list, 0,
1023
                           lex->update_list, lex->value_list,
1024
                           lex->duplicates,
163 by Brian Aker
Merge Monty's code.
1025
                           &select_lex->where, true, false, false))
1026
    return(true);
1 by brian
clean slate
1027
1028
  /*
1029
    exclude first table from leaf tables list, because it belong to
1030
    INSERT
1031
  */
51.2.2 by Patrick Galbraith
Removed DBUGs from
1032
  assert(select_lex->leaf_tables != 0);
1 by brian
clean slate
1033
  lex->leaf_tables_insert= select_lex->leaf_tables;
1034
  /* skip all leaf tables belonged to view where we are insert */
327.1.7 by Brian Aker
Removed belong_to_view variable
1035
  select_lex->leaf_tables= select_lex->leaf_tables->next_leaf;
163 by Brian Aker
Merge Monty's code.
1036
  return(false);
1 by brian
clean slate
1037
}
1038
1039
327.2.4 by Brian Aker
Refactoring table.h
1040
select_insert::select_insert(TableList *table_list_par, Table *table_par,
1 by brian
clean slate
1041
                             List<Item> *fields_par,
1042
                             List<Item> *update_fields,
1043
                             List<Item> *update_values,
1044
                             enum_duplicates duplic,
1045
                             bool ignore_check_option_errors)
1046
  :table_list(table_list_par), table(table_par), fields(fields_par),
1047
   autoinc_value_of_last_inserted_row(0),
1048
   insert_into_view(table_list_par && 0 != 0)
1049
{
212.6.6 by Mats Kindahl
Removing redundant use of casts in drizzled/ for memcmp(), memcpy(), memset(), and memmove().
1050
  memset(&info, 0, sizeof(info));
1 by brian
clean slate
1051
  info.handle_duplicates= duplic;
1052
  info.ignore= ignore_check_option_errors;
1053
  info.update_fields= update_fields;
1054
  info.update_values= update_values;
1055
}
1056
1057
1058
int
848 by Brian Aker
typdef class removal (just... use the name of the class).
1059
select_insert::prepare(List<Item> &values, Select_Lex_Unit *u)
1 by brian
clean slate
1060
{
520.1.22 by Brian Aker
Second pass of thd cleanup
1061
  LEX *lex= session->lex;
1 by brian
clean slate
1062
  int res;
1063
  table_map map= 0;
846 by Brian Aker
Removing on typedeffed class.
1064
  Select_Lex *lex_current_select_save= lex->current_select;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1065
1 by brian
clean slate
1066
1067
  unit= u;
1068
1069
  /*
1070
    Since table in which we are going to insert is added to the first
1071
    select, LEX::current_select should point to the first select while
1072
    we are fixing fields from insert list.
1073
  */
1074
  lex->current_select= &lex->select_lex;
520.1.22 by Brian Aker
Second pass of thd cleanup
1075
  res= check_insert_fields(session, table_list, *fields, values,
1 by brian
clean slate
1076
                           !insert_into_view, &map) ||
520.1.22 by Brian Aker
Second pass of thd cleanup
1077
       setup_fields(session, 0, values, MARK_COLUMNS_READ, 0, 0);
1 by brian
clean slate
1078
1079
  if (!res && fields->elements)
1080
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
1081
    bool saved_abort_on_warning= session->abort_on_warning;
1082
    session->abort_on_warning= !info.ignore;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1083
    res= check_that_all_fields_are_given_values(session, table_list->table,
1 by brian
clean slate
1084
                                                table_list);
520.1.22 by Brian Aker
Second pass of thd cleanup
1085
    session->abort_on_warning= saved_abort_on_warning;
1 by brian
clean slate
1086
  }
1087
1088
  if (info.handle_duplicates == DUP_UPDATE && !res)
1089
  {
1090
    Name_resolution_context *context= &lex->select_lex.context;
1091
    Name_resolution_context_state ctx_state;
1092
1093
    /* Save the state of the current name resolution context. */
1094
    ctx_state.save_state(context, table_list);
1095
1096
    /* Perform name resolution only in the first table - 'table_list'. */
1097
    table_list->next_local= 0;
1098
    context->resolve_in_table_list_only(table_list);
1099
520.1.22 by Brian Aker
Second pass of thd cleanup
1100
    res= res || check_update_fields(session, context->table_list,
1 by brian
clean slate
1101
                                    *info.update_fields, &map);
1102
    /*
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1103
      When we are not using GROUP BY and there are no ungrouped aggregate functions
1 by brian
clean slate
1104
      we can refer to other tables in the ON DUPLICATE KEY part.
1105
      We use next_name_resolution_table descructively, so check it first (views?)
1106
    */
51.2.2 by Patrick Galbraith
Removed DBUGs from
1107
    assert (!table_list->next_name_resolution_table);
1 by brian
clean slate
1108
    if (lex->select_lex.group_list.elements == 0 &&
1109
        !lex->select_lex.with_sum_func)
1110
      /*
1111
        We must make a single context out of the two separate name resolution contexts :
1112
        the INSERT table and the tables in the SELECT part of INSERT ... SELECT.
1113
        To do that we must concatenate the two lists
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1114
      */
1115
      table_list->next_name_resolution_table=
1 by brian
clean slate
1116
        ctx_state.get_first_name_resolution_table();
1117
520.1.22 by Brian Aker
Second pass of thd cleanup
1118
    res= res || setup_fields(session, 0, *info.update_values,
1 by brian
clean slate
1119
                             MARK_COLUMNS_READ, 0, 0);
1120
    if (!res)
1121
    {
1122
      /*
1123
        Traverse the update values list and substitute fields from the
1124
        select for references (Item_ref objects) to them. This is done in
1125
        order to get correct values from those fields when the select
1126
        employs a temporary table.
1127
      */
1128
      List_iterator<Item> li(*info.update_values);
1129
      Item *item;
1130
1131
      while ((item= li++))
1132
      {
1133
        item->transform(&Item::update_value_transformer,
481 by Brian Aker
Remove all of uchar.
1134
                        (unsigned char*)lex->current_select);
1 by brian
clean slate
1135
      }
1136
    }
1137
1138
    /* Restore the current context. */
1139
    ctx_state.restore_state(context, table_list);
1140
  }
1141
1142
  lex->current_select= lex_current_select_save;
1143
  if (res)
51.2.2 by Patrick Galbraith
Removed DBUGs from
1144
    return(1);
1 by brian
clean slate
1145
  /*
1146
    if it is INSERT into join view then check_insert_fields already found
1147
    real table for insert
1148
  */
1149
  table= table_list->table;
1150
1151
  /*
1152
    Is table which we are changing used somewhere in other parts of
1153
    query
1154
  */
1220.1.13 by Brian Aker
Remove mysql_lock_have_duplicate() (we don't have merge, and our partition
1155
  if (unique_table(table_list, table_list->next_global))
1 by brian
clean slate
1156
  {
1157
    /* Using same table for INSERT and SELECT */
1158
    lex->current_select->options|= OPTION_BUFFER_RESULT;
1159
    lex->current_select->join->select_options|= OPTION_BUFFER_RESULT;
1160
  }
1161
  else if (!(lex->current_select->options & OPTION_BUFFER_RESULT))
1162
  {
1163
    /*
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1164
      We must not yet prepare the result table if it is the same as one of the
1165
      source tables (INSERT SELECT). The preparation may disable
1 by brian
clean slate
1166
      indexes on the result table, which may be used during the select, if it
1167
      is the same table (Bug #6034). Do the preparation after the select phase
1168
      in select_insert::prepare2().
1169
      We won't start bulk inserts at all if this statement uses functions or
1170
      should invoke triggers since they may access to the same table too.
1171
    */
1208.3.2 by brian
Update for Cursor renaming.
1172
    table->cursor->ha_start_bulk_insert((ha_rows) 0);
1 by brian
clean slate
1173
  }
997.5.1 by chris
Replace macros around unireg.h, store_record,restore_record,cmp_record,empty_record
1174
  table->restoreRecordAsDefault();		// Get empty record
1 by brian
clean slate
1175
  table->next_number_field=table->found_next_number_field;
1176
520.1.22 by Brian Aker
Second pass of thd cleanup
1177
  session->cuted_fields=0;
1 by brian
clean slate
1178
  if (info.ignore || info.handle_duplicates != DUP_ERROR)
1208.3.2 by brian
Update for Cursor renaming.
1179
    table->cursor->extra(HA_EXTRA_IGNORE_DUP_KEY);
1 by brian
clean slate
1180
  if (info.handle_duplicates == DUP_REPLACE)
1208.3.2 by brian
Update for Cursor renaming.
1181
    table->cursor->extra(HA_EXTRA_WRITE_CAN_REPLACE);
1 by brian
clean slate
1182
  if (info.handle_duplicates == DUP_UPDATE)
1208.3.2 by brian
Update for Cursor renaming.
1183
    table->cursor->extra(HA_EXTRA_INSERT_WITH_UPDATE);
520.1.22 by Brian Aker
Second pass of thd cleanup
1184
  session->abort_on_warning= !info.ignore;
1 by brian
clean slate
1185
  table->mark_columns_needed_for_insert();
1186
1187
51.2.2 by Patrick Galbraith
Removed DBUGs from
1188
  return(res);
1 by brian
clean slate
1189
}
1190
1191
1192
/*
1193
  Finish the preparation of the result table.
1194
1195
  SYNOPSIS
1196
    select_insert::prepare2()
1197
    void
1198
1199
  DESCRIPTION
1200
    If the result table is the same as one of the source tables (INSERT SELECT),
1201
    the result table is not finally prepared at the join prepair phase.
1202
    Do the final preparation now.
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1203
1 by brian
clean slate
1204
  RETURN
1205
    0   OK
1206
*/
1207
1208
int select_insert::prepare2(void)
1209
{
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1210
520.1.22 by Brian Aker
Second pass of thd cleanup
1211
  if (session->lex->current_select->options & OPTION_BUFFER_RESULT)
1208.3.2 by brian
Update for Cursor renaming.
1212
    table->cursor->ha_start_bulk_insert((ha_rows) 0);
51.2.2 by Patrick Galbraith
Removed DBUGs from
1213
  return(0);
1 by brian
clean slate
1214
}
1215
1216
1217
void select_insert::cleanup()
1218
{
1219
  /* select_insert/select_create are never re-used in prepared statement */
51.2.2 by Patrick Galbraith
Removed DBUGs from
1220
  assert(0);
1 by brian
clean slate
1221
}
1222
1223
select_insert::~select_insert()
1224
{
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1225
1 by brian
clean slate
1226
  if (table)
1227
  {
1228
    table->next_number_field=0;
163 by Brian Aker
Merge Monty's code.
1229
    table->auto_increment_field_not_null= false;
1208.3.2 by brian
Update for Cursor renaming.
1230
    table->cursor->ha_reset();
1 by brian
clean slate
1231
  }
520.1.22 by Brian Aker
Second pass of thd cleanup
1232
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
1233
  session->abort_on_warning= 0;
51.2.2 by Patrick Galbraith
Removed DBUGs from
1234
  return;
1 by brian
clean slate
1235
}
1236
1237
1238
bool select_insert::send_data(List<Item> &values)
1239
{
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1240
1 by brian
clean slate
1241
  bool error=0;
1242
1243
  if (unit->offset_limit_cnt)
1244
  {						// using limit offset,count
1245
    unit->offset_limit_cnt--;
51.2.2 by Patrick Galbraith
Removed DBUGs from
1246
    return(0);
1 by brian
clean slate
1247
  }
1248
520.1.22 by Brian Aker
Second pass of thd cleanup
1249
  session->count_cuted_fields= CHECK_FIELD_WARN;	// Calculate cuted fields
1 by brian
clean slate
1250
  store_values(values);
520.1.22 by Brian Aker
Second pass of thd cleanup
1251
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
1252
  if (session->is_error())
51.2.2 by Patrick Galbraith
Removed DBUGs from
1253
    return(1);
1 by brian
clean slate
1254
991.1.1 by Stewart Smith
Release latches in case bulk insert takes a long time
1255
  // Release latches in case bulk insert takes a long time
1152.1.4 by Brian Aker
Remove wrappers from SE
1256
  plugin::StorageEngine::releaseTemporaryLatches(session);
991.1.1 by Stewart Smith
Release latches in case bulk insert takes a long time
1257
520.1.22 by Brian Aker
Second pass of thd cleanup
1258
  error= write_record(session, table, &info);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1259
1 by brian
clean slate
1260
  if (!error)
1261
  {
1262
    if (info.handle_duplicates == DUP_UPDATE)
1263
    {
1264
      /*
1265
        Restore fields of the record since it is possible that they were
1266
        changed by ON DUPLICATE KEY UPDATE clause.
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1267
1 by brian
clean slate
1268
        If triggers exist then whey can modify some fields which were not
1269
        originally touched by INSERT ... SELECT, so we have to restore
1270
        their original values for the next row.
1271
      */
997.5.1 by chris
Replace macros around unireg.h, store_record,restore_record,cmp_record,empty_record
1272
      table->restoreRecordAsDefault();
1 by brian
clean slate
1273
    }
1274
    if (table->next_number_field)
1275
    {
1276
      /*
1277
        If no value has been autogenerated so far, we need to remember the
1278
        value we just saw, we may need to send it to client in the end.
1279
      */
520.1.22 by Brian Aker
Second pass of thd cleanup
1280
      if (session->first_successful_insert_id_in_cur_stmt == 0) // optimization
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1281
        autoinc_value_of_last_inserted_row=
1 by brian
clean slate
1282
          table->next_number_field->val_int();
1283
      /*
1284
        Clear auto-increment field for the next record, if triggers are used
1285
        we will clear it twice, but this should be cheap.
1286
      */
1287
      table->next_number_field->reset();
1288
    }
1289
  }
51.2.2 by Patrick Galbraith
Removed DBUGs from
1290
  return(error);
1 by brian
clean slate
1291
}
1292
1293
1294
void select_insert::store_values(List<Item> &values)
1295
{
1296
  if (fields->elements)
1235.1.11 by Brian Aker
Small cleanups, did in MERGE table only engine flag.
1297
    fill_record(session, *fields, values, true);
1 by brian
clean slate
1298
  else
1235.1.11 by Brian Aker
Small cleanups, did in MERGE table only engine flag.
1299
    fill_record(session, table->field, values, true);
1 by brian
clean slate
1300
}
1301
482 by Brian Aker
Remove uint.
1302
void select_insert::send_error(uint32_t errcode,const char *err)
1 by brian
clean slate
1303
{
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1304
1 by brian
clean slate
1305
1306
  my_message(errcode, err, MYF(0));
1307
51.2.2 by Patrick Galbraith
Removed DBUGs from
1308
  return;
1 by brian
clean slate
1309
}
1310
1311
1312
bool select_insert::send_eof()
1313
{
1314
  int error;
1208.3.2 by brian
Update for Cursor renaming.
1315
  bool const trans_table= table->cursor->has_transactions();
151 by Brian Aker
Ulonglong to uint64_t
1316
  uint64_t id;
1 by brian
clean slate
1317
  bool changed;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1318
1208.3.2 by brian
Update for Cursor renaming.
1319
  error= table->cursor->ha_end_bulk_insert();
1320
  table->cursor->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
1321
  table->cursor->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
1 by brian
clean slate
1322
1323
  if ((changed= (info.copied || info.deleted || info.updated)))
1324
  {
1325
    /*
1326
      We must invalidate the table in the query cache before binlog writing
1327
      and ha_autocommit_or_rollback.
1328
    */
520.1.22 by Brian Aker
Second pass of thd cleanup
1329
    if (session->transaction.stmt.modified_non_trans_table)
1330
      session->transaction.all.modified_non_trans_table= true;
1 by brian
clean slate
1331
  }
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1332
  assert(trans_table || !changed ||
520.1.22 by Brian Aker
Second pass of thd cleanup
1333
              session->transaction.stmt.modified_non_trans_table);
1 by brian
clean slate
1334
1208.3.2 by brian
Update for Cursor renaming.
1335
  table->cursor->ha_release_auto_increment();
1 by brian
clean slate
1336
1337
  if (error)
1338
  {
1216.1.1 by Brian Aker
Move print_error up to Engine.
1339
    table->print_error(error,MYF(0));
1126.10.12 by Padraig O'Sullivan
Added calls to the dtrace probes related to insertion.
1340
    DRIZZLE_INSERT_SELECT_DONE(error, 0);
1341
    return 1;
1 by brian
clean slate
1342
  }
1343
  char buff[160];
1344
  if (info.ignore)
1345
    sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
520.1.22 by Brian Aker
Second pass of thd cleanup
1346
	    (ulong) (info.records - info.copied), (ulong) session->cuted_fields);
1 by brian
clean slate
1347
  else
1348
    sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
520.1.22 by Brian Aker
Second pass of thd cleanup
1349
	    (ulong) (info.deleted+info.updated), (ulong) session->cuted_fields);
971.3.59 by Eric Day
Removed client_capabilities from session and pushed functionality into protocol plugin.
1350
  session->row_count_func= info.copied + info.deleted + info.updated;
1 by brian
clean slate
1351
520.1.22 by Brian Aker
Second pass of thd cleanup
1352
  id= (session->first_successful_insert_id_in_cur_stmt > 0) ?
1353
    session->first_successful_insert_id_in_cur_stmt :
1354
    (session->arg_of_last_insert_id_function ?
1355
     session->first_successful_insert_id_in_prev_stmt :
1 by brian
clean slate
1356
     (info.copied ? autoinc_value_of_last_inserted_row : 0));
971.3.59 by Eric Day
Removed client_capabilities from session and pushed functionality into protocol plugin.
1357
  session->my_ok((ulong) session->row_count_func,
1358
                 info.copied + info.deleted + info.touched, id, buff);
1126.10.12 by Padraig O'Sullivan
Added calls to the dtrace probes related to insertion.
1359
  DRIZZLE_INSERT_SELECT_DONE(0, session->row_count_func);
1360
  return 0;
1 by brian
clean slate
1361
}
1362
1363
void select_insert::abort() {
1364
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1365
1 by brian
clean slate
1366
  /*
1367
    If the creation of the table failed (due to a syntax error, for
1368
    example), no table will have been opened and therefore 'table'
1369
    will be NULL. In that case, we still need to execute the rollback
1370
    and the end of the function.
1371
   */
1372
  if (table)
1373
  {
1374
    bool changed, transactional_table;
1375
1208.3.2 by brian
Update for Cursor renaming.
1376
    table->cursor->ha_end_bulk_insert();
1 by brian
clean slate
1377
1378
    /*
1379
      If at least one row has been inserted/modified and will stay in
1380
      the table (the table doesn't have transactions) we must write to
1381
      the binlog (and the error code will make the slave stop).
1382
1383
      For many errors (example: we got a duplicate key error while
1384
      inserting into a MyISAM table), no row will be added to the table,
1385
      so passing the error to the slave will not help since there will
1386
      be an error code mismatch (the inserts will succeed on the slave
1387
      with no error).
1388
1389
      If table creation failed, the number of rows modified will also be
1390
      zero, so no check for that is made.
1391
    */
1392
    changed= (info.copied || info.deleted || info.updated);
1208.3.2 by brian
Update for Cursor renaming.
1393
    transactional_table= table->cursor->has_transactions();
51.2.2 by Patrick Galbraith
Removed DBUGs from
1394
    assert(transactional_table || !changed ||
520.1.22 by Brian Aker
Second pass of thd cleanup
1395
		session->transaction.stmt.modified_non_trans_table);
1208.3.2 by brian
Update for Cursor renaming.
1396
    table->cursor->ha_release_auto_increment();
1 by brian
clean slate
1397
  }
1398
1126.10.12 by Padraig O'Sullivan
Added calls to the dtrace probes related to insertion.
1399
  if (DRIZZLE_INSERT_SELECT_DONE_ENABLED())
1400
  {
1401
    DRIZZLE_INSERT_SELECT_DONE(0, info.copied + info.deleted + info.updated);
1402
  }
1403
51.2.2 by Patrick Galbraith
Removed DBUGs from
1404
  return;
1 by brian
clean slate
1405
}
1406
1407
1408
/***************************************************************************
1409
  CREATE TABLE (SELECT) ...
1410
***************************************************************************/
1411
1412
/*
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
1413
  Create table from lists of fields and items (or just return Table
1 by brian
clean slate
1414
  object for pre-opened existing table).
1415
1416
  SYNOPSIS
1417
    create_table_from_items()
520.1.22 by Brian Aker
Second pass of thd cleanup
1418
      session          in     Thread object
1 by brian
clean slate
1419
      create_info  in     Create information (like MAX_ROWS, ENGINE or
1420
                          temporary table flag)
327.2.4 by Brian Aker
Refactoring table.h
1421
      create_table in     Pointer to TableList object providing database
1 by brian
clean slate
1422
                          and name for table to be created or to be open
1423
      alter_info   in/out Initial list of columns and indexes for the table
1424
                          to be created
1425
      items        in     List of items which should be used to produce rest
1426
                          of fields for the table (corresponding fields will
1427
                          be added to the end of alter_info->create_list)
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
1428
      lock         out    Pointer to the DRIZZLE_LOCK object for table created
1 by brian
clean slate
1429
                          (or open temporary table) will be returned in this
1430
                          parameter. Since this table is not included in
520.1.21 by Brian Aker
THD -> Session rename
1431
                          Session::lock caller is responsible for explicitly
1 by brian
clean slate
1432
                          unlocking this table.
1433
      hooks
1434
1435
  NOTES
1436
    This function behaves differently for base and temporary tables:
1437
    - For base table we assume that either table exists and was pre-opened
1109.1.3 by Brian Aker
Move names around a bit (to align similar methods)
1438
      and locked at openTablesLock() stage (and in this case we just
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
1439
      emit error or warning and return pre-opened Table object) or special
1 by brian
clean slate
1440
      placeholder was put in table cache that guarantees that this table
1441
      won't be created or opened until the placeholder will be removed
1442
      (so there is an exclusive lock on this table).
1443
    - We don't pre-open existing temporary table, instead we either open
1444
      or create and then open table in this function.
1445
1446
    Since this function contains some logic specific to CREATE TABLE ...
1447
    SELECT it should be changed before it can be used in other contexts.
1448
1449
  RETURN VALUES
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
1450
    non-zero  Pointer to Table object for table created or opened
1 by brian
clean slate
1451
    0         Error
1452
*/
1453
520.1.22 by Brian Aker
Second pass of thd cleanup
1454
static Table *create_table_from_items(Session *session, HA_CREATE_INFO *create_info,
327.2.4 by Brian Aker
Refactoring table.h
1455
                                      TableList *create_table,
1118.2.1 by Stewart Smith
fix drizzled::message::Table usage so that in kernel .cc files we are 'using namespace drizzled'
1456
				      message::Table *table_proto,
1126.3.3 by Jay Pipes
Moves Alter_info out into its own header and source file, cleans up some related include mess in sql_lex.h, and renames Alter_info to AlterInfo.
1457
                                      AlterInfo *alter_info,
1 by brian
clean slate
1458
                                      List<Item> *items,
1222.2.3 by Brian Aker
Remove a few more options, from options in HA_CREATE_INFO.
1459
                                      bool is_if_not_exists,
831 by Brian Aker
Remove dead hooks code.
1460
                                      DRIZZLE_LOCK **lock)
1 by brian
clean slate
1461
{
1052.2.3 by Nathan Williams
No actual code changes. Changed Create_field to CreateField to be consistent with coding standards.
1462
  Table tmp_table;		// Used during 'CreateField()'
1000.1.3 by Brian Aker
Renamed TABLE_SHARE to TableShare
1463
  TableShare share;
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
1464
  Table *table= 0;
482 by Brian Aker
Remove uint.
1465
  uint32_t select_field_count= items->elements;
1 by brian
clean slate
1466
  /* Add selected items to field list */
1467
  List_iterator_fast<Item> it(*items);
1468
  Item *item;
1469
  Field *tmp_field;
1470
  bool not_used;
1471
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
1472
  bool lex_identified_temp_table= (table_proto->type() == message::Table::TEMPORARY);
1222.2.1 by Brian Aker
Takes the majority usage of parser based HA_CREATE_INFO usage around
1473
1474
  if (!(lex_identified_temp_table) &&
784.1.6 by Stewart Smith
merge
1475
      create_table->table->db_stat)
1 by brian
clean slate
1476
  {
1109.1.3 by Brian Aker
Move names around a bit (to align similar methods)
1477
    /* Table already exists and was open at openTablesLock() stage. */
1222.2.3 by Brian Aker
Remove a few more options, from options in HA_CREATE_INFO.
1478
    if (is_if_not_exists)
1 by brian
clean slate
1479
    {
1480
      create_info->table_existed= 1;		// Mark that table existed
520.1.22 by Brian Aker
Second pass of thd cleanup
1481
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1 by brian
clean slate
1482
                          ER_TABLE_EXISTS_ERROR, ER(ER_TABLE_EXISTS_ERROR),
1483
                          create_table->table_name);
1054.1.9 by Brian Aker
This is a large number of refactors against the Session class for its
1484
      return create_table->table;
1 by brian
clean slate
1485
    }
1486
1487
    my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_table->table_name);
1054.1.9 by Brian Aker
This is a large number of refactors against the Session class for its
1488
    return NULL;
1 by brian
clean slate
1489
  }
1490
1491
  tmp_table.alias= 0;
1492
  tmp_table.timestamp_field= 0;
1493
  tmp_table.s= &share;
1494
1495
  tmp_table.s->db_create_options=0;
1496
  tmp_table.s->blob_ptr_size= portable_sizeof_char_ptr;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1497
  tmp_table.s->db_low_byte_first=
960.2.25 by Monty Taylor
First step of hton rename.
1498
        test(create_info->db_type == myisam_engine ||
1499
             create_info->db_type == heap_engine);
274 by Brian Aker
my_bool conversion in Table
1500
  tmp_table.null_row= false;
1501
  tmp_table.maybe_null= false;
1 by brian
clean slate
1502
1503
  while ((item=it++))
1504
  {
1052.2.3 by Nathan Williams
No actual code changes. Changed Create_field to CreateField to be consistent with coding standards.
1505
    CreateField *cr_field;
1 by brian
clean slate
1506
    Field *field, *def_field;
1507
    if (item->type() == Item::FUNC_ITEM)
1508
      if (item->result_type() != STRING_RESULT)
1509
        field= item->tmp_table_field(&tmp_table);
1510
      else
1511
        field= item->tmp_table_field_from_field_type(&tmp_table, 0);
1512
    else
520.1.22 by Brian Aker
Second pass of thd cleanup
1513
      field= create_tmp_field(session, &tmp_table, item, item->type(),
1273.2.4 by Stewart Smith
remove unused parameter to create_tmp_field() - was table_cant_handle_bit_fields which was around for HEAP based temp tables.
1514
                              (Item ***) 0, &tmp_field, &def_field, false,
1515
                              false, false, 0);
1 by brian
clean slate
1516
    if (!field ||
1052.2.3 by Nathan Williams
No actual code changes. Changed Create_field to CreateField to be consistent with coding standards.
1517
	!(cr_field=new CreateField(field,(item->type() == Item::FIELD_ITEM ?
1 by brian
clean slate
1518
					   ((Item_field *)item)->field :
1519
					   (Field*) 0))))
1054.1.9 by Brian Aker
This is a large number of refactors against the Session class for its
1520
      return NULL;
1 by brian
clean slate
1521
    if (item->maybe_null)
1522
      cr_field->flags &= ~NOT_NULL_FLAG;
1523
    alter_info->create_list.push_back(cr_field);
1524
  }
1525
1223.4.3 by Brian Aker
More identifier work.
1526
  TableIdentifier identifier(create_table->db,
1527
                             create_table->table_name,
1235.1.3 by Brian Aker
Remove the need for trans/non-trans temp tables for lock conditions.
1528
                             lex_identified_temp_table ?  TEMP_TABLE :
1223.4.3 by Brian Aker
More identifier work.
1529
                             NO_TMP_TABLE);
1530
1531
1 by brian
clean slate
1532
  /*
1533
    Create and lock table.
1534
1535
    Note that we either creating (or opening existing) temporary table or
1536
    creating base table on which name we have exclusive lock. So code below
1537
    should not cause deadlocks or races.
1538
  */
1539
  {
1223.4.1 by Brian Aker
Table Identifier patch.
1540
    if (!mysql_create_table_no_lock(session,
1223.4.3 by Brian Aker
More identifier work.
1541
                                    identifier,
1008.3.20 by Stewart Smith
Put table_name into table proto in parser, and use that when creating table proto file.
1542
                                    create_info,
1067.1.1 by Stewart Smith
Beginning of create table proto in parser: pass which Engine is being used for CREATE TABLE in table proto.
1543
				    table_proto,
1223.4.1 by Brian Aker
Table Identifier patch.
1544
				    alter_info,
1545
                                    false,
1222.2.3 by Brian Aker
Remove a few more options, from options in HA_CREATE_INFO.
1546
                                    select_field_count,
1547
                                    is_if_not_exists))
1 by brian
clean slate
1548
    {
1549
      if (create_info->table_existed &&
1222.2.1 by Brian Aker
Takes the majority usage of parser based HA_CREATE_INFO usage around
1550
          !(lex_identified_temp_table))
1 by brian
clean slate
1551
      {
1552
        /*
1553
          This means that someone created table underneath server
1554
          or it was created via different mysqld front-end to the
1555
          cluster. We don't have much options but throw an error.
1556
        */
1557
        my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_table->table_name);
1054.1.9 by Brian Aker
This is a large number of refactors against the Session class for its
1558
        return NULL;
1 by brian
clean slate
1559
      }
1560
1222.2.1 by Brian Aker
Takes the majority usage of parser based HA_CREATE_INFO usage around
1561
      if (!(lex_identified_temp_table))
1 by brian
clean slate
1562
      {
1046.1.2 by Brian Aker
Comments on LOCK_open
1563
        pthread_mutex_lock(&LOCK_open); /* CREATE TABLE... has found that the table already exists for insert and is adapting to use it */
1054.1.9 by Brian Aker
This is a large number of refactors against the Session class for its
1564
        if (session->reopen_name_locked_table(create_table, false))
1 by brian
clean slate
1565
        {
1235.2.1 by Brian Aker
More identifier work.
1566
          quick_rm_table(*session, identifier);
1 by brian
clean slate
1567
        }
1568
        else
1569
          table= create_table->table;
398.1.10 by Monty Taylor
Actually removed VOID() this time.
1570
        pthread_mutex_unlock(&LOCK_open);
1 by brian
clean slate
1571
      }
1572
      else
1573
      {
1109.1.3 by Brian Aker
Move names around a bit (to align similar methods)
1574
        if (!(table= session->openTable(create_table, (bool*) 0,
1054.1.10 by Brian Aker
Move open_table() to session.
1575
                                         DRIZZLE_OPEN_TEMPORARY_ONLY)) &&
1 by brian
clean slate
1576
            !create_info->table_existed)
1577
        {
1578
          /*
1579
            This shouldn't happen as creation of temporary table should make
1580
            it preparable for open. But let us do close_temporary_table() here
1581
            just in case.
1582
          */
1054.1.9 by Brian Aker
This is a large number of refactors against the Session class for its
1583
          session->drop_temporary_table(create_table);
1 by brian
clean slate
1584
        }
1585
      }
1586
    }
1587
    if (!table)                                   // open failed
1054.1.9 by Brian Aker
This is a large number of refactors against the Session class for its
1588
      return NULL;
1 by brian
clean slate
1589
  }
1590
1591
  table->reginfo.lock_type=TL_WRITE;
520.1.22 by Brian Aker
Second pass of thd cleanup
1592
  if (! ((*lock)= mysql_lock_tables(session, &table, 1,
831 by Brian Aker
Remove dead hooks code.
1593
                                    DRIZZLE_LOCK_IGNORE_FLUSH, &not_used)))
1 by brian
clean slate
1594
  {
1595
    if (*lock)
1596
    {
520.1.22 by Brian Aker
Second pass of thd cleanup
1597
      mysql_unlock_tables(session, *lock);
1 by brian
clean slate
1598
      *lock= 0;
1599
    }
1600
1601
    if (!create_info->table_existed)
1054.1.9 by Brian Aker
This is a large number of refactors against the Session class for its
1602
      session->drop_open_table(table, create_table->db, create_table->table_name);
1603
    return NULL;
1 by brian
clean slate
1604
  }
1054.1.9 by Brian Aker
This is a large number of refactors against the Session class for its
1605
1606
  return table;
1 by brian
clean slate
1607
}
1608
1609
1610
int
848 by Brian Aker
typdef class removal (just... use the name of the class).
1611
select_create::prepare(List<Item> &values, Select_Lex_Unit *u)
1 by brian
clean slate
1612
{
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
1613
  bool lex_identified_temp_table= (table_proto->type() == message::Table::TEMPORARY);
1222.2.1 by Brian Aker
Takes the majority usage of parser based HA_CREATE_INFO usage around
1614
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
1615
  DRIZZLE_LOCK *extra_lock= NULL;
1 by brian
clean slate
1616
  /*
1617
    For row-based replication, the CREATE-SELECT statement is written
1618
    in two pieces: the first one contain the CREATE TABLE statement
1619
    necessary to create the table and the second part contain the rows
1620
    that should go into the table.
1621
1622
    For non-temporary tables, the start of the CREATE-SELECT
1623
    implicitly commits the previous transaction, and all events
1624
    forming the statement will be stored the transaction cache. At end
1625
    of the statement, the entire statement is committed as a
1626
    transaction, and all events are written to the binary log.
1627
1628
    On the master, the table is locked for the duration of the
1629
    statement, but since the CREATE part is replicated as a simple
1630
    statement, there is no way to lock the table for accesses on the
1631
    slave.  Hence, we have to hold on to the CREATE part of the
1632
    statement until the statement has finished.
1633
   */
1634
1635
  unit= u;
1636
1637
  /*
1638
    Start a statement transaction before the create if we are using
1639
    row-based replication for the statement.  If we are creating a
1640
    temporary table, we need to start a statement transaction.
1641
  */
1642
520.1.22 by Brian Aker
Second pass of thd cleanup
1643
  if (!(table= create_table_from_items(session, create_info, create_table,
1067.1.1 by Stewart Smith
Beginning of create table proto in parser: pass which Engine is being used for CREATE TABLE in table proto.
1644
				       table_proto,
1 by brian
clean slate
1645
                                       alter_info, &values,
1222.2.3 by Brian Aker
Remove a few more options, from options in HA_CREATE_INFO.
1646
                                       is_if_not_exists,
831 by Brian Aker
Remove dead hooks code.
1647
                                       &extra_lock)))
51.2.2 by Patrick Galbraith
Removed DBUGs from
1648
    return(-1);				// abort() deletes table
1 by brian
clean slate
1649
1650
  if (extra_lock)
1651
  {
51.2.2 by Patrick Galbraith
Removed DBUGs from
1652
    assert(m_plock == NULL);
1 by brian
clean slate
1653
1222.2.1 by Brian Aker
Takes the majority usage of parser based HA_CREATE_INFO usage around
1654
    if (lex_identified_temp_table)
1 by brian
clean slate
1655
      m_plock= &m_lock;
1656
    else
520.1.22 by Brian Aker
Second pass of thd cleanup
1657
      m_plock= &session->extra_lock;
1 by brian
clean slate
1658
1659
    *m_plock= extra_lock;
1660
  }
1661
1662
  if (table->s->fields < values.elements)
1663
  {
1664
    my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), 1);
51.2.2 by Patrick Galbraith
Removed DBUGs from
1665
    return(-1);
1 by brian
clean slate
1666
  }
1667
1668
 /* First field to copy */
1669
  field= table->field+table->s->fields - values.elements;
1670
1671
  /* Mark all fields that are given values */
1672
  for (Field **f= field ; *f ; f++)
1005.2.12 by Monty Taylor
Moved some things to the API.
1673
    table->setWriteSet((*f)->field_index);
1 by brian
clean slate
1674
1675
  /* Don't set timestamp if used */
1676
  table->timestamp_field_type= TIMESTAMP_NO_AUTO_SET;
1677
  table->next_number_field=table->found_next_number_field;
1678
997.5.1 by chris
Replace macros around unireg.h, store_record,restore_record,cmp_record,empty_record
1679
  table->restoreRecordAsDefault();      // Get empty record
520.1.22 by Brian Aker
Second pass of thd cleanup
1680
  session->cuted_fields=0;
1 by brian
clean slate
1681
  if (info.ignore || info.handle_duplicates != DUP_ERROR)
1208.3.2 by brian
Update for Cursor renaming.
1682
    table->cursor->extra(HA_EXTRA_IGNORE_DUP_KEY);
1 by brian
clean slate
1683
  if (info.handle_duplicates == DUP_REPLACE)
1208.3.2 by brian
Update for Cursor renaming.
1684
    table->cursor->extra(HA_EXTRA_WRITE_CAN_REPLACE);
1 by brian
clean slate
1685
  if (info.handle_duplicates == DUP_UPDATE)
1208.3.2 by brian
Update for Cursor renaming.
1686
    table->cursor->extra(HA_EXTRA_INSERT_WITH_UPDATE);
1687
  table->cursor->ha_start_bulk_insert((ha_rows) 0);
520.1.22 by Brian Aker
Second pass of thd cleanup
1688
  session->abort_on_warning= !info.ignore;
1689
  if (check_that_all_fields_are_given_values(session, table, table_list))
51.2.2 by Patrick Galbraith
Removed DBUGs from
1690
    return(1);
1 by brian
clean slate
1691
  table->mark_columns_needed_for_insert();
1208.3.2 by brian
Update for Cursor renaming.
1692
  table->cursor->extra(HA_EXTRA_WRITE_CACHE);
51.2.2 by Patrick Galbraith
Removed DBUGs from
1693
  return(0);
1 by brian
clean slate
1694
}
1695
1696
void select_create::store_values(List<Item> &values)
1697
{
1235.1.11 by Brian Aker
Small cleanups, did in MERGE table only engine flag.
1698
  fill_record(session, field, values, true);
1 by brian
clean slate
1699
}
1700
1701
482 by Brian Aker
Remove uint.
1702
void select_create::send_error(uint32_t errcode,const char *err)
1 by brian
clean slate
1703
{
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1704
1 by brian
clean slate
1705
1706
  /*
1707
    This will execute any rollbacks that are necessary before writing
1708
    the transcation cache.
1709
1710
    We disable the binary log since nothing should be written to the
1711
    binary log.  This disabling is important, since we potentially do
1712
    a "roll back" of non-transactional tables by removing the table,
1713
    and the actual rollback might generate events that should not be
1714
    written to the binary log.
1715
1716
  */
1717
  select_insert::send_error(errcode, err);
1718
51.2.2 by Patrick Galbraith
Removed DBUGs from
1719
  return;
1 by brian
clean slate
1720
}
1721
1722
1723
bool select_create::send_eof()
1724
{
1725
  bool tmp=select_insert::send_eof();
1726
  if (tmp)
1727
    abort();
1728
  else
1729
  {
1730
    /*
1731
      Do an implicit commit at end of statement for non-temporary
1732
      tables.  This can fail, but we should unlock the table
1733
      nevertheless.
1734
    */
1735
    if (!table->s->tmp_table)
1736
    {
1273.1.2 by Jay Pipes
This patch does not change any algorithms or code paths,
1737
      TransactionServices &transaction_services= TransactionServices::singleton();
1738
      transaction_services.ha_autocommit_or_rollback(session, 0);
934.2.11 by Jay Pipes
Moves end_trans(), begin_trans(), end_active_trans() out of the parser module and adds startTransaction(), endTransaction(), and endActiveTransaction() member methods of Session object.
1739
      (void) session->endActiveTransaction();
1 by brian
clean slate
1740
    }
1741
1208.3.2 by brian
Update for Cursor renaming.
1742
    table->cursor->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
1743
    table->cursor->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
1 by brian
clean slate
1744
    if (m_plock)
1745
    {
520.1.22 by Brian Aker
Second pass of thd cleanup
1746
      mysql_unlock_tables(session, *m_plock);
1 by brian
clean slate
1747
      *m_plock= NULL;
1748
      m_plock= NULL;
1749
    }
1750
  }
1751
  return tmp;
1752
}
1753
1754
1755
void select_create::abort()
1756
{
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1757
1 by brian
clean slate
1758
1759
  /*
1760
    In select_insert::abort() we roll back the statement, including
1761
    truncating the transaction cache of the binary log. To do this, we
1762
    pretend that the statement is transactional, even though it might
1763
    be the case that it was not.
1764
1765
    We roll back the statement prior to deleting the table and prior
1766
    to releasing the lock on the table, since there might be potential
1767
    for failure if the rollback is executed after the drop or after
1768
    unlocking the table.
1769
1770
    We also roll back the statement regardless of whether the creation
1771
    of the table succeeded or not, since we need to reset the binary
1772
    log state.
1773
  */
1774
  select_insert::abort();
520.1.22 by Brian Aker
Second pass of thd cleanup
1775
  session->transaction.stmt.modified_non_trans_table= false;
1 by brian
clean slate
1776
1777
1778
  if (m_plock)
1779
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
1780
    mysql_unlock_tables(session, *m_plock);
1 by brian
clean slate
1781
    *m_plock= NULL;
1782
    m_plock= NULL;
1783
  }
1784
1785
  if (table)
1786
  {
1208.3.2 by brian
Update for Cursor renaming.
1787
    table->cursor->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
1788
    table->cursor->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
1 by brian
clean slate
1789
    if (!create_info->table_existed)
1054.1.9 by Brian Aker
This is a large number of refactors against the Session class for its
1790
      session->drop_open_table(table, create_table->db, create_table->table_name);
1791
    table= NULL;                                    // Safety
1 by brian
clean slate
1792
  }
1793
}
1794
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
1795
} /* namespace drizzled */