~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_insert.cc

  • Committer: Mark Atwood
  • Date: 2011-08-12 04:08:33 UTC
  • mfrom: (2385.2.17 refactor5)
  • Revision ID: me@mark.atwood.name-20110812040833-u6j85nc6ahuc0dtz
mergeĀ lp:~olafvdspek/drizzle/refactor5

Show diffs side-by-side

added added

removed removed

Lines of Context:
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>
27
27
#include <drizzled/sql_load.h>
28
28
#include <drizzled/field/epoch.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
 
 
35
 
#include "drizzled/table/shell.h"
36
 
 
37
 
namespace drizzled
38
 
{
 
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
#include <drizzled/table/shell.h>
 
37
#include <drizzled/alter_info.h>
 
38
#include <drizzled/sql_parse.h>
 
39
#include <drizzled/sql_lex.h>
 
40
#include <drizzled/statistics_variables.h>
 
41
#include <drizzled/session/transactions.h>
 
42
#include <drizzled/open_tables_state.h>
 
43
#include <drizzled/table/cache.h>
 
44
#include <drizzled/create_field.h>
 
45
 
 
46
namespace drizzled {
39
47
 
40
48
extern plugin::StorageEngine *heap_engine;
41
49
extern plugin::StorageEngine *myisam_engine;
68
76
{
69
77
  Table *table= table_list->table;
70
78
 
71
 
  if (fields.elements == 0 && values.elements != 0)
 
79
  if (fields.size() == 0 && values.size() != 0)
72
80
  {
73
 
    if (values.elements != table->getShare()->sizeFields())
 
81
    if (values.size() != table->getShare()->sizeFields())
74
82
    {
75
83
      my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), 1L);
76
84
      return -1;
85
93
  }
86
94
  else
87
95
  {                                             // Part field list
88
 
    Select_Lex *select_lex= &session->lex->select_lex;
 
96
    Select_Lex *select_lex= &session->lex().select_lex;
89
97
    Name_resolution_context *context= &select_lex->context;
90
98
    Name_resolution_context_state ctx_state;
91
99
    int res;
92
100
 
93
 
    if (fields.elements != values.elements)
 
101
    if (fields.size() != values.size())
94
102
    {
95
103
      my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), 1L);
96
104
      return -1;
243
251
  uint64_t id;
244
252
  CopyInfo info;
245
253
  Table *table= 0;
246
 
  List_iterator_fast<List_item> its(values_list);
 
254
  List<List_item>::iterator its(values_list.begin());
247
255
  List_item *values;
248
256
  Name_resolution_context *context;
249
257
  Name_resolution_context_state ctx_state;
250
 
  thr_lock_type lock_type;
251
258
  Item *unused_conds= 0;
252
259
 
253
260
 
256
263
    the current connection mode or table operation.
257
264
  */
258
265
  upgrade_lock_type(session, &table_list->lock_type, duplic,
259
 
                    values_list.elements > 1);
 
266
                    values_list.size() > 1);
260
267
 
261
268
  if (session->openTablesLock(table_list))
262
269
  {
264
271
    return true;
265
272
  }
266
273
 
267
 
  lock_type= table_list->lock_type;
268
 
 
269
274
  session->set_proc_info("init");
270
275
  session->used_tables=0;
271
276
  values= its++;
272
 
  value_count= values->elements;
 
277
  value_count= values->size();
273
278
 
274
279
  if (prepare_insert(session, table_list, table, fields, values,
275
280
                           update_fields, update_values, duplic, &unused_conds,
276
281
                           false,
277
 
                           (fields.elements || !value_count ||
 
282
                           (fields.size() || !value_count ||
278
283
                            (0) != 0), !ignore))
279
284
  {
280
285
    if (table != NULL)
281
286
      table->cursor->ha_release_auto_increment();
282
287
    if (!joins_freed)
283
 
      free_underlaid_joins(session, &session->lex->select_lex);
 
288
      free_underlaid_joins(session, &session->lex().select_lex);
284
289
    session->setAbortOnWarning(false);
285
290
    DRIZZLE_INSERT_DONE(1, 0);
286
291
    return true;
289
294
  /* mysql_prepare_insert set table_list->table if it was not set */
290
295
  table= table_list->table;
291
296
 
292
 
  context= &session->lex->select_lex.context;
 
297
  context= &session->lex().select_lex.context;
293
298
  /*
294
299
    These three asserts test the hypothesis that the resetting of the name
295
300
    resolution context below is not necessary at all since the list of local
312
317
  while ((values= its++))
313
318
  {
314
319
    counter++;
315
 
    if (values->elements != value_count)
 
320
    if (values->size() != value_count)
316
321
    {
317
322
      my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), counter);
318
323
 
319
324
      if (table != NULL)
320
325
        table->cursor->ha_release_auto_increment();
321
326
      if (!joins_freed)
322
 
        free_underlaid_joins(session, &session->lex->select_lex);
 
327
        free_underlaid_joins(session, &session->lex().select_lex);
323
328
      session->setAbortOnWarning(false);
324
329
      DRIZZLE_INSERT_DONE(1, 0);
325
330
 
330
335
      if (table != NULL)
331
336
        table->cursor->ha_release_auto_increment();
332
337
      if (!joins_freed)
333
 
        free_underlaid_joins(session, &session->lex->select_lex);
 
338
        free_underlaid_joins(session, &session->lex().select_lex);
334
339
      session->setAbortOnWarning(false);
335
340
      DRIZZLE_INSERT_DONE(1, 0);
336
341
      return true;
337
342
    }
338
343
  }
339
 
  its.rewind ();
 
344
  its= values_list.begin();
340
345
 
341
346
  /* Restore the current context. */
342
347
  ctx_state.restore_state(context, table_list);
368
373
  {
369
374
    if (duplic != DUP_ERROR || ignore)
370
375
      table->cursor->extra(HA_EXTRA_IGNORE_DUP_KEY);
371
 
    table->cursor->ha_start_bulk_insert(values_list.elements);
 
376
    table->cursor->ha_start_bulk_insert(values_list.size());
372
377
  }
373
378
 
374
379
 
378
383
 
379
384
  while ((values= its++))
380
385
  {
381
 
    if (fields.elements || !value_count)
 
386
    if (fields.size() || !value_count)
382
387
    {
383
388
      table->restoreRecordAsDefault();  // Get empty record
384
389
      if (fill_record(session, fields, *values))
385
390
      {
386
 
        if (values_list.elements != 1 && ! session->is_error())
 
391
        if (values_list.size() != 1 && ! session->is_error())
387
392
        {
388
393
          info.records++;
389
394
          continue;
403
408
 
404
409
      if (fill_record(session, table->getFields(), *values))
405
410
      {
406
 
        if (values_list.elements != 1 && ! session->is_error())
 
411
        if (values_list.size() != 1 && ! session->is_error())
407
412
        {
408
413
          info.records++;
409
414
          continue;
422
427
    session->row_count++;
423
428
  }
424
429
 
425
 
  free_underlaid_joins(session, &session->lex->select_lex);
 
430
  free_underlaid_joins(session, &session->lex().select_lex);
426
431
  joins_freed= true;
427
432
 
428
433
  /*
482
487
    if (table != NULL)
483
488
      table->cursor->ha_release_auto_increment();
484
489
    if (!joins_freed)
485
 
      free_underlaid_joins(session, &session->lex->select_lex);
 
490
      free_underlaid_joins(session, &session->lex().select_lex);
486
491
    session->setAbortOnWarning(false);
487
492
    DRIZZLE_INSERT_DONE(1, 0);
488
493
    return true;
489
494
  }
490
495
 
491
 
  if (values_list.elements == 1 && (!(session->options & OPTION_WARNINGS) ||
 
496
  if (values_list.size() == 1 && (!(session->options & OPTION_WARNINGS) ||
492
497
                                    !session->cuted_fields))
493
498
  {
494
499
    session->row_count_func= info.copied + info.deleted + info.updated;
545
550
     than INSERT.
546
551
  */
547
552
 
548
 
  if (setup_tables_and_check_access(session, &session->lex->select_lex.context,
549
 
                                    &session->lex->select_lex.top_join_list,
550
 
                                    table_list,
551
 
                                    &session->lex->select_lex.leaf_tables,
552
 
                                    select_insert))
553
 
    return(true);
554
 
 
555
 
  return(false);
 
553
  return setup_tables_and_check_access(session, &session->lex().select_lex.context,
 
554
    &session->lex().select_lex.top_join_list, table_list, &session->lex().select_lex.leaf_tables, select_insert);
556
555
}
557
556
 
558
557
 
595
594
                          bool select_insert,
596
595
                          bool check_fields, bool abort_on_warning)
597
596
{
598
 
  Select_Lex *select_lex= &session->lex->select_lex;
 
597
  Select_Lex *select_lex= &session->lex().select_lex;
599
598
  Name_resolution_context *context= &select_lex->context;
600
599
  Name_resolution_context_state ctx_state;
601
600
  bool insert_into_view= (0 != 0);
628
627
  if (duplic == DUP_UPDATE)
629
628
  {
630
629
    /* it should be allocated before Item::fix_fields() */
631
 
    if (table_list->set_insert_values(session->mem_root))
632
 
      return(true);
 
630
    table_list->set_insert_values();
633
631
  }
634
632
 
635
633
  if (prepare_insert_check_table(session, table_list, fields, select_insert))
636
 
    return(true);
 
634
    return true;
637
635
 
638
636
 
639
637
  /* Prepare the fields in the statement. */
681
679
  }
682
680
 
683
681
  if (res)
684
 
    return(res);
 
682
    return res;
685
683
 
686
684
  if (not table)
687
685
    table= table_list->table;
688
686
 
689
 
  if (not select_insert)
 
687
  if (not select_insert && unique_table(table_list, table_list->next_global, true))
690
688
  {
691
 
    TableList *duplicate;
692
 
    if ((duplicate= unique_table(table_list, table_list->next_global, true)))
693
 
    {
694
 
      my_error(ER_UPDATE_TABLE_USED, MYF(0), table_list->alias);
695
 
 
696
 
      return true;
697
 
    }
 
689
    my_error(ER_UPDATE_TABLE_USED, MYF(0), table_list->alias);
 
690
    return true;
698
691
  }
699
692
 
700
693
  if (duplic == DUP_UPDATE || duplic == DUP_REPLACE)
836
829
        assert(table->insert_values.size());
837
830
        table->storeRecordAsInsert();
838
831
        table->restoreRecord();
839
 
        assert(info->update_fields->elements ==
840
 
                    info->update_values->elements);
 
832
        assert(info->update_fields->size() ==
 
833
                    info->update_values->size());
841
834
        if (fill_record(session, *info->update_fields,
842
835
                                                 *info->update_values,
843
836
                                                 info->ignore))
957
950
gok_or_after_err:
958
951
  if (!table->cursor->has_transactions())
959
952
    session->transaction.stmt.markModifiedNonTransData();
960
 
  return(0);
 
953
  return 0;
961
954
 
962
955
err:
963
956
  info->last_errno= error;
964
957
  /* current_select is NULL if this is a delayed insert */
965
 
  if (session->lex->current_select)
966
 
    session->lex->current_select->no_error= 0;        // Give error
 
958
  if (session->lex().current_select)
 
959
    session->lex().current_select->no_error= 0;        // Give error
967
960
  table->print_error(error,MYF(0));
968
961
 
969
962
before_err:
984
977
 
985
978
  for (Field **field=entry->getFields() ; *field ; field++)
986
979
  {
987
 
    if (((*field)->isWriteSet()) == false)
 
980
    if (not (*field)->isWriteSet())
988
981
    {
989
982
      /*
990
983
       * If the field doesn't have any default value
1002
995
    {
1003
996
      /*
1004
997
       * However, if an actual NULL value was specified
1005
 
       * for the field and the field is a NOT NULL field, 
 
998
       * for the field and the field is a NOT NULL field,
1006
999
       * throw ER_BAD_NULL_ERROR.
1007
1000
       *
1008
1001
       * Per the SQL standard, inserting NULL into a NOT NULL
1038
1031
 
1039
1032
bool insert_select_prepare(Session *session)
1040
1033
{
1041
 
  LEX *lex= session->lex;
 
1034
  LEX *lex= &session->lex();
1042
1035
  Select_Lex *select_lex= &lex->select_lex;
1043
1036
 
1044
1037
  /*
1051
1044
                           lex->update_list, lex->value_list,
1052
1045
                           lex->duplicates,
1053
1046
                           &select_lex->where, true, false, false))
1054
 
    return(true);
 
1047
    return true;
1055
1048
 
1056
1049
  /*
1057
1050
    exclude first table from leaf tables list, because it belong to
1061
1054
  lex->leaf_tables_insert= select_lex->leaf_tables;
1062
1055
  /* skip all leaf tables belonged to view where we are insert */
1063
1056
  select_lex->leaf_tables= select_lex->leaf_tables->next_leaf;
1064
 
  return(false);
 
1057
  return false;
1065
1058
}
1066
1059
 
1067
1060
 
1085
1078
int
1086
1079
select_insert::prepare(List<Item> &values, Select_Lex_Unit *u)
1087
1080
{
1088
 
  LEX *lex= session->lex;
1089
1081
  int res;
1090
1082
  table_map map= 0;
1091
 
  Select_Lex *lex_current_select_save= lex->current_select;
 
1083
  Select_Lex *lex_current_select_save= session->lex().current_select;
1092
1084
 
1093
1085
 
1094
1086
  unit= u;
1098
1090
    select, LEX::current_select should point to the first select while
1099
1091
    we are fixing fields from insert list.
1100
1092
  */
1101
 
  lex->current_select= &lex->select_lex;
 
1093
  session->lex().current_select= &session->lex().select_lex;
1102
1094
  res= check_insert_fields(session, table_list, *fields, values,
1103
1095
                           !insert_into_view, &map) ||
1104
1096
       setup_fields(session, 0, values, MARK_COLUMNS_READ, 0, 0);
1105
1097
 
1106
 
  if (!res && fields->elements)
 
1098
  if (!res && fields->size())
1107
1099
  {
1108
1100
    bool saved_abort_on_warning= session->abortOnWarning();
1109
1101
    session->setAbortOnWarning(not info.ignore);
1114
1106
 
1115
1107
  if (info.handle_duplicates == DUP_UPDATE && !res)
1116
1108
  {
1117
 
    Name_resolution_context *context= &lex->select_lex.context;
 
1109
    Name_resolution_context *context= &session->lex().select_lex.context;
1118
1110
    Name_resolution_context_state ctx_state;
1119
1111
 
1120
1112
    /* Save the state of the current name resolution context. */
1132
1124
      We use next_name_resolution_table descructively, so check it first (views?)
1133
1125
    */
1134
1126
    assert (!table_list->next_name_resolution_table);
1135
 
    if (lex->select_lex.group_list.elements == 0 &&
1136
 
        !lex->select_lex.with_sum_func)
 
1127
    if (session->lex().select_lex.group_list.elements == 0 and
 
1128
        not session->lex().select_lex.with_sum_func)
1137
1129
      /*
1138
1130
        We must make a single context out of the two separate name resolution contexts :
1139
1131
        the INSERT table and the tables in the SELECT part of INSERT ... SELECT.
1152
1144
        order to get correct values from those fields when the select
1153
1145
        employs a temporary table.
1154
1146
      */
1155
 
      List_iterator<Item> li(*info.update_values);
 
1147
      List<Item>::iterator li(info.update_values->begin());
1156
1148
      Item *item;
1157
1149
 
1158
1150
      while ((item= li++))
1159
1151
      {
1160
1152
        item->transform(&Item::update_value_transformer,
1161
 
                        (unsigned char*)lex->current_select);
 
1153
                        (unsigned char*)session->lex().current_select);
1162
1154
      }
1163
1155
    }
1164
1156
 
1166
1158
    ctx_state.restore_state(context, table_list);
1167
1159
  }
1168
1160
 
1169
 
  lex->current_select= lex_current_select_save;
 
1161
  session->lex().current_select= lex_current_select_save;
1170
1162
  if (res)
1171
 
    return(1);
 
1163
    return 1;
1172
1164
  /*
1173
1165
    if it is INSERT into join view then check_insert_fields already found
1174
1166
    real table for insert
1182
1174
  if (unique_table(table_list, table_list->next_global))
1183
1175
  {
1184
1176
    /* Using same table for INSERT and SELECT */
1185
 
    lex->current_select->options|= OPTION_BUFFER_RESULT;
1186
 
    lex->current_select->join->select_options|= OPTION_BUFFER_RESULT;
 
1177
    session->lex().current_select->options|= OPTION_BUFFER_RESULT;
 
1178
    session->lex().current_select->join->select_options|= OPTION_BUFFER_RESULT;
1187
1179
  }
1188
 
  else if (!(lex->current_select->options & OPTION_BUFFER_RESULT))
 
1180
  else if (not (session->lex().current_select->options & OPTION_BUFFER_RESULT))
1189
1181
  {
1190
1182
    /*
1191
1183
      We must not yet prepare the result table if it is the same as one of the
1216
1208
  table->mark_columns_needed_for_insert();
1217
1209
 
1218
1210
 
1219
 
  return(res);
 
1211
  return res;
1220
1212
}
1221
1213
 
1222
1214
 
1238
1230
 
1239
1231
int select_insert::prepare2(void)
1240
1232
{
1241
 
 
1242
 
  if (session->lex->current_select->options & OPTION_BUFFER_RESULT)
 
1233
  if (session->lex().current_select->options & OPTION_BUFFER_RESULT)
1243
1234
    table->cursor->ha_start_bulk_insert((ha_rows) 0);
1244
 
  return(0);
 
1235
 
 
1236
  return 0;
1245
1237
}
1246
1238
 
1247
1239
 
1269
1261
bool select_insert::send_data(List<Item> &values)
1270
1262
{
1271
1263
 
1272
 
  bool error=0;
 
1264
  bool error= false;
1273
1265
 
1274
1266
  if (unit->offset_limit_cnt)
1275
1267
  {                                             // using limit offset,count
1325
1317
 
1326
1318
void select_insert::store_values(List<Item> &values)
1327
1319
{
1328
 
  if (fields->elements)
 
1320
  if (fields->size())
1329
1321
    fill_record(session, *fields, values, true);
1330
1322
  else
1331
1323
    fill_record(session, table->getFields(), values, true);
1384
1376
     (info.copied ? autoinc_value_of_last_inserted_row : 0));
1385
1377
  session->my_ok((ulong) session->rowCount(),
1386
1378
                 info.copied + info.deleted + info.touched, id, buff);
1387
 
  session->status_var.inserted_row_count+= session->rowCount(); 
 
1379
  session->status_var.inserted_row_count+= session->rowCount();
1388
1380
  DRIZZLE_INSERT_SELECT_DONE(0, session->rowCount());
1389
1381
  return 0;
1390
1382
}
1487
1479
                                      List<Item> *items,
1488
1480
                                      bool is_if_not_exists,
1489
1481
                                      DrizzleLock **lock,
1490
 
                                      identifier::Table::const_reference identifier)
 
1482
                                      const identifier::Table& identifier)
1491
1483
{
1492
1484
  TableShare share(message::Table::INTERNAL);
1493
 
  uint32_t select_field_count= items->elements;
 
1485
  uint32_t select_field_count= items->size();
1494
1486
  /* Add selected items to field list */
1495
 
  List_iterator_fast<Item> it(*items);
 
1487
  List<Item>::iterator it(items->begin());
1496
1488
  Item *item;
1497
1489
  Field *tmp_field;
1498
1490
 
1593
1585
      if (not identifier.isTmp())
1594
1586
      {
1595
1587
        /* CREATE TABLE... has found that the table already exists for insert and is adapting to use it */
1596
 
        boost::mutex::scoped_lock scopedLock(table::Cache::singleton().mutex());
 
1588
        boost::mutex::scoped_lock scopedLock(table::Cache::mutex());
1597
1589
 
1598
1590
        if (create_table->table)
1599
1591
        {
1624
1616
            it preparable for open. But let us do close_temporary_table() here
1625
1617
            just in case.
1626
1618
          */
1627
 
          session->drop_temporary_table(identifier);
 
1619
          session->open_tables.drop_temporary_table(identifier);
1628
1620
        }
1629
1621
      }
1630
1622
    }
1643
1635
 
1644
1636
    if (not create_info->table_existed)
1645
1637
      session->drop_open_table(table, identifier);
 
1638
 
1646
1639
    return NULL;
1647
1640
  }
1648
1641
 
1656
1649
  DrizzleLock *extra_lock= NULL;
1657
1650
  /*
1658
1651
    For replication, the CREATE-SELECT statement is written
1659
 
    in two pieces: the first transaction messsage contains 
 
1652
    in two pieces: the first transaction messsage contains
1660
1653
    the CREATE TABLE statement as a CreateTableStatement message
1661
1654
    necessary to create the table.
1662
 
    
 
1655
 
1663
1656
    The second transaction message contains all the InsertStatement
1664
1657
    and associated InsertRecords that should go into the table.
1665
1658
   */
1682
1675
    if (identifier.isTmp())
1683
1676
      m_plock= &m_lock;
1684
1677
    else
1685
 
      m_plock= &session->extra_lock;
 
1678
      m_plock= &session->open_tables.extra_lock;
1686
1679
 
1687
1680
    *m_plock= extra_lock;
1688
1681
  }
1689
1682
 
1690
 
  if (table->getShare()->sizeFields() < values.elements)
 
1683
  if (table->getShare()->sizeFields() < values.size())
1691
1684
  {
1692
1685
    my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), 1);
1693
1686
    return(-1);
1694
1687
  }
1695
1688
 
1696
1689
 /* First field to copy */
1697
 
  field= table->getFields() + table->getShare()->sizeFields() - values.elements;
 
1690
  field= table->getFields() + table->getShare()->sizeFields() - values.size();
1698
1691
 
1699
1692
  /* Mark all fields that are given values */
1700
1693
  for (Field **f= field ; *f ; f++)
1720
1713
  table->cursor->ha_start_bulk_insert((ha_rows) 0);
1721
1714
  session->setAbortOnWarning(not info.ignore);
1722
1715
  if (check_that_all_fields_are_given_values(session, table, table_list))
1723
 
    return(1);
 
1716
    return 1;
1724
1717
 
1725
1718
  table->mark_columns_needed_for_insert();
1726
1719
  table->cursor->extra(HA_EXTRA_WRITE_CACHE);
1727
 
  return(0);
 
1720
  return 0;
1728
1721
}
1729
1722
 
1730
1723
void select_create::store_values(List<Item> &values)
1764
1757
    */
1765
1758
    if (!table->getShare()->getType())
1766
1759
    {
1767
 
      TransactionServices &transaction_services= TransactionServices::singleton();
1768
 
      transaction_services.autocommitOrRollback(*session, 0);
 
1760
      TransactionServices::autocommitOrRollback(*session, 0);
1769
1761
      (void) session->endActiveTransaction();
1770
1762
    }
1771
1763