~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_insert.cc

  • Committer: Joe Daly
  • Date: 2010-03-08 04:23:54 UTC
  • mto: This revision was merged to the branch mainline in revision 1380.
  • Revision ID: skinny.moey@gmail.com-20100308042354-7k0jibdqaxkhac7o
scoreboardĀ implementationĀ forĀ statistics

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
127
        table->setWriteSet(table->timestamp_field->field_index);
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->field_index);
174
 
    table->write_set->reset(table->timestamp_field->field_index);
 
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
182
      table->setWriteSet(table->timestamp_field->field_index);
193
 
    }
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 ==
847
817
            table->next_number_field->val_int());
848
818
        info->touched++;
849
819
        if ((table->cursor->getEngine()->check_flag(HTON_BIT_PARTIAL_COLUMN_READ) &&
850
 
            ! table->write_set->is_subset_of(*table->read_set)) ||
 
820
             !bitmap_is_subset(table->write_set, table->read_set)) ||
851
821
            table->compare_record())
852
822
        {
853
 
          if ((error=table->cursor->updateRecord(table->getUpdateRecord(),
854
 
                                                table->getInsertRecord())) &&
 
823
          if ((error=table->cursor->ha_update_row(table->record[1],
 
824
                                                table->record[0])) &&
855
825
              error != HA_ERR_RECORD_IS_THE_SAME)
856
826
          {
857
827
            if (info->ignore &&
905
875
            (table->timestamp_field_type == TIMESTAMP_NO_AUTO_SET ||
906
876
             table->timestamp_field_type == TIMESTAMP_AUTO_SET_ON_BOTH))
907
877
        {
908
 
          if ((error=table->cursor->updateRecord(table->getUpdateRecord(),
909
 
                                                table->getInsertRecord())) &&
 
878
          if ((error=table->cursor->ha_update_row(table->record[1],
 
879
                                                table->record[0])) &&
910
880
              error != HA_ERR_RECORD_IS_THE_SAME)
911
881
            goto err;
912
882
          if (error != HA_ERR_RECORD_IS_THE_SAME)
922
892
        }
923
893
        else
924
894
        {
925
 
          if ((error=table->cursor->deleteRecord(table->getUpdateRecord())))
 
895
          if ((error=table->cursor->ha_delete_row(table->record[1])))
926
896
            goto err;
927
897
          info->deleted++;
928
898
          if (!table->cursor->has_transactions())
938
908
    */
939
909
    if (table->read_set != save_read_set ||
940
910
        table->write_set != save_write_set)
941
 
      table->column_bitmaps_set(*save_read_set, *save_write_set);
 
911
      table->column_bitmaps_set(save_read_set, save_write_set);
942
912
  }
943
 
  else if ((error=table->cursor->insertRecord(table->getInsertRecord())))
 
913
  else if ((error=table->cursor->ha_write_row(table->record[0])))
944
914
  {
945
915
    if (!info->ignore ||
946
916
        table->cursor->is_fatal_error(error, HA_CHECK_DUP))
954
924
  session->record_first_successful_insert_id_in_cur_stmt(table->cursor->insert_id_for_cur_row);
955
925
 
956
926
gok_or_after_err:
 
927
  if (key)
 
928
    free(key);
957
929
  if (!table->cursor->has_transactions())
958
930
    session->transaction.stmt.markModifiedNonTransData();
959
931
  return(0);
967
939
 
968
940
before_err:
969
941
  table->cursor->restore_auto_increment(prev_insert_id);
970
 
  table->column_bitmaps_set(*save_read_set, *save_write_set);
971
 
  return 1;
 
942
  if (key)
 
943
    free(key);
 
944
  table->column_bitmaps_set(save_read_set, save_write_set);
 
945
  return(1);
972
946
}
973
947
 
974
948
 
981
955
{
982
956
  int err= 0;
983
957
 
984
 
  for (Field **field=entry->getFields() ; *field ; field++)
 
958
  for (Field **field=entry->field ; *field ; field++)
985
959
  {
986
960
    if (((*field)->isWriteSet()) == false)
987
961
    {
1069
1043
                             List<Item> *update_fields,
1070
1044
                             List<Item> *update_values,
1071
1045
                             enum_duplicates duplic,
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)
 
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)
1076
1050
{
 
1051
  memset(&info, 0, sizeof(info));
1077
1052
  info.handle_duplicates= duplic;
1078
1053
  info.ignore= ignore_check_option_errors;
1079
1054
  info.update_fields= update_fields;
1276
1251
  store_values(values);
1277
1252
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
1278
1253
  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));
1287
1254
    return(1);
1288
 
  }
1289
1255
 
1290
1256
  // Release latches in case bulk insert takes a long time
1291
1257
  plugin::TransactionalStorageEngine::releaseTemporaryLatches(session);
1292
1258
 
1293
1259
  error= write_record(session, table, &info);
1294
 
  table->auto_increment_field_not_null= false;
1295
1260
 
1296
1261
  if (!error)
1297
1262
  {
1332
1297
  if (fields->elements)
1333
1298
    fill_record(session, *fields, values, true);
1334
1299
  else
1335
 
    fill_record(session, table->getFields(), values, true);
 
1300
    fill_record(session, table->field, values, true);
1336
1301
}
1337
1302
 
1338
1303
void select_insert::send_error(uint32_t errcode,const char *err)
1360
1325
  {
1361
1326
    /*
1362
1327
      We must invalidate the table in the query cache before binlog writing
1363
 
      and autocommitOrRollback.
 
1328
      and ha_autocommit_or_rollback.
1364
1329
    */
1365
1330
    if (session->transaction.stmt.hasModifiedNonTransData())
1366
1331
      session->transaction.all.markModifiedNonTransData();
1378
1343
  }
1379
1344
  char buff[160];
1380
1345
  if (info.ignore)
1381
 
    snprintf(buff, sizeof(buff), ER(ER_INSERT_INFO), (ulong) info.records,
 
1346
    sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
1382
1347
            (ulong) (info.records - info.copied), (ulong) session->cuted_fields);
1383
1348
  else
1384
 
    snprintf(buff, sizeof(buff), ER(ER_INSERT_INFO), (ulong) info.records,
 
1349
    sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
1385
1350
            (ulong) (info.deleted+info.updated), (ulong) session->cuted_fields);
1386
1351
  session->row_count_func= info.copied + info.deleted + info.updated;
1387
1352
 
1392
1357
     (info.copied ? autoinc_value_of_last_inserted_row : 0));
1393
1358
  session->my_ok((ulong) session->row_count_func,
1394
1359
                 info.copied + info.deleted + info.touched, id, buff);
1395
 
  session->status_var.inserted_row_count+= session->row_count_func; 
1396
1360
  DRIZZLE_INSERT_SELECT_DONE(0, session->row_count_func);
1397
1361
  return 0;
1398
1362
}
1462
1426
      items        in     List of items which should be used to produce rest
1463
1427
                          of fields for the table (corresponding fields will
1464
1428
                          be added to the end of alter_info->create_list)
1465
 
      lock         out    Pointer to the DrizzleLock object for table created
 
1429
      lock         out    Pointer to the DRIZZLE_LOCK object for table created
1466
1430
                          (or open temporary table) will be returned in this
1467
1431
                          parameter. Since this table is not included in
1468
1432
                          Session::lock caller is responsible for explicitly
1490
1454
 
1491
1455
static Table *create_table_from_items(Session *session, HA_CREATE_INFO *create_info,
1492
1456
                                      TableList *create_table,
1493
 
                                      message::Table &table_proto,
 
1457
                                      message::Table *table_proto,
1494
1458
                                      AlterInfo *alter_info,
1495
1459
                                      List<Item> *items,
1496
1460
                                      bool is_if_not_exists,
1497
 
                                      DrizzleLock **lock,
1498
 
                                      TableIdentifier &identifier)
 
1461
                                      DRIZZLE_LOCK **lock)
