~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_insert.cc

  • Committer: lbieber
  • Date: 2010-10-01 13:06:31 UTC
  • mfrom: (1802.2.2 drizzle-bug-651948)
  • mto: This revision was merged to the branch mainline in revision 1805.
  • Revision ID: lbieber@orisndriz08-20101001130631-xubscnhmj7r5dn6g
Merge Andrew - Fix bug 651948 - Index lengths not retrieved using drizzledump

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
13
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
16
 
17
17
/* Insert of records */
25
25
#include <drizzled/probes.h>
26
26
#include <drizzled/sql_base.h>
27
27
#include <drizzled/sql_load.h>
28
 
#include <drizzled/field/epoch.h>
 
28
#include <drizzled/field/timestamp.h>
29
29
#include <drizzled/lock.h>
30
30
#include "drizzled/sql_table.h"
31
31
#include "drizzled/pthread_globals.h"
32
32
#include "drizzled/transaction_services.h"
33
33
#include "drizzled/plugin/transactional_storage_engine.h"
34
34
 
35
 
#include "drizzled/table/shell.h"
36
 
 
37
35
namespace drizzled
38
36
{
39
37
 
129
127
      }
130
128
      else
131
129
      {
132
 
        table->setWriteSet(table->timestamp_field->position());
 
130
        table->setWriteSet(table->timestamp_field->field_index);
133
131
      }
134
132
    }
135
133
  }
170
168
      Unmark the timestamp field so that we can check if this is modified
171
169
      by update_fields
172
170
    */
173
 
    timestamp_mark= table->write_set->test(table->timestamp_field->position());
174
 
    table->write_set->reset(table->timestamp_field->position());
 
171
    timestamp_mark= table->write_set->testAndClear(table->timestamp_field->field_index);
175
172
  }
176
173
 
177
174
  /* Check the fields we are going to modify */
189
186
 
190
187
    if (timestamp_mark)
191
188
    {
192
 
      table->setWriteSet(table->timestamp_field->position());
 
189
      table->setWriteSet(table->timestamp_field->field_index);
193
190
    }
194
191
  }
195
192
  return 0;
227
224
  end of dispatch_command().
228
225
*/
229
226
 
230
 
bool insert_query(Session *session,TableList *table_list,
 
227
bool mysql_insert(Session *session,TableList *table_list,
231
228
                  List<Item> &fields,
232
229
                  List<List_item> &values_list,
233
230
                  List<Item> &update_fields,
271
268
  values= its++;
272
269
  value_count= values->elements;
273
270
 
274
 
  if (prepare_insert(session, table_list, table, fields, values,
 
271
  if (mysql_prepare_insert(session, table_list, table, fields, values,
275
272
                           update_fields, update_values, duplic, &unused_conds,
276
273
                           false,
277
274
                           (fields.elements || !value_count ||
278
275
                            (0) != 0), !ignore))
279
 
  {
280
 
    if (table != NULL)
281
 
      table->cursor->ha_release_auto_increment();
282
 
    if (!joins_freed)
283
 
      free_underlaid_joins(session, &session->lex->select_lex);
284
 
    session->setAbortOnWarning(false);
285
 
    DRIZZLE_INSERT_DONE(1, 0);
286
 
    return true;
287
 
  }
 
276
    goto abort;
288
277
 
289
278
  /* mysql_prepare_insert set table_list->table if it was not set */
290
279
  table= table_list->table;
315
304
    if (values->elements != value_count)
316
305
    {
317
306
      my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), counter);
318
 
 
319
 
      if (table != NULL)
320
 
        table->cursor->ha_release_auto_increment();
321
 
      if (!joins_freed)
322
 
        free_underlaid_joins(session, &session->lex->select_lex);
323
 
      session->setAbortOnWarning(false);
324
 
      DRIZZLE_INSERT_DONE(1, 0);
325
 
 
326
 
      return true;
 
307
      goto abort;
327
308
    }
328
309
    if (setup_fields(session, 0, *values, MARK_COLUMNS_READ, 0, 0))
329
 
    {
330
 
      if (table != NULL)
331
 
        table->cursor->ha_release_auto_increment();
332
 
      if (!joins_freed)
333
 
        free_underlaid_joins(session, &session->lex->select_lex);
334
 
      session->setAbortOnWarning(false);
335
 
      DRIZZLE_INSERT_DONE(1, 0);
336
 
      return true;
337
 
    }
 
310
      goto abort;
338
311
  }
339
312
  its.rewind ();
340
313
 
372
345
  }
