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 */
17
17
/* Insert of records */
19
#include <drizzled/server_includes.h>
21
20
#include <drizzled/sql_select.h>
22
21
#include <drizzled/show.h>
23
22
#include <drizzled/error.h>
27
26
#include <drizzled/sql_load.h>
28
27
#include <drizzled/field/timestamp.h>
29
28
#include <drizzled/lock.h>
30
#include "drizzled/sql_table.h"
31
#include "drizzled/pthread_globals.h"
32
#include "drizzled/transaction_services.h"
33
#include "drizzled/plugin/transactional_storage_engine.h"
35
#include "drizzled/table/shell.h"
40
extern plugin::StorageEngine *heap_engine;
41
extern plugin::StorageEngine *myisam_engine;
30
using namespace drizzled;
44
33
Check if insert fields are correct.
315
288
if (values->elements != value_count)
317
290
my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), counter);
320
table->cursor->ha_release_auto_increment();
322
free_underlaid_joins(session, &session->lex->select_lex);
323
session->abort_on_warning= 0;
324
DRIZZLE_INSERT_DONE(1, 0);
328
293
if (setup_fields(session, 0, *values, MARK_COLUMNS_READ, 0, 0))
331
table->cursor->ha_release_auto_increment();
333
free_underlaid_joins(session, &session->lex->select_lex);
334
session->abort_on_warning= 0;
335
DRIZZLE_INSERT_DONE(1, 0);
354
312
For single line insert, generate an error if try to set a NOT NULL field
357
session->count_cuted_fields= ignore ? CHECK_FIELD_WARN : CHECK_FIELD_ERROR_FOR_NULL;
315
session->count_cuted_fields= ((values_list.elements == 1 &&
317
CHECK_FIELD_ERROR_FOR_NULL :
359
319
session->cuted_fields = 0L;
360
320
table->next_number_field=table->found_next_number_field;
363
323
session->set_proc_info("update");
364
324
if (duplic == DUP_REPLACE)
365
table->cursor->extra(HA_EXTRA_WRITE_CAN_REPLACE);
325
table->file->extra(HA_EXTRA_WRITE_CAN_REPLACE);
366
326
if (duplic == DUP_UPDATE)
367
table->cursor->extra(HA_EXTRA_INSERT_WITH_UPDATE);
327
table->file->extra(HA_EXTRA_INSERT_WITH_UPDATE);
369
329
if (duplic != DUP_ERROR || ignore)
370
table->cursor->extra(HA_EXTRA_IGNORE_DUP_KEY);
371
table->cursor->ha_start_bulk_insert(values_list.elements);
330
table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
331
table->file->ha_start_bulk_insert(values_list.elements);
402
table->restoreRecordAsDefault(); // Get empty record
404
if (fill_record(session, table->getFields(), *values))
362
if (session->used_tables) // Column used in values()
363
table->restoreRecordAsDefault(); // Get empty record
367
Fix delete marker. No need to restore rest of record since it will
368
be overwritten by fill_record() anyway (and fill_record() does not
369
use default values in this case).
371
table->record[0][0]= table->s->default_values[0];
373
if (fill_record(session, table->field, *values, 0))
406
375
if (values_list.elements != 1 && ! session->is_error())
434
403
Do not do this release if this is a delayed insert, it would steal
435
404
auto_inc values from the delayed_insert thread as they share Table.
437
table->cursor->ha_release_auto_increment();
438
if (table->cursor->ha_end_bulk_insert() && !error)
406
table->file->ha_release_auto_increment();
407
if (table->file->ha_end_bulk_insert() && !error)
440
table->print_error(errno,MYF(0));
409
table->file->print_error(my_errno,MYF(0));
443
412
if (duplic != DUP_ERROR || ignore)
444
table->cursor->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
413
table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
446
transactional_table= table->cursor->has_transactions();
415
transactional_table= table->file->has_transactions();
448
417
changed= (info.copied || info.deleted || info.updated);
449
if ((changed && error <= 0) || session->transaction.stmt.hasModifiedNonTransData())
418
if ((changed && error <= 0) || session->transaction.stmt.modified_non_trans_table)
451
if (session->transaction.stmt.hasModifiedNonTransData())
452
session->transaction.all.markModifiedNonTransData();
420
if (session->transaction.stmt.modified_non_trans_table)
421
session->transaction.all.modified_non_trans_table= true;
454
assert(transactional_table || !changed || session->transaction.stmt.hasModifiedNonTransData());
423
assert(transactional_table || !changed || session->transaction.stmt.modified_non_trans_table);
457
426
session->set_proc_info("end");
475
444
session->count_cuted_fields= CHECK_FIELD_IGNORE;
476
445
table->auto_increment_field_not_null= false;
477
446
if (duplic == DUP_REPLACE)
478
table->cursor->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
447
table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
483
table->cursor->ha_release_auto_increment();
485
free_underlaid_joins(session, &session->lex->select_lex);
486
session->abort_on_warning= 0;
487
DRIZZLE_INSERT_DONE(1, 0);
491
451
if (values_list.elements == 1 && (!(session->options & OPTION_WARNINGS) ||
492
452
!session->cuted_fields))
502
snprintf(buff, sizeof(buff), ER(ER_INSERT_INFO), (ulong) info.records,
462
sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
503
463
(ulong) (info.records - info.copied), (ulong) session->cuted_fields);
505
snprintf(buff, sizeof(buff), ER(ER_INSERT_INFO), (ulong) info.records,
465
sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
506
466
(ulong) (info.deleted + info.updated), (ulong) session->cuted_fields);
507
467
session->row_count_func= info.copied + info.deleted + info.updated;
508
468
session->my_ok((ulong) session->row_count_func,
509
469
info.copied + info.deleted + info.touched, id, buff);
511
session->status_var.inserted_row_count+= session->row_count_func;
512
471
session->abort_on_warning= 0;
513
472
DRIZZLE_INSERT_DONE(0, session->row_count_func);
477
table->file->ha_release_auto_increment();
479
free_underlaid_joins(session, &session->lex->select_lex);
480
session->abort_on_warning= 0;
481
DRIZZLE_INSERT_DONE(1, 0);
743
int write_record(Session *session, Table *table,CopyInfo *info)
710
int write_record(Session *session, Table *table,COPY_INFO *info)
746
std::vector<unsigned char> key;
747
boost::dynamic_bitset<> *save_read_set, *save_write_set;
748
uint64_t prev_insert_id= table->cursor->next_insert_id;
714
MyBitmap *save_read_set, *save_write_set;
715
uint64_t prev_insert_id= table->file->next_insert_id;
749
716
uint64_t insert_id_for_cur_row= 0;
765
732
the autogenerated value to avoid session->insert_id_for_cur_row to become
768
if (table->cursor->insert_id_for_cur_row > 0)
769
insert_id_for_cur_row= table->cursor->insert_id_for_cur_row;
735
if (table->file->insert_id_for_cur_row > 0)
736
insert_id_for_cur_row= table->file->insert_id_for_cur_row;
771
table->cursor->insert_id_for_cur_row= insert_id_for_cur_row;
738
table->file->insert_id_for_cur_row= insert_id_for_cur_row;
772
739
bool is_duplicate_key_error;
773
if (table->cursor->is_fatal_error(error, HA_CHECK_DUP))
740
if (table->file->is_fatal_error(error, HA_CHECK_DUP))
775
is_duplicate_key_error= table->cursor->is_fatal_error(error, 0);
742
is_duplicate_key_error= table->file->is_fatal_error(error, 0);
776
743
if (!is_duplicate_key_error)
799
766
if (info->handle_duplicates == DUP_REPLACE &&
800
767
table->next_number_field &&
801
key_nr == table->getShare()->next_number_index &&
768
key_nr == table->s->next_number_index &&
802
769
(insert_id_for_cur_row > 0))
804
if (table->cursor->getEngine()->check_flag(HTON_BIT_DUPLICATE_POS))
771
if (table->file->ha_table_flags() & HA_DUPLICATE_POS)
806
if (table->cursor->rnd_pos(table->getUpdateRecord(),table->cursor->dup_ref))
773
if (table->file->rnd_pos(table->record[1],table->file->dup_ref))
811
if (table->cursor->extra(HA_EXTRA_FLUSH_CACHE)) /* Not needed with NISAM */
778
if (table->file->extra(HA_EXTRA_FLUSH_CACHE)) /* Not needed with NISAM */
819
key.resize(table->getShare()->max_unique_length);
786
if (!(key=(char*) malloc(table->s->max_unique_length)))
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,
792
key_copy((unsigned char*) key,table->record[0],table->key_info+key_nr,0);
793
if ((error=(table->file->index_read_idx_map(table->record[1],key_nr,
794
(unsigned char*) key, HA_WHOLE_KEY,
824
795
HA_READ_KEY_EXACT))))
844
table->cursor->restore_auto_increment(prev_insert_id);
815
table->file->restore_auto_increment(prev_insert_id);
845
816
if (table->next_number_field)
846
table->cursor->adjust_next_insert_id_after_explicit_value(
817
table->file->adjust_next_insert_id_after_explicit_value(
847
818
table->next_number_field->val_int());
850
if (! table->records_are_comparable() || table->compare_records())
820
if ((table->file->ha_table_flags() & HA_PARTIAL_COLUMN_READ &&
821
!bitmap_is_subset(table->write_set, table->read_set)) ||
822
table->compare_record())
852
if ((error=table->cursor->updateRecord(table->getUpdateRecord(),
853
table->getInsertRecord())) &&
824
if ((error=table->file->ha_update_row(table->record[1],
825
table->record[0])) &&
854
826
error != HA_ERR_RECORD_IS_THE_SAME)
856
828
if (info->ignore &&
857
!table->cursor->is_fatal_error(error, HA_CHECK_DUP_KEY))
829
!table->file->is_fatal_error(error, HA_CHECK_DUP_KEY))
859
831
goto gok_or_after_err;
900
872
ON UPDATE triggers.
902
874
if (last_uniq_key(table,key_nr) &&
903
!table->cursor->referenced_by_foreign_key() &&
875
!table->file->referenced_by_foreign_key() &&
904
876
(table->timestamp_field_type == TIMESTAMP_NO_AUTO_SET ||
905
877
table->timestamp_field_type == TIMESTAMP_AUTO_SET_ON_BOTH))
907
if ((error=table->cursor->updateRecord(table->getUpdateRecord(),
908
table->getInsertRecord())) &&
879
if ((error=table->file->ha_update_row(table->record[1],
880
table->record[0])) &&
909
881
error != HA_ERR_RECORD_IS_THE_SAME)
911
883
if (error != HA_ERR_RECORD_IS_THE_SAME)
915
session->record_first_successful_insert_id_in_cur_stmt(table->cursor->insert_id_for_cur_row);
887
session->record_first_successful_insert_id_in_cur_stmt(table->file->insert_id_for_cur_row);
917
889
Since we pretend that we have done insert we should call
918
890
its after triggers.
924
if ((error=table->cursor->deleteRecord(table->getUpdateRecord())))
896
if ((error=table->file->ha_delete_row(table->record[1])))
927
if (!table->cursor->has_transactions())
928
session->transaction.stmt.markModifiedNonTransData();
899
if (!table->file->has_transactions())
900
session->transaction.stmt.modified_non_trans_table= true;
929
901
/* Let us attempt do write_row() once more */
933
session->record_first_successful_insert_id_in_cur_stmt(table->cursor->insert_id_for_cur_row);
905
session->record_first_successful_insert_id_in_cur_stmt(table->file->insert_id_for_cur_row);
935
907
Restore column maps if they where replaced during an duplicate key
938
910
if (table->read_set != save_read_set ||
939
911
table->write_set != save_write_set)
940
table->column_bitmaps_set(*save_read_set, *save_write_set);
912
table->column_bitmaps_set(save_read_set, save_write_set);
942
else if ((error=table->cursor->insertRecord(table->getInsertRecord())))
914
else if ((error=table->file->ha_write_row(table->record[0])))
944
916
if (!info->ignore ||
945
table->cursor->is_fatal_error(error, HA_CHECK_DUP))
917
table->file->is_fatal_error(error, HA_CHECK_DUP))
947
table->cursor->restore_auto_increment(prev_insert_id);
919
table->file->restore_auto_increment(prev_insert_id);
948
920
goto gok_or_after_err;
951
923
after_n_copied_inc:
953
session->record_first_successful_insert_id_in_cur_stmt(table->cursor->insert_id_for_cur_row);
925
session->record_first_successful_insert_id_in_cur_stmt(table->file->insert_id_for_cur_row);
955
927
gok_or_after_err:
956
if (!table->cursor->has_transactions())
957
session->transaction.stmt.markModifiedNonTransData();
930
if (!table->file->has_transactions())
931
session->transaction.stmt.modified_non_trans_table= true;
962
936
/* current_select is NULL if this is a delayed insert */
963
937
if (session->lex->current_select)
964
938
session->lex->current_select->no_error= 0; // Give error
965
table->print_error(error,MYF(0));
939
table->file->print_error(error,MYF(0));
968
table->cursor->restore_auto_increment(prev_insert_id);
969
table->column_bitmaps_set(*save_read_set, *save_write_set);
942
table->file->restore_auto_increment(prev_insert_id);
945
table->column_bitmaps_set(save_read_set, save_write_set);
1068
1044
List<Item> *update_fields,
1069
1045
List<Item> *update_values,
1070
1046
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)
1047
bool ignore_check_option_errors)
1048
:table_list(table_list_par), table(table_par), fields(fields_par),
1049
autoinc_value_of_last_inserted_row(0),
1050
insert_into_view(table_list_par && 0 != 0)
1052
memset(&info, 0, sizeof(info));
1076
1053
info.handle_duplicates= duplic;
1077
1054
info.ignore= ignore_check_option_errors;
1078
1055
info.update_fields= update_fields;
1194
1171
We won't start bulk inserts at all if this statement uses functions or
1195
1172
should invoke triggers since they may access to the same table too.
1197
table->cursor->ha_start_bulk_insert((ha_rows) 0);
1174
table->file->ha_start_bulk_insert((ha_rows) 0);
1199
1176
table->restoreRecordAsDefault(); // Get empty record
1200
1177
table->next_number_field=table->found_next_number_field;
1202
1179
session->cuted_fields=0;
1203
1180
if (info.ignore || info.handle_duplicates != DUP_ERROR)
1204
table->cursor->extra(HA_EXTRA_IGNORE_DUP_KEY);
1181
table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
1205
1182
if (info.handle_duplicates == DUP_REPLACE)
1206
table->cursor->extra(HA_EXTRA_WRITE_CAN_REPLACE);
1183
table->file->extra(HA_EXTRA_WRITE_CAN_REPLACE);
1207
1184
if (info.handle_duplicates == DUP_UPDATE)
1208
table->cursor->extra(HA_EXTRA_INSERT_WITH_UPDATE);
1185
table->file->extra(HA_EXTRA_INSERT_WITH_UPDATE);
1209
1186
session->abort_on_warning= !info.ignore;
1210
1187
table->mark_columns_needed_for_insert();
1320
1296
void select_insert::store_values(List<Item> &values)
1322
1298
if (fields->elements)
1323
fill_record(session, *fields, values, true);
1299
fill_record(session, *fields, values, 1);
1325
fill_record(session, table->getFields(), values, true);
1301
fill_record(session, table->field, values, 1);
1328
1304
void select_insert::send_error(uint32_t errcode,const char *err)
1338
1314
bool select_insert::send_eof()
1341
bool const trans_table= table->cursor->has_transactions();
1317
bool const trans_table= table->file->has_transactions();
1345
error= table->cursor->ha_end_bulk_insert();
1346
table->cursor->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
1347
table->cursor->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
1321
error= table->file->ha_end_bulk_insert();
1322
table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
1323
table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
1349
1325
if ((changed= (info.copied || info.deleted || info.updated)))
1352
1328
We must invalidate the table in the query cache before binlog writing
1353
and autocommitOrRollback.
1329
and ha_autocommit_or_rollback.
1355
if (session->transaction.stmt.hasModifiedNonTransData())
1356
session->transaction.all.markModifiedNonTransData();
1331
if (session->transaction.stmt.modified_non_trans_table)
1332
session->transaction.all.modified_non_trans_table= true;
1358
1334
assert(trans_table || !changed ||
1359
session->transaction.stmt.hasModifiedNonTransData());
1335
session->transaction.stmt.modified_non_trans_table);
1361
table->cursor->ha_release_auto_increment();
1337
table->file->ha_release_auto_increment();
1365
table->print_error(error,MYF(0));
1341
table->file->print_error(error,MYF(0));
1366
1342
DRIZZLE_INSERT_SELECT_DONE(error, 0);
1369
1345
char buff[160];
1370
1346
if (info.ignore)
1371
snprintf(buff, sizeof(buff), ER(ER_INSERT_INFO), (ulong) info.records,
1347
sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
1372
1348
(ulong) (info.records - info.copied), (ulong) session->cuted_fields);
1374
snprintf(buff, sizeof(buff), ER(ER_INSERT_INFO), (ulong) info.records,
1350
sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
1375
1351
(ulong) (info.deleted+info.updated), (ulong) session->cuted_fields);
1376
1352
session->row_count_func= info.copied + info.deleted + info.updated;
1417
1392
zero, so no check for that is made.
1419
1394
changed= (info.copied || info.deleted || info.updated);
1420
transactional_table= table->cursor->has_transactions();
1395
transactional_table= table->file->has_transactions();
1421
1396
assert(transactional_table || !changed ||
1422
session->transaction.stmt.hasModifiedNonTransData());
1423
table->cursor->ha_release_auto_increment();
1397
session->transaction.stmt.modified_non_trans_table);
1398
table->file->ha_release_auto_increment();
1426
1401
if (DRIZZLE_INSERT_SELECT_DONE_ENABLED())
1481
1456
static Table *create_table_from_items(Session *session, HA_CREATE_INFO *create_info,
1482
1457
TableList *create_table,
1483
message::Table &table_proto,
1458
message::Table *table_proto,
1484
1459
AlterInfo *alter_info,
1485
1460
List<Item> *items,
1486
bool is_if_not_exists,
1488
TableIdentifier &identifier)
1461
DRIZZLE_LOCK **lock)
1490
TableShare share(message::Table::INTERNAL);
1463
Table tmp_table; // Used during 'CreateField()'
1491
1466
uint32_t select_field_count= items->elements;
1492
1467
/* Add selected items to field list */
1493
1468
List_iterator_fast<Item> it(*items);
1495
1470
Field *tmp_field;
1498
if (not (identifier.isTmp()) && create_table->table->db_stat)
1473
if (!(create_info->options & HA_LEX_CREATE_TMP_TABLE) &&
1474
create_table->table->db_stat)
1500
1476
/* Table already exists and was open at openTablesLock() stage. */
1501
if (is_if_not_exists)
1477
if (create_info->options & HA_LEX_CREATE_IF_NOT_EXISTS)
1503
1479
create_info->table_existed= 1; // Mark that table existed
1504
1480
push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1505
1481
ER_TABLE_EXISTS_ERROR, ER(ER_TABLE_EXISTS_ERROR),
1506
create_table->getTableName());
1482
create_table->table_name);
1507
1483
return create_table->table;
1510
my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_table->getTableName());
1486
my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_table->table_name);
1491
tmp_table.timestamp_field= 0;
1492
tmp_table.s= &share;
1494
tmp_table.s->db_create_options=0;
1495
tmp_table.s->blob_ptr_size= portable_sizeof_char_ptr;
1496
tmp_table.s->db_low_byte_first=
1497
test(create_info->db_type == myisam_engine ||
1498
create_info->db_type == heap_engine);
1499
tmp_table.null_row= false;
1500
tmp_table.maybe_null= false;
1515
table::Shell tmp_table(share); // Used during 'CreateField()'
1516
tmp_table.timestamp_field= 0;
1518
tmp_table.getMutableShare()->db_create_options= 0;
1519
tmp_table.getMutableShare()->blob_ptr_size= portable_sizeof_char_ptr;
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;
1526
tmp_table.null_row= false;
1527
tmp_table.maybe_null= false;
1529
tmp_table.in_use= session;
1533
CreateField *cr_field;
1534
Field *field, *def_field;
1535
if (item->type() == Item::FUNC_ITEM)
1537
if (item->result_type() != STRING_RESULT)
1539
field= item->tmp_table_field(&tmp_table);
1543
field= item->tmp_table_field_from_field_type(&tmp_table, 0);
1504
CreateField *cr_field;
1505
Field *field, *def_field;
1506
if (item->type() == Item::FUNC_ITEM)
1507
if (item->result_type() != STRING_RESULT)
1508
field= item->tmp_table_field(&tmp_table);
1548
field= create_tmp_field(session, &tmp_table, item, item->type(),
1549
(Item ***) 0, &tmp_field, &def_field, false,
1554
!(cr_field=new CreateField(field,(item->type() == Item::FIELD_ITEM ?
1555
((Item_field *)item)->field :
1561
if (item->maybe_null)
1563
cr_field->flags &= ~NOT_NULL_FLAG;
1566
alter_info->create_list.push_back(cr_field);
1510
field= item->tmp_table_field_from_field_type(&tmp_table, 0);
1512
field= create_tmp_field(session, &tmp_table, item, item->type(),
1513
(Item ***) 0, &tmp_field, &def_field, 0, 0, 0, 0,
1516
!(cr_field=new CreateField(field,(item->type() == Item::FIELD_ITEM ?
1517
((Item_field *)item)->field :
1520
if (item->maybe_null)
1521
cr_field->flags &= ~NOT_NULL_FLAG;
1522
alter_info->create_list.push_back(cr_field);
1574
1529
creating base table on which name we have exclusive lock. So code below
1575
1530
should not cause deadlocks or races.
1579
if (not mysql_create_table_no_lock(session,
1533
if (!mysql_create_table_no_lock(session, create_table->db,
1534
create_table->table_name,
1538
select_field_count))
1588
if (create_info->table_existed && not identifier.isTmp())
1540
if (create_info->table_existed &&
1541
!(create_info->options & HA_LEX_CREATE_TMP_TABLE))
1591
1544
This means that someone created table underneath server
1592
1545
or it was created via different mysqld front-end to the
1593
1546
cluster. We don't have much options but throw an error.
1595
my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_table->getTableName());
1548
my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_table->table_name);
1599
if (not identifier.isTmp())
1552
if (!(create_info->options & HA_LEX_CREATE_TMP_TABLE))
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());
1604
if (create_table->table)
1554
pthread_mutex_lock(&LOCK_open); /* CREATE TABLE... has found that the table already exists for insert and is adapting to use it */
1555
if (session->reopen_name_locked_table(create_table, false))
1606
table::Concurrent *concurrent_table= static_cast<table::Concurrent *>(create_table->table);
1608
if (concurrent_table->reopen_name_locked_table(create_table, session))
1610
plugin::StorageEngine::dropTable(*session, identifier);
1614
table= create_table->table;
1557
quick_rm_table(create_info->db_type, create_table->db,
1558
create_table->table_name, false);
1619
plugin::StorageEngine::dropTable(*session, identifier);
1561
table= create_table->table;
1562
pthread_mutex_unlock(&LOCK_open);
1624
if (not (table= session->openTable(create_table, (bool*) 0,
1625
DRIZZLE_OPEN_TEMPORARY_ONLY)) &&
1626
not create_info->table_existed)
1566
if (!(table= session->openTable(create_table, (bool*) 0,
1567
DRIZZLE_OPEN_TEMPORARY_ONLY)) &&
1568
!create_info->table_existed)
1629
1571
This shouldn't happen as creation of temporary table should make
1630
1572
it preparable for open. But let us do close_temporary_table() here
1633
session->drop_temporary_table(identifier);
1575
session->drop_temporary_table(create_table);
1637
if (not table) // open failed
1579
if (!table) // open failed
1641
1583
table->reginfo.lock_type=TL_WRITE;
1642
if (! ((*lock)= session->lockTables(&table, 1, DRIZZLE_LOCK_IGNORE_FLUSH, ¬_used)))
1584
if (! ((*lock)= mysql_lock_tables(session, &table, 1,
1585
DRIZZLE_LOCK_IGNORE_FLUSH, ¬_used)))
1646
session->unlockTables(*lock);
1589
mysql_unlock_tables(session, *lock);
1650
if (not create_info->table_existed)
1651
session->drop_open_table(table, identifier);
1593
if (!create_info->table_existed)
1594
session->drop_open_table(table, create_table->db, create_table->table_name);
1660
1603
select_create::prepare(List<Item> &values, Select_Lex_Unit *u)
1662
DrizzleLock *extra_lock= NULL;
1605
DRIZZLE_LOCK *extra_lock= NULL;
1664
For replication, the CREATE-SELECT statement is written
1665
in two pieces: the first transaction messsage contains
1666
the CREATE TABLE statement as a CreateTableStatement message
1667
necessary to create the table.
1669
The second transaction message contains all the InsertStatement
1670
and associated InsertRecords that should go into the table.
1607
For row-based replication, the CREATE-SELECT statement is written
1608
in two pieces: the first one contain the CREATE TABLE statement
1609
necessary to create the table and the second part contain the rows
1610
that should go into the table.
1612
For non-temporary tables, the start of the CREATE-SELECT
1613
implicitly commits the previous transaction, and all events
1614
forming the statement will be stored the transaction cache. At end
1615
of the statement, the entire statement is committed as a
1616
transaction, and all events are written to the binary log.
1618
On the master, the table is locked for the duration of the
1619
statement, but since the CREATE part is replicated as a simple
1620
statement, there is no way to lock the table for accesses on the
1621
slave. Hence, we have to hold on to the CREATE part of the
1622
statement until the statement has finished.
1675
if (not (table= create_table_from_items(session, create_info, create_table,
1677
alter_info, &values,
1679
&extra_lock, identifier)))
1628
Start a statement transaction before the create if we are using
1629
row-based replication for the statement. If we are creating a
1630
temporary table, we need to start a statement transaction.
1633
if (!(table= create_table_from_items(session, create_info, create_table,
1635
alter_info, &values,
1681
1637
return(-1); // abort() deletes table
1684
1639
if (extra_lock)
1686
1641
assert(m_plock == NULL);
1688
if (identifier.isTmp())
1643
if (create_info->options & HA_LEX_CREATE_TMP_TABLE)
1689
1644
m_plock= &m_lock;
1691
1646
m_plock= &session->extra_lock;
1693
1648
*m_plock= extra_lock;
1696
if (table->getShare()->sizeFields() < values.elements)
1651
if (table->s->fields < values.elements)
1698
1653
my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), 1);
1702
1657
/* First field to copy */
1703
field= table->getFields() + table->getShare()->sizeFields() - values.elements;
1658
field= table->field+table->s->fields - values.elements;
1705
1660
/* Mark all fields that are given values */
1706
1661
for (Field **f= field ; *f ; f++)
1708
table->setWriteSet((*f)->position());
1662
table->setWriteSet((*f)->field_index);
1711
1664
/* Don't set timestamp if used */
1712
1665
table->timestamp_field_type= TIMESTAMP_NO_AUTO_SET;
1715
1668
table->restoreRecordAsDefault(); // Get empty record
1716
1669
session->cuted_fields=0;
1717
1670
if (info.ignore || info.handle_duplicates != DUP_ERROR)
1718
table->cursor->extra(HA_EXTRA_IGNORE_DUP_KEY);
1671
table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
1719
1672
if (info.handle_duplicates == DUP_REPLACE)
1720
table->cursor->extra(HA_EXTRA_WRITE_CAN_REPLACE);
1673
table->file->extra(HA_EXTRA_WRITE_CAN_REPLACE);
1721
1674
if (info.handle_duplicates == DUP_UPDATE)
1722
table->cursor->extra(HA_EXTRA_INSERT_WITH_UPDATE);
1723
table->cursor->ha_start_bulk_insert((ha_rows) 0);
1675
table->file->extra(HA_EXTRA_INSERT_WITH_UPDATE);
1676
table->file->ha_start_bulk_insert((ha_rows) 0);
1724
1677
session->abort_on_warning= !info.ignore;
1725
1678
if (check_that_all_fields_are_given_values(session, table, table_list))
1727
1680
table->mark_columns_needed_for_insert();
1728
table->cursor->extra(HA_EXTRA_WRITE_CACHE);
1681
table->file->extra(HA_EXTRA_WRITE_CACHE);
1732
1685
void select_create::store_values(List<Item> &values)
1734
fill_record(session, field, values, true);
1687
fill_record(session, field, values, 1);
1738
1691
void select_create::send_error(uint32_t errcode,const char *err)
1741
1696
This will execute any rollbacks that are necessary before writing
1742
1697
the transcation cache.
1766
1721
tables. This can fail, but we should unlock the table
1769
if (!table->getShare()->getType())
1724
if (!table->s->tmp_table)
1771
TransactionServices &transaction_services= TransactionServices::singleton();
1772
transaction_services.autocommitOrRollback(session, 0);
1726
ha_autocommit_or_rollback(session, 0);
1773
1727
(void) session->endActiveTransaction();
1776
table->cursor->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
1777
table->cursor->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
1730
table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
1731
table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
1780
session->unlockTables(*m_plock);
1734
mysql_unlock_tables(session, *m_plock);
1781
1735
*m_plock= NULL;
1806
1762
select_insert::abort();
1763
session->transaction.stmt.modified_non_trans_table= false;
1810
session->unlockTables(*m_plock);
1768
mysql_unlock_tables(session, *m_plock);
1811
1769
*m_plock= NULL;
1817
table->cursor->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
1818
table->cursor->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
1819
if (not create_info->table_existed)
1820
session->drop_open_table(table, identifier);
1775
table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
1776
table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
1777
if (!create_info->table_existed)
1778
session->drop_open_table(table, create_table->db, create_table->table_name);
1821
1779
table= NULL; // Safety
1825
} /* namespace drizzled */
1784
/*****************************************************************************
1785
Instansiate templates
1786
*****************************************************************************/
1788
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
1789
template class List_iterator_fast<List_item>;
1790
#endif /* HAVE_EXPLICIT_TEMPLATE_INSTANTIATION */