~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_insert.cc

new date/time additions

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>
 
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"
36
34
 
37
 
#include <drizzled/table/shell.h>
 
35
#include "drizzled/table/shell.h"
38
36
 
39
37
namespace drizzled
40
38
{
87
85
  }
88
86
  else
89
87
  {                                             // Part field list
90
 
    Select_Lex *select_lex= &session->getLex()->select_lex;
 
88
    Select_Lex *select_lex= &session->lex->select_lex;
91
89
    Name_resolution_context *context= &select_lex->context;
92
90
    Name_resolution_context_state ctx_state;
93
91
    int res;
245
243
  uint64_t id;
246
244
  CopyInfo info;
247
245
  Table *table= 0;
248
 
  List<List_item>::iterator its(values_list.begin());
 
246
  List_iterator_fast<List_item> its(values_list);
249
247
  List_item *values;
250
248
  Name_resolution_context *context;
251
249
  Name_resolution_context_state ctx_state;
282
280
    if (table != NULL)
283
281
      table->cursor->ha_release_auto_increment();
284
282
    if (!joins_freed)
285
 
      free_underlaid_joins(session, &session->getLex()->select_lex);
286
 
    session->setAbortOnWarning(false);
 
283
      free_underlaid_joins(session, &session->lex->select_lex);
 
284
    session->abort_on_warning= 0;
287
285
    DRIZZLE_INSERT_DONE(1, 0);
288
286
    return true;
289
287
  }
291
289
  /* mysql_prepare_insert set table_list->table if it was not set */
292
290
  table= table_list->table;
293
291
 
294
 
  context= &session->getLex()->select_lex.context;
 
292
  context= &session->lex->select_lex.context;
295
293
  /*
296
294
    These three asserts test the hypothesis that the resetting of the name
297
295
    resolution context below is not necessary at all since the list of local
321
319
      if (table != NULL)
322
320
        table->cursor->ha_release_auto_increment();
323
321
      if (!joins_freed)
324
 
        free_underlaid_joins(session, &session->getLex()->select_lex);
325
 
      session->setAbortOnWarning(false);
 
322
        free_underlaid_joins(session, &session->lex->select_lex);
 
323
      session->abort_on_warning= 0;
326
324
      DRIZZLE_INSERT_DONE(1, 0);
327
325
 
328
326
      return true;
332
330
      if (table != NULL)
333
331
        table->cursor->ha_release_auto_increment();
334
332
      if (!joins_freed)
335
 
        free_underlaid_joins(session, &session->getLex()->select_lex);
336
 
      session->setAbortOnWarning(false);
 
333
        free_underlaid_joins(session, &session->lex->select_lex);
 
334
      session->abort_on_warning= 0;
337
335
      DRIZZLE_INSERT_DONE(1, 0);
338
336
      return true;
339
337
    }
340
338
  }
341
 
  its= values_list.begin();
 
339
  its.rewind ();
342
340
 
343
341
  /* Restore the current context. */
344
342
  ctx_state.restore_state(context, table_list);
374
372
  }
375
373
 
376
374
 
377
 
  session->setAbortOnWarning(not ignore);
 
375
  session->abort_on_warning= !ignore;
378
376
 
379
377
  table->mark_columns_needed_for_insert();
380
378
 
424
422
    session->row_count++;
425
423
  }
426
424
 
427
 
  free_underlaid_joins(session, &session->getLex()->select_lex);
 
425
  free_underlaid_joins(session, &session->lex->select_lex);
428
426
  joins_freed= true;
429
427
 