373
346
 
374
347
 
375
 
  session->setAbortOnWarning(not ignore);
 
348
  session->abort_on_warning= !ignore;
376
349
 
377
350
  table->mark_columns_needed_for_insert();
378
351
 
478
451
    table->cursor->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
479
452
 
480
453
  if (error)
481
 
  {
482
 
    if (table != NULL)
483
 
      table->cursor->ha_release_auto_increment();
484
 
    if (!joins_freed)
485
 
      free_underlaid_joins(session, &session->lex->select_lex);
486
 
    session->setAbortOnWarning(false);
487
 
    DRIZZLE_INSERT_DONE(1, 0);
488
 
    return true;
489
 
  }
490
 
 
 
454
    goto abort;
491
455
  if (values_list.elements == 1 && (!(session->options & OPTION_WARNINGS) ||
492
456
                                    !session->cuted_fields))
493
457
  {
494
458
    session->row_count_func= info.copied + info.deleted + info.updated;
495
 
    session->my_ok((ulong) session->rowCount(),
 
459
    session->my_ok((ulong) session->row_count_func,
496
460
                   info.copied + info.deleted + info.touched, id);
497
461
  }
498
462
  else
505
469
      snprintf(buff, sizeof(buff), ER(ER_INSERT_INFO), (ulong) info.records,
506
470
              (ulong) (info.deleted + info.updated), (ulong) session->cuted_fields);
507
471
    session->row_count_func= info.copied + info.deleted + info.updated;
508
 
    session->my_ok((ulong) session->rowCount(),
 
472
    session->my_ok((ulong) session->row_count_func,
509
473
                   info.copied + info.deleted + info.touched, id, buff);
510
474
  }
511
 
  session->status_var.inserted_row_count+= session->rowCount();
512
 
  session->setAbortOnWarning(false);
513
 
  DRIZZLE_INSERT_DONE(0, session->rowCount());
514
 
 
 
475
  session->status_var.inserted_row_count+= session->row_count_func;
 
476
  session->abort_on_warning= 0;
 
477
  DRIZZLE_INSERT_DONE(0, session->row_count_func);
515
478
  return false;
 
479
 
 
480
abort:
 
481
  if (table != NULL)
 
482
    table->cursor->ha_release_auto_increment();
 
483
  if (!joins_freed)
 
484
    free_underlaid_joins(session, &session->lex->select_lex);
 
485
  session->abort_on_warning= 0;
 
486
  DRIZZLE_INSERT_DONE(1, 0);
 
487
  return true;
516
488
}
517
489
 
518
490
 
520
492
  Check if table can be updated
521
493
 
522
494
  SYNOPSIS
523
 
     prepare_insert_check_table()
 
495
     mysql_prepare_insert_check_table()
524
496
     session            Thread handle
525
497
     table_list         Table list
526
498
     fields             List of fields to be updated
532
504
     true  ERROR
533
505
*/
534
506
 
535
 
