~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_insert.cc

  • Committer: Olaf van der Spek
  • Date: 2011-02-28 14:09:50 UTC
  • mfrom: (2207 bootstrap)
  • mto: (2209.1.2 build)
  • mto: This revision was merged to the branch mainline in revision 2210.
  • Revision ID: olafvdspek@gmail.com-20110228140950-2nu0hyzhuww3wssx
Merge trunk

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