~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_insert.cc

  • Committer: Siddharth Prakash Singh
  • Date: 2010-03-26 17:24:57 UTC
  • mto: This revision was merged to the branch mainline in revision 1425.
  • Revision ID: spsneo@spsneo-laptop-20100326172457-vni09y22ktvvefmn
some more sprintf --> snprintf

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 */
18
18
 
19
19
#include "config.h"
20
 
#include <cstdio>
21
20
#include <drizzled/sql_select.h>
22
21
#include <drizzled/show.h>
23
22
#include <drizzled/error.h>
32
31
#include "drizzled/transaction_services.h"
33
32
#include "drizzled/plugin/transactional_storage_engine.h"
34
33
 
35
 
#include "drizzled/table/shell.h"
36
 
 
37
34
namespace drizzled
38
35
{
39
36
 
70
67
 
71
68
  if (fields.elements == 0 && values.elements != 0)
72
69
  {
73
 
    if (values.elements != table->getShare()->sizeFields())
 
70
    if (values.elements != table->s->fields)
74
71
    {
75
72
      my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), 1L);
76
73
      return -1;
123
120
    if (table->timestamp_field) // Don't automaticly set timestamp if used
124
121
    {
125
122
      if (table->timestamp_field->isWriteSet())
126
 
      {
127
123
        clear_timestamp_auto_bits(table->timestamp_field_type,
128
124
                                  TIMESTAMP_AUTO_SET_ON_INSERT);
129
 
      }
130
125
      else
131
126
      {
132
 
        table->setWriteSet(table->timestamp_field->position());
 
127
        table->setWriteSet(table->timestamp_field->field_index);
133
128
      }
134
129
    }
135
130
  }
170
165
      Unmark the timestamp field so that we can check if this is modified
171
166
      by update_fields
172
167
    */
173
 
    timestamp_mark= table->write_set->test(table->timestamp_field->position());
174
 
    table->write_set->reset(table->timestamp_field->position());
 
168
    timestamp_mark= table->write_set->testAndClear(table->timestamp_field->field_index);
175
169
  }
176
170
 
177
171
  /* Check the fields we are going to modify */
182
176
  {
183
177
    /* Don't set timestamp column if this is modified. */
184
178
    if (table->timestamp_field->isWriteSet())
185
 
    {
186
179
      clear_timestamp_auto_bits(table->timestamp_field_type,
187
180
                                TIMESTAMP_AUTO_SET_ON_UPDATE);
188
 
    }
189
 
 
190
181
    if (timestamp_mark)
191
 
    {
192
 
      table->setWriteSet(table->timestamp_field->position());
193
 
    }
 
182
      table->setWriteSet(table->timestamp_field->field_index);
194
183
  }
195
184
  return 0;
196
185
}
241
230
  uint32_t value_count;
242
231
  ulong counter = 1;
243
232
  uint64_t id;
244
 
  CopyInfo info;
 
233
  COPY_INFO info;
245
234
  Table *table= 0;
246
235
  List_iterator_fast<List_item> its(values_list);
247
236
  List_item *values;
276
265
                           false,
277
266
                           (fields.elements || !value_count ||
278
267
                            (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->abort_on_warning= 0;
285
 
    DRIZZLE_INSERT_DONE(1, 0);
286
 
    return true;
287
 
  }
 
268
    goto abort;
288
269
 
289
270
  /* mysql_prepare_insert set table_list->table if it was not set */
290
271
  table= table_list->table;
315
296
    if (values->elements != value_count)
316
297
    {
317
298
      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->abort_on_warning= 0;
324
 
      DRIZZLE_INSERT_DONE(1, 0);
325
 
 
326
 
      return true;
 
299
      goto abort;
327
300
    }
328
301
    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->abort_on_warning= 0;
335
 
      DRIZZLE_INSERT_DONE(1, 0);
336
 
      return true;
337
 
    }
 
302
      goto abort;
338
303
  }
339
304
  its.rewind ();
340
305
 
344
309
  /*
345
310
    Fill in the given fields and dump it to the table cursor
346
311
  */
 
312
  memset(&info, 0, sizeof(info));
347
313
  info.ignore= ignore;
348
314
  info.handle_duplicates=duplic;
349
315
  info.update_fields= &update_fields;
354
320
    For single line insert, generate an error if try to set a NOT NULL field
355
321
    to NULL.
356
322
  */
