~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_insert.cc

Merged vcol stuff.

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