1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2010 Brian Aker
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.
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.
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
22
#include <drizzled/session.h>
24
#include <drizzled/global_charset_info.h>
25
#include <drizzled/charset.h>
26
#include <drizzled/transaction_services.h>
28
#include <drizzled/plugin/storage_engine.h>
29
#include <drizzled/plugin/authorization.h>
37
class AddSchemaNames :
38
public std::unary_function<StorageEngine *, void>
40
identifier::Schema::vector &schemas;
44
AddSchemaNames(identifier::Schema::vector &of_names) :
49
result_type operator() (argument_type engine)
51
engine->doGetSchemaIdentifiers(schemas);
55
void StorageEngine::getIdentifiers(Session &session, identifier::Schema::vector &schemas)
57
// Add hook here for engines to register schema.
58
std::for_each(StorageEngine::getSchemaEngines().begin(), StorageEngine::getSchemaEngines().end(),
59
AddSchemaNames(schemas));
61
plugin::Authorization::pruneSchemaNames(*session.user(), schemas);
64
class StorageEngineGetSchemaDefinition: public std::unary_function<StorageEngine *, bool>
66
const identifier::Schema &identifier;
67
message::schema::shared_ptr &schema_proto;
70
StorageEngineGetSchemaDefinition(const identifier::Schema &identifier_arg,
71
message::schema::shared_ptr &schema_proto_arg) :
72
identifier(identifier_arg),
73
schema_proto(schema_proto_arg)
77
result_type operator() (argument_type engine)
79
schema_proto= engine->doGetSchemaDefinition(identifier);
85
Return value is "if parsed"
87
message::schema::shared_ptr StorageEngine::getSchemaDefinition(const drizzled::identifier::Table &identifier)
89
return StorageEngine::getSchemaDefinition(identifier);
92
message::schema::shared_ptr StorageEngine::getSchemaDefinition(const identifier::Schema &identifier)
94
message::schema::shared_ptr proto;
96
EngineVector::iterator iter=
97
std::find_if(StorageEngine::getSchemaEngines().begin(), StorageEngine::getSchemaEngines().end(),
98
StorageEngineGetSchemaDefinition(identifier, proto));
100
if (iter != StorageEngine::getSchemaEngines().end())
105
return message::schema::shared_ptr();
108
bool StorageEngine::doesSchemaExist(const identifier::Schema &identifier)
110
message::schema::shared_ptr proto;
112
return StorageEngine::getSchemaDefinition(identifier);
116
const CHARSET_INFO *StorageEngine::getSchemaCollation(const identifier::Schema &identifier)
118
message::schema::shared_ptr schmema_proto;
120
schmema_proto= StorageEngine::getSchemaDefinition(identifier);
122
if (schmema_proto && schmema_proto->has_collation())
124
const std::string buffer= schmema_proto->collation();
125
const CHARSET_INFO* cs= get_charset_by_name(buffer.c_str());
130
identifier.getSQLPath(path);
132
errmsg_printf(error::ERROR,
133
_("Error while loading database options: '%s':"), path.c_str());
134
errmsg_printf(error::ERROR, ER(ER_UNKNOWN_COLLATION), buffer.c_str());
136
return default_charset_info;
142
return default_charset_info;
146
public std::unary_function<StorageEngine *, void>
148
const drizzled::message::Schema &schema_message;
149
uint64_t &success_count;
153
CreateSchema(const drizzled::message::Schema &arg, uint64_t &success_count_arg) :
155
success_count(success_count_arg)
159
result_type operator() (argument_type engine)
161
// @todo eomeday check that at least one engine said "true"
162
bool success= engine->doCreateSchema(schema_message);
167
TransactionServices &transaction_services= TransactionServices::singleton();
168
transaction_services.allocateNewTransactionId();
173
bool StorageEngine::createSchema(const drizzled::message::Schema &schema_message)
175
// Add hook here for engines to register schema.
176
uint64_t success_count= 0;
177
std::for_each(StorageEngine::getSchemaEngines().begin(), StorageEngine::getSchemaEngines().end(),
178
CreateSchema(schema_message, success_count));
182
TransactionServices &transaction_services= TransactionServices::singleton();
183
transaction_services.allocateNewTransactionId();
186
return (bool)success_count;
190
public std::unary_function<StorageEngine *, void>
192
uint64_t &success_count;
193
const identifier::Schema &identifier;
197
DropSchema(const identifier::Schema &arg, uint64_t &count_arg) :
198
success_count(count_arg),
203
result_type operator() (argument_type engine)
205
// @todo someday check that at least one engine said "true"
206
bool success= engine->doDropSchema(identifier);
211
TransactionServices &transaction_services= TransactionServices::singleton();
212
transaction_services.allocateNewTransactionId();
217
static bool drop_all_tables_in_schema(Session& session,
218
identifier::Schema::const_reference identifier,
219
identifier::Table::vector &dropped_tables,
222
TransactionServices &transaction_services= TransactionServices::singleton();
224
plugin::StorageEngine::getIdentifiers(session, identifier, dropped_tables);
226
for (identifier::Table::vector::iterator it= dropped_tables.begin();
227
it != dropped_tables.end();
230
boost::mutex::scoped_lock scopedLock(table::Cache::singleton().mutex());
232
message::table::shared_ptr message= StorageEngine::getTableMessage(session, *it, false);
235
my_error(ER_TABLE_DROP, *it);
239
table::Cache::singleton().removeTable(&session, *it,
240
RTFC_WAIT_OTHER_THREAD_FLAG |
241
RTFC_CHECK_KILLED_FLAG);
242
if (not plugin::StorageEngine::dropTable(session, *it))
244
my_error(ER_TABLE_DROP, *it);
247
transaction_services.dropTable(session, *it, *message, true);
254
bool StorageEngine::dropSchema(Session::reference session,
255
identifier::Schema::const_reference identifier,
256
message::schema::const_reference schema_message)
260
identifier::Table::vector dropped_tables;
264
// Remove all temp tables first, this prevents loss of table from
265
// shadowing (ie temp over standard table)
267
// Lets delete the temporary tables first outside of locks.
268
identifier::Table::vector set_of_identifiers;
269
session.doGetTableIdentifiers(identifier, set_of_identifiers);
271
for (identifier::Table::vector::iterator iter= set_of_identifiers.begin(); iter != set_of_identifiers.end(); iter++)
273
if (session.drop_temporary_table(*iter))
275
my_error(ER_TABLE_DROP, *iter);
282
/* After deleting database, remove all cache entries related to schema */
283
table::Cache::singleton().removeSchema(identifier);
285
if (not drop_all_tables_in_schema(session, identifier, dropped_tables, deleted))
288
my_error(ER_DROP_SCHEMA, identifier);
293
// Add hook here for engines to register schema.
294
std::for_each(StorageEngine::getSchemaEngines().begin(), StorageEngine::getSchemaEngines().end(),
295
DropSchema(identifier, counter));
299
my_error(ER_DROP_SCHEMA, identifier);
306
/* We've already verified that the schema does exist, so safe to log it */
307
TransactionServices &transaction_services= TransactionServices::singleton();
308
transaction_services.dropSchema(session, identifier, schema_message);
314
session.clear_error();
315
session.server_status|= SERVER_STATUS_DB_DROPPED;
316
session.my_ok((uint32_t) deleted);
317
session.server_status&= ~SERVER_STATUS_DB_DROPPED;
325
public std::unary_function<StorageEngine *, void>
327
uint64_t &success_count;
328
const drizzled::message::Schema &schema_message;
332
AlterSchema(const drizzled::message::Schema &arg, uint64_t &count_arg) :
333
success_count(count_arg),
338
result_type operator() (argument_type engine)
340
// @todo eomeday check that at least one engine said "true"
341
bool success= engine->doAlterSchema(schema_message);
351
bool StorageEngine::alterSchema(const drizzled::message::Schema &schema_message)
353
uint64_t success_count= 0;
355
std::for_each(StorageEngine::getSchemaEngines().begin(), StorageEngine::getSchemaEngines().end(),
356
AlterSchema(schema_message, success_count));
360
TransactionServices &transaction_services= TransactionServices::singleton();
361
transaction_services.allocateNewTransactionId();
364
return success_count ? true : false;
367
} /* namespace plugin */
368
} /* namespace drizzled */