~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_insert.cc

  • Committer: mordred
  • Date: 2008-11-01 00:46:20 UTC
  • mto: (572.1.1 devel) (575.1.1 devel)
  • mto: This revision was merged to the branch mainline in revision 573.
  • Revision ID: mordred@opensolaris-20081101004620-vd0kzsl9k40hvf4p
Some updates to dtrace support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
#include <drizzled/server_includes.h>
26
26
#include <drizzled/sql_select.h>
27
27
#include <drizzled/sql_show.h>
28
 
#include "rpl_mi.h"
29
 
#include <drizzled/drizzled_error_messages.h>
 
28
#include <drizzled/rpl_mi.h>
 
29
#include <drizzled/error.h>
 
30
#include <drizzled/slave.h>
 
31
#include <drizzled/sql_parse.h>
 
32
#include <drizzled/probes.h>
30
33
 
31
34
/* Define to force use of my_malloc() if the allocated memory block is big */
32
35
 
45
48
 
46
49
  SYNOPSIS
47
50
    check_insert_fields()
48
 
    thd                         The current thread.
 
51
    session                         The current thread.
49
52
    table                       The table for insert.
50
53
    fields                      The insert fields.
51
54
    values                      The insert values.
61
64
    -1          Error
62
65
*/
63
66
 
64
 
static int check_insert_fields(THD *thd, TableList *table_list,
 
67
static int check_insert_fields(Session *session, TableList *table_list,
65
68
                               List<Item> &fields, List<Item> &values,
66
69
                               bool check_unique,
67
70
                               table_map *map __attribute__((unused)))
85
88
  }
86
89
  else
87
90
  {                                             // Part field list
88
 
    SELECT_LEX *select_lex= &thd->lex->select_lex;
 
91
    SELECT_LEX *select_lex= &session->lex->select_lex;
89
92
    Name_resolution_context *context= &select_lex->context;
90
93
    Name_resolution_context_state ctx_state;
91
94
    int res;
96
99
      return -1;
97
100
    }
98
101
 
99
 
    thd->dup_field= 0;
 
102
    session->dup_field= 0;
100
103
 
101
104
    /* Save the state of the current name resolution context. */
102
105
    ctx_state.save_state(context, table_list);
107
110
    */
108
111
    table_list->next_local= 0;
109
112
    context->resolve_in_table_list_only(table_list);
110
 
    res= setup_fields(thd, 0, fields, MARK_COLUMNS_WRITE, 0, 0);
 
113
    res= setup_fields(session, 0, fields, MARK_COLUMNS_WRITE, 0, 0);
111
114
 
112
115
    /* Restore the current context. */
113
116
    ctx_state.restore_state(context, table_list);
115
118
    if (res)
116
119
      return -1;
117
120
 
118
 
    if (check_unique && thd->dup_field)
 
121
    if (check_unique && session->dup_field)
119
122
    {
120
 
      my_error(ER_FIELD_SPECIFIED_TWICE, MYF(0), thd->dup_field->field_name);
 
123
      my_error(ER_FIELD_SPECIFIED_TWICE, MYF(0), session->dup_field->field_name);
121
124
      return -1;
122
125
    }
123
126
    if (table->timestamp_field) // Don't automaticly set timestamp if used
132
135
                       table->timestamp_field->field_index);
133
136
      }
134
137
    }
 
138
    /* Mark all virtual columns for write*/
 
139
    if (table->vfield)
 
140
      table->mark_virtual_columns();
135
141
  }
136
142
 
137
143
  return 0;
143
149
 
144
150
  SYNOPSIS
145
151
    check_update_fields()
146
 
    thd                         The current thread.
 
152
    session                         The current thread.
147
153
    insert_table_list           The insert table list.
148
154
    table                       The table for update.
149
155
    update_fields               The update fields.
157
163
    -1          Error
158
164
*/
159
165
 
160
 