1499
1462
{
1500
 
  TableShare share(message::Table::INTERNAL);
 
1463
  Table tmp_table;              // Used during 'CreateField()'
 
1464
  TableShare share;
 
1465
  Table *table= 0;
1501
1466
  uint32_t select_field_count= items->elements;
1502
1467
  /* Add selected items to field list */
1503
1468
  List_iterator_fast<Item> it(*items);
1505
1470
  Field *tmp_field;
1506
1471
  bool not_used;
1507
1472
 
1508
 
  if (not (identifier.isTmp()) && create_table->table->db_stat)
 
1473
  bool lex_identified_temp_table= (table_proto->type() == message::Table::TEMPORARY);
 
1474
 
 
1475
  if (!(lex_identified_temp_table) &&
 
1476
      create_table->table->db_stat)
1509
1477
  {
1510
1478
    /* Table already exists and was open at openTablesLock() stage. */
1511
1479
    if (is_if_not_exists)
1513
1481
      create_info->table_existed= 1;            // Mark that table existed
1514
1482
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1515
1483
                          ER_TABLE_EXISTS_ERROR, ER(ER_TABLE_EXISTS_ERROR),
1516
 
                          create_table->getTableName());
 
1484
                          create_table->table_name);
1517
1485
      return create_table->table;