430
428
  /*
484
482
    if (table != NULL)
485
483
      table->cursor->ha_release_auto_increment();
486
484
    if (!joins_freed)
487
 
      free_underlaid_joins(session, &session->getLex()->select_lex);
488
 
    session->setAbortOnWarning(false);
 
485
      free_underlaid_joins(session, &session->lex->select_lex);
 
486
    session->abort_on_warning= 0;
489
487
    DRIZZLE_INSERT_DONE(1, 0);
490
488
    return true;
491
489
  }
494
492
                                    !session->cuted_fields))
495
493
  {
496
494
    session->row_count_func= info.copied + info.deleted + info.updated;
497
 
    session->my_ok((ulong) session->rowCount(),
 
495
    session->my_ok((ulong) session->row_count_func,
498
496
                   info.copied + info.deleted + info.touched, id);
499
497
  }
500
498
  else
507
505
      snprintf(buff, sizeof(buff), ER(ER_INSERT_INFO), (ulong) info.records,
508
506
              (ulong) (info.deleted + info.updated), (ulong) session->cuted_fields);
509
507
    session->row_count_func= info.copied + info.deleted + info.updated;
510
 
    session->my_ok((ulong) session->rowCount(),
 
508
    session->my_ok((ulong) session->row_count_func,
511
509
                   info.copied + info.deleted + info.touched, id, buff);
512
510
  }
513
 
  session->status_var.inserted_row_count+= session->rowCount();
514
 
  session->setAbortOnWarning(false);
515
 
  DRIZZLE_INSERT_DONE(0, session->rowCount());
 
511
  session->status_var.inserted_row_count+= session->row_count_func;
 
512
  session->abort_on_warning= 0;
 
513
  DRIZZLE_INSERT_DONE(0, session->row_count_func);
516
514
 
517
515
  return false;
518
516
}
547
545
     than INSERT.
548
546
  */
549
547
 
550
 
  if (setup_tables_and_check_access(session, &session->getLex()->select_lex.context,
551
 
                                    &session->getLex()->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,
552
550
                                    table_list,
553
 
                                    &session->getLex()->select_lex.leaf_tables,
 
551
                                    &session->lex->select_lex.leaf_tables,
554
552
                                    select_insert))
555
553
    return(true);
556
554
 
597
595
                          bool select_insert,
598
596
                          bool check_fields, bool abort_on_warning)
599
597
{
600
 
  Select_Lex *select_lex= &session->getLex()->select_lex;
 
598
  Select_Lex *select_lex= &session->lex->select_lex;
601
599
  Name_resolution_context *context= &select_lex->context;
602
600
  Name_resolution_context_state ctx_state;
603
601
  bool insert_into_view= (0 != 0);
612
610
    inserting (for INSERT ... SELECT this is done by changing table_list,
613
611
    because INSERT ... SELECT share Select_Lex it with SELECT.
614
612
  */
615
 
  if (not select_insert)
 
613
  if (!select_insert)
616
614
  {
617
615
    for (Select_Lex_Unit *un= select_lex->first_inner_unit();
618
616
         un;
660
658
 
661
659
    if (!res && check_fields)
662
660
    {
663
 
      bool saved_abort_on_warning= session->abortOnWarning();
664
 
 
665
 
      session->setAbortOnWarning(abort_on_warning);
 
661
      bool saved_abort_on_warning= session->abort_on_warning;
 
662
      session->abort_on_warning= abort_on_warning;
666
663
      res= check_that_all_fields_are_given_values(session,
667
664
                                                  table ? table :
668
665
                                                  context->table_list->table,
669
666
                                                  context->table_list);
670
 
      session->setAbortOnWarning(saved_abort_on_warning);
 
667
      session->abort_on_warning= saved_abort_on_warning;
671
668
    }
672
669
 
673
670
    if (!res && duplic == DUP_UPDATE)
678
675
    /* Restore the current context. */
679
676
    ctx_state.restore_state(context, table_list);
680
677
 
681
 
    if (not res)
 
678
    if (!res)
682
679
      res= setup_fields(session, 0, update_values, MARK_COLUMNS_READ, 0, 0);
683
680
  }
684
681
 
685
682
  if (res)
686
683
    return(res);
687
684
 
688
 
  if (not table)
 
685
  if (!table)
689
686
    table= table_list->table;
690
687
 
691
 
  if (not select_insert)
 
688
  if (!select_insert)
692
689
  {
693
690
    TableList *duplicate;
694
691
    if ((duplicate= unique_table(table_list, table_list->next_global, true)))
698
695
      return true;
699
696
    }
700
697
  }
701
 
 
702
698
  if (duplic == DUP_UPDATE || duplic == DUP_REPLACE)
703
699
    table->prepare_for_position();
704
700
 
964
960
err:
965
961
  info->last_errno= error;
966
962
  /* current_select is NULL if this is a delayed insert */
967
 
  if (session->getLex()->current_select)
968
 
    session->getLex()->current_select->no_error= 0;        // Give error
 
963
  if (session->lex->current_select)
 
964
    session->lex->current_select->no_error= 0;        // Give error
969
965
  table->print_error(error,MYF(0));
970
966
 
971
967
before_err:
1018
1014
      }
1019
1015
    }
