~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_insert.cc

  • Committer: Lee Bieber
  • Date: 2011-03-18 04:10:25 UTC
  • mfrom: (2241.1.2 build)
  • Revision ID: kalebral@gmail.com-20110318041025-1xoj1azy6zobhnbm
Merge Stewart - refactoring of default values
Merge Olaf - more refactoring

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"
 
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>
36
40
 
37
41
namespace drizzled
38
42
{
68
72
{
69
73
  Table *table= table_list->table;
70
74
 
71
 
  if (fields.elements == 0 && values.elements != 0)
 
75
  if (fields.size() == 0 && values.size() != 0)
72
76
  {
73
 
    if (values.elements != table->getShare()->sizeFields())
 
77
    if (values.size() != table->getShare()->sizeFields())
74
78
    {
75
79
      my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), 1L);
76
80
      return -1;
85
89
  }
86
90
  else
87
91
  {                                             // Part field list
88
 
    Select_Lex *select_lex= &session->lex->select_lex;
 
92
    Select_Lex *select_lex= &session->lex().select_lex;
89
93
    Name_resolution_context *context= &select_lex->context;
90
94
    Name_resolution_context_state ctx_state;
91
95
    int res;
92
96
 
93
 
    if (fields.elements != values.elements)
 
97
    if (fields.size() != values.size())
94
98
    {
95
99
      my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), 1L);
96
100
      return -1;
243
247
  uint64_t id;
244
248
  CopyInfo info;
245
249
  Table *table= 0;
246
 
  List_iterator_fast<List_item> its(values_list);
 
250
  List<List_item>::iterator its(values_list.begin());
247
251
  List_item *values;
248
252
  Name_resolution_context *context;
249
253
  Name_resolution_context_state ctx_state;
256
260
    the current connection mode or table operation.
257
261
  */
258
262
  upgrade_lock_type(session, &table_list->lock_type, duplic,
259
 
                    values_list.elements > 1);
 
263
                    values_list.size() > 1);
260
264
 
261
265
  if (session->openTablesLock(table_list))
