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/catalog/local.h>
26
#include <drizzled/error.h>
28
#include <boost/foreach.hpp>
35
// Private container we use for holding the instances of engines passed to
36
// use from the catalog plugins.
38
catalog::Engine::vector _catalogs;
41
static Engines& singleton()
47
catalog::Engine::vector &catalogs()
57
bool Catalog::create(const identifier::Catalog& identifier)
59
message::catalog::shared_ptr message= message::catalog::make_shared(identifier);
60
return create(identifier, message);
63
bool Catalog::create(const identifier::Catalog& identifier, message::catalog::shared_ptr &message)
67
catalog::lock::Create lock(identifier);
69
if (not lock.locked())
71
my_error(ER_CATALOG_NO_LOCK, MYF(0), identifier.getName().c_str());
75
size_t create_count= 0;
76
BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
78
if (ref->create(identifier, message))
81
assert(create_count < 2);
85
my_error(ER_CATALOG_CANNOT_CREATE, MYF(0), identifier.getName().c_str());
92
bool Catalog::drop(const identifier::Catalog& identifier)
94
if (identifier == drizzled::catalog::local_identifier())
96
my_error(drizzled::ER_CATALOG_NO_DROP_LOCAL, MYF(0));
100
catalog::lock::Erase lock(identifier);
101
if (not lock.locked())
103
my_error(ER_CATALOG_NO_LOCK, MYF(0), identifier.getName().c_str());
108
size_t drop_count= 0;
109
BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
111
if (ref->drop(identifier))
114
assert(drop_count < 2);
118
my_error(ER_CATALOG_DOES_NOT_EXIST, MYF(0), identifier.getName().c_str());
125
bool Catalog::lock(const identifier::Catalog& identifier)
127
drizzled::error_t error;
129
// We insert a lock into the cache, if this fails we bail.
130
if (not catalog::Cache::lock(identifier, error))
132
my_error(error, identifier);
141
bool Catalog::unlock(const identifier::Catalog& identifier)
143
drizzled::error_t error;
144
if (not catalog::Cache::unlock(identifier, error))
146
my_error(error, identifier);
152
bool plugin::Catalog::addPlugin(plugin::Catalog *arg)
154
Engines::singleton().catalogs().push_back(arg->engine());
159
bool plugin::Catalog::exist(const identifier::Catalog& identifier)
161
if (catalog::Cache::exist(identifier))
164
BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
166
if (ref->exist(identifier))
173
void plugin::Catalog::getIdentifiers(identifier::catalog::vector &identifiers)
175
BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
177
ref->getIdentifiers(identifiers);
181
void plugin::Catalog::getMessages(message::catalog::vector &messages)
183
BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
185
ref->getMessages(messages);
189
message::catalog::shared_ptr plugin::Catalog::getMessage(const identifier::Catalog& identifier)
191
drizzled::error_t error;
192
catalog::Instance::shared_ptr instance= catalog::Cache::find(identifier, error);
193
message::catalog::shared_ptr message;
195
if (instance and instance->message())
197
return instance->message();
200
BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
202
if ((message= ref->getMessage(identifier)))
209
catalog::Instance::shared_ptr plugin::Catalog::getInstance(const identifier::Catalog& identifier)
211
drizzled::error_t error;
212
catalog::Instance::shared_ptr instance= catalog::Cache::find(identifier, error);
217
BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
219
message::catalog::shared_ptr message;
220
if (message= ref->getMessage(identifier))
222
instance= catalog::Instance::make_shared(message);
223
// If this should fail inserting into the cache, we are in a world of
225
catalog::Cache::insert(identifier, instance, error);
231
return catalog::Instance::shared_ptr();
235
void plugin::Catalog::removePlugin(plugin::Catalog *)
239
} /* namespace plugin */
240
} /* namespace drizzled */