~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_insert.cc

Merged in latest plugin-slot-reorg.

Show diffs side-by-side

added added

removed removed

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