~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_insert.cc

  • Committer: Brian Aker
  • Date: 2008-12-16 07:07:50 UTC
  • Revision ID: brian@tangent.org-20081216070750-o5ykltxxqvn2awrx
Fixed errors test.

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
*/
25
25
#include <drizzled/server_includes.h>
26
26
#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) : my_malloc(size,MYF(0)))
38
 
#define my_safe_afree(ptr, size, min_length) if (size > min_length) my_free(ptr,MYF(0))
39
 
#endif
40
 
 
 
27
#include <drizzled/show.h>
 
28
#include <drizzled/replication/mi.h>
 
29
#include <drizzled/error.h>
 
30
#include <drizzled/name_resolution_context_state.h>
 
31
#include <drizzled/slave.h>
 
32
#include <drizzled/sql_parse.h>
 
33
#include <drizzled/probes.h>
 
34
#include <drizzled/tableop_hooks.h>
 
35
#include <drizzled/sql_base.h>
 
36
#include <drizzled/sql_load.h>
 
37
#include <drizzled/field/timestamp.h>
 
38
#include <drizzled/lock.h>
41
39
 
42
40
 
43
41
/*
45
43
 
46
44
  SYNOPSIS
47
45
    check_insert_fields()
48
 
    thd                         The current thread.
 
46
    session                         The current thread.
49
47
    table                       The table for insert.
50
48
    fields                      The insert fields.
51
49
    values                      The insert values.
61
59
    -1          Error
62
60
*/
63
61
 
64
 
static int check_insert_fields(THD *thd, TABLE_LIST *table_list,
 
62
static int check_insert_fields(Session *session, TableList *table_list,
65
63
                               List<Item> &fields, List<Item> &values,
66
64
                               bool check_unique,
67
65
                               table_map *map __attribute__((unused)))
68
66
{
69
 
  TABLE *table= table_list->table;
 
67
  Table *table= table_list->table;
70
68
 
71
69
  if (fields.elements == 0 && values.elements != 0)
72
70
  {
85
83
  }
86
84
  else
87
85
  {                                             // Part field list
88
 
    SELECT_LEX *select_lex= &thd->lex->select_lex;
 
86
    SELECT_LEX *select_lex= &session->lex->select_lex;
89
87
    Name_resolution_context *context= &select_lex->context;
90
88
    Name_resolution_context_state ctx_state;
91
89
    int res;
96
94
      return -1;
97
95
    }
98
96
 
99
 
    thd->dup_field= 0;
 
97
    session->dup_field= 0;
100
98
 
101
99
    /* Save the state of the current name resolution context. */
102
100
    ctx_state.save_state(context, table_list);
107
105
    */
108
106
    table_list->next_local= 0;
109
107
    context->resolve_in_table_list_only(table_list);
110
 
    res= setup_fields(thd, 0, fields, MARK_COLUMNS_WRITE, 0, 0);
 
108
    res= setup_fields(session, 0, fields, MARK_COLUMNS_WRITE, 0, 0);
111
109
 
112
110
    /* Restore the current context. */
113
111
    ctx_state.restore_state(context, table_list);
115
113
    if (res)
116
114
      return -1;
117
115
 
118
 
    if (check_unique && thd->dup_field)
 
116
    if (check_unique && session->dup_field)
119
117
    {
120
 
      my_error(ER_FIELD_SPECIFIED_TWICE, MYF(0), thd->dup_field->field_name);
 
118
      my_error(ER_FIELD_SPECIFIED_TWICE, MYF(0), session->dup_field->field_name);
121
119
      return -1;
122
120
    }
123
121
    if (table->timestamp_field) // Don't automaticly set timestamp if used
132
130
                       table->timestamp_field->field_index);
133
131
      }
134
132
    }
 
133
    /* Mark all virtual columns for write*/
 
134
    if (table->vfield)
 
135
      table->mark_virtual_columns();
135
136
  }
136
137
 
137
138
  return 0;
143
144
 
144
145
  SYNOPSIS
145
146
    check_update_fields()
146
 
    thd                         The current thread.
 
147
    session                         The current thread.
147
148
    insert_table_list           The insert table list.
148
149
    table                       The table for update.
149
150
    update_fields               The update fields.
157
158
    -1          Error
158
159
*/
159
160
 
160
 