1518
1486
    }
1519
1487
 
1520
 
    my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_table->getTableName());
 
1488
    my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_table->table_name);
1521
1489
    return NULL;
1522
1490
  }
1523
1491
 
 
1492
  tmp_table.alias= 0;
 
1493
  tmp_table.timestamp_field= 0;
 
1494
  tmp_table.s= &share;
 
1495
 
 
1496
  tmp_table.s->db_create_options=0;
 
1497
  tmp_table.s->blob_ptr_size= portable_sizeof_char_ptr;
 
1498
  tmp_table.s->db_low_byte_first=
 
1499
        test(create_info->db_type == myisam_engine ||
 
1500
             create_info->db_type == heap_engine);
 
1501
  tmp_table.null_row= false;
 
1502
  tmp_table.maybe_null= false;
 
1503
 
 
1504
  while ((item=it++))
1524
1505
  {
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
 
      }
 
1506
    CreateField *cr_field;
 
1507
    Field *field, *def_field;
 
1508
    if (item->type() == Item::FUNC_ITEM)
 
1509
      if (item->result_type() != STRING_RESULT)
 
1510
        field= item->tmp_table_field(&tmp_table);
1556
1511
      else
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
 
    }
 
1512
        field= item->tmp_table_field_from_field_type(&tmp_table, 0);
 
1513
    else
 
1514
      field= create_tmp_field(session, &tmp_table, item, item->type(),
 
1515
                              (Item ***) 0, &tmp_field, &def_field, false,
 
1516
                              false, false, 0);
 
1517
    if (!field ||
 
1518
        !(cr_field=new CreateField(field,(item->type() == Item::FIELD_ITEM ?
 
1519
                                           ((Item_field *)item)->field :
 
1520
                                           (Field*) 0))))
 
1521
      return NULL;
 
1522
    if (item->maybe_null)
 
1523
      cr_field->flags &= ~NOT_NULL_FLAG;
 
1524
    alter_info->create_list.push_back(cr_field);
1578
1525
  }
1579
1526
 
 
1527
  TableIdentifier identifier(create_table->db,
 
1528
                             create_table->table_name,
 
1529
                             lex_identified_temp_table ?  TEMP_TABLE :
 
1530
                             STANDARD_TABLE);
 
1531
 
 
1532
 
1580
1533
  /*
1581
1534
    Create and lock table.
1582
1535
 
1584
1537
    creating base table on which name we have exclusive lock. So code below
1585
1538
    should not cause deadlocks or races.
1586
1539
  */
