~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_insert.cc

  • Committer: David Shrewsbury
  • Date: 2011-02-28 14:33:56 UTC
  • mfrom: (2207 bootstrap)
  • mto: (2208.1.3 build)
  • mto: This revision was merged to the branch mainline in revision 2209.
  • Revision ID: shrewsbury.dave@gmail.com-20110228143356-ws2m2tp7an44egkd
Merge with trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
#include <drizzled/plugin/transactional_storage_engine.h>
34
34
#include <drizzled/select_insert.h>
35
35
#include <drizzled/select_create.h>
36
 
 
37
36
#include <drizzled/table/shell.h>
 
37
#include <drizzled/alter_info.h>
38
38
 
39
39
namespace drizzled
40
40
{
70
70
{
71
71
  Table *table= table_list->table;
72
72
 
73
 
  if (fields.elements == 0 && values.elements != 0)
 
73
  if (fields.size() == 0 && values.size() != 0)
74
74
  {
75
 
    if (values.elements != table->getShare()->sizeFields())
 
75
    if (values.size() != table->getShare()->sizeFields())
76
76
    {
77
77
      my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), 1L);
78
78
      return -1;
87
87
  }
88
88
  else
89
89
  {                                             // Part field list
90
 
    Select_Lex *select_lex= &session->lex->select_lex;
 
90
    Select_Lex *select_lex= &session->getLex()->select_lex;
91
91
    Name_resolution_context *context= &select_lex->context;
92
92
    Name_resolution_context_state ctx_state;
93
93
    int res;
94
94
 
95
 
    if (fields.elements != values.elements)
 
95
    if (fields.size() != values.size())
96
96
    {
97
97
      my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), 1L);
98
98
      return -1;
245
245
  uint64_t id;
246
246
  CopyInfo info;
247
247
  Table *table= 0;
248
 
  List_iterator_fast<List_item> its(values_list);
 
248
  List<List_item>::iterator its(values_list.begin());
249
249
  List_item *values;
250
250
  Name_resolution_context *context;
251
251
  Name_resolution_context_state ctx_state;
258
258
    the current connection mode or table operation.
259
259
  */
260
260
  upgrade_lock_type(session, &table_list->lock_type, duplic,
261
 
                    values_list.elements > 1);
 
261
                    values_list.size() > 1);
262
262
 
263
263
  if (session->openTablesLock(table_list))
