~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_insert.cc

  • Committer: Monty Taylor
  • Date: 2010-07-09 14:06:50 UTC
  • Revision ID: mordred@inaugust.com-20100709140650-ojeih829v3wbdkyv
Added include guard for generator.h to make cpplint happy.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
13
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
16
 
17
17
/* Insert of records */
18
18
 
19
 
#include <config.h>
 
19
#include "config.h"
20
20
#include <cstdio>
21
21
#include <drizzled/sql_select.h>
22
22
#include <drizzled/show.h>
25
25
#include <drizzled/probes.h>
26
26
#include <drizzled/sql_base.h>
27
27
#include <drizzled/sql_load.h>
28
 
#include <drizzled/field/epoch.h>
 
28
#include <drizzled/field/timestamp.h>
29
29
#include <drizzled/lock.h>
30
 
#include <drizzled/sql_table.h>
31
 
#include <drizzled/pthread_globals.h>
32
 
#include <drizzled/transaction_services.h>
33
 
#include <drizzled/plugin/transactional_storage_engine.h>
34
 
#include <drizzled/select_insert.h>
35
 
#include <drizzled/select_create.h>
36
 
 
37
 
#include <drizzled/table/shell.h>
 
30
#include "drizzled/sql_table.h"
 
31
#include "drizzled/pthread_globals.h"
 
32
#include "drizzled/transaction_services.h"
 
33
#include "drizzled/plugin/transactional_storage_engine.h"
38
34
 
39
35
namespace drizzled
40
36
{
87
83
  }
88
84
  else
89
85
  {                                             // Part field list
90
 
    Select_Lex *select_lex= &session->getLex()->select_lex;
 
86
    Select_Lex *select_lex= &session->lex->select_lex;
91
87
    Name_resolution_context *context= &select_lex->context;
92
88
    Name_resolution_context_state ctx_state;
93
89
    int res;
131
127
      }
132
128
      else
133
129
      {
134
 
        table->setWriteSet(table->timestamp_field->position());
 
130
        table->setWriteSet(table->timestamp_field->field_index);
135
131
      }
136
132
    }
137
133
  }
172
168
      Unmark the timestamp field so that we can check if this is modified
173
169
      by update_fields
174
170
    */
175
 
    timestamp_mark= table->write_set->test(table->timestamp_field->position());
176
 
    table->write_set->reset(table->timestamp_field->position());
 
171
    timestamp_mark= table->write_set->testAndClear(table->timestamp_field->field_index);
177
172
  }
178
173
 
179
174
  /* Check the fields we are going to modify */
191
186
 
192
187
    if (timestamp_mark)
193
188
    {
194
 
      table->setWriteSet(table->timestamp_field->position());
 
189
      table->setWriteSet(table->timestamp_field->field_index);
195
190
    }
196
191
  }
197
192
  return 0;
229
224
  end of dispatch_command().
230
225
*/
231
226
 
232
 
