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>
29
#include "drizzled/strfunc.h"
30
#include <drizzled/db.h>
30
31
#include <drizzled/lock.h>
31
32
#include <drizzled/unireg.h>
32
33
#include <drizzled/item/int.h>
33
34
#include <drizzled/item/empty_string.h>
34
35
#include <drizzled/transaction_services.h>
35
#include <drizzled/transaction_services.h>
36
#include "drizzled/transaction_services.h"
36
37
#include <drizzled/table_proto.h>
37
38
#include <drizzled/plugin/client.h>
38
39
#include <drizzled/identifier.h>
39
#include <drizzled/internal/m_string.h>
40
#include <drizzled/global_charset_info.h>
41
#include <drizzled/charset.h>
43
#include <drizzled/definition/cache.h>
45
#include <drizzled/statement/alter_table.h>
46
#include <drizzled/sql_table.h>
47
#include <drizzled/pthread_globals.h>
48
#include <drizzled/typelib.h>
49
#include <drizzled/plugin/storage_engine.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"
51
51
#include <algorithm>
138
140
-1 Thread was killed
141
int rm_table_part2(Session *session, TableList *tables, bool if_exists,
143
int mysql_rm_table_part2(Session *session, TableList *tables, bool if_exists,
144
146
TableList *table;
145
util::string::vector wrong_tables;
147
149
bool foreign_key_error= false;
151
boost::mutex::scoped_lock scopedLock(table::Cache::singleton().mutex());
153
if (not drop_temporary && session->lock_table_names_exclusively(tables))
158
/* Don't give warnings for not found errors, as we already generate notes */
159
session->no_warnings_for_error= 1;
161
for (table= tables; table; table= table->next_local)
163
identifier::Table tmp_identifier(table->getSchemaName(), table->getTableName());
165
error= session->drop_temporary_table(tmp_identifier);
169
// removed temporary table
151
LOCK_open.lock(); /* Part 2 of rm a table */
153
if (not drop_temporary && session->lock_table_names_exclusively(tables))
159
/* Don't give warnings for not found errors, as we already generate notes */
160
session->no_warnings_for_error= 1;
162
for (table= tables; table; table= table->next_local)
164
TableIdentifier tmp_identifier(table->getSchemaName(), table->getTableName());
166
error= session->drop_temporary_table(tmp_identifier);
170
// removed temporary table
174
goto err_with_placeholders;
176
// temporary table not found
180
if (drop_temporary == false)
183
abort_locked_tables(session, tmp_identifier);
184
table::Cache::singleton().removeTable(session, tmp_identifier,
185
RTFC_WAIT_OTHER_THREAD_FLAG |
186
RTFC_CHECK_KILLED_FLAG);
188
If the table was used in lock tables, remember it so that
189
unlock_table_names can free it
191
if ((locked_table= drop_locked_tables(session, tmp_identifier)))
192
table->table= locked_table;
194
if (session->getKilled())
197
goto err_with_placeholders;
200
TableIdentifier identifier(table->getSchemaName(), table->getTableName(), table->getInternalTmpTable() ? message::Table::INTERNAL : message::Table::STANDARD);
202
if (drop_temporary || not plugin::StorageEngine::doesTableExist(*session, identifier))
204
// Table was not found on disk and table can't be created from engine
206
push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
207
ER_BAD_TABLE_ERROR, ER(ER_BAD_TABLE_ERROR),
208
table->getTableName());
175
// temporary table not found
214
error= plugin::StorageEngine::dropTable(*session, identifier);
216
if ((error == ENOENT || error == HA_ERR_NO_SUCH_TABLE) && if_exists)
179
if (drop_temporary == false)
182
abort_locked_tables(session, tmp_identifier);
183
table::Cache::singleton().removeTable(session, tmp_identifier,
184
RTFC_WAIT_OTHER_THREAD_FLAG |
185
RTFC_CHECK_KILLED_FLAG);
187
If the table was used in lock tables, remember it so that
188
unlock_table_names can free it
190
if ((locked_table= drop_locked_tables(session, tmp_identifier)))
191
table->table= locked_table;
193
if (session->getKilled())
199
identifier::Table identifier(table->getSchemaName(), table->getTableName(), table->getInternalTmpTable() ? message::Table::INTERNAL : message::Table::STANDARD);
201
message::table::shared_ptr message= plugin::StorageEngine::getTableMessage(*session, identifier, true);
203
if (drop_temporary || not plugin::StorageEngine::doesTableExist(*session, identifier))
205
// Table was not found on disk and table can't be created from engine
208
push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
209
ER_BAD_TABLE_ERROR, ER(ER_BAD_TABLE_ERROR),
210
table->getTableName());
219
drizzled::error_t local_error;
221
/* Generate transaction event ONLY when we successfully drop */
222
if (plugin::StorageEngine::dropTable(*session, identifier, local_error))
224
if (message) // If we have no definition, we don't know if the table should have been replicated
226
TransactionServices &transaction_services= TransactionServices::singleton();
227
transaction_services.dropTable(*session, identifier, *message, if_exists);
232
if (local_error == HA_ERR_NO_SUCH_TABLE and if_exists)
235
session->clear_error();
238
if (local_error == HA_ERR_ROW_IS_REFERENCED)
240
/* the table is referenced by a foreign key constraint */
241
foreign_key_error= true;
249
wrong_tables.push_back(table->getTableName());
253
tables->unlock_table_names();
257
if (wrong_tables.size())
219
session->clear_error();
222
if (error == HA_ERR_ROW_IS_REFERENCED)
224
/* the table is referenced by a foreign key constraint */
225
foreign_key_error= true;
229
if (error == 0 || (if_exists && foreign_key_error == false))
231
TransactionServices &transaction_services= TransactionServices::singleton();
232
transaction_services.dropTable(session, string(table->getSchemaName()), string(table->getTableName()), if_exists);
237
if (wrong_tables.length())
238
wrong_tables.append(',');
239
wrong_tables.append(String(table->getTableName(), system_charset_info));
243
It's safe to unlock LOCK_open: we have an exclusive lock
249
if (wrong_tables.length())
259
251
if (not foreign_key_error)
261
std::string table_error;
263
for (util::string::vector::iterator iter= wrong_tables.begin();
264
iter != wrong_tables.end();
270
table_error.resize(table_error.size() -1);
272
253
my_printf_error(ER_BAD_TABLE_ERROR, ER(ER_BAD_TABLE_ERROR), MYF(0),
273
table_error.c_str());
254
wrong_tables.c_ptr());
515
523
else if (sql_field->unireg_check != Field::NONE)
517
524
(*timestamps_with_niladic)++;
524
case DRIZZLE_TYPE_BOOLEAN:
525
case DRIZZLE_TYPE_DATE: // Rest of string types
526
case DRIZZLE_TYPE_DATETIME:
527
case DRIZZLE_TYPE_DECIMAL:
528
case DRIZZLE_TYPE_DOUBLE:
529
case DRIZZLE_TYPE_LONG:
530
case DRIZZLE_TYPE_LONGLONG:
531
case DRIZZLE_TYPE_NULL:
532
case DRIZZLE_TYPE_TIME:
533
case DRIZZLE_TYPE_UUID:
534
case DRIZZLE_TYPE_VARCHAR:
541
static int prepare_create_table(Session *session,
542
HA_CREATE_INFO *create_info,
543
message::Table &create_proto,
544
AlterInfo *alter_info,
546
uint32_t *db_options,
547
KeyInfo **key_info_buffer,
549
int select_field_count)
535
static int mysql_prepare_create_table(Session *session,
536
HA_CREATE_INFO *create_info,
537
message::Table &create_proto,
538
AlterInfo *alter_info,
540
uint32_t *db_options,
541
KeyInfo **key_info_buffer,
543
int select_field_count)
551
545
const char *key_name;
552
546
CreateField *sql_field,*dup_field;
1846
1809
char buff[FN_REFLEN + DRIZZLE_ERRMSG_SIZE];
1847
1810
uint32_t length;
1848
session->getClient()->store(table_name.c_str());
1849
session->getClient()->store(operator_name);
1850
session->getClient()->store(STRING_WITH_LEN("error"));
1811
session->client->store(table_name);
1812
session->client->store(operator_name);
1813
session->client->store(STRING_WITH_LEN("error"));
1851
1814
length= snprintf(buff, sizeof(buff), ER(ER_OPEN_AS_READONLY),
1852
table_name.c_str());
1853
session->getClient()->store(buff, length);
1854
transaction_services.autocommitOrRollback(*session, false);
1816
session->client->store(buff, length);
1817
transaction_services.autocommitOrRollback(session, false);
1855
1818
session->endTransaction(COMMIT);
1856
1819
session->close_thread_tables();
1857
session->getLex()->reset_query_tables_list(false);
1820
lex->reset_query_tables_list(false);
1858
1821
table->table=0; // For query cache
1859
if (session->getClient()->flush())
1822
if (session->client->flush())
1883
session->getLex()->cleanup_after_one_table_open();
1846
lex->cleanup_after_one_table_open();
1884
1847
session->clear_error(); // these errors shouldn't get client
1886
List<DRIZZLE_ERROR>::iterator it(session->warn_list.begin());
1849
List_iterator_fast<DRIZZLE_ERROR> it(session->warn_list);
1887
1850
DRIZZLE_ERROR *err;
1888
1851
while ((err= it++))
1890
session->getClient()->store(table_name.c_str());
1891
session->getClient()->store(operator_name);
1892
session->getClient()->store(warning_level_names[err->level].str,
1853
session->client->store(table_name);
1854
session->client->store(operator_name);
1855
session->client->store(warning_level_names[err->level].str,
1893
1856
warning_level_names[err->level].length);
1894
session->getClient()->store(err->msg);
1895
if (session->getClient()->flush())
1857
session->client->store(err->msg);
1858
if (session->client->flush())
1898
1861
drizzle_reset_errors(session, true);
1900
session->getClient()->store(table_name.c_str());
1901
session->getClient()->store(operator_name);
1863
session->client->store(table_name);
1864
session->client->store(operator_name);
1903
1866
switch (result_code) {
1904
1867
case HA_ADMIN_NOT_IMPLEMENTED:
1906
1869
char buf[ERRMSGSIZE+20];
1907
1870
uint32_t length=snprintf(buf, ERRMSGSIZE,
1908
1871
ER(ER_CHECK_NOT_IMPLEMENTED), operator_name);
1909
session->getClient()->store(STRING_WITH_LEN("note"));
1910
session->getClient()->store(buf, length);
1872
session->client->store(STRING_WITH_LEN("note"));
1873
session->client->store(buf, length);
1914
1877
case HA_ADMIN_OK:
1915
session->getClient()->store(STRING_WITH_LEN("status"));
1916
session->getClient()->store(STRING_WITH_LEN("OK"));
1878
session->client->store(STRING_WITH_LEN("status"));
1879
session->client->store(STRING_WITH_LEN("OK"));
1919
1882
case HA_ADMIN_FAILED:
1920
session->getClient()->store(STRING_WITH_LEN("status"));
1921
session->getClient()->store(STRING_WITH_LEN("Operation failed"));
1883
session->client->store(STRING_WITH_LEN("status"));
1884
session->client->store(STRING_WITH_LEN("Operation failed"));
1924
1887
case HA_ADMIN_REJECT:
1925
session->getClient()->store(STRING_WITH_LEN("status"));
1926
session->getClient()->store(STRING_WITH_LEN("Operation need committed state"));
1888
session->client->store(STRING_WITH_LEN("status"));
1889
session->client->store(STRING_WITH_LEN("Operation need committed state"));
1927
1890
open_for_modify= false;
1930
1893
case HA_ADMIN_ALREADY_DONE:
1931
session->getClient()->store(STRING_WITH_LEN("status"));
1932
session->getClient()->store(STRING_WITH_LEN("Table is already up to date"));
1894
session->client->store(STRING_WITH_LEN("status"));
1895
session->client->store(STRING_WITH_LEN("Table is already up to date"));
1935
1898
case HA_ADMIN_CORRUPT:
1936
session->getClient()->store(STRING_WITH_LEN("error"));
1937
session->getClient()->store(STRING_WITH_LEN("Corrupt"));
1899
session->client->store(STRING_WITH_LEN("error"));
1900
session->client->store(STRING_WITH_LEN("Corrupt"));
1941
1904
case HA_ADMIN_INVALID:
1942
session->getClient()->store(STRING_WITH_LEN("error"));
1943
session->getClient()->store(STRING_WITH_LEN("Invalid argument"));
1905
session->client->store(STRING_WITH_LEN("error"));
1906
session->client->store(STRING_WITH_LEN("Invalid argument"));
1946
1909
default: // Probably HA_ADMIN_INTERNAL_ERROR
2001
1964
Altough exclusive name-lock on target table protects us from concurrent
2002
1965
DML and DDL operations on it we still want to wrap .FRM creation and call
2003
1966
to plugin::StorageEngine::createTable() in critical section protected by
2004
table::Cache::singleton().mutex() in order to provide minimal atomicity against operations which
1967
LOCK_open in order to provide minimal atomicity against operations which
2005
1968
disregard name-locks, like I_S implementation, for example. This is a
2006
1969
temporary and should not be copied. Instead we should fix our code to
2007
1970
always honor name-locks.
2009
Also some engines (e.g. NDB cluster) require that table::Cache::singleton().mutex() should be held
1972
Also some engines (e.g. NDB cluster) require that LOCK_open should be held
2010
1973
during the call to plugin::StorageEngine::createTable().
2011
1974
See bug #28614 for more info.
2013
static bool create_table_wrapper(Session &session,
2014
const message::Table& create_table_proto,
2015
identifier::Table::const_reference destination_identifier,
2016
identifier::Table::const_reference source_identifier,
1976
static bool create_table_wrapper(Session &session, const message::Table& create_table_proto,
1977
TableIdentifier &destination_identifier,
1978
TableIdentifier &src_table,
2017
1979
bool is_engine_set)
2019
// We require an additional table message because during parsing we used
2020
// a "new" message and it will not have all of the information that the
2021
// source table message would have.
2022
message::Table new_table_message;
2024
message::table::shared_ptr source_table_message= plugin::StorageEngine::getTableMessage(session, source_identifier);
2026
if (not source_table_message)
2028
my_error(ER_TABLE_UNKNOWN, source_identifier);
2032
new_table_message.CopyFrom(*source_table_message);
1981
int protoerr= EEXIST;
1982
message::Table new_proto;
1983
message::table::shared_ptr src_proto;
1985
protoerr= plugin::StorageEngine::getTableDefinition(session,
1988
new_proto.CopyFrom(*src_proto);
2034
1990
if (destination_identifier.isTmp())
2036
new_table_message.set_type(message::Table::TEMPORARY);
1992
new_proto.set_type(message::Table::TEMPORARY);
2040
new_table_message.set_type(message::Table::STANDARD);
1996
new_proto.set_type(message::Table::STANDARD);
2043
1999
if (is_engine_set)
2045
new_table_message.mutable_engine()->set_name(create_table_proto.engine().name());
2001
new_proto.mutable_engine()->set_name(create_table_proto.engine().name());
2048
2004
{ // We now do a selective copy of elements on to the new table.
2049
new_table_message.set_name(create_table_proto.name());
2050
new_table_message.set_schema(create_table_proto.schema());
2051
new_table_message.set_catalog(create_table_proto.catalog());
2005
new_proto.set_name(create_table_proto.name());
2006
new_proto.set_schema(create_table_proto.schema());
2007
new_proto.set_catalog(create_table_proto.catalog());
2054
/* Fix names of foreign keys being added */
2055
for (int32_t j= 0; j < new_table_message.fk_constraint_size(); j++)
2010
if (protoerr && protoerr != EEXIST)
2057
if (new_table_message.fk_constraint(j).has_name())
2059
std::string name(new_table_message.name());
2062
name.append("_ibfk_");
2063
snprintf(number, sizeof(number), "%d", j+1);
2064
name.append(number);
2066
message::Table::ForeignKeyConstraint *pfkey= new_table_message.mutable_fk_constraint(j);
2067
pfkey->set_name(name);
2012
if (errno == ENOENT)
2013
my_error(ER_BAD_DB_ERROR,MYF(0), destination_identifier.getSchemaName().c_str());
2015
my_error(ER_CANT_CREATE_FILE, MYF(0), destination_identifier.getPath().c_str(), errno);
2072
2021
As mysql_truncate don't work on a new table at this stage of
2073
creation, instead create the table directly (for both normal and temporary tables).
2022
creation, instead create the table directly (for both normal
2023
and temporary tables).
2075
bool success= plugin::StorageEngine::createTable(session,
2076
destination_identifier,
2025
int err= plugin::StorageEngine::createTable(session,
2026
destination_identifier,
2079
if (success && not destination_identifier.isTmp())
2029
if (err == false && not destination_identifier.isTmp())
2081
2031
TransactionServices &transaction_services= TransactionServices::singleton();
2082
transaction_services.createTable(session, new_table_message);
2032
transaction_services.createTable(&session, new_proto);
2035
return err ? false : true;
2089
2039
Create a table identical to the specified table
2042
mysql_create_like_table()
2093
2043
session Thread object
2094
2044
table Table list element for target table
2095
2045
src_table Table list element for source table
2103
bool create_like_table(Session* session,
2104
identifier::Table::const_reference destination_identifier,
2105
identifier::Table::const_reference source_identifier,
2106
message::Table &create_table_proto,
2107
bool is_if_not_exists,
2053
bool mysql_create_like_table(Session* session,
2054
TableIdentifier &destination_identifier,
2055
TableList* table, TableList* src_table,
2056
message::Table &create_table_proto,
2057
bool is_if_not_exists,
2110
2060
bool res= true;
2111
bool table_exists= false;
2064
By opening source table we guarantee that it exists and no concurrent
2065
DDL operation will mess with it. Later we also take an exclusive
2066
name-lock on target table name, which makes copying of .frm cursor,
2067
call to plugin::StorageEngine::createTable() and binlogging atomic
2068
against concurrent DML and DDL operations on target table.
2069
Thus by holding both these "locks" we ensure that our statement is
2070
properly isolated from all concurrent operations which matter.
2072
if (session->open_tables_from_list(&src_table, ¬_used))
2075
TableIdentifier src_identifier(src_table->table->getShare()->getSchemaName(),
2076
src_table->table->getShare()->getTableName(), src_table->table->getShare()->getType());
2114
2081
Check that destination tables does not exist. Note that its name
2202
2167
char warn_buff[DRIZZLE_ERRMSG_SIZE];
2203
2168
snprintf(warn_buff, sizeof(warn_buff),
2204
ER(ER_TABLE_EXISTS_ERROR), destination_identifier.getTableName().c_str());
2169
ER(ER_TABLE_EXISTS_ERROR), table->getTableName());
2205
2170
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
2206
ER_TABLE_EXISTS_ERROR, warn_buff);
2210
my_error(ER_TABLE_EXISTS_ERROR, destination_identifier);
2171
ER_TABLE_EXISTS_ERROR,warn_buff);
2176
my_error(ER_TABLE_EXISTS_ERROR, MYF(0), table->getTableName());
2219
bool analyze_table(Session* session, TableList* tables, HA_CHECK_OPT* check_opt)
2184
bool mysql_analyze_table(Session* session, TableList* tables, HA_CHECK_OPT* check_opt)
2221
2186
thr_lock_type lock_type = TL_READ_NO_INSERT;
2223
return(admin_table(session, tables, check_opt,
2188
return(mysql_admin_table(session, tables, check_opt,
2224
2189
"analyze", lock_type, true,
2225
2190
&Cursor::ha_analyze));
2229
bool check_table(Session* session, TableList* tables,HA_CHECK_OPT* check_opt)
2194
bool mysql_check_table(Session* session, TableList* tables,HA_CHECK_OPT* check_opt)
2231
2196
thr_lock_type lock_type = TL_READ_NO_INSERT;
2233
return(admin_table(session, tables, check_opt,
2198
return(mysql_admin_table(session, tables, check_opt,
2234
2199
"check", lock_type,
2236
2201
&Cursor::ha_check));