static bool prepare_insert_check_table(Session *session, TableList *table_list,
 
507
static bool mysql_prepare_insert_check_table(Session *session, TableList *table_list,
536
508
                                             List<Item> &,
537
509
                                             bool select_insert)
538
510
{
560
532
  Prepare items in INSERT statement
561
533
 
562
534
  SYNOPSIS
563
 
    prepare_insert()
 
535
    mysql_prepare_insert()
564
536
    session                     Thread handler
565
537
    table_list          Global/local table list
566
538
    table               Table to insert into (can be NULL if table should
587
559
    true  error
588
560
*/
589
561
 
590
 
bool prepare_insert(Session *session, TableList *table_list,
 
562
bool mysql_prepare_insert(Session *session, TableList *table_list,
591
563
                          Table *table, List<Item> &fields, List_item *values,
592
564
                          List<Item> &update_fields, List<Item> &update_values,
593
565
                          enum_duplicates duplic,
610
582
    inserting (for INSERT ... SELECT this is done by changing table_list,
611
583
    because INSERT ... SELECT share Select_Lex it with SELECT.
612
584
  */
613
 
  if (not select_insert)
 
585
  if (!select_insert)
614
586
  {
615
587
    for (Select_Lex_Unit *un= select_lex->first_inner_unit();
616
588
         un;
632
604
      return(true);
633
605
  }
634
606
 
635
 
  if (prepare_insert_check_table(session, table_list, fields, select_insert))
 
607
  if (mysql_prepare_insert_check_table(session, table_list, fields, select_insert))
636
608
    return(true);
637
609
 
638
610
 
658
630
 
659
631
    if (!res && check_fields)
660
632
    {
661
 
      bool saved_abort_on_warning= session->abortOnWarning();
662
 
 
663
 
      session->setAbortOnWarning(abort_on_warning);
 
633
      bool saved_abort_on_warning= session->abort_on_warning;
 
634
      session->abort_on_warning= abort_on_warning;
664
635
      res= check_that_all_fields_are_given_values(session,
665
636
                                                  table ? table :
666
637
                                                  context->table_list->table,
667
638
                                                  context->table_list);
668
 
      session->setAbortOnWarning(saved_abort_on_warning);
 
639
      session->abort_on_warning= saved_abort_on_warning;
669
640
    }
670
641
 
671
642
    if (!res && duplic == DUP_UPDATE)
676
647
    /* Restore the current context. */
677
648
    ctx_state.restore_state(context, table_list);
678
649
 
679
 
    if (not res)
 
650
    if (!res)
680
651
      res= setup_fields(session, 0, update_values, MARK_COLUMNS_READ, 0, 0);
681
652
  }
682
653
 
683
654
  if (res)
684
655
    return(res);
685
656
 
686
 
  if (not table)
 
657
  if (!table)
687
658
    table= table_list->table;
688
659
 
689
 
  if (not select_insert)
 
660
  if (!select_insert)
690
661
  {
691
662
    TableList *duplicate;
692
663
    if ((duplicate= unique_table(table_list, table_list->next_global, true)))
696
667
      return true;
697
668
    }
698
669
  }
699
 
 
700
670
  if (duplic == DUP_UPDATE || duplic == DUP_REPLACE)
701
671
    table->prepare_for_position();
702
672
 
746
716
{
747
717
  int error;
748
718
  std::vector<unsigned char> key;
749
 
  boost::dynamic_bitset<> *save_read_set, *save_write_set;
 
719
  MyBitmap *save_read_set, *save_write_set;
750
720
  uint64_t prev_insert_id= table->cursor->next_insert_id;
751
721
  uint64_t insert_id_for_cur_row= 0;
752
722
 
848
818
          table->cursor->adjust_next_insert_id_after_explicit_value(
849
819
            table->next_number_field->val_int());
850
820
        info->touched++;
851
 
 
852
 
        if (! table->records_are_comparable() || table->compare_records())
 
821
        if ((table->cursor->getEngine()->check_flag(HTON_BIT_PARTIAL_COLUMN_READ) &&
 
822
             !bitmap_is_subset(table->write_set, table->read_set)) ||
 
823
            table->compare_record())
853
824
        {
854
825
          if ((error=table->cursor->updateRecord(table->getUpdateRecord(),
855
826
                                                table->getInsertRecord())) &&
870
841
          /*
871
842
            If ON DUP KEY UPDATE updates a row instead of inserting one, it's
872
843
            like a regular UPDATE statement: it should not affect the value of a
873
 
            next SELECT LAST_INSERT_ID() or insert_id().
 
844
            next SELECT LAST_INSERT_ID() or mysql_insert_id().
874
845
            Except if LAST_INSERT_ID(#) was in the INSERT query, which is
875
846
            handled separately by Session::arg_of_last_insert_id_function.
876
847
          */
939
910
    */
940
911
    if (table->read_set != save_read_set ||
941
912
        table->write_set != save_write_set)
942
 
      table->column_bitmaps_set(*save_read_set, *save_write_set);
 
913
      table->column_bitmaps_set(save_read_set, save_write_set);
943
914
  }
944
915
  else if ((error=table->cursor->insertRecord(table->getInsertRecord())))
945
916
  {
968
939
 
969
940
before_err:
970
941
  table->cursor->restore_auto_increment(prev_insert_id);
971
 
  table->column_bitmaps_set(*save_read_set, *save_write_set);
972
 
  return 1;
 
942
  table->column_bitmaps_set(save_read_set, save_write_set);
 
943
  return(1);
973
944
}
974
945
 
975
946
 
1016
987
      }
1017
988
    }
1018
989
  }
1019
 
  return session->abortOnWarning() ? err : 0;
 
990
  return session->abort_on_warning ? err : 0;
1020
991
}
1021
992
 
1022
993
/***************************************************************************
1028
999
  make insert specific preparation and checks after opening tables
1029
1000
 
1030
1001
  SYNOPSIS
1031
 
    insert_select_prepare()
 
1002
    mysql_insert_select_prepare()
1032
1003
    session         thread handler
1033
1004
 
1034
1005
  RETURN
1036
1007
    true  Error
1037
1008
*/
1038
1009
 
1039
 
bool insert_select_prepare(Session *session)
 
1010
bool mysql_insert_select_prepare(Session *session)
1040
1011
{
1041
1012
  LEX *lex= session->lex;
1042
1013
  Select_Lex *select_lex= &lex->select_lex;
1046
1017
    clause if table is VIEW
1047
1018
  */
1048
1019
 
1049
 
  if (prepare_insert(session, lex->query_tables,
 
1020
  if (mysql_prepare_insert(session, lex->query_tables,
1050
1021
                           lex->query_tables->table, lex->field_list, 0,
1051
1022
                           lex->update_list, lex->value_list,
1052
1023
                           lex->duplicates,
1105
1076
 
1106
1077
  if (!res && fields->elements)
1107
1078
  {
1108
 
    bool saved_abort_on_warning= session->abortOnWarning();
1109
 
    session->setAbortOnWarning(not info.ignore);
 
1079
    bool saved_abort_on_warning= session->abort_on_warning;
 
1080
    session->abort_on_warning= !info.ignore;
1110
1081
    res= check_that_all_fields_are_given_values(session, table_list->table,
1111
1082
                                                table_list);
1112
 
    session->setAbortOnWarning(saved_abort_on_warning);
 
1083
    session->abort_on_warning= saved_abort_on_warning;
1113
1084
  }
1114
1085
 
1115
1086
  if (info.handle_duplicates == DUP_UPDATE && !res)
1202
1173
  table->next_number_field=table->found_next_number_field;
1203
1174
 
1204
1175
  session->cuted_fields=0;
1205
 
 
1206
1176
  if (info.ignore || info.handle_duplicates != DUP_ERROR)
1207
1177
    table->cursor->extra(HA_EXTRA_IGNORE_DUP_KEY);
1208
 
 
1209
1178
  if (info.handle_duplicates == DUP_REPLACE)
1210
1179
    table->cursor->extra(HA_EXTRA_WRITE_CAN_REPLACE);
1211
 
 
1212
1180
  if (info.handle_duplicates == DUP_UPDATE)
1213
1181
    table->cursor->extra(HA_EXTRA_INSERT_WITH_UPDATE);
1214
 
 
1215
 
  session->setAbortOnWarning(not info.ignore);
 
1182
  session->abort_on_warning= !info.ignore;
1216
1183
  table->mark_columns_needed_for_insert();
1217
1184
 
1218
1185
 
1261
1228
    table->cursor->ha_reset();
1262
1229
  }
1263
1230
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
1264
 
  session->setAbortOnWarning(false);
 
1231
  session->abort_on_warning= 0;
1265
1232
  return;
1266
1233
}
1267
1234
 
1274
1241
  if (unit->offset_limit_cnt)
1275
1242
  {                                             // using limit offset,count
1276
1243
    unit->offset_limit_cnt--;
1277
 
    return false;
 
1244
    return(0);
1278
1245
  }
1279
1246
 
1280
1247
  session->count_cuted_fields= CHECK_FIELD_WARN;        // Calculate cuted fields
1281
1248
  store_values(values);
1282
1249
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
1283
1250
  if (session->is_error())
1284
 
    return true;
 
1251
    return(1);
1285
1252
 
1286
1253
  // Release latches in case bulk insert takes a long time
1287
1254
  plugin::TransactionalStorageEngine::releaseTemporaryLatches(session);
1331
1298
    fill_record(session, table->getFields(), values, true);
1332
1299
}
1333
1300
 
1334
 
void select_insert::send_error(drizzled::error_t errcode,const char *err)
 
1301
void select_insert::send_error(uint32_t errcode,const char *err)
1335
1302
{
 
1303
 
 
1304
 
1336
1305
  my_message(errcode, err, MYF(0));
 
1306
 
 
1307
  return;
1337
1308
}
1338
1309
 
1339
1310
 
1382
1353
    (session->arg_of_last_insert_id_function ?
1383
1354
     session->first_successful_insert_id_in_prev_stmt :
1384
1355
     (info.copied ? autoinc_value_of_last_inserted_row : 0));
1385
 
  session->my_ok((ulong) session->rowCount(),
 
1356
  session->my_ok((ulong) session->row_count_func,
1386
1357
                 info.copied + info.deleted + info.touched, id, buff);
1387
 
  session->status_var.inserted_row_count+= session->rowCount(); 
1388
 
  DRIZZLE_INSERT_SELECT_DONE(0, session->rowCount());
 
1358
  session->status_var.inserted_row_count+= session->row_count_func; 
 
1359
  DRIZZLE_INSERT_SELECT_DONE(0, session->row_count_func);
1389
1360
  return 0;
1390
1361
}
1391
1362
 
1487
1458
                                      List<Item> *items,
1488
1459
                                      bool is_if_not_exists,
1489
1460
                                      DrizzleLock **lock,
1490
 
                                      identifier::Table::const_reference identifier)
 
1461
                                      TableIdentifier &identifier)
1491
1462
{
 
1463
  Table tmp_table;              // Used during 'CreateField()'
1492
1464
  TableShare share(message::Table::INTERNAL);
 
1465
  Table *table= 0;
1493
1466
  uint32_t select_field_count= items->elements;
1494
1467
  /* Add selected items to field list */
1495
1468
  List_iterator_fast<Item> it(*items);
1496
1469
  Item *item;
1497
1470
  Field *tmp_field;
 
1471
  bool not_used;
1498
1472
 
1499
1473
  if (not (identifier.isTmp()) && create_table->table->db_stat)
1500
1474
  {
1504
1478
      create_info->table_existed= 1;            // Mark that table existed
1505
1479
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1506
1480
                          ER_TABLE_EXISTS_ERROR, ER(ER_TABLE_EXISTS_ERROR),
1507
 
                          create_table->getTableName());
 
1481
                          create_table->table_name);
1508
1482
      return create_table->table;
1509
1483
    }
1510
1484
 
1511
 
    my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_table->getTableName());
 
1485
    my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_table->table_name);
