~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_insert.cc

  • Committer: Stewart Smith
  • Date: 2010-11-03 03:27:09 UTC
  • mto: (1902.1.1 build) (1910.1.2 build)
  • mto: This revision was merged to the branch mainline in revision 1903.
  • Revision ID: stewart@flamingspork.com-20101103032709-oyvfrc6eb8fzj0mr
fix docs warning: docs/unlock.rst:2: (WARNING/2) Title underline too short.

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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
14
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
15
15
 
16
16
 
17
17
/* Insert of records */
18
18
 
19
19
#include "config.h"
 
20
#include <cstdio>
20
21
#include <drizzled/sql_select.h>
21
22
#include <drizzled/show.h>
22
23
#include <drizzled/error.h>
31
32
#include "drizzled/transaction_services.h"
32
33
#include "drizzled/plugin/transactional_storage_engine.h"
33
34
 
 
35
#include "drizzled/table/shell.h"
 
36
 
34
37
namespace drizzled
35
38
{
36
39
 
67
70
 
68
71
  if (fields.elements == 0 && values.elements != 0)
69
72
  {
70
 
    if (values.elements != table->s->fields)
 
73
    if (values.elements != table->getShare()->sizeFields())
71
74
    {
72
75
      my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), 1L);
73
76
      return -1;
120
123
    if (table->timestamp_field) // Don't automaticly set timestamp if used
121
124
    {
122
125
      if (table->timestamp_field->isWriteSet())
 
126
      {
123
127
        clear_timestamp_auto_bits(table->timestamp_field_type,
124
128
                                  TIMESTAMP_AUTO_SET_ON_INSERT);
 
129
      }
125
130
      else
126
131
      {
127
132
        table->setWriteSet(table->timestamp_field->field_index);
165
170
      Unmark the timestamp field so that we can check if this is modified
166
171
      by update_fields
167
172
    */
168
 
    timestamp_mark= table->write_set->testAndClear(table->timestamp_field->field_index);
 
173
    timestamp_mark= table->write_set->test(table->timestamp_field->field_index);
 
174
    table->write_set->reset(table->timestamp_field->field_index);
169
175
  }
170
176
 
171
177
  /* Check the fields we are going to modify */
176
182
  {
177
183
    /* Don't set timestamp column if this is modified. */
178
184
    if (table->timestamp_field->isWriteSet())
 
185
    {
179
186
      clear_timestamp_auto_bits(table->timestamp_field_type,
180
187
                                TIMESTAMP_AUTO_SET_ON_UPDATE);
 
188
    }
 
189
 
181
190
    if (timestamp_mark)
 
191
    {
182
192
      table->setWriteSet(table->timestamp_field->field_index);
 
193
    }
183
194
  }
184
195
  return 0;
185
196
}
230
241
  uint32_t value_count;
231
242
  ulong counter = 1;
232
243
  uint64_t id;
233
 
  COPY_INFO info;
 
244
  CopyInfo info;
234
245
  Table *table= 0;
235
246
  List_iterator_fast<List_item> its(values_list);
236
247
  List_item *values;
265
276
                           false,
266
277
                           (fields.elements || !value_count ||
267
278
                            (0) != 0), !ignore))
268
 
    goto abort;
 
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->abort_on_warning= 0;
 
285
    DRIZZLE_INSERT_DONE(1, 0);
 
286
    return true;
 
287
  }
269
288
 
270
289
  /* mysql_prepare_insert set table_list->table if it was not set */
271
290
  table= table_list->table;
296
315
    if (values->elements != value_count)
297
316
    {
298
317
      my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), counter);
299
 
      goto abort;
 
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->abort_on_warning= 0;
 
324
      DRIZZLE_INSERT_DONE(1, 0);
 
325
 
 
326
      return true;
300
327
    }
301
328
    if (setup_fields(session, 0, *values, MARK_COLUMNS_READ, 0, 0))
