26
26
#include <drizzled/sql_lex.h>
27
27
#include <drizzled/session.h>
28
28
#include <drizzled/sql_base.h>
29
#include "drizzled/strfunc.h"
30
#include <drizzled/db.h>
31
29
#include <drizzled/lock.h>
32
30
#include <drizzled/unireg.h>
33
31
#include <drizzled/item/int.h>
34
32
#include <drizzled/item/empty_string.h>
35
33
#include <drizzled/transaction_services.h>
36
#include "drizzled/transaction_services.h"
34
#include <drizzled/transaction_services.h>
37
35
#include <drizzled/table_proto.h>
38
36
#include <drizzled/plugin/client.h>
39
37
#include <drizzled/identifier.h>
40
#include "drizzled/internal/m_string.h"
41
#include "drizzled/global_charset_info.h"
42
#include "drizzled/charset.h"
44
#include "drizzled/definition/cache.h"
47
#include "drizzled/statement/alter_table.h"
48
#include "drizzled/sql_table.h"
49
#include "drizzled/pthread_globals.h"
38
#include <drizzled/internal/m_string.h>
39
#include <drizzled/global_charset_info.h>
40
#include <drizzled/charset.h>
42
#include <drizzled/definition/cache.h>
44
#include <drizzled/statement/alter_table.h>
45
#include <drizzled/sql_table.h>
46
#include <drizzled/pthread_globals.h>
47
#include <drizzled/typelib.h>
48
#include <drizzled/plugin/storage_engine.h>
51
50
#include <algorithm>
195
192
if (session->getKilled())
198
goto err_with_placeholders;
201
TableIdentifier identifier(table->getSchemaName(), table->getTableName(), table->getInternalTmpTable() ? message::Table::INTERNAL : message::Table::STANDARD);
198
identifier::Table identifier(table->getSchemaName(), table->getTableName(), table->getInternalTmpTable() ? message::Table::INTERNAL : message::Table::STANDARD);
200
message::table::shared_ptr message= plugin::StorageEngine::getTableMessage(*session, identifier, true);
203
202
if (drop_temporary || not plugin::StorageEngine::doesTableExist(*session, identifier))
205
204
// Table was not found on disk and table can't be created from engine
207
207
push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
208
208
ER_BAD_TABLE_ERROR, ER(ER_BAD_TABLE_ERROR),
209
209
table->getTableName());
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);
218
drizzled::error_t local_error;
220
/* Generate transaction event ONLY when we successfully drop */
221
if (plugin::StorageEngine::dropTable(*session, identifier, local_error))
223
if (message) // If we have no definition, we don't know if the table should have been replicated
225
TransactionServices &transaction_services= TransactionServices::singleton();
226
transaction_services.dropTable(*session, identifier, *message, if_exists);
231
if (local_error == HA_ERR_NO_SUCH_TABLE and if_exists)
234
session->clear_error();
237
if (local_error == HA_ERR_ROW_IS_REFERENCED)
239
/* the table is referenced by a foreign key constraint */
240
foreign_key_error= true;
238
if (wrong_tables.length())
239
wrong_tables.append(',');
240
wrong_tables.append(String(table->getTableName(), system_charset_info));
248
wrong_tables.push_back(table->getTableName());
244
It's safe to unlock table::Cache::singleton().mutex(): we have an exclusive lock
247
table::Cache::singleton().mutex().unlock();
251
if (wrong_tables.length())
252
tables->unlock_table_names();
256
if (wrong_tables.size())
253
258
if (not foreign_key_error)
260
std::string table_error;
262
for (util::string::vector::iterator iter= wrong_tables.begin();
263
iter != wrong_tables.end();
269
table_error.resize(table_error.size() -1);
255
271
my_printf_error(ER_BAD_TABLE_ERROR, ER(ER_BAD_TABLE_ERROR), MYF(0),
256
wrong_tables.c_ptr());
272
table_error.c_str());
471
482
sql_field->length= 8; // Unireg field length
472
483
(*blob_columns)++;
474
case DRIZZLE_TYPE_VARCHAR:
476
486
case DRIZZLE_TYPE_ENUM:
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:
488
if (check_duplicates_in_interval("ENUM",
489
sql_field->field_name,
499
case DRIZZLE_TYPE_MICROTIME:
490
500
case DRIZZLE_TYPE_TIMESTAMP:
491
501
/* We should replace old TIMESTAMP fields with their newer analogs */
492
502
if (sql_field->unireg_check == Field::TIMESTAMP_OLD_FIELD)
504
514
else if (sql_field->unireg_check != Field::NONE)
505
516
(*timestamps_with_niladic)++;
523
case DRIZZLE_TYPE_BOOLEAN:
524
case DRIZZLE_TYPE_DATE: // Rest of string types
525
case DRIZZLE_TYPE_DATETIME:
526
case DRIZZLE_TYPE_DECIMAL:
527
case DRIZZLE_TYPE_DOUBLE:
528
case DRIZZLE_TYPE_LONG:
529
case DRIZZLE_TYPE_LONGLONG:
530
case DRIZZLE_TYPE_NULL:
531
case DRIZZLE_TYPE_TIME:
532
case DRIZZLE_TYPE_UUID:
533
case DRIZZLE_TYPE_VARCHAR:
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)
540
static int prepare_create_table(Session *session,
541
HA_CREATE_INFO *create_info,
542
message::Table &create_proto,
543
AlterInfo *alter_info,
545
uint32_t *db_options,
546
KeyInfo **key_info_buffer,
548
int select_field_count)
526
550
const char *key_name;
527
551
CreateField *sql_field,*dup_field;
530
554
KeyInfo *key_info;
531
555
KeyPartInfo *key_part_info;
532
556
int timestamps= 0, timestamps_with_niladic= 0;
534
558
int select_field_pos,auto_increment=0;
535
List_iterator<CreateField> it(alter_info->create_list);
536
List_iterator<CreateField> it2(alter_info->create_list);
559
List<CreateField>::iterator it(alter_info->create_list.begin());
560
List<CreateField>::iterator it2(alter_info->create_list.begin());
537
561
uint32_t total_uneven_bit_length= 0;
539
563
plugin::StorageEngine *engine= plugin::StorageEngine::findByName(create_proto.engine().name());
541
select_field_pos= alter_info->create_list.elements - select_field_count;
565
select_field_pos= alter_info->create_list.size() - select_field_count;
542
566
null_fields=blob_columns=0;
543
567
max_key_length= engine->max_key_length();
545
for (field_no=0; (sql_field=it++) ; field_no++)
569
for (int32_t field_no=0; (sql_field=it++) ; field_no++)
547
571
const CHARSET_INFO *save_cs;
963
992
message::Table::Field *protofield= NULL;
965
List_iterator<Key_part_spec> cols(key->columns), cols2(key->columns);
994
List<Key_part_spec>::iterator cols(key->columns.begin());
995
List<Key_part_spec>::iterator cols2(key->columns.begin());
966
996
for (uint32_t column_nr=0 ; (column=cols++) ; column_nr++)
969
999
Key_part_spec *dup_column;
970
1000
int proto_field_nr= 0;
1002
it= alter_info->create_list.begin();
974
1004
while ((sql_field=it++) && ++proto_field_nr &&
975
1005
my_strcasecmp(system_charset_info,
976
1006
column->field_name.str,
977
1007
sql_field->field_name))
981
1014
my_error(ER_KEY_COLUMN_DOES_NOT_EXITS, MYF(0), column->field_name.str);
984
1018
while ((dup_column= cols2++) != column)
986
1020
if (!my_strcasecmp(system_charset_info,
1163
1199
key_info->name=(char*) key_name;
1166
1203
if (!key_info->name || check_column_name(key_info->name))
1168
1205
my_error(ER_WRONG_NAME_FOR_INDEX, MYF(0), key_info->name);
1171
1209
if (!(key_info->flags & HA_NULL_PART_KEY))
1173
1214
key_info->key_length=(uint16_t) key_length;
1174
1216
if (key_length > max_key_length)
1176
1218
my_error(ER_TOO_LONG_KEY,MYF(0),max_key_length);
1181
1225
if (!unique_key && !primary_key &&
1182
1226
(engine->check_flag(HTON_BIT_REQUIRE_PRIMARY_KEY)))
1184
1228
my_message(ER_REQUIRES_PRIMARY_KEY, ER(ER_REQUIRES_PRIMARY_KEY), MYF(0));
1187
1232
if (auto_increment > 0)
1189
1234
my_message(ER_WRONG_AUTO_KEY, ER(ER_WRONG_AUTO_KEY), MYF(0));
1426
1467
set_table_default_charset(create_info, identifier.getSchemaName().c_str());
1428
1469
/* Build a Table object to pass down to the engine, and the do the actual create. */
1429
if (not mysql_prepare_create_table(session, create_info, table_proto, alter_info,
1432
&key_info_buffer, &key_count,
1433
select_field_count))
1470
if (not prepare_create_table(session, create_info, table_proto, alter_info,
1473
&key_info_buffer, &key_count,
1474
select_field_count))
1435
1476
boost_unique_lock_t lock(table::Cache::singleton().mutex()); /* CREATE TABLE (some confussion on naming, double check) */
1436
1477
error= locked_create_event(session,
1512
Database locking aware wrapper for mysql_create_table_no_lock(),
1551
Database locking aware wrapper for create_table_no_lock(),
1514
bool mysql_create_table(Session *session,
1515
const TableIdentifier &identifier,
1553
bool create_table(Session *session,
1554
const identifier::Table &identifier,
1516
1555
HA_CREATE_INFO *create_info,
1517
1556
message::Table &table_proto,
1518
1557
AlterInfo *alter_info,
1748
1787
item->maybe_null = 1;
1749
1788
field_list.push_back(item = new Item_empty_string("Msg_text", 255, cs));
1750
1789
item->maybe_null = 1;
1751
if (session->client->sendFields(&field_list))
1790
if (session->getClient()->sendFields(&field_list))
1754
1793
for (table= tables; table; table= table->next_local)
1756
char table_name[NAME_LEN*2+2];
1795
identifier::Table table_identifier(table->getSchemaName(), table->getTableName());
1796
std::string table_name;
1757
1797
bool fatal_error=0;
1759
snprintf(table_name, sizeof(table_name), "%s.%s", table->getSchemaName(), table->getTableName());
1799
table_identifier.getSQLPath(table_name);
1760
1801
table->lock_type= lock_type;
1761
1802
/* open only one table from local list of command */
1804
1845
char buff[FN_REFLEN + DRIZZLE_ERRMSG_SIZE];
1805
1846
uint32_t length;
1806
session->client->store(table_name);
1807
session->client->store(operator_name);
1808
session->client->store(STRING_WITH_LEN("error"));
1847
session->getClient()->store(table_name.c_str());
1848
session->getClient()->store(operator_name);
1849
session->getClient()->store(STRING_WITH_LEN("error"));
1809
1850
length= snprintf(buff, sizeof(buff), ER(ER_OPEN_AS_READONLY),
1811
session->client->store(buff, length);
1812
transaction_services.autocommitOrRollback(session, false);
1851
table_name.c_str());
1852
session->getClient()->store(buff, length);
1853
transaction_services.autocommitOrRollback(*session, false);
1813
1854
session->endTransaction(COMMIT);
1814
1855
session->close_thread_tables();
1815
lex->reset_query_tables_list(false);
1856
session->getLex()->reset_query_tables_list(false);
1816
1857
table->table=0; // For query cache
1817
if (session->client->flush())
1858
if (session->getClient()->flush())
1826
1867
const char *old_message=session->enter_cond(COND_refresh, table::Cache::singleton().mutex(),
1827
1868
"Waiting to get writelock");
1828
1869
session->abortLock(table->table);
1829
TableIdentifier identifier(table->table->getShare()->getSchemaName(), table->table->getShare()->getTableName());
1870
identifier::Table identifier(table->table->getShare()->getSchemaName(), table->table->getShare()->getTableName());
1830
1871
table::Cache::singleton().removeTable(session, identifier, RTFC_WAIT_OTHER_THREAD_FLAG | RTFC_CHECK_KILLED_FLAG);
1831
1872
session->exit_cond(old_message);
1832
1873
if (session->getKilled())
1841
lex->cleanup_after_one_table_open();
1882
session->getLex()->cleanup_after_one_table_open();
1842
1883
session->clear_error(); // these errors shouldn't get client
1844
List_iterator_fast<DRIZZLE_ERROR> it(session->warn_list);
1885
List<DRIZZLE_ERROR>::iterator it(session->warn_list.begin());
1845
1886
DRIZZLE_ERROR *err;
1846
1887
while ((err= it++))
1848
session->client->store(table_name);
1849
session->client->store(operator_name);
1850
session->client->store(warning_level_names[err->level].str,
1889
session->getClient()->store(table_name.c_str());
1890
session->getClient()->store(operator_name);
1891
session->getClient()->store(warning_level_names[err->level].str,
1851
1892
warning_level_names[err->level].length);
1852
session->client->store(err->msg);
1853
if (session->client->flush())
1893
session->getClient()->store(err->msg);
1894
if (session->getClient()->flush())
1856
1897
drizzle_reset_errors(session, true);
1858
session->client->store(table_name);
1859
session->client->store(operator_name);
1899
session->getClient()->store(table_name.c_str());
1900
session->getClient()->store(operator_name);
1861
1902
switch (result_code) {
1862
1903
case HA_ADMIN_NOT_IMPLEMENTED:
1864
1905
char buf[ERRMSGSIZE+20];
1865
1906
uint32_t length=snprintf(buf, ERRMSGSIZE,
1866
1907
ER(ER_CHECK_NOT_IMPLEMENTED), operator_name);
1867
session->client->store(STRING_WITH_LEN("note"));
1868
session->client->store(buf, length);
1908
session->getClient()->store(STRING_WITH_LEN("note"));
1909
session->getClient()->store(buf, length);
1872
1913
case HA_ADMIN_OK:
1873
session->client->store(STRING_WITH_LEN("status"));
1874
session->client->store(STRING_WITH_LEN("OK"));
1914
session->getClient()->store(STRING_WITH_LEN("status"));
1915
session->getClient()->store(STRING_WITH_LEN("OK"));
1877
1918
case HA_ADMIN_FAILED:
1878
session->client->store(STRING_WITH_LEN("status"));
1879
session->client->store(STRING_WITH_LEN("Operation failed"));
1919
session->getClient()->store(STRING_WITH_LEN("status"));
1920
session->getClient()->store(STRING_WITH_LEN("Operation failed"));
1882
1923
case HA_ADMIN_REJECT:
1883
session->client->store(STRING_WITH_LEN("status"));
1884
session->client->store(STRING_WITH_LEN("Operation need committed state"));
1924
session->getClient()->store(STRING_WITH_LEN("status"));
1925
session->getClient()->store(STRING_WITH_LEN("Operation need committed state"));
1885
1926
open_for_modify= false;
1888
1929
case HA_ADMIN_ALREADY_DONE:
1889
session->client->store(STRING_WITH_LEN("status"));
1890
session->client->store(STRING_WITH_LEN("Table is already up to date"));
1930
session->getClient()->store(STRING_WITH_LEN("status"));
1931
session->getClient()->store(STRING_WITH_LEN("Table is already up to date"));
1893
1934
case HA_ADMIN_CORRUPT:
1894
session->client->store(STRING_WITH_LEN("error"));
1895
session->client->store(STRING_WITH_LEN("Corrupt"));
1935
session->getClient()->store(STRING_WITH_LEN("error"));
1936
session->getClient()->store(STRING_WITH_LEN("Corrupt"));
1899
1940
case HA_ADMIN_INVALID:
1900
session->client->store(STRING_WITH_LEN("error"));
1901
session->client->store(STRING_WITH_LEN("Invalid argument"));
1941
session->getClient()->store(STRING_WITH_LEN("error"));
1942
session->getClient()->store(STRING_WITH_LEN("Invalid argument"));
1904
1945
default: // Probably HA_ADMIN_INTERNAL_ERROR
1930
1971
boost::unique_lock<boost::mutex> lock(table::Cache::singleton().mutex());
1931
TableIdentifier identifier(table->table->getShare()->getSchemaName(), table->table->getShare()->getTableName());
1972
identifier::Table identifier(table->table->getShare()->getSchemaName(), table->table->getShare()->getTableName());
1932
1973
table::Cache::singleton().removeTable(session, identifier, RTFC_NO_FLAG);
1936
transaction_services.autocommitOrRollback(session, false);
1977
transaction_services.autocommitOrRollback(*session, false);
1937
1978
session->endTransaction(COMMIT);
1938
1979
session->close_thread_tables();
1939
1980
table->table=0; // For query cache
1940
if (session->client->flush())
1981
if (session->getClient()->flush())
1968
2009
during the call to plugin::StorageEngine::createTable().
1969
2010
See bug #28614 for more info.
1971
static bool create_table_wrapper(Session &session, const message::Table& create_table_proto,
1972
const TableIdentifier &destination_identifier,
1973
const TableIdentifier &src_table,
2012
static bool create_table_wrapper(Session &session,
2013
const message::Table& create_table_proto,
2014
identifier::Table::const_reference destination_identifier,
2015
identifier::Table::const_reference source_identifier,
1974
2016
bool is_engine_set)
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);
2018
// We require an additional table message because during parsing we used
2019
// a "new" message and it will not have all of the information that the
2020
// source table message would have.
2021
message::Table new_table_message;
2023
message::table::shared_ptr source_table_message= plugin::StorageEngine::getTableMessage(session, source_identifier);
2025
if (not source_table_message)
2027
my_error(ER_TABLE_UNKNOWN, source_identifier);
2031
new_table_message.CopyFrom(*source_table_message);
1985
2033
if (destination_identifier.isTmp())
1987
new_proto.set_type(message::Table::TEMPORARY);
2035
new_table_message.set_type(message::Table::TEMPORARY);
1991
new_proto.set_type(message::Table::STANDARD);
2039
new_table_message.set_type(message::Table::STANDARD);
1994
2042
if (is_engine_set)
1996
new_proto.mutable_engine()->set_name(create_table_proto.engine().name());
2044
new_table_message.mutable_engine()->set_name(create_table_proto.engine().name());
1999
2047
{ // We now do a selective copy of elements on to the new table.
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());
2048
new_table_message.set_name(create_table_proto.name());
2049
new_table_message.set_schema(create_table_proto.schema());
2050
new_table_message.set_catalog(create_table_proto.catalog());
2005
if (protoerr && protoerr != EEXIST)
2053
/* Fix names of foreign keys being added */
2054
for (int32_t j= 0; j < new_table_message.fk_constraint_size(); j++)
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);
2056
if (new_table_message.fk_constraint(j).has_name())
2058
std::string name(new_table_message.name());
2061
name.append("_ibfk_");
2062
snprintf(number, sizeof(number), "%d", j+1);
2063
name.append(number);
2065
message::Table::ForeignKeyConstraint *pfkey= new_table_message.mutable_fk_constraint(j);
2066
pfkey->set_name(name);
2016
2071
As mysql_truncate don't work on a new table at this stage of
2017
creation, instead create the table directly (for both normal
2018
and temporary tables).
2072
creation, instead create the table directly (for both normal and temporary tables).
2020
int err= plugin::StorageEngine::createTable(session,
2021
destination_identifier,
2074
bool success= plugin::StorageEngine::createTable(session,
2075
destination_identifier,
2024
if (err == false && not destination_identifier.isTmp())
2078
if (success && not destination_identifier.isTmp())
2026
2080
TransactionServices &transaction_services= TransactionServices::singleton();
2027
transaction_services.createTable(&session, new_proto);
2081
transaction_services.createTable(session, new_table_message);
2030
return err ? false : true;
2034
2088
Create a table identical to the specified table
2037
mysql_create_like_table()
2038
2092
session Thread object
2039
2093
table Table list element for target table
2040
2094
src_table Table list element for source table
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,
2102
bool create_like_table(Session* session,
2103
identifier::Table::const_reference destination_identifier,
2104
identifier::Table::const_reference source_identifier,
2105
message::Table &create_table_proto,
2106
bool is_if_not_exists,
2055
2109
bool res= true;
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());
2110
bool table_exists= false;
2076
2113
Check that destination tables does not exist. Note that its name
2162
2201
char warn_buff[DRIZZLE_ERRMSG_SIZE];
2163
2202
snprintf(warn_buff, sizeof(warn_buff),
2164
ER(ER_TABLE_EXISTS_ERROR), table->getTableName());
2203
ER(ER_TABLE_EXISTS_ERROR), destination_identifier.getTableName().c_str());
2165
2204
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
2166
ER_TABLE_EXISTS_ERROR,warn_buff);
2171
my_error(ER_TABLE_EXISTS_ERROR, MYF(0), table->getTableName());
2205
ER_TABLE_EXISTS_ERROR, warn_buff);
2209
my_error(ER_TABLE_EXISTS_ERROR, destination_identifier);
2179
bool mysql_analyze_table(Session* session, TableList* tables, HA_CHECK_OPT* check_opt)
2218
bool analyze_table(Session* session, TableList* tables, HA_CHECK_OPT* check_opt)
2181
2220
thr_lock_type lock_type = TL_READ_NO_INSERT;
2183
return(mysql_admin_table(session, tables, check_opt,
2222
return(admin_table(session, tables, check_opt,
2184
2223
"analyze", lock_type, true,
2185
2224
&Cursor::ha_analyze));
2189
bool mysql_check_table(Session* session, TableList* tables,HA_CHECK_OPT* check_opt)
2228
bool check_table(Session* session, TableList* tables,HA_CHECK_OPT* check_opt)
2191
2230
thr_lock_type lock_type = TL_READ_NO_INSERT;
2193
return(mysql_admin_table(session, tables, check_opt,
2232
return(admin_table(session, tables, check_opt,
2194
2233
"check", lock_type,
2196
2235
&Cursor::ha_check));