static int check_update_fields(THD *thd, TableList *insert_table_list,
 
166
static int check_update_fields(Session *session, TableList *insert_table_list,
161
167
                               List<Item> &update_fields,
162
168
                               table_map *map __attribute__((unused)))
163
169
{
175
181
  }
176
182
 
177
183
  /* Check the fields we are going to modify */
178
 
  if (setup_fields(thd, 0, update_fields, MARK_COLUMNS_WRITE, 0, 0))
 
184
  if (setup_fields(session, 0, update_fields, MARK_COLUMNS_WRITE, 0, 0))
179
185
    return -1;
180
186
 
181
187
  if (table->timestamp_field)
202
208
*/
203
209
 
204
210
static
205
 
void upgrade_lock_type(THD *thd __attribute__((unused)),
 
211
void upgrade_lock_type(Session *session __attribute__((unused)),
206
212
                       thr_lock_type *lock_type,
207
213
                       enum_duplicates duplic,
208
214
                       bool is_multi_insert __attribute__((unused)))
224
230
  end of dispatch_command().
225
231
*/
226
232
 
227
 
bool mysql_insert(THD *thd,TableList *table_list,
 
233
bool mysql_insert(Session *session,TableList *table_list,
228
234
                  List<Item> &fields,
229
235
                  List<List_item> &values_list,
230
236
                  List<Item> &update_fields,
253
259
    Upgrade lock type if the requested lock is incompatible with
254
260
    the current connection mode or table operation.
255
261
  */
256
 
  upgrade_lock_type(thd, &table_list->lock_type, duplic,
 
262
  upgrade_lock_type(session, &table_list->lock_type, duplic,
257
263
                    values_list.elements > 1);
258
264
 
259
265
  /*
262
268
    never be able to get a lock on the table. QQQ: why not
263
269
    upgrade the lock here instead?
264
270
  */
265
 
  if (table_list->lock_type == TL_WRITE_DELAYED && thd->locked_tables &&
266
 
      find_locked_table(thd, table_list->db, table_list->table_name))
 
271
  if (table_list->lock_type == TL_WRITE_DELAYED && session->locked_tables &&
 
272
      find_locked_table(session, table_list->db, table_list->table_name))
267
273
  {
268
274
    my_error(ER_DELAYED_INSERT_TABLE_LOCKED, MYF(0),
269
275
             table_list->table_name);
271
277
  }
272
278
 
273
279
  {
274
 
    if (open_and_lock_tables(thd, table_list))
 
280
    if (open_and_lock_tables(session, table_list))
275
281
      return(true);
276
282
  }
277
283
  lock_type= table_list->lock_type;
278
284
 
279
 
  thd_proc_info(thd, "init");
280
 
  thd->used_tables=0;
 
285
  session->set_proc_info("init");
 
286
  session->used_tables=0;
281
287
  values= its++;
282
288
  value_count= values->elements;
283
289
 
284
 
  if (mysql_prepare_insert(thd, table_list, table, fields, values,
 
290
  if (mysql_prepare_insert(session, table_list, table, fields, values,
285
291
                           update_fields, update_values, duplic, &unused_conds,
286
292
                           false,
287
293
                           (fields.elements || !value_count ||
291
297
  /* mysql_prepare_insert set table_list->table if it was not set */
292
298
  table= table_list->table;
293
299
 
294
 
  context= &thd->lex->select_lex.context;
 
300
  context= &session->lex->select_lex.context;
295
301
  /*
296
302
    These three asserts test the hypothesis that the resetting of the name
297
303
    resolution context below is not necessary at all since the list of local
319
325
      my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), counter);
320
326
      goto abort;
321
327
    }
322
 
    if (setup_fields(thd, 0, *values, MARK_COLUMNS_READ, 0, 0))
 
328
    if (setup_fields(session, 0, *values, MARK_COLUMNS_READ, 0, 0))
323
329
      goto abort;
324
330
  }
325
331
  its.rewind ();
341
347
    For single line insert, generate an error if try to set a NOT NULL field
342
348
    to NULL.
343
349
  */
344
 
  thd->count_cuted_fields= ((values_list.elements == 1 &&
 
350
  session->count_cuted_fields= ((values_list.elements == 1 &&
345
351
                             !ignore) ?
346
352
                            CHECK_FIELD_ERROR_FOR_NULL :
347
353
                            CHECK_FIELD_WARN);
348
 
  thd->cuted_fields = 0L;
 
354
  session->cuted_fields = 0L;
349
355
  table->next_number_field=table->found_next_number_field;
350
356
 
351
 
  if (thd->slave_thread &&
 
357
  if (session->slave_thread &&
352
358
      (info.handle_duplicates == DUP_UPDATE) &&
353
359
      (table->next_number_field != NULL) &&
354
360
      rpl_master_has_bug(&active_mi->rli, 24432))
355
361
    goto abort;
356
362
 
357
363
  error=0;
358
 
  thd_proc_info(thd, "update");
 
364
  session->set_proc_info("update");
359
365
  if (duplic == DUP_REPLACE)
360
366
    table->file->extra(HA_EXTRA_WRITE_CAN_REPLACE);
361
367
  if (duplic == DUP_UPDATE)
367
373
  }
368
374
 
369
375
 
370
 
  thd->abort_on_warning= !ignore;
 
376
  session->abort_on_warning= !ignore;
371
377
 
372
378
  table->mark_columns_needed_for_insert();
373
379
 
376
382
    if (fields.elements || !value_count)
377
383
    {
378
384
      restore_record(table,s->default_values);  // Get empty record
379
 
      if (fill_record(thd, fields, *values, 0))
 
385
      if (fill_record(session, fields, *values, 0))
380
386
      {
381
 
        if (values_list.elements != 1 && ! thd->is_error())
 
387
        if (values_list.elements != 1 && ! session->is_error())
382
388
        {
383
389
          info.records++;
384
390
          continue;
385
391
        }
386
392
        /*
387
 
          TODO: set thd->abort_on_warning if values_list.elements == 1
 
393
          TODO: set session->abort_on_warning if values_list.elements == 1
388
394
          and check that all items return warning in case of problem with
389
395
          storing field.
390
396
        */
394
400
    }
395
401
    else
396
402
    {
397
 
      if (thd->used_tables)                     // Column used in values()
 
403
      if (session->used_tables)                 // Column used in values()
398
404
        restore_record(table,s->default_values);        // Get empty record
399
405
      else
400
406
      {
405
411
        */
406
412
        table->record[0][0]= table->s->default_values[0];
407
413
      }
408
 
      if (fill_record(thd, table->field, *values, 0))
 
414
      if (fill_record(session, table->field, *values, 0))
409
415
      {
410
 
        if (values_list.elements != 1 && ! thd->is_error())
 
416
        if (values_list.elements != 1 && ! session->is_error())
411
417
        {
412
418
          info.records++;
413
419
          continue;
417
423
      }
418
424
    }
419
425
 
420
 
    error=write_record(thd, table ,&info);
 
426
    error=write_record(session, table ,&info);
421
427
    if (error)
422
428
      break;
423
 
    thd->row_count++;
 
429
    session->row_count++;
424
430
  }
425
431
 
426
 
  free_underlaid_joins(thd, &thd->lex->select_lex);
 
432
  free_underlaid_joins(session, &session->lex->select_lex);
427
433
  joins_freed= true;
428
434
 
429
435
  /*
454
460
        before binlog writing and ha_autocommit_or_rollback
455
461
      */
456
462
    }
457
 
    if ((changed && error <= 0) || thd->transaction.stmt.modified_non_trans_table || was_insert_delayed)
 
463
    if ((changed && error <= 0) || session->transaction.stmt.modified_non_trans_table || was_insert_delayed)
458
464
    {
459
465
      if (mysql_bin_log.is_open())
460
466
      {
462
468
        {
463
469
          /*
464
470
            [Guilhem wrote] Temporary errors may have filled
465
 
            thd->net.last_error/errno.  For example if there has
 
471
            session->net.last_error/errno.  For example if there has
466
472
            been a disk full error when writing the row, and it was
467
 
            MyISAM, then thd->net.last_error/errno will be set to
 
473
            MyISAM, then session->net.last_error/errno will be set to
468
474
            "disk full"... and the my_pwrite() will wait until free
469
475
            space appears, and so when it finishes then the
470
476
            write_row() was entirely successful
471
477
          */
472
478
          /* todo: consider removing */
473
 
          thd->clear_error();
 
479
          session->clear_error();
474
480
        }
475
481
        /* bug#22725:
476
482
 
484
490
        routines did not result in any error due to the KILLED.  In
485
491
        such case the flag is ignored for constructing binlog event.
486
492
        */
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,
 
493
        assert(session->killed != Session::KILL_BAD_DATA || error > 0);
 
494
        if (session->binlog_query(Session::ROW_QUERY_TYPE,
 
495
                              session->query, session->query_length,
490
496
                              transactional_table, false,
491
 
                              (error>0) ? thd->killed : THD::NOT_KILLED) &&
 
497
                              (error>0) ? session->killed : Session::NOT_KILLED) &&
492
498
            transactional_table)
493
499
        {
494
500
          error=1;
495
501
        }
496
502
      }
497
 
      if (thd->transaction.stmt.modified_non_trans_table)
498
 
        thd->transaction.all.modified_non_trans_table= true;
 
503
      if (session->transaction.stmt.modified_non_trans_table)
 
504
        session->transaction.all.modified_non_trans_table= true;
499
505
    }
500
506
    assert(transactional_table || !changed || 
501
 
                thd->transaction.stmt.modified_non_trans_table);
 
507
                session->transaction.stmt.modified_non_trans_table);
502
508
 
503
509
  }
504
 
  thd_proc_info(thd, "end");
 
510
  session->set_proc_info("end");
505
511
  /*
506
512
    We'll report to the client this id:
507
513
    - if the table contains an autoincrement column and we successfully
512
518
    inserted, the id of the last "inserted" row (if IGNORE, that value may not
513
519
    have been really inserted but ignored).
514
520
  */
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 :
 
521
  id= (session->first_successful_insert_id_in_cur_stmt > 0) ?
 
522
    session->first_successful_insert_id_in_cur_stmt :
 
523
    (session->arg_of_last_insert_id_function ?
 
524
     session->first_successful_insert_id_in_prev_stmt :
519
525
     ((table->next_number_field && info.copied) ?
520
526
     table->next_number_field->val_int() : 0));
521
527
  table->next_number_field=0;
522
 
  thd->count_cuted_fields= CHECK_FIELD_IGNORE;
 
528
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
523
529
  table->auto_increment_field_not_null= false;
524
530
  if (duplic == DUP_REPLACE)
525
531
    table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
526
532
 
527
533
  if (error)
528
534
    goto abort;
529
 
  if (values_list.elements == 1 && (!(thd->options & OPTION_WARNINGS) ||
530
 
                                    !thd->cuted_fields))
 
535
  if (values_list.elements == 1 && (!(session->options & OPTION_WARNINGS) ||
 
536
                                    !session->cuted_fields))
531
537
  {
532
 
    thd->row_count_func= info.copied + info.deleted +
533
 
                         ((thd->client_capabilities & CLIENT_FOUND_ROWS) ?
 
538
    session->row_count_func= info.copied + info.deleted +
 
539
                         ((session->client_capabilities & CLIENT_FOUND_ROWS) ?
534
540
                          info.touched : info.updated);
535
 
    my_ok(thd, (ulong) thd->row_count_func, id);
 
541
    my_ok(session, (ulong) session->row_count_func, id);
536
542
  }
537
543
  else
538
544
  {
539
545
    char buff[160];
540
 
    ha_rows updated=((thd->client_capabilities & CLIENT_FOUND_ROWS) ?
 
546
    ha_rows updated=((session->client_capabilities & CLIENT_FOUND_ROWS) ?
541
547
                     info.touched : info.updated);
542
548
    if (ignore)
543
549
      sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
544
 
              (ulong) (info.records - info.copied), (ulong) thd->cuted_fields);
 
550
              (ulong) (info.records - info.copied), (ulong) session->cuted_fields);
545
551
    else
546
552
      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);
 
553
              (ulong) (info.deleted + updated), (ulong) session->cuted_fields);
 
554
    session->row_count_func= info.copied + info.deleted + updated;
 
555
    ::my_ok(session, (ulong) session->row_count_func, id, buff);
550
556
  }
551
 
  thd->abort_on_warning= 0;
 
557
  session->abort_on_warning= 0;
552
558
  DRIZZLE_INSERT_END();
553
559
  return(false);
554
560
 
556
562
  if (table != NULL)
557
563
    table->file->ha_release_auto_increment();
558
564
  if (!joins_freed)
559
 
    free_underlaid_joins(thd, &thd->lex->select_lex);
560
 
  thd->abort_on_warning= 0;
 
565
    free_underlaid_joins(session, &session->lex->select_lex);
 
566
  session->abort_on_warning= 0;
561
567
  DRIZZLE_INSERT_END();
562
568
  return(true);
563
569
}
568
574
 
569
575
  SYNOPSIS
570
576
     mysql_prepare_insert_check_table()
571
 
     thd                Thread handle
 
577
     session            Thread handle
572
578
     table_list         Table list
573
579
     fields             List of fields to be updated
574
580
     where              Pointer to where clause
579
585
     true  ERROR
580
586
*/
581
587
 
582
 
static bool mysql_prepare_insert_check_table(THD *thd, TableList *table_list,
 
588
static bool mysql_prepare_insert_check_table(Session *session, TableList *table_list,
583
589
                                             List<Item> &fields __attribute__((unused)),
584
590
                                             bool select_insert)
585
591
{
592
598
     than INSERT.
593
599
  */
594
600
 
595
 
  if (setup_tables_and_check_access(thd, &thd->lex->select_lex.context,
596
 
                                    &thd->lex->select_lex.top_join_list,
 
601
  if (setup_tables_and_check_access(session, &session->lex->select_lex.context,
 
602
                                    &session->lex->select_lex.top_join_list,
597
603
                                    table_list,
598
 
                                    &thd->lex->select_lex.leaf_tables,
 
604
                                    &session->lex->select_lex.leaf_tables,
599
605
                                    select_insert))
600
606
    return(true);
601
607
 
608
614
 
609
615
  SYNOPSIS
610
616
    mysql_prepare_insert()
611
 
    thd                 Thread handler
 
617
    session                     Thread handler
612
618
    table_list          Global/local table list
613
619
    table               Table to insert into (can be NULL if table should
614
620
                        be taken from table_list->table)    
634
640
    true  error
635
641
*/
636
642
 
637
 
bool mysql_prepare_insert(THD *thd, TableList *table_list,
 
643
bool mysql_prepare_insert(Session *session, TableList *table_list,
638
644
                          Table *table, List<Item> &fields, List_item *values,
639
645
                          List<Item> &update_fields, List<Item> &update_values,
640
646
                          enum_duplicates duplic,
642
648
                          bool select_insert,
643
649
                          bool check_fields, bool abort_on_warning)
644
650
{
645
 
  SELECT_LEX *select_lex= &thd->lex->select_lex;
 
651
  SELECT_LEX *select_lex= &session->lex->select_lex;
646
652
  Name_resolution_context *context= &select_lex->context;
647
653
  Name_resolution_context_state ctx_state;
648
654
  bool insert_into_view= (0 != 0);
675
681
  if (duplic == DUP_UPDATE)
676
682
  {
677
683
    /* it should be allocated before Item::fix_fields() */
678
 
    if (table_list->set_insert_values(thd->mem_root))
 
684
    if (table_list->set_insert_values(session->mem_root))
679
685
      return(true);
680
686
  }
681
687
 
682
 
  if (mysql_prepare_insert_check_table(thd, table_list, fields, select_insert))
 
688
  if (mysql_prepare_insert_check_table(session, table_list, fields, select_insert))
683
689
    return(true);
684
690
 
685
691
 
699
705
    table_list->next_local= 0;
700
706
    context->resolve_in_table_list_only(table_list);
701
707
 
702
 
    res= check_insert_fields(thd, context->table_list, fields, *values,
 
708
    res= check_insert_fields(session, context->table_list, fields, *values,
703
709
                             !insert_into_view, &map) ||
704
 
      setup_fields(thd, 0, *values, MARK_COLUMNS_READ, 0, 0);
 
710
      setup_fields(session, 0, *values, MARK_COLUMNS_READ, 0, 0);
705
711
 
706
712
    if (!res && check_fields)
707
713
    {
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, 
 
714
      bool saved_abort_on_warning= session->abort_on_warning;
 
715
      session->abort_on_warning= abort_on_warning;
 
716
      res= check_that_all_fields_are_given_values(session, 
711
717
                                                  table ? table : 
712
718
                                                  context->table_list->table,
713
719
                                                  context->table_list);
714
 
      thd->abort_on_warning= saved_abort_on_warning;
 
720
      session->abort_on_warning= saved_abort_on_warning;
715
721
    }
716
722
 
717
723
    if (!res && duplic == DUP_UPDATE)
718
724
    {
719
 
      res= check_update_fields(thd, context->table_list, update_fields, &map);
 
725
      res= check_update_fields(session, context->table_list, update_fields, &map);
720
726
    }
721
727
 
722
728
    /* Restore the current context. */
723
729
    ctx_state.restore_state(context, table_list);
724
730
 
725
731
    if (!res)
726
 
      res= setup_fields(thd, 0, update_values, MARK_COLUMNS_READ, 0, 0);
 
732
      res= setup_fields(session, 0, update_values, MARK_COLUMNS_READ, 0, 0);
727
733
  }
728
734
 
729
735
  if (res)
735
741
  if (!select_insert)
736
742
  {
737
743
    TableList *duplicate;
738
 
    if ((duplicate= unique_table(thd, table_list, table_list->next_global, 1)))
 
744
    if ((duplicate= unique_table(session, table_list, table_list->next_global, 1)))
739
745
    {
740
746
      update_non_unique_table_error(table_list, "INSERT", duplicate);
741
747
      return(true);
764
770
 
765
771
  SYNOPSIS
766
772
     write_record()
767
 
      thd   - thread context
 
773
      session   - thread context
768
774
      table - table to which record should be written
769
775
      info  - COPY_INFO structure describing handling of duplicates
770
776
              and which is used for counting number of records inserted
776
782
    then both on update triggers will work instead. Similarly both on
777
783
    delete triggers will be invoked if we will delete conflicting records.
778
784
 
779
 
    Sets thd->transaction.stmt.modified_non_trans_table to true if table which is updated didn't have
 
785
    Sets session->transaction.stmt.modified_non_trans_table to true if table which is updated didn't have
780
786
    transactions.
781
787
 
782
788
  RETURN VALUE
785
791
*/
786
792
 
787
793
 
788
 
int write_record(THD *thd, Table *table,COPY_INFO *info)
 
794
int write_record(Session *session, Table *table,COPY_INFO *info)
789
795
{
790
796
  int error;
791
797
  char *key=0;
808
814
        If we do more than one iteration of this loop, from the second one the
809
815
        row will have an explicit value in the autoinc field, which was set at
810
816
        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
 
817
        the autogenerated value to avoid session->insert_id_for_cur_row to become
812
818
        0.
813
819
      */
814
820
      if (table->file->insert_id_for_cur_row > 0)
887
893
        restore_record(table,record[1]);
888
894
        assert(info->update_fields->elements ==
889
895
                    info->update_values->elements);
890
 
        if (fill_record(thd, *info->update_fields,
 
896
        if (fill_record(session, *info->update_fields,
891
897
                                                 *info->update_values,
892
898
                                                 info->ignore))
893
899
          goto before_err;
922
928
            like a regular UPDATE statement: it should not affect the value of a
923
929
            next SELECT LAST_INSERT_ID() or mysql_insert_id().
924
930
            Except if LAST_INSERT_ID(#) was in the INSERT query, which is
925
 
            handled separately by THD::arg_of_last_insert_id_function.
 
931
            handled separately by Session::arg_of_last_insert_id_function.
926
932
          */
927
933
          insert_id_for_cur_row= table->file->insert_id_for_cur_row= 0;
928
934
          info->copied++;
964
970
            info->deleted++;
965
971
          else
966
972
            error= 0;
967
 
          thd->record_first_successful_insert_id_in_cur_stmt(table->file->insert_id_for_cur_row);
 
973
          session->record_first_successful_insert_id_in_cur_stmt(table->file->insert_id_for_cur_row);
968
974
          /*
969
975
            Since we pretend that we have done insert we should call
970
976
            its after triggers.
977
983
            goto err;
978
984
          info->deleted++;
979
985
          if (!table->file->has_transactions())
980
 
            thd->transaction.stmt.modified_non_trans_table= true;
 
986
            session->transaction.stmt.modified_non_trans_table= true;
981
987
          /* Let us attempt do write_row() once more */
982
988
        }
983
989
      }
984
990
    }
985
 
    thd->record_first_successful_insert_id_in_cur_stmt(table->file->insert_id_for_cur_row);
 
991
    session->record_first_successful_insert_id_in_cur_stmt(table->file->insert_id_for_cur_row);
986
992
    /*
987
993
      Restore column maps if they where replaced during an duplicate key
988
994
      problem.
1002
1008
 
1003
1009
after_n_copied_inc:
1004
1010
  info->copied++;
1005
 
  thd->record_first_successful_insert_id_in_cur_stmt(table->file->insert_id_for_cur_row);
 
1011
  session->record_first_successful_insert_id_in_cur_stmt(table->file->insert_id_for_cur_row);
1006
1012
 
1007
1013
gok_or_after_err:
1008
1014
  if (key)
1009
1015
    my_safe_afree(key,table->s->max_unique_length,MAX_KEY_LENGTH);
1010
1016
  if (!table->file->has_transactions())
1011
 
    thd->transaction.stmt.modified_non_trans_table= true;
 
1017
    session->transaction.stmt.modified_non_trans_table= true;
1012
1018
  return(0);
1013
1019
 
1014
1020
err:
1015
1021
  info->last_errno= error;
1016
1022
  /* 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
 
1023
  if (session->lex->current_select)
 
1024
    session->lex->current_select->no_error= 0;        // Give error
1019
1025
  table->file->print_error(error,MYF(0));
1020
1026
  
1021
1027
before_err:
1031
1037
  Check that all fields with arn't null_fields are used
1032
1038
******************************************************************************/
1033
1039
 
1034
 
int check_that_all_fields_are_given_values(THD *thd, Table *entry,
 
1040
int check_that_all_fields_are_given_values(Session *session, Table *entry,
1035
1041
                                           TableList *table_list)
1036
1042
{
1037
1043
  int err= 0;
1050
1056
        view= test(0);
1051
1057
      }
1052
1058
      {
1053
 
        push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
1059
        push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1054
1060
                            ER_NO_DEFAULT_FOR_FIELD,
1055
1061
                            ER(ER_NO_DEFAULT_FOR_FIELD),
1056
1062
                            (*field)->field_name);
1058
1064
      err= 1;
1059
1065
    }
1060
1066
  }
1061
 
  return thd->abort_on_warning ? err : 0;
 
1067
  return session->abort_on_warning ? err : 0;
1062
1068
}
1063
1069
 
1064
1070
/***************************************************************************
1071
1077
 
1072
1078
  SYNOPSIS
1073
1079
    mysql_insert_select_prepare()
1074
 
    thd         thread handler
 
1080
    session         thread handler
1075
1081
 
1076
1082
  RETURN
1077
1083
    false OK
1078
1084
    true  Error
1079
1085
*/
1080
1086
 
1081
 
bool mysql_insert_select_prepare(THD *thd)
 
1087
bool mysql_insert_select_prepare(Session *session)
1082
1088
{
1083
 
  LEX *lex= thd->lex;
 
1089
  LEX *lex= session->lex;
1084
1090
  SELECT_LEX *select_lex= &lex->select_lex;
1085
1091
  
1086
1092
 
1095
1101
  if (lex->current_select->select_limit)
1096
1102
  {
1097
1103
    lex->set_stmt_unsafe();
1098
 
    thd->set_current_stmt_binlog_row_based_if_mixed();
 
1104
    session->set_current_stmt_binlog_row_based_if_mixed();
1099
1105
  }
1100
1106
  /*
1101
1107
    SELECT_LEX do not belong to INSERT statement, so we can't add WHERE
1102
1108
    clause if table is VIEW
1103
1109
  */
1104
1110
  
1105
 
  if (mysql_prepare_insert(thd, lex->query_tables,
 
1111
  if (mysql_prepare_insert(session, lex->query_tables,
1106
1112
                           lex->query_tables->table, lex->field_list, 0,
1107
1113
                           lex->update_list, lex->value_list,
1108
1114
                           lex->duplicates,
1142
1148
int
1143
1149
select_insert::prepare(List<Item> &values, SELECT_LEX_UNIT *u)
1144
1150
{
1145
 
  LEX *lex= thd->lex;
 
1151
  LEX *lex= session->lex;
1146
1152
  int res;
1147
1153
  table_map map= 0;
1148
1154
  SELECT_LEX *lex_current_select_save= lex->current_select;
1156
1162
    we are fixing fields from insert list.
1157
1163
  */
1158
1164
  lex->current_select= &lex->select_lex;
1159
 
  res= check_insert_fields(thd, table_list, *fields, values,
 
1165
  res= check_insert_fields(session, table_list, *fields, values,
1160
1166
                           !insert_into_view, &map) ||
1161
 
       setup_fields(thd, 0, values, MARK_COLUMNS_READ, 0, 0);
 
1167
       setup_fields(session, 0, values, MARK_COLUMNS_READ, 0, 0);
1162
1168
 
1163
1169
  if (!res && fields->elements)
1164
1170
  {
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, 
 
1171
    bool saved_abort_on_warning= session->abort_on_warning;
 
1172
    session->abort_on_warning= !info.ignore;
 
1173
    res= check_that_all_fields_are_given_values(session, table_list->table, 
1168
1174
                                                table_list);
1169
 
    thd->abort_on_warning= saved_abort_on_warning;
 
1175
    session->abort_on_warning= saved_abort_on_warning;
1170
1176
  }
1171
1177
 
1172
1178
  if (info.handle_duplicates == DUP_UPDATE && !res)
1181
1187
    table_list->next_local= 0;
1182
1188
    context->resolve_in_table_list_only(table_list);
1183
1189
 
1184
 
    res= res || check_update_fields(thd, context->table_list,
 
1190
    res= res || check_update_fields(session, context->table_list,
1185
1191
                                    *info.update_fields, &map);
1186
1192
    /*
1187
1193
      When we are not using GROUP BY and there are no ungrouped aggregate functions 
1199
1205
      table_list->next_name_resolution_table= 
1200
1206
        ctx_state.get_first_name_resolution_table();
1201
1207
 
1202
 
    res= res || setup_fields(thd, 0, *info.update_values,
 
1208
    res= res || setup_fields(session, 0, *info.update_values,
1203
1209
                             MARK_COLUMNS_READ, 0, 0);
1204
1210
    if (!res)
1205
1211
    {
1236
1242
    Is table which we are changing used somewhere in other parts of
1237
1243
    query
1238
1244
  */
1239
 
  if (unique_table(thd, table_list, table_list->next_global, 0))
 
1245
  if (unique_table(session, table_list, table_list->next_global, 0))
1240
1246
  {
1241
1247
    /* Using same table for INSERT and SELECT */
1242
1248
    lex->current_select->options|= OPTION_BUFFER_RESULT;
1258
1264
  restore_record(table,s->default_values);              // Get empty record
1259
1265
  table->next_number_field=table->found_next_number_field;
1260
1266
 
1261
 
  if (thd->slave_thread &&
 
1267
  if (session->slave_thread &&
1262
1268
      (info.handle_duplicates == DUP_UPDATE) &&
1263
1269
      (table->next_number_field != NULL) &&
1264
1270
      rpl_master_has_bug(&active_mi->rli, 24432))
1265
1271
    return(1);
1266
1272
 
1267
 
  thd->cuted_fields=0;
 
1273
  session->cuted_fields=0;
1268
1274
  if (info.ignore || info.handle_duplicates != DUP_ERROR)
1269
1275
    table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
1270
1276
  if (info.handle_duplicates == DUP_REPLACE)
1271
1277
    table->file->extra(HA_EXTRA_WRITE_CAN_REPLACE);
1272
1278
  if (info.handle_duplicates == DUP_UPDATE)
1273
1279
    table->file->extra(HA_EXTRA_INSERT_WITH_UPDATE);
1274
 
  thd->abort_on_warning= !info.ignore;
 
1280
  session->abort_on_warning= !info.ignore;
1275
1281
  table->mark_columns_needed_for_insert();
1276
1282
 
1277
1283
 
1298
1304
int select_insert::prepare2(void)
1299
1305
{
1300
1306
  
1301
 
  if (thd->lex->current_select->options & OPTION_BUFFER_RESULT)
 
1307
  if (session->lex->current_select->options & OPTION_BUFFER_RESULT)
1302
1308
    table->file->ha_start_bulk_insert((ha_rows) 0);
1303
1309
  return(0);
1304
1310
}
1319
1325
    table->auto_increment_field_not_null= false;
1320
1326
    table->file->ha_reset();
1321
1327
  }
1322
 
  thd->count_cuted_fields= CHECK_FIELD_IGNORE;
1323
 
  thd->abort_on_warning= 0;
 
1328
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
 
1329
  session->abort_on_warning= 0;
1324
1330
  return;
1325
1331
}
1326
1332
 
1336
1342
    return(0);
1337
1343
  }
1338
1344
 
1339
 
  thd->count_cuted_fields= CHECK_FIELD_WARN;    // Calculate cuted fields
 
1345
  session->count_cuted_fields= CHECK_FIELD_WARN;        // Calculate cuted fields
1340
1346
  store_values(values);
1341
 
  thd->count_cuted_fields= CHECK_FIELD_IGNORE;
1342
 
  if (thd->is_error())
 
1347
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
 
1348
  if (session->is_error())
1343
1349
    return(1);
1344
1350
 
1345
 
  error= write_record(thd, table, &info);
 
1351
  error= write_record(session, table, &info);
1346
1352
    
1347
1353
  if (!error)
1348
1354
  {
1364
1370
        If no value has been autogenerated so far, we need to remember the
1365
1371
        value we just saw, we may need to send it to client in the end.
1366
1372
      */
1367
 
      if (thd->first_successful_insert_id_in_cur_stmt == 0) // optimization
 
1373
      if (session->first_successful_insert_id_in_cur_stmt == 0) // optimization
1368
1374
        autoinc_value_of_last_inserted_row= 
1369
1375
          table->next_number_field->val_int();
1370
1376
      /*
1381
1387
void select_insert::store_values(List<Item> &values)
1382
1388
{
1383
1389
  if (fields->elements)
1384
 
    fill_record(thd, *fields, values, 1);
 
1390
    fill_record(session, *fields, values, 1);
1385
1391
  else
1386
 
    fill_record(thd, table->field, values, 1);
 
1392
    fill_record(session, table->field, values, 1);
1387
1393
}
1388
1394
 
1389
1395
void select_insert::send_error(uint32_t errcode,const char *err)
1402
1408
  bool const trans_table= table->file->has_transactions();
1403
1409
  uint64_t id;
1404
1410
  bool changed;
1405
 
  THD::killed_state killed_status= thd->killed;
 
1411
  Session::killed_state killed_status= session->killed;
1406
1412
  
1407
1413
  error= table->file->ha_end_bulk_insert();
1408
1414
  table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
1414
1420
      We must invalidate the table in the query cache before binlog writing
1415
1421
      and ha_autocommit_or_rollback.
1416
1422
    */
1417
 
    if (thd->transaction.stmt.modified_non_trans_table)
1418
 
      thd->transaction.all.modified_non_trans_table= true;
 
1423
    if (session->transaction.stmt.modified_non_trans_table)
 
1424
      session->transaction.all.modified_non_trans_table= true;
1419
1425
  }
1420
1426
  assert(trans_table || !changed || 
1421
 
              thd->transaction.stmt.modified_non_trans_table);
 
1427
              session->transaction.stmt.modified_non_trans_table);
1422
1428
 
1423
1429
  /*
1424
1430
    Write to binlog before commiting transaction.  No statement will
1429
1435
  if (mysql_bin_log.is_open())
1430
1436
  {
1431
1437
    if (!error)
1432
 
      thd->clear_error();
1433
 
    thd->binlog_query(THD::ROW_QUERY_TYPE,
1434
 
                      thd->query, thd->query_length,
 
1438
      session->clear_error();
 
1439
    session->binlog_query(Session::ROW_QUERY_TYPE,
 
1440
                      session->query, session->query_length,
1435
1441
                      trans_table, false, killed_status);
1436
1442
  }
1437
1443
  table->file->ha_release_auto_increment();
1444
1450
  char buff[160];
1445
1451
  if (info.ignore)
1446
1452
    sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
1447
 
            (ulong) (info.records - info.copied), (ulong) thd->cuted_fields);
 
1453
            (ulong) (info.records - info.copied), (ulong) session->cuted_fields);
1448
1454
  else
1449
1455
    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) ?
 
1456
            (ulong) (info.deleted+info.updated), (ulong) session->cuted_fields);
 
1457
  session->row_count_func= info.copied + info.deleted +
 
1458
                       ((session->client_capabilities & CLIENT_FOUND_ROWS) ?
1453
1459
                        info.touched : info.updated);
1454
1460
 
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 :
 
1461
  id= (session->first_successful_insert_id_in_cur_stmt > 0) ?
 
1462
    session->first_successful_insert_id_in_cur_stmt :
 
1463
    (session->arg_of_last_insert_id_function ?
 
1464
     session->first_successful_insert_id_in_prev_stmt :
1459
1465
     (info.copied ? autoinc_value_of_last_inserted_row : 0));
1460
 
  ::my_ok(thd, (ulong) thd->row_count_func, id, buff);
 
1466
  ::my_ok(session, (ulong) session->row_count_func, id, buff);
1461
1467
  return(0);
1462
1468
}
1463
1469
 
1492
1498
    */
1493
1499
    changed= (info.copied || info.deleted || info.updated);
1494
1500
    transactional_table= table->file->has_transactions();
1495
 
    if (thd->transaction.stmt.modified_non_trans_table)
 
1501
    if (session->transaction.stmt.modified_non_trans_table)
1496
1502
    {
1497
1503
        if (mysql_bin_log.is_open())
1498
 
          thd->binlog_query(THD::ROW_QUERY_TYPE, thd->query, thd->query_length,
 
1504
          session->binlog_query(Session::ROW_QUERY_TYPE, session->query, session->query_length,
1499
1505
                            transactional_table, false);
1500
 
        if (!thd->current_stmt_binlog_row_based && !can_rollback_data())
1501
 
          thd->transaction.all.modified_non_trans_table= true;
 
1506
        if (!session->current_stmt_binlog_row_based && !can_rollback_data())
 
1507
          session->transaction.all.modified_non_trans_table= true;
1502
1508
    }
1503
1509
    assert(transactional_table || !changed ||
1504
 
                thd->transaction.stmt.modified_non_trans_table);
 
1510
                session->transaction.stmt.modified_non_trans_table);
1505
1511
    table->file->ha_release_auto_increment();
1506
1512
  }
1507
1513
 
1519
1525
 
1520
1526
  SYNOPSIS
1521
1527
    create_table_from_items()
1522
 
      thd          in     Thread object
 
1528
      session          in     Thread object
1523
1529
      create_info  in     Create information (like MAX_ROWS, ENGINE or
1524
1530
                          temporary table flag)
1525
1531
      create_table in     Pointer to TableList object providing database
1532
1538
      lock         out    Pointer to the DRIZZLE_LOCK object for table created
1533
1539
                          (or open temporary table) will be returned in this
1534
1540
                          parameter. Since this table is not included in
1535
 
                          THD::lock caller is responsible for explicitly
 
1541
                          Session::lock caller is responsible for explicitly
1536
1542
                          unlocking this table.
1537
1543
      hooks
1538
1544
 
1555
1561
    0         Error
1556
1562
*/
1557
1563
 
1558
 
static Table *create_table_from_items(THD *thd, HA_CREATE_INFO *create_info,
 
1564
static Table *create_table_from_items(Session *session, HA_CREATE_INFO *create_info,
1559
1565
                                      TableList *create_table,
1560
1566
                                      Alter_info *alter_info,
1561
1567
                                      List<Item> *items,
1579
1585
    if (create_info->options & HA_LEX_CREATE_IF_NOT_EXISTS)
1580
1586
    {
1581
1587
      create_info->table_existed= 1;            // Mark that table existed
1582
 
      push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
 
1588
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1583
1589
                          ER_TABLE_EXISTS_ERROR, ER(ER_TABLE_EXISTS_ERROR),
1584
1590
                          create_table->table_name);
1585
1591
      return(create_table->table);
1592
1598
  tmp_table.alias= 0;
1593
1599
  tmp_table.timestamp_field= 0;
1594
1600
  tmp_table.s= &share;
1595
 
  init_tmp_table_share(thd, &share, "", 0, "", "");
 
1601
  init_tmp_table_share(session, &share, "", 0, "", "");
1596
1602
 
1597
1603
  tmp_table.s->db_create_options=0;
1598
1604
  tmp_table.s->blob_ptr_size= portable_sizeof_char_ptr;
1612
1618
      else
1613
1619
        field= item->tmp_table_field_from_field_type(&tmp_table, 0);
1614
1620
    else
1615
 
      field= create_tmp_field(thd, &tmp_table, item, item->type(),
 
1621
      field= create_tmp_field(session, &tmp_table, item, item->type(),
1616
1622
                              (Item ***) 0, &tmp_field, &def_field, 0, 0, 0, 0,
1617
1623
                              0);
1618
1624
    if (!field ||
1642
1648
    open_table().
1643
1649
  */
1644
1650
  {
1645
 
    tmp_disable_binlog(thd);
1646
 
    if (!mysql_create_table_no_lock(thd, create_table->db,
 
1651
    tmp_disable_binlog(session);
 
1652
    if (!mysql_create_table_no_lock(session, create_table->db,
1647
1653
                                    create_table->table_name,
1648
1654
                                    create_info, alter_info, 0,
1649
 
                                    select_field_count))
 
1655
                                    select_field_count, true))
1650
1656
    {
1651
1657
      if (create_info->table_existed &&
1652
1658
          !(create_info->options & HA_LEX_CREATE_TMP_TABLE))
1663
1669
      if (!(create_info->options & HA_LEX_CREATE_TMP_TABLE))
1664
1670
      {
1665
1671
        pthread_mutex_lock(&LOCK_open);
1666
 
        if (reopen_name_locked_table(thd, create_table, false))
 
1672
        if (reopen_name_locked_table(session, create_table, false))
1667
1673
        {
1668
1674
          quick_rm_table(create_info->db_type, create_table->db,
1669
1675
                         table_case_name(create_info, create_table->table_name),
1675
1681
      }
1676
1682
      else
1677
1683
      {
1678
 
        if (!(table= open_table(thd, create_table, (bool*) 0,
 
1684
        if (!(table= open_table(session, create_table, (bool*) 0,
1679
1685
                                DRIZZLE_OPEN_TEMPORARY_ONLY)) &&
1680
1686
            !create_info->table_existed)
1681
1687
        {
1684
1690
            it preparable for open. But let us do close_temporary_table() here
1685
1691
            just in case.
1686
1692
          */
1687
 
          drop_temporary_table(thd, create_table);
 
1693
          drop_temporary_table(session, create_table);
1688
1694
        }
1689
1695
      }
1690
1696
    }
1691
 
    reenable_binlog(thd);
 
1697
    reenable_binlog(session);
1692
1698
    if (!table)                                   // open failed
1693
1699
      return(0);
1694
1700
  }
1695
1701
 
1696
1702
  table->reginfo.lock_type=TL_WRITE;
1697
1703
  hooks->prelock(&table, 1);                    // Call prelock hooks
1698
 
  if (! ((*lock)= mysql_lock_tables(thd, &table, 1,
 
1704
  if (! ((*lock)= mysql_lock_tables(session, &table, 1,
1699
1705
                                    DRIZZLE_LOCK_IGNORE_FLUSH, &not_used)) ||
1700
1706
        hooks->postlock(&table, 1))
1701
1707
  {
1702
1708
    if (*lock)
1703
1709
    {
1704
 
      mysql_unlock_tables(thd, *lock);
 
1710
      mysql_unlock_tables(session, *lock);
1705
1711
      *lock= 0;
1706
1712
    }
1707
1713
 
1708
1714
    if (!create_info->table_existed)
1709
 
      drop_open_table(thd, table, create_table->db, create_table->table_name);
 
1715
      drop_open_table(session, table, create_table->db, create_table->table_name);
1710
1716
    return(0);
1711
1717
  }
1712
1718
  return(table);
1750
1756
  private:
1751
1757
    virtual int do_postlock(Table **tables, uint32_t count)
1752
1758
    {
1753
 
      THD *thd= const_cast<THD*>(ptr->get_thd());
1754
 
      if (int error= decide_logging_format(thd, &all_tables))
 
1759
      Session *session= const_cast<Session*>(ptr->get_session());
 
1760
      if (int error= decide_logging_format(session, &all_tables))
1755
1761
        return error;
1756
1762
 
1757
1763
      Table const *const table = *tables;
1758
 
      if (thd->current_stmt_binlog_row_based  &&
 
1764
      if (session->current_stmt_binlog_row_based  &&
1759
1765
          !table->s->tmp_table &&
1760
1766
          !ptr->get_create_info()->table_existed)
1761
1767
      {
1778
1784
    row-based replication for the statement.  If we are creating a
1779
1785
    temporary table, we need to start a statement transaction.
1780
1786
  */
1781
 
  if ((thd->lex->create_info.options & HA_LEX_CREATE_TMP_TABLE) == 0 &&
1782
 
      thd->current_stmt_binlog_row_based)
 
1787
  if ((session->lex->create_info.options & HA_LEX_CREATE_TMP_TABLE) == 0 &&
 
1788
      session->current_stmt_binlog_row_based)
1783
1789
  {
1784
 
    thd->binlog_start_trans_and_stmt();
 
1790
    session->binlog_start_trans_and_stmt();
1785
1791
  }
1786
1792
 
1787
 
  if (!(table= create_table_from_items(thd, create_info, create_table,
 
1793
  if (!(table= create_table_from_items(session, create_info, create_table,
1788
1794
                                       alter_info, &values,
1789
1795
                                       &extra_lock, hook_ptr)))
1790
1796
    return(-1);                         // abort() deletes table
1796
1802
    if (create_info->options & HA_LEX_CREATE_TMP_TABLE)
1797
1803
      m_plock= &m_lock;
1798
1804
    else
1799
 
      m_plock= &thd->extra_lock;
 
1805
      m_plock= &session->extra_lock;
1800
1806
 
1801
1807
    *m_plock= extra_lock;
1802
1808
  }
1819
1825
  table->next_number_field=table->found_next_number_field;
1820
1826
 
1821
1827
  restore_record(table,s->default_values);      // Get empty record
1822
 
  thd->cuted_fields=0;
 
1828
  session->cuted_fields=0;
1823
1829
  if (info.ignore || info.handle_duplicates != DUP_ERROR)
1824
1830
    table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
1825
1831
  if (info.handle_duplicates == DUP_REPLACE)
1827
1833
  if (info.handle_duplicates == DUP_UPDATE)
1828
1834
    table->file->extra(HA_EXTRA_INSERT_WITH_UPDATE);
1829
1835
  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))
 
1836
  session->abort_on_warning= !info.ignore;
 
1837
  if (check_that_all_fields_are_given_values(session, table, table_list))
1832
1838
    return(1);
1833
1839
  table->mark_columns_needed_for_insert();
1834
1840
  table->file->extra(HA_EXTRA_WRITE_CACHE);
1855
1861
    schema that will do a close_thread_tables(), destroying the
1856
1862
    statement transaction cache.
1857
1863
  */
1858
 
  assert(thd->current_stmt_binlog_row_based);
 
1864
  assert(session->current_stmt_binlog_row_based);
1859
1865
  assert(tables && *tables && count > 0);
1860
1866
 
1861
1867
  char buf[2048];
1867
1873
  tmp_table_list.table = *tables;
1868
1874
  query.length(0);      // Have to zero it since constructor doesn't
1869
1875
 
1870
 
  result= store_create_info(thd, &tmp_table_list, &query, create_info);
 
1876
  result= store_create_info(session, &tmp_table_list, &query, create_info);
1871
1877
  assert(result == 0); /* store_create_info() always return 0 */
1872
1878
 
1873
 
  thd->binlog_query(THD::STMT_QUERY_TYPE,
 
1879
  session->binlog_query(Session::STMT_QUERY_TYPE,
1874
1880
                    query.ptr(), query.length(),
1875
1881
                    /* is_trans */ true,
1876
1882
                    /* suppress_use */ false);
1878
1884
 
1879
1885
void select_create::store_values(List<Item> &values)
1880
1886
{
1881
 
  fill_record(thd, field, values, 1);
 
1887
  fill_record(session, field, values, 1);
1882
1888
}
1883
1889
 
1884
1890
 
1897
1903
    written to the binary log.
1898
1904
 
1899
1905
  */
1900
 
  tmp_disable_binlog(thd);
 
1906
  tmp_disable_binlog(session);
1901
1907
  select_insert::send_error(errcode, err);
1902
 
  reenable_binlog(thd);
 
1908
  reenable_binlog(session);
1903
1909
 
1904
1910
  return;
1905
1911
}
1919
1925
    */
1920
1926
    if (!table->s->tmp_table)
1921
1927
    {
1922
 
      ha_autocommit_or_rollback(thd, 0);
1923
 
      end_active_trans(thd);
 
1928
      ha_autocommit_or_rollback(session, 0);
 
1929
      end_active_trans(session);
1924
1930
    }
1925
1931
 
1926
1932
    table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
1927
1933
    table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
1928
1934
    if (m_plock)
1929
1935
    {
1930
 
      mysql_unlock_tables(thd, *m_plock);
 
1936
      mysql_unlock_tables(session, *m_plock);
1931
1937
      *m_plock= NULL;
1932
1938
      m_plock= NULL;
1933
1939
    }
1955
1961
    of the table succeeded or not, since we need to reset the binary
1956
1962
    log state.
1957
1963
  */
1958
 
  tmp_disable_binlog(thd);
 
1964
  tmp_disable_binlog(session);
1959
1965
  select_insert::abort();
1960
 
  thd->transaction.stmt.modified_non_trans_table= false;
1961
 
  reenable_binlog(thd);
 
1966
  session->transaction.stmt.modified_non_trans_table= false;
 
1967
  reenable_binlog(session);
1962
1968
 
1963
1969
 
1964
1970
  if (m_plock)
1965
1971
  {
1966
 
    mysql_unlock_tables(thd, *m_plock);
 
1972
    mysql_unlock_tables(session, *m_plock);
1967
1973
    *m_plock= NULL;
1968
1974
    m_plock= NULL;
1969
1975
  }
1973
1979
    table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
1974
1980
    table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
1975
1981
    if (!create_info->table_existed)
1976
 
      drop_open_table(thd, table, create_table->db, create_table->table_name);
 
1982
      drop_open_table(session, table, create_table->db, create_table->table_name);
1977
1983
    table=0;                                    // Safety
1978
1984
  }
1979
1985
  return;