357
 
  session->count_cuted_fields= ignore ? CHECK_FIELD_WARN : CHECK_FIELD_ERROR_FOR_NULL;
358
 
 
 
323
  session->count_cuted_fields= ((values_list.elements == 1 &&
 
324
                                 !ignore) ?
 
325
                                CHECK_FIELD_ERROR_FOR_NULL :
 
326
                                CHECK_FIELD_WARN);
359
327
  session->cuted_fields = 0L;
360
328
  table->next_number_field=table->found_next_number_field;
361
329
 
401
369
    {
402
370
      table->restoreRecordAsDefault();  // Get empty record
403
371
 
404
 
      if (fill_record(session, table->getFields(), *values))
 
372
      if (fill_record(session, table->field, *values))
405
373
      {
406
374
        if (values_list.elements != 1 && ! session->is_error())
407
375
        {
478
446
    table->cursor->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
479
447
 
480
448
  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->abort_on_warning= 0;
487
 
    DRIZZLE_INSERT_DONE(1, 0);
488
 
    return true;
489
 
  }
490
 
 
 
449
    goto abort;
491
450
  if (values_list.elements == 1 && (!(session->options & OPTION_WARNINGS) ||
492
451
                                    !session->cuted_fields))
493
452
  {
499
458
  {
500
459
    char buff[160];
501
460
    if (ignore)
502
 
      snprintf(buff, sizeof(buff), ER(ER_INSERT_INFO), (ulong) info.records,
 
461
      sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
503
462
              (ulong) (info.records - info.copied), (ulong) session->cuted_fields);
504
463
    else
505
 
      snprintf(buff, sizeof(buff), ER(ER_INSERT_INFO), (ulong) info.records,
 
464
      sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
506
465
              (ulong) (info.deleted + info.updated), (ulong) session->cuted_fields);
507
466
    session->row_count_func= info.copied + info.deleted + info.updated;
508
467
    session->my_ok((ulong) session->row_count_func,
509
468
                   info.copied + info.deleted + info.touched, id, buff);
510
469
  }
511
 
  session->status_var.inserted_row_count+= session->row_count_func;
512
470
  session->abort_on_warning= 0;
513
471
  DRIZZLE_INSERT_DONE(0, session->row_count_func);
514
 
 
515
472
  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;
516
482
}
517
483
 
518
484
 
706
672
 
707
673
static int last_uniq_key(Table *table,uint32_t keynr)
708
674
{
709
 
  while (++keynr < table->getShare()->sizeKeys())
 
675
  while (++keynr < table->s->keys)
710
676
    if (table->key_info[keynr].flags & HA_NOSAME)
711
677
      return 0;
712
678
  return 1;
721
687
     write_record()
722
688
      session   - thread context
723
689
      table - table to which record should be written
724
 
      info  - CopyInfo structure describing handling of duplicates
 
690
      info  - COPY_INFO structure describing handling of duplicates
725
691
              and which is used for counting number of records inserted
726
692
              and deleted.
727
693
 
740
706
*/
741
707
 
742
708
 
743
 
int write_record(Session *session, Table *table,CopyInfo *info)
 
709
int write_record(Session *session, Table *table,COPY_INFO *info)
744
710
{
745
711
  int error;
746
 
  std::vector<unsigned char> key;
747
 
  boost::dynamic_bitset<> *save_read_set, *save_write_set;
 
712
  char *key=0;
 
713
  MyBitmap *save_read_set, *save_write_set;
748
714
  uint64_t prev_insert_id= table->cursor->next_insert_id;
749
715
  uint64_t insert_id_for_cur_row= 0;
750
716
 
755
721
 
756
722
  if (info->handle_duplicates == DUP_REPLACE || info->handle_duplicates == DUP_UPDATE)
757
723
  {
758
 
    while ((error=table->cursor->insertRecord(table->getInsertRecord())))
 
724
    while ((error=table->cursor->ha_write_row(table->record[0])))
759
725
    {
760
726
      uint32_t key_nr;
761
727
      /*
798
764
      */
799
765
      if (info->handle_duplicates == DUP_REPLACE &&
800
766
          table->next_number_field &&
801
 
          key_nr == table->getShare()->next_number_index &&
 
767
          key_nr == table->s->next_number_index &&
802
768
          (insert_id_for_cur_row > 0))
803
769
        goto err;
804
770
      if (table->cursor->getEngine()->check_flag(HTON_BIT_DUPLICATE_POS))
805
771
      {
806
 
        if (table->cursor->rnd_pos(table->getUpdateRecord(),table->cursor->dup_ref))
 
772
        if (table->cursor->rnd_pos(table->record[1],table->cursor->dup_ref))
807
773
          goto err;
808
774
      }
809
775
      else
814
780
          goto err;
815
781
        }
816
782
 
817
 
        if (not key.size())
 
783
        if (!key)
818
784
        {
819
 
          key.resize(table->getShare()->max_unique_length);
 
785
          if (!(key=(char*) malloc(table->s->max_unique_length)))
 
786
          {
 
787
            error=ENOMEM;
 
788
            goto err;
 
789
          }
820
790
        }
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,
 
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,
824
794
                                                    HA_READ_KEY_EXACT))))
825
795
          goto err;
826
796
      }
