207
208
ER_BAD_TABLE_ERROR, ER(ER_BAD_TABLE_ERROR),
208
209
table->getTableName());
216
drizzled::error_t local_error;
218
/* Generate transaction event ONLY when we successfully drop */
219
if (plugin::StorageEngine::dropTable(*session, identifier, local_error))
221
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;
215
error= plugin::StorageEngine::dropTable(*session, identifier);
217
if ((error == ENOENT || error == HA_ERR_NO_SUCH_TABLE) && if_exists)
220
session->clear_error();
223
if (error == HA_ERR_ROW_IS_REFERENCED)
225
/* the table is referenced by a foreign key constraint */
226
foreign_key_error= true;
230
if (error == 0 || (if_exists && foreign_key_error == false))
232
TransactionServices &transaction_services= TransactionServices::singleton();
233
transaction_services.dropTable(session, string(table->getSchemaName()), string(table->getTableName()), if_exists);
468
471
sql_field->length= 8; // Unireg field length
469
472
(*blob_columns)++;
474
case DRIZZLE_TYPE_VARCHAR:
472
476
case DRIZZLE_TYPE_ENUM:
474
if (check_duplicates_in_interval("ENUM",
475
sql_field->field_name,
485
case DRIZZLE_TYPE_MICROTIME:
477
if (check_duplicates_in_interval("ENUM",
478
sql_field->field_name,
484
case DRIZZLE_TYPE_DATE: // Rest of string types
485
case DRIZZLE_TYPE_DATETIME:
486
case DRIZZLE_TYPE_NULL:
488
case DRIZZLE_TYPE_DECIMAL:
486
490
case DRIZZLE_TYPE_TIMESTAMP:
487
491
/* We should replace old TIMESTAMP fields with their newer analogs */
488
492
if (sql_field->unireg_check == Field::TIMESTAMP_OLD_FIELD)
500
504
else if (sql_field->unireg_check != Field::NONE)
502
505
(*timestamps_with_niladic)++;
509
case DRIZZLE_TYPE_BOOLEAN:
510
case DRIZZLE_TYPE_DATE: // Rest of string types
511
case DRIZZLE_TYPE_DATETIME:
512
case DRIZZLE_TYPE_DECIMAL:
513
case DRIZZLE_TYPE_DOUBLE:
514
case DRIZZLE_TYPE_LONG:
515
case DRIZZLE_TYPE_LONGLONG:
516
case DRIZZLE_TYPE_NULL:
517
case DRIZZLE_TYPE_TIME:
518
case DRIZZLE_TYPE_UUID:
519
case DRIZZLE_TYPE_VARCHAR:
526
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)
516
static int mysql_prepare_create_table(Session *session,
517
HA_CREATE_INFO *create_info,
518
message::Table &create_proto,
519
AlterInfo *alter_info,
521
uint32_t *db_options,
522
KeyInfo **key_info_buffer,
524
int select_field_count)
536
526
const char *key_name;
537
527
CreateField *sql_field,*dup_field;
1184
1163
key_info->name=(char*) key_name;
1188
1166
if (!key_info->name || check_column_name(key_info->name))
1190
1168
my_error(ER_WRONG_NAME_FOR_INDEX, MYF(0), key_info->name);
1194
1171
if (!(key_info->flags & HA_NULL_PART_KEY))
1199
1173
key_info->key_length=(uint16_t) key_length;
1201
1174
if (key_length > max_key_length)
1203
1176
my_error(ER_TOO_LONG_KEY,MYF(0),max_key_length);
1210
1181
if (!unique_key && !primary_key &&
1211
1182
(engine->check_flag(HTON_BIT_REQUIRE_PRIMARY_KEY)))
1213
1184
my_message(ER_REQUIRES_PRIMARY_KEY, ER(ER_REQUIRES_PRIMARY_KEY), MYF(0));
1217
1187
if (auto_increment > 0)
1219
1189
my_message(ER_WRONG_AUTO_KEY, ER(ER_WRONG_AUTO_KEY), MYF(0));
1452
1426
set_table_default_charset(create_info, identifier.getSchemaName().c_str());
1454
1428
/* Build a Table object to pass down to the engine, and the do the actual create. */
1455
if (not prepare_create_table(session, create_info, table_proto, alter_info,
1458
&key_info_buffer, &key_count,
1459
select_field_count))
1429
if (not mysql_prepare_create_table(session, create_info, table_proto, alter_info,
1432
&key_info_buffer, &key_count,
1433
select_field_count))
1461
1435
boost_unique_lock_t lock(table::Cache::singleton().mutex()); /* CREATE TABLE (some confussion on naming, double check) */
1462
1436
error= locked_create_event(session,
1536
Database locking aware wrapper for create_table_no_lock(),
1512
Database locking aware wrapper for mysql_create_table_no_lock(),
1538
bool create_table(Session *session,
1539
const identifier::Table &identifier,
1514
bool mysql_create_table(Session *session,
1515
const TableIdentifier &identifier,
1540
1516
HA_CREATE_INFO *create_info,
1541
1517
message::Table &table_proto,
1542
1518
AlterInfo *alter_info,
1828
1804
char buff[FN_REFLEN + DRIZZLE_ERRMSG_SIZE];
1829
1805
uint32_t length;
1830
session->getClient()->store(table_name);
1831
session->getClient()->store(operator_name);
1832
session->getClient()->store(STRING_WITH_LEN("error"));
1806
session->client->store(table_name);
1807
session->client->store(operator_name);
1808
session->client->store(STRING_WITH_LEN("error"));
1833
1809
length= snprintf(buff, sizeof(buff), ER(ER_OPEN_AS_READONLY),
1835
session->getClient()->store(buff, length);
1836
transaction_services.autocommitOrRollback(*session, false);
1811
session->client->store(buff, length);
1812
transaction_services.autocommitOrRollback(session, false);
1837
1813
session->endTransaction(COMMIT);
1838
1814
session->close_thread_tables();
1839
1815
lex->reset_query_tables_list(false);
1840
1816
table->table=0; // For query cache
1841
if (session->getClient()->flush())
1817
if (session->client->flush())
1850
1826
const char *old_message=session->enter_cond(COND_refresh, table::Cache::singleton().mutex(),
1851
1827
"Waiting to get writelock");
1852
1828
session->abortLock(table->table);
1853
identifier::Table identifier(table->table->getShare()->getSchemaName(), table->table->getShare()->getTableName());
1829
TableIdentifier identifier(table->table->getShare()->getSchemaName(), table->table->getShare()->getTableName());
1854
1830
table::Cache::singleton().removeTable(session, identifier, RTFC_WAIT_OTHER_THREAD_FLAG | RTFC_CHECK_KILLED_FLAG);
1855
1831
session->exit_cond(old_message);
1856
1832
if (session->getKilled())
1869
1845
DRIZZLE_ERROR *err;
1870
1846
while ((err= it++))
1872
session->getClient()->store(table_name);
1873
session->getClient()->store(operator_name);
1874
session->getClient()->store(warning_level_names[err->level].str,
1848
session->client->store(table_name);
1849
session->client->store(operator_name);
1850
session->client->store(warning_level_names[err->level].str,
1875
1851
warning_level_names[err->level].length);
1876
session->getClient()->store(err->msg);
1877
if (session->getClient()->flush())
1852
session->client->store(err->msg);
1853
if (session->client->flush())
1880
1856
drizzle_reset_errors(session, true);
1882
session->getClient()->store(table_name);
1883
session->getClient()->store(operator_name);
1858
session->client->store(table_name);
1859
session->client->store(operator_name);
1885
1861
switch (result_code) {
1886
1862
case HA_ADMIN_NOT_IMPLEMENTED:
1888
1864
char buf[ERRMSGSIZE+20];
1889
1865
uint32_t length=snprintf(buf, ERRMSGSIZE,
1890
1866
ER(ER_CHECK_NOT_IMPLEMENTED), operator_name);
1891
session->getClient()->store(STRING_WITH_LEN("note"));
1892
session->getClient()->store(buf, length);
1867
session->client->store(STRING_WITH_LEN("note"));
1868
session->client->store(buf, length);
1896
1872
case HA_ADMIN_OK:
1897
session->getClient()->store(STRING_WITH_LEN("status"));
1898
session->getClient()->store(STRING_WITH_LEN("OK"));
1873
session->client->store(STRING_WITH_LEN("status"));
1874
session->client->store(STRING_WITH_LEN("OK"));
1901
1877
case HA_ADMIN_FAILED:
1902
session->getClient()->store(STRING_WITH_LEN("status"));
1903
session->getClient()->store(STRING_WITH_LEN("Operation failed"));
1878
session->client->store(STRING_WITH_LEN("status"));
1879
session->client->store(STRING_WITH_LEN("Operation failed"));
1906
1882
case HA_ADMIN_REJECT:
1907
session->getClient()->store(STRING_WITH_LEN("status"));
1908
session->getClient()->store(STRING_WITH_LEN("Operation need committed state"));
1883
session->client->store(STRING_WITH_LEN("status"));
1884
session->client->store(STRING_WITH_LEN("Operation need committed state"));
1909
1885
open_for_modify= false;
1912
1888
case HA_ADMIN_ALREADY_DONE:
1913
session->getClient()->store(STRING_WITH_LEN("status"));
1914
session->getClient()->store(STRING_WITH_LEN("Table is already up to date"));
1889
session->client->store(STRING_WITH_LEN("status"));
1890
session->client->store(STRING_WITH_LEN("Table is already up to date"));
1917
1893
case HA_ADMIN_CORRUPT:
1918
session->getClient()->store(STRING_WITH_LEN("error"));
1919
session->getClient()->store(STRING_WITH_LEN("Corrupt"));
1894
session->client->store(STRING_WITH_LEN("error"));
1895
session->client->store(STRING_WITH_LEN("Corrupt"));
1923
1899
case HA_ADMIN_INVALID:
1924
session->getClient()->store(STRING_WITH_LEN("error"));
1925
session->getClient()->store(STRING_WITH_LEN("Invalid argument"));
1900
session->client->store(STRING_WITH_LEN("error"));
1901
session->client->store(STRING_WITH_LEN("Invalid argument"));
1928
1904
default: // Probably HA_ADMIN_INTERNAL_ERROR
1954
1930
boost::unique_lock<boost::mutex> lock(table::Cache::singleton().mutex());
1955
identifier::Table identifier(table->table->getShare()->getSchemaName(), table->table->getShare()->getTableName());
1931
TableIdentifier identifier(table->table->getShare()->getSchemaName(), table->table->getShare()->getTableName());
1956
1932
table::Cache::singleton().removeTable(session, identifier, RTFC_NO_FLAG);
1960
transaction_services.autocommitOrRollback(*session, false);
1936
transaction_services.autocommitOrRollback(session, false);
1961
1937
session->endTransaction(COMMIT);
1962
1938
session->close_thread_tables();
1963
1939
table->table=0; // For query cache
1964
if (session->getClient()->flush())
1940
if (session->client->flush())
1992
1968
during the call to plugin::StorageEngine::createTable().
1993
1969
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,
1971
static bool create_table_wrapper(Session &session, const message::Table& create_table_proto,
1972
const TableIdentifier &destination_identifier,
1973
const TableIdentifier &src_table,
1999
1974
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);
1976
int protoerr= EEXIST;
1977
message::Table new_proto;
1978
message::table::shared_ptr src_proto;
1980
protoerr= plugin::StorageEngine::getTableDefinition(session,
1983
new_proto.CopyFrom(*src_proto);
2017
1985
if (destination_identifier.isTmp())
2019
new_table_message.set_type(message::Table::TEMPORARY);
1987
new_proto.set_type(message::Table::TEMPORARY);
2023
new_table_message.set_type(message::Table::STANDARD);
1991
new_proto.set_type(message::Table::STANDARD);
2026
1994
if (is_engine_set)
2028
new_table_message.mutable_engine()->set_name(create_table_proto.engine().name());
1996
new_proto.mutable_engine()->set_name(create_table_proto.engine().name());
2031
1999
{ // 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());
2000
new_proto.set_name(create_table_proto.name());
2001
new_proto.set_schema(create_table_proto.schema());
2002
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++)
2005
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);
2007
if (errno == ENOENT)
2008
my_error(ER_BAD_DB_ERROR,MYF(0), destination_identifier.getSchemaName().c_str());
2010
my_error(ER_CANT_CREATE_FILE, MYF(0), destination_identifier.getPath().c_str(), errno);
2055
2016
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).
2017
creation, instead create the table directly (for both normal
2018
and temporary tables).
2058
bool success= plugin::StorageEngine::createTable(session,
2059
destination_identifier,
2020
int err= plugin::StorageEngine::createTable(session,
2021
destination_identifier,
2062
if (success && not destination_identifier.isTmp())
2024
if (err == false && not destination_identifier.isTmp())
2064
2026
TransactionServices &transaction_services= TransactionServices::singleton();
2065
transaction_services.createTable(session, new_table_message);
2027
transaction_services.createTable(&session, new_proto);
2030
return err ? false : true;
2072
2034
Create a table identical to the specified table
2037
mysql_create_like_table()
2076
2038
session Thread object
2077
2039
table Table list element for target table
2078
2040
src_table Table list element for source table
2086
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,
2048
bool mysql_create_like_table(Session* session,
2049
const TableIdentifier &destination_identifier,
2050
TableList* table, TableList* src_table,
2051
message::Table &create_table_proto,
2052
bool is_if_not_exists,
2093
2055
bool res= true;
2094
bool table_exists= false;
2059
By opening source table we guarantee that it exists and no concurrent
2060
DDL operation will mess with it. Later we also take an exclusive
2061
name-lock on target table name, which makes copying of .frm cursor,
2062
call to plugin::StorageEngine::createTable() and binlogging atomic
2063
against concurrent DML and DDL operations on target table.
2064
Thus by holding both these "locks" we ensure that our statement is
2065
properly isolated from all concurrent operations which matter.
2067
if (session->open_tables_from_list(&src_table, ¬_used))
2070
TableIdentifier src_identifier(src_table->table->getShare()->getSchemaName(),
2071
src_table->table->getShare()->getTableName(), src_table->table->getShare()->getType());
2097
2076
Check that destination tables does not exist. Note that its name
2185
2162
char warn_buff[DRIZZLE_ERRMSG_SIZE];
2186
2163
snprintf(warn_buff, sizeof(warn_buff),
2187
ER(ER_TABLE_EXISTS_ERROR), destination_identifier.getTableName().c_str());
2164
ER(ER_TABLE_EXISTS_ERROR), table->getTableName());
2188
2165
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
2189
ER_TABLE_EXISTS_ERROR, warn_buff);
2193
my_error(ER_TABLE_EXISTS_ERROR, destination_identifier);
2166
ER_TABLE_EXISTS_ERROR,warn_buff);
2171
my_error(ER_TABLE_EXISTS_ERROR, MYF(0), table->getTableName());
2202
bool analyze_table(Session* session, TableList* tables, HA_CHECK_OPT* check_opt)
2179
bool mysql_analyze_table(Session* session, TableList* tables, HA_CHECK_OPT* check_opt)
2204
2181
thr_lock_type lock_type = TL_READ_NO_INSERT;
2206
return(admin_table(session, tables, check_opt,
2183
return(mysql_admin_table(session, tables, check_opt,
2207
2184
"analyze", lock_type, true,
2208
2185
&Cursor::ha_analyze));
2212
bool check_table(Session* session, TableList* tables,HA_CHECK_OPT* check_opt)
2189
bool mysql_check_table(Session* session, TableList* tables,HA_CHECK_OPT* check_opt)
2214
2191
thr_lock_type lock_type = TL_READ_NO_INSERT;
2216
return(admin_table(session, tables, check_opt,
2193
return(mysql_admin_table(session, tables, check_opt,
2217
2194
"check", lock_type,
2219
2196
&Cursor::ha_check));