1020
1016
  }
1021
 
  return session->abortOnWarning() ? err : 0;
 
1017
  return session->abort_on_warning ? err : 0;
1022
1018
}
1023
1019
 
1024
1020
/***************************************************************************
1040
1036
 
1041
1037
bool insert_select_prepare(Session *session)
1042
1038
{
1043
 
  LEX *lex= session->getLex();
 
1039
  LEX *lex= session->lex;
1044
1040
  Select_Lex *select_lex= &lex->select_lex;
1045
1041
 
1046
1042
  /*
1087
1083
int
1088
1084
select_insert::prepare(List<Item> &values, Select_Lex_Unit *u)
1089
1085
{
 
1086
  LEX *lex= session->lex;
1090
1087
  int res;
1091
1088
  table_map map= 0;
1092
 
  Select_Lex *lex_current_select_save= session->getLex()->current_select;
 
1089
  Select_Lex *lex_current_select_save= lex->current_select;
1093
1090
 
1094
1091
 
1095
1092
  unit= u;
1099
1096
    select, LEX::current_select should point to the first select while
1100
1097
    we are fixing fields from insert list.
1101
1098
  */
1102
 
  session->getLex()->current_select= &session->getLex()->select_lex;
 
1099
  lex->current_select= &lex->select_lex;
1103
1100
  res= check_insert_fields(session, table_list, *fields, values,
1104
1101
                           !insert_into_view, &map) ||
1105
1102
       setup_fields(session, 0, values, MARK_COLUMNS_READ, 0, 0);
1106
1103
 
1107
1104
  if (!res && fields->elements)
1108
1105
  {
1109
 
    bool saved_abort_on_warning= session->abortOnWarning();
1110
 
    session->setAbortOnWarning(not info.ignore);
 
1106
    bool saved_abort_on_warning= session->abort_on_warning;
 
1107
    session->abort_on_warning= !info.ignore;
1111
1108
    res= check_that_all_fields_are_given_values(session, table_list->table,
1112
1109
                                                table_list);
1113
 
    session->setAbortOnWarning(saved_abort_on_warning);
 
1110
    session->abort_on_warning= saved_abort_on_warning;
1114
1111
  }
1115
1112
 
1116
1113
  if (info.handle_duplicates == DUP_UPDATE && !res)
1117
1114
  {
1118
 
    Name_resolution_context *context= &session->getLex()->select_lex.context;
 
1115
    Name_resolution_context *context= &lex->select_lex.context;
1119
1116
    Name_resolution_context_state ctx_state;
1120
1117
 
1121
1118
    /* Save the state of the current name resolution context. */
1133
1130
      We use next_name_resolution_table descructively, so check it first (views?)
1134
1131
    */
1135
1132
    assert (!table_list->next_name_resolution_table);
1136
 
    if (session->getLex()->select_lex.group_list.elements == 0 and
1137
 
        not session->getLex()->select_lex.with_sum_func)
 
1133
    if (lex->select_lex.group_list.elements == 0 &&
 
1134
        !lex->select_lex.with_sum_func)
1138
1135
      /*
1139
1136
        We must make a single context out of the two separate name resolution contexts :
1140
1137
        the INSERT table and the tables in the SELECT part of INSERT ... SELECT.
1153
1150
        order to get correct values from those fields when the select
1154
1151
        employs a temporary table.
1155
1152
      */
1156
 
      List<Item>::iterator li(info.update_values->begin());
 
1153
      List_iterator<Item> li(*info.update_values);
1157
1154
      Item *item;
1158
1155
 
1159
1156
      while ((item= li++))
1160
1157
      {
1161
1158
        item->transform(&Item::update_value_transformer,
1162
 
                        (unsigned char*)session->getLex()->current_select);
 
1159
                        (unsigned char*)lex->current_select);
1163
1160
      }