1587
 
  Table *table= 0;
1588
1540
  {
1589
 
    if (not mysql_create_table_no_lock(session,
1590
 
                                       identifier,
1591
 
                                       create_info,
1592
 
                                       table_proto,
1593
 
                                       alter_info,
1594
 
                                       false,
1595
 
                                       select_field_count,
1596
 
                                       is_if_not_exists))
 
1541
    if (!mysql_create_table_no_lock(session,
 
1542
                                    identifier,
 
1543
                                    create_info,
 
1544
                                    table_proto,
 
1545
                                    alter_info,
 
1546
                                    false,
 
1547
                                    select_field_count,
 
1548
                                    is_if_not_exists))
1597
1549
    {
1598
 
      if (create_info->table_existed && not identifier.isTmp())
 
1550
      if (create_info->table_existed &&
 
1551
          !(lex_identified_temp_table))
1599
1552
      {
1600
1553
        /*
1601
1554
          This means that someone created table underneath server
1602
1555
          or it was created via different mysqld front-end to the
1603
1556
          cluster. We don't have much options but throw an error.
1604
1557
        */
1605
 
        my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_table->getTableName());
 
1558
        my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_table->table_name);
1606
1559
        return NULL;
1607
1560
      }
1608
1561
 
1609
 
      if (not identifier.isTmp())
 
1562
      if (!(lex_identified_temp_table))
1610
1563
      {
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
 
1564
        pthread_mutex_lock(&LOCK_open); /* CREATE TABLE... has found that the table already exists for insert and is adapting to use it */
 
1565
        if (session->reopen_name_locked_table(create_table, false))
1627
1566
        {
1628
1567
          quick_rm_table(*session, identifier);
1629
1568
        }
1630
 
 
1631
 
        LOCK_open.unlock();
 
1569
        else
 
1570
          table= create_table->table;
 
1571
        pthread_mutex_unlock(&LOCK_open);
1632
1572
      }
1633
1573
      else
1634
1574
      {
1635
 
        if (not (table= session->openTable(create_table, (bool*) 0,
1636
 
                                           DRIZZLE_OPEN_TEMPORARY_ONLY)) &&
1637
 
            not create_info->table_existed)
 
1575
        if (!(table= session->openTable(create_table, (bool*) 0,
 
1576
                                         DRIZZLE_OPEN_TEMPORARY_ONLY)) &&
 
1577
            !create_info->table_existed)
1638
1578
        {
1639
1579
          /*
1640
1580
            This shouldn't happen as creation of temporary table should make
1641
1581
            it preparable for open. But let us do close_temporary_table() here
1642
1582
            just in case.
1643
1583
          */
1644
 
          session->drop_temporary_table(identifier);
 
1584
          session->drop_temporary_table(create_table);
1645
1585
        }
1646
1586
      }
1647
1587
    }
1648
 
    if (not table)                                   // open failed
 
1588
    if (!table)                                   // open failed
1649
1589
      return NULL;
1650
1590
  }
1651
1591
 
1652
1592
  table->reginfo.lock_type=TL_WRITE;
1653
 
  if (! ((*lock)= session->lockTables(&table, 1, DRIZZLE_LOCK_IGNORE_FLUSH, &not_used)))
 
1593
  if (! ((*lock)= mysql_lock_tables(session, &table, 1,
 
1594
                                    DRIZZLE_LOCK_IGNORE_FLUSH, &not_used)))
1654
1595
  {
1655
1596
    if (*lock)
1656
1597
    {
1657
 
      session->unlockTables(*lock);
 
1598
      mysql_unlock_tables(session, *lock);
1658
1599
      *lock= 0;
1659
1600
    }
1660
1601
 
1661
 
    if (not create_info->table_existed)
1662
 
      session->drop_open_table(table, identifier);
 
1602
    if (!create_info->table_existed)
 
1603
      session->drop_open_table(table, create_table->db, create_table->table_name);
1663
1604
    return NULL;
1664
1605
  }
