1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
*
* Copyright (C) 2009 Sun Microsystems
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "config.h"
#include <drizzled/show.h>
#include <drizzled/lock.h>
#include <drizzled/session.h>
#include <drizzled/statement/create_table.h>
#include <drizzled/table_identifier.h>
namespace drizzled
{
bool statement::CreateTable::execute()
{
TableList *first_table= (TableList *) session->lex->select_lex.table_list.first;
TableList *all_tables= session->lex->query_tables;
assert(first_table == all_tables && first_table != 0);
Select_Lex *select_lex= &session->lex->select_lex;
Select_Lex_Unit *unit= &session->lex->unit;
bool need_start_waiting= false;
bool res= false;
bool link_to_local= false;
bool lex_identified_temp_table=
create_table_proto.type() == message::Table::TEMPORARY;
if (is_engine_set)
{
create_info.db_type=
plugin::StorageEngine::findByName(*session, create_table_proto.engine().name());
if (create_info.db_type == NULL)
{
my_error(ER_UNKNOWN_STORAGE_ENGINE, MYF(0),
create_table_proto.name().c_str());
return true;
}
}
else /* We now get the default, place it in create_info, and put the engine name in table proto */
{
create_info.db_type= session->getDefaultStorageEngine();
}
/*
Now we set the name in our Table proto so that it will match
create_info.db_type.
*/
{
message::Table::StorageEngine *protoengine;
protoengine= create_table_proto.mutable_engine();
protoengine->set_name(create_info.db_type->getName());
}
/* If CREATE TABLE of non-temporary table, do implicit commit */
if (! lex_identified_temp_table)
{
if (! session->endActiveTransaction())
{
return true;
}
}
/* Skip first table, which is the table we are creating */
TableList *create_table= session->lex->unlink_first_table(&link_to_local);
TableList *select_tables= session->lex->query_tables;
/*
Now that we have the engine, we can figure out the table identifier. We need the engine in order
to determine if the table is transactional or not if it is temp.
*/
TableIdentifier new_table_identifier(create_table->db,
create_table->table_name,
create_table_proto.type() != message::Table::TEMPORARY ? STANDARD_TABLE : TEMP_TABLE);
if (create_table_precheck(new_table_identifier))
{
/* put tables back for PS rexecuting */
session->lex->link_first_table_back(create_table, link_to_local);
return true;
}
/* Might have been updated in create_table_precheck */
create_info.alias= create_table->alias;
/*
The create-select command will open and read-lock the select table
and then create, open and write-lock the new table. If a global
read lock steps in, we get a deadlock. The write lock waits for
the global read lock, while the global read lock waits for the
select table to be closed. So we wait until the global readlock is
gone before starting both steps. Note that
wait_if_global_read_lock() sets a protection against a new global
read lock when it succeeds. This needs to be released by
start_waiting_global_read_lock(). We protect the normal CREATE
TABLE in the same way. That way we avoid that a new table is
created during a gobal read lock.
*/
if (! (need_start_waiting= ! wait_if_global_read_lock(session, 0, 1)))
{
/* put tables back for PS rexecuting */
session->lex->link_first_table_back(create_table, link_to_local);
return true;
}
if (select_lex->item_list.elements) // With select
{
select_result *result;
select_lex->options|= SELECT_NO_UNLOCK;
unit->set_limit(select_lex);
if (! lex_identified_temp_table)
{
session->lex->link_first_table_back(create_table, link_to_local);
create_table->create= true;
}
if (! (res= session->openTablesLock(session->lex->query_tables)))
{
/*
Is table which we are changing used somewhere in other parts
of query
*/
if (! lex_identified_temp_table)
{
TableList *duplicate= NULL;
create_table= session->lex->unlink_first_table(&link_to_local);
if ((duplicate= unique_table(create_table, select_tables)))
{
my_error(ER_UPDATE_TABLE_USED, MYF(0), create_table->alias);
/*
Release the protection against the global read lock and wake
everyone, who might want to set a global read lock.
*/
start_waiting_global_read_lock(session);
/* put tables back for PS rexecuting */
session->lex->link_first_table_back(create_table, link_to_local);
return true;
}
}
/*
select_create is currently not re-execution friendly and
needs to be created for every execution of a PS/SP.
*/
if ((result= new select_create(create_table,
is_if_not_exists,
&create_info,
&create_table_proto,
&alter_info,
select_lex->item_list,
session->lex->duplicates,
session->lex->ignore,
select_tables)))
{
/*
CREATE from SELECT give its Select_Lex for SELECT,
and item_list belong to SELECT
*/
res= handle_select(session, session->lex, result, 0);
delete result;
}
}
else if (! lex_identified_temp_table)
{
create_table= session->lex->unlink_first_table(&link_to_local);
}
}
else
{
/* regular create */
if (is_create_table_like)
{
res= mysql_create_like_table(session,
create_table,
select_tables,
create_table_proto,
create_info.db_type,
is_if_not_exists,
is_engine_set);
}
else
{
for (int32_t x= 0; x < alter_info.alter_proto.added_field_size(); x++)
{
message::Table::Field *field= create_table_proto.add_field();
*field= alter_info.alter_proto.added_field(x);
}
res= mysql_create_table(session,
new_table_identifier,
&create_info,
&create_table_proto,
&alter_info,
false,
0,
is_if_not_exists);
}
if (! res)
{
session->my_ok();
}
}
/*
Release the protection against the global read lock and wake
everyone, who might want to set a global read lock.
*/
start_waiting_global_read_lock(session);
return res;
}
} /* namespace drizzled */
|