831
801
          that matches, is updated. If update causes a conflict again,
832
802
          an error is returned
833
803
        */
834
 
        assert(table->insert_values.size());
 
804
        assert(table->insert_values != NULL);
835
805
        table->storeRecordAsInsert();
836
806
        table->restoreRecord();
837
807
        assert(info->update_fields->elements ==
846
816
          table->cursor->adjust_next_insert_id_after_explicit_value(
847
817
            table->next_number_field->val_int());
848
818
        info->touched++;
849
 
 
850
 
        if (! table->records_are_comparable() || table->compare_records())
 
819
        if ((table->cursor->getEngine()->check_flag(HTON_BIT_PARTIAL_COLUMN_READ) &&
 
820
             !bitmap_is_subset(table->write_set, table->read_set)) ||
 
821
            table->compare_record())
851
822
        {
852
 
          if ((error=table->cursor->updateRecord(table->getUpdateRecord(),
853
 
                                                table->getInsertRecord())) &&
 
823
          if ((error=table->cursor->ha_update_row(table->record[1],
 
824
                                                table->record[0])) &&
854
825
              error != HA_ERR_RECORD_IS_THE_SAME)
855
826
          {
856
827
            if (info->ignore &&
904
875
            (table->timestamp_field_type == TIMESTAMP_NO_AUTO_SET ||
905
876
             table->timestamp_field_type == TIMESTAMP_AUTO_SET_ON_BOTH))
906
877
        {
907
 
          if ((error=table->cursor->updateRecord(table->getUpdateRecord(),
908
 
                                                table->getInsertRecord())) &&
 
878
          if ((error=table->cursor->ha_update_row(table->record[1],
 
879
                                                table->record[0])) &&
909
880
              error != HA_ERR_RECORD_IS_THE_SAME)
910
881
            goto err;
911
882
          if (error != HA_ERR_RECORD_IS_THE_SAME)
921
892
        }
922
893
        else
923
894
        {
924
 
          if ((error=table->cursor->deleteRecord(table->getUpdateRecord())))
 
895
          if ((error=table->cursor->ha_delete_row(table->record[1])))
925
896
            goto err;
926
897
          info->deleted++;
927
898
          if (!table->cursor->has_transactions())
937
908
    */
938
909
    if (table->read_set != save_read_set ||
939
910
        table->write_set != save_write_set)
940
 
      table->column_bitmaps_set(*save_read_set, *save_write_set);
 
911
      table->column_bitmaps_set(save_read_set, save_write_set);
941
912
  }
942
 
  else if ((error=table->cursor->insertRecord(table->getInsertRecord())))
 
913
  else if ((error=table->cursor->ha_write_row(table->record[0])))
943
914
  {
944
915
    if (!info->ignore ||
945
916
        table->cursor->is_fatal_error(error, HA_CHECK_DUP))
953
924
  session->record_first_successful_insert_id_in_cur_stmt(table->cursor->insert_id_for_cur_row);
954
925
 
955
926
gok_or_after_err:
 
927
  if (key)
 
928
    free(key);
956
929
  if (!table->cursor->has_transactions())
957
930
    session->transaction.stmt.markModifiedNonTransData();
958
931
  return(0);
966
939
 
967
940
before_err:
968
941
  table->cursor->restore_auto_increment(prev_insert_id);
969
 
  table->column_bitmaps_set(*save_read_set, *save_write_set);
970
 
  return 1;
 
942
  if (key)
 
943
    free(key);
 
944
  table->column_bitmaps_set(save_read_set, save_write_set);
 
945
  return(1);
971
946
}
972
947
 
973
948
 
980
955
{
981
956
  int err= 0;
982
957
 
983
 
  for (Field **field=entry->getFields() ; *field ; field++)
 
958
  for (Field **field=entry->field ; *field ; field++)
984
959
  {
985
960
    if (((*field)->isWriteSet()) == false)
986
961
    {
1068
1043
                             List<Item> *update_fields,
1069
1044
                             List<Item> *update_values,
1070
1045
                             enum_duplicates duplic,
1071
 
                             bool ignore_check_option_errors) :
1072
 
  table_list(table_list_par), table(table_par), fields(fields_par),
1073
 
  autoinc_value_of_last_inserted_row(0),
1074
 
  insert_into_view(table_list_par && 0 != 0)
 
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)
1075
1050
{
 
1051
  memset(&info, 0, sizeof(info));
1076
1052
  info.handle_duplicates= duplic;
1077
1053
  info.ignore= ignore_check_option_errors;
1078
1054
  info.update_fields= update_fields;
1281
1257
  plugin::TransactionalStorageEngine::releaseTemporaryLatches(session);
1282
1258
 
1283
1259
  error= write_record(session, table, &info);
1284
 
  table->auto_increment_field_not_null= false;
1285
1260
 
1286
1261
  if (!error)
1287
1262
  {
1322
1297
  if (fields->elements)
1323
1298
    fill_record(session, *fields, values, true);
1324
1299
  else
1325
 
    fill_record(session, table->getFields(), values, true);
 
1300
    fill_record(session, table->field, values, true);
1326
1301
}
1327
1302
 
1328
1303
void select_insert::send_error(uint32_t errcode,const char *err)
1350
1325
  {
1351
1326
    /*
1352
1327
      We must invalidate the table in the query cache before binlog writing
1353
 
      and autocommitOrRollback.
 
1328
      and ha_autocommit_or_rollback.
1354
1329
    */
1355
1330
    if (session->transaction.stmt.hasModifiedNonTransData())
1356
1331
      session->transaction.all.markModifiedNonTransData();
1368
1343
  }
1369
1344
  char buff[160];
1370
1345
  if (info.ignore)
1371
 
    snprintf(buff, sizeof(buff), ER(ER_INSERT_INFO), (ulong) info.records,
 
1346
    sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
1372
1347
            (ulong) (info.records - info.copied), (ulong) session->cuted_fields);
1373
1348
  else
1374
 
    snprintf(buff, sizeof(buff), ER(ER_INSERT_INFO), (ulong) info.records,
 
1349
    sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
1375
1350
            (ulong) (info.deleted+info.updated), (ulong) session->cuted_fields);
1376
1351
  session->row_count_func= info.copied + info.deleted + info.updated;
1377
1352
 
1382
1357
     (info.copied ? autoinc_value_of_last_inserted_row : 0));
1383
1358
  session->my_ok((ulong) session->row_count_func,
1384
1359
                 info.copied + info.deleted + info.touched, id, buff);
1385
 
  session->status_var.inserted_row_count+= session->row_count_func; 
1386
1360
  DRIZZLE_INSERT_SELECT_DONE(0, session->row_count_func);
1387
1361
  return 0;
1388
1362
}
1452
1426
      items        in     List of items which should be used to produce rest
1453
1427
                          of fields for the table (corresponding fields will
1454
1428
                          be added to the end of alter_info->create_list)
1455
 
      lock         out    Pointer to the DrizzleLock object for table created
 
1429
      lock         out    Pointer to the DRIZZLE_LOCK object for table created
1456
1430
                          (or open temporary table) will be returned in this
1457
1431
                          parameter. Since this table is not included in
1458
1432
                          Session::lock caller is responsible for explicitly
1484
1458
                                      AlterInfo *alter_info,
1485
1459
                                      List<Item> *items,
1486
1460
                                      bool is_if_not_exists,
1487
 
                                      DrizzleLock **lock,
 
1461
                                      DRIZZLE_LOCK **lock,
1488
1462
                                      TableIdentifier &identifier)
1489
1463
{
1490
 
  TableShare share(message::Table::INTERNAL);
 
1464
  Table tmp_table;              // Used during 'CreateField()'
 
1465
  TableShare share;
 
1466
  Table *table= 0;
1491
1467
  uint32_t select_field_count= items->elements;
1492
1468
  /* Add selected items to field list */
1493
1469
  List_iterator_fast<Item> it(*items);
1495
1471
  Field *tmp_field;
1496
1472
  bool not_used;
1497
1473
 
1498
 
  if (not (identifier.isTmp()) && create_table->table->db_stat)
 
1474
  bool lex_identified_temp_table= (table_proto.type() == message::Table::TEMPORARY);
 
1475
 
 
1476
  if (not (lex_identified_temp_table) &&
 
1477
      create_table->table->db_stat)
1499
1478
  {
1500
1479
    /* Table already exists and was open at openTablesLock() stage. */
1501
1480
    if (is_if_not_exists)
1503
1482
      create_info->table_existed= 1;            // Mark that table existed
1504
1483
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1505
1484
                          ER_TABLE_EXISTS_ERROR, ER(ER_TABLE_EXISTS_ERROR),
1506
 
                          create_table->getTableName());
 
1485
                          create_table->table_name);
1507
1486
      return create_table->table;
1508
1487
    }
1509
1488
 
1510
 
    my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_table->getTableName());
 
1489
    my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_table->table_name);
1511
1490
    return NULL;
1512
1491
  }