1512
1486
    return NULL;
1513
1487
  }
1514
1488
 
 
1489
  tmp_table.timestamp_field= 0;
 
1490
  tmp_table.setShare(&share);
 
1491
 
 
1492
  tmp_table.getMutableShare()->db_create_options= 0;
 
1493
  tmp_table.getMutableShare()->blob_ptr_size= portable_sizeof_char_ptr;
 
1494
 
 
1495
  if (not table_proto.engine().name().compare("MyISAM"))
 
1496
    tmp_table.getMutableShare()->db_low_byte_first= true;
 
1497
  else if (not table_proto.engine().name().compare("MEMORY"))
 
1498
    tmp_table.getMutableShare()->db_low_byte_first= true;
 
1499
 
 
1500
  tmp_table.null_row= false;
 
1501
  tmp_table.maybe_null= false;
 
1502
 
 
1503
  while ((item=it++))
1515
1504
  {
1516
 
    table::Shell tmp_table(share);              // Used during 'CreateField()'
1517
 
 
1518
 
    if (not table_proto.engine().name().compare("MyISAM"))
1519
 
      tmp_table.getMutableShare()->db_low_byte_first= true;
1520
 
    else if (not table_proto.engine().name().compare("MEMORY"))
1521
 
      tmp_table.getMutableShare()->db_low_byte_first= true;
1522
 
 
1523
 
    tmp_table.in_use= session;
1524
 
 
1525
 
    while ((item=it++))
1526
 
    {
1527
 
      CreateField *cr_field;
1528
 
      Field *field, *def_field;
1529
 
      if (item->type() == Item::FUNC_ITEM)
1530
 
      {
1531
 
        if (item->result_type() != STRING_RESULT)
1532
 
        {
1533
 
          field= item->tmp_table_field(&tmp_table);
1534
 
        }
1535
 
        else
1536
 
        {
1537
 
          field= item->tmp_table_field_from_field_type(&tmp_table, 0);
1538
 
        }
1539
 
      }
 
1505
    CreateField *cr_field;
 
1506
    Field *field, *def_field;
 
1507
    if (item->type() == Item::FUNC_ITEM)
 
1508
      if (item->result_type() != STRING_RESULT)
 
1509
        field= item->tmp_table_field(&tmp_table);
1540
1510
      else
1541
 
      {
1542
 
        field= create_tmp_field(session, &tmp_table, item, item->type(),
1543
 
                                (Item ***) 0, &tmp_field, &def_field, false,
1544
 
                                false, false, 0);
1545
 
      }
1546
 
 
1547
 
      if (!field ||
1548
 
          !(cr_field=new CreateField(field,(item->type() == Item::FIELD_ITEM ?
1549
 
                                            ((Item_field *)item)->field :
1550
 
                                            (Field*) 0))))
1551
 
      {
1552
 
        return NULL;
1553
 
      }
1554
 
 
1555
 
      if (item->maybe_null)
1556
 
      {
1557
 
        cr_field->flags &= ~NOT_NULL_FLAG;
1558
 
      }
1559
 
 
1560
 
      alter_info->create_list.push_back(cr_field);
1561
 
    }
 
1511
        field= item->tmp_table_field_from_field_type(&tmp_table, 0);
 
1512
    else
 
1513
      field= create_tmp_field(session, &tmp_table, item, item->type(),
 
1514
                              (Item ***) 0, &tmp_field, &def_field, false,
 
1515
                              false, false, 0);
 
1516
    if (!field ||
 
1517
        !(cr_field=new CreateField(field,(item->type() == Item::FIELD_ITEM ?
 
1518
                                           ((Item_field *)item)->field :
 
1519
                                           (Field*) 0))))
 
1520
      return NULL;
 
1521
    if (item->maybe_null)
 
1522
      cr_field->flags &= ~NOT_NULL_FLAG;
 
1523
    alter_info->create_list.push_back(cr_field);
1562
1524
  }