264
264
  {
271
271
  session->set_proc_info("init");
272
272
  session->used_tables=0;
273
273
  values= its++;
274
 
  value_count= values->elements;
 
274
  value_count= values->size();
275
275
 
276
276
  if (prepare_insert(session, table_list, table, fields, values,
277
277
                           update_fields, update_values, duplic, &unused_conds,
278
278
                           false,
279
 
                           (fields.elements || !value_count ||
 
279
                           (fields.size() || !value_count ||
280
280
                            (0) != 0), !ignore))
281
281
  {
282
282
    if (table != NULL)
283
283
      table->cursor->ha_release_auto_increment();
284
284
    if (!joins_freed)
285
 
      free_underlaid_joins(session, &session->lex->select_lex);
 
285
      free_underlaid_joins(session, &session->getLex()->select_lex);
286
286
    session->setAbortOnWarning(false);
287
287
    DRIZZLE_INSERT_DONE(1, 0);
288
288
    return true;
291
291
  /* mysql_prepare_insert set table_list->table if it was not set */
292
292
  table= table_list->table;
293
293
 
294
 
  context= &session->lex->select_lex.context;
 
294
  context= &session->getLex()->select_lex.context;
295
295
  /*
296
296
    These three asserts test the hypothesis that the resetting of the name
297
297
    resolution context below is not necessary at all since the list of local
314
314
  while ((values= its++))
315
315
  {
316
316
    counter++;
317
 
    if (values->elements != value_count)
 
317
    if (values->size() != value_count)
318
318
    {
319
319
      my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), counter);
320
320
 
321
321
      if (table != NULL)
322
322
        table->cursor->ha_release_auto_increment();
323
323
      if (!joins_freed)
324
 
        free_underlaid_joins(session, &session->lex->select_lex);
 
324
        free_underlaid_joins(session, &session->getLex()->select_lex);
325
325
      session->setAbortOnWarning(false);
326
326
      DRIZZLE_INSERT_DONE(1, 0);
327
327
 
332
332
      if (table != NULL)
333
333
        table->cursor->ha_release_auto_increment();
334
334
      if (!joins_freed)
335
 
        free_underlaid_joins(session, &session->lex->select_lex);
 
335
        free_underlaid_joins(session, &session->getLex()->select_lex);
336
336
      session->setAbortOnWarning(false);
337
337
      DRIZZLE_INSERT_DONE(1, 0);
338
338
      return true;
339
339
    }
340
340
  }
341
 
  its= values_list;
 
341
  its= values_list.begin();
342
342
 
343
343
  /* Restore the current context. */
344
344
  ctx_state.restore_state(context, table_list);
370
370
  {
371
371
    if (duplic != DUP_ERROR || ignore)
372
372
      table->cursor->extra(HA_EXTRA_IGNORE_DUP_KEY);
373
 
    table->cursor->ha_start_bulk_insert(values_list.elements);
 
373
    table->cursor->ha_start_bulk_insert(values_list.size());
374
374
  }
375
375
 
376
376
 
380
380
 
381
381
  while ((values= its++))
382
382
  {
383
 
    if (fields.elements || !value_count)
 
383
    if (fields.size() || !value_count)
384
384
    {
385
385
      table->restoreRecordAsDefault();  // Get empty record
386
386
      if (fill_record(session, fields, *values))
387
387
      {
388
 
        if (values_list.elements != 1 && ! session->is_error())
 
388
        if (values_list.size() != 1 && ! session->is_error())
389
389
        {
390
390
          info.records++;
391
391
          continue;
405
405
 
406
406
      if (fill_record(session, table->getFields(), *values))
407
407
      {
408
 
        if (values_list.elements != 1 && ! session->is_error())
 
408
        if (values_list.size() != 1 && ! session->is_error())
409
409
        {
410
410
          info.records++;
411
411
          continue;
424
424
    session->row_count++;
425
425
  }
426
426
 
427
 
  free_underlaid_joins(session, &session->lex->select_lex);
 
427
  free_underlaid_joins(session, &session->getLex()->select_lex);
428
428
  joins_freed= true;
429
429
 
430
430
  /*
484
484
    if (table != NULL)
485
485
      table->cursor->ha_release_auto_increment();
486
486
    if (!joins_freed)
487
 
      free_underlaid_joins(session, &session->lex->select_lex);
 
487
      free_underlaid_joins(session, &session->getLex()->select_lex);
488
488
    session->setAbortOnWarning(false);
489
489
    DRIZZLE_INSERT_DONE(1, 0);
490
490
    return true;
491
491
  }
492
492
 
493
 
  if (values_list.elements == 1 && (!(session->options & OPTION_WARNINGS) ||
 
493
  if (values_list.size() == 1 && (!(session->options & OPTION_WARNINGS) ||
494
494
                                    !session->cuted_fields))
495
495
  {
496
496
    session->row_count_func= info.copied + info.deleted + info.updated;
547
547
     than INSERT.
548
548
  */
549
549
 
550
 
  if (setup_tables_and_check_access(session, &session->lex->select_lex.context,
551
 
                                    &session->lex->select_lex.top_join_list,
 
550
  if (setup_tables_and_check_access(session, &session->getLex()->select_lex.context,
 
551
                                    &session->getLex()->select_lex.top_join_list,
552
552
                                    table_list,
553
 
                                    &session->lex->select_lex.leaf_tables,
 
553
                                    &session->getLex()->select_lex.leaf_tables,
554
554
                                    select_insert))
555
555
    return(true);
556
556
 
597
597
                          bool select_insert,
598
598
                          bool check_fields, bool abort_on_warning)
599
599
{
600
 
  Select_Lex *select_lex= &session->lex->select_lex;
 
600
  Select_Lex *select_lex= &session->getLex()->select_lex;
601
601
  Name_resolution_context *context= &select_lex->context;
602
602
  Name_resolution_context_state ctx_state;
603
603
  bool insert_into_view= (0 != 0);
838
838
        assert(table->insert_values.size());
839
839
        table->storeRecordAsInsert();
840
840
        table->restoreRecord();
841
 
        assert(info->update_fields->elements ==
842
 
                    info->update_values->elements);
 
841
        assert(info->update_fields->size() ==
 
842
                    info->update_values->size());
843
843
        if (fill_record(session, *info->update_fields,
844
844
                                                 *info->update_values,
845
845
                                                 info->ignore))
964
964
err:
965
965
  info->last_errno= error;
966
966
  /* current_select is NULL if this is a delayed insert */
967
 
  if (session->lex->current_select)
968
 
    session->lex->current_select->no_error= 0;        // Give error
 
967
  if (session->getLex()->current_select)
 
968
    session->getLex()->current_select->no_error= 0;        // Give error
969
969
  table->print_error(error,MYF(0));
970
970
 
971
971
before_err:
1040
1040
 
1041
1041
bool insert_select_prepare(Session *session)
1042
1042
{
1043
 
  LEX *lex= session->lex;
 
1043
  LEX *lex= session->getLex();
1044
1044
  Select_Lex *select_lex= &lex->select_lex;
1045
1045
 
1046
1046
  /*
1087
1087
int
1088
1088
select_insert::prepare(List<Item> &values, Select_Lex_Unit *u)
1089
1089
{
1090
 
  LEX *lex= session->lex;
1091
1090
  int res;
1092
1091
  table_map map= 0;
1093
 
  Select_Lex *lex_current_select_save= lex->current_select;
 
1092
  Select_Lex *lex_current_select_save= session->getLex()->current_select;
1094
1093
 
1095
1094
 
1096
1095
  unit= u;
1100
1099
    select, LEX::current_select should point to the first select while
1101
1100
    we are fixing fields from insert list.
1102
1101
  */
1103
 
  lex->current_select= &lex->select_lex;
 
1102
  session->getLex()->current_select= &session->getLex()->select_lex;
1104
1103
  res= check_insert_fields(session, table_list, *fields, values,
1105
1104
                           !insert_into_view, &map) ||
1106
1105
       setup_fields(session, 0, values, MARK_COLUMNS_READ, 0, 0);
1107
1106
 
1108
 
  if (!res && fields->elements)
 
1107
  if (!res && fields->size())
1109
1108
  {
1110
1109
    bool saved_abort_on_warning= session->abortOnWarning();
1111
1110
    session->setAbortOnWarning(not info.ignore);
1116
1115
 
1117
1116
  if (info.handle_duplicates == DUP_UPDATE && !res)
1118
1117
  {
1119
 
    Name_resolution_context *context= &lex->select_lex.context;
 
1118
    Name_resolution_context *context= &session->getLex()->select_lex.context;
1120
1119
    Name_resolution_context_state ctx_state;
1121
1120
 
1122
1121
    /* Save the state of the current name resolution context. */
1134
1133
      We use next_name_resolution_table descructively, so check it first (views?)
1135
1134
    */
1136
1135
    assert (!table_list->next_name_resolution_table);
1137
 
    if (lex->select_lex.group_list.elements == 0 &&
1138
 
        !lex->select_lex.with_sum_func)
 
1136
    if (session->getLex()->select_lex.group_list.elements == 0 and
 
1137
        not session->getLex()->select_lex.with_sum_func)
1139
1138
      /*
1140
1139
        We must make a single context out of the two separate name resolution contexts :
1141
1140
        the INSERT table and the tables in the SELECT part of INSERT ... SELECT.
1154
1153
        order to get correct values from those fields when the select
1155
1154
        employs a temporary table.
1156
1155
      */
1157
 
      List<Item>::iterator li(*info.update_values);
 
1156
      List<Item>::iterator li(info.update_values->begin());
1158
1157
      Item *item;
1159
1158
 
1160
1159
      while ((item= li++))
1161
1160
      {
1162
1161
        item->transform(&Item::update_value_transformer,
1163
 
                        (unsigned char*)lex->current_select);
 
1162
                        (unsigned char*)session->getLex()->current_select);
1164
1163
      }
1165
1164
    }
1166
1165
 
1168
1167
    ctx_state.restore_state(context, table_list);
1169
1168
  }
1170
1169
 
1171
 
  lex->current_select= lex_current_select_save;
 
1170
  session->getLex()->current_select= lex_current_select_save;
1172
1171
  if (res)
1173
1172
    return(1);
1174
1173
  /*
1184
1183
  if (unique_table(table_list, table_list->next_global))
1185
1184
  {
1186
1185
    /* Using same table for INSERT and SELECT */
1187
 
    lex->current_select->options|= OPTION_BUFFER_RESULT;
1188
 
    lex->current_select->join->select_options|= OPTION_BUFFER_RESULT;
 
1186
    session->getLex()->current_select->options|= OPTION_BUFFER_RESULT;
 
1187
    session->getLex()->current_select->join->select_options|= OPTION_BUFFER_RESULT;
1189
1188
  }
1190
 
  else if (!(lex->current_select->options & OPTION_BUFFER_RESULT))
 
1189
  else if (not (session->getLex()->current_select->options & OPTION_BUFFER_RESULT))
1191
1190
  {
1192
1191
    /*
1193
1192
      We must not yet prepare the result table if it is the same as one of the
1240
1239
 
1241
1240
int select_insert::prepare2(void)
1242
1241
{
1243
 
 
1244
 
  if (session->lex->current_select->options & OPTION_BUFFER_RESULT)
 
1242
  if (session->getLex()->current_select->options & OPTION_BUFFER_RESULT)
1245
1243
    table->cursor->ha_start_bulk_insert((ha_rows) 0);
 
1244
 
1246
1245
  return(0);
1247
1246
}
1248
1247
 
1271
1270
bool select_insert::send_data(List<Item> &values)
1272
1271
{
1273
1272
 
1274
 
  bool error=0;
 
1273
  bool error= false;
1275
1274
 
1276
1275
  if (unit->offset_limit_cnt)
1277
1276
  {                                             // using limit offset,count
1327
1326
 
1328
1327
void select_insert::store_values(List<Item> &values)
1329
1328
{
1330
 
  if (fields->elements)
 
1329
  if (fields->size())
1331
1330
    fill_record(session, *fields, values, true);
1332
1331
  else
1333
1332
    fill_record(session, table->getFields(), values, true);
1492
1491
                                      identifier::Table::const_reference identifier)
1493
1492
{
1494
1493
  TableShare share(message::Table::INTERNAL);
1495
 
  uint32_t select_field_count= items->elements;
 
1494
  uint32_t select_field_count= items->size();
1496
1495
  /* Add selected items to field list */
1497
 
  List_iterator_fast<Item> it(*items);
 
1496
  List<Item>::iterator it(items->begin());
1498
1497
  Item *item;
1499
1498
  Field *tmp_field;
1500
1499
 
1690
1689
    *m_plock= extra_lock;
1691
1690
  }
1692
1691
 
1693
 
  if (table->getShare()->sizeFields() < values.elements)
 
1692
  if (table->getShare()->sizeFields() < values.size())
1694
1693
  {
1695
1694
    my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), 1);
1696
1695
    return(-1);
1697
1696
  }
1698
1697
 
1699
1698
 /* First field to copy */
1700
 
  field= table->getFields() + table->getShare()->sizeFields() - values.elements;
 
1699
  field= table->getFields() + table->getShare()->sizeFields() - values.size();
1701
1700
 
1702
1701
  /* Mark all fields that are given values */
1703
1702
  for (Field **f= field ; *f ; f++)