static int check_update_fields(THD *thd, TABLE_LIST *insert_table_list,
 
161
static int check_update_fields(Session *session, TableList *insert_table_list,
161
162
                               List<Item> &update_fields,
162
163
                               table_map *map __attribute__((unused)))
163
164
{
164
 
  TABLE *table= insert_table_list->table;
 
165
  Table *table= insert_table_list->table;
165
166
  bool timestamp_mark= false;
166
167
 
167
168
  if (table->timestamp_field)
175
176
  }
176
177
 
177
178
  /* Check the fields we are going to modify */
178
 
  if (setup_fields(thd, 0, update_fields, MARK_COLUMNS_WRITE, 0, 0))
 
179
  if (setup_fields(session, 0, update_fields, MARK_COLUMNS_WRITE, 0, 0))
179
180
    return -1;
180
181
 
181
182
  if (table->timestamp_field)
202
203
*/
203
204
 
204
205
static
205
 
void upgrade_lock_type(THD *thd __attribute__((unused)),
 
206
void upgrade_lock_type(Session *session __attribute__((unused)),
206
207
                       thr_lock_type *lock_type,
207
208
                       enum_duplicates duplic,
208
209
                       bool is_multi_insert __attribute__((unused)))
224
225
  end of dispatch_command().
225
226
*/
226
227
 
227
 
bool mysql_insert(THD *thd,TABLE_LIST *table_list,
 
228
bool mysql_insert(Session *session,TableList *table_list,
228
229
                  List<Item> &fields,
229
230
                  List<List_item> &values_list,
230
231
                  List<Item> &update_fields,
236
237
  bool transactional_table, joins_freed= false;
237
238
  bool changed;
238
239
  bool was_insert_delayed= (table_list->lock_type ==  TL_WRITE_DELAYED);
239
 
  uint value_count;
 
240
  uint32_t value_count;
240
241
  ulong counter = 1;
241
242
  uint64_t id;
242
243
  COPY_INFO info;
243
 
  TABLE *table= 0;
 
244
  Table *table= 0;
244
245
  List_iterator_fast<List_item> its(values_list);
245
246
  List_item *values;
246
247
  Name_resolution_context *context;
247
248
  Name_resolution_context_state ctx_state;
248
249
  thr_lock_type lock_type;
249
250
  Item *unused_conds= 0;
250
 
  
 
251
 
251
252
 
252
253
  /*
253
254
    Upgrade lock type if the requested lock is incompatible with
254
255
    the current connection mode or table operation.
255
256
  */
256
 
  upgrade_lock_type(thd, &table_list->lock_type, duplic,
 
257
  upgrade_lock_type(session, &table_list->lock_type, duplic,
257
258
                    values_list.elements > 1);
258
259
 
259
260
  /*
262
263
    never be able to get a lock on the table. QQQ: why not
263
264
    upgrade the lock here instead?
264
265
  */
265
 
  if (table_list->lock_type == TL_WRITE_DELAYED && thd->locked_tables &&
266
 
      find_locked_table(thd, table_list->db, table_list->table_name))
 
266
  if (table_list->lock_type == TL_WRITE_DELAYED && session->locked_tables &&
 
267
      find_locked_table(session, table_list->db, table_list->table_name))
267
268
  {
268
269
    my_error(ER_DELAYED_INSERT_TABLE_LOCKED, MYF(0),
269
270
             table_list->table_name);
271
272
  }
272
273
 
273
274
  {
274
 
    if (open_and_lock_tables(thd, table_list))
 
275
    if (open_and_lock_tables(session, table_list))
275
276
      return(true);
276
277
  }
277
278
  lock_type= table_list->lock_type;
278
279
 
279
 
  thd_proc_info(thd, "init");
280
 
  thd->used_tables=0;
 
280
  session->set_proc_info("init");
 
281
  session->used_tables=0;
281
282
  values= its++;
282
283
  value_count= values->elements;
283
284
 
284
 
  if (mysql_prepare_insert(thd, table_list, table, fields, values,
 
285
  if (mysql_prepare_insert(session, table_list, table, fields, values,
285
286
                           update_fields, update_values, duplic, &unused_conds,
286
287
                           false,
287
288
                           (fields.elements || !value_count ||
291
292
  /* mysql_prepare_insert set table_list->table if it was not set */
292
293
  table= table_list->table;
293
294
 
294
 
  context= &thd->lex->select_lex.context;
 
295
  context= &session->lex->select_lex.context;
295
296
  /*
296
297
    These three asserts test the hypothesis that the resetting of the name
297
298
    resolution context below is not necessary at all since the list of local
319
320
      my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), counter);
320
321
      goto abort;
321
322
    }
322
 
    if (setup_fields(thd, 0, *values, MARK_COLUMNS_READ, 0, 0))
 
323
    if (setup_fields(session, 0, *values, MARK_COLUMNS_READ, 0, 0))
323
324
      goto abort;
324
325
  }
325
326
  its.rewind ();
326
 
 
 
327
 
327
328
  /* Restore the current context. */
328
329
  ctx_state.restore_state(context, table_list);
329
330
 
341
342
    For single line insert, generate an error if try to set a NOT NULL field
342
343
    to NULL.
343
344
  */
344
 
  thd->count_cuted_fields= ((values_list.elements == 1 &&
 
345
  session->count_cuted_fields= ((values_list.elements == 1 &&
345
346
                             !ignore) ?
346
347
                            CHECK_FIELD_ERROR_FOR_NULL :
347
348
                            CHECK_FIELD_WARN);
348
 
  thd->cuted_fields = 0L;
 
349
  session->cuted_fields = 0L;
349
350
  table->next_number_field=table->found_next_number_field;
350
351
 
351
 
#ifdef HAVE_REPLICATION
352
 
  if (thd->slave_thread &&
 
352
  if (session->slave_thread &&
353
353
      (info.handle_duplicates == DUP_UPDATE) &&
354
354
      (table->next_number_field != NULL) &&
355
355
      rpl_master_has_bug(&active_mi->rli, 24432))
356
356
    goto abort;
357
 
#endif
358
357
 
359
358
  error=0;
360
 
  thd_proc_info(thd, "update");
 
359
  session->set_proc_info("update");
361
360
  if (duplic == DUP_REPLACE)
362
361
    table->file->extra(HA_EXTRA_WRITE_CAN_REPLACE);
363
362
  if (duplic == DUP_UPDATE)
369
368
  }
370
369
 
371
370
 
372
 
  thd->abort_on_warning= !ignore;
 
371
  session->abort_on_warning= !ignore;
373
372
 
374
373
  table->mark_columns_needed_for_insert();
375
374
 
378
377
    if (fields.elements || !value_count)
379
378
    {
380
379
      restore_record(table,s->default_values);  // Get empty record
381
 
      if (fill_record(thd, fields, *values, 0))
 
380
      if (fill_record(session, fields, *values, 0))
382
381
      {
383
 
        if (values_list.elements != 1 && ! thd->is_error())
 
382
        if (values_list.elements != 1 && ! session->is_error())
384
383
        {
385
384
          info.records++;
386
385
          continue;
387
386
        }
388
387
        /*
389
 
          TODO: set thd->abort_on_warning if values_list.elements == 1
 
388
          TODO: set session->abort_on_warning if values_list.elements == 1
390
389
          and check that all items return warning in case of problem with
391
390
          storing field.
392
391
        */
396
395
    }
397
396
    else
398
397
    {
399
 
      if (thd->used_tables)                     // Column used in values()
 
398
      if (session->used_tables)                 // Column used in values()
400
399
        restore_record(table,s->default_values);        // Get empty record
401
400
      else
402
401
      {
407
406
        */
408
407
        table->record[0][0]= table->s->default_values[0];
409
408
      }
410
 
      if (fill_record(thd, table->field, *values, 0))
 
409
      if (fill_record(session, table->field, *values, 0))
411
410
      {
412
 
        if (values_list.elements != 1 && ! thd->is_error())
 
411
        if (values_list.elements != 1 && ! session->is_error())
413
412
        {
414
413
          info.records++;
415
414
          continue;
419
418
      }
420
419
    }
421
420
 
422
 
    error=write_record(thd, table ,&info);
 
421
    error=write_record(session, table ,&info);
423
422
    if (error)
424
423
      break;
425
 
    thd->row_count++;
 
424
    session->row_count++;
426
425
  }
427
426
 
428
 
  free_underlaid_joins(thd, &thd->lex->select_lex);
 
427
  free_underlaid_joins(session, &session->lex->select_lex);
429
428
  joins_freed= true;
430
429
 
431
430
  /*
435
434
  {
436
435
    /*
437
436
      Do not do this release if this is a delayed insert, it would steal
438
 
      auto_inc values from the delayed_insert thread as they share TABLE.
 
437
      auto_inc values from the delayed_insert thread as they share Table.
439
438
    */
440
439
    table->file->ha_release_auto_increment();
441
440
    if (table->file->ha_end_bulk_insert() && !error)
456
455
        before binlog writing and ha_autocommit_or_rollback
457
456
      */
458
457
    }
459
 
    if ((changed && error <= 0) || thd->transaction.stmt.modified_non_trans_table || was_insert_delayed)
 
458
    if ((changed && error <= 0) || session->transaction.stmt.modified_non_trans_table || was_insert_delayed)
460
459
    {
461
 
      if (mysql_bin_log.is_open())
462
 
      {
463
 
        if (error <= 0)
464
 
        {
465
 
          /*
466
 
            [Guilhem wrote] Temporary errors may have filled
467
 
            thd->net.last_error/errno.  For example if there has
468
 
            been a disk full error when writing the row, and it was
469
 
            MyISAM, then thd->net.last_error/errno will be set to
470
 
            "disk full"... and the my_pwrite() will wait until free
471
 
            space appears, and so when it finishes then the
472
 
            write_row() was entirely successful
473
 
          */
474
 
          /* todo: consider removing */
475
 
          thd->clear_error();
476
 
        }
477
 
        /* bug#22725:
478
 
 
479
 
        A query which per-row-loop can not be interrupted with
480
 
        KILLED, like INSERT, and that does not invoke stored
481
 
        routines can be binlogged with neglecting the KILLED error.
482
 
        
483
 
        If there was no error (error == zero) until after the end of
484
 
        inserting loop the KILLED flag that appeared later can be
485
 
        disregarded since previously possible invocation of stored
486
 
        routines did not result in any error due to the KILLED.  In
487
 
        such case the flag is ignored for constructing binlog event.
488
 
        */
489
 
        assert(thd->killed != THD::KILL_BAD_DATA || error > 0);
490
 
        if (thd->binlog_query(THD::ROW_QUERY_TYPE,
491
 
                              thd->query, thd->query_length,
492
 
                              transactional_table, false,
493
 
                              (error>0) ? thd->killed : THD::NOT_KILLED) &&
494
 
            transactional_table)
495
 
        {
496
 
          error=1;
497
 
        }
498
 
      }
499
 
      if (thd->transaction.stmt.modified_non_trans_table)
500
 
        thd->transaction.all.modified_non_trans_table= true;
 
460
      if (session->transaction.stmt.modified_non_trans_table)
 
461
        session->transaction.all.modified_non_trans_table= true;
501
462
    }
502
 
    assert(transactional_table || !changed || 
503
 
                thd->transaction.stmt.modified_non_trans_table);
 
463
    assert(transactional_table || !changed ||
 
464
                session->transaction.stmt.modified_non_trans_table);
504
465
 
505
466
  }
506
 
  thd_proc_info(thd, "end");
 
467
  session->set_proc_info("end");
507
468
  /*
508
469
    We'll report to the client this id:
509
470
    - if the table contains an autoincrement column and we successfully
514
475
    inserted, the id of the last "inserted" row (if IGNORE, that value may not
515
476
    have been really inserted but ignored).
516
477
  */
517
 
  id= (thd->first_successful_insert_id_in_cur_stmt > 0) ?
518
 
    thd->first_successful_insert_id_in_cur_stmt :
519
 
    (thd->arg_of_last_insert_id_function ?
520
 
     thd->first_successful_insert_id_in_prev_stmt :
 
478
  id= (session->first_successful_insert_id_in_cur_stmt > 0) ?
 
479
    session->first_successful_insert_id_in_cur_stmt :
 
480
    (session->arg_of_last_insert_id_function ?
 
481
     session->first_successful_insert_id_in_prev_stmt :
521
482
     ((table->next_number_field && info.copied) ?
522
483
     table->next_number_field->val_int() : 0));
523
484
  table->next_number_field=0;
524
 
  thd->count_cuted_fields= CHECK_FIELD_IGNORE;
 
485
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
525
486
  table->auto_increment_field_not_null= false;
526
487
  if (duplic == DUP_REPLACE)
527
488
    table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
528
489
 
529
490
  if (error)
530
491
    goto abort;
531
 
  if (values_list.elements == 1 && (!(thd->options & OPTION_WARNINGS) ||
532
 
                                    !thd->cuted_fields))
 
492
  if (values_list.elements == 1 && (!(session->options & OPTION_WARNINGS) ||
 
493
                                    !session->cuted_fields))
533
494
  {
534
 
    thd->row_count_func= info.copied + info.deleted +
535
 
                         ((thd->client_capabilities & CLIENT_FOUND_ROWS) ?
 
495
    session->row_count_func= info.copied + info.deleted +
 
496
                         ((session->client_capabilities & CLIENT_FOUND_ROWS) ?
536
497
                          info.touched : info.updated);
537
 
    my_ok(thd, (ulong) thd->row_count_func, id);
 
498
    my_ok(session, (ulong) session->row_count_func, id);
538
499
  }
539
500
  else
540
501
  {
541
502
    char buff[160];
542
 
    ha_rows updated=((thd->client_capabilities & CLIENT_FOUND_ROWS) ?
 
503
    ha_rows updated=((session->client_capabilities & CLIENT_FOUND_ROWS) ?
543
504
                     info.touched : info.updated);
544
505
    if (ignore)
545
506
      sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
546
 
              (ulong) (info.records - info.copied), (ulong) thd->cuted_fields);
 
507
              (ulong) (info.records - info.copied), (ulong) session->cuted_fields);
547
508
    else
548
509
      sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
549
 
              (ulong) (info.deleted + updated), (ulong) thd->cuted_fields);
550
 
    thd->row_count_func= info.copied + info.deleted + updated;
551
 
    ::my_ok(thd, (ulong) thd->row_count_func, id, buff);
 
510
              (ulong) (info.deleted + updated), (ulong) session->cuted_fields);
 
511
    session->row_count_func= info.copied + info.deleted + updated;
 
512
    ::my_ok(session, (ulong) session->row_count_func, id, buff);
552
513
  }
553
 
  thd->abort_on_warning= 0;
 
514
  session->abort_on_warning= 0;
554
515
  DRIZZLE_INSERT_END();
555
516
  return(false);
556
517
 
558
519
  if (table != NULL)
559
520
    table->file->ha_release_auto_increment();
560
521
  if (!joins_freed)
561
 
    free_underlaid_joins(thd, &thd->lex->select_lex);
562
 
  thd->abort_on_warning= 0;
 
522
    free_underlaid_joins(session, &session->lex->select_lex);
 
523
  session->abort_on_warning= 0;
563
524
  DRIZZLE_INSERT_END();
564
525
  return(true);
565
526
}
570
531
 
571
532
  SYNOPSIS
572
533
     mysql_prepare_insert_check_table()
573
 
     thd                Thread handle
 
534
     session            Thread handle
574
535
     table_list         Table list
575
536
     fields             List of fields to be updated
576
537
     where              Pointer to where clause
581
542
     true  ERROR
582
543
*/
583
544
 
584
 
static bool mysql_prepare_insert_check_table(THD *thd, TABLE_LIST *table_list,
 
545
static bool mysql_prepare_insert_check_table(Session *session, TableList *table_list,
585
546
                                             List<Item> &fields __attribute__((unused)),
586
547
                                             bool select_insert)
587
548
{
588
 
  
 
549
 
589
550
 
590
551
  /*
591
552
     first table in list is the one we'll INSERT into, requires INSERT_ACL.
594
555
     than INSERT.
595
556
  */
596
557
 
597
 
  if (setup_tables_and_check_access(thd, &thd->lex->select_lex.context,
598
 
                                    &thd->lex->select_lex.top_join_list,
 
558
  if (setup_tables_and_check_access(session, &session->lex->select_lex.context,
 
559
                                    &session->lex->select_lex.top_join_list,
599
560
                                    table_list,
600
 
                                    &thd->lex->select_lex.leaf_tables,
 
561
                                    &session->lex->select_lex.leaf_tables,
601
562
                                    select_insert))
602
563
    return(true);
603
564
 
610
571
 
611
572
  SYNOPSIS
612
573
    mysql_prepare_insert()
613
 
    thd                 Thread handler
 
574
    session                     Thread handler
614
575
    table_list          Global/local table list
615
576
    table               Table to insert into (can be NULL if table should
616
 
                        be taken from table_list->table)    
 
577
                        be taken from table_list->table)
617
578
    where               Where clause (for insert ... select)
618
579
    select_insert       true if INSERT ... SELECT statement
619
 
    check_fields        true if need to check that all INSERT fields are 
 
580
    check_fields        true if need to check that all INSERT fields are
620
581
                        given values.
621
 
    abort_on_warning    whether to report if some INSERT field is not 
 
582
    abort_on_warning    whether to report if some INSERT field is not
622
583
                        assigned as an error (true) or as a warning (false).
623
584
 
624
585
  TODO (in far future)
630
591
  WARNING
631
592
    You MUST set table->insert_values to 0 after calling this function
632
593
    before releasing the table object.
633
 
  
 
594
 
634
595
  RETURN VALUE
635
596
    false OK
636
597
    true  error
637
598
*/
638
599
 
639
 
bool mysql_prepare_insert(THD *thd, TABLE_LIST *table_list,
640
 
                          TABLE *table, List<Item> &fields, List_item *values,
 
600
bool mysql_prepare_insert(Session *session, TableList *table_list,
 
601
                          Table *table, List<Item> &fields, List_item *values,
641
602
                          List<Item> &update_fields, List<Item> &update_values,
642
603
                          enum_duplicates duplic,
643
604
                          COND **where __attribute__((unused)),
644
605
                          bool select_insert,
645
606
                          bool check_fields, bool abort_on_warning)
646
607
{
647
 
  SELECT_LEX *select_lex= &thd->lex->select_lex;
 
608
  SELECT_LEX *select_lex= &session->lex->select_lex;
648
609
  Name_resolution_context *context= &select_lex->context;
649
610
  Name_resolution_context_state ctx_state;
650
611
  bool insert_into_view= (0 != 0);
651
612
  bool res= 0;
652
613
  table_map map= 0;
653
 
  
 
614
 
654
615
  /* INSERT should have a SELECT or VALUES clause */
655
616
  assert (!select_insert || !values);
656
617
 
677
638
  if (duplic == DUP_UPDATE)
678
639
  {
679
640
    /* it should be allocated before Item::fix_fields() */
680
 
    if (table_list->set_insert_values(thd->mem_root))
 
641
    if (table_list->set_insert_values(session->mem_root))
681
642
      return(true);
682
643
  }
683
644
 
684
 
  if (mysql_prepare_insert_check_table(thd, table_list, fields, select_insert))
 
645
  if (mysql_prepare_insert_check_table(session, table_list, fields, select_insert))
685
646
    return(true);
686
647
 
687
648
 
701
662
    table_list->next_local= 0;
702
663
    context->resolve_in_table_list_only(table_list);
703
664
 
704
 
    res= check_insert_fields(thd, context->table_list, fields, *values,
 
665
    res= check_insert_fields(session, context->table_list, fields, *values,
705
666
                             !insert_into_view, &map) ||
706
 
      setup_fields(thd, 0, *values, MARK_COLUMNS_READ, 0, 0);
 
667
      setup_fields(session, 0, *values, MARK_COLUMNS_READ, 0, 0);
707
668
 
708
669
    if (!res && check_fields)
709
670
    {
710
 
      bool saved_abort_on_warning= thd->abort_on_warning;
711
 
      thd->abort_on_warning= abort_on_warning;
712
 
      res= check_that_all_fields_are_given_values(thd, 
713
 
                                                  table ? table : 
 
671
      bool saved_abort_on_warning= session->abort_on_warning;
 
672
      session->abort_on_warning= abort_on_warning;
 
673
      res= check_that_all_fields_are_given_values(session,
 
674
                                                  table ? table :
714
675
                                                  context->table_list->table,
715
676
                                                  context->table_list);
716
 
      thd->abort_on_warning= saved_abort_on_warning;
 
677
      session->abort_on_warning= saved_abort_on_warning;
717
678
    }
718
679
 
719
680
    if (!res && duplic == DUP_UPDATE)
720
681
    {
721
 
      res= check_update_fields(thd, context->table_list, update_fields, &map);
 
682
      res= check_update_fields(session, context->table_list, update_fields, &map);
722
683
    }
723
684
 
724
685
    /* Restore the current context. */
725
686
    ctx_state.restore_state(context, table_list);
726
687
 
727
688
    if (!res)
728
 
      res= setup_fields(thd, 0, update_values, MARK_COLUMNS_READ, 0, 0);
 
689
      res= setup_fields(session, 0, update_values, MARK_COLUMNS_READ, 0, 0);
729
690
  }
730
691
 
731
692
  if (res)
736
697
 
737
698
  if (!select_insert)
738
699
  {
739
 
    TABLE_LIST *duplicate;
740
 
    if ((duplicate= unique_table(thd, table_list, table_list->next_global, 1)))
 
700
    TableList *duplicate;
 
701
    if ((duplicate= unique_table(session, table_list, table_list->next_global, 1)))
741
702
    {
742
703
      update_non_unique_table_error(table_list, "INSERT", duplicate);
743
704
      return(true);
744
705
    }
745
 
    select_lex->first_execution= 0;
746
706
  }
747
707
  if (duplic == DUP_UPDATE || duplic == DUP_REPLACE)
748
708
    table->prepare_for_position();
752
712
 
753
713
        /* Check if there is more uniq keys after field */
754
714
 
755
 
static int last_uniq_key(TABLE *table,uint keynr)
 
715
static int last_uniq_key(Table *table,uint32_t keynr)
756
716
{
757
717
  while (++keynr < table->s->keys)
758
718
    if (table->key_info[keynr].flags & HA_NOSAME)
767
727
 
768
728
  SYNOPSIS
769
729
     write_record()
770
 
      thd   - thread context
 
730
      session   - thread context
771
731
      table - table to which record should be written
772
732
      info  - COPY_INFO structure describing handling of duplicates
773
733
              and which is used for counting number of records inserted
779
739
    then both on update triggers will work instead. Similarly both on
780
740
    delete triggers will be invoked if we will delete conflicting records.
781
741
 
782
 
    Sets thd->transaction.stmt.modified_non_trans_table to true if table which is updated didn't have
 
742
    Sets session->transaction.stmt.modified_non_trans_table to true if table which is updated didn't have
783
743
    transactions.
784
744
 
785
745
  RETURN VALUE
788
748
*/
789
749
 
790
750
 
791
 
int write_record(THD *thd, TABLE *table,COPY_INFO *info)
 
751
int write_record(Session *session, Table *table,COPY_INFO *info)
792
752
{
793
753
  int error;
794
754
  char *key=0;
795
755
  MY_BITMAP *save_read_set, *save_write_set;
796
756
  uint64_t prev_insert_id= table->file->next_insert_id;
797
757
  uint64_t insert_id_for_cur_row= 0;
798
 
  
 
758
 
799
759
 
800
760
  info->records++;
801
761
  save_read_set=  table->read_set;
806
766
  {
807
767
    while ((error=table->file->ha_write_row(table->record[0])))
808
768
    {
809
 
      uint key_nr;
 
769
      uint32_t key_nr;
810
770
      /*
811
771
        If we do more than one iteration of this loop, from the second one the
812
772
        row will have an explicit value in the autoinc field, which was set at
813
773
        the first call of handler::update_auto_increment(). So we must save
814
 
        the autogenerated value to avoid thd->insert_id_for_cur_row to become
 
774
        the autogenerated value to avoid session->insert_id_for_cur_row to become
815
775
        0.
816
776
      */
817
777
      if (table->file->insert_id_for_cur_row > 0)
865
825
 
866
826
        if (!key)
867
827
        {
868
 
          if (!(key=(char*) my_safe_alloca(table->s->max_unique_length,
869
 
                                           MAX_KEY_LENGTH)))
 
828
          if (!(key=(char*) malloc(table->s->max_unique_length)))
870
829
          {
871
830
            error=ENOMEM;
872
831
            goto err;
873
832
          }
874
833
        }
875
 
        key_copy((uchar*) key,table->record[0],table->key_info+key_nr,0);
 
834
        key_copy((unsigned char*) key,table->record[0],table->key_info+key_nr,0);
876
835
        if ((error=(table->file->index_read_idx_map(table->record[1],key_nr,
877
 
                                                    (uchar*) key, HA_WHOLE_KEY,
 
836
                                                    (unsigned char*) key, HA_WHOLE_KEY,
878
837
                                                    HA_READ_KEY_EXACT))))
879
838
          goto err;
880
839
      }
890
849
        restore_record(table,record[1]);
891
850
        assert(info->update_fields->elements ==
892
851
                    info->update_values->elements);
893
 
        if (fill_record(thd, *info->update_fields,
 
852
        if (fill_record(session, *info->update_fields,
894
853
                                                 *info->update_values,
895
854
                                                 info->ignore))
896
855
          goto before_err;
902
861
        info->touched++;
903
862
        if ((table->file->ha_table_flags() & HA_PARTIAL_COLUMN_READ &&
904
863
             !bitmap_is_subset(table->write_set, table->read_set)) ||
905
 
            compare_record(table))
 
864
            table->compare_record())
906
865
        {
907
866
          if ((error=table->file->ha_update_row(table->record[1],
908
867
                                                table->record[0])) &&
925
884
            like a regular UPDATE statement: it should not affect the value of a
926
885
            next SELECT LAST_INSERT_ID() or mysql_insert_id().
927
886
            Except if LAST_INSERT_ID(#) was in the INSERT query, which is
928
 
            handled separately by THD::arg_of_last_insert_id_function.
 
887
            handled separately by Session::arg_of_last_insert_id_function.
929
888
          */
930
889
          insert_id_for_cur_row= table->file->insert_id_for_cur_row= 0;
931
890
          info->copied++;
945
904
          an INSERT or DELETE(s) + INSERT; FOREIGN KEY checks in
946
905
          InnoDB do not function in the defined way if we allow MySQL
947
906
          to convert the latter operation internally to an UPDATE.
948
 
          We also should not perform this conversion if we have 
 
907
          We also should not perform this conversion if we have
949
908
          timestamp field with ON UPDATE which is different from DEFAULT.
950
909
          Another case when conversion should not be performed is when
951
910
          we have ON DELETE trigger on table so user may notice that
967
926
            info->deleted++;
968
927
          else
969
928
            error= 0;
970
 
          thd->record_first_successful_insert_id_in_cur_stmt(table->file->insert_id_for_cur_row);
 
929
          session->record_first_successful_insert_id_in_cur_stmt(table->file->insert_id_for_cur_row);
971
930
          /*
972
931
            Since we pretend that we have done insert we should call
973
932
            its after triggers.
980
939
            goto err;
981
940
          info->deleted++;
982
941
          if (!table->file->has_transactions())
983
 
            thd->transaction.stmt.modified_non_trans_table= true;
 
942
            session->transaction.stmt.modified_non_trans_table= true;
984
943
          /* Let us attempt do write_row() once more */
985
944
        }
986
945
      }
987
946
    }
988
 
    thd->record_first_successful_insert_id_in_cur_stmt(table->file->insert_id_for_cur_row);
 
947
    session->record_first_successful_insert_id_in_cur_stmt(table->file->insert_id_for_cur_row);
989
948
    /*
990
949
      Restore column maps if they where replaced during an duplicate key
991
950
      problem.
1005
964
 
1006
965
after_n_copied_inc:
1007
966
  info->copied++;
1008
 
  thd->record_first_successful_insert_id_in_cur_stmt(table->file->insert_id_for_cur_row);
 
967
  session->record_first_successful_insert_id_in_cur_stmt(table->file->insert_id_for_cur_row);
1009
968
 
1010
969
gok_or_after_err:
1011
970
  if (key)
1012
 
    my_safe_afree(key,table->s->max_unique_length,MAX_KEY_LENGTH);
 
971
    free(key);
1013
972
  if (!table->file->has_transactions())
1014
 
    thd->transaction.stmt.modified_non_trans_table= true;
 
973
    session->transaction.stmt.modified_non_trans_table= true;
1015
974
  return(0);
1016
975
 
1017
976
err:
1018
977
  info->last_errno= error;
1019
978
  /* current_select is NULL if this is a delayed insert */
1020
 
  if (thd->lex->current_select)
1021
 
    thd->lex->current_select->no_error= 0;        // Give error
 
979
  if (session->lex->current_select)
 
980
    session->lex->current_select->no_error= 0;        // Give error
1022
981
  table->file->print_error(error,MYF(0));
1023
 
  
 
982
 
1024
983
before_err:
1025
984
  table->file->restore_auto_increment(prev_insert_id);
1026
985
  if (key)
1027
 
    my_safe_afree(key, table->s->max_unique_length, MAX_KEY_LENGTH);
 
986
    free(key);
1028
987
  table->column_bitmaps_set(save_read_set, save_write_set);
1029
988
  return(1);
1030
989
}
1034
993
  Check that all fields with arn't null_fields are used
1035
994
******************************************************************************/
1036
995
 
1037
 
int check_that_all_fields_are_given_values(THD *thd, TABLE *entry,
1038
 
                                           TABLE_LIST *table_list)
 
996
int check_that_all_fields_are_given_values(Session *session, Table *entry,
 
997
                                           TableList *table_list)
1039
998
{
1040
999
  int err= 0;
1041
1000
  MY_BITMAP *write_set= entry->write_set;
1053
1012
        view= test(0);
1054
1013
      }
1055
1014
      {
1056
 
        push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
1015
        push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1057
1016
                            ER_NO_DEFAULT_FOR_FIELD,
1058
1017
                            ER(ER_NO_DEFAULT_FOR_FIELD),
1059
1018
                            (*field)->field_name);
1061
1020
      err= 1;
1062
1021
    }
1063
1022
  }
1064
 
  return thd->abort_on_warning ? err : 0;
 
1023
  return session->abort_on_warning ? err : 0;
1065
1024
}
1066
1025
 
1067
1026
/***************************************************************************
1074
1033
 
1075
1034
  SYNOPSIS
1076
1035
    mysql_insert_select_prepare()
1077
 
    thd         thread handler
 
1036
    session         thread handler
1078
1037
 
1079
1038
  RETURN
1080
1039
    false OK
1081
1040
    true  Error
1082
1041
*/
1083
1042
 
1084
 
bool mysql_insert_select_prepare(THD *thd)
 
1043
bool mysql_insert_select_prepare(Session *session)
1085
1044
{
1086
 
  LEX *lex= thd->lex;
 
1045
  LEX *lex= session->lex;
1087
1046
  SELECT_LEX *select_lex= &lex->select_lex;
1088
 
  TABLE_LIST *first_select_leaf_table;
1089
 
  
1090
 
 
1091
 
  /*
1092
 
    Statement-based replication of INSERT ... SELECT ... LIMIT is not safe
1093
 
    as order of rows is not defined, so in mixed mode we go to row-based.
1094
 
 
1095
 
    Note that we may consider a statement as safe if ORDER BY primary_key
1096
 
    is present or we SELECT a constant. However it may confuse users to
1097
 
    see very similiar statements replicated differently.
1098
 
  */
1099
 
  if (lex->current_select->select_limit)
1100
 
  {
1101
 
    lex->set_stmt_unsafe();
1102
 
    thd->set_current_stmt_binlog_row_based_if_mixed();
1103
 
  }
 
1047
 
1104
1048
  /*
1105
1049
    SELECT_LEX do not belong to INSERT statement, so we can't add WHERE
1106
1050
    clause if table is VIEW
1107
1051
  */
1108
 
  
1109
 
  if (mysql_prepare_insert(thd, lex->query_tables,
 
1052
 
 
1053
  if (mysql_prepare_insert(session, lex->query_tables,
1110
1054
                           lex->query_tables->table, lex->field_list, 0,
1111
1055
                           lex->update_list, lex->value_list,
1112
1056
                           lex->duplicates,
1120
1064
  assert(select_lex->leaf_tables != 0);
1121
1065
  lex->leaf_tables_insert= select_lex->leaf_tables;
1122
1066
  /* skip all leaf tables belonged to view where we are insert */
1123
 
  for (first_select_leaf_table= select_lex->leaf_tables->next_leaf;
1124
 
       first_select_leaf_table &&
1125
 
       first_select_leaf_table->belong_to_view &&
1126
 
       first_select_leaf_table->belong_to_view ==
1127
 
       lex->leaf_tables_insert->belong_to_view;
1128
 
       first_select_leaf_table= first_select_leaf_table->next_leaf)
1129
 
  {}
1130
 
  select_lex->leaf_tables= first_select_leaf_table;
 
1067
  select_lex->leaf_tables= select_lex->leaf_tables->next_leaf;
1131
1068
  return(false);
1132
1069
}
1133
1070
 
1134
1071
 
1135
 
select_insert::select_insert(TABLE_LIST *table_list_par, TABLE *table_par,
 
1072
select_insert::select_insert(TableList *table_list_par, Table *table_par,
1136
1073
                             List<Item> *fields_par,
1137
1074
                             List<Item> *update_fields,
1138
1075
                             List<Item> *update_values,
1153
1090
int
1154
1091
select_insert::prepare(List<Item> &values, SELECT_LEX_UNIT *u)
1155
1092
{
1156
 
  LEX *lex= thd->lex;
 
1093
  LEX *lex= session->lex;
1157
1094
  int res;
1158
1095
  table_map map= 0;
1159
1096
  SELECT_LEX *lex_current_select_save= lex->current_select;
1160
 
  
 
1097
 
1161
1098
 
1162
1099
  unit= u;
1163
1100
 
1167
1104
    we are fixing fields from insert list.
1168
1105
  */
1169
1106
  lex->current_select= &lex->select_lex;
1170
 
  res= check_insert_fields(thd, table_list, *fields, values,
 
1107
  res= check_insert_fields(session, table_list, *fields, values,
1171
1108
                           !insert_into_view, &map) ||
1172
 
       setup_fields(thd, 0, values, MARK_COLUMNS_READ, 0, 0);
 
1109
       setup_fields(session, 0, values, MARK_COLUMNS_READ, 0, 0);
1173
1110
 
1174
1111
  if (!res && fields->elements)
1175
1112
  {
1176
 
    bool saved_abort_on_warning= thd->abort_on_warning;
1177
 
    thd->abort_on_warning= !info.ignore;
1178
 
    res= check_that_all_fields_are_given_values(thd, table_list->table, 
 
1113
    bool saved_abort_on_warning= session->abort_on_warning;
 
1114
    session->abort_on_warning= !info.ignore;
 
1115
    res= check_that_all_fields_are_given_values(session, table_list->table,
1179
1116
                                                table_list);
1180
 
    thd->abort_on_warning= saved_abort_on_warning;
 
1117
    session->abort_on_warning= saved_abort_on_warning;
1181
1118
  }
1182
1119
 
1183
1120
  if (info.handle_duplicates == DUP_UPDATE && !res)
1192
1129
    table_list->next_local= 0;
1193
1130
    context->resolve_in_table_list_only(table_list);
1194
1131
 
1195
 
    res= res || check_update_fields(thd, context->table_list,
 
1132
    res= res || check_update_fields(session, context->table_list,
1196
1133
                                    *info.update_fields, &map);
1197
1134
    /*
1198
 
      When we are not using GROUP BY and there are no ungrouped aggregate functions 
 
1135
      When we are not using GROUP BY and there are no ungrouped aggregate functions
1199
1136
      we can refer to other tables in the ON DUPLICATE KEY part.
1200
1137
      We use next_name_resolution_table descructively, so check it first (views?)
1201
1138
    */
1206
1143
        We must make a single context out of the two separate name resolution contexts :
1207
1144
        the INSERT table and the tables in the SELECT part of INSERT ... SELECT.
1208
1145
        To do that we must concatenate the two lists
1209
 
      */  
1210
 
      table_list->next_name_resolution_table= 
 
1146
      */
 
1147
      table_list->next_name_resolution_table=
1211
1148
        ctx_state.get_first_name_resolution_table();
1212
1149
 
1213
 
    res= res || setup_fields(thd, 0, *info.update_values,
 
1150
    res= res || setup_fields(session, 0, *info.update_values,
1214
1151
                             MARK_COLUMNS_READ, 0, 0);
1215
1152
    if (!res)
1216
1153
    {
1226
1163
      while ((item= li++))
1227
1164
      {
1228
1165
        item->transform(&Item::update_value_transformer,
1229
 
                        (uchar*)lex->current_select);
 
1166
                        (unsigned char*)lex->current_select);
1230
1167
      }
1231
1168
    }
1232
1169
 
1247
1184
    Is table which we are changing used somewhere in other parts of
1248
1185
    query
1249
1186
  */
1250
 
  if (unique_table(thd, table_list, table_list->next_global, 0))
 
1187
  if (unique_table(session, table_list, table_list->next_global, 0))
1251
1188
  {
1252
1189
    /* Using same table for INSERT and SELECT */
1253
1190
    lex->current_select->options|= OPTION_BUFFER_RESULT;
1256
1193
  else if (!(lex->current_select->options & OPTION_BUFFER_RESULT))
1257
1194
  {
1258
1195
    /*
1259
 
      We must not yet prepare the result table if it is the same as one of the 
1260
 
      source tables (INSERT SELECT). The preparation may disable 
 
1196
      We must not yet prepare the result table if it is the same as one of the
 
1197
      source tables (INSERT SELECT). The preparation may disable
1261
1198
      indexes on the result table, which may be used during the select, if it
1262
1199
      is the same table (Bug #6034). Do the preparation after the select phase
1263
1200
      in select_insert::prepare2().
1269
1206
  restore_record(table,s->default_values);              // Get empty record
1270
1207
  table->next_number_field=table->found_next_number_field;
1271
1208
 
1272
 
#ifdef HAVE_REPLICATION
1273
 
  if (thd->slave_thread &&
 
1209
  if (session->slave_thread &&
1274
1210
      (info.handle_duplicates == DUP_UPDATE) &&
1275
1211
      (table->next_number_field != NULL) &&
1276
1212
      rpl_master_has_bug(&active_mi->rli, 24432))
1277
1213
    return(1);
1278
 
#endif
1279
1214
 
1280
 
  thd->cuted_fields=0;
 
1215
  session->cuted_fields=0;
1281
1216
  if (info.ignore || info.handle_duplicates != DUP_ERROR)
1282
1217
    table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
1283
1218
  if (info.handle_duplicates == DUP_REPLACE)
1284
1219
    table->file->extra(HA_EXTRA_WRITE_CAN_REPLACE);
1285
1220
  if (info.handle_duplicates == DUP_UPDATE)
1286
1221
    table->file->extra(HA_EXTRA_INSERT_WITH_UPDATE);
1287
 
  thd->abort_on_warning= !info.ignore;
 
1222
  session->abort_on_warning= !info.ignore;
1288
1223
  table->mark_columns_needed_for_insert();
1289
1224
 
1290
1225
 
1303
1238
    If the result table is the same as one of the source tables (INSERT SELECT),
1304
1239
    the result table is not finally prepared at the join prepair phase.
1305
1240
    Do the final preparation now.
1306
 
                       
 
1241
 
1307
1242
  RETURN
1308
1243
    0   OK
1309
1244
*/
1310
1245
 
1311
1246
int select_insert::prepare2(void)
1312
1247
{
1313
 
  
1314
 
  if (thd->lex->current_select->options & OPTION_BUFFER_RESULT)
 
1248
 
 
1249
  if (session->lex->current_select->options & OPTION_BUFFER_RESULT)
1315
1250
    table->file->ha_start_bulk_insert((ha_rows) 0);
1316
1251
  return(0);
1317
1252
}
1325
1260
 
1326
1261
select_insert::~select_insert()
1327
1262
{
1328
 
  
 
1263
 
1329
1264
  if (table)
1330
1265
  {
1331
1266
    table->next_number_field=0;
1332
1267
    table->auto_increment_field_not_null= false;
1333
1268
    table->file->ha_reset();
1334
1269
  }
1335
 
  thd->count_cuted_fields= CHECK_FIELD_IGNORE;
1336
 
  thd->abort_on_warning= 0;
 
1270
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
 
1271
  session->abort_on_warning= 0;
1337
1272
  return;
1338
1273
}
1339
1274
 
1340
1275
 
1341
1276
bool select_insert::send_data(List<Item> &values)
1342
1277
{
1343
 
  
 
1278
 
1344
1279
  bool error=0;
1345
1280
 
1346
1281
  if (unit->offset_limit_cnt)
1349
1284
    return(0);
1350
1285
  }
1351
1286
 
1352
 
  thd->count_cuted_fields= CHECK_FIELD_WARN;    // Calculate cuted fields
 
1287
  session->count_cuted_fields= CHECK_FIELD_WARN;        // Calculate cuted fields
1353
1288
  store_values(values);
1354
 
  thd->count_cuted_fields= CHECK_FIELD_IGNORE;
1355
 
  if (thd->is_error())
 
1289
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
 
1290
  if (session->is_error())
1356
1291
    return(1);
1357
1292
 
1358
 
  error= write_record(thd, table, &info);
1359
 
    
 
1293
  error= write_record(session, table, &info);
 
1294
 
1360
1295
  if (!error)
1361
1296
  {
1362
1297
    if (info.handle_duplicates == DUP_UPDATE)
1364
1299
      /*
1365
1300
        Restore fields of the record since it is possible that they were
1366
1301
        changed by ON DUPLICATE KEY UPDATE clause.
1367
 
    
 
1302
 
1368
1303
        If triggers exist then whey can modify some fields which were not
1369
1304
        originally touched by INSERT ... SELECT, so we have to restore
1370
1305
        their original values for the next row.
1377
1312
        If no value has been autogenerated so far, we need to remember the
1378
1313
        value we just saw, we may need to send it to client in the end.
1379
1314
      */
1380
 
      if (thd->first_successful_insert_id_in_cur_stmt == 0) // optimization
1381
 
        autoinc_value_of_last_inserted_row= 
 
1315
      if (session->first_successful_insert_id_in_cur_stmt == 0) // optimization
 
1316
        autoinc_value_of_last_inserted_row=
1382
1317
          table->next_number_field->val_int();
1383
1318
      /*
1384
1319
        Clear auto-increment field for the next record, if triggers are used
1394
1329
void select_insert::store_values(List<Item> &values)
1395
1330
{
1396
1331
  if (fields->elements)
1397
 
    fill_record(thd, *fields, values, 1);
 
1332
    fill_record(session, *fields, values, 1);
1398
1333
  else
1399
 
    fill_record(thd, table->field, values, 1);
 
1334
    fill_record(session, table->field, values, 1);
1400
1335
}
1401
1336
 
1402
 
void select_insert::send_error(uint errcode,const char *err)
 
1337
void select_insert::send_error(uint32_t errcode,const char *err)
1403
1338
{
1404
 
  
 
1339
 
1405
1340
 
1406
1341
  my_message(errcode, err, MYF(0));
1407
1342
 
1415
1350
  bool const trans_table= table->file->has_transactions();
1416
1351
  uint64_t id;
1417
1352
  bool changed;
1418
 
  THD::killed_state killed_status= thd->killed;
1419
 
  
 
1353
 
1420
1354
  error= table->file->ha_end_bulk_insert();
1421
1355
  table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
1422
1356
  table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
1427
1361
      We must invalidate the table in the query cache before binlog writing
1428
1362
      and ha_autocommit_or_rollback.
1429
1363
    */
1430
 
    if (thd->transaction.stmt.modified_non_trans_table)
1431
 
      thd->transaction.all.modified_non_trans_table= true;
 
1364
    if (session->transaction.stmt.modified_non_trans_table)
 
1365
      session->transaction.all.modified_non_trans_table= true;
1432
1366
  }
1433
 
  assert(trans_table || !changed || 
1434
 
              thd->transaction.stmt.modified_non_trans_table);
 
1367
  assert(trans_table || !changed ||
 
1368
              session->transaction.stmt.modified_non_trans_table);
1435
1369
 
1436
 
  /*
1437
 
    Write to binlog before commiting transaction.  No statement will
1438
 
    be written by the binlog_query() below in RBR mode.  All the
1439
 
    events are in the transaction cache and will be written when
1440
 
    ha_autocommit_or_rollback() is issued below.
1441
 
  */
1442
 
  if (mysql_bin_log.is_open())
1443
 
  {
1444
 
    if (!error)
1445
 
      thd->clear_error();
1446
 
    thd->binlog_query(THD::ROW_QUERY_TYPE,
1447
 
                      thd->query, thd->query_length,
1448
 
                      trans_table, false, killed_status);
1449
 
  }
1450
1370
  table->file->ha_release_auto_increment();
1451
1371
 
1452
1372
  if (error)
1457
1377
  char buff[160];
1458
1378
  if (info.ignore)
1459
1379
    sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
1460
 
            (ulong) (info.records - info.copied), (ulong) thd->cuted_fields);
 
1380
            (ulong) (info.records - info.copied), (ulong) session->cuted_fields);
1461
1381
  else
1462
1382
    sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
1463
 
            (ulong) (info.deleted+info.updated), (ulong) thd->cuted_fields);
1464
 
  thd->row_count_func= info.copied + info.deleted +
1465
 
                       ((thd->client_capabilities & CLIENT_FOUND_ROWS) ?
 
1383
            (ulong) (info.deleted+info.updated), (ulong) session->cuted_fields);
 
1384
  session->row_count_func= info.copied + info.deleted +
 
1385
                       ((session->client_capabilities & CLIENT_FOUND_ROWS) ?
1466
1386
                        info.touched : info.updated);
1467
1387
 
1468
 
  id= (thd->first_successful_insert_id_in_cur_stmt > 0) ?
1469
 
    thd->first_successful_insert_id_in_cur_stmt :
1470
 
    (thd->arg_of_last_insert_id_function ?
1471
 
     thd->first_successful_insert_id_in_prev_stmt :
 
1388
  id= (session->first_successful_insert_id_in_cur_stmt > 0) ?
 
1389
    session->first_successful_insert_id_in_cur_stmt :
 
1390
    (session->arg_of_last_insert_id_function ?
 
1391
     session->first_successful_insert_id_in_prev_stmt :
1472
1392
     (info.copied ? autoinc_value_of_last_inserted_row : 0));
1473
 
  ::my_ok(thd, (ulong) thd->row_count_func, id, buff);
 
1393
  ::my_ok(session, (ulong) session->row_count_func, id, buff);
1474
1394
  return(0);
1475
1395
}
1476
1396
 
1477
1397
void select_insert::abort() {
1478
1398
 
1479
 
  
 
1399
 
1480
1400
  /*
1481
1401
    If the creation of the table failed (due to a syntax error, for
1482
1402
    example), no table will have been opened and therefore 'table'
1505
1425
    */
1506
1426
    changed= (info.copied || info.deleted || info.updated);
1507
1427
    transactional_table= table->file->has_transactions();
1508
 
    if (thd->transaction.stmt.modified_non_trans_table)
1509
 
    {
1510
 
        if (mysql_bin_log.is_open())
1511
 
          thd->binlog_query(THD::ROW_QUERY_TYPE, thd->query, thd->query_length,
1512
 
                            transactional_table, false);
1513
 
        if (!thd->current_stmt_binlog_row_based && !can_rollback_data())
1514
 
          thd->transaction.all.modified_non_trans_table= true;
1515
 
    }
1516
1428
    assert(transactional_table || !changed ||
1517
 
                thd->transaction.stmt.modified_non_trans_table);
 
1429
                session->transaction.stmt.modified_non_trans_table);
1518
1430
    table->file->ha_release_auto_increment();
1519
1431
  }
1520
1432
 
1527
1439
***************************************************************************/
1528
1440
 
1529
1441
/*
1530
 
  Create table from lists of fields and items (or just return TABLE
 
1442
  Create table from lists of fields and items (or just return Table
1531
1443
  object for pre-opened existing table).
1532
1444
 
1533
1445
  SYNOPSIS
1534
1446
    create_table_from_items()
1535
 
      thd          in     Thread object
 
1447
      session          in     Thread object
1536
1448
      create_info  in     Create information (like MAX_ROWS, ENGINE or
1537
1449
                          temporary table flag)
1538
 
      create_table in     Pointer to TABLE_LIST object providing database
 
1450
      create_table in     Pointer to TableList object providing database
1539
1451
                          and name for table to be created or to be open
1540
1452
      alter_info   in/out Initial list of columns and indexes for the table
1541
1453
                          to be created
1545
1457
      lock         out    Pointer to the DRIZZLE_LOCK object for table created
1546
1458
                          (or open temporary table) will be returned in this
1547
1459
                          parameter. Since this table is not included in
1548
 
                          THD::lock caller is responsible for explicitly
 
1460
                          Session::lock caller is responsible for explicitly
1549
1461
                          unlocking this table.
1550
1462
      hooks
1551
1463
 
1553
1465
    This function behaves differently for base and temporary tables:
1554
1466
    - For base table we assume that either table exists and was pre-opened
1555
1467
      and locked at open_and_lock_tables() stage (and in this case we just
1556
 
      emit error or warning and return pre-opened TABLE object) or special
 
1468
      emit error or warning and return pre-opened Table object) or special
1557
1469
      placeholder was put in table cache that guarantees that this table
1558
1470
      won't be created or opened until the placeholder will be removed
1559
1471
      (so there is an exclusive lock on this table).
1564
1476
    SELECT it should be changed before it can be used in other contexts.
1565
1477
 
1566
1478
  RETURN VALUES
1567
 
    non-zero  Pointer to TABLE object for table created or opened
 
1479
    non-zero  Pointer to Table object for table created or opened
1568
1480
    0         Error
1569
1481
*/
1570
1482
 
1571
 
static TABLE *create_table_from_items(THD *thd, HA_CREATE_INFO *create_info,
1572
 
                                      TABLE_LIST *create_table,
 
1483
static Table *create_table_from_items(Session *session, HA_CREATE_INFO *create_info,
 
1484
                                      TableList *create_table,
1573
1485
                                      Alter_info *alter_info,
1574
1486
                                      List<Item> *items,
1575
1487
                                      DRIZZLE_LOCK **lock,
1576
 
                                      TABLEOP_HOOKS *hooks)
 
1488
                                      Tableop_hooks *hooks)
1577
1489
{
1578
 
  TABLE tmp_table;              // Used during 'Create_field()'
 
1490
  Table tmp_table;              // Used during 'Create_field()'
1579
1491
  TABLE_SHARE share;
1580
 
  TABLE *table= 0;
1581
 
  uint select_field_count= items->elements;
 
1492
  Table *table= 0;
 
1493
  uint32_t select_field_count= items->elements;
1582
1494
  /* Add selected items to field list */
1583
1495
  List_iterator_fast<Item> it(*items);
1584
1496
  Item *item;
1592
1504
    if (create_info->options & HA_LEX_CREATE_IF_NOT_EXISTS)
1593
1505
    {
1594
1506
      create_info->table_existed= 1;            // Mark that table existed
1595
 
      push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
 
1507
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1596
1508
                          ER_TABLE_EXISTS_ERROR, ER(ER_TABLE_EXISTS_ERROR),
1597
1509
                          create_table->table_name);
1598
1510
      return(create_table->table);
1605
1517
  tmp_table.alias= 0;
1606
1518
  tmp_table.timestamp_field= 0;
1607
1519
  tmp_table.s= &share;
1608
 
  init_tmp_table_share(thd, &share, "", 0, "", "");
 
1520
  init_tmp_table_share(session, &share, "", 0, "", "");
1609
1521
 
1610
1522
  tmp_table.s->db_create_options=0;
1611
1523
  tmp_table.s->blob_ptr_size= portable_sizeof_char_ptr;
1612
 
  tmp_table.s->db_low_byte_first= 
 
1524
  tmp_table.s->db_low_byte_first=
1613
1525
        test(create_info->db_type == myisam_hton ||
1614
1526
             create_info->db_type == heap_hton);
1615
1527
  tmp_table.null_row= false;
1625
1537
      else
1626
1538
        field= item->tmp_table_field_from_field_type(&tmp_table, 0);
1627
1539
    else
1628
 
      field= create_tmp_field(thd, &tmp_table, item, item->type(),
 
1540
      field= create_tmp_field(session, &tmp_table, item, item->type(),
1629
1541
                              (Item ***) 0, &tmp_field, &def_field, 0, 0, 0, 0,
1630
1542
                              0);
1631
1543
    if (!field ||
1651
1563
    binlog when a HEAP table is opened for the first time since startup, must
1652
1564
    not be written: 1) it would be wrong (imagine we're in CREATE SELECT: we
1653
1565
    don't want to delete from it) 2) it would be written before the CREATE
1654
 
    TABLE, which is a wrong order. So we keep binary logging disabled when we
 
1566
    Table, which is a wrong order. So we keep binary logging disabled when we
1655
1567
    open_table().
1656
1568
  */
1657
1569
  {
1658
 
    tmp_disable_binlog(thd);
1659
 
    if (!mysql_create_table_no_lock(thd, create_table->db,
 
1570
    tmp_disable_binlog(session);
 
1571
    if (!mysql_create_table_no_lock(session, create_table->db,
1660
1572
                                    create_table->table_name,
1661
1573
                                    create_info, alter_info, 0,
1662
 
                                    select_field_count))
 
1574
                                    select_field_count, true))
1663
1575
    {
1664
1576
      if (create_info->table_existed &&
1665
1577
          !(create_info->options & HA_LEX_CREATE_TMP_TABLE))
1675
1587
 
1676
1588
      if (!(create_info->options & HA_LEX_CREATE_TMP_TABLE))
1677
1589
      {
1678
 
        VOID(pthread_mutex_lock(&LOCK_open));
1679
 
        if (reopen_name_locked_table(thd, create_table, false))
 
1590
        pthread_mutex_lock(&LOCK_open);
 
1591
        if (reopen_name_locked_table(session, create_table, false))
1680
1592
        {
1681
1593
          quick_rm_table(create_info->db_type, create_table->db,
1682
1594
                         table_case_name(create_info, create_table->table_name),
1684
1596
        }
1685
1597
        else
1686
1598
          table= create_table->table;
1687
 
        VOID(pthread_mutex_unlock(&LOCK_open));
 
1599
        pthread_mutex_unlock(&LOCK_open);
1688
1600
      }
1689
1601
      else
1690
1602
      {
1691
 
        if (!(table= open_table(thd, create_table, thd->mem_root, (bool*) 0,
 
1603
        if (!(table= open_table(session, create_table, (bool*) 0,
1692
1604
                                DRIZZLE_OPEN_TEMPORARY_ONLY)) &&
1693
1605
            !create_info->table_existed)
1694
1606
        {
1697
1609
            it preparable for open. But let us do close_temporary_table() here
1698
1610
            just in case.
1699
1611
          */
1700
 
          drop_temporary_table(thd, create_table);
 
1612
          drop_temporary_table(session, create_table);
1701
1613
        }
1702
1614
      }
1703
1615
    }
1704
 
    reenable_binlog(thd);
 
1616
    reenable_binlog(session);
1705
1617
    if (!table)                                   // open failed
1706
1618
      return(0);
1707
1619
  }
1708
1620
 
1709
1621
  table->reginfo.lock_type=TL_WRITE;
1710
1622
  hooks->prelock(&table, 1);                    // Call prelock hooks
1711
 
  if (! ((*lock)= mysql_lock_tables(thd, &table, 1,
 
1623
  if (! ((*lock)= mysql_lock_tables(session, &table, 1,
1712
1624
                                    DRIZZLE_LOCK_IGNORE_FLUSH, &not_used)) ||
1713
1625
        hooks->postlock(&table, 1))
1714
1626
  {
1715
1627
    if (*lock)
1716
1628
    {
1717
 
      mysql_unlock_tables(thd, *lock);
 
1629
      mysql_unlock_tables(session, *lock);
1718
1630
      *lock= 0;
1719
1631
    }
1720
1632
 
1721
1633
    if (!create_info->table_existed)
1722
 
      drop_open_table(thd, table, create_table->db, create_table->table_name);
 
1634
      drop_open_table(session, table, create_table->db, create_table->table_name);
1723
1635
    return(0);
1724
1636
  }
1725
1637
  return(table);
1730
1642
select_create::prepare(List<Item> &values, SELECT_LEX_UNIT *u)
1731
1643
{
1732
1644
  DRIZZLE_LOCK *extra_lock= NULL;
1733
 
  
1734
 
 
1735
 
  TABLEOP_HOOKS *hook_ptr= NULL;
 
1645
 
 
1646
 
 
1647
  Tableop_hooks *hook_ptr= NULL;
1736
1648
  /*
1737
1649
    For row-based replication, the CREATE-SELECT statement is written
1738
1650
    in two pieces: the first one contain the CREATE TABLE statement
1751
1663
    slave.  Hence, we have to hold on to the CREATE part of the
1752
1664
    statement until the statement has finished.
1753
1665
   */
1754
 
  class MY_HOOKS : public TABLEOP_HOOKS {
 
1666
  class MY_HOOKS : public Tableop_hooks {
1755
1667
  public:
1756
 
    MY_HOOKS(select_create *x, TABLE_LIST *create_table,
1757
 
             TABLE_LIST *select_tables)
 
1668
    MY_HOOKS(select_create *x, TableList *create_table,
 
1669
             TableList *select_tables)
1758
1670
      : ptr(x), all_tables(*create_table)
1759
1671
      {
1760
1672
        all_tables.next_global= select_tables;
1761
1673
      }
1762
1674
 
1763
1675
  private:
1764
 
    virtual int do_postlock(TABLE **tables, uint count)
 
1676
    virtual int do_postlock(Table **tables, uint32_t count)
1765
1677
    {
1766
 
      THD *thd= const_cast<THD*>(ptr->get_thd());
1767
 
      if (int error= decide_logging_format(thd, &all_tables))
1768
 
        return error;
1769
 
 
1770
 
      TABLE const *const table = *tables;
1771
 
      if (thd->current_stmt_binlog_row_based  &&
1772
 
          !table->s->tmp_table &&
1773
 
          !ptr->get_create_info()->table_existed)
 
1678
      Table const *const table = *tables;
 
1679
      if (drizzle_bin_log.is_open()
 
1680
          && !table->s->tmp_table
 
1681
          && !ptr->get_create_info()->table_existed)
1774
1682
      {
1775
1683
        ptr->binlog_show_create_table(tables, count);
1776
1684
      }
1778
1686
    }
1779
1687
 
1780
1688
    select_create *ptr;
1781
 
    TABLE_LIST all_tables;
 
1689
    TableList all_tables;
1782
1690
  };
1783
1691
 
1784
1692
  MY_HOOKS hooks(this, create_table, select_tables);
1791
1699
    row-based replication for the statement.  If we are creating a
1792
1700
    temporary table, we need to start a statement transaction.
1793
1701
  */
1794
 
  if ((thd->lex->create_info.options & HA_LEX_CREATE_TMP_TABLE) == 0 &&
1795
 
      thd->current_stmt_binlog_row_based)
 
1702
  if ((session->lex->create_info.options & HA_LEX_CREATE_TMP_TABLE) == 0
 
1703
      && drizzle_bin_log.is_open())
1796
1704
  {
1797
 
    thd->binlog_start_trans_and_stmt();
1798
1705
  }
1799
1706
 
1800
 
  if (!(table= create_table_from_items(thd, create_info, create_table,
 
1707
  if (!(table= create_table_from_items(session, create_info, create_table,
1801
1708
                                       alter_info, &values,
1802
1709
                                       &extra_lock, hook_ptr)))
1803
1710
    return(-1);                         // abort() deletes table
1809
1716
    if (create_info->options & HA_LEX_CREATE_TMP_TABLE)
1810
1717
      m_plock= &m_lock;
1811
1718
    else
1812
 
      m_plock= &thd->extra_lock;
 
1719
      m_plock= &session->extra_lock;
1813
1720
 
1814
1721
    *m_plock= extra_lock;
1815
1722
  }
1832
1739
  table->next_number_field=table->found_next_number_field;
1833
1740
 
1834
1741
  restore_record(table,s->default_values);      // Get empty record
1835
 
  thd->cuted_fields=0;
 
1742
  session->cuted_fields=0;
1836
1743
  if (info.ignore || info.handle_duplicates != DUP_ERROR)
1837
1744
    table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
1838
1745
  if (info.handle_duplicates == DUP_REPLACE)
1840
1747
  if (info.handle_duplicates == DUP_UPDATE)
1841
1748
    table->file->extra(HA_EXTRA_INSERT_WITH_UPDATE);
1842
1749
  table->file->ha_start_bulk_insert((ha_rows) 0);
1843
 
  thd->abort_on_warning= !info.ignore;
1844
 
  if (check_that_all_fields_are_given_values(thd, table, table_list))
 
1750
  session->abort_on_warning= !info.ignore;
 
1751
  if (check_that_all_fields_are_given_values(session, table, table_list))
1845
1752
    return(1);
1846
1753
  table->mark_columns_needed_for_insert();
1847
1754
  table->file->extra(HA_EXTRA_WRITE_CACHE);
1849
1756
}
1850
1757
 
1851
1758
void
1852
 
select_create::binlog_show_create_table(TABLE **tables, uint count)
 
1759
select_create::binlog_show_create_table(Table **tables, uint32_t count)
1853
1760
{
1854
1761
  /*
1855
1762
    Note 1: In RBR mode, we generate a CREATE TABLE statement for the
1868
1775
    schema that will do a close_thread_tables(), destroying the
1869
1776
    statement transaction cache.
1870
1777
  */
1871
 
  assert(thd->current_stmt_binlog_row_based);
1872
1778
  assert(tables && *tables && count > 0);
1873
1779
 
1874
1780
  char buf[2048];
1875
1781
  String query(buf, sizeof(buf), system_charset_info);
1876
1782
  int result;
1877
 
  TABLE_LIST tmp_table_list;
 
1783
  TableList tmp_table_list;
1878
1784
 
1879
1785
  memset(&tmp_table_list, 0, sizeof(tmp_table_list));
1880
1786
  tmp_table_list.table = *tables;
1881
1787
  query.length(0);      // Have to zero it since constructor doesn't
1882
1788
 
1883
 
  result= store_create_info(thd, &tmp_table_list, &query, create_info);
 
1789
  result= store_create_info(session, &tmp_table_list, &query, create_info);
1884
1790
  assert(result == 0); /* store_create_info() always return 0 */
1885
 
 
1886
 
  thd->binlog_query(THD::STMT_QUERY_TYPE,
1887
 
                    query.ptr(), query.length(),
1888
 
                    /* is_trans */ true,
1889
 
                    /* suppress_use */ false);
1890
1791
}
1891
1792
 
1892
1793
void select_create::store_values(List<Item> &values)
1893
1794
{
1894
 
  fill_record(thd, field, values, 1);
 
1795
  fill_record(session, field, values, 1);
1895
1796
}
1896
1797
 
1897
1798
 
1898
 
void select_create::send_error(uint errcode,const char *err)
 
1799
void select_create::send_error(uint32_t errcode,const char *err)
1899
1800
{
1900
 
  
 
1801
 
1901
1802
 
1902
1803
  /*
1903
1804
    This will execute any rollbacks that are necessary before writing
1910
1811
    written to the binary log.
1911
1812
 
1912
1813
  */
1913
 
  tmp_disable_binlog(thd);
 
1814
  tmp_disable_binlog(session);
1914
1815
  select_insert::send_error(errcode, err);
1915
 
  reenable_binlog(thd);
 
1816
  reenable_binlog(session);
1916
1817
 
1917
1818
  return;
1918
1819
}
1932
1833
    */
1933
1834
    if (!table->s->tmp_table)
1934
1835
    {
1935
 
      ha_autocommit_or_rollback(thd, 0);
1936
 
      end_active_trans(thd);
 
1836
      ha_autocommit_or_rollback(session, 0);
 
1837
      end_active_trans(session);
1937
1838
    }
1938
1839
 
1939
1840
    table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
1940
1841
    table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
1941
1842
    if (m_plock)
1942
1843
    {
1943
 
      mysql_unlock_tables(thd, *m_plock);
 
1844
      mysql_unlock_tables(session, *m_plock);
1944
1845
      *m_plock= NULL;
1945
1846
      m_plock= NULL;
1946
1847
    }
1951
1852
 
1952
1853
void select_create::abort()
1953
1854
{
1954
 
  
 
1855
 
1955
1856
 
1956
1857
  /*
1957
1858
    In select_insert::abort() we roll back the statement, including
1968
1869
    of the table succeeded or not, since we need to reset the binary
1969
1870
    log state.
1970
1871
  */
1971
 
  tmp_disable_binlog(thd);
 
1872
  tmp_disable_binlog(session);
1972
1873
  select_insert::abort();
1973
 
  thd->transaction.stmt.modified_non_trans_table= false;
1974
 
  reenable_binlog(thd);
 
1874
  session->transaction.stmt.modified_non_trans_table= false;
 
1875
  reenable_binlog(session);
1975
1876
 
1976
1877
 
1977
1878
  if (m_plock)
1978
1879
  {
1979
 
    mysql_unlock_tables(thd, *m_plock);
 
1880
    mysql_unlock_tables(session, *m_plock);
1980
1881
    *m_plock= NULL;
1981
1882
    m_plock= NULL;
1982
1883
  }
1986
1887
    table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
1987
1888
    table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
1988
1889
    if (!create_info->table_existed)
1989
 
      drop_open_table(thd, table, create_table->db, create_table->table_name);
 
1890
      drop_open_table(session, table, create_table->db, create_table->table_name);
1990
1891
    table=0;                                    // Safety
1991
1892
  }
1992
1893
  return;