107
105
assert(select_lex->db);
109
107
/* 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;
108
message::Table original_table_message;
112
TableIdentifier identifier(first_table->getSchemaName(), first_table->getTableName());
110
TableIdentifier identifier(first_table->db, first_table->table_name);
113
111
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());
113
my_error(ER_BAD_TABLE_ERROR, MYF(0), identifier.getSQLPath().c_str());
121
117
if (not create_info.db_type)
123
119
create_info.db_type=
124
plugin::StorageEngine::findByName(*session, original_table_message->engine().name());
120
plugin::StorageEngine::findByName(*session, original_table_message.engine().name());
126
122
if (not create_info.db_type)
129
identifier.getSQLPath(path);
130
my_error(ER_BAD_TABLE_ERROR, MYF(0), path.c_str());
124
my_error(ER_BAD_TABLE_ERROR, MYF(0), identifier.getSQLPath().c_str());
136
130
if (not validateCreateTableOption())
139
135
/* ALTER TABLE ends previous transaction */
140
136
if (not session->endActiveTransaction())
143
if (not (need_start_waiting= not session->wait_if_global_read_lock(0, 1)))
141
if (not (need_start_waiting= ! wait_if_global_read_lock(session, 0, 1)))
147
if (original_table_message->type() == message::Table::STANDARD )
147
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());
149
TableIdentifier identifier(first_table->db, first_table->table_name);
150
TableIdentifier new_identifier(select_lex->db ? select_lex->db : first_table->db,
151
session->lex->name.str ? session->lex->name.str : first_table->table_name);
153
153
res= alter_table(session,
157
*original_table_message,
157
original_table_message,
158
158
create_table_message,
161
161
select_lex->order_list.elements,
162
(Order *) select_lex->order_list.first,
162
(order_st *) select_lex->order_list.first,
163
163
session->lex->ignore);
167
TableIdentifier catch22(first_table->getSchemaName(), first_table->getTableName());
168
Table *table= session->find_temporary_table(catch22);
167
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(),
170
TableIdentifier identifier(first_table->db, first_table->table_name, table->getMutableShare()->getPath());
171
TableIdentifier new_identifier(select_lex->db ? select_lex->db : first_table->db,
172
session->lex->name.str ? session->lex->name.str : first_table->table_name,
174
173
table->getMutableShare()->getPath());
176
175
res= alter_table(session,
180
*original_table_message,
179
original_table_message,
181
180
create_table_message,
184
183
select_lex->order_list.elements,
185
(Order *) select_lex->order_list.first,
184
(order_st *) select_lex->order_list.first,
186
185
session->lex->ignore);
368
364
if (def->change && ! def->field)
370
366
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.
370
Check that the DATE/DATETIME not null field we are going to add is
371
either has a default value or the '0000-00-00' is allowed by the
373
If the '0000-00-00' value isn't allowed then raise the error_if_not_empty
374
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)
376
if ((def->sql_type == DRIZZLE_TYPE_DATE ||
377
def->sql_type == DRIZZLE_TYPE_DATETIME) &&
378
! alter_info->datetime_field &&
379
! (~def->flags & (NO_DEFAULT_VALUE_FLAG | NOT_NULL_FLAG)))
381
alter_info->datetime_field= def;
378
382
alter_info->error_if_not_empty= true;
380
384
if (! def->after)
382
385
new_create_list.push_back(def);
384
386
else if (def->after == first_keyword)
386
387
new_create_list.push_front(def);
390
390
CreateField *find;
807
802
if (not name_lock)
810
new_table_identifier.getSQLPath(path);
811
my_error(ER_TABLE_EXISTS_ERROR, MYF(0), path.c_str());
804
my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_table_identifier.getSQLPath().c_str());
815
808
if (plugin::StorageEngine::doesTableExist(session, new_table_identifier))
818
new_table_identifier.getSQLPath(path);
820
810
/* Table will be closed by Session::executeCommand() */
821
my_error(ER_TABLE_EXISTS_ERROR, MYF(0), path.c_str());
811
my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_table_identifier.getSQLPath().c_str());
823
table::Cache::singleton().mutex().lock(); /* ALTER TABLe */
813
LOCK_open.lock(); /* ALTER TABLe */
824
814
session.unlink_open_table(name_lock);
825
table::Cache::singleton().mutex().unlock();
970
952
while the fact that the table is still open gives us protection
971
953
from concurrent DDL statements.
973
table::Cache::singleton().mutex().lock(); /* DDL wait for/blocker */
955
LOCK_open.lock(); /* DDL wait for/blocker */
974
956
wait_while_table_is_used(session, table, HA_EXTRA_FORCE_REOPEN);
975
table::Cache::singleton().mutex().unlock();
976
958
error= table->cursor->ha_enable_indexes(HA_KEY_SWITCH_NONUNIQ_SAVE);
977
959
/* COND_refresh will be signaled in close_thread_tables() */
980
table::Cache::singleton().mutex().lock(); /* DDL wait for/blocker */
962
LOCK_open.lock(); /* DDL wait for/blocker */
981
963
wait_while_table_is_used(session, table, HA_EXTRA_FORCE_REOPEN);
982
table::Cache::singleton().mutex().unlock();
983
965
error=table->cursor->ha_disable_indexes(HA_KEY_SWITCH_NONUNIQ_SAVE);
984
966
/* COND_refresh will be signaled in close_thread_tables() */
997
979
table->getAlias());
1000
table::Cache::singleton().mutex().lock(); /* Lock to remove all instances of table from table cache before ALTER */
982
LOCK_open.lock(); /* Lock to remove all instances of table from table cache before ALTER */
1002
984
Unlike to the above case close_cached_table() below will remove ALL
1003
985
instances of Table from table cache (it will also remove table lock
1004
986
held by this thread). So to make actual table renaming and writing
1005
987
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
988
protected by LOCK_open mutex. This also removes gap for races between
1007
989
access() and mysql_rename_table() calls.
1150
1128
if (alter_info->error_if_not_empty && session->row_count)
1152
my_error(ER_INVALID_ALTER_TABLE_FOR_NOT_NULL, MYF(0));
1130
const char *f_val= 0;
1131
enum enum_drizzle_timestamp_type t_type= DRIZZLE_TIMESTAMP_DATE;
1133
switch (alter_info->datetime_field->sql_type)
1135
case DRIZZLE_TYPE_DATE:
1136
f_val= "0000-00-00";
1137
t_type= DRIZZLE_TIMESTAMP_DATE;
1139
case DRIZZLE_TYPE_DATETIME:
1140
f_val= "0000-00-00 00:00:00";
1141
t_type= DRIZZLE_TIMESTAMP_DATETIME;
1144
/* Shouldn't get here. */
1147
bool save_abort_on_warning= session->abort_on_warning;
1148
session->abort_on_warning= true;
1149
make_truncated_value_warning(session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
1150
f_val, internal::strlength(f_val), t_type,
1151
alter_info->datetime_field->field_name);
1152
session->abort_on_warning= save_abort_on_warning;
1155
1155
if (original_table_identifier.isTmp())
1287
1287
/* Try to get everything back. */
1290
plugin::StorageEngine::dropTable(*session, new_table_identifier);
1290
quick_rm_table(*session, new_table_identifier);
1292
plugin::StorageEngine::dropTable(*session, new_table_as_temporary);
1292
quick_rm_table(*session, new_table_as_temporary);
1294
1294
mysql_rename_table(*session, original_engine, original_table_to_drop, original_table_identifier);
1298
plugin::StorageEngine::dropTable(*session, original_table_to_drop);
1298
quick_rm_table(*session, original_table_to_drop);
1496
FileSort filesort(*session);
1497
1496
from->sort.io_cache= new internal::IO_CACHE;
1499
1498
memset(&tables, 0, sizeof(tables));
1500
1499
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()));
1500
tables.alias= tables.table_name= const_cast<char *>(from->getMutableShare()->getTableName());
1501
tables.db= const_cast<char *>(from->getMutableShare()->getSchemaName());
1506
1504
if (session->lex->select_lex.setup_ref_array(session, order_num) ||
1507
1505
setup_order(session, session->lex->select_lex.ref_pointer_array,
1508
1506
&tables, fields, all_fields, order) ||
1509
1507
!(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)
1508
(from->sort.found_records= filesort(session, from, sortorder, length,
1509
(optimizer::SqlSelect *) 0, HA_POS_ERROR,
1510
1, &examined_rows)) ==
1546
1545
to->next_number_field->reset();
1549
for (CopyField *copy_ptr= copy; copy_ptr != copy_end ; copy_ptr++)
1548
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
1550
copy_ptr->do_copy(copy_ptr);
1568
1552
prev_insert_id= to->cursor->next_insert_id;
1569
1553
error= to->cursor->insertRecord(to->record[0]);
1570
1554
to->auto_increment_field_not_null= false;
1574
if (!ignore || to->cursor->is_fatal_error(error, HA_CHECK_DUP))
1559
to->cursor->is_fatal_error(error, HA_CHECK_DUP))
1576
1561
to->print_error(error, MYF(0));