262
266
  {
269
273
  session->set_proc_info("init");
270
274
  session->used_tables=0;
271
275
  values= its++;
272
 
  value_count= values->elements;
 
276
  value_count= values->size();
273
277
 
274
278
  if (prepare_insert(session, table_list, table, fields, values,
275
279
                           update_fields, update_values, duplic, &unused_conds,
276
280
                           false,
277
 
                           (fields.elements || !value_count ||
 
281
                           (fields.size() || !value_count ||
278
282
                            (0) != 0), !ignore))
279
283
  {
280
284
    if (table != NULL)
281
285
      table->cursor->ha_release_auto_increment();
282
286
    if (!joins_freed)
283
 
      free_underlaid_joins(session, &session->lex->select_lex);
 
287
      free_underlaid_joins(session, &session->lex().select_lex);
284
288
    session->setAbortOnWarning(false);
285
289
    DRIZZLE_INSERT_DONE(1, 0);
286
290
    return true;
289
293
  /* mysql_prepare_insert set table_list->table if it was not set */
290
294
  table= table_list->table;
291
295
 
292
 
  context= &session->lex->select_lex.context;
 
296
  context= &session->lex().select_lex.context;
293
297
  /*
294
298
    These three asserts test the hypothesis that the resetting of the name
295
299
    resolution context below is not necessary at all since the list of local
312
316
  while ((values= its++))
313
317
  {
314
318
    counter++;
315
 
    if (values->elements != value_count)
 
319
    if (values->size() != value_count)
316
320
    {
317
321
      my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), counter);
318
322
 
319
323
      if (table != NULL)
320
324
        table->cursor->ha_release_auto_increment();
321
325
      if (!joins_freed)
322
 
        free_underlaid_joins(session, &session->lex->select_lex);
 
326
        free_underlaid_joins(session, &session->lex().select_lex);
323
327
      session->setAbortOnWarning(false);
324
328
      DRIZZLE_INSERT_DONE(1, 0);
325
329
 
330
334
      if (table != NULL)
331
335
        table->cursor->ha_release_auto_increment();
332
336
      if (!joins_freed)
333
 
        free_underlaid_joins(session, &session->lex->select_lex);
 
337
        free_underlaid_joins(session, &session->lex().select_lex);
334
338
      session->setAbortOnWarning(false);
335
339
      DRIZZLE_INSERT_DONE(1, 0);
336
340
      return true;
337
341
    }
338
342
  }
339
 
  its.rewind ();
 
343
  its= values_list.begin();
340
344
 
341
345
  /* Restore the current context. */
342
346
  ctx_state.restore_state(context, table_list);
368
372
  {
369
373
    if (duplic != DUP_ERROR || ignore)
370
374
      table->cursor->extra(HA_EXTRA_IGNORE_DUP_KEY);
371
 
    table->cursor->ha_start_bulk_insert(values_list.elements);
 
375
    table->cursor->ha_start_bulk_insert(values_list.size());
372
376
  }
373
377
 
374
378
 
378
382
 
379
383
  while ((values= its++))
380
384
  {
381
 
    if (fields.elements || !value_count)
 
385
    if (fields.size() || !value_count)
382
386
    {
383
387
      table->restoreRecordAsDefault();  // Get empty record
384
388
      if (fill_record(session, fields, *values))
385
389
      {
386
 
        if (values_list.elements != 1 && ! session->is_error())
 
390
        if (values_list.size() != 1 && ! session->is_error())
387
391
        {
388
392
          info.records++;
389
393
          continue;
403
407
 
404
408
      if (fill_record(session, table->getFields(), *values))
405
409
      {
406
 
        if (values_list.elements != 1 && ! session->is_error())
 
410
        if (values_list.size() != 1 && ! session->is_error())
407
411
        {
408
412
          info.records++;
409
413
          continue;
422
426
    session->row_count++;
423
427
  }
424
428
 
425
 
  free_underlaid_joins(session, &session->lex->select_lex);
 
429
  free_underlaid_joins(session, &session->lex().select_lex);
426
430
  joins_freed= true;
427
431
 
428
432
  /*
482
486
    if (table != NULL)
483
487
      table->cursor->ha_release_auto_increment();
484
488
    if (!joins_freed)
485
 
      free_underlaid_joins(session, &session->lex->select_lex);
 
489
      free_underlaid_joins(session, &session->lex().select_lex);
486
490
    session->setAbortOnWarning(false);
487
491
    DRIZZLE_INSERT_DONE(1, 0);
488
492
    return true;
489
493
  }
490
494
 
491
 
  if (values_list.elements == 1 && (!(session->options & OPTION_WARNINGS) ||
 
495
  if (values_list.size() == 1 && (!(session->options & OPTION_WARNINGS) ||
492
496
                                    !session->cuted_fields))
493
497
  {
494
498
    session->row_count_func= info.copied + info.deleted + info.updated;
545
549
     than INSERT.
546
550
  */
547
551
 
548
 
  if (setup_tables_and_check_access(session, &session->lex->select_lex.context,
549
 
                                    &session->lex->select_lex.top_join_list,
 
552
  if (setup_tables_and_check_access(session, &session->lex().select_lex.context,
 
553
                                    &session->lex().select_lex.top_join_list,
550
554
                                    table_list,
551
 
                                    &session->lex->select_lex.leaf_tables,
 
555
                                    &session->lex().select_lex.leaf_tables,
552
556
                                    select_insert))
553
557
    return(true);
554
558
 
595
599
                          bool select_insert,
596
600
                          bool check_fields, bool abort_on_warning)
597
601
{
598
 
  Select_Lex *select_lex= &session->lex->select_lex;
 
602
  Select_Lex *select_lex= &session->lex().select_lex;
599
603
  Name_resolution_context *context= &select_lex->context;
600
604
  Name_resolution_context_state ctx_state;
601
605
  bool insert_into_view= (0 != 0);
836
840
        assert(table->insert_values.size());
837
841
        table->storeRecordAsInsert();
838
842
        table->restoreRecord();
839
 
        assert(info->update_fields->elements ==
840
 
                    info->update_values->elements);
 
843
        assert(info->update_fields->size() ==
 
844
                    info->update_values->size());
841
845
        if (fill_record(session, *info->update_fields,
842
846
                                                 *info->update_values,
843
847
                                                 info->ignore))
962
966
err:
963
967
  info->last_errno= error;
964
968
  /* 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
 
969
  if (session->lex().current_select)
 
970
    session->lex().current_select->no_error= 0;        // Give error
967
971
  table->print_error(error,MYF(0));
968
972
 
969
973
before_err:
1002
1006
    {
1003
1007
      /*
1004
1008
       * However, if an actual NULL value was specified
1005
 
       * for the field and the field is a NOT NULL field, 
 
1009
       * for the field and the field is a NOT NULL field,
1006
1010
       * throw ER_BAD_NULL_ERROR.
1007
1011
       *
1008
1012
       * Per the SQL standard, inserting NULL into a NOT NULL
1038
1042
 
1039
1043
bool insert_select_prepare(Session *session)
1040
1044
{
1041
 
  LEX *lex= session->lex;
 
1045
  LEX *lex= &session->lex();
1042
1046
  Select_Lex *select_lex= &lex->select_lex;
1043
1047
 
1044
1048
  /*
1085
1089
int
1086
1090
select_insert::prepare(List<Item> &values, Select_Lex_Unit *u)
1087
1091
{
1088
 
  LEX *lex= session->lex;
1089
1092
  int res;
1090
1093
  table_map map= 0;
1091
 
  Select_Lex *lex_current_select_save= lex->current_select;
 
1094
  Select_Lex *lex_current_select_save= session->lex().current_select;
1092
1095
 
1093
1096
 
1094
1097
  unit= u;
1098
1101
    select, LEX::current_select should point to the first select while
1099
1102
    we are fixing fields from insert list.
1100
1103
  */
1101
 
  lex->current_select= &lex->select_lex;
 
1104
  session->lex().current_select= &session->lex().select_lex;
1102
1105
  res= check_insert_fields(session, table_list, *fields, values,
1103
1106
                           !insert_into_view, &map) ||
1104
1107
       setup_fields(session, 0, values, MARK_COLUMNS_READ, 0, 0);
1105
1108
 
1106
 
  if (!res && fields->elements)
 
1109
  if (!res && fields->size())
1107
1110
  {
1108
1111
    bool saved_abort_on_warning= session->abortOnWarning();
1109
1112
    session->setAbortOnWarning(not info.ignore);
1114
1117
 
1115
1118
  if (info.handle_duplicates == DUP_UPDATE && !res)
1116
1119
  {
1117
 
    Name_resolution_context *context= &lex->select_lex.context;
 
1120
    Name_resolution_context *context= &session->lex().select_lex.context;
1118
1121
    Name_resolution_context_state ctx_state;
1119
1122
 
1120
1123
    /* Save the state of the current name resolution context. */
1132
1135
      We use next_name_resolution_table descructively, so check it first (views?)
1133
1136
    */
1134
1137
    assert (!table_list->next_name_resolution_table);
1135
 
    if (lex->select_lex.group_list.elements == 0 &&
1136
 
        !lex->select_lex.with_sum_func)
 
1138
    if (session->lex().select_lex.group_list.elements == 0 and
 
1139
        not session->lex().select_lex.with_sum_func)
1137
1140
      /*
1138
1141
        We must make a single context out of the two separate name resolution contexts :
1139
1142
        the INSERT table and the tables in the SELECT part of INSERT ... SELECT.
1152
1155
        order to get correct values from those fields when the select
1153
1156
        employs a temporary table.
1154
1157
      */
1155
 
      List_iterator<Item> li(*info.update_values);
 
1158
      List<Item>::iterator li(info.update_values->begin());
1156
1159
      Item *item;
1157
1160
 
1158
1161
      while ((item= li++))
1159
1162
      {
1160
1163
        item->transform(&Item::update_value_transformer,
1161
 
                        (unsigned char*)lex->current_select);
 
1164
                        (unsigned char*)session->lex().current_select);
1162
1165
      }
1163
1166
    }
1164
1167
 
1166
1169
    ctx_state.restore_state(context, table_list);
1167
1170
  }
1168
1171
 
1169
 
  lex->current_select= lex_current_select_save;
 
1172
  session->lex().current_select= lex_current_select_save;
1170
1173
  if (res)
1171
1174
    return(1);
1172
1175
  /*
1182
1185
  if (unique_table(table_list, table_list->next_global))
1183
1186
  {
1184
1187
    /* 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;
 
1188
    session->lex().current_select->options|= OPTION_BUFFER_RESULT;
 
1189
    session->lex().current_select->join->select_options|= OPTION_BUFFER_RESULT;
1187
1190
  }
1188
 
  else if (!(lex->current_select->options & OPTION_BUFFER_RESULT))
 
1191
  else if (not (session->lex().current_select->options & OPTION_BUFFER_RESULT))
1189
1192
  {
1190
1193
    /*
1191
1194
      We must not yet prepare the result table if it is the same as one of the
1238
1241
 
1239
1242
int select_insert::prepare2(void)
1240
1243
{
1241
 
 
1242
 
  if (session->lex->current_select->options & OPTION_BUFFER_RESULT)
 
1244
  if (session->lex().current_select->options & OPTION_BUFFER_RESULT)
1243
1245
    table->cursor->ha_start_bulk_insert((ha_rows) 0);
 
1246
 
1244
1247
  return(0);
1245
1248
}
1246
1249
 
1269
1272
bool select_insert::send_data(List<Item> &values)
1270
1273
{
1271
1274
 
1272
 
  bool error=0;
 
1275
  bool error= false;
1273
1276
 
1274
1277
  if (unit->offset_limit_cnt)
1275
1278
  {                                             // using limit offset,count
1325
1328
 
1326
1329
void select_insert::store_values(List<Item> &values)
1327
1330
{
1328
 
  if (fields->elements)
 
1331
  if (fields->size())
1329
1332
    fill_record(session, *fields, values, true);
1330
1333
  else
1331
1334
    fill_record(session, table->getFields(), values, true);
1384
1387
     (info.copied ? autoinc_value_of_last_inserted_row : 0));
1385
1388
  session->my_ok((ulong) session->rowCount(),
1386
1389
                 info.copied + info.deleted + info.touched, id, buff);
1387
 
  session->status_var.inserted_row_count+= session->rowCount(); 
 
1390
  session->status_var.inserted_row_count+= session->rowCount();
1388
1391
  DRIZZLE_INSERT_SELECT_DONE(0, session->rowCount());
1389
1392
  return 0;
1390
1393
}
1490
1493
                                      identifier::Table::const_reference identifier)
1491
1494
{
1492
1495
  TableShare share(message::Table::INTERNAL);
1493
 
  uint32_t select_field_count= items->elements;
 
1496
  uint32_t select_field_count= items->size();
1494
1497
  /* Add selected items to field list */
1495
 
  List_iterator_fast<Item> it(*items);
 
1498
  List<Item>::iterator it(items->begin());
1496
1499
  Item *item;
1497
1500
  Field *tmp_field;
1498
1501
 
1643
1646
 
1644
1647
    if (not create_info->table_existed)
1645
1648
      session->drop_open_table(table, identifier);
 
1649
 
1646
1650
    return NULL;
1647
1651
  }
1648
1652
 
1656
1660
  DrizzleLock *extra_lock= NULL;
1657
1661
  /*
1658
1662
    For replication, the CREATE-SELECT statement is written
1659
 
    in two pieces: the first transaction messsage contains 
 
1663
    in two pieces: the first transaction messsage contains
1660
1664
    the CREATE TABLE statement as a CreateTableStatement message
1661
1665
    necessary to create the table.
1662
 
    
 
1666
 
1663
1667
    The second transaction message contains all the InsertStatement
1664
1668
    and associated InsertRecords that should go into the table.
1665
1669
   */
1687
1691
    *m_plock= extra_lock;
1688
1692
  }
1689
1693
 
1690
 
  if (table->getShare()->sizeFields() < values.elements)
 
1694
  if (table->getShare()->sizeFields() < values.size())
1691
1695
  {
1692
1696
    my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), 1);
1693
1697
    return(-1);
1694
1698
  }
1695
1699
 
1696
1700
 /* First field to copy */
1697
 
  field= table->getFields() + table->getShare()->sizeFields() - values.elements;
 
1701
  field= table->getFields() + table->getShare()->sizeFields() - values.size();
1698
1702
 
1699
1703
  /* Mark all fields that are given values */
1700
1704
  for (Field **f= field ; *f ; f++)