~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_insert.cc

  • Committer: Jay Pipes
  • Date: 2008-09-11 16:03:22 UTC
  • mto: (383.5.1 trunk)
  • mto: This revision was merged to the branch mainline in revision 386.
  • Revision ID: jay@mysql.com-20080911160322-vrl0k1djo6q6ytv1
Removed SQL_MODE variances from comment_table.test and ensured correct error thrown when a comment that is too long was input.  After moving to proto buffer definition for table, the 2048 length will go away.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
13
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
16
 
17
17
/* Insert of records */
18
18
 
19
 
#include "config.h"
20
 
#include <cstdio>
 
19
/*
 
20
  INSERT DELAYED
 
21
 
 
22
  Drizzle has a different form of DELAYED then MySQL. DELAYED is just
 
23
  a hint to the the sorage engine (which can then do whatever it likes.
 
24
*/
 
25
#include <drizzled/server_includes.h>
21
26
#include <drizzled/sql_select.h>
22
 
#include <drizzled/show.h>
23
 
#include <drizzled/error.h>
24
 
#include <drizzled/name_resolution_context_state.h>
25
 
#include <drizzled/probes.h>
26
 
#include <drizzled/sql_base.h>
27
 
#include <drizzled/sql_load.h>
28
 
#include <drizzled/field/timestamp.h>
29
 
#include <drizzled/lock.h>
30
 
#include "drizzled/sql_table.h"
31
 
#include "drizzled/pthread_globals.h"
32
 
#include "drizzled/transaction_services.h"
33
 
#include "drizzled/plugin/transactional_storage_engine.h"
34
 
 
35
 
#include "drizzled/table/shell.h"
36
 
 
37
 
namespace drizzled
38
 
{
39
 
 
40
 
extern plugin::StorageEngine *heap_engine;
41
 
extern plugin::StorageEngine *myisam_engine;
 
27
#include <drizzled/sql_show.h>
 
28
#include "rpl_mi.h"
 
29
#include <drizzled/drizzled_error_messages.h>
 
30
 
 
31
/* Define to force use of my_malloc() if the allocated memory block is big */
 
32
 
 
33
#ifndef HAVE_ALLOCA
 
34
#define my_safe_alloca(size, min_length) my_alloca(size)
 
35
#define my_safe_afree(ptr, size, min_length) my_afree(ptr)
 
36
#else
 
37
#define my_safe_alloca(size, min_length) ((size <= min_length) ? my_alloca(size) : my_malloc(size,MYF(0)))
 
38
#define my_safe_afree(ptr, size, min_length) if (size > min_length) my_free(ptr,MYF(0))
 
39
#endif
 
40
 
 
41
 
42
42
 
43
43
/*
44
44
  Check if insert fields are correct.
45
45
 
46
46
  SYNOPSIS
47
47
    check_insert_fields()
48
 
    session                         The current thread.
 
48
    thd                         The current thread.
49
49
    table                       The table for insert.
50
50
    fields                      The insert fields.
51
51
    values                      The insert values.
61
61
    -1          Error
62
62
*/
63
63
 
64
 
static int check_insert_fields(Session *session, TableList *table_list,
 
64
static int check_insert_fields(THD *thd, TableList *table_list,
65
65
                               List<Item> &fields, List<Item> &values,
66
66
                               bool check_unique,
67
 
                               table_map *)
 
67
                               table_map *map __attribute__((unused)))
68
68
{
69
69
  Table *table= table_list->table;
70
70
 
71
71
  if (fields.elements == 0 && values.elements != 0)
72
72
  {
73
 
    if (values.elements != table->getShare()->sizeFields())
 
73
    if (values.elements != table->s->fields)
74
74
    {
75
75
      my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), 1L);
76
76
      return -1;
81
81
      No fields are provided so all fields must be provided in the values.
82
82
      Thus we set all bits in the write set.
83
83
    */
84
 
    table->setWriteSet();
 
84
    bitmap_set_all(table->write_set);
85
85
  }
86
86
  else
87
87
  {                                             // Part field list
88
 
    Select_Lex *select_lex= &session->lex->select_lex;
 
88
    SELECT_LEX *select_lex= &thd->lex->select_lex;
89
89
    Name_resolution_context *context= &select_lex->context;
90
90
    Name_resolution_context_state ctx_state;
91
91
    int res;
96
96
      return -1;
97
97
    }
98
98
 
99
 
    session->dup_field= 0;
 
99
    thd->dup_field= 0;
100
100
 
101
101
    /* Save the state of the current name resolution context. */
102
102
    ctx_state.save_state(context, table_list);
107
107
    */
108
108
    table_list->next_local= 0;
109
109
    context->resolve_in_table_list_only(table_list);
110
 
    res= setup_fields(session, 0, fields, MARK_COLUMNS_WRITE, 0, 0);
 
110
    res= setup_fields(thd, 0, fields, MARK_COLUMNS_WRITE, 0, 0);
111
111
 
112
112
    /* Restore the current context. */
113
113
    ctx_state.restore_state(context, table_list);
115
115
    if (res)
116
116
      return -1;
117
117
 
118
 
    if (check_unique && session->dup_field)
 
118
    if (check_unique && thd->dup_field)
119
119
    {
120
 
      my_error(ER_FIELD_SPECIFIED_TWICE, MYF(0), session->dup_field->field_name);
 
120
      my_error(ER_FIELD_SPECIFIED_TWICE, MYF(0), thd->dup_field->field_name);
121
121
      return -1;
122
122
    }
123
123
    if (table->timestamp_field) // Don't automaticly set timestamp if used
124
124
    {
125
 
      if (table->timestamp_field->isWriteSet())
126
 
      {
 
125
      if (bitmap_is_set(table->write_set,
 
126
                        table->timestamp_field->field_index))
127
127
        clear_timestamp_auto_bits(table->timestamp_field_type,
128
128
                                  TIMESTAMP_AUTO_SET_ON_INSERT);
129
 
      }
130
129
      else
131
130
      {
132
 
        table->setWriteSet(table->timestamp_field->position());
 
131
        bitmap_set_bit(table->write_set,
 
132
                       table->timestamp_field->field_index);
133
133
      }
134
134
    }
135
135
  }
143
143
 
144
144
  SYNOPSIS
145
145
    check_update_fields()
146
 
    session                         The current thread.
 
146
    thd                         The current thread.
147
147
    insert_table_list           The insert table list.
148
148
    table                       The table for update.
149
149
    update_fields               The update fields.
157
157
    -1          Error
158
158
*/
159
159
 
160
 
static int check_update_fields(Session *session, TableList *insert_table_list,
 
160
static int check_update_fields(THD *thd, TableList *insert_table_list,
161
161
                               List<Item> &update_fields,
162
 
                               table_map *)
 
162
                               table_map *map __attribute__((unused)))
163
163
{
164
164
  Table *table= insert_table_list->table;
165
165
  bool timestamp_mark= false;
170
170
      Unmark the timestamp field so that we can check if this is modified
171
171
      by update_fields
172
172
    */
173
 
    timestamp_mark= table->write_set->test(table->timestamp_field->position());
174
 
    table->write_set->reset(table->timestamp_field->position());
 
173
    timestamp_mark= bitmap_test_and_clear(table->write_set,
 
174
                                          table->timestamp_field->field_index);
175
175
  }
176
176
 
177
177
  /* Check the fields we are going to modify */
178
 
  if (setup_fields(session, 0, update_fields, MARK_COLUMNS_WRITE, 0, 0))
 
178
  if (setup_fields(thd, 0, update_fields, MARK_COLUMNS_WRITE, 0, 0))
179
179
    return -1;
180
180
 
181
181
  if (table->timestamp_field)
182
182
  {
183
183
    /* Don't set timestamp column if this is modified. */
184
 
    if (table->timestamp_field->isWriteSet())
185
 
    {
 
184
    if (bitmap_is_set(table->write_set,
 
185
                      table->timestamp_field->field_index))
186
186
      clear_timestamp_auto_bits(table->timestamp_field_type,
187
187
                                TIMESTAMP_AUTO_SET_ON_UPDATE);
188
 
    }
189
 
 
190
188
    if (timestamp_mark)
191
 
    {
192
 
      table->setWriteSet(table->timestamp_field->position());
193
 
    }
 
189
      bitmap_set_bit(table->write_set,
 
190
                     table->timestamp_field->field_index);
194
191
  }
195
192
  return 0;
196
193
}
205
202
*/
206
203
 
207
204
static
208
 
void upgrade_lock_type(Session *,
 
205
void upgrade_lock_type(THD *thd __attribute__((unused)),
209
206
                       thr_lock_type *lock_type,
210
207
                       enum_duplicates duplic,
211
 
                       bool )
 
208
                       bool is_multi_insert __attribute__((unused)))
