207
208
ER_BAD_TABLE_ERROR, ER(ER_BAD_TABLE_ERROR),
208
209
table->getTableName());
216
drizzled::error_t local_error;
215
error= plugin::StorageEngine::dropTable(*session, identifier);
218
217
/* Generate transaction event ONLY when we successfully drop */
219
if (plugin::StorageEngine::dropTable(*session, identifier, local_error))
221
220
TransactionServices &transaction_services= TransactionServices::singleton();
222
transaction_services.dropTable(*session, identifier, if_exists);
226
if (local_error == HA_ERR_NO_SUCH_TABLE and if_exists)
229
session->clear_error();
232
if (local_error == HA_ERR_ROW_IS_REFERENCED)
234
/* the table is referenced by a foreign key constraint */
235
foreign_key_error= true;
221
transaction_services.dropTable(session, string(table->getSchemaName()), string(table->getTableName()));
224
if ((error == ENOENT || error == HA_ERR_NO_SUCH_TABLE) && if_exists)
227
session->clear_error();
230
if (error == HA_ERR_ROW_IS_REFERENCED)
232
/* the table is referenced by a foreign key constraint */
233
foreign_key_error= true;
468
472
sql_field->length= 8; // Unireg field length
469
473
(*blob_columns)++;
475
case DRIZZLE_TYPE_VARCHAR:
472
477
case DRIZZLE_TYPE_ENUM:
474
if (check_duplicates_in_interval("ENUM",
475
sql_field->field_name,
485
case DRIZZLE_TYPE_MICROTIME:
478
if (check_duplicates_in_interval("ENUM",
479
sql_field->field_name,
485
case DRIZZLE_TYPE_DATE: // Rest of string types
486
case DRIZZLE_TYPE_TIME:
487
case DRIZZLE_TYPE_DATETIME:
488
case DRIZZLE_TYPE_NULL:
490
case DRIZZLE_TYPE_DECIMAL:
486
492
case DRIZZLE_TYPE_TIMESTAMP:
487
493
/* We should replace old TIMESTAMP fields with their newer analogs */
488
494
if (sql_field->unireg_check == Field::TIMESTAMP_OLD_FIELD)
526
518
static int prepare_create_table(Session *session,
527
HA_CREATE_INFO *create_info,
528
message::Table &create_proto,
529
AlterInfo *alter_info,
531
uint32_t *db_options,
532
KeyInfo **key_info_buffer,
534
int select_field_count)
519
HA_CREATE_INFO *create_info,
520
message::Table &create_proto,
521
AlterInfo *alter_info,
523
uint32_t *db_options,
524
KeyInfo **key_info_buffer,
526
int select_field_count)
536
528
const char *key_name;
537
529
CreateField *sql_field,*dup_field;
1184
1165
key_info->name=(char*) key_name;
1188
1168
if (!key_info->name || check_column_name(key_info->name))
1190
1170
my_error(ER_WRONG_NAME_FOR_INDEX, MYF(0), key_info->name);
1194
1173
if (!(key_info->flags & HA_NULL_PART_KEY))
1199
1175
key_info->key_length=(uint16_t) key_length;
1201
1176
if (key_length > max_key_length)
1203
1178
my_error(ER_TOO_LONG_KEY,MYF(0),max_key_length);
1210
1183
if (!unique_key && !primary_key &&
1211
1184
(engine->check_flag(HTON_BIT_REQUIRE_PRIMARY_KEY)))
1213
1186
my_message(ER_REQUIRES_PRIMARY_KEY, ER(ER_REQUIRES_PRIMARY_KEY), MYF(0));
1217
1189
if (auto_increment > 0)
1219
1191
my_message(ER_WRONG_AUTO_KEY, ER(ER_WRONG_AUTO_KEY), MYF(0));
1454
1430
/* Build a Table object to pass down to the engine, and the do the actual create. */
1455
1431
if (not prepare_create_table(session, create_info, table_proto, alter_info,
1458
&key_info_buffer, &key_count,
1459
select_field_count))
1434
&key_info_buffer, &key_count,
1435
select_field_count))
1461
1437
boost_unique_lock_t lock(table::Cache::singleton().mutex()); /* CREATE TABLE (some confussion on naming, double check) */
1462
1438
error= locked_create_event(session,
1850
1828
const char *old_message=session->enter_cond(COND_refresh, table::Cache::singleton().mutex(),
1851
1829
"Waiting to get writelock");
1852
1830
session->abortLock(table->table);
1853
identifier::Table identifier(table->table->getShare()->getSchemaName(), table->table->getShare()->getTableName());
1831
TableIdentifier identifier(table->table->getShare()->getSchemaName(), table->table->getShare()->getTableName());
1854
1832
table::Cache::singleton().removeTable(session, identifier, RTFC_WAIT_OTHER_THREAD_FLAG | RTFC_CHECK_KILLED_FLAG);
1855
1833
session->exit_cond(old_message);
1856
1834
if (session->getKilled())
1954
1932
boost::unique_lock<boost::mutex> lock(table::Cache::singleton().mutex());
1955
identifier::Table identifier(table->table->getShare()->getSchemaName(), table->table->getShare()->getTableName());
1933
TableIdentifier identifier(table->table->getShare()->getSchemaName(), table->table->getShare()->getTableName());
1956
1934
table::Cache::singleton().removeTable(session, identifier, RTFC_NO_FLAG);
1960
transaction_services.autocommitOrRollback(*session, false);
1938
transaction_services.autocommitOrRollback(session, false);
1961
1939
session->endTransaction(COMMIT);
1962
1940
session->close_thread_tables();
1963
1941
table->table=0; // For query cache
1992
1970
during the call to plugin::StorageEngine::createTable().
1993
1971
See bug #28614 for more info.
1995
static bool create_table_wrapper(Session &session,
1996
const message::Table& create_table_proto,
1997
identifier::Table::const_reference destination_identifier,
1998
identifier::Table::const_reference source_identifier,
1973
static bool create_table_wrapper(Session &session, const message::Table& create_table_proto,
1974
const TableIdentifier &destination_identifier,
1975
const TableIdentifier &src_table,
1999
1976
bool is_engine_set)
2001
// We require an additional table message because during parsing we used
2002
// a "new" message and it will not have all of the information that the
2003
// source table message would have.
2004
message::Table new_table_message;
2005
drizzled::error_t error;
2007
message::table::shared_ptr source_table_message= plugin::StorageEngine::getTableMessage(session, source_identifier, error);
2009
if (not source_table_message)
2011
my_error(ER_TABLE_UNKNOWN, source_identifier);
2015
new_table_message.CopyFrom(*source_table_message);
1978
int protoerr= EEXIST;
1979
message::Table new_proto;
1980
message::table::shared_ptr src_proto;
1982
protoerr= plugin::StorageEngine::getTableDefinition(session,
1985
new_proto.CopyFrom(*src_proto);
2017
1987
if (destination_identifier.isTmp())
2019
new_table_message.set_type(message::Table::TEMPORARY);
1989
new_proto.set_type(message::Table::TEMPORARY);
2023
new_table_message.set_type(message::Table::STANDARD);
1993
new_proto.set_type(message::Table::STANDARD);
2026
1996
if (is_engine_set)
2028
new_table_message.mutable_engine()->set_name(create_table_proto.engine().name());
1998
new_proto.mutable_engine()->set_name(create_table_proto.engine().name());
2031
2001
{ // We now do a selective copy of elements on to the new table.
2032
new_table_message.set_name(create_table_proto.name());
2033
new_table_message.set_schema(create_table_proto.schema());
2034
new_table_message.set_catalog(create_table_proto.catalog());
2002
new_proto.set_name(create_table_proto.name());
2003
new_proto.set_schema(create_table_proto.schema());
2004
new_proto.set_catalog(create_table_proto.catalog());
2037
/* Fix names of foreign keys being added */
2038
for (int32_t j= 0; j < new_table_message.fk_constraint_size(); j++)
2007
if (protoerr && protoerr != EEXIST)
2040
if (new_table_message.fk_constraint(j).has_name())
2042
std::string name(new_table_message.name());
2045
name.append("_ibfk_");
2046
snprintf(number, sizeof(number), "%d", j+1);
2047
name.append(number);
2049
message::Table::ForeignKeyConstraint *pfkey= new_table_message.mutable_fk_constraint(j);
2050
pfkey->set_name(name);
2009
if (errno == ENOENT)
2010
my_error(ER_BAD_DB_ERROR,MYF(0), destination_identifier.getSchemaName().c_str());
2012
my_error(ER_CANT_CREATE_FILE, MYF(0), destination_identifier.getPath().c_str(), errno);
2055
2018
As mysql_truncate don't work on a new table at this stage of
2056
creation, instead create the table directly (for both normal and temporary tables).
2019
creation, instead create the table directly (for both normal
2020
and temporary tables).
2058
bool success= plugin::StorageEngine::createTable(session,
2059
destination_identifier,
2022
int err= plugin::StorageEngine::createTable(session,
2023
destination_identifier,
2062
if (success && not destination_identifier.isTmp())
2026
if (err == false && not destination_identifier.isTmp())
2064
2028
TransactionServices &transaction_services= TransactionServices::singleton();
2065
transaction_services.createTable(session, new_table_message);
2029
transaction_services.createTable(&session, new_proto);
2032
return err ? false : true;
2086
2050
bool create_like_table(Session* session,
2087
identifier::Table::const_reference destination_identifier,
2088
identifier::Table::const_reference source_identifier,
2089
message::Table &create_table_proto,
2090
bool is_if_not_exists,
2051
const TableIdentifier &destination_identifier,
2052
TableList* table, TableList* src_table,
2053
message::Table &create_table_proto,
2054
bool is_if_not_exists,
2093
2057
bool res= true;
2094
bool table_exists= false;
2061
By opening source table we guarantee that it exists and no concurrent
2062
DDL operation will mess with it. Later we also take an exclusive
2063
name-lock on target table name, which makes copying of .frm cursor,
2064
call to plugin::StorageEngine::createTable() and binlogging atomic
2065
against concurrent DML and DDL operations on target table.
2066
Thus by holding both these "locks" we ensure that our statement is
2067
properly isolated from all concurrent operations which matter.
2069
if (session->open_tables_from_list(&src_table, ¬_used))
2072
TableIdentifier src_identifier(src_table->table->getShare()->getSchemaName(),
2073
src_table->table->getShare()->getTableName(), src_table->table->getShare()->getType());
2097
2078
Check that destination tables does not exist. Note that its name
2185
2164
char warn_buff[DRIZZLE_ERRMSG_SIZE];
2186
2165
snprintf(warn_buff, sizeof(warn_buff),
2187
ER(ER_TABLE_EXISTS_ERROR), destination_identifier.getTableName().c_str());
2166
ER(ER_TABLE_EXISTS_ERROR), table->getTableName());
2188
2167
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
2189
ER_TABLE_EXISTS_ERROR, warn_buff);
2193
my_error(ER_TABLE_EXISTS_ERROR, destination_identifier);
2168
ER_TABLE_EXISTS_ERROR,warn_buff);
2173
my_error(ER_TABLE_EXISTS_ERROR, MYF(0), table->getTableName());