1665
1606
 
1670
1611
int
1671
1612
select_create::prepare(List<Item> &values, Select_Lex_Unit *u)
1672
1613
{
1673
 
  DrizzleLock *extra_lock= NULL;
 
1614
  bool lex_identified_temp_table= (table_proto->type() == message::Table::TEMPORARY);
 
1615
 
 
1616
  DRIZZLE_LOCK *extra_lock= NULL;
1674
1617
  /*
1675
1618
    For replication, the CREATE-SELECT statement is written
1676
1619
    in two pieces: the first transaction messsage contains 
1683
1626
 
1684
1627
  unit= u;
1685
1628
 
1686
 
  if (not (table= create_table_from_items(session, create_info, create_table,
1687
 
                                          table_proto,
1688
 
                                          alter_info, &values,
1689
 
                                          is_if_not_exists,
1690
 
                                          &extra_lock, identifier)))
1691
 
  {
 
1629
  if (!(table= create_table_from_items(session, create_info, create_table,
 
1630
                                       table_proto,
 
1631
                                       alter_info, &values,
 
1632
                                       is_if_not_exists,
 
1633
                                       &extra_lock)))
1692
1634
    return(-1);                         // abort() deletes table
1693
 
  }
1694
1635
 
1695
1636
  if (extra_lock)
1696
1637
  {
1697
1638
    assert(m_plock == NULL);
1698
1639
 
1699
 
    if (identifier.isTmp())
 
1640
    if (lex_identified_temp_table)
1700
1641
      m_plock= &m_lock;
1701
1642
    else
1702
1643
      m_plock= &session->extra_lock;
1704
1645
    *m_plock= extra_lock;
1705
1646
  }
1706
1647
 
1707
 
  if (table->getShare()->sizeFields() < values.elements)
 
1648
  if (table->s->fields < values.elements)
1708
1649
  {
1709
1650
    my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), 1);
1710
1651
    return(-1);
1711
1652
  }
1712
1653
 
1713
1654
 /* First field to copy */
1714
 
  field= table->getFields() + table->getShare()->sizeFields() - values.elements;
 
1655
  field= table->field+table->s->fields - values.elements;
1715
1656
 
1716
1657
  /* Mark all fields that are given values */
1717
1658
  for (Field **f= field ; *f ; f++)
1775
1716
      tables.  This can fail, but we should unlock the table
1776
1717
      nevertheless.
1777
1718
    */
1778
 
    if (!table->getShare()->getType())
 
1719
    if (!table->s->tmp_table)
1779
1720
    {
1780
1721
      TransactionServices &transaction_services= TransactionServices::singleton();
1781
 
      transaction_services.autocommitOrRollback(session, 0);
 
1722
      transaction_services.ha_autocommit_or_rollback(session, 0);
1782
1723
      (void) session->endActiveTransaction();
1783
1724
    }
1784
1725
 
1786
1727
    table->cursor->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
1787
1728
    if (m_plock)
1788
1729
    {
1789
 
      session->unlockTables(*m_plock);
 
1730
      mysql_unlock_tables(session, *m_plock);
1790
1731
      *m_plock= NULL;
1791
1732
      m_plock= NULL;
1792
1733
    }
1816
1757
 
1817
1758
  if (m_plock)
1818
1759
  {
1819
 
    session->unlockTables(*m_plock);
 
1760
    mysql_unlock_tables(session, *m_plock);
1820
1761
    *m_plock= NULL;
1821
1762
    m_plock= NULL;
1822
1763
  }
1825
1766
  {
1826
1767
    table->cursor->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
1827
1768
    table->cursor->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
1828
 
    if (not create_info->table_existed)
1829
 
      session->drop_open_table(table, identifier);
 
1769
    if (!create_info->table_existed)
 
1770
      session->drop_open_table(table, create_table->db, create_table->table_name);
1830
1771
    table= NULL;                                    // Safety
1831
1772
  }
1832
1773
}