1563
1525
 
1564
1526
  /*
1568
1530
    creating base table on which name we have exclusive lock. So code below
1569
1531
    should not cause deadlocks or races.
1570
1532
  */
1571
 
  Table *table= 0;
1572
1533
  {
1573
 
    if (not create_table_no_lock(session,
1574
 
                                 identifier,
1575
 
                                 create_info,
1576
 
                                 table_proto,
1577
 
                                 alter_info,
1578
 
                                 false,
1579
 
                                 select_field_count,
1580
 
                                 is_if_not_exists))
 
1534
    if (not mysql_create_table_no_lock(session,
 
1535
                                       identifier,
 
1536
                                       create_info,
 
1537
                                       table_proto,
 
1538
                                       alter_info,
 
1539
                                       false,
 
1540
                                       select_field_count,
 
1541
                                       is_if_not_exists))
1581
1542
    {
1582
1543
      if (create_info->table_existed && not identifier.isTmp())
1583
1544
      {
1586
1547
          or it was created via different mysqld front-end to the
1587
1548
          cluster. We don't have much options but throw an error.
1588
1549
        */
1589
 
        my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_table->getTableName());
 
1550
        my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_table->table_name);
1590
1551
        return NULL;
1591
1552
      }
1592
1553
 
1593
1554
      if (not identifier.isTmp())