1513
1492
 
 
1493
  tmp_table.alias= 0;
 
1494
  tmp_table.timestamp_field= 0;
 
1495
  tmp_table.s= &share;
 
1496
 
 
1497
  tmp_table.s->db_create_options=0;
 
1498
  tmp_table.s->blob_ptr_size= portable_sizeof_char_ptr;
 
1499
 
 
1500
  if (not table_proto.engine().name().compare("MyISAM"))
 
1501
    tmp_table.s->db_low_byte_first= true;
 
1502
  else if (not table_proto.engine().name().compare("MEMORY"))
 
1503
    tmp_table.s->db_low_byte_first= true;
 
1504
 
 
1505
  tmp_table.null_row= false;
 
1506
  tmp_table.maybe_null= false;
 
1507
 
 
1508
  while ((item=it++))
1514
1509
  {
1515
 
    table::Shell tmp_table(share);              // Used during 'CreateField()'
1516
 
    tmp_table.timestamp_field= 0;
1517
 
 
1518
 
    tmp_table.getMutableShare()->db_create_options= 0;
1519
 
    tmp_table.getMutableShare()->blob_ptr_size= portable_sizeof_char_ptr;
1520
 
 
1521
 
    if (not table_proto.engine().name().compare("MyISAM"))
1522
 
      tmp_table.getMutableShare()->db_low_byte_first= true;
1523
 
    else if (not table_proto.engine().name().compare("MEMORY"))
1524
 
      tmp_table.getMutableShare()->db_low_byte_first= true;
1525
 
 
1526
 
    tmp_table.null_row= false;
1527
 
    tmp_table.maybe_null= false;
1528
 
 
1529
 
    tmp_table.in_use= session;
1530
 
 
1531
 
    while ((item=it++))
1532
 
    {
1533
 
      CreateField *cr_field;
1534
 
      Field *field, *def_field;
1535
 
      if (item->type() == Item::FUNC_ITEM)
1536
 
      {
1537
 
        if (item->result_type() != STRING_RESULT)
1538
 
        {
1539
 
          field= item->tmp_table_field(&tmp_table);
1540
 
        }
1541
 
        else
1542
 
        {
1543
 
          field= item->tmp_table_field_from_field_type(&tmp_table, 0);
1544
 
        }
1545
 
      }
 
1510
    CreateField *cr_field;
 
1511
    Field *field, *def_field;
 
1512
    if (item->type() == Item::FUNC_ITEM)
 
1513
      if (item->result_type() != STRING_RESULT)
 
1514
        field= item->tmp_table_field(&tmp_table);
1546
1515
      else
1547
 
      {
1548
 
        field= create_tmp_field(session, &tmp_table, item, item->type(),
1549
 
                                (Item ***) 0, &tmp_field, &def_field, false,
1550
 
                                false, false, 0);
1551
 
      }
1552
 
 
1553
 
      if (!field ||
1554
 
          !(cr_field=new CreateField(field,(item->type() == Item::FIELD_ITEM ?
1555
 
                                            ((Item_field *)item)->field :
1556
 
                                            (Field*) 0))))
1557
 
      {
1558
 
        return NULL;
1559
 
      }
1560
 
 
1561
 
      if (item->maybe_null)
1562
 
      {
1563
 
        cr_field->flags &= ~NOT_NULL_FLAG;
1564
 
      }
1565
 
 
1566
 
      alter_info->create_list.push_back(cr_field);
1567
 
    }
 
1516
        field= item->tmp_table_field_from_field_type(&tmp_table, 0);
 
1517
    else
 
1518
      field= create_tmp_field(session, &tmp_table, item, item->type(),
 
1519
                              (Item ***) 0, &tmp_field, &def_field, false,
 
1520
                              false, false, 0);
 
1521
    if (!field ||
 
1522
        !(cr_field=new CreateField(field,(item->type() == Item::FIELD_ITEM ?
 
1523
                                           ((Item_field *)item)->field :
 
1524
                                           (Field*) 0))))
 
1525
      return NULL;
 
1526
    if (item->maybe_null)
 
1527
      cr_field->flags &= ~NOT_NULL_FLAG;
 
1528
    alter_info->create_list.push_back(cr_field);
1568
1529
  }
