drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
schema_engine.cc
1/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2010 Brian Aker
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#include <config.h>
21
22#include <drizzled/session.h>
23#include <drizzled/sql_base.h>
24#include <drizzled/charset.h>
25#include <drizzled/transaction_services.h>
26#include <drizzled/open_tables_state.h>
27#include <drizzled/table/cache.h>
28#include <drizzled/plugin/storage_engine.h>
29#include <drizzled/plugin/authorization.h>
30
31namespace drizzled {
32namespace plugin {
33
34class AddSchemaNames
35{
36 identifier::schema::vector &schemas;
37
38public:
39
40 AddSchemaNames(identifier::schema::vector &of_names) :
41 schemas(of_names)
42 {
43 }
44
45 void operator() (StorageEngine *engine)
46 {
47 engine->doGetSchemaIdentifiers(schemas);
48 }
49};
50
51void StorageEngine::getIdentifiers(Session &session, identifier::schema::vector &schemas)
52{
53 // Add hook here for engines to register schema.
54 std::for_each(StorageEngine::getSchemaEngines().begin(), StorageEngine::getSchemaEngines().end(),
55 AddSchemaNames(schemas));
56
57 plugin::Authorization::pruneSchemaNames(*session.user(), schemas);
58}
59
60class StorageEngineGetSchemaDefinition
61{
62 const identifier::Schema &identifier;
63 message::schema::shared_ptr &schema_proto;
64
65public:
66 StorageEngineGetSchemaDefinition(const identifier::Schema &identifier_arg,
67 message::schema::shared_ptr &schema_proto_arg) :
68 identifier(identifier_arg),
69 schema_proto(schema_proto_arg)
70 {
71 }
72
73 bool operator() (StorageEngine *engine)
74 {
75 schema_proto= engine->doGetSchemaDefinition(identifier);
76 return bool(schema_proto);
77 }
78};
79
80/*
81 Return value is "if parsed"
82*/
83message::schema::shared_ptr StorageEngine::getSchemaDefinition(const drizzled::identifier::Table &identifier)
84{
85 identifier::Schema schema_identifier= identifier;
86 return StorageEngine::getSchemaDefinition(schema_identifier);
87}
88
89message::schema::shared_ptr StorageEngine::getSchemaDefinition(const identifier::Schema &identifier)
90{
91 message::schema::shared_ptr proto;
92
93 EngineVector::iterator iter=
94 std::find_if(StorageEngine::getSchemaEngines().begin(), StorageEngine::getSchemaEngines().end(),
95 StorageEngineGetSchemaDefinition(identifier, proto));
96
97 if (iter != StorageEngine::getSchemaEngines().end())
98 {
99 return proto;
100 }
101
102 return message::schema::shared_ptr();
103}
104
105bool StorageEngine::doesSchemaExist(const identifier::Schema &identifier)
106{
107 message::schema::shared_ptr proto;
108
109 return bool(StorageEngine::getSchemaDefinition(identifier));
110}
111
112
113const charset_info_st *StorageEngine::getSchemaCollation(const identifier::Schema &identifier)
114{
115 message::schema::shared_ptr schmema_proto= StorageEngine::getSchemaDefinition(identifier);
116 if (not schmema_proto || not schmema_proto->has_collation())
117 return default_charset_info;
118 const std::string buffer= schmema_proto->collation();
119 if (const charset_info_st* cs= get_charset_by_name(buffer.c_str()))
120 return cs;
121 errmsg_printf(error::ERROR, _("Error while loading database options: '%s':"), identifier.getSQLPath().c_str());
122 errmsg_printf(error::ERROR, ER(ER_UNKNOWN_COLLATION), buffer.c_str());
123 return default_charset_info;
124}
125
126class CreateSchema
127{
128 const drizzled::message::Schema &schema_message;
129 uint64_t &success_count;
130
131public:
132
133 CreateSchema(const drizzled::message::Schema &arg, uint64_t &success_count_arg) :
134 schema_message(arg),
135 success_count(success_count_arg)
136 {
137 }
138
139 void operator() (StorageEngine *engine)
140 {
141 // @todo eomeday check that at least one engine said "true"
142 bool success= engine->doCreateSchema(schema_message);
143
144 if (success)
145 {
146 success_count++;
147 TransactionServices::allocateNewTransactionId();
148 }
149 }
150};
151
152bool StorageEngine::createSchema(const drizzled::message::Schema &schema_message)
153{
154 // Add hook here for engines to register schema.
155 uint64_t success_count= 0;
156 std::for_each(StorageEngine::getSchemaEngines().begin(), StorageEngine::getSchemaEngines().end(),
157 CreateSchema(schema_message, success_count));
158
159 if (success_count)
160 {
161 TransactionServices::allocateNewTransactionId();
162 }
163
164 return (bool)success_count;
165}
166
167class DropSchema
168{
169 uint64_t &success_count;
170 const identifier::Schema &identifier;
171
172public:
173
174 DropSchema(const identifier::Schema &arg, uint64_t &count_arg) :
175 success_count(count_arg),
176 identifier(arg)
177 {
178 }
179
180 void operator() (StorageEngine *engine)
181 {
182 // @todo someday check that at least one engine said "true"
183 bool success= engine->doDropSchema(identifier);
184
185 if (success)
186 {
187 success_count++;
188 TransactionServices::allocateNewTransactionId();
189 }
190 }
191};
192
193static bool drop_all_tables_in_schema(Session& session,
194 const identifier::Schema& identifier,
195 identifier::table::vector &dropped_tables,
196 uint64_t &deleted)
197{
198 plugin::StorageEngine::getIdentifiers(session, identifier, dropped_tables);
199
200 for (identifier::table::vector::iterator it= dropped_tables.begin(); it != dropped_tables.end(); it++)
201 {
202 boost::mutex::scoped_lock scopedLock(table::Cache::mutex());
203
204 message::table::shared_ptr message= StorageEngine::getTableMessage(session, *it, false);
205 if (not message)
206 {
207 my_error(ER_TABLE_DROP, *it);
208 return false;
209 }
210
211 table::Cache::removeTable(session, *it, RTFC_WAIT_OTHER_THREAD_FLAG | RTFC_CHECK_KILLED_FLAG);
212 if (not plugin::StorageEngine::dropTable(session, *it))
213 {
214 my_error(ER_TABLE_DROP, *it);
215 return false;
216 }
217 TransactionServices::dropTable(session, *it, *message, true);
218 deleted++;
219 }
220
221 return true;
222}
223
224bool StorageEngine::dropSchema(Session& session,
225 const identifier::Schema& identifier,
226 message::schema::const_reference schema_message)
227{
228 uint64_t deleted= 0;
229 bool error= false;
230 identifier::table::vector dropped_tables;
231
232 do
233 {
234 // Remove all temp tables first, this prevents loss of table from
235 // shadowing (ie temp over standard table)
236 {
237 // Lets delete the temporary tables first outside of locks.
238 identifier::table::vector set_of_identifiers;
239 session.open_tables.doGetTableIdentifiers(identifier, set_of_identifiers);
240
241 for (identifier::table::vector::iterator iter= set_of_identifiers.begin(); iter != set_of_identifiers.end(); iter++)
242 {
243 if (session.open_tables.drop_temporary_table(*iter))
244 {
245 my_error(ER_TABLE_DROP, *iter);
246 error= true;
247 break;
248 }
249 }
250 }
251
252 /* After deleting database, remove all cache entries related to schema */
253 table::Cache::removeSchema(identifier);
254
255 if (not drop_all_tables_in_schema(session, identifier, dropped_tables, deleted))
256 {
257 error= true;
258 my_error(ER_DROP_SCHEMA, identifier);
259 break;
260 }
261
262 uint64_t counter= 0;
263 // Add hook here for engines to register schema.
264 std::for_each(StorageEngine::getSchemaEngines().begin(), StorageEngine::getSchemaEngines().end(),
265 DropSchema(identifier, counter));
266
267 if (not counter)
268 {
269 my_error(ER_DROP_SCHEMA, identifier);
270 error= true;
271
272 break;
273 }
274 else
275 {
276 /* We've already verified that the schema does exist, so safe to log it */
277 TransactionServices::dropSchema(session, identifier, schema_message);
278 }
279 } while (0);
280
281 if (deleted > 0)
282 {
283 session.clear_error();
284 session.server_status|= SERVER_STATUS_DB_DROPPED;
285 session.my_ok((uint32_t) deleted);
286 session.server_status&= ~SERVER_STATUS_DB_DROPPED;
287 }
288
289
290 return error;
291}
292
293class AlterSchema
294{
295 uint64_t &success_count;
296 const drizzled::message::Schema &schema_message;
297
298public:
299
300 AlterSchema(const drizzled::message::Schema &arg, uint64_t &count_arg) :
301 success_count(count_arg),
302 schema_message(arg)
303 {
304 }
305
306 void operator() (StorageEngine *engine)
307 {
308 // @todo eomeday check that at least one engine said "true"
309 bool success= engine->doAlterSchema(schema_message);
310
311
312 if (success)
313 {
314 success_count++;
315 }
316 }
317};
318
319bool StorageEngine::alterSchema(const drizzled::message::Schema &schema_message)
320{
321 uint64_t success_count= 0;
322
323 std::for_each(StorageEngine::getSchemaEngines().begin(), StorageEngine::getSchemaEngines().end(),
324 AlterSchema(schema_message, success_count));
325
326 if (success_count)
327 {
328 TransactionServices::allocateNewTransactionId();
329 }
330
331 return success_count ? true : false;
332}
333
334} /* namespace plugin */
335} /* namespace drizzled */
static void dropSchema(Session &, const identifier::Schema &identifier, message::schema::const_reference schema)
static void dropTable(Session &, const identifier::Table &identifier, message::table::const_reference table, bool if_exists)
static void pruneSchemaNames(const drizzled::identifier::User &user_ctx, identifier::schema::vector &set_of_schemas)
TODO: Rename this file - func.h is stupid.