1594
1555
      {
1595
 
        /* CREATE TABLE... has found that the table already exists for insert and is adapting to use it */
1596
 
        boost::mutex::scoped_lock scopedLock(table::Cache::singleton().mutex());
1597
 
 
1598
 
        if (create_table->table)
 
1556
        LOCK_open.lock(); /* CREATE TABLE... has found that the table already exists for insert and is adapting to use it */
 
1557
        if (session->reopen_name_locked_table(create_table, false))
1599
1558
        {
1600
 
          table::Concurrent *concurrent_table= static_cast<table::Concurrent *>(create_table->table);
1601
 
 
1602
 
          if (concurrent_table->reopen_name_locked_table(create_table, session))
1603
 
          {
1604
 
            (void)plugin::StorageEngine::dropTable(*session, identifier);
1605
 
          }
1606
 
          else
1607
 
          {
1608
 
            table= create_table->table;
1609
 
          }
 
1559
          quick_rm_table(*session, identifier);
1610
1560
        }
1611
1561
        else
1612
 
        {
1613
 
          (void)plugin::StorageEngine::dropTable(*session, identifier);
1614
 
        }
 
1562
          table= create_table->table;
 
1563
        LOCK_open.unlock();
1615
1564
      }
1616
1565
      else
1617
1566
      {
1624
1573
            it preparable for open. But let us do close_temporary_table() here
1625
1574
            just in case.
1626
1575
          */
1627
 
          session->drop_temporary_table(identifier);
 
1576
          session->drop_temporary_table(create_table);
1628
1577
        }