1569
1530
 
1570
1531
  /*
1574
1535
    creating base table on which name we have exclusive lock. So code below
1575
1536
    should not cause deadlocks or races.
1576
1537
  */
1577
 
  Table *table= 0;
1578
1538
  {
1579
1539
    if (not mysql_create_table_no_lock(session,
1580
1540
                                       identifier,
1585
1545
                                       select_field_count,
1586
1546
                                       is_if_not_exists))
1587
1547
    {
1588
 
      if (create_info->table_existed && not identifier.isTmp())
 
1548
      if (create_info->table_existed &&
 
1549
          !(lex_identified_temp_table))
1589
1550
      {
1590
1551
        /*
1591
1552
          This means that someone created table underneath server
1592
1553
          or it was created via different mysqld front-end to the
1593
1554
          cluster. We don't have much options but throw an error.
1594
1555
        */
1595
 
        my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_table->getTableName());
 
1556
        my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_table->table_name);
1596
1557
        return NULL;
1597
1558
      }
1598
1559
 
1599
 
      if (not identifier.isTmp())
 
1560
      if (not lex_identified_temp_table)
1600
1561
      {
1601
 
        /* CREATE TABLE... has found that the table already exists for insert and is adapting to use it */
1602
 
        boost::mutex::scoped_lock scopedLock(table::Cache::singleton().mutex());
1603
 
 
1604
 
        if (create_table->table)
 
1562
        pthread_mutex_lock(&LOCK_open); /* CREATE TABLE... has found that the table already exists for insert and is adapting to use it */
 
1563
        if (session->reopen_name_locked_table(create_table, false))
1605
1564
        {
1606
 
          table::Concurrent *concurrent_table= static_cast<table::Concurrent *>(create_table->table);
1607
 
 
1608
 
          if (concurrent_table->reopen_name_locked_table(create_table, session))
1609
 
          {
1610
 
            plugin::StorageEngine::dropTable(*session, identifier);
1611
 
          }
1612
 
          else
1613
 
          {
1614
 
            table= create_table->table;
1615
 
          }
 
1565
          quick_rm_table(*session, identifier);
1616
1566
        }
1617
1567
        else
1618
 
        {
1619
 
          plugin::StorageEngine::dropTable(*session, identifier);
1620
 
        }
 
1568
          table= create_table->table;
 
1569
        pthread_mutex_unlock(&LOCK_open);
1621
1570
      }