302
 
      goto abort;
 
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->abort_on_warning= 0;
 
335
      DRIZZLE_INSERT_DONE(1, 0);
 
336
      return true;
 
337
    }
303
338
  }
304
339
  its.rewind ();
305
340
 
309
344
  /*
310
345
    Fill in the given fields and dump it to the table cursor
311
346
  */
312
 
  memset(&info, 0, sizeof(info));
313
347
  info.ignore= ignore;
314
348
  info.handle_duplicates=duplic;
315
349
  info.update_fields= &update_fields;
320
354
    For single line insert, generate an error if try to set a NOT NULL field
321
355
    to NULL.
322
356
  */
323
 
  session->count_cuted_fields= ((values_list.elements == 1 &&
324
 
                                 !ignore) ?
325
 
                                CHECK_FIELD_ERROR_FOR_NULL :
326
 
                                CHECK_FIELD_WARN);
 
357
  session->count_cuted_fields= ignore ? CHECK_FIELD_WARN : CHECK_FIELD_ERROR_FOR_NULL;
 
358
 
327
359
  session->cuted_fields = 0L;
328
360
  table->next_number_field=table->found_next_number_field;
329
361
 
369
401
    {
370
402
      table->restoreRecordAsDefault();  // Get empty record
371
403
 
372
 
      if (fill_record(session, table->field, *values))
 
404
      if (fill_record(session, table->getFields(), *values))
373
405
      {
374
406
        if (values_list.elements != 1 && ! session->is_error())
375
407
        {
446
478
    table->cursor->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
447
479
 
448
480
  if (error)
449
 
    goto abort;
 
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->abort_on_warning= 0;
 
487
    DRIZZLE_INSERT_DONE(1, 0);
 
488
    return true;
 
489
  }
 
490
 
450
491
  if (values_list.elements == 1 && (!(session->options & OPTION_WARNINGS) ||
451
492
                                    !session->cuted_fields))
452
493
  {
467
508
    session->my_ok((ulong) session->row_count_func,
468
509
                   info.copied + info.deleted + info.touched, id, buff);
469
510
  }
 
511
  session->status_var.inserted_row_count+= session->row_count_func;
470
512
  session->abort_on_warning= 0;
471
513
  DRIZZLE_INSERT_DONE(0, session->row_count_func);
 
514
 
472
515
  return false;
473
 
 
474
 
abort:
475
 
  if (table != NULL)
476
 
    table->cursor->ha_release_auto_increment();
477
 
  if (!joins_freed)
478
 
    free_underlaid_joins(session, &session->lex->select_lex);
479
 
  session->abort_on_warning= 0;
480
 
  DRIZZLE_INSERT_DONE(1, 0);
481
 
  return true;
482
516
}
483
517
 
484
518
 
672
706
 
673
707
static int last_uniq_key(Table *table,uint32_t keynr)
674
708
{
675
 
  while (++keynr < table->s->keys)
 
709
  while (++keynr < table->getShare()->sizeKeys())
676
710
    if (table->key_info[keynr].flags & HA_NOSAME)
677
711
      return 0;
678
712
  return 1;
687
721
     write_record()
688
722
      session   - thread context
689
723
      table - table to which record should be written
690
 
      info  - COPY_INFO structure describing handling of duplicates
 
724
      info  - CopyInfo structure describing handling of duplicates
691
725
              and which is used for counting number of records inserted
692
726
              and deleted.
693
727
 
706
740
*/
707
741
 
708
742
 
709
 
int write_record(Session *session, Table *table,COPY_INFO *info)
 
743
int write_record(Session *session, Table *table,CopyInfo *info)
710
744
{
711
745
  int error;
712
 
  char *key=0;
713
 
  MyBitmap *save_read_set, *save_write_set;
 
746
  std::vector<unsigned char> key;
 
747
  boost::dynamic_bitset<> *save_read_set, *save_write_set;
714
748
  uint64_t prev_insert_id= table->cursor->next_insert_id;
715
749
  uint64_t insert_id_for_cur_row= 0;
716
750
 
721
755
 
722
756
  if (info->handle_duplicates == DUP_REPLACE || info->handle_duplicates == DUP_UPDATE)
723
757
  {
724
 
    while ((error=table->cursor->ha_write_row(table->record[0])))
 
758
    while ((error=table->cursor->insertRecord(table->getInsertRecord())))
725
759
    {
726
760
      uint32_t key_nr;
727
761
      /*
764
798
      */
765
799
      if (info->handle_duplicates == DUP_REPLACE &&
766
800
          table->next_number_field &&
767
 
          key_nr == table->s->next_number_index &&
 
801
          key_nr == table->getShare()->next_number_index &&
768
802
          (insert_id_for_cur_row > 0))
769
803
        goto err;
770
804
      if (table->cursor->getEngine()->check_flag(HTON_BIT_DUPLICATE_POS))
771
805
      {
772
 
        if (table->cursor->rnd_pos(table->record[1],table->cursor->dup_ref))
 
806
        if (table->cursor->rnd_pos(table->getUpdateRecord(),table->cursor->dup_ref))
773
807
          goto err;
774
808
      }
775
809
      else
780
814
          goto err;
781
815
        }
782
816
 
783
 
        if (!key)
 
817
        if (not key.size())
784
818
        {
785
 
          if (!(key=(char*) malloc(table->s->max_unique_length)))
786
 
          {
787
 
            error=ENOMEM;
788
 
            goto err;
789
 
          }
 
819
          key.resize(table->getShare()->max_unique_length);
790
820
        }
791
 
        key_copy((unsigned char*) key,table->record[0],table->key_info+key_nr,0);
792
 
        if ((error=(table->cursor->index_read_idx_map(table->record[1],key_nr,
793
 
                                                    (unsigned char*) key, HA_WHOLE_KEY,
 
821
        key_copy(&key[0], table->getInsertRecord(), table->key_info+key_nr, 0);
 
822
        if ((error=(table->cursor->index_read_idx_map(table->getUpdateRecord(),key_nr,
 
823
                                                    &key[0], HA_WHOLE_KEY,
794
824
                                                    HA_READ_KEY_EXACT))))
795
825
          goto err;
796
826
      }
801
831
          that matches, is updated. If update causes a conflict again,
802
832
          an error is returned
803
833
        */
804
 
        assert(table->insert_values != NULL);
 
834
        assert(table->insert_values.size());
805
835
        table->storeRecordAsInsert();
806
836
        table->restoreRecord();
807
837
        assert(info->update_fields->elements ==
817
847
            table->next_number_field->val_int());
818
848
        info->touched++;
819
849
        if ((table->cursor->getEngine()->check_flag(HTON_BIT_PARTIAL_COLUMN_READ) &&
820
 
             !bitmap_is_subset(table->write_set, table->read_set)) ||
 
850
            ! table->write_set->is_subset_of(*table->read_set)) ||
821
851
            table->compare_record())
822
852
        {
823
 
          if ((error=table->cursor->ha_update_row(table->record[1],
824
 
                                                table->record[0])) &&
 
853
          if ((error=table->cursor->updateRecord(table->getUpdateRecord(),
 
854
                                                table->getInsertRecord())) &&
825
855
              error != HA_ERR_RECORD_IS_THE_SAME)
826
856
          {
827
857
            if (info->ignore &&
875
905
            (table->timestamp_field_type == TIMESTAMP_NO_AUTO_SET ||
876
906
             table->timestamp_field_type == TIMESTAMP_AUTO_SET_ON_BOTH))
877
907
        {
878
 
          if ((error=table->cursor->ha_update_row(table->record[1],
879
 
                                                table->record[0])) &&
 
908
          if ((error=table->cursor->updateRecord(table->getUpdateRecord(),
 
909
                                                table->getInsertRecord())) &&
880
910
              error != HA_ERR_RECORD_IS_THE_SAME)
881
911
            goto err;
882
912
          if (error != HA_ERR_RECORD_IS_THE_SAME)
892
922
        }
893
923
        else
894
924
        {
895
 
          if ((error=table->cursor->ha_delete_row(table->record[1])))
 
925
          if ((error=table->cursor->deleteRecord(table->getUpdateRecord())))
896
926
            goto err;
897
927
          info->deleted++;
898
928
          if (!table->cursor->has_transactions())
908
938
    */
909
939
    if (table->read_set != save_read_set ||
910
940
        table->write_set != save_write_set)
911
 
      table->column_bitmaps_set(save_read_set, save_write_set);
 
941
      table->column_bitmaps_set(*save_read_set, *save_write_set);
912
942
  }
913
 
  else if ((error=table->cursor->ha_write_row(table->record[0])))
 
943
  else if ((error=table->cursor->insertRecord(table->getInsertRecord())))
914
944
  {
915
945
    if (!info->ignore ||
916
946
        table->cursor->is_fatal_error(error, HA_CHECK_DUP))
924
954
  session->record_first_successful_insert_id_in_cur_stmt(table->cursor->insert_id_for_cur_row);
925
955
 
926
956
gok_or_after_err:
927
 
  if (key)
928
 
    free(key);
929
957
  if (!table->cursor->has_transactions())
930
958
    session->transaction.stmt.markModifiedNonTransData();
931
959
  return(0);
939
967
 
940
968
before_err:
941
969
  table->cursor->restore_auto_increment(prev_insert_id);
942
 
  if (key)
943
 
    free(key);
944
 
  table->column_bitmaps_set(save_read_set, save_write_set);
945
 
  return(1);
 
970
  table->column_bitmaps_set(*save_read_set, *save_write_set);
 
971
  return 1;
946
972
}
947
973
 
948
974
 
955
981
{
956
982
  int err= 0;
957
983
 
958
 
  for (Field **field=entry->field ; *field ; field++)
 
984
  for (Field **field=entry->getFields() ; *field ; field++)
959
985
  {
960
986
    if (((*field)->isWriteSet()) == false)
961
987
    {
1043
1069
                             List<Item> *update_fields,
1044
1070
                             List<Item> *update_values,
1045
1071
                             enum_duplicates duplic,
1046
 
                             bool ignore_check_option_errors)
1047
 
  :table_list(table_list_par), table(table_par), fields(fields_par),
1048
 
   autoinc_value_of_last_inserted_row(0),
1049
 
   insert_into_view(table_list_par && 0 != 0)
 
1072
                             bool ignore_check_option_errors) :
 
1073
  table_list(table_list_par), table(table_par), fields(fields_par),
 
1074
  autoinc_value_of_last_inserted_row(0),
 
1075
  insert_into_view(table_list_par && 0 != 0)
1050
1076
{
1051
 
  memset(&info, 0, sizeof(info));
1052
1077
  info.handle_duplicates= duplic;
1053
1078
  info.ignore= ignore_check_option_errors;
1054
1079
  info.update_fields= update_fields;
1251
1276
  store_values(values);
1252
1277
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
1253
1278
  if (session->is_error())
 
1279
  {
 
1280
    /*
 
1281
     * If we fail mid-way through INSERT..SELECT, we need to remove any
 
1282
     * records that we added to the current Statement message. We can
 
1283
     * use session->row_count to know how many records we have already added.
 
1284
     */
 
1285
    TransactionServices &ts= TransactionServices::singleton();
 
1286
    ts.removeStatementRecords(session, (session->row_count - 1));
1254
1287
    return(1);
 
1288
  }
1255
1289
 
1256
1290
  // Release latches in case bulk insert takes a long time
1257
1291
  plugin::TransactionalStorageEngine::releaseTemporaryLatches(session);
1258
1292
 
1259
1293
  error= write_record(session, table, &info);
 
1294
  table->auto_increment_field_not_null= false;
1260
1295
 
1261
1296
  if (!error)
1262
1297
  {
1297
1332
  if (fields->elements)
1298
1333
    fill_record(session, *fields, values, true);
1299
1334
  else
1300
 
    fill_record(session, table->field, values, true);
 
1335
    fill_record(session, table->getFields(), values, true);
1301
1336
}
1302
1337
 
1303
1338
void select_insert::send_error(uint32_t errcode,const char *err)
1325
1360
  {
1326
1361
    /*
1327
1362
      We must invalidate the table in the query cache before binlog writing
1328
 
      and ha_autocommit_or_rollback.
 
1363
      and autocommitOrRollback.
1329
1364
    */
1330
1365
    if (session->transaction.stmt.hasModifiedNonTransData())
1331
1366
      session->transaction.all.markModifiedNonTransData();
1357
1392
     (info.copied ? autoinc_value_of_last_inserted_row : 0));
1358
1393
  session->my_ok((ulong) session->row_count_func,
1359
1394
                 info.copied + info.deleted + info.touched, id, buff);
 
1395
  session->status_var.inserted_row_count+= session->row_count_func; 
1360
1396
  DRIZZLE_INSERT_SELECT_DONE(0, session->row_count_func);
1361
1397
  return 0;
1362
1398
}
1426
1462
      items        in     List of items which should be used to produce rest
1427
1463
                          of fields for the table (corresponding fields will
1428
1464
                          be added to the end of alter_info->create_list)
1429
 
      lock         out    Pointer to the DRIZZLE_LOCK object for table created
 
1465
      lock         out    Pointer to the DrizzleLock object for table created
1430
1466
                          (or open temporary table) will be returned in this
1431
1467
                          parameter. Since this table is not included in
1432
1468
                          Session::lock caller is responsible for explicitly
1458
1494
                                      AlterInfo *alter_info,
1459
1495
                                      List<Item> *items,
1460
1496
                                      bool is_if_not_exists,
1461
 
                                      DRIZZLE_LOCK **lock,
 
1497
                                      DrizzleLock **lock,
1462
1498
                                      TableIdentifier &identifier)
1463
1499
{
1464
 
  Table tmp_table;              // Used during 'CreateField()'
1465
 
  TableShare share;
1466
 
  Table *table= 0;
 
1500
  TableShare share(message::Table::INTERNAL);
1467
1501
  uint32_t select_field_count= items->elements;
1468
1502
  /* Add selected items to field list */
1469
1503
  List_iterator_fast<Item> it(*items);
1479
1513
      create_info->table_existed= 1;            // Mark that table existed
1480
1514
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1481
1515
                          ER_TABLE_EXISTS_ERROR, ER(ER_TABLE_EXISTS_ERROR),
1482
 
                          create_table->table_name);
 
1516
                          create_table->getTableName());
1483
1517
      return create_table->table;
1484
1518
    }
1485
1519
 
1486
 
    my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_table->table_name);
 
1520
    my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_table->getTableName());
1487
1521
    return NULL;
1488
1522
  }
1489
1523
 
1490
 
  tmp_table.alias= 0;
1491
 
  tmp_table.timestamp_field= 0;
1492
 
  tmp_table.s= &share;
1493
 
 
1494
 
  tmp_table.s->db_create_options=0;
1495
 
  tmp_table.s->blob_ptr_size= portable_sizeof_char_ptr;
1496
 
 
1497
 
  if (not table_proto.engine().name().compare("MyISAM"))
1498
 
    tmp_table.s->db_low_byte_first= true;
1499
 
  else if (not table_proto.engine().name().compare("MEMORY"))
1500
 
    tmp_table.s->db_low_byte_first= true;
1501
 
 
1502
 
  tmp_table.null_row= false;
1503
 
  tmp_table.maybe_null= false;
1504
 
 
1505
 
  while ((item=it++))
1506
1524
  {
1507
 
    CreateField *cr_field;
1508
 
    Field *field, *def_field;
1509
 
    if (item->type() == Item::FUNC_ITEM)
1510
 
      if (item->result_type() != STRING_RESULT)
1511
 
        field= item->tmp_table_field(&tmp_table);
 
1525
    table::Shell tmp_table(share);              // Used during 'CreateField()'
 
1526
    tmp_table.timestamp_field= 0;
 
1527
 
 
1528
    tmp_table.getMutableShare()->db_create_options= 0;
 
1529
    tmp_table.getMutableShare()->blob_ptr_size= portable_sizeof_char_ptr;
 
1530
 
 
1531
    if (not table_proto.engine().name().compare("MyISAM"))
 
1532
      tmp_table.getMutableShare()->db_low_byte_first= true;
 
1533
    else if (not table_proto.engine().name().compare("MEMORY"))
 
1534
      tmp_table.getMutableShare()->db_low_byte_first= true;
 
1535
 
 
1536
    tmp_table.null_row= false;
 
1537
    tmp_table.maybe_null= false;
 
1538
 
 
1539
    tmp_table.in_use= session;
 
1540
 
 
1541
    while ((item=it++))
 
1542
    {
 
1543
      CreateField *cr_field;
 
1544
      Field *field, *def_field;
 
1545
      if (item->type() == Item::FUNC_ITEM)
 
1546
      {
 
1547
        if (item->result_type() != STRING_RESULT)
 
1548
        {
 
1549
          field= item->tmp_table_field(&tmp_table);
 
1550
        }
 
1551
        else
 
1552
        {
 
1553
          field= item->tmp_table_field_from_field_type(&tmp_table, 0);
 
1554
        }
 
1555
      }
1512
1556
      else
1513
 
        field= item->tmp_table_field_from_field_type(&tmp_table, 0);
1514
 
    else
1515
 
      field= create_tmp_field(session, &tmp_table, item, item->type(),
1516
 
                              (Item ***) 0, &tmp_field, &def_field, false,
1517
 
                              false, false, 0);
1518
 
    if (!field ||
1519
 
        !(cr_field=new CreateField(field,(item->type() == Item::FIELD_ITEM ?
1520
 
                                           ((Item_field *)item)->field :
1521
 
                                           (Field*) 0))))
1522
 
      return NULL;
1523
 
    if (item->maybe_null)
1524
 
      cr_field->flags &= ~NOT_NULL_FLAG;
1525
 
    alter_info->create_list.push_back(cr_field);
 
1557
      {
 
1558
        field= create_tmp_field(session, &tmp_table, item, item->type(),
 
1559
                                (Item ***) 0, &tmp_field, &def_field, false,
 
1560
                                false, false, 0);
 
1561
      }
 
1562
 
 
1563
      if (!field ||
 
1564
          !(cr_field=new CreateField(field,(item->type() == Item::FIELD_ITEM ?
 
1565
                                            ((Item_field *)item)->field :
 
1566
                                            (Field*) 0))))
 
1567
      {
 
1568
        return NULL;
 
1569
      }
 
1570
 
 
1571
      if (item->maybe_null)
 
1572
      {
 
1573
        cr_field->flags &= ~NOT_NULL_FLAG;
 
1574
      }
 
1575
 
 
1576
      alter_info->create_list.push_back(cr_field);
 
1577
    }
1526
1578
  }
1527
1579
 
1528
1580
  /*
1532
1584
    creating base table on which name we have exclusive lock. So code below
1533
1585
    should not cause deadlocks or races.
1534
1586
  */
 
1587
  Table *table= 0;
1535
1588
  {
1536
1589
    if (not mysql_create_table_no_lock(session,
1537
1590
                                       identifier,
1549
1602
          or it was created via different mysqld front-end to the
1550
1603
          cluster. We don't have much options but throw an error.
1551
1604
        */
1552
 
        my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_table->table_name);
 
1605
        my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_table->getTableName());
1553
1606
        return NULL;
1554
1607
      }
1555
1608
 
1556
1609
      if (not identifier.isTmp())
1557
1610
      {
1558
 
        pthread_mutex_lock(&LOCK_open); /* CREATE TABLE... has found that the table already exists for insert and is adapting to use it */
1559
 
        if (session->reopen_name_locked_table(create_table, false))
 
1611
        LOCK_open.lock(); /* CREATE TABLE... has found that the table already exists for insert and is adapting to use it */
 
1612
 
 
1613
        if (create_table->table)
 
1614
        {
 
1615
          table::Concurrent *concurrent_table= static_cast<table::Concurrent *>(create_table->table);
 
1616
 
 
1617
          if (concurrent_table->reopen_name_locked_table(create_table, session))
 
1618
          {
 
1619
            quick_rm_table(*session, identifier);
 
1620
          }
 
1621
          else
 
1622
          {
 
1623
            table= create_table->table;
 
1624
          }
 
1625
        }
 
1626
        else
1560
1627
        {
1561
1628
          quick_rm_table(*session, identifier);
1562
1629
        }
1563
 
        else
1564
 
          table= create_table->table;
1565
 
        pthread_mutex_unlock(&LOCK_open);
 
1630
 
 
1631
        LOCK_open.unlock();
1566
1632
      }
1567
1633
      else
1568
1634
      {
1575
1641
            it preparable for open. But let us do close_temporary_table() here
1576
1642
            just in case.
1577
1643
          */
1578
 
          session->drop_temporary_table(create_table);
 
1644
          session->drop_temporary_table(identifier);
1579
1645
        }
1580
1646
      }
1581
1647
    }
1582
 
    if (!table)                                   // open failed
 
1648
    if (not table)                                   // open failed
1583
1649
      return NULL;
1584
1650
  }
1585
1651
 
1605
1671
int
1606
1672
select_create::prepare(List<Item> &values, Select_Lex_Unit *u)
1607
1673
{
1608
 
  DRIZZLE_LOCK *extra_lock= NULL;
 
1674
  DrizzleLock *extra_lock= NULL;
1609
1675
  /*
1610
1676
    For replication, the CREATE-SELECT statement is written
1611
1677
    in two pieces: the first transaction messsage contains 
1623
1689
                                          alter_info, &values,
1624
1690
                                          is_if_not_exists,
1625
1691
                                          &extra_lock, identifier)))
 
1692
  {
1626
1693
    return(-1);                         // abort() deletes table
 
1694
  }
1627
1695
 
1628
1696
  if (extra_lock)
1629
1697
  {
1637
1705
    *m_plock= extra_lock;
1638
1706
  }
1639
1707
 
1640
 
  if (table->s->fields < values.elements)
 
1708
  if (table->getShare()->sizeFields() < values.elements)
1641
1709
  {
1642
1710
    my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), 1);
1643
1711
    return(-1);
1644
1712
  }
1645
1713
 
1646
1714
 /* First field to copy */
1647
 
  field= table->field+table->s->fields - values.elements;
 
1715
  field= table->getFields() + table->getShare()->sizeFields() - values.elements;
1648
1716
 
1649
1717
  /* Mark all fields that are given values */
1650
1718
  for (Field **f= field ; *f ; f++)
1708
1776
      tables.  This can fail, but we should unlock the table
1709
1777
      nevertheless.
1710
1778
    */
1711
 
    if (!table->s->tmp_table)
 
1779
    if (!table->getShare()->getType())
1712
1780
    {
1713
1781
      TransactionServices &transaction_services= TransactionServices::singleton();
1714
 
      transaction_services.ha_autocommit_or_rollback(session, 0);
 
1782
      transaction_services.autocommitOrRollback(session, 0);
1715
1783
      (void) session->endActiveTransaction();
1716
1784
    }
1717
1785