212
209
{
213
210
  if (duplic == DUP_UPDATE ||
214
211
      (duplic == DUP_REPLACE && *lock_type == TL_WRITE_CONCURRENT_INSERT))
227
224
  end of dispatch_command().
228
225
*/
229
226
 
230
 
bool mysql_insert(Session *session,TableList *table_list,
 
227
bool mysql_insert(THD *thd,TableList *table_list,
231
228
                  List<Item> &fields,
232
229
                  List<List_item> &values_list,
233
230
                  List<Item> &update_fields,
238
235
  int error;
239
236
  bool transactional_table, joins_freed= false;
240
237
  bool changed;
241
 
  uint32_t value_count;
 
238
  bool was_insert_delayed= (table_list->lock_type ==  TL_WRITE_DELAYED);
 
239
  uint value_count;
242
240
  ulong counter = 1;
243
241
  uint64_t id;
244
 
  CopyInfo info;
 
242
  COPY_INFO info;
245
243
  Table *table= 0;
246
244
  List_iterator_fast<List_item> its(values_list);
247
245
  List_item *values;
249
247
  Name_resolution_context_state ctx_state;
250
248
  thr_lock_type lock_type;
251
249
  Item *unused_conds= 0;
252
 
 
 
250
  
253
251
 
254
252
  /*
255
253
    Upgrade lock type if the requested lock is incompatible with
256
254
    the current connection mode or table operation.
257
255
  */
258
 
  upgrade_lock_type(session, &table_list->lock_type, duplic,
 
256
  upgrade_lock_type(thd, &table_list->lock_type, duplic,
259
257
                    values_list.elements > 1);
260
258
 
261
 
  if (session->openTablesLock(table_list))
 
259
  /*
 
260
    We can't write-delayed into a table locked with LOCK TABLES:
 
261
    this will lead to a deadlock, since the delayed thread will
 
262
    never be able to get a lock on the table. QQQ: why not
 
263
    upgrade the lock here instead?
 
264
  */
 
265
  if (table_list->lock_type == TL_WRITE_DELAYED && thd->locked_tables &&
 
266
      find_locked_table(thd, table_list->db, table_list->table_name))
262
267
  {
263
 
    DRIZZLE_INSERT_DONE(1, 0);
264
 
    return true;
 
268
    my_error(ER_DELAYED_INSERT_TABLE_LOCKED, MYF(0),
 
269
             table_list->table_name);
 
270
    return(true);
265
271
  }
266
272
 
 
273
  {
 
274
    if (open_and_lock_tables(thd, table_list))
 
275
      return(true);
 
276
  }
267
277
  lock_type= table_list->lock_type;
268
278
 
269
 
  session->set_proc_info("init");
270
 
  session->used_tables=0;
 
279
  thd_proc_info(thd, "init");
 
280
  thd->used_tables=0;
271
281
  values= its++;
272
282
  value_count= values->elements;
273
283
 
274
 
  if (mysql_prepare_insert(session, table_list, table, fields, values,
 
284
  if (mysql_prepare_insert(thd, table_list, table, fields, values,
275
285
                           update_fields, update_values, duplic, &unused_conds,
276
286
                           false,
277
287
                           (fields.elements || !value_count ||
278
288
                            (0) != 0), !ignore))
279
 
  {
280
 
    if (table != NULL)
281
 
      table->cursor->ha_release_auto_increment();
282
 
    if (!joins_freed)
283
 
      free_underlaid_joins(session, &session->lex->select_lex);
284
 
    session->abort_on_warning= 0;
285
 
    DRIZZLE_INSERT_DONE(1, 0);
286
 
    return true;
287
 
  }
 
289
    goto abort;
288
290
 
289
291
  /* mysql_prepare_insert set table_list->table if it was not set */
290
292
  table= table_list->table;
291
293
 
292
 
  context= &session->lex->select_lex.context;
 
294
  context= &thd->lex->select_lex.context;
293
295
  /*
294
296
    These three asserts test the hypothesis that the resetting of the name
295
297
    resolution context below is not necessary at all since the list of local
315
317
    if (values->elements != value_count)
316
318
    {
317
319
      my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), counter);
318
 
 
319
 
      if (table != NULL)
320
 
        table->cursor->ha_release_auto_increment();
321
 
      if (!joins_freed)
322
 
        free_underlaid_joins(session, &session->lex->select_lex);
323
 
      session->abort_on_warning= 0;
324
 
      DRIZZLE_INSERT_DONE(1, 0);
325
 
 
326
 
      return true;
327
 
    }
328
 
    if (setup_fields(session, 0, *values, MARK_COLUMNS_READ, 0, 0))
329
 
    {
330
 
      if (table != NULL)
331
 
        table->cursor->ha_release_auto_increment();
332
 
      if (!joins_freed)
333
 
        free_underlaid_joins(session, &session->lex->select_lex);
334
 
      session->abort_on_warning= 0;
335
 
      DRIZZLE_INSERT_DONE(1, 0);
336
 
      return true;
337
 
    }
 
320
      goto abort;
 
321
    }
 
322
    if (setup_fields(thd, 0, *values, MARK_COLUMNS_READ, 0, 0))
 
323
      goto abort;
338
324
  }
339
325
  its.rewind ();
340
 
 
 
326
 
341
327
  /* Restore the current context. */
342
328
  ctx_state.restore_state(context, table_list);
343
329
 
344
330
  /*
345
 
    Fill in the given fields and dump it to the table cursor
 
331
    Fill in the given fields and dump it to the table file
346
332
  */
 
333
  memset(&info, 0, sizeof(info));
347
334
  info.ignore= ignore;
348
335
  info.handle_duplicates=duplic;
349
336
  info.update_fields= &update_fields;
354
341
    For single line insert, generate an error if try to set a NOT NULL field
355
342
    to NULL.
356
343
  */
357
 
  session->count_cuted_fields= ignore ? CHECK_FIELD_WARN : CHECK_FIELD_ERROR_FOR_NULL;
358
 
 
359
 
  session->cuted_fields = 0L;
 
344
  thd->count_cuted_fields= ((values_list.elements == 1 &&
 
345
                             !ignore) ?
 
346
                            CHECK_FIELD_ERROR_FOR_NULL :
 
347
                            CHECK_FIELD_WARN);
 
348
  thd->cuted_fields = 0L;
360
349
  table->next_number_field=table->found_next_number_field;
361
350
 
 
351
#ifdef HAVE_REPLICATION
 
352
  if (thd->slave_thread &&
 
353
      (info.handle_duplicates == DUP_UPDATE) &&
 
354
      (table->next_number_field != NULL) &&
 
355
      rpl_master_has_bug(&active_mi->rli, 24432))
 
356
    goto abort;
 
357
#endif
 
358
 
362
359
  error=0;
363
 
  session->set_proc_info("update");
 
360
  thd_proc_info(thd, "update");
364
361
  if (duplic == DUP_REPLACE)
365
 
    table->cursor->extra(HA_EXTRA_WRITE_CAN_REPLACE);
 
362
    table->file->extra(HA_EXTRA_WRITE_CAN_REPLACE);
366
363
  if (duplic == DUP_UPDATE)
367
 
    table->cursor->extra(HA_EXTRA_INSERT_WITH_UPDATE);
 
364
    table->file->extra(HA_EXTRA_INSERT_WITH_UPDATE);
368
365
  {
369
366
    if (duplic != DUP_ERROR || ignore)
370
 
      table->cursor->extra(HA_EXTRA_IGNORE_DUP_KEY);
371
 
    table->cursor->ha_start_bulk_insert(values_list.elements);
 
367
      table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
 
368
    table->file->ha_start_bulk_insert(values_list.elements);
372
369
  }
373
370
 
374
371
 
375
 
  session->abort_on_warning= !ignore;
 
372
  thd->abort_on_warning= !ignore;
376
373
 
377
374
  table->mark_columns_needed_for_insert();
378
375
 
380
377
  {
381
378
    if (fields.elements || !value_count)
382
379
    {
383
 
      table->restoreRecordAsDefault();  // Get empty record
384
 
      if (fill_record(session, fields, *values))
 
380
      restore_record(table,s->default_values);  // Get empty record
 
381
      if (fill_record(thd, fields, *values, 0))
385
382
      {
386
 
        if (values_list.elements != 1 && ! session->is_error())
 
383
        if (values_list.elements != 1 && ! thd->is_error())
387
384
        {
388
385
          info.records++;
389
386
          continue;
390
387
        }
391
388
        /*
392
 
          TODO: set session->abort_on_warning if values_list.elements == 1
 
389
          TODO: set thd->abort_on_warning if values_list.elements == 1
393
390
          and check that all items return warning in case of problem with
394
391
          storing field.
395
392
        */
399
396
    }
400
397
    else
401
398
    {
402
 
      table->restoreRecordAsDefault();  // Get empty record
403
 
 
404
 
      if (fill_record(session, table->getFields(), *values))
405
 
      {
406
 
        if (values_list.elements != 1 && ! session->is_error())
 
399
      if (thd->used_tables)                     // Column used in values()
 
400
        restore_record(table,s->default_values);        // Get empty record
 
401
      else
 
402
      {
 
403
        /*
 
404
          Fix delete marker. No need to restore rest of record since it will
 
405
          be overwritten by fill_record() anyway (and fill_record() does not
 
406
          use default values in this case).
 
407
        */
 
408
        table->record[0][0]= table->s->default_values[0];
 
409
      }
 
410
      if (fill_record(thd, table->field, *values, 0))
 
411
      {
 
412
        if (values_list.elements != 1 && ! thd->is_error())
407
413
        {
408
414
          info.records++;
409
415
          continue;
413
419
      }
414
420
    }
415
421
 
416
 
    // Release latches in case bulk insert takes a long time
417
 
    plugin::TransactionalStorageEngine::releaseTemporaryLatches(session);
418
 
 
419
 
    error=write_record(session, table ,&info);
 
422
    error=write_record(thd, table ,&info);
420
423
    if (error)
421
424
      break;
422
 
    session->row_count++;
 
425
    thd->row_count++;
423
426
  }
424
427
 
425
 
  free_underlaid_joins(session, &session->lex->select_lex);
 
428
  free_underlaid_joins(thd, &thd->lex->select_lex);
426
429
  joins_freed= true;
427
430
 
428
431
  /*
434
437
      Do not do this release if this is a delayed insert, it would steal
435
438
      auto_inc values from the delayed_insert thread as they share Table.
436
439
    */
437
 
    table->cursor->ha_release_auto_increment();
438
 
    if (table->cursor->ha_end_bulk_insert() && !error)
 
440
    table->file->ha_release_auto_increment();
 
441
    if (table->file->ha_end_bulk_insert() && !error)
439
442
    {
440
 
      table->print_error(errno,MYF(0));
 
443
      table->file->print_error(my_errno,MYF(0));
441
444
      error=1;
442
445
    }
443
446
    if (duplic != DUP_ERROR || ignore)
444
 
      table->cursor->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
445
 
 
446
 
    transactional_table= table->cursor->has_transactions();
447
 
 
448
 
    changed= (info.copied || info.deleted || info.updated);
449
 
    if ((changed && error <= 0) || session->transaction.stmt.hasModifiedNonTransData())
450
 
    {
451
 
      if (session->transaction.stmt.hasModifiedNonTransData())
452
 
        session->transaction.all.markModifiedNonTransData();
453
 
    }
454
 
    assert(transactional_table || !changed || session->transaction.stmt.hasModifiedNonTransData());
 
447
      table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
 
448
 
 
449
    transactional_table= table->file->has_transactions();
 
450
 
 
451
    if ((changed= (info.copied || info.deleted || info.updated)))
 
452
    {
 
453
      /*
 
454
        Invalidate the table in the query cache if something changed.
 
455
        For the transactional algorithm to work the invalidation must be
 
456
        before binlog writing and ha_autocommit_or_rollback
 
457
      */
 
458
    }
 
459
    if ((changed && error <= 0) || thd->transaction.stmt.modified_non_trans_table || was_insert_delayed)
 
460
    {
 
461
      if (mysql_bin_log.is_open())
 
462
      {
 
463
        if (error <= 0)
 
464
        {
 
465
          /*
 
466
            [Guilhem wrote] Temporary errors may have filled
 
467
            thd->net.last_error/errno.  For example if there has
 
468
            been a disk full error when writing the row, and it was
 
469
            MyISAM, then thd->net.last_error/errno will be set to
 
470
            "disk full"... and the my_pwrite() will wait until free
 
471
            space appears, and so when it finishes then the
 
472
            write_row() was entirely successful
 
473
          */
 
474
          /* todo: consider removing */
 
475
          thd->clear_error();
 
476
        }
 
477
        /* bug#22725:
 
478
 
 
479
        A query which per-row-loop can not be interrupted with
 
480
        KILLED, like INSERT, and that does not invoke stored
 
481
        routines can be binlogged with neglecting the KILLED error.
 
482
        
 
483
        If there was no error (error == zero) until after the end of
 
484
        inserting loop the KILLED flag that appeared later can be
 
485
        disregarded since previously possible invocation of stored
 
486
        routines did not result in any error due to the KILLED.  In
 
487
        such case the flag is ignored for constructing binlog event.
 
488
        */
 
489
        assert(thd->killed != THD::KILL_BAD_DATA || error > 0);
 
490
        if (thd->binlog_query(THD::ROW_QUERY_TYPE,
 
491
                              thd->query, thd->query_length,
 
492
                              transactional_table, false,
 
493
                              (error>0) ? thd->killed : THD::NOT_KILLED) &&
 
494
            transactional_table)
 
495
        {
 
496
          error=1;
 
497
        }
 
498
      }
 
499
      if (thd->transaction.stmt.modified_non_trans_table)
 
500
        thd->transaction.all.modified_non_trans_table= true;
 
501
    }
 
502
    assert(transactional_table || !changed || 
 
503
                thd->transaction.stmt.modified_non_trans_table);
455
504
 
456
505
  }
457
 
  session->set_proc_info("end");
 
506
  thd_proc_info(thd, "end");
458
507
  /*
459
508
    We'll report to the client this id:
460
509
    - if the table contains an autoincrement column and we successfully
465
514
    inserted, the id of the last "inserted" row (if IGNORE, that value may not
466
515
    have been really inserted but ignored).
467
516
  */
468
 
  id= (session->first_successful_insert_id_in_cur_stmt > 0) ?
469
 
    session->first_successful_insert_id_in_cur_stmt :
470
 
    (session->arg_of_last_insert_id_function ?
471
 
     session->first_successful_insert_id_in_prev_stmt :
 
517
  id= (thd->first_successful_insert_id_in_cur_stmt > 0) ?
 
518
    thd->first_successful_insert_id_in_cur_stmt :
 
519
    (thd->arg_of_last_insert_id_function ?
 
520
     thd->first_successful_insert_id_in_prev_stmt :
472
521
     ((table->next_number_field && info.copied) ?
473
522
     table->next_number_field->val_int() : 0));
474
523
  table->next_number_field=0;
475
 
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
 
524
  thd->count_cuted_fields= CHECK_FIELD_IGNORE;
476
525
  table->auto_increment_field_not_null= false;
477
526
  if (duplic == DUP_REPLACE)
478
 
    table->cursor->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
 
527
    table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
479
528
 
480
529
  if (error)
481
 
  {
482
 
    if (table != NULL)
483
 
      table->cursor->ha_release_auto_increment();
484
 
    if (!joins_freed)
485
 
      free_underlaid_joins(session, &session->lex->select_lex);
486
 
    session->abort_on_warning= 0;
487
 
    DRIZZLE_INSERT_DONE(1, 0);
488
 
    return true;
489
 
  }
490
 
 
491
 
  if (values_list.elements == 1 && (!(session->options & OPTION_WARNINGS) ||
492
 
                                    !session->cuted_fields))
493
 
  {
494
 
    session->row_count_func= info.copied + info.deleted + info.updated;
495
 
    session->my_ok((ulong) session->row_count_func,
496
 
                   info.copied + info.deleted + info.touched, id);
 
530
    goto abort;
 
531
  if (values_list.elements == 1 && (!(thd->options & OPTION_WARNINGS) ||
 
532
                                    !thd->cuted_fields))
 
533
  {
 
534
    thd->row_count_func= info.copied + info.deleted +
 
535
                         ((thd->client_capabilities & CLIENT_FOUND_ROWS) ?
 
536
                          info.touched : info.updated);
 
537
    my_ok(thd, (ulong) thd->row_count_func, id);
497
538
  }
498
539
  else
499
540
  {
500
541
    char buff[160];
 
542
    ha_rows updated=((thd->client_capabilities & CLIENT_FOUND_ROWS) ?
 
543
                     info.touched : info.updated);
501
544
    if (ignore)
502
 
      snprintf(buff, sizeof(buff), ER(ER_INSERT_INFO), (ulong) info.records,
503
 
              (ulong) (info.records - info.copied), (ulong) session->cuted_fields);
 
545
      sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
 
546
              (ulong) (info.records - info.copied), (ulong) thd->cuted_fields);
504
547
    else
505
 
      snprintf(buff, sizeof(buff), ER(ER_INSERT_INFO), (ulong) info.records,
506
 
              (ulong) (info.deleted + info.updated), (ulong) session->cuted_fields);
507
 
    session->row_count_func= info.copied + info.deleted + info.updated;
508
 
    session->my_ok((ulong) session->row_count_func,
509
 
                   info.copied + info.deleted + info.touched, id, buff);
 
548
      sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
 
549
              (ulong) (info.deleted + updated), (ulong) thd->cuted_fields);
 
550
    thd->row_count_func= info.copied + info.deleted + updated;
 
551
    ::my_ok(thd, (ulong) thd->row_count_func, id, buff);
510
552
  }
511
 
  session->status_var.inserted_row_count+= session->row_count_func;
512
 
  session->abort_on_warning= 0;
513
 
  DRIZZLE_INSERT_DONE(0, session->row_count_func);
 
553
  thd->abort_on_warning= 0;
 
554
  DRIZZLE_INSERT_END();
 
555
  return(false);
514
556
 
515
 
  return false;
 
557
abort:
 
558
  if (table != NULL)
 
559
    table->file->ha_release_auto_increment();
 
560
  if (!joins_freed)
 
561
    free_underlaid_joins(thd, &thd->lex->select_lex);
 
562
  thd->abort_on_warning= 0;
 
563
  DRIZZLE_INSERT_END();
 
564
  return(true);
516
565
}
517
566
 
518
567
 
521
570
 
522
571
  SYNOPSIS
523
572
     mysql_prepare_insert_check_table()
524
 
     session            Thread handle
 
573
     thd                Thread handle
525
574
     table_list         Table list
526
575
     fields             List of fields to be updated
527
576
     where              Pointer to where clause
532
581
     true  ERROR
533
582
*/
534
583
 
535
 
static bool mysql_prepare_insert_check_table(Session *session, TableList *table_list,
536
 
                                             List<Item> &,
 
584
static bool mysql_prepare_insert_check_table(THD *thd, TableList *table_list,
 
585
                                             List<Item> &fields __attribute__((unused)),
537
586
                                             bool select_insert)
538
587
{
539
 
 
 
588
  
540
589
 
541
590
  /*
542
591
     first table in list is the one we'll INSERT into, requires INSERT_ACL.
545
594
     than INSERT.
546
595
  */
547
596
 
548
 
  if (setup_tables_and_check_access(session, &session->lex->select_lex.context,
549
 
                                    &session->lex->select_lex.top_join_list,
 
597
  if (setup_tables_and_check_access(thd, &thd->lex->select_lex.context,
 
598
                                    &thd->lex->select_lex.top_join_list,
550
599
                                    table_list,
551
 
                                    &session->lex->select_lex.leaf_tables,
 
600
                                    &thd->lex->select_lex.leaf_tables,
552
601
                                    select_insert))
553
602
    return(true);
554
603
 
561
610
 
562
611
  SYNOPSIS
563
612
    mysql_prepare_insert()
564
 
    session                     Thread handler
 
613
    thd                 Thread handler
565
614
    table_list          Global/local table list
566
615
    table               Table to insert into (can be NULL if table should
567
 
                        be taken from table_list->table)
 
616
                        be taken from table_list->table)    
568
617
    where               Where clause (for insert ... select)
569
618
    select_insert       true if INSERT ... SELECT statement
570
 
    check_fields        true if need to check that all INSERT fields are
 
619
    check_fields        true if need to check that all INSERT fields are 
571
620
                        given values.
572
 
    abort_on_warning    whether to report if some INSERT field is not
 
621
    abort_on_warning    whether to report if some INSERT field is not 
573
622
                        assigned as an error (true) or as a warning (false).
574
623
 
575
624
  TODO (in far future)
581
630
  WARNING
582
631
    You MUST set table->insert_values to 0 after calling this function
583
632
    before releasing the table object.
584
 
 
 
633
  
585
634
  RETURN VALUE
586
635
    false OK
587
636
    true  error
588
637
*/
589
638
 
590
 
bool mysql_prepare_insert(Session *session, TableList *table_list,
 
639
bool mysql_prepare_insert(THD *thd, TableList *table_list,
591
640
                          Table *table, List<Item> &fields, List_item *values,
592
641
                          List<Item> &update_fields, List<Item> &update_values,
593
642
                          enum_duplicates duplic,
594
 
                          COND **,
 
643
                          COND **where __attribute__((unused)),
595
644
                          bool select_insert,
596
645
                          bool check_fields, bool abort_on_warning)
597
646
{
598
 
  Select_Lex *select_lex= &session->lex->select_lex;
 
647
  SELECT_LEX *select_lex= &thd->lex->select_lex;
599
648
  Name_resolution_context *context= &select_lex->context;
600
649
  Name_resolution_context_state ctx_state;
601
650
  bool insert_into_view= (0 != 0);
602
651
  bool res= 0;
603
652
  table_map map= 0;
604
 
 
 
653
  
605
654
  /* INSERT should have a SELECT or VALUES clause */
606
655
  assert (!select_insert || !values);
607
656
 
608
657
  /*
609
658
    For subqueries in VALUES() we should not see the table in which we are
610
659
    inserting (for INSERT ... SELECT this is done by changing table_list,
611
 
    because INSERT ... SELECT share Select_Lex it with SELECT.
 
660
    because INSERT ... SELECT share SELECT_LEX it with SELECT.
612
661
  */
613
662
  if (!select_insert)
614
663
  {
615
 
    for (Select_Lex_Unit *un= select_lex->first_inner_unit();
 
664
    for (SELECT_LEX_UNIT *un= select_lex->first_inner_unit();
616
665
         un;
617
666
         un= un->next_unit())
618
667
    {
619
 
      for (Select_Lex *sl= un->first_select();
 
668
      for (SELECT_LEX *sl= un->first_select();
620
669
           sl;
621
670
           sl= sl->next_select())
622
671
      {
628
677
  if (duplic == DUP_UPDATE)
629
678
  {
630
679
    /* it should be allocated before Item::fix_fields() */
631
 
    if (table_list->set_insert_values(session->mem_root))
 
680
    if (table_list->set_insert_values(thd->mem_root))
632
681
      return(true);
633
682
  }
634
683
 
635
 
  if (mysql_prepare_insert_check_table(session, table_list, fields, select_insert))
 
684
  if (mysql_prepare_insert_check_table(thd, table_list, fields, select_insert))
636
685
    return(true);
637
686
 
638
687
 
652
701
    table_list->next_local= 0;
653
702
    context->resolve_in_table_list_only(table_list);
654
703
 
655
 
    res= check_insert_fields(session, context->table_list, fields, *values,
 
704
    res= check_insert_fields(thd, context->table_list, fields, *values,
656
705
                             !insert_into_view, &map) ||
657
 
      setup_fields(session, 0, *values, MARK_COLUMNS_READ, 0, 0);
 
706
      setup_fields(thd, 0, *values, MARK_COLUMNS_READ, 0, 0);
658
707
 
659
708
    if (!res && check_fields)
660
709
    {
661
 
      bool saved_abort_on_warning= session->abort_on_warning;
662
 
      session->abort_on_warning= abort_on_warning;
663
 
      res= check_that_all_fields_are_given_values(session,
664
 
                                                  table ? table :
 
710
      bool saved_abort_on_warning= thd->abort_on_warning;
 
711
      thd->abort_on_warning= abort_on_warning;
 
712
      res= check_that_all_fields_are_given_values(thd, 
 
713
                                                  table ? table : 
665
714
                                                  context->table_list->table,
666
715
                                                  context->table_list);
667
 
      session->abort_on_warning= saved_abort_on_warning;
 
716
      thd->abort_on_warning= saved_abort_on_warning;
668
717
    }
669
718
 
670
719
    if (!res && duplic == DUP_UPDATE)
671
720
    {
672
 
      res= check_update_fields(session, context->table_list, update_fields, &map);
 
721
      res= check_update_fields(thd, context->table_list, update_fields, &map);
673
722
    }
674
723
 
675
724
    /* Restore the current context. */
676
725
    ctx_state.restore_state(context, table_list);
677
726
 
678
727
    if (!res)
679
 
      res= setup_fields(session, 0, update_values, MARK_COLUMNS_READ, 0, 0);
 
728
      res= setup_fields(thd, 0, update_values, MARK_COLUMNS_READ, 0, 0);
680
729
  }
681
730
 
682
731
  if (res)
688
737
  if (!select_insert)
689
738
  {
690
739
    TableList *duplicate;
691
 
    if ((duplicate= unique_table(table_list, table_list->next_global, true)))
 
740
    if ((duplicate= unique_table(thd, table_list, table_list->next_global, 1)))
692
741
    {
693
 
      my_error(ER_UPDATE_TABLE_USED, MYF(0), table_list->alias);
694
 
 
695
 
      return true;
 
742
      update_non_unique_table_error(table_list, "INSERT", duplicate);
 
743
      return(true);
696
744
    }
 
745
    select_lex->first_execution= 0;
697
746
  }
698
747
  if (duplic == DUP_UPDATE || duplic == DUP_REPLACE)
699
748
    table->prepare_for_position();
700
 
 
701
 
  return false;
 
749
  return(false);
702
750
}
703
751
 
704
752
 
705
753
        /* Check if there is more uniq keys after field */
706
754
 
707
 
static int last_uniq_key(Table *table,uint32_t keynr)
 
755
static int last_uniq_key(Table *table,uint keynr)
708
756
{
709
 
  while (++keynr < table->getShare()->sizeKeys())
 
757
  while (++keynr < table->s->keys)
710
758
    if (table->key_info[keynr].flags & HA_NOSAME)
711
759
      return 0;
712
760
  return 1;
719
767
 
720
768
  SYNOPSIS
721
769
     write_record()
722
 
      session   - thread context
 
770
      thd   - thread context
723
771
      table - table to which record should be written
724
 
      info  - CopyInfo structure describing handling of duplicates
 
772
      info  - COPY_INFO structure describing handling of duplicates
725
773
              and which is used for counting number of records inserted
726
774
              and deleted.
727
775
 
731
779
    then both on update triggers will work instead. Similarly both on
732
780
    delete triggers will be invoked if we will delete conflicting records.
733
781
 
734
 
    Sets session->transaction.stmt.modified_non_trans_data to true if table which is updated didn't have
 
782
    Sets thd->transaction.stmt.modified_non_trans_table to true if table which is updated didn't have
735
783
    transactions.
736
784
 
737
785
  RETURN VALUE
740
788
*/
741
789
 
742
790
 
743
 
int write_record(Session *session, Table *table,CopyInfo *info)
 
791
int write_record(THD *thd, Table *table,COPY_INFO *info)
744
792
{
745
793
  int error;
746
 
  std::vector<unsigned char> key;
747
 
  boost::dynamic_bitset<> *save_read_set, *save_write_set;
748
 
  uint64_t prev_insert_id= table->cursor->next_insert_id;
 
794
  char *key=0;
 
795
  MY_BITMAP *save_read_set, *save_write_set;
 
796
  uint64_t prev_insert_id= table->file->next_insert_id;
749
797
  uint64_t insert_id_for_cur_row= 0;
750
 
 
 
798
  
751
799
 
752
800
  info->records++;
753
801
  save_read_set=  table->read_set;
754
802
  save_write_set= table->write_set;
755
803
 
756
 
  if (info->handle_duplicates == DUP_REPLACE || info->handle_duplicates == DUP_UPDATE)
 
804
  if (info->handle_duplicates == DUP_REPLACE ||
 
805
      info->handle_duplicates == DUP_UPDATE)
757
806
  {
758
 
    while ((error=table->cursor->insertRecord(table->getInsertRecord())))
 
807
    while ((error=table->file->ha_write_row(table->record[0])))
759
808
    {
760
 
      uint32_t key_nr;
 
809
      uint key_nr;
761
810
      /*
762
811
        If we do more than one iteration of this loop, from the second one the
763
812
        row will have an explicit value in the autoinc field, which was set at
764
813
        the first call of handler::update_auto_increment(). So we must save
765
 
        the autogenerated value to avoid session->insert_id_for_cur_row to become
 
814
        the autogenerated value to avoid thd->insert_id_for_cur_row to become
766
815
        0.
767
816
      */
768
 
      if (table->cursor->insert_id_for_cur_row > 0)
769
 
        insert_id_for_cur_row= table->cursor->insert_id_for_cur_row;
 
817
      if (table->file->insert_id_for_cur_row > 0)
 
818
        insert_id_for_cur_row= table->file->insert_id_for_cur_row;
770
819
      else
771
 
        table->cursor->insert_id_for_cur_row= insert_id_for_cur_row;
 
820
        table->file->insert_id_for_cur_row= insert_id_for_cur_row;
772
821
      bool is_duplicate_key_error;
773
 
      if (table->cursor->is_fatal_error(error, HA_CHECK_DUP))
 
822
      if (table->file->is_fatal_error(error, HA_CHECK_DUP))
774
823
        goto err;
775
 
      is_duplicate_key_error= table->cursor->is_fatal_error(error, 0);
 
824
      is_duplicate_key_error= table->file->is_fatal_error(error, 0);
776
825
      if (!is_duplicate_key_error)
777
826
      {
778
827
        /*
784
833
          goto gok_or_after_err; /* Ignoring a not fatal error, return 0 */
785
834
        goto err;
786
835
      }
787
 
      if ((int) (key_nr = table->get_dup_key(error)) < 0)
 
836
      if ((int) (key_nr = table->file->get_dup_key(error)) < 0)
788
837
      {
789
838
        error= HA_ERR_FOUND_DUPP_KEY;         /* Database can't find key */
790
839
        goto err;
798
847
      */
799
848
      if (info->handle_duplicates == DUP_REPLACE &&
800
849
          table->next_number_field &&
801
 
          key_nr == table->getShare()->next_number_index &&
 
850
          key_nr == table->s->next_number_index &&
802
851
          (insert_id_for_cur_row > 0))
803
852
        goto err;
804
 
      if (table->cursor->getEngine()->check_flag(HTON_BIT_DUPLICATE_POS))
 
853
      if (table->file->ha_table_flags() & HA_DUPLICATE_POS)
805
854
      {
806
 
        if (table->cursor->rnd_pos(table->getUpdateRecord(),table->cursor->dup_ref))
 
855
        if (table->file->rnd_pos(table->record[1],table->file->dup_ref))
807
856
          goto err;
808
857
      }
809
858
      else
810
859
      {
811
 
        if (table->cursor->extra(HA_EXTRA_FLUSH_CACHE)) /* Not needed with NISAM */
 
860
        if (table->file->extra(HA_EXTRA_FLUSH_CACHE)) /* Not needed with NISAM */
812
861
        {
813
 
          error=errno;
 
862
          error=my_errno;
814
863
          goto err;
815
864
        }
816
865
 
817
 
        if (not key.size())
 
866
        if (!key)
818
867
        {
819
 
          key.resize(table->getShare()->max_unique_length);
 
868
          if (!(key=(char*) my_safe_alloca(table->s->max_unique_length,
 
869
                                           MAX_KEY_LENGTH)))
 
870
          {
 
871
            error=ENOMEM;
 
872
            goto err;
 
873
          }
820
874
        }
821
 
        key_copy(&key[0], table->getInsertRecord(), table->key_info+key_nr, 0);
822
 
        if ((error=(table->cursor->index_read_idx_map(table->getUpdateRecord(),key_nr,
823
 
                                                    &key[0], HA_WHOLE_KEY,
 
875
        key_copy((uchar*) key,table->record[0],table->key_info+key_nr,0);
 
876
        if ((error=(table->file->index_read_idx_map(table->record[1],key_nr,
 
877
                                                    (uchar*) key, HA_WHOLE_KEY,
824
878
                                                    HA_READ_KEY_EXACT))))
825
879
          goto err;
826
880
      }
831
885
          that matches, is updated. If update causes a conflict again,
832
886
          an error is returned
833
887
        */
834
 
        assert(table->insert_values.size());
835
 
        table->storeRecordAsInsert();
836
 
        table->restoreRecord();
 
888
        assert(table->insert_values != NULL);
 
889
        store_record(table,insert_values);
 
890
        restore_record(table,record[1]);
837
891
        assert(info->update_fields->elements ==
838
892
                    info->update_values->elements);
839
 
        if (fill_record(session, *info->update_fields,
 
893
        if (fill_record(thd, *info->update_fields,
840
894
                                                 *info->update_values,
841
895
                                                 info->ignore))
842
896
          goto before_err;
843
897
 
844
 
        table->cursor->restore_auto_increment(prev_insert_id);
 
898
        table->file->restore_auto_increment(prev_insert_id);
845
899
        if (table->next_number_field)
846
 
          table->cursor->adjust_next_insert_id_after_explicit_value(
 
900
          table->file->adjust_next_insert_id_after_explicit_value(
847
901
            table->next_number_field->val_int());
848
902
        info->touched++;
849
 
 
850
 
        if (! table->records_are_comparable() || table->compare_records())
 
903
        if ((table->file->ha_table_flags() & HA_PARTIAL_COLUMN_READ &&
 
904
             !bitmap_is_subset(table->write_set, table->read_set)) ||
 
905
            table->compare_record())
851
906
        {
852
 
          if ((error=table->cursor->updateRecord(table->getUpdateRecord(),
853
 
                                                table->getInsertRecord())) &&
 
907
          if ((error=table->file->ha_update_row(table->record[1],
 
908
                                                table->record[0])) &&
854
909
              error != HA_ERR_RECORD_IS_THE_SAME)
855
910
          {
856
911
            if (info->ignore &&
857
 
                !table->cursor->is_fatal_error(error, HA_CHECK_DUP_KEY))
 
912
                !table->file->is_fatal_error(error, HA_CHECK_DUP_KEY))
858
913
            {
859
914
              goto gok_or_after_err;
860
915
            }
870
925
            like a regular UPDATE statement: it should not affect the value of a
871
926
            next SELECT LAST_INSERT_ID() or mysql_insert_id().
872
927
            Except if LAST_INSERT_ID(#) was in the INSERT query, which is
873
 
            handled separately by Session::arg_of_last_insert_id_function.
 
928
            handled separately by THD::arg_of_last_insert_id_function.
874
929
          */
875
 
          insert_id_for_cur_row= table->cursor->insert_id_for_cur_row= 0;
 
930
          insert_id_for_cur_row= table->file->insert_id_for_cur_row= 0;
876
931
          info->copied++;
877
932
        }
878
933
 
879
934
        if (table->next_number_field)
880
 
          table->cursor->adjust_next_insert_id_after_explicit_value(
 
935
          table->file->adjust_next_insert_id_after_explicit_value(
881
936
            table->next_number_field->val_int());
882
937
        info->touched++;
883
938
 
890
945
          an INSERT or DELETE(s) + INSERT; FOREIGN KEY checks in
891
946
          InnoDB do not function in the defined way if we allow MySQL
892
947
          to convert the latter operation internally to an UPDATE.
893
 
          We also should not perform this conversion if we have
 
948
          We also should not perform this conversion if we have 
894
949
          timestamp field with ON UPDATE which is different from DEFAULT.
895
950
          Another case when conversion should not be performed is when
896
951
          we have ON DELETE trigger on table so user may notice that
900
955
          ON UPDATE triggers.
901
956
        */
902
957
        if (last_uniq_key(table,key_nr) &&
903
 
            !table->cursor->referenced_by_foreign_key() &&
 
958
            !table->file->referenced_by_foreign_key() &&
904
959
            (table->timestamp_field_type == TIMESTAMP_NO_AUTO_SET ||
905
960
             table->timestamp_field_type == TIMESTAMP_AUTO_SET_ON_BOTH))
906
961
        {
907
 
          if ((error=table->cursor->updateRecord(table->getUpdateRecord(),
908
 
                                                table->getInsertRecord())) &&
 
962
          if ((error=table->file->ha_update_row(table->record[1],
 
963
                                                table->record[0])) &&
909
964
              error != HA_ERR_RECORD_IS_THE_SAME)
910
965
            goto err;
911
966
          if (error != HA_ERR_RECORD_IS_THE_SAME)
912
967
            info->deleted++;
913
968
          else
914
969
            error= 0;
915
 
          session->record_first_successful_insert_id_in_cur_stmt(table->cursor->insert_id_for_cur_row);
 
970
          thd->record_first_successful_insert_id_in_cur_stmt(table->file->insert_id_for_cur_row);
916
971
          /*
917
972
            Since we pretend that we have done insert we should call
918
973
            its after triggers.
921
976
        }
922
977
        else
923
978
        {
924
 
          if ((error=table->cursor->deleteRecord(table->getUpdateRecord())))
 
979
          if ((error=table->file->ha_delete_row(table->record[1])))
925
980
            goto err;
926
981
          info->deleted++;
927
 
          if (!table->cursor->has_transactions())
928
 
            session->transaction.stmt.markModifiedNonTransData();
 
982
          if (!table->file->has_transactions())
 
983
            thd->transaction.stmt.modified_non_trans_table= true;
929
984
          /* Let us attempt do write_row() once more */
930
985
        }
931
986
      }
932
987
    }
933
 
    session->record_first_successful_insert_id_in_cur_stmt(table->cursor->insert_id_for_cur_row);
 
988
    thd->record_first_successful_insert_id_in_cur_stmt(table->file->insert_id_for_cur_row);
934
989
    /*
935
990
      Restore column maps if they where replaced during an duplicate key
936
991
      problem.
937
992
    */
938
993
    if (table->read_set != save_read_set ||
939
994
        table->write_set != save_write_set)
940
 
      table->column_bitmaps_set(*save_read_set, *save_write_set);
 
995
      table->column_bitmaps_set(save_read_set, save_write_set);
941
996
  }
942
 
  else if ((error=table->cursor->insertRecord(table->getInsertRecord())))
 
997
  else if ((error=table->file->ha_write_row(table->record[0])))
943
998
  {
944
999
    if (!info->ignore ||
945
 
        table->cursor->is_fatal_error(error, HA_CHECK_DUP))
 
1000
        table->file->is_fatal_error(error, HA_CHECK_DUP))
946
1001
      goto err;
947
 
    table->cursor->restore_auto_increment(prev_insert_id);
 
1002
    table->file->restore_auto_increment(prev_insert_id);
948
1003
    goto gok_or_after_err;
949
1004
  }
950
1005
 
951
1006
after_n_copied_inc:
952
1007
  info->copied++;
953
 
  session->record_first_successful_insert_id_in_cur_stmt(table->cursor->insert_id_for_cur_row);
 
1008
  thd->record_first_successful_insert_id_in_cur_stmt(table->file->insert_id_for_cur_row);
954
1009
 
955
1010
gok_or_after_err:
956
 
  if (!table->cursor->has_transactions())
957
 
    session->transaction.stmt.markModifiedNonTransData();
 
1011
  if (key)
 
1012
    my_safe_afree(key,table->s->max_unique_length,MAX_KEY_LENGTH);
 
1013
  if (!table->file->has_transactions())
 
1014
    thd->transaction.stmt.modified_non_trans_table= true;
958
1015
  return(0);
959
1016
 
960
1017
err:
961
1018
  info->last_errno= error;
962
1019
  /* current_select is NULL if this is a delayed insert */
963
 
  if (session->lex->current_select)
964
 
    session->lex->current_select->no_error= 0;        // Give error
965
 
  table->print_error(error,MYF(0));
966
 
 
 
1020
  if (thd->lex->current_select)
 
1021
    thd->lex->current_select->no_error= 0;        // Give error
 
1022
  table->file->print_error(error,MYF(0));
 
1023
  
967
1024
before_err:
968
 
  table->cursor->restore_auto_increment(prev_insert_id);
969
 
  table->column_bitmaps_set(*save_read_set, *save_write_set);
970
 
  return 1;
 
1025
  table->file->restore_auto_increment(prev_insert_id);
 
1026
  if (key)
 
1027
    my_safe_afree(key, table->s->max_unique_length, MAX_KEY_LENGTH);
 
1028
  table->column_bitmaps_set(save_read_set, save_write_set);
 
1029
  return(1);
971
1030
}
972
1031
 
973
1032
 
975
1034
  Check that all fields with arn't null_fields are used
976
1035
******************************************************************************/
977
1036
 
978
 
int check_that_all_fields_are_given_values(Session *session, Table *entry,
979
 
                                           TableList *)
 
1037
int check_that_all_fields_are_given_values(THD *thd, Table *entry,
 
1038
                                           TableList *table_list)
980
1039
{
981
1040
  int err= 0;
 
1041
  MY_BITMAP *write_set= entry->write_set;
982
1042
 
983
 
  for (Field **field=entry->getFields() ; *field ; field++)
 
1043
  for (Field **field=entry->field ; *field ; field++)
984
1044
  {
985
 
    if (((*field)->isWriteSet()) == false)
986
 
    {
987
 
      /*
988
 
       * If the field doesn't have any default value
989
 
       * and there is no actual value specified in the
990
 
       * INSERT statement, throw error ER_NO_DEFAULT_FOR_FIELD.
991
 
       */
992
 
      if (((*field)->flags & NO_DEFAULT_VALUE_FLAG) &&
 
1045
    if (!bitmap_is_set(write_set, (*field)->field_index) &&
 
1046
        ((*field)->flags & NO_DEFAULT_VALUE_FLAG) &&
993
1047
        ((*field)->real_type() != DRIZZLE_TYPE_ENUM))
994
 
      {
995
 
        my_error(ER_NO_DEFAULT_FOR_FIELD, MYF(0), (*field)->field_name);
996
 
        err= 1;
997
 
      }
998
 
    }
999
 
    else
1000
1048
    {
1001
 
      /*
1002
 
       * However, if an actual NULL value was specified
1003
 
       * for the field and the field is a NOT NULL field, 
1004
 
       * throw ER_BAD_NULL_ERROR.
1005
 
       *
1006
 
       * Per the SQL standard, inserting NULL into a NOT NULL
1007
 
       * field requires an error to be thrown.
1008
 
       */
1009
 
      if (((*field)->flags & NOT_NULL_FLAG) &&
1010
 
          (*field)->is_null())
1011
 
      {
1012
 
        my_error(ER_BAD_NULL_ERROR, MYF(0), (*field)->field_name);
1013
 
        err= 1;
1014
 
      }
 
1049
      bool view= false;
 
1050
      if (table_list)
 
1051
      {
 
1052
        table_list= table_list->top_table();
 
1053
        view= test(0);
 
1054
      }
 
1055
      {
 
1056
        push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
1057
                            ER_NO_DEFAULT_FOR_FIELD,
 
1058
                            ER(ER_NO_DEFAULT_FOR_FIELD),
 
1059
                            (*field)->field_name);
 
1060
      }
 
1061
      err= 1;
1015
1062
    }
1016
1063
  }
1017
 
  return session->abort_on_warning ? err : 0;
 
1064
  return thd->abort_on_warning ? err : 0;
1018
1065
}
1019
1066
 
1020
1067
/***************************************************************************
1027
1074
 
1028
1075
  SYNOPSIS
1029
1076
    mysql_insert_select_prepare()
1030
 
    session         thread handler
 
1077
    thd         thread handler
1031
1078
 
1032
1079
  RETURN
1033
1080
    false OK
1034
1081
    true  Error
1035
1082
*/
1036
1083
 
1037
 
bool mysql_insert_select_prepare(Session *session)
 
1084
bool mysql_insert_select_prepare(THD *thd)
1038
1085
{
1039
 
  LEX *lex= session->lex;
1040
 
  Select_Lex *select_lex= &lex->select_lex;
1041
 
 
1042
 
  /*
1043
 
    Select_Lex do not belong to INSERT statement, so we can't add WHERE
 
1086
  LEX *lex= thd->lex;
 
1087
  SELECT_LEX *select_lex= &lex->select_lex;
 
1088
  
 
1089
 
 
1090
  /*
 
1091
    Statement-based replication of INSERT ... SELECT ... LIMIT is not safe
 
1092
    as order of rows is not defined, so in mixed mode we go to row-based.
 
1093
 
 
1094
    Note that we may consider a statement as safe if ORDER BY primary_key
 
1095
    is present or we SELECT a constant. However it may confuse users to
 
1096
    see very similiar statements replicated differently.
 
1097
  */
 
1098
  if (lex->current_select->select_limit)
 
1099
  {
 
1100
    lex->set_stmt_unsafe();
 
1101
    thd->set_current_stmt_binlog_row_based_if_mixed();
 
1102
  }
 
1103
  /*
 
1104
    SELECT_LEX do not belong to INSERT statement, so we can't add WHERE
1044
1105
    clause if table is VIEW
1045
1106
  */
1046
 
 
1047
 
  if (mysql_prepare_insert(session, lex->query_tables,
 
1107
  
 
1108
  if (mysql_prepare_insert(thd, lex->query_tables,
1048
1109
                           lex->query_tables->table, lex->field_list, 0,
1049
1110
                           lex->update_list, lex->value_list,
1050
1111
                           lex->duplicates,
1068
1129
                             List<Item> *update_fields,
1069
1130
                             List<Item> *update_values,
1070
1131
                             enum_duplicates duplic,
1071
 
                             bool ignore_check_option_errors) :
1072
 
  table_list(table_list_par), table(table_par), fields(fields_par),
1073
 
  autoinc_value_of_last_inserted_row(0),
1074
 
  insert_into_view(table_list_par && 0 != 0)
 
1132
                             bool ignore_check_option_errors)
 
1133
  :table_list(table_list_par), table(table_par), fields(fields_par),
 
1134
   autoinc_value_of_last_inserted_row(0),
 
1135
   insert_into_view(table_list_par && 0 != 0)
1075
1136
{
 
1137
  memset(&info, 0, sizeof(info));
1076
1138
  info.handle_duplicates= duplic;
1077
1139
  info.ignore= ignore_check_option_errors;
1078
1140
  info.update_fields= update_fields;
1081
1143
 
1082
1144
 
1083
1145
int
1084
 
select_insert::prepare(List<Item> &values, Select_Lex_Unit *u)
 
1146
select_insert::prepare(List<Item> &values, SELECT_LEX_UNIT *u)
1085
1147
{
1086
 
  LEX *lex= session->lex;
 
1148
  LEX *lex= thd->lex;
1087
1149
  int res;
1088
1150
  table_map map= 0;
1089
 
  Select_Lex *lex_current_select_save= lex->current_select;
1090
 
 
 
1151
  SELECT_LEX *lex_current_select_save= lex->current_select;
 
1152
  
1091
1153
 
1092
1154
  unit= u;
1093
1155
 
1097
1159
    we are fixing fields from insert list.
1098
1160
  */
1099
1161
  lex->current_select= &lex->select_lex;
1100
 
  res= check_insert_fields(session, table_list, *fields, values,
 
1162
  res= check_insert_fields(thd, table_list, *fields, values,
1101
1163
                           !insert_into_view, &map) ||
1102
 
       setup_fields(session, 0, values, MARK_COLUMNS_READ, 0, 0);
 
1164
       setup_fields(thd, 0, values, MARK_COLUMNS_READ, 0, 0);
1103
1165
 
1104
1166
  if (!res && fields->elements)
1105
1167
  {
1106
 
    bool saved_abort_on_warning= session->abort_on_warning;
1107
 
    session->abort_on_warning= !info.ignore;
1108
 
    res= check_that_all_fields_are_given_values(session, table_list->table,
 
1168
    bool saved_abort_on_warning= thd->abort_on_warning;
 
1169
    thd->abort_on_warning= !info.ignore;
 
1170
    res= check_that_all_fields_are_given_values(thd, table_list->table, 
1109
1171
                                                table_list);
1110
 
    session->abort_on_warning= saved_abort_on_warning;
 
1172
    thd->abort_on_warning= saved_abort_on_warning;
1111
1173
  }
1112
1174
 
1113
1175
  if (info.handle_duplicates == DUP_UPDATE && !res)
1122
1184
    table_list->next_local= 0;
1123
1185
    context->resolve_in_table_list_only(table_list);
1124
1186
 
1125
 
    res= res || check_update_fields(session, context->table_list,
 
1187
    res= res || check_update_fields(thd, context->table_list,
1126
1188
                                    *info.update_fields, &map);
1127
1189
    /*
1128
 
      When we are not using GROUP BY and there are no ungrouped aggregate functions
 
1190
      When we are not using GROUP BY and there are no ungrouped aggregate functions 
1129
1191
      we can refer to other tables in the ON DUPLICATE KEY part.
1130
1192
      We use next_name_resolution_table descructively, so check it first (views?)
1131
1193
    */
1136
1198
        We must make a single context out of the two separate name resolution contexts :
1137
1199
        the INSERT table and the tables in the SELECT part of INSERT ... SELECT.
1138
1200
        To do that we must concatenate the two lists
1139
 
      */
1140
 
      table_list->next_name_resolution_table=
 
1201
      */  
 
1202
      table_list->next_name_resolution_table= 
1141
1203
        ctx_state.get_first_name_resolution_table();
1142
1204
 
1143
 
    res= res || setup_fields(session, 0, *info.update_values,
 
1205
    res= res || setup_fields(thd, 0, *info.update_values,
1144
1206
                             MARK_COLUMNS_READ, 0, 0);
1145
1207
    if (!res)
1146
1208
    {
1156
1218
      while ((item= li++))
1157
1219
      {
1158
1220
        item->transform(&Item::update_value_transformer,
1159
 
                        (unsigned char*)lex->current_select);
 
1221
                        (uchar*)lex->current_select);
1160
1222
      }
1161
1223
    }
1162
1224
 
1177
1239
    Is table which we are changing used somewhere in other parts of
1178
1240
    query
1179
1241
  */
1180
 
  if (unique_table(table_list, table_list->next_global))
 
1242
  if (unique_table(thd, table_list, table_list->next_global, 0))
1181
1243
  {
1182
1244
    /* Using same table for INSERT and SELECT */
1183
1245
    lex->current_select->options|= OPTION_BUFFER_RESULT;
1186
1248
  else if (!(lex->current_select->options & OPTION_BUFFER_RESULT))
1187
1249
  {
1188
1250
    /*
1189
 
      We must not yet prepare the result table if it is the same as one of the
1190
 
      source tables (INSERT SELECT). The preparation may disable
 
1251
      We must not yet prepare the result table if it is the same as one of the 
 
1252
      source tables (INSERT SELECT). The preparation may disable 
1191
1253
      indexes on the result table, which may be used during the select, if it
1192
1254
      is the same table (Bug #6034). Do the preparation after the select phase
1193
1255
      in select_insert::prepare2().
1194
1256
      We won't start bulk inserts at all if this statement uses functions or
1195
1257
      should invoke triggers since they may access to the same table too.
1196
1258
    */
1197
 
    table->cursor->ha_start_bulk_insert((ha_rows) 0);
 
1259
    table->file->ha_start_bulk_insert((ha_rows) 0);
1198
1260
  }
1199
 
  table->restoreRecordAsDefault();              // Get empty record
 
1261
  restore_record(table,s->default_values);              // Get empty record
1200
1262
  table->next_number_field=table->found_next_number_field;
1201
1263
 
1202
 
  session->cuted_fields=0;
 
1264
#ifdef HAVE_REPLICATION
 
1265
  if (thd->slave_thread &&
 
1266
      (info.handle_duplicates == DUP_UPDATE) &&
 
1267
      (table->next_number_field != NULL) &&
 
1268
      rpl_master_has_bug(&active_mi->rli, 24432))
 
1269
    return(1);
 
1270
#endif
 
1271
 
 
1272
  thd->cuted_fields=0;
1203
1273
  if (info.ignore || info.handle_duplicates != DUP_ERROR)
1204
 
    table->cursor->extra(HA_EXTRA_IGNORE_DUP_KEY);
 
1274
    table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
1205
1275
  if (info.handle_duplicates == DUP_REPLACE)
1206
 
    table->cursor->extra(HA_EXTRA_WRITE_CAN_REPLACE);
 
1276
    table->file->extra(HA_EXTRA_WRITE_CAN_REPLACE);
1207
1277
  if (info.handle_duplicates == DUP_UPDATE)
1208
 
    table->cursor->extra(HA_EXTRA_INSERT_WITH_UPDATE);
1209
 
  session->abort_on_warning= !info.ignore;
 
1278
    table->file->extra(HA_EXTRA_INSERT_WITH_UPDATE);
 
1279
  thd->abort_on_warning= !info.ignore;
1210
1280
  table->mark_columns_needed_for_insert();
1211
1281
 
1212
1282
 
1225
1295
    If the result table is the same as one of the source tables (INSERT SELECT),
1226
1296
    the result table is not finally prepared at the join prepair phase.
1227
1297
    Do the final preparation now.
1228
 
 
 
1298
                       
1229
1299
  RETURN
1230
1300
    0   OK
1231
1301
*/
1232
1302
 
1233
1303
int select_insert::prepare2(void)
1234
1304
{
1235
 
 
1236
 
  if (session->lex->current_select->options & OPTION_BUFFER_RESULT)
1237
 
    table->cursor->ha_start_bulk_insert((ha_rows) 0);
 
1305
  
 
1306
  if (thd->lex->current_select->options & OPTION_BUFFER_RESULT)
 
1307
    table->file->ha_start_bulk_insert((ha_rows) 0);
1238
1308
  return(0);
1239
1309
}
1240
1310
 
1247
1317
 
1248
1318
select_insert::~select_insert()
1249
1319
{
1250
 
 
 
1320
  
1251
1321
  if (table)
1252
1322
  {
1253
1323
    table->next_number_field=0;
1254
1324
    table->auto_increment_field_not_null= false;
1255
 
    table->cursor->ha_reset();
 
1325
    table->file->ha_reset();
1256
1326
  }
1257
 
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
1258
 
  session->abort_on_warning= 0;
 
1327
  thd->count_cuted_fields= CHECK_FIELD_IGNORE;
 
1328
  thd->abort_on_warning= 0;
1259
1329
  return;
1260
1330
}
1261
1331
 
1262
1332
 
1263
1333
bool select_insert::send_data(List<Item> &values)
1264
1334
{
1265
 
 
 
1335
  
1266
1336
  bool error=0;
1267
1337
 
1268
1338
  if (unit->offset_limit_cnt)
1271
1341
    return(0);
1272
1342
  }
1273
1343
 
1274
 
  session->count_cuted_fields= CHECK_FIELD_WARN;        // Calculate cuted fields
 
1344
  thd->count_cuted_fields= CHECK_FIELD_WARN;    // Calculate cuted fields
1275
1345
  store_values(values);
1276
 
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
1277
 
  if (session->is_error())
 
1346
  thd->count_cuted_fields= CHECK_FIELD_IGNORE;
 
1347
  if (thd->is_error())
1278
1348
    return(1);
1279
1349
 
1280
 
  // Release latches in case bulk insert takes a long time
1281
 
  plugin::TransactionalStorageEngine::releaseTemporaryLatches(session);
1282
 
 
1283
 
  error= write_record(session, table, &info);
1284
 
  table->auto_increment_field_not_null= false;
1285
 
 
 
1350
  error= write_record(thd, table, &info);
 
1351
    
1286
1352
  if (!error)
1287
1353
  {
1288
1354
    if (info.handle_duplicates == DUP_UPDATE)
1290
1356
      /*
1291
1357
        Restore fields of the record since it is possible that they were
1292
1358
        changed by ON DUPLICATE KEY UPDATE clause.
1293
 
 
 
1359
    
1294
1360
        If triggers exist then whey can modify some fields which were not
1295
1361
        originally touched by INSERT ... SELECT, so we have to restore
1296
1362
        their original values for the next row.
1297
1363
      */
1298
 
      table->restoreRecordAsDefault();
 
1364
      restore_record(table, s->default_values);
1299
1365
    }
1300
1366
    if (table->next_number_field)
1301
1367
    {
1303
1369
        If no value has been autogenerated so far, we need to remember the
1304
1370
        value we just saw, we may need to send it to client in the end.
1305
1371
      */
1306
 
      if (session->first_successful_insert_id_in_cur_stmt == 0) // optimization
1307
 
        autoinc_value_of_last_inserted_row=
 
1372
      if (thd->first_successful_insert_id_in_cur_stmt == 0) // optimization
 
1373
        autoinc_value_of_last_inserted_row= 
1308
1374
          table->next_number_field->val_int();
1309
1375
      /*
1310
1376
        Clear auto-increment field for the next record, if triggers are used
1320
1386
void select_insert::store_values(List<Item> &values)
1321
1387
{
1322
1388
  if (fields->elements)
1323
 
    fill_record(session, *fields, values, true);
 
1389
    fill_record(thd, *fields, values, 1);
1324
1390
  else
1325
 
    fill_record(session, table->getFields(), values, true);
 
1391
    fill_record(thd, table->field, values, 1);
1326
1392
}
1327
1393
 
1328
 
void select_insert::send_error(uint32_t errcode,const char *err)
 
1394
void select_insert::send_error(uint errcode,const char *err)
1329
1395
{
1330
 
 
 
1396
  
1331
1397
 
1332
1398
  my_message(errcode, err, MYF(0));
1333
1399
 
1338
1404
bool select_insert::send_eof()
1339
1405
{
1340
1406
  int error;
1341
 
  bool const trans_table= table->cursor->has_transactions();
 
1407
  bool const trans_table= table->file->has_transactions();
1342
1408
  uint64_t id;
1343
1409
  bool changed;
1344
 
 
1345
 
  error= table->cursor->ha_end_bulk_insert();
1346
 
  table->cursor->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
1347
 
  table->cursor->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
 
1410
  THD::killed_state killed_status= thd->killed;
 
1411
  
 
1412
  error= table->file->ha_end_bulk_insert();
 
1413
  table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
 
1414
  table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
1348
1415
 
1349
1416
  if ((changed= (info.copied || info.deleted || info.updated)))
1350
1417
  {
1351
1418
    /*
1352
1419
      We must invalidate the table in the query cache before binlog writing
1353
 
      and autocommitOrRollback.
 
1420
      and ha_autocommit_or_rollback.
1354
1421
    */
1355
 
    if (session->transaction.stmt.hasModifiedNonTransData())
1356
 
      session->transaction.all.markModifiedNonTransData();
 
1422
    if (thd->transaction.stmt.modified_non_trans_table)
 
1423
      thd->transaction.all.modified_non_trans_table= true;
1357
1424
  }
1358
 
  assert(trans_table || !changed ||
1359
 
              session->transaction.stmt.hasModifiedNonTransData());
 
1425
  assert(trans_table || !changed || 
 
1426
              thd->transaction.stmt.modified_non_trans_table);
1360
1427
 
1361
 
  table->cursor->ha_release_auto_increment();
 
1428
  /*
 
1429
    Write to binlog before commiting transaction.  No statement will
 
1430
    be written by the binlog_query() below in RBR mode.  All the
 
1431
    events are in the transaction cache and will be written when
 
1432
    ha_autocommit_or_rollback() is issued below.
 
1433
  */
 
1434
  if (mysql_bin_log.is_open())
 
1435
  {
 
1436
    if (!error)
 
1437
      thd->clear_error();
 
1438
    thd->binlog_query(THD::ROW_QUERY_TYPE,
 
1439
                      thd->query, thd->query_length,
 
1440
                      trans_table, false, killed_status);
 
1441
  }
 
1442
  table->file->ha_release_auto_increment();
1362
1443
 
1363
1444
  if (error)
1364
1445
  {
1365
 
    table->print_error(error,MYF(0));
1366
 
    DRIZZLE_INSERT_SELECT_DONE(error, 0);
1367
 
    return 1;
 
1446
    table->file->print_error(error,MYF(0));
 
1447
    return(1);
1368
1448
  }
1369
1449
  char buff[160];
1370
1450
  if (info.ignore)
1371
 
    snprintf(buff, sizeof(buff), ER(ER_INSERT_INFO), (ulong) info.records,
1372
 
            (ulong) (info.records - info.copied), (ulong) session->cuted_fields);
 
1451
    sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
 
1452
            (ulong) (info.records - info.copied), (ulong) thd->cuted_fields);
1373
1453
  else
1374
 
    snprintf(buff, sizeof(buff), ER(ER_INSERT_INFO), (ulong) info.records,
1375
 
            (ulong) (info.deleted+info.updated), (ulong) session->cuted_fields);
1376
 
  session->row_count_func= info.copied + info.deleted + info.updated;
 
1454
    sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
 
1455
            (ulong) (info.deleted+info.updated), (ulong) thd->cuted_fields);
 
1456
  thd->row_count_func= info.copied + info.deleted +
 
1457
                       ((thd->client_capabilities & CLIENT_FOUND_ROWS) ?
 
1458
                        info.touched : info.updated);
1377
1459
 
1378
 
  id= (session->first_successful_insert_id_in_cur_stmt > 0) ?
1379
 
    session->first_successful_insert_id_in_cur_stmt :
1380
 
    (session->arg_of_last_insert_id_function ?
1381
 
     session->first_successful_insert_id_in_prev_stmt :
 
1460
  id= (thd->first_successful_insert_id_in_cur_stmt > 0) ?
 
1461
    thd->first_successful_insert_id_in_cur_stmt :
 
1462
    (thd->arg_of_last_insert_id_function ?
 
1463
     thd->first_successful_insert_id_in_prev_stmt :
1382
1464
     (info.copied ? autoinc_value_of_last_inserted_row : 0));
1383
 
  session->my_ok((ulong) session->row_count_func,
1384
 
                 info.copied + info.deleted + info.touched, id, buff);
1385
 
  session->status_var.inserted_row_count+= session->row_count_func; 
1386
 
  DRIZZLE_INSERT_SELECT_DONE(0, session->row_count_func);
1387
 
  return 0;
 
1465
  ::my_ok(thd, (ulong) thd->row_count_func, id, buff);
 
1466
  return(0);
1388
1467
}
1389
1468
 
1390
1469
void select_insert::abort() {
1391
1470
 
1392
 
 
 
1471
  
1393
1472
  /*
1394
1473
    If the creation of the table failed (due to a syntax error, for
1395
1474
    example), no table will have been opened and therefore 'table'
1400
1479
  {
1401
1480
    bool changed, transactional_table;
1402
1481
 
1403
 
    table->cursor->ha_end_bulk_insert();
 
1482
    table->file->ha_end_bulk_insert();
1404
1483
 
1405
1484
    /*
1406
1485
      If at least one row has been inserted/modified and will stay in
1417
1496
      zero, so no check for that is made.
1418
1497
    */
1419
1498
    changed= (info.copied || info.deleted || info.updated);
1420
 
    transactional_table= table->cursor->has_transactions();
 
1499
    transactional_table= table->file->has_transactions();
 
1500
    if (thd->transaction.stmt.modified_non_trans_table)
 
1501
    {
 
1502
        if (mysql_bin_log.is_open())
 
1503
          thd->binlog_query(THD::ROW_QUERY_TYPE, thd->query, thd->query_length,
 
1504
                            transactional_table, false);
 
1505
        if (!thd->current_stmt_binlog_row_based && !can_rollback_data())
 
1506
          thd->transaction.all.modified_non_trans_table= true;
 
1507
    }
1421
1508
    assert(transactional_table || !changed ||
1422
 
                session->transaction.stmt.hasModifiedNonTransData());
1423
 
    table->cursor->ha_release_auto_increment();
1424
 
  }
1425
 
 
1426
 
  if (DRIZZLE_INSERT_SELECT_DONE_ENABLED())
1427
 
  {
1428
 
    DRIZZLE_INSERT_SELECT_DONE(0, info.copied + info.deleted + info.updated);
 
1509
                thd->transaction.stmt.modified_non_trans_table);
 
1510
    table->file->ha_release_auto_increment();
1429
1511
  }
1430
1512
 
1431
1513
  return;
1442
1524
 
1443
1525
  SYNOPSIS
1444
1526
    create_table_from_items()
1445
 
      session          in     Thread object
 
1527
      thd          in     Thread object
1446
1528
      create_info  in     Create information (like MAX_ROWS, ENGINE or
1447
1529
                          temporary table flag)
1448
1530
      create_table in     Pointer to TableList object providing database
1452
1534
      items        in     List of items which should be used to produce rest
1453
1535
                          of fields for the table (corresponding fields will
1454
1536
                          be added to the end of alter_info->create_list)
1455
 
      lock         out    Pointer to the DrizzleLock object for table created
 
1537
      lock         out    Pointer to the DRIZZLE_LOCK object for table created
1456
1538
                          (or open temporary table) will be returned in this
1457
1539
                          parameter. Since this table is not included in
1458
 
                          Session::lock caller is responsible for explicitly
 
1540
                          THD::lock caller is responsible for explicitly
1459
1541
                          unlocking this table.
1460
1542
      hooks
1461
1543
 
1462
1544
  NOTES
1463
1545
    This function behaves differently for base and temporary tables:
1464
1546
    - For base table we assume that either table exists and was pre-opened
1465
 
      and locked at openTablesLock() stage (and in this case we just
 
1547
      and locked at open_and_lock_tables() stage (and in this case we just
1466
1548
      emit error or warning and return pre-opened Table object) or special
1467
1549
      placeholder was put in table cache that guarantees that this table
1468
1550
      won't be created or opened until the placeholder will be removed
1478
1560
    0         Error
1479
1561
*/
1480
1562
 
1481
 
static Table *create_table_from_items(Session *session, HA_CREATE_INFO *create_info,
 
1563
static Table *create_table_from_items(THD *thd, HA_CREATE_INFO *create_info,
1482
1564
                                      TableList *create_table,
1483
 
                                      message::Table &table_proto,
1484
 
                                      AlterInfo *alter_info,
 
1565
                                      Alter_info *alter_info,
1485
1566
                                      List<Item> *items,
1486
 
                                      bool is_if_not_exists,
1487
 
                                      DrizzleLock **lock,
1488
 
                                      TableIdentifier &identifier)
 
1567
                                      DRIZZLE_LOCK **lock,
 
1568
                                      TABLEOP_HOOKS *hooks)
1489
1569
{
1490
 
  TableShare share(message::Table::INTERNAL);
1491
 
  uint32_t select_field_count= items->elements;
 
1570
  Table tmp_table;              // Used during 'Create_field()'
 
1571
  TABLE_SHARE share;
 
1572
  Table *table= 0;
 
1573
  uint select_field_count= items->elements;
1492
1574
  /* Add selected items to field list */
1493
1575
  List_iterator_fast<Item> it(*items);
1494
1576
  Item *item;
1495
1577
  Field *tmp_field;
1496
1578
  bool not_used;
1497
1579
 
1498
 
  if (not (identifier.isTmp()) && create_table->table->db_stat)
 
1580
  if (!(create_info->options & HA_LEX_CREATE_TMP_TABLE) &&
 
1581
      create_table->table->db_stat)
1499
1582
  {
1500
 
    /* Table already exists and was open at openTablesLock() stage. */
1501
 
    if (is_if_not_exists)
 
1583
    /* Table already exists and was open at open_and_lock_tables() stage. */
 
1584
    if (create_info->options & HA_LEX_CREATE_IF_NOT_EXISTS)
1502
1585
    {
1503
1586
      create_info->table_existed= 1;            // Mark that table existed
1504
 
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
 
1587
      push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1505
1588
                          ER_TABLE_EXISTS_ERROR, ER(ER_TABLE_EXISTS_ERROR),
1506
 
                          create_table->getTableName());
1507
 
      return create_table->table;
 
1589
                          create_table->table_name);
 
1590
      return(create_table->table);
1508
1591
    }
1509
1592
 
1510
 
    my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_table->getTableName());
1511
 
    return NULL;
 
1593
    my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_table->table_name);
 
1594
    return(0);
1512
1595
  }
1513
1596
 
 
1597
  tmp_table.alias= 0;
 
1598
  tmp_table.timestamp_field= 0;
 
1599
  tmp_table.s= &share;
 
1600
  init_tmp_table_share(thd, &share, "", 0, "", "");
 
1601
 
 
1602
  tmp_table.s->db_create_options=0;
 
1603
  tmp_table.s->blob_ptr_size= portable_sizeof_char_ptr;
 
1604
  tmp_table.s->db_low_byte_first= 
 
1605
        test(create_info->db_type == myisam_hton ||
 
1606
             create_info->db_type == heap_hton);
 
1607
  tmp_table.null_row= false;
 
1608
  tmp_table.maybe_null= false;
 
1609
 
 
1610
  while ((item=it++))
1514
1611
  {
1515
 
    table::Shell tmp_table(share);              // Used during 'CreateField()'
1516
 
    tmp_table.timestamp_field= 0;
1517
 
 
1518
 
    tmp_table.getMutableShare()->db_create_options= 0;
1519
 
    tmp_table.getMutableShare()->blob_ptr_size= portable_sizeof_char_ptr;
1520
 
 
1521
 
    if (not table_proto.engine().name().compare("MyISAM"))
1522
 
      tmp_table.getMutableShare()->db_low_byte_first= true;
1523
 
    else if (not table_proto.engine().name().compare("MEMORY"))
1524
 
      tmp_table.getMutableShare()->db_low_byte_first= true;
1525
 
 
1526
 
    tmp_table.null_row= false;
1527
 
    tmp_table.maybe_null= false;
1528
 
 
1529
 
    tmp_table.in_use= session;
1530
 
 
1531
 
    while ((item=it++))
1532
 
    {
1533
 
      CreateField *cr_field;
1534
 
      Field *field, *def_field;
1535
 
      if (item->type() == Item::FUNC_ITEM)
1536
 
      {
1537
 
        if (item->result_type() != STRING_RESULT)
1538
 
        {
1539
 
          field= item->tmp_table_field(&tmp_table);
1540
 
        }
1541
 
        else
1542
 
        {
1543
 
          field= item->tmp_table_field_from_field_type(&tmp_table, 0);
1544
 
        }
1545
 
      }
 
1612
    Create_field *cr_field;
 
1613
    Field *field, *def_field;
 
1614
    if (item->type() == Item::FUNC_ITEM)
 
1615
      if (item->result_type() != STRING_RESULT)
 
1616
        field= item->tmp_table_field(&tmp_table);
1546
1617
      else
1547
 
      {
1548
 
        field= create_tmp_field(session, &tmp_table, item, item->type(),
1549
 
                                (Item ***) 0, &tmp_field, &def_field, false,
1550
 
                                false, false, 0);
1551
 
      }
1552
 
 
1553
 
      if (!field ||
1554
 
          !(cr_field=new CreateField(field,(item->type() == Item::FIELD_ITEM ?
1555
 
                                            ((Item_field *)item)->field :
1556
 
                                            (Field*) 0))))
1557
 
      {
1558
 
        return NULL;
1559
 
      }
1560
 
 
1561
 
      if (item->maybe_null)
1562
 
      {
1563
 
        cr_field->flags &= ~NOT_NULL_FLAG;
1564
 
      }
1565
 
 
1566
 
      alter_info->create_list.push_back(cr_field);
1567
 
    }
 
1618
        field= item->tmp_table_field_from_field_type(&tmp_table, 0);
 
1619
    else
 
1620
      field= create_tmp_field(thd, &tmp_table, item, item->type(),
 
1621
                              (Item ***) 0, &tmp_field, &def_field, 0, 0, 0, 0,
 
1622
                              0);
 
1623
    if (!field ||
 
1624
        !(cr_field=new Create_field(field,(item->type() == Item::FIELD_ITEM ?
 
1625
                                           ((Item_field *)item)->field :
 
1626
                                           (Field*) 0))))
 
1627
      return(0);
 
1628
    if (item->maybe_null)
 
1629
      cr_field->flags &= ~NOT_NULL_FLAG;
 
1630
    alter_info->create_list.push_back(cr_field);
1568
1631
  }
1569
1632
 
1570
1633
  /*
1573
1636
    Note that we either creating (or opening existing) temporary table or
1574
1637
    creating base table on which name we have exclusive lock. So code below
1575
1638
    should not cause deadlocks or races.
 
1639
 
 
1640
    We don't log the statement, it will be logged later.
 
1641
 
 
1642
    If this is a HEAP table, the automatic DELETE FROM which is written to the
 
1643
    binlog when a HEAP table is opened for the first time since startup, must
 
1644
    not be written: 1) it would be wrong (imagine we're in CREATE SELECT: we
 
1645
    don't want to delete from it) 2) it would be written before the CREATE
 
1646
    Table, which is a wrong order. So we keep binary logging disabled when we
 
1647
    open_table().
1576
1648
  */
1577
 
  Table *table= 0;
1578
1649
  {
1579
 
    if (not mysql_create_table_no_lock(session,
1580
 
                                       identifier,
1581
 
                                       create_info,
1582
 
                                       table_proto,
1583
 
                                       alter_info,
1584
 
                                       false,
1585
 
                                       select_field_count,
1586
 
                                       is_if_not_exists))
 
1650
    tmp_disable_binlog(thd);
 
1651
    if (!mysql_create_table_no_lock(thd, create_table->db,
 
1652
                                    create_table->table_name,
 
1653
                                    create_info, alter_info, 0,
 
1654
                                    select_field_count))
1587
1655
    {
1588
 
      if (create_info->table_existed && not identifier.isTmp())
 
1656
      if (create_info->table_existed &&
 
1657
          !(create_info->options & HA_LEX_CREATE_TMP_TABLE))
1589
1658
      {
1590
1659
        /*
1591
1660
          This means that someone created table underneath server
1592
1661
          or it was created via different mysqld front-end to the
1593
1662
          cluster. We don't have much options but throw an error.
1594
1663
        */
1595
 
        my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_table->getTableName());
1596
 
        return NULL;
 
1664
        my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_table->table_name);
 
1665
        return(0);
1597
1666
      }
1598
1667
 
1599
 
      if (not identifier.isTmp())
 
1668
      if (!(create_info->options & HA_LEX_CREATE_TMP_TABLE))
1600
1669
      {
1601
 
        /* CREATE TABLE... has found that the table already exists for insert and is adapting to use it */
1602
 
        boost::mutex::scoped_lock scopedLock(table::Cache::singleton().mutex());
1603
 
 
1604
 
        if (create_table->table)
 
1670
        VOID(pthread_mutex_lock(&LOCK_open));
 
1671
        if (reopen_name_locked_table(thd, create_table, false))
1605
1672
        {
1606
 
          table::Concurrent *concurrent_table= static_cast<table::Concurrent *>(create_table->table);
1607
 
 
1608
 
          if (concurrent_table->reopen_name_locked_table(create_table, session))
1609
 
          {
1610
 
            plugin::StorageEngine::dropTable(*session, identifier);
1611
 
          }
1612
 
          else
1613
 
          {
1614
 
            table= create_table->table;
1615
 
          }
 
1673
          quick_rm_table(create_info->db_type, create_table->db,
 
1674
                         table_case_name(create_info, create_table->table_name),
 
1675
                         0);
1616
1676
        }
1617
1677
        else
1618
 
        {
1619
 
          plugin::StorageEngine::dropTable(*session, identifier);
1620
 
        }
 
1678
          table= create_table->table;
 
1679
        VOID(pthread_mutex_unlock(&LOCK_open));
1621
1680
      }
1622
1681
      else
1623
1682
      {
1624
 
        if (not (table= session->openTable(create_table, (bool*) 0,
1625
 
                                           DRIZZLE_OPEN_TEMPORARY_ONLY)) &&
1626
 
            not create_info->table_existed)
 
1683
        if (!(table= open_table(thd, create_table, (bool*) 0,
 
1684
                                DRIZZLE_OPEN_TEMPORARY_ONLY)) &&
 
1685
            !create_info->table_existed)
1627
1686
        {
1628
1687
          /*
1629
1688
            This shouldn't happen as creation of temporary table should make
1630
1689
            it preparable for open. But let us do close_temporary_table() here
1631
1690
            just in case.
1632
1691
          */
1633
 
          session->drop_temporary_table(identifier);
 
1692
          drop_temporary_table(thd, create_table);
1634
1693
        }
1635
1694
      }
1636
1695
    }
1637
 
    if (not table)                                   // open failed
1638
 
      return NULL;
 
1696
    reenable_binlog(thd);
 
1697
    if (!table)                                   // open failed
 
1698
      return(0);
1639
1699
  }
1640
1700
 
1641
1701
  table->reginfo.lock_type=TL_WRITE;
1642
 
  if (! ((*lock)= session->lockTables(&table, 1, DRIZZLE_LOCK_IGNORE_FLUSH, &not_used)))
 
1702
  hooks->prelock(&table, 1);                    // Call prelock hooks
 
1703
  if (! ((*lock)= mysql_lock_tables(thd, &table, 1,
 
1704
                                    DRIZZLE_LOCK_IGNORE_FLUSH, &not_used)) ||
 
1705
        hooks->postlock(&table, 1))
1643
1706
  {
1644
1707
    if (*lock)
1645
1708
    {
1646
 
      session->unlockTables(*lock);
 
1709
      mysql_unlock_tables(thd, *lock);
1647
1710
      *lock= 0;
1648
1711
    }
1649
1712
 
1650
 
    if (not create_info->table_existed)
1651
 
      session->drop_open_table(table, identifier);
1652
 
    return NULL;
 
1713
    if (!create_info->table_existed)
 
1714
      drop_open_table(thd, table, create_table->db, create_table->table_name);
 
1715
    return(0);
1653
1716
  }
1654
 
 
1655
 
  return table;
 
1717
  return(table);
1656
1718
}
1657
1719
 
1658
1720
 
1659
1721
int
1660
 
select_create::prepare(List<Item> &values, Select_Lex_Unit *u)
 
1722
select_create::prepare(List<Item> &values, SELECT_LEX_UNIT *u)
1661
1723
{
1662
 
  DrizzleLock *extra_lock= NULL;
 
1724
  DRIZZLE_LOCK *extra_lock= NULL;
 
1725
  
 
1726
 
 
1727
  TABLEOP_HOOKS *hook_ptr= NULL;
1663
1728
  /*
1664
 
    For replication, the CREATE-SELECT statement is written
1665
 
    in two pieces: the first transaction messsage contains 
1666
 
    the CREATE TABLE statement as a CreateTableStatement message
1667
 
    necessary to create the table.
1668
 
    
1669
 
    The second transaction message contains all the InsertStatement
1670
 
    and associated InsertRecords that should go into the table.
 
1729
    For row-based replication, the CREATE-SELECT statement is written
 
1730
    in two pieces: the first one contain the CREATE TABLE statement
 
1731
    necessary to create the table and the second part contain the rows
 
1732
    that should go into the table.
 
1733
 
 
1734
    For non-temporary tables, the start of the CREATE-SELECT
 
1735
    implicitly commits the previous transaction, and all events
 
1736
    forming the statement will be stored the transaction cache. At end
 
1737
    of the statement, the entire statement is committed as a
 
1738
    transaction, and all events are written to the binary log.
 
1739
 
 
1740
    On the master, the table is locked for the duration of the
 
1741
    statement, but since the CREATE part is replicated as a simple
 
1742
    statement, there is no way to lock the table for accesses on the
 
1743
    slave.  Hence, we have to hold on to the CREATE part of the
 
1744
    statement until the statement has finished.
1671
1745
   */
 
1746
  class MY_HOOKS : public TABLEOP_HOOKS {
 
1747
  public:
 
1748
    MY_HOOKS(select_create *x, TableList *create_table,
 
1749
             TableList *select_tables)
 
1750
      : ptr(x), all_tables(*create_table)
 
1751
      {
 
1752
        all_tables.next_global= select_tables;
 
1753
      }
 
1754
 
 
1755
  private:
 
1756
    virtual int do_postlock(Table **tables, uint count)
 
1757
    {
 
1758
      THD *thd= const_cast<THD*>(ptr->get_thd());
 
1759
      if (int error= decide_logging_format(thd, &all_tables))
 
1760
        return error;
 
1761
 
 
1762
      Table const *const table = *tables;
 
1763
      if (thd->current_stmt_binlog_row_based  &&
 
1764
          !table->s->tmp_table &&
 
1765
          !ptr->get_create_info()->table_existed)
 
1766
      {
 
1767
        ptr->binlog_show_create_table(tables, count);
 
1768
      }
 
1769
      return 0;
 
1770
    }
 
1771
 
 
1772
    select_create *ptr;
 
1773
    TableList all_tables;
 
1774
  };
 
1775
 
 
1776
  MY_HOOKS hooks(this, create_table, select_tables);
 
1777
  hook_ptr= &hooks;
1672
1778
 
1673
1779
  unit= u;
1674
1780
 
1675
 
  if (not (table= create_table_from_items(session, create_info, create_table,
1676
 
                                          table_proto,
1677
 
                                          alter_info, &values,
1678
 
                                          is_if_not_exists,
1679
 
                                          &extra_lock, identifier)))
 
1781
  /*
 
1782
    Start a statement transaction before the create if we are using
 
1783
    row-based replication for the statement.  If we are creating a
 
1784
    temporary table, we need to start a statement transaction.
 
1785
  */
 
1786
  if ((thd->lex->create_info.options & HA_LEX_CREATE_TMP_TABLE) == 0 &&
 
1787
      thd->current_stmt_binlog_row_based)
1680
1788
  {
 
1789
    thd->binlog_start_trans_and_stmt();
 
1790
  }
 
1791
 
 
1792
  if (!(table= create_table_from_items(thd, create_info, create_table,
 
1793
                                       alter_info, &values,
 
1794
                                       &extra_lock, hook_ptr)))
1681
1795
    return(-1);                         // abort() deletes table
1682
 
  }
1683
1796
 
1684
1797
  if (extra_lock)
1685
1798
  {
1686
1799
    assert(m_plock == NULL);
1687
1800
 
1688
 
    if (identifier.isTmp())
 
1801
    if (create_info->options & HA_LEX_CREATE_TMP_TABLE)
1689
1802
      m_plock= &m_lock;
1690
1803
    else
1691
 
      m_plock= &session->extra_lock;
 
1804
      m_plock= &thd->extra_lock;
1692
1805
 
1693
1806
    *m_plock= extra_lock;
1694
1807
  }
1695
1808
 
1696
 
  if (table->getShare()->sizeFields() < values.elements)
 
1809
  if (table->s->fields < values.elements)
1697
1810
  {
1698
1811
    my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), 1);
1699
1812
    return(-1);
1700
1813
  }
1701
1814
 
1702
1815
 /* First field to copy */
1703
 
  field= table->getFields() + table->getShare()->sizeFields() - values.elements;
 
1816
  field= table->field+table->s->fields - values.elements;
1704
1817
 
1705
1818
  /* Mark all fields that are given values */
1706
1819
  for (Field **f= field ; *f ; f++)
1707
 
  {
1708
 
    table->setWriteSet((*f)->position());
1709
 
  }
 
1820
    bitmap_set_bit(table->write_set, (*f)->field_index);
1710
1821
 
1711
1822
  /* Don't set timestamp if used */
1712
1823
  table->timestamp_field_type= TIMESTAMP_NO_AUTO_SET;
1713
1824
  table->next_number_field=table->found_next_number_field;
1714
1825
 
1715
 
  table->restoreRecordAsDefault();      // Get empty record
1716
 
  session->cuted_fields=0;
 
1826
  restore_record(table,s->default_values);      // Get empty record
 
1827
  thd->cuted_fields=0;
1717
1828
  if (info.ignore || info.handle_duplicates != DUP_ERROR)
1718
 
    table->cursor->extra(HA_EXTRA_IGNORE_DUP_KEY);
 
1829
    table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
1719
1830
  if (info.handle_duplicates == DUP_REPLACE)
1720
 
    table->cursor->extra(HA_EXTRA_WRITE_CAN_REPLACE);
 
1831
    table->file->extra(HA_EXTRA_WRITE_CAN_REPLACE);
1721
1832
  if (info.handle_duplicates == DUP_UPDATE)
1722
 
    table->cursor->extra(HA_EXTRA_INSERT_WITH_UPDATE);
1723
 
  table->cursor->ha_start_bulk_insert((ha_rows) 0);
1724
 
  session->abort_on_warning= !info.ignore;
1725
 
  if (check_that_all_fields_are_given_values(session, table, table_list))
 
1833
    table->file->extra(HA_EXTRA_INSERT_WITH_UPDATE);
 
1834
  table->file->ha_start_bulk_insert((ha_rows) 0);
 
1835
  thd->abort_on_warning= !info.ignore;
 
1836
  if (check_that_all_fields_are_given_values(thd, table, table_list))
1726
1837
    return(1);
1727
1838
  table->mark_columns_needed_for_insert();
1728
 
  table->cursor->extra(HA_EXTRA_WRITE_CACHE);
 
1839
  table->file->extra(HA_EXTRA_WRITE_CACHE);
1729
1840
  return(0);
1730
1841
}
1731
1842
 
 
1843
void
 
1844
select_create::binlog_show_create_table(Table **tables, uint count)
 
1845
{
 
1846
  /*
 
1847
    Note 1: In RBR mode, we generate a CREATE TABLE statement for the
 
1848
    created table by calling store_create_info() (behaves as SHOW
 
1849
    CREATE TABLE).  In the event of an error, nothing should be
 
1850
    written to the binary log, even if the table is non-transactional;
 
1851
    therefore we pretend that the generated CREATE TABLE statement is
 
1852
    for a transactional table.  The event will then be put in the
 
1853
    transaction cache, and any subsequent events (e.g., table-map
 
1854
    events and binrow events) will also be put there.  We can then use
 
1855
    ha_autocommit_or_rollback() to either throw away the entire
 
1856
    kaboodle of events, or write them to the binary log.
 
1857
 
 
1858
    We write the CREATE TABLE statement here and not in prepare()
 
1859
    since there potentially are sub-selects or accesses to information
 
1860
    schema that will do a close_thread_tables(), destroying the
 
1861
    statement transaction cache.
 
1862
  */
 
1863
  assert(thd->current_stmt_binlog_row_based);
 
1864
  assert(tables && *tables && count > 0);
 
1865
 
 
1866
  char buf[2048];
 
1867
  String query(buf, sizeof(buf), system_charset_info);
 
1868
  int result;
 
1869
  TableList tmp_table_list;
 
1870
 
 
1871
  memset(&tmp_table_list, 0, sizeof(tmp_table_list));
 
1872
  tmp_table_list.table = *tables;
 
1873
  query.length(0);      // Have to zero it since constructor doesn't
 
1874
 
 
1875
  result= store_create_info(thd, &tmp_table_list, &query, create_info);
 
1876
  assert(result == 0); /* store_create_info() always return 0 */
 
1877
 
 
1878
  thd->binlog_query(THD::STMT_QUERY_TYPE,
 
1879
                    query.ptr(), query.length(),
 
1880
                    /* is_trans */ true,
 
1881
                    /* suppress_use */ false);
 
1882
}
 
1883
 
1732
1884
void select_create::store_values(List<Item> &values)
1733
1885
{
1734
 
  fill_record(session, field, values, true);
 
1886
  fill_record(thd, field, values, 1);
1735
1887
}
1736
1888
 
1737
1889
 
1738
 
void select_create::send_error(uint32_t errcode,const char *err)
 
1890
void select_create::send_error(uint errcode,const char *err)
1739
1891
{
 
1892
  
 
1893
 
1740
1894
  /*
1741
1895
    This will execute any rollbacks that are necessary before writing
1742
1896
    the transcation cache.
1748
1902
    written to the binary log.
1749
1903
 
1750
1904
  */
 
1905
  tmp_disable_binlog(thd);
1751
1906
  select_insert::send_error(errcode, err);
 
1907
  reenable_binlog(thd);
1752
1908
 
1753
1909
  return;
1754
1910
}
1766
1922
      tables.  This can fail, but we should unlock the table
1767
1923
      nevertheless.
1768
1924
    */
1769
 
    if (!table->getShare()->getType())
 
1925
    if (!table->s->tmp_table)
1770
1926
    {
1771
 
      TransactionServices &transaction_services= TransactionServices::singleton();
1772
 
      transaction_services.autocommitOrRollback(session, 0);
1773
 
      (void) session->endActiveTransaction();
 
1927
      ha_autocommit_or_rollback(thd, 0);
 
1928
      end_active_trans(thd);
1774
1929
    }
1775
1930
 
1776
 
    table->cursor->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
1777
 
    table->cursor->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
 
1931
    table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
 
1932
    table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
1778
1933
    if (m_plock)
1779
1934
    {
1780
 
      session->unlockTables(*m_plock);
 
1935
      mysql_unlock_tables(thd, *m_plock);
1781
1936
      *m_plock= NULL;
1782
1937
      m_plock= NULL;
1783
1938
    }
1788
1943
 
1789
1944
void select_create::abort()
1790
1945
{
 
1946
  
 
1947
 
1791
1948
  /*
1792
1949
    In select_insert::abort() we roll back the statement, including
1793
1950
    truncating the transaction cache of the binary log. To do this, we
1803
1960
    of the table succeeded or not, since we need to reset the binary
1804
1961
    log state.
1805
1962
  */
 
1963
  tmp_disable_binlog(thd);
1806
1964
  select_insert::abort();
 
1965
  thd->transaction.stmt.modified_non_trans_table= false;
 
1966
  reenable_binlog(thd);
 
1967
 
1807
1968
 
1808
1969
  if (m_plock)
1809
1970
  {
1810
 
    session->unlockTables(*m_plock);
 
1971
    mysql_unlock_tables(thd, *m_plock);
1811
1972
    *m_plock= NULL;
1812
1973
    m_plock= NULL;
1813
1974
  }
1814
1975
 
1815
1976
  if (table)
1816
1977
  {
1817
 
    table->cursor->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
1818
 
    table->cursor->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
1819
 
    if (not create_info->table_existed)
1820
 
      session->drop_open_table(table, identifier);
1821
 
    table= NULL;                                    // Safety
 
1978
    table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
 
1979
    table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
 
1980
    if (!create_info->table_existed)
 
1981
      drop_open_table(thd, table, create_table->db, create_table->table_name);
 
1982
    table=0;                                    // Safety
1822
1983
  }
 
1984
  return;
1823
1985
}
1824
1986
 
1825
 
} /* namespace drizzled */
 
1987
 
 
1988
/*****************************************************************************
 
1989
  Instansiate templates
 
1990
*****************************************************************************/
 
1991
 
 
1992
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
 
1993
template class List_iterator_fast<List_item>;
 
1994
#endif /* HAVE_EXPLICIT_TEMPLATE_INSTANTIATION */