107
103
assert(select_lex->db);
109
105
/* Chicken/Egg... we need to search for the table, to know if the table exists, so we can build a full identifier from it */
110
message::table::shared_ptr original_table_message;
106
message::Table original_table_message;
112
TableIdentifier identifier(first_table->getSchemaName(), first_table->getTableName());
108
TableIdentifier identifier(first_table->db, first_table->table_name);
113
109
if (plugin::StorageEngine::getTableDefinition(*session, identifier, original_table_message) != EEXIST)
116
identifier.getSQLPath(path);
117
my_error(ER_BAD_TABLE_ERROR, MYF(0), path.c_str());
111
my_error(ER_BAD_TABLE_ERROR, MYF(0), identifier.getSQLPath().c_str());
121
115
if (not create_info.db_type)
123
117
create_info.db_type=
124
plugin::StorageEngine::findByName(*session, original_table_message->engine().name());
118
plugin::StorageEngine::findByName(*session, original_table_message.engine().name());
126
120
if (not create_info.db_type)
129
identifier.getSQLPath(path);
130
my_error(ER_BAD_TABLE_ERROR, MYF(0), path.c_str());
122
my_error(ER_BAD_TABLE_ERROR, MYF(0), identifier.getSQLPath().c_str());
136
128
if (not validateCreateTableOption())
139
133
/* ALTER TABLE ends previous transaction */
140
134
if (not session->endActiveTransaction())
143
if (not (need_start_waiting= not session->wait_if_global_read_lock(0, 1)))
139
if (not (need_start_waiting= ! wait_if_global_read_lock(session, 0, 1)))
147
if (original_table_message->type() == message::Table::STANDARD )
145
if (original_table_message.type() == message::Table::STANDARD )
149
TableIdentifier identifier(first_table->getSchemaName(), first_table->getTableName());
150
TableIdentifier new_identifier(select_lex->db ? select_lex->db : first_table->getSchemaName(),
151
session->lex->name.str ? session->lex->name.str : first_table->getTableName());
147
TableIdentifier identifier(first_table->db, first_table->table_name);
148
TableIdentifier new_identifier(select_lex->db ? select_lex->db : first_table->db,
149
session->lex->name.str ? session->lex->name.str : first_table->table_name);
153
151
res= alter_table(session,
157
*original_table_message,
155
original_table_message,
158
156
create_table_message,
161
159
select_lex->order_list.elements,
162
(Order *) select_lex->order_list.first,
160
(order_st *) select_lex->order_list.first,
163
161
session->lex->ignore);
167
TableIdentifier catch22(first_table->getSchemaName(), first_table->getTableName());
168
Table *table= session->find_temporary_table(catch22);
165
Table *table= session->find_temporary_table(first_table);
171
TableIdentifier identifier(first_table->getSchemaName(), first_table->getTableName(), table->getMutableShare()->getPath());
172
TableIdentifier new_identifier(select_lex->db ? select_lex->db : first_table->getSchemaName(),
173
session->lex->name.str ? session->lex->name.str : first_table->getTableName(),
168
TableIdentifier identifier(first_table->db, first_table->table_name, table->getMutableShare()->getPath());
169
TableIdentifier new_identifier(select_lex->db ? select_lex->db : first_table->db,
170
session->lex->name.str ? session->lex->name.str : first_table->table_name,
174
171
table->getMutableShare()->getPath());
176
173
res= alter_table(session,
180
*original_table_message,
177
original_table_message,
181
178
create_table_message,
184
181
select_lex->order_list.elements,
185
(Order *) select_lex->order_list.first,
182
(order_st *) select_lex->order_list.first,
186
183
session->lex->ignore);
368
362
if (def->change && ! def->field)
370
364
my_error(ER_BAD_FIELD_ERROR, MYF(0), def->change, table->getMutableShare()->getTableName());
374
If we have been given a field which has no default value, and is not null then we need to bail.
368
Check that the DATE/DATETIME not null field we are going to add is
369
either has a default value or the '0000-00-00' is allowed by the
371
If the '0000-00-00' value isn't allowed then raise the error_if_not_empty
372
flag to allow ALTER Table only if the table to be altered is empty.
376
if (not (~def->flags & (NO_DEFAULT_VALUE_FLAG | NOT_NULL_FLAG)) and not def->change)
374
if ((def->sql_type == DRIZZLE_TYPE_DATE ||
375
def->sql_type == DRIZZLE_TYPE_DATETIME) &&
376
! alter_info->datetime_field &&
377
! (~def->flags & (NO_DEFAULT_VALUE_FLAG | NOT_NULL_FLAG)))
379
alter_info->datetime_field= def;
378
380
alter_info->error_if_not_empty= true;
380
382
if (! def->after)
382
383
new_create_list.push_back(def);
384
384
else if (def->after == first_keyword)
386
385
new_create_list.push_front(def);
390
388
CreateField *find;
807
798
if (not name_lock)
810
new_table_identifier.getSQLPath(path);
811
my_error(ER_TABLE_EXISTS_ERROR, MYF(0), path.c_str());
800
my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_table_identifier.getSQLPath().c_str());
815
804
if (plugin::StorageEngine::doesTableExist(session, new_table_identifier))
818
new_table_identifier.getSQLPath(path);
820
806
/* Table will be closed by Session::executeCommand() */
821
my_error(ER_TABLE_EXISTS_ERROR, MYF(0), path.c_str());
807
my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_table_identifier.getSQLPath().c_str());
823
table::Cache::singleton().mutex().lock(); /* ALTER TABLe */
809
LOCK_open.lock(); /* ALTER TABLe */
824
810
session.unlink_open_table(name_lock);
825
table::Cache::singleton().mutex().unlock();
970
948
while the fact that the table is still open gives us protection
971
949
from concurrent DDL statements.
973
table::Cache::singleton().mutex().lock(); /* DDL wait for/blocker */
951
LOCK_open.lock(); /* DDL wait for/blocker */
974
952
wait_while_table_is_used(session, table, HA_EXTRA_FORCE_REOPEN);
975
table::Cache::singleton().mutex().unlock();
976
954
error= table->cursor->ha_enable_indexes(HA_KEY_SWITCH_NONUNIQ_SAVE);
977
955
/* COND_refresh will be signaled in close_thread_tables() */
980
table::Cache::singleton().mutex().lock(); /* DDL wait for/blocker */
958
LOCK_open.lock(); /* DDL wait for/blocker */
981
959
wait_while_table_is_used(session, table, HA_EXTRA_FORCE_REOPEN);
982
table::Cache::singleton().mutex().unlock();
983
961
error=table->cursor->ha_disable_indexes(HA_KEY_SWITCH_NONUNIQ_SAVE);
984
962
/* COND_refresh will be signaled in close_thread_tables() */
997
975
table->getAlias());
1000
table::Cache::singleton().mutex().lock(); /* Lock to remove all instances of table from table cache before ALTER */
978
LOCK_open.lock(); /* Lock to remove all instances of table from table cache before ALTER */
1002
980
Unlike to the above case close_cached_table() below will remove ALL
1003
981
instances of Table from table cache (it will also remove table lock
1004
982
held by this thread). So to make actual table renaming and writing
1005
983
to binlog atomic we have to put them into the same critical section
1006
protected by table::Cache::singleton().mutex() mutex. This also removes gap for races between
984
protected by LOCK_open mutex. This also removes gap for races between
1007
985
access() and mysql_rename_table() calls.
1150
1124
if (alter_info->error_if_not_empty && session->row_count)
1152
my_error(ER_INVALID_ALTER_TABLE_FOR_NOT_NULL, MYF(0));
1126
const char *f_val= 0;
1127
enum enum_drizzle_timestamp_type t_type= DRIZZLE_TIMESTAMP_DATE;
1129
switch (alter_info->datetime_field->sql_type)
1131
case DRIZZLE_TYPE_DATE:
1132
f_val= "0000-00-00";
1133
t_type= DRIZZLE_TIMESTAMP_DATE;
1135
case DRIZZLE_TYPE_DATETIME:
1136
f_val= "0000-00-00 00:00:00";
1137
t_type= DRIZZLE_TIMESTAMP_DATETIME;
1140
/* Shouldn't get here. */
1143
bool save_abort_on_warning= session->abort_on_warning;
1144
session->abort_on_warning= true;
1145
make_truncated_value_warning(session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
1146
f_val, internal::strlength(f_val), t_type,
1147
alter_info->datetime_field->field_name);
1148
session->abort_on_warning= save_abort_on_warning;
1155
1151
if (original_table_identifier.isTmp())
1287
1285
/* Try to get everything back. */
1290
plugin::StorageEngine::dropTable(*session, new_table_identifier);
1288
quick_rm_table(*session, new_table_identifier);
1292
plugin::StorageEngine::dropTable(*session, new_table_as_temporary);
1290
quick_rm_table(*session, new_table_as_temporary);
1294
1292
mysql_rename_table(*session, original_engine, original_table_to_drop, original_table_identifier);
1298
plugin::StorageEngine::dropTable(*session, original_table_to_drop);
1296
quick_rm_table(*session, original_table_to_drop);
1496
FileSort filesort(*session);
1497
1494
from->sort.io_cache= new internal::IO_CACHE;
1499
1496
memset(&tables, 0, sizeof(tables));
1500
1497
tables.table= from;
1501
tables.setTableName(const_cast<char *>(from->getMutableShare()->getTableName()));
1502
tables.alias= const_cast<char *>(tables.getTableName());
1503
tables.setSchemaName(const_cast<char *>(from->getMutableShare()->getSchemaName()));
1498
tables.alias= tables.table_name= const_cast<char *>(from->getMutableShare()->getTableName());
1499
tables.db= const_cast<char *>(from->getMutableShare()->getSchemaName());
1506
1502
if (session->lex->select_lex.setup_ref_array(session, order_num) ||
1507
1503
setup_order(session, session->lex->select_lex.ref_pointer_array,
1508
1504
&tables, fields, all_fields, order) ||
1509
1505
!(sortorder= make_unireg_sortorder(order, &length, NULL)) ||
1510
(from->sort.found_records= filesort.run(from, sortorder, length,
1511
(optimizer::SqlSelect *) 0, HA_POS_ERROR,
1512
1, examined_rows)) == HA_POS_ERROR)
1506
(from->sort.found_records= filesort(session, from, sortorder, length,
1507
(optimizer::SqlSelect *) 0, HA_POS_ERROR,
1508
1, &examined_rows)) ==
1546
1543
to->next_number_field->reset();
1549
for (CopyField *copy_ptr= copy; copy_ptr != copy_end ; copy_ptr++)
1546
for (CopyField *copy_ptr=copy ; copy_ptr != copy_end ; copy_ptr++)
1551
if (not copy->to_field->hasDefault() and copy->from_null_ptr and *copy->from_null_ptr & copy->from_bit)
1553
copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
1554
ER_WARN_DATA_TRUNCATED, 1);
1555
copy->to_field->reset();
1560
1548
copy_ptr->do_copy(copy_ptr);
1568
1550
prev_insert_id= to->cursor->next_insert_id;
1569
1551
error= to->cursor->insertRecord(to->record[0]);
1570
1552
to->auto_increment_field_not_null= false;
1574
if (!ignore || to->cursor->is_fatal_error(error, HA_CHECK_DUP))
1557
to->cursor->is_fatal_error(error, HA_CHECK_DUP))
1576
1559
to->print_error(error, MYF(0));