18
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
#include <drizzled/server_includes.h>
22
22
#include <drizzled/show.h>
23
23
#include <drizzled/lock.h>
24
24
#include <drizzled/session.h>
25
25
#include <drizzled/statement/create_table.h>
26
#include <drizzled/message.h>
27
#include <drizzled/identifier.h>
41
37
bool need_start_waiting= false;
43
39
bool link_to_local= false;
44
bool lex_identified_temp_table=
45
create_table_message.type() == message::Table::TEMPORARY;
41
if (create_info.used_fields & HA_CREATE_USED_ENGINE)
49
44
create_info.db_type=
50
plugin::StorageEngine::findByName(*session, create_table_message.engine().name());
45
plugin::StorageEngine::findByName(session, create_table_proto.engine().name());
52
47
if (create_info.db_type == NULL)
54
49
my_error(ER_UNKNOWN_STORAGE_ENGINE, MYF(0),
55
create_table_message.engine().name().c_str());
50
create_table_proto.name().c_str());
62
57
create_info.db_type= session->getDefaultStorageEngine();
65
if (not validateCreateTableOption())
63
Now we set the name in our Table proto so that it will match
67
message::Table::StorageEngine *protoengine;
69
protoengine= create_table_proto.mutable_engine();
70
protoengine->set_name(create_info.db_type->getName());
71
74
/* If CREATE TABLE of non-temporary table, do implicit commit */
72
if (not lex_identified_temp_table)
75
if (! (create_info.options & HA_LEX_CREATE_TMP_TABLE))
74
if (not session->endActiveTransaction())
77
if (! session->endActiveTransaction())
80
83
TableList *create_table= session->lex->unlink_first_table(&link_to_local);
81
84
TableList *select_tables= session->lex->query_tables;
83
drizzled::message::init(create_table_message, create_table_message.name(), create_table->getSchemaName(), create_info.db_type->getName());
85
TableIdentifier new_table_identifier(create_table->getSchemaName(),
86
create_table->getTableName(),
87
create_table_message.type());
89
if (not check(new_table_identifier))
86
if (create_table_precheck(session, select_tables, create_table))
91
88
/* put tables back for PS rexecuting */
92
89
session->lex->link_first_table_back(create_table, link_to_local);
109
106
TABLE in the same way. That way we avoid that a new table is
110
107
created during a gobal read lock.
112
if (! (need_start_waiting= not session->wait_if_global_read_lock(0, 1)))
109
if (! (need_start_waiting= ! wait_if_global_read_lock(session, 0, 1)))
114
111
/* put tables back for PS rexecuting */
115
112
session->lex->link_first_table_back(create_table, link_to_local);
123
120
select_lex->options|= SELECT_NO_UNLOCK;
124
121
unit->set_limit(select_lex);
126
if (not lex_identified_temp_table)
123
if (! (create_info.options & HA_LEX_CREATE_TMP_TABLE))
128
125
session->lex->link_first_table_back(create_table, link_to_local);
129
create_table->setCreate(true);
126
create_table->create= true;
132
if (not (res= session->openTablesLock(session->lex->query_tables)))
129
if (! (res= session->openTablesLock(session->lex->query_tables)))
135
132
Is table which we are changing used somewhere in other parts
138
if (not lex_identified_temp_table)
135
if (! (create_info.options & HA_LEX_CREATE_TMP_TABLE))
140
137
TableList *duplicate= NULL;
141
138
create_table= session->lex->unlink_first_table(&link_to_local);
142
if ((duplicate= unique_table(create_table, select_tables)))
139
if ((duplicate= unique_table(session, create_table, select_tables, 0)))
144
141
my_error(ER_UPDATE_TABLE_USED, MYF(0), create_table->alias);
146
143
Release the protection against the global read lock and wake
147
144
everyone, who might want to set a global read lock.
149
session->startWaitingGlobalReadLock();
146
start_waiting_global_read_lock(session);
150
147
/* put tables back for PS rexecuting */
151
148
session->lex->link_first_table_back(create_table, link_to_local);
159
155
needs to be created for every execution of a PS/SP.
161
157
if ((result= new select_create(create_table,
164
create_table_message,
166
161
select_lex->item_list,
167
162
session->lex->duplicates,
168
163
session->lex->ignore,
170
new_table_identifier)))
173
167
CREATE from SELECT give its Select_Lex for SELECT,
187
181
/* regular create */
188
if (is_create_table_like)
182
if (create_info.options & HA_LEX_CREATE_TABLE_LIKE)
190
184
res= mysql_create_like_table(session,
191
new_table_identifier,
194
create_table_message,
201
for (int32_t x= 0; x < alter_info.alter_proto.added_field_size(); x++)
203
message::Table::Field *field= create_table_message.add_field();
205
*field= alter_info.alter_proto.added_field(x);
208
192
res= mysql_create_table(session,
209
new_table_identifier,
194
create_table->table_name,
211
create_table_message,
220
203
session->my_ok();
225
208
Release the protection against the global read lock and wake
226
209
everyone, who might want to set a global read lock.
228
session->startWaitingGlobalReadLock();
211
start_waiting_global_read_lock(session);
233
bool statement::CreateTable::check(const TableIdentifier &identifier)
235
// Check table name for validity
236
if (not identifier.isValid())
239
// See if any storage engine objects to the name of the file
240
if (not plugin::StorageEngine::canCreateTable(identifier))
242
my_error(ER_DBACCESS_DENIED_ERROR, MYF(0), "", "", identifier.getSchemaName().c_str());
247
// Make sure the schema exists, we will do this again during the actual
248
// create for the table.
249
if (not plugin::StorageEngine::doesSchemaExist(identifier))
251
my_error(ER_BAD_DB_ERROR, MYF(0), identifier.getSchemaName().c_str());
259
bool statement::CreateTable::validateCreateTableOption()
262
size_t num_engine_options= create_table_message.engine().options_size();
264
assert(create_info.db_type);
266
for (size_t y= 0; y < num_engine_options; ++y)
268
bool valid= create_info.db_type->validateCreateTableOption(create_table_message.engine().options(y).name(),
269
create_table_message.engine().options(y).state());
273
my_error(ER_UNKNOWN_ENGINE_OPTION, MYF(0),
274
create_table_message.engine().options(y).name().c_str(),
275
create_table_message.engine().options(y).state().c_str());
284
216
} /* namespace drizzled */