1622
1571
      else
1623
1572
      {
1630
1579
            it preparable for open. But let us do close_temporary_table() here
1631
1580
            just in case.
1632
1581
          */
1633
 
          session->drop_temporary_table(identifier);
 
1582
          session->drop_temporary_table(create_table);
1634
1583
        }
1635
1584
      }
1636
1585
    }
1637
 
    if (not table)                                   // open failed
 
1586
    if (!table)                                   // open failed
1638
1587
      return NULL;
1639
1588
  }
1640
1589
 
1641
1590
  table->reginfo.lock_type=TL_WRITE;
1642
 
  if (! ((*lock)= session->lockTables(&table, 1, DRIZZLE_LOCK_IGNORE_FLUSH, &not_used)))
 
1591
  if (! ((*lock)= mysql_lock_tables(session, &table, 1,
 
1592
                                    DRIZZLE_LOCK_IGNORE_FLUSH, &not_used)))
1643
1593
  {
1644
1594
    if (*lock)
1645
1595
    {
1646
 
      session->unlockTables(*lock);
 
1596
      mysql_unlock_tables(session, *lock);
1647
1597
      *lock= 0;
1648
1598
    }
1649
1599
 
1650
 
    if (not create_info->table_existed)
1651
 
      session->drop_open_table(table, identifier);
 
1600
    if (!create_info->table_existed)
 
1601
      session->drop_open_table(table, create_table->db, create_table->table_name);
1652
1602
    return NULL;
1653
1603
  }