1629
1578
      }
1630
1579
    }
1631
 
    if (not table)                                   // open failed
 
1580
    if (!table)                                   // open failed
1632
1581
      return NULL;
1633
1582
  }
1634
1583
 
1635
1584
  table->reginfo.lock_type=TL_WRITE;
1636
 
  if (not ((*lock)= session->lockTables(&table, 1, DRIZZLE_LOCK_IGNORE_FLUSH)))
 
1585
  if (! ((*lock)= mysql_lock_tables(session, &table, 1,
 
1586
                                    DRIZZLE_LOCK_IGNORE_FLUSH, &not_used)))
1637
1587
  {
1638
1588
    if (*lock)
1639
1589
    {
1640
 
      session->unlockTables(*lock);
 
1590
      mysql_unlock_tables(session, *lock);
1641
1591
      *lock= 0;
1642
1592
    }
1643
1593
 
1698
1648
 
1699
1649
  /* Mark all fields that are given values */
1700
1650
  for (Field **f= field ; *f ; f++)
1701
 
  {
1702
 
    table->setWriteSet((*f)->position());
1703
 
  }
 
1651
    table->setWriteSet((*f)->field_index);
1704
1652
 
1705
1653
  /* Don't set timestamp if used */
1706
1654
  table->timestamp_field_type= TIMESTAMP_NO_AUTO_SET;
1710
1658
  session->cuted_fields=0;
1711
1659
  if (info.ignore || info.handle_duplicates != DUP_ERROR)
1712
1660
    table->cursor->extra(HA_EXTRA_IGNORE_DUP_KEY);
1713
 
 
1714
1661
  if (info.handle_duplicates == DUP_REPLACE)
1715
1662
    table->cursor->extra(HA_EXTRA_WRITE_CAN_REPLACE);
1716
 
 
1717
1663
  if (info.handle_duplicates == DUP_UPDATE)
1718
1664
    table->cursor->extra(HA_EXTRA_INSERT_WITH_UPDATE);
1719
 
 
1720
1665
  table->cursor->ha_start_bulk_insert((ha_rows) 0);
1721
 
  session->setAbortOnWarning(not info.ignore);
 
1666
  session->abort_on_warning= !info.ignore;
1722
1667
  if (check_that_all_fields_are_given_values(session, table, table_list))
1723
1668
    return(1);
1724
 
 
1725
1669
  table->mark_columns_needed_for_insert();
1726
1670
  table->cursor->extra(HA_EXTRA_WRITE_CACHE);
1727
1671
  return(0);
1733
1677
}
1734
1678
 
1735
1679
 
1736
 
void select_create::send_error(drizzled::error_t errcode,const char *err)
 
1680
void select_create::send_error(uint32_t errcode,const char *err)
1737
1681
{
1738
1682
  /*
1739
1683
    This will execute any rollbacks that are necessary before writing
1747
1691
 
1748
1692
  */
1749
1693
  select_insert::send_error(errcode, err);
 
1694
 
 
1695
  return;
1750
1696
}
1751
1697
 
1752
1698
 
1765
1711
    if (!table->getShare()->getType())
1766
1712
    {
1767
1713
      TransactionServices &transaction_services= TransactionServices::singleton();
1768
 
      transaction_services.autocommitOrRollback(*session, 0);
 
1714
      transaction_services.autocommitOrRollback(session, 0);
1769
1715
      (void) session->endActiveTransaction();
1770
1716
    }
1771
1717
 
1773
1719
    table->cursor->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
1774
1720
    if (m_plock)
1775
1721
    {
1776
 
      session->unlockTables(*m_plock);
 
1722
      mysql_unlock_tables(session, *m_plock);
1777
1723
      *m_plock= NULL;
1778
1724
      m_plock= NULL;
1779
1725
    }
1803
1749
 
1804
1750
  if (m_plock)
1805
1751
  {
1806
 
    session->unlockTables(*m_plock);
 
1752
    mysql_unlock_tables(session, *m_plock);
1807
1753
    *m_plock= NULL;
1808
1754
    m_plock= NULL;
1809
1755
  }