~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_insert.cc

  • Committer: Brian Aker
  • Date: 2011-02-12 08:10:17 UTC
  • mto: This revision was merged to the branch mainline in revision 2161.
  • Revision ID: brian@tangent.org-20110212081017-7793i41ybt7gp5ty
More removal of session from includes.

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