1654
1604
 
1659
1609
int
1660
1610
select_create::prepare(List<Item> &values, Select_Lex_Unit *u)
1661
1611
{
1662
 
  DrizzleLock *extra_lock= NULL;
 
1612
  bool lex_identified_temp_table= (table_proto.type() == message::Table::TEMPORARY);
 
1613
 
 
1614
  DRIZZLE_LOCK *extra_lock= NULL;
1663
1615
  /*
1664
1616
    For replication, the CREATE-SELECT statement is written
1665
1617
    in two pieces: the first transaction messsage contains 
1677
1629
                                          alter_info, &values,
1678
1630
                                          is_if_not_exists,
1679
1631
                                          &extra_lock, identifier)))
1680
 
  {
1681
1632
    return(-1);                         // abort() deletes table
1682
 
  }
1683
1633
 
1684
1634
  if (extra_lock)
1685
1635
  {
1686
1636
    assert(m_plock == NULL);
1687
1637
 
1688
 
    if (identifier.isTmp())
 
1638
    if (lex_identified_temp_table)
1689
1639
      m_plock= &m_lock;
1690
1640
    else
1691
1641
      m_plock= &session->extra_lock;
1693
1643
    *m_plock= extra_lock;
1694
1644
  }
1695
1645
 
1696
 
  if (table->getShare()->sizeFields() < values.elements)
 
1646
  if (table->s->fields < values.elements)
1697
1647
  {
1698
1648
    my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), 1);
1699
1649
    return(-1);
1700
1650
  }
1701
1651
 
1702
1652
 /* First field to copy */
1703
 
  field= table->getFields() + table->getShare()->sizeFields() - values.elements;
 
1653
  field= table->field+table->s->fields - values.elements;
1704
1654
 
1705
1655
  /* Mark all fields that are given values */
1706
1656
  for (Field **f= field ; *f ; f++)
1707
 
  {
1708
 
    table->setWriteSet((*f)->position());
1709
 
  }
 
1657
    table->setWriteSet((*f)->field_index);
1710
1658
 
1711
1659
  /* Don't set timestamp if used */
1712
1660
  table->timestamp_field_type= TIMESTAMP_NO_AUTO_SET;
1766
1714
      tables.  This can fail, but we should unlock the table
1767
1715
      nevertheless.
1768
1716
    */
1769
 
    if (!table->getShare()->getType())
 
1717
    if (!table->s->tmp_table)
1770
1718
    {
1771
1719
      TransactionServices &transaction_services= TransactionServices::singleton();
1772
 
      transaction_services.autocommitOrRollback(session, 0);
 
1720
      transaction_services.ha_autocommit_or_rollback(session, 0);
1773
1721
      (void) session->endActiveTransaction();
1774
1722
    }
1775
1723
 
1777
1725
    table->cursor->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
1778
1726
    if (m_plock)
1779
1727
    {
1780
 
      session->unlockTables(*m_plock);
 
1728
      mysql_unlock_tables(session, *m_plock);
1781
1729
      *m_plock= NULL;
1782
1730
      m_plock= NULL;
1783
1731
    }
1807
1755
 
1808
1756
  if (m_plock)
1809
1757
  {
1810
 
    session->unlockTables(*m_plock);
 
1758
    mysql_unlock_tables(session, *m_plock);
1811
1759
    *m_plock= NULL;
1812
1760
    m_plock= NULL;
1813
1761
  }
1816
1764
  {
1817
1765
    table->cursor->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
1818
1766
    table->cursor->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
1819
 
    if (not create_info->table_existed)
1820
 
      session->drop_open_table(table, identifier);
 
1767
    if (!create_info->table_existed)
 
1768
      session->drop_open_table(table, create_table->db, create_table->table_name);
1821
1769
    table= NULL;                                    // Safety
1822
1770
  }
1823
1771
}