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; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23
#include "drizzled/plugin/catalog.h"
24
#include "drizzled/catalog/cache.h"
25
#include "drizzled/error.h"
27
#include <boost/foreach.hpp>
34
// Private container we use for holding the instances of engines passed to
35
// use from the catalog plugins.
37
catalog::Engine::vector _catalogs;
40
static Engines& singleton()
46
catalog::Engine::vector &catalogs()
55
const identifier::Catalog &identifier;
56
catalog::error_t error;
59
LockErase(const identifier::Catalog &identifier_arg) :
61
identifier(identifier_arg)
75
if (not catalog::Cache::singleton().unlock(identifier, error))
77
catalog::error(error, identifier);
86
// We insert a lock into the cache, if this fails we bail.
87
if (not catalog::Cache::singleton().lock(identifier, error))
100
const identifier::Catalog &identifier;
101
catalog::error_t error;
104
CreateLock(const identifier::Catalog &identifier_arg) :
106
identifier(identifier_arg)
120
if (not catalog::Cache::singleton().unlock(identifier, error))
122
catalog::error(error, identifier);
132
// We insert a lock into the cache, if this fails we bail.
133
if (not catalog::Cache::singleton().lock(identifier, error))
147
bool Catalog::create(const identifier::Catalog &identifier)
149
message::catalog::shared_ptr message= message::catalog::make_shared(identifier);
150
return create(identifier, message);
153
bool Catalog::create(const identifier::Catalog &identifier, message::catalog::shared_ptr &message)
157
CreateLock lock(identifier);
159
if (not lock.locked())
161
my_error(ER_CATALOG_NO_LOCK, MYF(0), identifier.getName().c_str());
165
BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
167
if (ref->create(identifier, message))
176
bool Catalog::drop(const identifier::Catalog &identifier)
178
static drizzled::identifier::Catalog LOCAL_IDENTIFIER("local");
179
if (identifier == LOCAL_IDENTIFIER)
181
my_error(drizzled::ER_CATALOG_NO_DROP_LOCAL, MYF(0));
185
LockErase lock(identifier);
186
if (not lock.locked())
188
my_error(ER_CATALOG_NO_LOCK, MYF(0), identifier.getName().c_str());
193
BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
195
if (not ref->drop(identifier))
204
bool Catalog::lock(const identifier::Catalog &identifier)
206
catalog::error_t error;
208
// We insert a lock into the cache, if this fails we bail.
209
if (not catalog::Cache::singleton().lock(identifier, error))
211
catalog::error(error, identifier);
220
bool Catalog::unlock(const identifier::Catalog &identifier)
222
catalog::error_t error;
223
if (not catalog::Cache::singleton().unlock(identifier, error))
225
catalog::error(error, identifier);
231
bool plugin::Catalog::addPlugin(plugin::Catalog *arg)
233
Engines::singleton().catalogs().push_back(arg->engine());
238
bool plugin::Catalog::exist(const identifier::Catalog &identifier)
240
if (catalog::Cache::singleton().exist(identifier))
243
BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
245
if (ref->exist(identifier))
252
void plugin::Catalog::getIdentifiers(identifier::Catalog::vector &identifiers)
254
BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
256
ref->getIdentifiers(identifiers);
260
void plugin::Catalog::getMessages(message::catalog::vector &messages)
262
BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
264
ref->getMessages(messages);
268
bool plugin::Catalog::getMessage(const identifier::Catalog &identifier, message::catalog::shared_ptr &message)
270
catalog::error_t error;
271
catalog::Instance::shared_ptr instance= catalog::Cache::singleton().find(identifier, error);
273
if (instance and instance->message())
275
message= instance->message();
281
BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
283
if (ref->getMessage(identifier, message))
290
bool plugin::Catalog::getInstance(const identifier::Catalog &identifier, catalog::Instance::shared_ptr &instance)
292
catalog::error_t error;
293
instance= catalog::Cache::singleton().find(identifier, error);
298
BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
300
if (ref->getInstance(identifier, instance))
304
// If this should fail, we are in a world of pain.
305
catalog::Cache::singleton().insert(identifier, instance, error);
311
void plugin::Catalog::removePlugin(plugin::Catalog *arg)
313
Engines::singleton().catalogs().erase(find(Engines::singleton().catalogs().begin(),
314
Engines::singleton().catalogs().end(),
318
} /* namespace plugin */
319
} /* namespace drizzled */