bool insert_query(Session *session,TableList *table_list,
 
227
bool mysql_insert(Session *session,TableList *table_list,
233
228
                  List<Item> &fields,
234
229
                  List<List_item> &values_list,
235
230
                  List<Item> &update_fields,
243
238
  uint32_t value_count;
244
239
  ulong counter = 1;
245
240
  uint64_t id;
246
 
  CopyInfo info;
 
241
  COPY_INFO info;
247
242
  Table *table= 0;
248
 
  List<List_item>::iterator its(values_list.begin());
 
243
  List_iterator_fast<List_item> its(values_list);
249
244
  List_item *values;
250
245
  Name_resolution_context *context;
251
246
  Name_resolution_context_state ctx_state;
273
268
  values= its++;
274
269
  value_count= values->elements;
275
270
 
276
 
  if (prepare_insert(session, table_list, table, fields, values,
 
271
  if (mysql_prepare_insert(session, table_list, table, fields, values,
277
272
                           update_fields, update_values, duplic, &unused_conds,
278
273
                           false,
279
274
                           (fields.elements || !value_count ||
280
275
                            (0) != 0), !ignore))
281
 
  {
282
 
    if (table != NULL)
283
 
      table->cursor->ha_release_auto_increment();
284
 
    if (!joins_freed)
285
 
      free_underlaid_joins(session, &session->getLex()->select_lex);
286
 
    session->setAbortOnWarning(false);
287
 
    DRIZZLE_INSERT_DONE(1, 0);
288
 
    return true;
289
 
  }
 
276
    goto abort;
290
277
 
291
278
  /* mysql_prepare_insert set table_list->table if it was not set */
292
279
  table= table_list->table;
293
280
 
294
 
  context= &session->getLex()->select_lex.context;
 
281
  context= &session->lex->select_lex.context;
295
282
  /*
296
283
    These three asserts test the hypothesis that the resetting of the name
297
284
    resolution context below is not necessary at all since the list of local
317
304
    if (values->elements != value_count)
318
305
    {
319
306
      my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), counter);
320
 
 
321
 
      if (table != NULL)
322
 
        table->cursor->ha_release_auto_increment();
323
 
      if (!joins_freed)
324
 
        free_underlaid_joins(session, &session->getLex()->select_lex);
325
 
      session->setAbortOnWarning(false);
326
 
      DRIZZLE_INSERT_DONE(1, 0);
327
 
 
328
 
      return true;
 
307
      goto abort;
329
308
    }
330
309
    if (setup_fields(session, 0, *values, MARK_COLUMNS_READ, 0, 0))
331
 
    {
332
 
      if (table != NULL)
333
 
        table->cursor->ha_release_auto_increment();
334
 
      if (!joins_freed)
335
 
        free_underlaid_joins(session, &session->getLex()->select_lex);
336
 
      session->setAbortOnWarning(false);
337
 
      DRIZZLE_INSERT_DONE(1, 0);
338
 
      return true;
339
 
    }
 
310
      goto abort;
340
311
  }
341
 
  its= values_list.begin();
 
312
  its.rewind ();
342
313
 
343
314
  /* Restore the current context. */
344
315
  ctx_state.restore_state(context, table_list);
346
317
  /*
347
318
    Fill in the given fields and dump it to the table cursor
348
319
  */
 
320
  memset(&info, 0, sizeof(info));
349
321
  info.ignore= ignore;
350
322
  info.handle_duplicates=duplic;
351
323
  info.update_fields= &update_fields;
374
346
  }
375
347
 
376
348
 
377
 
  session->setAbortOnWarning(not ignore);
 
349
  session->abort_on_warning= !ignore;
378
350
 
379
351
  table->mark_columns_needed_for_insert();
380
352
 
424
396
    session->row_count++;
425
397
  }
426
398
 
427
 
  free_underlaid_joins(session, &session->getLex()->select_lex);
 
399
  free_underlaid_joins(session, &session->lex->select_lex);
428
400
  joins_freed= true;
429
401
 
430
402
  /*
480
452
    table->cursor->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
481
453
 
482
454
  if (error)
483
 
  {
484
 
    if (table != NULL)
485
 
      table->cursor->ha_release_auto_increment();
486
 
    if (!joins_freed)
487
 
      free_underlaid_joins(session, &session->getLex()->select_lex);
488
 
    session->setAbortOnWarning(false);
489
 
    DRIZZLE_INSERT_DONE(1, 0);
490
 
    return true;
491
 
  }
492
 
 
 
455
    goto abort;
493
456
  if (values_list.elements == 1 && (!(session->options & OPTION_WARNINGS) ||
494
457
                                    !session->cuted_fields))
495
458
  {
496
459
    session->row_count_func= info.copied + info.deleted + info.updated;
497
 
    session->my_ok((ulong) session->rowCount(),
 
460
    session->my_ok((ulong) session->row_count_func,
498
461
                   info.copied + info.deleted + info.touched, id);
499
462
  }
500
463
  else
507
470
      snprintf(buff, sizeof(buff), ER(ER_INSERT_INFO), (ulong) info.records,
508
471
              (ulong) (info.deleted + info.updated), (ulong) session->cuted_fields);
509
472
    session->row_count_func= info.copied + info.deleted + info.updated;
510
 
    session->my_ok((ulong) session->rowCount(),
 
473
    session->my_ok((ulong) session->row_count_func,
511
474
                   info.copied + info.deleted + info.touched, id, buff);
512
475
  }
513
 
  session->status_var.inserted_row_count+= session->rowCount();
514
 
  session->setAbortOnWarning(false);
515
 
  DRIZZLE_INSERT_DONE(0, session->rowCount());
516
 
 
 
476
  session->abort_on_warning= 0;
 
477
  DRIZZLE_INSERT_DONE(0, session->row_count_func);
517
478
  return false;
 
479
 
 
480
abort:
 
481
  if (table != NULL)
 
482
    table->cursor->ha_release_auto_increment();
 
483
  if (!joins_freed)
 
484
    free_underlaid_joins(session, &session->lex->select_lex);
 
485
  session->abort_on_warning= 0;
 
486
  DRIZZLE_INSERT_DONE(1, 0);
 
487
  return true;
518
488
}
519
489
 
520
490
 
522
492
  Check if table can be updated
523
493
 
524
494
  SYNOPSIS
525
 
     prepare_insert_check_table()
 
495
     mysql_prepare_insert_check_table()
526
496
     session            Thread handle
527
497
     table_list         Table list
528
498
     fields             List of fields to be updated
534
504
     true  ERROR
535
505
*/
536
506
 
537
 
static bool prepare_insert_check_table(Session *session, TableList *table_list,
 
507
static bool mysql_prepare_insert_check_table(Session *session, TableList *table_list,
538
508
                                             List<Item> &,
539
509
                                             bool select_insert)
540
510
{
547
517
     than INSERT.
548
518
  */
549
519
 
550
 
  if (setup_tables_and_check_access(session, &session->getLex()->select_lex.context,
551
 
                                    &session->getLex()->select_lex.top_join_list,
 
520
  if (setup_tables_and_check_access(session, &session->lex->select_lex.context,
 
521
                                    &session->lex->select_lex.top_join_list,
552
522
                                    table_list,
553
 
                                    &session->getLex()->select_lex.leaf_tables,
 
523
                                    &session->lex->select_lex.leaf_tables,
554
524
                                    select_insert))
555
525
    return(true);
556
526
 
562
532
  Prepare items in INSERT statement
563
533
 
564
534
  SYNOPSIS
565
 
    prepare_insert()
 
535
    mysql_prepare_insert()
566
536
    session                     Thread handler
567
537
    table_list          Global/local table list
568
538
    table               Table to insert into (can be NULL if table should
589
559
    true  error
590
560
*/
591
561
 
592
 
bool prepare_insert(Session *session, TableList *table_list,
 
562
bool mysql_prepare_insert(Session *session, TableList *table_list,
593
563
                          Table *table, List<Item> &fields, List_item *values,
594
564
                          List<Item> &update_fields, List<Item> &update_values,
595
565
                          enum_duplicates duplic,
597
567
                          bool select_insert,
598
568
                          bool check_fields, bool abort_on_warning)
599
569
{
600
 
  Select_Lex *select_lex= &session->getLex()->select_lex;
 
570
  Select_Lex *select_lex= &session->lex->select_lex;
601
571
  Name_resolution_context *context= &select_lex->context;
602
572
  Name_resolution_context_state ctx_state;
603
573
  bool insert_into_view= (0 != 0);
612
582
    inserting (for INSERT ... SELECT this is done by changing table_list,
613
583
    because INSERT ... SELECT share Select_Lex it with SELECT.
614
584
  */
615
 
  if (not select_insert)
 
585
  if (!select_insert)
616
586
  {
617
587
    for (Select_Lex_Unit *un= select_lex->first_inner_unit();
618
588
         un;
634
604
      return(true);
635
605
  }
636
606
 
637
 
  if (prepare_insert_check_table(session, table_list, fields, select_insert))
 
607
  if (mysql_prepare_insert_check_table(session, table_list, fields, select_insert))
638
608
    return(true);
639
609
 
640
610
 
660
630
 
661
631
    if (!res && check_fields)
662
632
    {
663
 
      bool saved_abort_on_warning= session->abortOnWarning();
664
 
 
665
 
      session->setAbortOnWarning(abort_on_warning);
 
633
      bool saved_abort_on_warning= session->abort_on_warning;
 
634
      session->abort_on_warning= abort_on_warning;
666
635
      res= check_that_all_fields_are_given_values(session,
667
636
                                                  table ? table :
668
637
                                                  context->table_list->table,
669
638
                                                  context->table_list);
670
 
      session->setAbortOnWarning(saved_abort_on_warning);
 
639
      session->abort_on_warning= saved_abort_on_warning;
671
640
    }
672
641
 
673
642
    if (!res && duplic == DUP_UPDATE)
678
647
    /* Restore the current context. */
679
648
    ctx_state.restore_state(context, table_list);
680
649
 
681
 
    if (not res)
 
650
    if (!res)
682
651
      res= setup_fields(session, 0, update_values, MARK_COLUMNS_READ, 0, 0);
683
652
  }
684
653
 
685
654
  if (res)
686
655
    return(res);
687
656
 
688
 
  if (not table)
 
657
  if (!table)
689
658
    table= table_list->table;
690
659
 
691
 
  if (not select_insert)
 
660
  if (!select_insert)
692
661
  {
693
662
    TableList *duplicate;
694
663
    if ((duplicate= unique_table(table_list, table_list->next_global, true)))
698
667
      return true;
699
668
    }
700
669
  }
701
 
 
702
670
  if (duplic == DUP_UPDATE || duplic == DUP_REPLACE)
703
671
    table->prepare_for_position();
704
672
 
725
693
     write_record()
726
694
      session   - thread context
727
695
      table - table to which record should be written
728
 
      info  - CopyInfo structure describing handling of duplicates
 
696
      info  - COPY_INFO structure describing handling of duplicates
729
697
              and which is used for counting number of records inserted
730
698
              and deleted.
731
699
 
744
712
*/
745
713
 
746
714
 
747
 
int write_record(Session *session, Table *table,CopyInfo *info)
 
715
int write_record(Session *session, Table *table,COPY_INFO *info)
748
716
{
749
717
  int error;
750
 
  std::vector<unsigned char> key;
751
 
  boost::dynamic_bitset<> *save_read_set, *save_write_set;
 
718
  char *key=0;
 
719
  MyBitmap *save_read_set, *save_write_set;
752
720
  uint64_t prev_insert_id= table->cursor->next_insert_id;
753
721
  uint64_t insert_id_for_cur_row= 0;
754
722
 
759
727
 
760
728
  if (info->handle_duplicates == DUP_REPLACE || info->handle_duplicates == DUP_UPDATE)
761
729
  {
762
 
    while ((error=table->cursor->insertRecord(table->getInsertRecord())))
 
730
    while ((error=table->cursor->insertRecord(table->record[0])))
763
731
    {
764
732
      uint32_t key_nr;
765
733
      /*
807
775
        goto err;
808
776
      if (table->cursor->getEngine()->check_flag(HTON_BIT_DUPLICATE_POS))
809
777
      {
810
 
        if (table->cursor->rnd_pos(table->getUpdateRecord(),table->cursor->dup_ref))
 
778
        if (table->cursor->rnd_pos(table->record[1],table->cursor->dup_ref))
811
779
          goto err;
812
780
      }
813
781
      else
818
786
          goto err;
819
787
        }
820
788
 
821
 
        if (not key.size())
 
789
        if (!key)
822
790
        {
823
 
          key.resize(table->getShare()->max_unique_length);
 
791
          if (!(key=(char*) malloc(table->getShare()->max_unique_length)))
 
792
          {
 
793
            error=ENOMEM;
 
794
            goto err;
 
795
          }
824
796
        }
825
 
        key_copy(&key[0], table->getInsertRecord(), table->key_info+key_nr, 0);
826
 
        if ((error=(table->cursor->index_read_idx_map(table->getUpdateRecord(),key_nr,
827
 
                                                    &key[0], HA_WHOLE_KEY,
 
797
        key_copy((unsigned char*) key,table->record[0],table->key_info+key_nr,0);
 
798
        if ((error=(table->cursor->index_read_idx_map(table->record[1],key_nr,
 
799
                                                    (unsigned char*) key, HA_WHOLE_KEY,
828
800
                                                    HA_READ_KEY_EXACT))))
829
801
          goto err;
830
802
      }
835
807
          that matches, is updated. If update causes a conflict again,
836
808
          an error is returned
837
809
        */
838
 
        assert(table->insert_values.size());
 
810
        assert(table->insert_values != NULL);
839
811
        table->storeRecordAsInsert();
840
812
        table->restoreRecord();
841
813
        assert(info->update_fields->elements ==
850
822
          table->cursor->adjust_next_insert_id_after_explicit_value(
851
823
            table->next_number_field->val_int());
852
824
        info->touched++;
853
 
 
854
 
        if (! table->records_are_comparable() || table->compare_records())
 
825
        if ((table->cursor->getEngine()->check_flag(HTON_BIT_PARTIAL_COLUMN_READ) &&
 
826
             !bitmap_is_subset(table->write_set, table->read_set)) ||
 
827
            table->compare_record())
855
828
        {
856
 
          if ((error=table->cursor->updateRecord(table->getUpdateRecord(),
857
 
                                                table->getInsertRecord())) &&
 
829
          if ((error=table->cursor->updateRecord(table->record[1],
 
830
                                                table->record[0])) &&
858
831
              error != HA_ERR_RECORD_IS_THE_SAME)
859
832
          {
860
833
            if (info->ignore &&
872
845
          /*
873
846
            If ON DUP KEY UPDATE updates a row instead of inserting one, it's
874
847
            like a regular UPDATE statement: it should not affect the value of a
875
 
            next SELECT LAST_INSERT_ID() or insert_id().
 
848
            next SELECT LAST_INSERT_ID() or mysql_insert_id().
876
849
            Except if LAST_INSERT_ID(#) was in the INSERT query, which is
877
850
            handled separately by Session::arg_of_last_insert_id_function.
878
851
          */
908
881
            (table->timestamp_field_type == TIMESTAMP_NO_AUTO_SET ||
909
882
             table->timestamp_field_type == TIMESTAMP_AUTO_SET_ON_BOTH))
910
883
        {
911
 
          if ((error=table->cursor->updateRecord(table->getUpdateRecord(),
912
 
                                                table->getInsertRecord())) &&
 
884
          if ((error=table->cursor->updateRecord(table->record[1],
 
885
                                                table->record[0])) &&
913
886
              error != HA_ERR_RECORD_IS_THE_SAME)
914
887
            goto err;
915
888
          if (error != HA_ERR_RECORD_IS_THE_SAME)
925
898
        }
926
899
        else
927
900
        {
928
 
          if ((error=table->cursor->deleteRecord(table->getUpdateRecord())))
 
901
          if ((error=table->cursor->deleteRecord(table->record[1])))
929
902
            goto err;
930
903
          info->deleted++;
931
904
          if (!table->cursor->has_transactions())
941
914
    */
942
915
    if (table->read_set != save_read_set ||
943
916
        table->write_set != save_write_set)
944
 
      table->column_bitmaps_set(*save_read_set, *save_write_set);
 
917
      table->column_bitmaps_set(save_read_set, save_write_set);
945
918
  }
946
 
  else if ((error=table->cursor->insertRecord(table->getInsertRecord())))
 
919
  else if ((error=table->cursor->insertRecord(table->record[0])))
947
920
  {
948
921
    if (!info->ignore ||
949
922
        table->cursor->is_fatal_error(error, HA_CHECK_DUP))
957
930
  session->record_first_successful_insert_id_in_cur_stmt(table->cursor->insert_id_for_cur_row);
958
931
 
959
932
gok_or_after_err:
 
933
  if (key)
 
934
    free(key);
960
935
  if (!table->cursor->has_transactions())
961
936
    session->transaction.stmt.markModifiedNonTransData();
962
937
  return(0);
964
939
err:
965
940
  info->last_errno= error;
966
941
  /* current_select is NULL if this is a delayed insert */
967
 
  if (session->getLex()->current_select)
968
 
    session->getLex()->current_select->no_error= 0;        // Give error
 
942
  if (session->lex->current_select)
 
943
    session->lex->current_select->no_error= 0;        // Give error
969
944
  table->print_error(error,MYF(0));
970
945
 
971
946
before_err:
972
947
  table->cursor->restore_auto_increment(prev_insert_id);
973
 
  table->column_bitmaps_set(*save_read_set, *save_write_set);
974
 
  return 1;
 
948
  if (key)
 
949
    free(key);
 
950
  table->column_bitmaps_set(save_read_set, save_write_set);
 
951
  return(1);
975
952
}
976
953
 
977
954
 
1018
995
      }
1019
996
    }
1020
997
  }
1021
 
  return session->abortOnWarning() ? err : 0;
 
998
  return session->abort_on_warning ? err : 0;
1022
999
}
1023
1000
 
1024
1001
/***************************************************************************
1030
1007
  make insert specific preparation and checks after opening tables
1031
1008
 
1032
1009
  SYNOPSIS
1033
 
    insert_select_prepare()
 
1010
    mysql_insert_select_prepare()
1034
1011
    session         thread handler
1035
1012
 
1036
1013
  RETURN
1038
1015
    true  Error
1039
1016
*/
1040
1017
 
1041
 
bool insert_select_prepare(Session *session)
 
1018
bool mysql_insert_select_prepare(Session *session)
1042
1019
{
1043
 
  LEX *lex= session->getLex();
 
1020
  LEX *lex= session->lex;
1044
1021
  Select_Lex *select_lex= &lex->select_lex;
1045
1022
 
1046
1023
  /*
1048
1025
    clause if table is VIEW
1049
1026
  */
1050
1027
 
1051
 
  if (prepare_insert(session, lex->query_tables,
 
1028
  if (mysql_prepare_insert(session, lex->query_tables,
1052
1029
                           lex->query_tables->table, lex->field_list, 0,
1053
1030
                           lex->update_list, lex->value_list,
1054
1031
                           lex->duplicates,
1072
1049
                             List<Item> *update_fields,
1073
1050
                             List<Item> *update_values,
1074
1051
                             enum_duplicates duplic,
1075
 
                             bool ignore_check_option_errors) :
1076
 
  table_list(table_list_par), table(table_par), fields(fields_par),
1077
 
  autoinc_value_of_last_inserted_row(0),
1078
 
  insert_into_view(table_list_par && 0 != 0)
 
1052
                             bool ignore_check_option_errors)
 
1053
  :table_list(table_list_par), table(table_par), fields(fields_par),
 
1054
   autoinc_value_of_last_inserted_row(0),
 
1055
   insert_into_view(table_list_par && 0 != 0)
1079
1056
{
 
1057
  memset(&info, 0, sizeof(info));
1080
1058
  info.handle_duplicates= duplic;
1081
1059
  info.ignore= ignore_check_option_errors;
1082
1060
  info.update_fields= update_fields;
1087
1065
int
1088
1066
select_insert::prepare(List<Item> &values, Select_Lex_Unit *u)
1089
1067
{
 
1068
  LEX *lex= session->lex;
1090
1069
  int res;
1091
1070
  table_map map= 0;
1092
 
  Select_Lex *lex_current_select_save= session->getLex()->current_select;
 
1071
  Select_Lex *lex_current_select_save= lex->current_select;
1093
1072
 
1094
1073
 
1095
1074
  unit= u;
1099
1078
    select, LEX::current_select should point to the first select while
1100
1079
    we are fixing fields from insert list.
1101
1080
  */
1102
 
  session->getLex()->current_select= &session->getLex()->select_lex;
 
1081
  lex->current_select= &lex->select_lex;
1103
1082
  res= check_insert_fields(session, table_list, *fields, values,
1104
1083
                           !insert_into_view, &map) ||
1105
1084
       setup_fields(session, 0, values, MARK_COLUMNS_READ, 0, 0);
1106
1085
 
1107
1086
  if (!res && fields->elements)
1108
1087
  {
1109
 
    bool saved_abort_on_warning= session->abortOnWarning();
1110
 
    session->setAbortOnWarning(not info.ignore);
 
1088
    bool saved_abort_on_warning= session->abort_on_warning;
 
1089
    session->abort_on_warning= !info.ignore;
1111
1090
    res= check_that_all_fields_are_given_values(session, table_list->table,
1112
1091
                                                table_list);
1113
 
    session->setAbortOnWarning(saved_abort_on_warning);
 
1092
    session->abort_on_warning= saved_abort_on_warning;
1114
1093
  }
1115
1094
 
1116
1095
  if (info.handle_duplicates == DUP_UPDATE && !res)
1117
1096
  {
1118
 
    Name_resolution_context *context= &session->getLex()->select_lex.context;
 
1097
    Name_resolution_context *context= &lex->select_lex.context;
1119
1098
    Name_resolution_context_state ctx_state;
1120
1099
 
1121
1100
    /* Save the state of the current name resolution context. */
1133
1112
      We use next_name_resolution_table descructively, so check it first (views?)
1134
1113
    */
1135
1114
    assert (!table_list->next_name_resolution_table);
1136
 
    if (session->getLex()->select_lex.group_list.elements == 0 and
1137
 
        not session->getLex()->select_lex.with_sum_func)
 
1115
    if (lex->select_lex.group_list.elements == 0 &&
 
1116
        !lex->select_lex.with_sum_func)
1138
1117
      /*
1139
1118
        We must make a single context out of the two separate name resolution contexts :
1140
1119
        the INSERT table and the tables in the SELECT part of INSERT ... SELECT.
1153
1132
        order to get correct values from those fields when the select
1154
1133
        employs a temporary table.
1155
1134
      */
1156
 
      List<Item>::iterator li(info.update_values->begin());
 
1135
      List_iterator<Item> li(*info.update_values);
1157
1136
      Item *item;
1158
1137
 
1159
1138
      while ((item= li++))
1160
1139
      {
1161
1140
        item->transform(&Item::update_value_transformer,
1162
 
                        (unsigned char*)session->getLex()->current_select);
 
1141
                        (unsigned char*)lex->current_select);
1163
1142
      }
1164
1143
    }
1165
1144
 
1167
1146
    ctx_state.restore_state(context, table_list);
1168
1147
  }
1169
1148
 
1170
 
  session->getLex()->current_select= lex_current_select_save;
 
1149
  lex->current_select= lex_current_select_save;
1171
1150
  if (res)
1172
1151
    return(1);
1173
1152
  /*
1183
1162
  if (unique_table(table_list, table_list->next_global))
1184
1163
  {
1185
1164
    /* Using same table for INSERT and SELECT */
1186
 
    session->getLex()->current_select->options|= OPTION_BUFFER_RESULT;
1187
 
    session->getLex()->current_select->join->select_options|= OPTION_BUFFER_RESULT;
 
1165
    lex->current_select->options|= OPTION_BUFFER_RESULT;
 
1166
    lex->current_select->join->select_options|= OPTION_BUFFER_RESULT;
1188
1167
  }
1189
 
  else if (not (session->getLex()->current_select->options & OPTION_BUFFER_RESULT))
 
1168
  else if (!(lex->current_select->options & OPTION_BUFFER_RESULT))
1190
1169
  {
1191
1170
    /*
1192
1171
      We must not yet prepare the result table if it is the same as one of the
1203
1182
  table->next_number_field=table->found_next_number_field;
1204
1183
 
1205
1184
  session->cuted_fields=0;
1206
 
 
1207
1185
  if (info.ignore || info.handle_duplicates != DUP_ERROR)
1208
1186
    table->cursor->extra(HA_EXTRA_IGNORE_DUP_KEY);
1209
 
 
1210
1187
  if (info.handle_duplicates == DUP_REPLACE)
1211
1188
    table->cursor->extra(HA_EXTRA_WRITE_CAN_REPLACE);
1212
 
 
1213
1189
  if (info.handle_duplicates == DUP_UPDATE)
1214
1190
    table->cursor->extra(HA_EXTRA_INSERT_WITH_UPDATE);
1215
 
 
1216
 
  session->setAbortOnWarning(not info.ignore);
 
1191
  session->abort_on_warning= !info.ignore;
1217
1192
  table->mark_columns_needed_for_insert();
1218
1193
 
1219
1194
 
1239
1214
 
1240
1215
int select_insert::prepare2(void)
1241
1216
{
1242
 
  if (session->getLex()->current_select->options & OPTION_BUFFER_RESULT)
 
1217
 
 
1218
  if (session->lex->current_select->options & OPTION_BUFFER_RESULT)
1243
1219
    table->cursor->ha_start_bulk_insert((ha_rows) 0);
1244
 
 
1245
1220
  return(0);
1246
1221
}
1247
1222
 
1262
1237
    table->cursor->ha_reset();
1263
1238
  }
1264
1239
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
1265
 
  session->setAbortOnWarning(false);
 
1240
  session->abort_on_warning= 0;
1266
1241
  return;
1267
1242
}
1268
1243
 
1270
1245
bool select_insert::send_data(List<Item> &values)
1271
1246
{
1272
1247
 
1273
 
  bool error= false;
 
1248
  bool error=0;
1274
1249
 
1275
1250
  if (unit->offset_limit_cnt)
1276
1251
  {                                             // using limit offset,count
1277
1252
    unit->offset_limit_cnt--;
1278
 
    return false;
 
1253
    return(0);
1279
1254
  }
1280
1255
 
1281
1256
  session->count_cuted_fields= CHECK_FIELD_WARN;        // Calculate cuted fields
1282
1257
  store_values(values);
1283
1258
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
1284
1259
  if (session->is_error())
1285
 
    return true;
 
1260
    return(1);
1286
1261
 
1287
1262
  // Release latches in case bulk insert takes a long time
1288
1263
  plugin::TransactionalStorageEngine::releaseTemporaryLatches(session);
1332
1307
    fill_record(session, table->getFields(), values, true);
1333
1308
}
1334
1309
 
1335
 
void select_insert::send_error(drizzled::error_t errcode,const char *err)
 
1310
void select_insert::send_error(uint32_t errcode,const char *err)
1336
1311
{
 
1312
 
 
1313
 
1337
1314
  my_message(errcode, err, MYF(0));
 
1315
 
 
1316
  return;
1338
1317
}
1339
1318
 
1340
1319
 
1383
1362
    (session->arg_of_last_insert_id_function ?
1384
1363
     session->first_successful_insert_id_in_prev_stmt :
1385
1364
     (info.copied ? autoinc_value_of_last_inserted_row : 0));
1386
 
  session->my_ok((ulong) session->rowCount(),
 
1365
  session->my_ok((ulong) session->row_count_func,
1387
1366
                 info.copied + info.deleted + info.touched, id, buff);
1388
 
  session->status_var.inserted_row_count+= session->rowCount(); 
1389
 
  DRIZZLE_INSERT_SELECT_DONE(0, session->rowCount());
 
1367
  DRIZZLE_INSERT_SELECT_DONE(0, session->row_count_func);
1390
1368
  return 0;
1391
1369
}
1392
1370
 
1455
1433
      items        in     List of items which should be used to produce rest
1456
1434
                          of fields for the table (corresponding fields will
1457
1435
                          be added to the end of alter_info->create_list)
1458
 
      lock         out    Pointer to the DrizzleLock object for table created
 
1436
      lock         out    Pointer to the DRIZZLE_LOCK object for table created
1459
1437
                          (or open temporary table) will be returned in this
1460
1438
                          parameter. Since this table is not included in
1461
1439
                          Session::lock caller is responsible for explicitly
1487
1465
                                      AlterInfo *alter_info,
1488
1466
                                      List<Item> *items,
1489
1467
                                      bool is_if_not_exists,
1490
 
                                      DrizzleLock **lock,
1491
 
                                      identifier::Table::const_reference identifier)
 
1468
                                      DRIZZLE_LOCK **lock,
 
1469
                                      TableIdentifier &identifier)
1492
1470
{
 
1471
  Table tmp_table;              // Used during 'CreateField()'
1493
1472
  TableShare share(message::Table::INTERNAL);
 
1473
  Table *table= 0;
1494
1474
  uint32_t select_field_count= items->elements;
1495
1475
  /* Add selected items to field list */
1496
 
  List<Item>::iterator it(items->begin());
 
1476
  List_iterator_fast<Item> it(*items);
1497
1477
  Item *item;
1498
1478
  Field *tmp_field;
 
1479
  bool not_used;
1499
1480
 
1500
1481
  if (not (identifier.isTmp()) && create_table->table->db_stat)
1501
1482
  {
1505
1486
      create_info->table_existed= 1;            // Mark that table existed
1506
1487
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1507
1488
                          ER_TABLE_EXISTS_ERROR, ER(ER_TABLE_EXISTS_ERROR),
1508
 
                          create_table->getTableName());
 
1489
                          create_table->table_name);
1509
1490
      return create_table->table;
1510
1491
    }
1511
1492
 
1512
 
    my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_table->getTableName());
 
1493
    my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_table->table_name);
1513
1494
    return NULL;
1514
1495
  }
1515
1496
 
 
1497
  tmp_table.alias= 0;
 
1498
  tmp_table.timestamp_field= 0;
 
1499
  tmp_table.setShare(&share);
 
1500
 
 
1501
  tmp_table.getMutableShare()->db_create_options= 0;
 
1502
  tmp_table.getMutableShare()->blob_ptr_size= portable_sizeof_char_ptr;
 
1503
 
 
1504
  if (not table_proto.engine().name().compare("MyISAM"))
 
1505
    tmp_table.getMutableShare()->db_low_byte_first= true;
 
1506
  else if (not table_proto.engine().name().compare("MEMORY"))
 
1507
    tmp_table.getMutableShare()->db_low_byte_first= true;
 
1508
 
 
1509
  tmp_table.null_row= false;
 
1510
  tmp_table.maybe_null= false;
 
1511
 
 
1512
  while ((item=it++))
1516
1513
  {
1517
 
    table::Shell tmp_table(share);              // Used during 'CreateField()'
1518
 
 
1519
 
    if (not table_proto.engine().name().compare("MyISAM"))
1520
 
      tmp_table.getMutableShare()->db_low_byte_first= true;
1521
 
    else if (not table_proto.engine().name().compare("MEMORY"))
1522
 
      tmp_table.getMutableShare()->db_low_byte_first= true;
1523
 
 
1524
 
    tmp_table.in_use= session;
1525
 
 
1526
 
    while ((item=it++))
1527
 
    {
1528
 
      CreateField *cr_field;
1529
 
      Field *field, *def_field;
1530
 
      if (item->type() == Item::FUNC_ITEM)
1531
 
      {
1532
 
        if (item->result_type() != STRING_RESULT)
1533
 
        {
1534
 
          field= item->tmp_table_field(&tmp_table);
1535
 
        }
1536
 
        else
1537
 
        {
1538
 
          field= item->tmp_table_field_from_field_type(&tmp_table, 0);
1539
 
        }
1540
 
      }
 
1514
    CreateField *cr_field;
 
1515
    Field *field, *def_field;
 
1516
    if (item->type() == Item::FUNC_ITEM)
 
1517
      if (item->result_type() != STRING_RESULT)
 
1518
        field= item->tmp_table_field(&tmp_table);
1541
1519
      else
1542
 
      {
1543
 
        field= create_tmp_field(session, &tmp_table, item, item->type(),
1544
 
                                (Item ***) 0, &tmp_field, &def_field, false,
1545
 
                                false, false, 0);
1546
 
      }
1547
 
 
1548
 
      if (!field ||
1549
 
          !(cr_field=new CreateField(field,(item->type() == Item::FIELD_ITEM ?
1550
 
                                            ((Item_field *)item)->field :
1551
 
                                            (Field*) 0))))
1552
 
      {
1553
 
        return NULL;
1554
 
      }
1555
 
 
1556
 
      if (item->maybe_null)
1557
 
      {
1558
 
        cr_field->flags &= ~NOT_NULL_FLAG;
1559
 
      }
1560
 
 
1561
 
      alter_info->create_list.push_back(cr_field);
1562
 
    }
 
1520
        field= item->tmp_table_field_from_field_type(&tmp_table, 0);
 
1521
    else
 
1522
      field= create_tmp_field(session, &tmp_table, item, item->type(),
 
1523
                              (Item ***) 0, &tmp_field, &def_field, false,
 
1524
                              false, false, 0);
 
1525
    if (!field ||
 
1526
        !(cr_field=new CreateField(field,(item->type() == Item::FIELD_ITEM ?
 
1527
                                           ((Item_field *)item)->field :
 
1528
                                           (Field*) 0))))
 
1529
      return NULL;
 
1530
    if (item->maybe_null)
 
1531
      cr_field->flags &= ~NOT_NULL_FLAG;
 
1532
    alter_info->create_list.push_back(cr_field);
1563
1533
  }
1564
1534
 
1565
1535
  /*
1569
1539
    creating base table on which name we have exclusive lock. So code below
1570
1540
    should not cause deadlocks or races.
1571
1541
  */
1572
 
  Table *table= 0;
1573
1542
  {
1574
 
    if (not create_table_no_lock(session,
1575
 
                                 identifier,
1576
 
                                 create_info,
1577
 
                                 table_proto,
1578
 
                                 alter_info,
1579
 
                                 false,
1580
 
                                 select_field_count,
1581
 
                                 is_if_not_exists))
 
1543
    if (not mysql_create_table_no_lock(session,
 
1544
                                       identifier,
 
1545
                                       create_info,
 
1546
                                       table_proto,
 
1547
                                       alter_info,
 
1548
                                       false,
 
1549
                                       select_field_count,
 
1550
                                       is_if_not_exists))
1582
1551
    {
1583
1552
      if (create_info->table_existed && not identifier.isTmp())
1584
1553
      {
1587
1556
          or it was created via different mysqld front-end to the
1588
1557
          cluster. We don't have much options but throw an error.
1589
1558
        */
1590
 
        my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_table->getTableName());
 
1559
        my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_table->table_name);
1591
1560
        return NULL;
1592
1561
      }
1593
1562
 
1594
1563
      if (not identifier.isTmp())
1595
1564
      {
1596
 
        /* CREATE TABLE... has found that the table already exists for insert and is adapting to use it */
1597
 
        boost::mutex::scoped_lock scopedLock(table::Cache::singleton().mutex());
1598
 
 
1599
 
        if (create_table->table)
 
1565
        pthread_mutex_lock(&LOCK_open); /* CREATE TABLE... has found that the table already exists for insert and is adapting to use it */
 
1566
        if (session->reopen_name_locked_table(create_table, false))
1600
1567
        {
1601
 
          table::Concurrent *concurrent_table= static_cast<table::Concurrent *>(create_table->table);
1602
 
 
1603
 
          if (concurrent_table->reopen_name_locked_table(create_table, session))
1604
 
          {
1605
 
            (void)plugin::StorageEngine::dropTable(*session, identifier);
1606
 
          }
1607
 
          else
1608
 
          {
1609
 
            table= create_table->table;
1610
 
          }
 
1568
          quick_rm_table(*session, identifier);
1611
1569
        }
1612
1570
        else
1613
 
        {
1614
 
          (void)plugin::StorageEngine::dropTable(*session, identifier);
1615
 
        }
 
1571
          table= create_table->table;
 
1572
        pthread_mutex_unlock(&LOCK_open);
1616
1573
      }
1617
1574
      else
1618
1575
      {
1625
1582
            it preparable for open. But let us do close_temporary_table() here
1626
1583
            just in case.
1627
1584
          */
1628
 
          session->drop_temporary_table(identifier);
 
1585
          session->drop_temporary_table(create_table);
1629
1586
        }
1630
1587
      }
1631
1588
    }
1632
 
    if (not table)                                   // open failed
 
1589
    if (!table)                                   // open failed
1633
1590
      return NULL;
1634
1591
  }
1635
1592
 
1636
1593
  table->reginfo.lock_type=TL_WRITE;
1637
 
  if (not ((*lock)= session->lockTables(&table, 1, DRIZZLE_LOCK_IGNORE_FLUSH)))
 
1594
  if (! ((*lock)= mysql_lock_tables(session, &table, 1,
 
1595
                                    DRIZZLE_LOCK_IGNORE_FLUSH, &not_used)))
1638
1596
  {
1639
1597
    if (*lock)
1640
1598
    {
1641
 
      session->unlockTables(*lock);
 
1599
      mysql_unlock_tables(session, *lock);
1642
1600
      *lock= 0;
1643
1601
    }
1644
1602
 
1645
1603
    if (not create_info->table_existed)
1646
1604
      session->drop_open_table(table, identifier);
1647
 
 
1648
1605
    return NULL;
1649
1606
  }
1650
1607
 
1655
1612
int
1656
1613
select_create::prepare(List<Item> &values, Select_Lex_Unit *u)
1657
1614
{
1658
 
  DrizzleLock *extra_lock= NULL;
 
1615
  DRIZZLE_LOCK *extra_lock= NULL;
1659
1616
  /*
1660
1617
    For replication, the CREATE-SELECT statement is written
1661
1618
    in two pieces: the first transaction messsage contains 
1700
1657
 
1701
1658
  /* Mark all fields that are given values */
1702
1659
  for (Field **f= field ; *f ; f++)
1703
 
  {
1704
 
    table->setWriteSet((*f)->position());
1705
 
  }
 
1660
    table->setWriteSet((*f)->field_index);
1706
1661
 
1707
1662
  /* Don't set timestamp if used */
1708
1663
  table->timestamp_field_type= TIMESTAMP_NO_AUTO_SET;
1712
1667
  session->cuted_fields=0;
1713
1668
  if (info.ignore || info.handle_duplicates != DUP_ERROR)
1714
1669
    table->cursor->extra(HA_EXTRA_IGNORE_DUP_KEY);
1715
 
 
1716
1670
  if (info.handle_duplicates == DUP_REPLACE)
1717
1671
    table->cursor->extra(HA_EXTRA_WRITE_CAN_REPLACE);
1718
 
 
1719
1672
  if (info.handle_duplicates == DUP_UPDATE)
1720
1673
    table->cursor->extra(HA_EXTRA_INSERT_WITH_UPDATE);
1721
 
 
1722
1674
  table->cursor->ha_start_bulk_insert((ha_rows) 0);
1723
 
  session->setAbortOnWarning(not info.ignore);
 
1675
  session->abort_on_warning= !info.ignore;
1724
1676
  if (check_that_all_fields_are_given_values(session, table, table_list))
1725
1677
    return(1);
1726
 
 
1727
1678
  table->mark_columns_needed_for_insert();
1728
1679
  table->cursor->extra(HA_EXTRA_WRITE_CACHE);
1729
1680
  return(0);
1735
1686
}
1736
1687
 
1737
1688
 
1738
 
void select_create::send_error(drizzled::error_t errcode,const char *err)
 
1689
void select_create::send_error(uint32_t errcode,const char *err)
1739
1690
{
1740
1691
  /*
1741
1692
    This will execute any rollbacks that are necessary before writing
1749
1700
 
1750
1701
  */
1751
1702
  select_insert::send_error(errcode, err);
 
1703
 
 
1704
  return;
1752
1705
}
1753
1706
 
1754
1707
 
1767
1720
    if (!table->getShare()->getType())
1768
1721
    {
1769
1722
      TransactionServices &transaction_services= TransactionServices::singleton();
1770
 
      transaction_services.autocommitOrRollback(*session, 0);
 
1723
      transaction_services.autocommitOrRollback(session, 0);
1771
1724
      (void) session->endActiveTransaction();
1772
1725
    }
1773
1726
 
1775
1728
    table->cursor->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
1776
1729
    if (m_plock)
1777
1730
    {
1778
 
      session->unlockTables(*m_plock);
 
1731
      mysql_unlock_tables(session, *m_plock);
1779
1732
      *m_plock= NULL;
1780
1733
      m_plock= NULL;
1781
1734
    }
1805
1758
 
1806
1759
  if (m_plock)
1807
1760
  {
1808
 
    session->unlockTables(*m_plock);
 
1761
    mysql_unlock_tables(session, *m_plock);
1809
1762
    *m_plock= NULL;
1810
1763
    m_plock= NULL;
1811
1764
  }