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 */
21
20
#include <drizzled/sql_select.h>
22
21
#include <drizzled/show.h>
23
22
#include <drizzled/error.h>
25
24
#include <drizzled/probes.h>
26
25
#include <drizzled/sql_base.h>
27
26
#include <drizzled/sql_load.h>
28
#include <drizzled/field/epoch.h>
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>
34
#include <drizzled/select_insert.h>
35
#include <drizzled/select_create.h>
36
#include <drizzled/table/shell.h>
37
#include <drizzled/alter_info.h>
38
#include <drizzled/sql_parse.h>
39
#include <drizzled/sql_lex.h>
40
#include <drizzled/statistics_variables.h>
41
#include <drizzled/session/transactions.h>
42
#include <drizzled/open_tables_state.h>
43
#include <drizzled/table/cache.h>
29
#include "drizzled/sql_table.h"
30
#include "drizzled/pthread_globals.h"
31
#include "drizzled/transaction_services.h"
47
36
extern plugin::StorageEngine *heap_engine;
48
37
extern plugin::StorageEngine *myisam_engine;
255
lock_type= table_list->lock_type;
273
257
session->set_proc_info("init");
274
258
session->used_tables=0;
276
value_count= values->size();
260
value_count= values->elements;
278
if (prepare_insert(session, table_list, table, fields, values,
262
if (mysql_prepare_insert(session, table_list, table, fields, values,
279
263
update_fields, update_values, duplic, &unused_conds,
281
(fields.size() || !value_count ||
265
(fields.elements || !value_count ||
282
266
(0) != 0), !ignore))
285
table->cursor->ha_release_auto_increment();
287
free_underlaid_joins(session, &session->lex().select_lex);
288
session->setAbortOnWarning(false);
289
DRIZZLE_INSERT_DONE(1, 0);
293
269
/* mysql_prepare_insert set table_list->table if it was not set */
294
270
table= table_list->table;
296
context= &session->lex().select_lex.context;
272
context= &session->lex->select_lex.context;
298
274
These three asserts test the hypothesis that the resetting of the name
299
275
resolution context below is not necessary at all since the list of local
316
292
while ((values= its++))
319
if (values->size() != value_count)
295
if (values->elements != value_count)
321
297
my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), counter);
324
table->cursor->ha_release_auto_increment();
326
free_underlaid_joins(session, &session->lex().select_lex);
327
session->setAbortOnWarning(false);
328
DRIZZLE_INSERT_DONE(1, 0);
332
300
if (setup_fields(session, 0, *values, MARK_COLUMNS_READ, 0, 0))
335
table->cursor->ha_release_auto_increment();
337
free_underlaid_joins(session, &session->lex().select_lex);
338
session->setAbortOnWarning(false);
339
DRIZZLE_INSERT_DONE(1, 0);
343
its= values_list.begin();
345
305
/* Restore the current context. */
346
306
ctx_state.restore_state(context, table_list);
373
336
if (duplic != DUP_ERROR || ignore)
374
337
table->cursor->extra(HA_EXTRA_IGNORE_DUP_KEY);
375
table->cursor->ha_start_bulk_insert(values_list.size());
338
table->cursor->ha_start_bulk_insert(values_list.elements);
379
session->setAbortOnWarning(not ignore);
342
session->abort_on_warning= !ignore;
381
344
table->mark_columns_needed_for_insert();
383
346
while ((values= its++))
385
if (fields.size() || !value_count)
348
if (fields.elements || !value_count)
387
350
table->restoreRecordAsDefault(); // Get empty record
388
351
if (fill_record(session, fields, *values))
390
if (values_list.size() != 1 && ! session->is_error())
353
if (values_list.elements != 1 && ! session->is_error())
450
413
transactional_table= table->cursor->has_transactions();
452
415
changed= (info.copied || info.deleted || info.updated);
453
if ((changed && error <= 0) || session->transaction.stmt.hasModifiedNonTransData())
416
if ((changed && error <= 0) || session->transaction.stmt.modified_non_trans_table)
455
if (session->transaction.stmt.hasModifiedNonTransData())
456
session->transaction.all.markModifiedNonTransData();
418
if (session->transaction.stmt.modified_non_trans_table)
419
session->transaction.all.modified_non_trans_table= true;
458
assert(transactional_table || !changed || session->transaction.stmt.hasModifiedNonTransData());
421
assert(transactional_table || !changed || session->transaction.stmt.modified_non_trans_table);
461
424
session->set_proc_info("end");
482
445
table->cursor->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
487
table->cursor->ha_release_auto_increment();
489
free_underlaid_joins(session, &session->lex().select_lex);
490
session->setAbortOnWarning(false);
491
DRIZZLE_INSERT_DONE(1, 0);
495
if (values_list.size() == 1 && (!(session->options & OPTION_WARNINGS) ||
449
if (values_list.elements == 1 && (!(session->options & OPTION_WARNINGS) ||
496
450
!session->cuted_fields))
498
452
session->row_count_func= info.copied + info.deleted + info.updated;
499
session->my_ok((ulong) session->rowCount(),
453
session->my_ok((ulong) session->row_count_func,
500
454
info.copied + info.deleted + info.touched, id);
506
snprintf(buff, sizeof(buff), ER(ER_INSERT_INFO), (ulong) info.records,
460
sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
507
461
(ulong) (info.records - info.copied), (ulong) session->cuted_fields);
509
snprintf(buff, sizeof(buff), ER(ER_INSERT_INFO), (ulong) info.records,
463
sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
510
464
(ulong) (info.deleted + info.updated), (ulong) session->cuted_fields);
511
465
session->row_count_func= info.copied + info.deleted + info.updated;
512
session->my_ok((ulong) session->rowCount(),
466
session->my_ok((ulong) session->row_count_func,
513
467
info.copied + info.deleted + info.touched, id, buff);
515
session->status_var.inserted_row_count+= session->rowCount();
516
session->setAbortOnWarning(false);
517
DRIZZLE_INSERT_DONE(0, session->rowCount());
469
session->abort_on_warning= 0;
470
DRIZZLE_INSERT_DONE(0, session->row_count_func);
475
table->cursor->ha_release_auto_increment();
477
free_underlaid_joins(session, &session->lex->select_lex);
478
session->abort_on_warning= 0;
479
DRIZZLE_INSERT_DONE(1, 0);
552
if (setup_tables_and_check_access(session, &session->lex().select_lex.context,
553
&session->lex().select_lex.top_join_list,
513
if (setup_tables_and_check_access(session, &session->lex->select_lex.context,
514
&session->lex->select_lex.top_join_list,
555
&session->lex().select_lex.leaf_tables,
516
&session->lex->select_lex.leaf_tables,
663
624
if (!res && check_fields)
665
bool saved_abort_on_warning= session->abortOnWarning();
667
session->setAbortOnWarning(abort_on_warning);
626
bool saved_abort_on_warning= session->abort_on_warning;
627
session->abort_on_warning= abort_on_warning;
668
628
res= check_that_all_fields_are_given_values(session,
670
630
context->table_list->table,
671
631
context->table_list);
672
session->setAbortOnWarning(saved_abort_on_warning);
632
session->abort_on_warning= saved_abort_on_warning;
675
635
if (!res && duplic == DUP_UPDATE)
749
int write_record(Session *session, Table *table,CopyInfo *info)
708
int write_record(Session *session, Table *table,COPY_INFO *info)
752
std::vector<unsigned char> key;
753
boost::dynamic_bitset<> *save_read_set, *save_write_set;
712
MyBitmap *save_read_set, *save_write_set;
754
713
uint64_t prev_insert_id= table->cursor->next_insert_id;
755
714
uint64_t insert_id_for_cur_row= 0;
805
764
if (info->handle_duplicates == DUP_REPLACE &&
806
765
table->next_number_field &&
807
key_nr == table->getShare()->next_number_index &&
766
key_nr == table->s->next_number_index &&
808
767
(insert_id_for_cur_row > 0))
810
769
if (table->cursor->getEngine()->check_flag(HTON_BIT_DUPLICATE_POS))
812
if (table->cursor->rnd_pos(table->getUpdateRecord(),table->cursor->dup_ref))
771
if (table->cursor->rnd_pos(table->record[1],table->cursor->dup_ref))
825
key.resize(table->getShare()->max_unique_length);
784
if (!(key=(char*) malloc(table->s->max_unique_length)))
827
key_copy(&key[0], table->getInsertRecord(), table->key_info+key_nr, 0);
828
if ((error=(table->cursor->index_read_idx_map(table->getUpdateRecord(),key_nr,
829
&key[0], HA_WHOLE_KEY,
790
key_copy((unsigned char*) key,table->record[0],table->key_info+key_nr,0);
791
if ((error=(table->cursor->index_read_idx_map(table->record[1],key_nr,
792
(unsigned char*) key, HA_WHOLE_KEY,
830
793
HA_READ_KEY_EXACT))))
837
800
that matches, is updated. If update causes a conflict again,
838
801
an error is returned
840
assert(table->insert_values.size());
803
assert(table->insert_values != NULL);
841
804
table->storeRecordAsInsert();
842
805
table->restoreRecord();
843
assert(info->update_fields->size() ==
844
info->update_values->size());
806
assert(info->update_fields->elements ==
807
info->update_values->elements);
845
808
if (fill_record(session, *info->update_fields,
846
809
*info->update_values,
852
815
table->cursor->adjust_next_insert_id_after_explicit_value(
853
816
table->next_number_field->val_int());
856
if (! table->records_are_comparable() || table->compare_records())
818
if ((table->cursor->getEngine()->check_flag(HTON_BIT_PARTIAL_COLUMN_READ) &&
819
!bitmap_is_subset(table->write_set, table->read_set)) ||
820
table->compare_record())
858
if ((error=table->cursor->updateRecord(table->getUpdateRecord(),
859
table->getInsertRecord())) &&
822
if ((error=table->cursor->ha_update_row(table->record[1],
823
table->record[0])) &&
860
824
error != HA_ERR_RECORD_IS_THE_SAME)
862
826
if (info->ignore &&
959
923
session->record_first_successful_insert_id_in_cur_stmt(table->cursor->insert_id_for_cur_row);
961
925
gok_or_after_err:
962
928
if (!table->cursor->has_transactions())
963
session->transaction.stmt.markModifiedNonTransData();
929
session->transaction.stmt.modified_non_trans_table= true;
967
933
info->last_errno= error;
968
934
/* current_select is NULL if this is a delayed insert */
969
if (session->lex().current_select)
970
session->lex().current_select->no_error= 0; // Give error
935
if (session->lex->current_select)
936
session->lex->current_select->no_error= 0; // Give error
971
937
table->print_error(error,MYF(0));
974
940
table->cursor->restore_auto_increment(prev_insert_id);
975
table->column_bitmaps_set(*save_read_set, *save_write_set);
943
table->column_bitmaps_set(save_read_set, save_write_set);
1074
1042
List<Item> *update_fields,
1075
1043
List<Item> *update_values,
1076
1044
enum_duplicates duplic,
1077
bool ignore_check_option_errors) :
1078
table_list(table_list_par), table(table_par), fields(fields_par),
1079
autoinc_value_of_last_inserted_row(0),
1080
insert_into_view(table_list_par && 0 != 0)
1045
bool ignore_check_option_errors)
1046
:table_list(table_list_par), table(table_par), fields(fields_par),
1047
autoinc_value_of_last_inserted_row(0),
1048
insert_into_view(table_list_par && 0 != 0)
1050
memset(&info, 0, sizeof(info));
1082
1051
info.handle_duplicates= duplic;
1083
1052
info.ignore= ignore_check_option_errors;
1084
1053
info.update_fields= update_fields;
1101
1071
select, LEX::current_select should point to the first select while
1102
1072
we are fixing fields from insert list.
1104
session->lex().current_select= &session->lex().select_lex;
1074
lex->current_select= &lex->select_lex;
1105
1075
res= check_insert_fields(session, table_list, *fields, values,
1106
1076
!insert_into_view, &map) ||
1107
1077
setup_fields(session, 0, values, MARK_COLUMNS_READ, 0, 0);
1109
if (!res && fields->size())
1079
if (!res && fields->elements)
1111
bool saved_abort_on_warning= session->abortOnWarning();
1112
session->setAbortOnWarning(not info.ignore);
1081
bool saved_abort_on_warning= session->abort_on_warning;
1082
session->abort_on_warning= !info.ignore;
1113
1083
res= check_that_all_fields_are_given_values(session, table_list->table,
1115
session->setAbortOnWarning(saved_abort_on_warning);
1085
session->abort_on_warning= saved_abort_on_warning;
1118
1088
if (info.handle_duplicates == DUP_UPDATE && !res)
1120
Name_resolution_context *context= &session->lex().select_lex.context;
1090
Name_resolution_context *context= &lex->select_lex.context;
1121
1091
Name_resolution_context_state ctx_state;
1123
1093
/* Save the state of the current name resolution context. */
1185
1155
if (unique_table(table_list, table_list->next_global))
1187
1157
/* Using same table for INSERT and SELECT */
1188
session->lex().current_select->options|= OPTION_BUFFER_RESULT;
1189
session->lex().current_select->join->select_options|= OPTION_BUFFER_RESULT;
1158
lex->current_select->options|= OPTION_BUFFER_RESULT;
1159
lex->current_select->join->select_options|= OPTION_BUFFER_RESULT;
1191
else if (not (session->lex().current_select->options & OPTION_BUFFER_RESULT))
1161
else if (!(lex->current_select->options & OPTION_BUFFER_RESULT))
1194
1164
We must not yet prepare the result table if it is the same as one of the
1205
1175
table->next_number_field=table->found_next_number_field;
1207
1177
session->cuted_fields=0;
1209
1178
if (info.ignore || info.handle_duplicates != DUP_ERROR)
1210
1179
table->cursor->extra(HA_EXTRA_IGNORE_DUP_KEY);
1212
1180
if (info.handle_duplicates == DUP_REPLACE)
1213
1181
table->cursor->extra(HA_EXTRA_WRITE_CAN_REPLACE);
1215
1182
if (info.handle_duplicates == DUP_UPDATE)
1216
1183
table->cursor->extra(HA_EXTRA_INSERT_WITH_UPDATE);
1218
session->setAbortOnWarning(not info.ignore);
1184
session->abort_on_warning= !info.ignore;
1219
1185
table->mark_columns_needed_for_insert();
1272
1238
bool select_insert::send_data(List<Item> &values)
1277
1243
if (unit->offset_limit_cnt)
1278
1244
{ // using limit offset,count
1279
1245
unit->offset_limit_cnt--;
1283
1249
session->count_cuted_fields= CHECK_FIELD_WARN; // Calculate cuted fields
1284
1250
store_values(values);
1285
1251
session->count_cuted_fields= CHECK_FIELD_IGNORE;
1286
1252
if (session->is_error())
1289
1255
// Release latches in case bulk insert takes a long time
1290
plugin::TransactionalStorageEngine::releaseTemporaryLatches(session);
1256
plugin::StorageEngine::releaseTemporaryLatches(session);
1292
1258
error= write_record(session, table, &info);
1293
table->auto_increment_field_not_null= false;
1329
1294
void select_insert::store_values(List<Item> &values)
1296
if (fields->elements)
1332
1297
fill_record(session, *fields, values, true);
1334
fill_record(session, table->getFields(), values, true);
1299
fill_record(session, table->field, values, true);
1337
void select_insert::send_error(drizzled::error_t errcode,const char *err)
1302
void select_insert::send_error(uint32_t errcode,const char *err)
1339
1306
my_message(errcode, err, MYF(0));
1357
1326
We must invalidate the table in the query cache before binlog writing
1358
and autocommitOrRollback.
1327
and ha_autocommit_or_rollback.
1360
if (session->transaction.stmt.hasModifiedNonTransData())
1361
session->transaction.all.markModifiedNonTransData();
1329
if (session->transaction.stmt.modified_non_trans_table)
1330
session->transaction.all.modified_non_trans_table= true;
1363
1332
assert(trans_table || !changed ||
1364
session->transaction.stmt.hasModifiedNonTransData());
1333
session->transaction.stmt.modified_non_trans_table);
1366
1335
table->cursor->ha_release_auto_increment();
1374
1343
char buff[160];
1375
1344
if (info.ignore)
1376
snprintf(buff, sizeof(buff), ER(ER_INSERT_INFO), (ulong) info.records,
1345
sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
1377
1346
(ulong) (info.records - info.copied), (ulong) session->cuted_fields);
1379
snprintf(buff, sizeof(buff), ER(ER_INSERT_INFO), (ulong) info.records,
1348
sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
1380
1349
(ulong) (info.deleted+info.updated), (ulong) session->cuted_fields);
1381
1350
session->row_count_func= info.copied + info.deleted + info.updated;
1385
1354
(session->arg_of_last_insert_id_function ?
1386
1355
session->first_successful_insert_id_in_prev_stmt :
1387
1356
(info.copied ? autoinc_value_of_last_inserted_row : 0));
1388
session->my_ok((ulong) session->rowCount(),
1357
session->my_ok((ulong) session->row_count_func,
1389
1358
info.copied + info.deleted + info.touched, id, buff);
1390
session->status_var.inserted_row_count+= session->rowCount();
1391
DRIZZLE_INSERT_SELECT_DONE(0, session->rowCount());
1359
DRIZZLE_INSERT_SELECT_DONE(0, session->row_count_func);
1486
1454
static Table *create_table_from_items(Session *session, HA_CREATE_INFO *create_info,
1487
1455
TableList *create_table,
1488
message::Table &table_proto,
1456
message::Table *table_proto,
1489
1457
AlterInfo *alter_info,
1490
1458
List<Item> *items,
1491
1459
bool is_if_not_exists,
1493
const identifier::Table& identifier)
1460
DRIZZLE_LOCK **lock)
1495
TableShare share(message::Table::INTERNAL);
1496
uint32_t select_field_count= items->size();
1462
Table tmp_table; // Used during 'CreateField()'
1465
uint32_t select_field_count= items->elements;
1497
1466
/* Add selected items to field list */
1498
List<Item>::iterator it(items->begin());
1467
List_iterator_fast<Item> it(*items);
1500
1469
Field *tmp_field;
1502
if (not (identifier.isTmp()) && create_table->table->db_stat)
1472
bool lex_identified_temp_table= (table_proto->type() == message::Table::TEMPORARY);
1474
if (!(lex_identified_temp_table) &&
1475
create_table->table->db_stat)
1504
1477
/* Table already exists and was open at openTablesLock() stage. */
1505
1478
if (is_if_not_exists)
1507
1480
create_info->table_existed= 1; // Mark that table existed
1508
1481
push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1509
1482
ER_TABLE_EXISTS_ERROR, ER(ER_TABLE_EXISTS_ERROR),
1510
create_table->getTableName());
1483
create_table->table_name);
1511
1484
return create_table->table;
1514
my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_table->getTableName());
1487
my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_table->table_name);
1492
tmp_table.timestamp_field= 0;
1493
tmp_table.s= &share;
1495
tmp_table.s->db_create_options=0;
1496
tmp_table.s->blob_ptr_size= portable_sizeof_char_ptr;
1497
tmp_table.s->db_low_byte_first=
1498
test(create_info->db_type == myisam_engine ||
1499
create_info->db_type == heap_engine);
1500
tmp_table.null_row= false;
1501
tmp_table.maybe_null= false;
1519
table::Shell tmp_table(share); // Used during 'CreateField()'
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.in_use= session;
1530
CreateField *cr_field;
1531
Field *field, *def_field;
1532
if (item->type() == Item::FUNC_ITEM)
1534
if (item->result_type() != STRING_RESULT)
1536
field= item->tmp_table_field(&tmp_table);
1540
field= item->tmp_table_field_from_field_type(&tmp_table, 0);
1505
CreateField *cr_field;
1506
Field *field, *def_field;
1507
if (item->type() == Item::FUNC_ITEM)
1508
if (item->result_type() != STRING_RESULT)
1509
field= item->tmp_table_field(&tmp_table);
1545
field= create_tmp_field(session, &tmp_table, item, item->type(),
1546
(Item ***) 0, &tmp_field, &def_field, false,
1551
!(cr_field=new CreateField(field,(item->type() == Item::FIELD_ITEM ?
1552
((Item_field *)item)->field :
1558
if (item->maybe_null)
1560
cr_field->flags &= ~NOT_NULL_FLAG;
1563
alter_info->create_list.push_back(cr_field);
1511
field= item->tmp_table_field_from_field_type(&tmp_table, 0);
1513
field= create_tmp_field(session, &tmp_table, item, item->type(),
1514
(Item ***) 0, &tmp_field, &def_field, 0, 0, 0, 0,
1517
!(cr_field=new CreateField(field,(item->type() == Item::FIELD_ITEM ?
1518
((Item_field *)item)->field :
1521
if (item->maybe_null)
1522
cr_field->flags &= ~NOT_NULL_FLAG;
1523
alter_info->create_list.push_back(cr_field);
1526
TableIdentifier identifier(create_table->db,
1527
create_table->table_name,
1528
lex_identified_temp_table ? TEMP_TABLE :
1568
1533
Create and lock table.
1571
1536
creating base table on which name we have exclusive lock. So code below
1572
1537
should not cause deadlocks or races.
1576
if (not create_table_no_lock(session,
1540
if (!mysql_create_table_no_lock(session,
1585
if (create_info->table_existed && not identifier.isTmp())
1549
if (create_info->table_existed &&
1550
!(lex_identified_temp_table))
1588
1553
This means that someone created table underneath server
1589
1554
or it was created via different mysqld front-end to the
1590
1555
cluster. We don't have much options but throw an error.
1592
my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_table->getTableName());
1557
my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_table->table_name);
1596
if (not identifier.isTmp())
1561
if (!(lex_identified_temp_table))
1598
/* CREATE TABLE... has found that the table already exists for insert and is adapting to use it */
1599
boost::mutex::scoped_lock scopedLock(table::Cache::mutex());
1601
if (create_table->table)
1563
pthread_mutex_lock(&LOCK_open); /* CREATE TABLE... has found that the table already exists for insert and is adapting to use it */
1564
if (session->reopen_name_locked_table(create_table, false))
1603
table::Concurrent *concurrent_table= static_cast<table::Concurrent *>(create_table->table);
1605
if (concurrent_table->reopen_name_locked_table(create_table, session))
1607
(void)plugin::StorageEngine::dropTable(*session, identifier);
1611
table= create_table->table;
1566
quick_rm_table(*session, identifier);
1616
(void)plugin::StorageEngine::dropTable(*session, identifier);
1569
table= create_table->table;
1570
pthread_mutex_unlock(&LOCK_open);
1621
if (not (table= session->openTable(create_table, (bool*) 0,
1622
DRIZZLE_OPEN_TEMPORARY_ONLY)) &&
1623
not create_info->table_existed)
1574
if (!(table= session->openTable(create_table, (bool*) 0,
1575
DRIZZLE_OPEN_TEMPORARY_ONLY)) &&
1576
!create_info->table_existed)
1626
1579
This shouldn't happen as creation of temporary table should make
1627
1580
it preparable for open. But let us do close_temporary_table() here
1630
session->open_tables.drop_temporary_table(identifier);
1583
session->drop_temporary_table(create_table);
1634
if (not table) // open failed
1587
if (!table) // open failed
1638
1591
table->reginfo.lock_type=TL_WRITE;
1639
if (not ((*lock)= session->lockTables(&table, 1, DRIZZLE_LOCK_IGNORE_FLUSH)))
1592
if (! ((*lock)= mysql_lock_tables(session, &table, 1,
1593
DRIZZLE_LOCK_IGNORE_FLUSH, ¬_used)))
1643
session->unlockTables(*lock);
1597
mysql_unlock_tables(session, *lock);
1647
if (not create_info->table_existed)
1648
session->drop_open_table(table, identifier);
1601
if (!create_info->table_existed)
1602
session->drop_open_table(table, create_table->db, create_table->table_name);
1658
1611
select_create::prepare(List<Item> &values, Select_Lex_Unit *u)
1660
DrizzleLock *extra_lock= NULL;
1613
bool lex_identified_temp_table= (table_proto->type() == message::Table::TEMPORARY);
1615
DRIZZLE_LOCK *extra_lock= NULL;
1662
For replication, the CREATE-SELECT statement is written
1663
in two pieces: the first transaction messsage contains
1664
the CREATE TABLE statement as a CreateTableStatement message
1665
necessary to create the table.
1667
The second transaction message contains all the InsertStatement
1668
and associated InsertRecords that should go into the table.
1617
For row-based replication, the CREATE-SELECT statement is written
1618
in two pieces: the first one contain the CREATE TABLE statement
1619
necessary to create the table and the second part contain the rows
1620
that should go into the table.
1622
For non-temporary tables, the start of the CREATE-SELECT
1623
implicitly commits the previous transaction, and all events
1624
forming the statement will be stored the transaction cache. At end
1625
of the statement, the entire statement is committed as a
1626
transaction, and all events are written to the binary log.
1628
On the master, the table is locked for the duration of the
1629
statement, but since the CREATE part is replicated as a simple
1630
statement, there is no way to lock the table for accesses on the
1631
slave. Hence, we have to hold on to the CREATE part of the
1632
statement until the statement has finished.
1673
if (not (table= create_table_from_items(session, create_info, create_table,
1675
alter_info, &values,
1677
&extra_lock, identifier)))
1638
Start a statement transaction before the create if we are using
1639
row-based replication for the statement. If we are creating a
1640
temporary table, we need to start a statement transaction.
1643
if (!(table= create_table_from_items(session, create_info, create_table,
1645
alter_info, &values,
1679
1648
return(-1); // abort() deletes table
1682
1650
if (extra_lock)
1684
1652
assert(m_plock == NULL);
1686
if (identifier.isTmp())
1654
if (lex_identified_temp_table)
1687
1655
m_plock= &m_lock;
1689
m_plock= &session->open_tables.extra_lock;
1657
m_plock= &session->extra_lock;
1691
1659
*m_plock= extra_lock;
1694
if (table->getShare()->sizeFields() < values.size())
1662
if (table->s->fields < values.elements)
1696
1664
my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), 1);
1700
1668
/* First field to copy */
1701
field= table->getFields() + table->getShare()->sizeFields() - values.size();
1669
field= table->field+table->s->fields - values.elements;
1703
1671
/* Mark all fields that are given values */
1704
1672
for (Field **f= field ; *f ; f++)
1706
table->setWriteSet((*f)->position());
1673
table->setWriteSet((*f)->field_index);
1709
1675
/* Don't set timestamp if used */
1710
1676
table->timestamp_field_type= TIMESTAMP_NO_AUTO_SET;
1714
1680
session->cuted_fields=0;
1715
1681
if (info.ignore || info.handle_duplicates != DUP_ERROR)
1716
1682
table->cursor->extra(HA_EXTRA_IGNORE_DUP_KEY);
1718
1683
if (info.handle_duplicates == DUP_REPLACE)
1719
1684
table->cursor->extra(HA_EXTRA_WRITE_CAN_REPLACE);
1721
1685
if (info.handle_duplicates == DUP_UPDATE)
1722
1686
table->cursor->extra(HA_EXTRA_INSERT_WITH_UPDATE);
1724
1687
table->cursor->ha_start_bulk_insert((ha_rows) 0);
1725
session->setAbortOnWarning(not info.ignore);
1688
session->abort_on_warning= !info.ignore;
1726
1689
if (check_that_all_fields_are_given_values(session, table, table_list))
1729
1691
table->mark_columns_needed_for_insert();
1730
1692
table->cursor->extra(HA_EXTRA_WRITE_CACHE);