1164
1161
    }
1165
1162
 
1167
1164
    ctx_state.restore_state(context, table_list);
1168
1165
  }
1169
1166
 
1170
 
  session->getLex()->current_select= lex_current_select_save;
 
1167
  lex->current_select= lex_current_select_save;
1171
1168
  if (res)
1172
1169
    return(1);
1173
1170
  /*
1183
1180
  if (unique_table(table_list, table_list->next_global))
1184
1181
  {
1185
1182
    /* Using same table for INSERT and SELECT */
1186
 
    session->getLex()->current_select->options|= OPTION_BUFFER_RESULT;
1187
 
    session->getLex()->current_select->join->select_options|= OPTION_BUFFER_RESULT;
 
1183
    lex->current_select->options|= OPTION_BUFFER_RESULT;
 
1184
    lex->current_select->join->select_options|= OPTION_BUFFER_RESULT;
1188
1185
  }
1189
 
  else if (not (session->getLex()->current_select->options & OPTION_BUFFER_RESULT))
 
1186
  else if (!(lex->current_select->options & OPTION_BUFFER_RESULT))
1190
1187
  {
1191
1188
    /*
1192
1189
      We must not yet prepare the result table if it is the same as one of the
1203
1200
  table->next_number_field=table->found_next_number_field;
1204
1201
 
1205
1202
  session->cuted_fields=0;
1206
 
 
1207
1203
  if (info.ignore || info.handle_duplicates != DUP_ERROR)
1208
1204
    table->cursor->extra(HA_EXTRA_IGNORE_DUP_KEY);
1209
 
 
1210
1205
  if (info.handle_duplicates == DUP_REPLACE)
1211
1206
    table->cursor->extra(HA_EXTRA_WRITE_CAN_REPLACE);
1212
 
 
1213
1207
  if (info.handle_duplicates == DUP_UPDATE)
1214
1208
    table->cursor->extra(HA_EXTRA_INSERT_WITH_UPDATE);
1215
 
 
1216
 
  session->setAbortOnWarning(not info.ignore);
 
1209
  session->abort_on_warning= !info.ignore;
1217
1210
  table->mark_columns_needed_for_insert();
1218
1211
 
1219
1212
 
1239
1232
 
1240
1233
int select_insert::prepare2(void)
1241
1234
{
1242
 
  if (session->getLex()->current_select->options & OPTION_BUFFER_RESULT)
 
1235
 
 
1236
  if (session->lex->current_select->options & OPTION_BUFFER_RESULT)
1243
1237
    table->cursor->ha_start_bulk_insert((ha_rows) 0);
1244
 
 
1245
1238
  return(0);
1246
1239
}
1247
1240
 
1262
1255
    table->cursor->ha_reset();
1263
1256
  }
1264
1257
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
1265
 
  session->setAbortOnWarning(false);
 
1258
  session->abort_on_warning= 0;
1266
1259
  return;
1267
1260
}
1268
1261
 
1270
1263
bool select_insert::send_data(List<Item> &values)
1271
1264
{
1272
1265
 
1273
 
  bool error= false;
 
1266
  bool error=0;
1274
1267
 
1275
1268
  if (unit->offset_limit_cnt)
1276
1269
  {                                             // using limit offset,count
1277
1270
    unit->offset_limit_cnt--;
1278
 
    return false;
 
1271
    return(0);
1279
1272
  }
1280
1273
 
1281
1274
  session->count_cuted_fields= CHECK_FIELD_WARN;        // Calculate cuted fields
1282
1275
  store_values(values);
1283
1276
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
1284
1277
  if (session->is_error())
1285
 
    return true;
 
1278
    return(1);
1286
1279
 
1287
1280
  // Release latches in case bulk insert takes a long time
1288
1281
  plugin::TransactionalStorageEngine::releaseTemporaryLatches(session);
1383
1376
    (session->arg_of_last_insert_id_function ?
1384
1377
     session->first_successful_insert_id_in_prev_stmt :
1385
1378
     (info.copied ? autoinc_value_of_last_inserted_row : 0));
1386
 
  session->my_ok((ulong) session->rowCount(),
 
1379
  session->my_ok((ulong) session->row_count_func,
1387
1380
                 info.copied + info.deleted + info.touched, id, buff);
1388
 
  session->status_var.inserted_row_count+= session->rowCount(); 
1389
 
  DRIZZLE_INSERT_SELECT_DONE(0, session->rowCount());
 
1381
  session->status_var.inserted_row_count+= session->row_count_func; 
 
1382
  DRIZZLE_INSERT_SELECT_DONE(0, session->row_count_func);
1390
1383
  return 0;
1391
1384
}
1392
1385
 
1493
1486
  TableShare share(message::Table::INTERNAL);
1494
1487
  uint32_t select_field_count= items->elements;
1495
1488
  /* Add selected items to field list */
1496
 
  List<Item>::iterator it(items->begin());
 
1489
  List_iterator_fast<Item> it(*items);
1497
1490
  Item *item;
1498
1491
  Field *tmp_field;
1499
1492
 
1515
1508
 
1516
1509
  {
1517
1510
    table::Shell tmp_table(share);              // Used during 'CreateField()'
 
1511
    tmp_table.timestamp_field= 0;
 
1512
 
 
1513
    tmp_table.getMutableShare()->db_create_options= 0;
 
1514
    tmp_table.getMutableShare()->blob_ptr_size= portable_sizeof_char_ptr;
1518
1515
 
1519
1516
    if (not table_proto.engine().name().compare("MyISAM"))
1520
1517
      tmp_table.getMutableShare()->db_low_byte_first= true;
1521
1518
    else if (not table_proto.engine().name().compare("MEMORY"))
1522
1519
      tmp_table.getMutableShare()->db_low_byte_first= true;
1523
1520
 
 
1521
    tmp_table.null_row= false;
 
1522
    tmp_table.maybe_null= false;
 
1523
 
1524
1524
    tmp_table.in_use= session;
1525
1525
 
1526
1526
    while ((item=it++))
1644
1644
 
1645
1645
    if (not create_info->table_existed)
1646
1646
      session->drop_open_table(table, identifier);
1647
 
 
1648
1647
    return NULL;
1649
1648
  }
1650
1649
 
1712
1711
  session->cuted_fields=0;
1713
1712
  if (info.ignore || info.handle_duplicates != DUP_ERROR)
1714
1713
    table->cursor->extra(HA_EXTRA_IGNORE_DUP_KEY);
1715
 
 
1716
1714
  if (info.handle_duplicates == DUP_REPLACE)
1717
1715
    table->cursor->extra(HA_EXTRA_WRITE_CAN_REPLACE);
1718
 
 
1719
1716
  if (info.handle_duplicates == DUP_UPDATE)
1720
1717
    table->cursor->extra(HA_EXTRA_INSERT_WITH_UPDATE);
1721
 
 
1722
1718
  table->cursor->ha_start_bulk_insert((ha_rows) 0);
1723
 
  session->setAbortOnWarning(not info.ignore);
 
1719
  session->abort_on_warning= !info.ignore;
1724
1720
  if (check_that_all_fields_are_given_values(session, table, table_list))
1725
1721
    return(1);
1726
 
 
1727
1722
  table->mark_columns_needed_for_insert();
1728
1723
  table->cursor->extra(HA_EXTRA_WRITE_CACHE);
1729
1724
  return(0);
1767
1762
    if (!table->getShare()->getType())
1768
1763
    {
1769
1764
      TransactionServices &transaction_services= TransactionServices::singleton();
1770
 
      transaction_services.autocommitOrRollback(*session, 0);
 
1765
      transaction_services.autocommitOrRollback(session, 0);
1771
1766
      (void) session->endActiveTransaction();
1772
1767
    }
1773
1768