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/table_identifier.h>
27
using namespace drizzled;
33
29
bool statement::CreateTable::execute()
40
36
bool need_start_waiting= false;
42
38
bool link_to_local= false;
43
bool lex_identified_temp_table=
44
create_table_message.type() == message::Table::TEMPORARY;
49
plugin::StorageEngine::findByName(*session, create_table_message.engine().name());
51
if (create_info.db_type == NULL)
53
my_error(ER_UNKNOWN_STORAGE_ENGINE, MYF(0),
54
create_table_message.name().c_str());
59
else /* We now get the default, place it in create_info, and put the engine name in table proto */
61
create_info.db_type= session->getDefaultStorageEngine();
65
Now we set the name in our Table proto so that it will match
69
message::Table::StorageEngine *protoengine;
71
protoengine= create_table_message.mutable_engine();
72
protoengine->set_name(create_info.db_type->getName());
76
40
/* If CREATE TABLE of non-temporary table, do implicit commit */
77
if (not lex_identified_temp_table)
41
if (! (session->lex->create_info.options & HA_LEX_CREATE_TMP_TABLE))
79
if (not session->endActiveTransaction())
43
if (! session->endActiveTransaction())
84
48
/* Skip first table, which is the table we are creating */
85
49
TableList *create_table= session->lex->unlink_first_table(&link_to_local);
86
50
TableList *select_tables= session->lex->query_tables;
90
Now that we have the engine, we can figure out the table identifier. We need the engine in order
91
to determine if the table is transactional or not if it is temp.
94
create_table_message.set_schema(create_table->db);
96
TableIdentifier new_table_identifier(create_table->db,
97
create_table->table_name,
98
create_table_message.type());
100
if (create_table_precheck(new_table_identifier))
52
Code below (especially in mysql_create_table() and select_create
53
methods) may modify HA_CREATE_INFO structure in LEX, so we have to
54
use a copy of this structure to make execution prepared statement-
55
safe. A shallow copy is enough as this code won't modify any memory
56
referenced from this structure.
58
HA_CREATE_INFO create_info(session->lex->create_info);
60
We need to copy alter_info for the same reasons of re-execution
61
safety, only in case of Alter_info we have to do (almost) a deep
64
Alter_info alter_info(session->lex->alter_info, session->mem_root);
66
if (session->is_fatal_error)
68
/* If out of memory when creating a copy of alter_info. */
69
/* put tables back for PS rexecuting */
70
session->lex->link_first_table_back(create_table, link_to_local);
74
if (create_table_precheck(session, select_tables, create_table))
102
76
/* put tables back for PS rexecuting */
103
77
session->lex->link_first_table_back(create_table, link_to_local);
134
107
select_lex->options|= SELECT_NO_UNLOCK;
135
108
unit->set_limit(select_lex);
137
if (not lex_identified_temp_table)
110
if (! (create_info.options & HA_LEX_CREATE_TMP_TABLE))
139
112
session->lex->link_first_table_back(create_table, link_to_local);
140
113
create_table->create= true;
143
if (not (res= session->openTablesLock(session->lex->query_tables)))
116
if (! (res= session->openTablesLock(session->lex->query_tables)))
146
119
Is table which we are changing used somewhere in other parts
149
if (not lex_identified_temp_table)
122
if (! (create_info.options & HA_LEX_CREATE_TMP_TABLE))
151
124
TableList *duplicate= NULL;
152
125
create_table= session->lex->unlink_first_table(&link_to_local);
153
if ((duplicate= unique_table(create_table, select_tables)))
126
if ((duplicate= unique_table(session, create_table, select_tables, 0)))
155
128
my_error(ER_UPDATE_TABLE_USED, MYF(0), create_table->alias);
169
142
needs to be created for every execution of a PS/SP.
171
144
if ((result= new select_create(create_table,
174
create_table_message,
146
session->lex->create_table_proto,
176
148
select_lex->item_list,
177
149
session->lex->duplicates,
178
150
session->lex->ignore,
180
new_table_identifier)))
183
154
CREATE from SELECT give its Select_Lex for SELECT,
190
else if (not lex_identified_temp_table)
161
else if (! (create_info.options & HA_LEX_CREATE_TMP_TABLE))
192
163
create_table= session->lex->unlink_first_table(&link_to_local);
168
/* So that CREATE TEMPORARY TABLE gets to binlog at commit/rollback */
169
if (create_info.options & HA_LEX_CREATE_TMP_TABLE)
170
session->options|= OPTION_KEEP_LOG;
197
171
/* regular create */
198
if (is_create_table_like)
172
if (create_info.options & HA_LEX_CREATE_TABLE_LIKE)
200
174
res= mysql_create_like_table(session,
201
new_table_identifier,
204
create_table_message,
211
for (int32_t x= 0; x < alter_info.alter_proto.added_field_size(); x++)
213
message::Table::Field *field= create_table_message.add_field();
215
*field= alter_info.alter_proto.added_field(x);
218
181
res= mysql_create_table(session,
219
new_table_identifier,
183
create_table->table_name,
221
create_table_message,
185
session->lex->create_table_proto,
230
192
session->my_ok();