18
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23
#include <plugin/schema_engine/schema.h>
24
#include <drizzled/schema.h>
25
#include <drizzled/sql_table.h>
26
#include <drizzled/global_charset_info.h>
27
#include <drizzled/charset.h>
28
#include <drizzled/charset_info.h>
29
#include <drizzled/cursor.h>
30
#include <drizzled/data_home.h>
32
#include <drizzled/pthread_globals.h>
34
#include <drizzled/execute.h>
36
#include <drizzled/internal/my_sys.h>
23
#include "plugin/schema_engine/schema.h"
24
#include "drizzled/db.h"
25
#include "drizzled/sql_table.h"
26
#include "drizzled/global_charset_info.h"
27
#include "drizzled/charset.h"
28
#include "drizzled/charset_info.h"
29
#include "drizzled/cursor.h"
30
#include "drizzled/data_home.h"
32
#include "drizzled/internal/my_sys.h"
33
#include "drizzled/transaction_services.h"
39
36
#include <sys/stat.h>
83
82
if (not entry->filename.compare(GLOBAL_TEMPORARY_EXT))
86
if (readSchemaFile(entry->filename, schema_message))
85
SchemaIdentifier filename(entry->filename);
86
if (readSchemaFile(filename, schema_message))
88
identifier::Schema schema_identifier(schema_message.name());
88
SchemaIdentifier schema_identifier(schema_message.name());
90
90
pair<SchemaCache::iterator, bool> ret=
91
91
schema_cache.insert(make_pair(schema_identifier.getPath(), new message::Schema(schema_message)));
93
93
if (ret.second == false)
95
95
abort(); // If this has happened, something really bad is going down.
101
void Schema::startup(drizzled::Session &)
105
void Schema::doGetSchemaIdentifiers(identifier::Schema::vector &set_of_names)
102
void Schema::doGetSchemaIdentifiers(SchemaIdentifiers &set_of_names)
107
104
mutex.lock_shared();
110
107
iter != schema_cache.end();
113
set_of_names.push_back(identifier::Schema((*iter).second->name()));
110
set_of_names.push_back(SchemaIdentifier((*iter).second->name()));
116
113
mutex.unlock_shared();
119
drizzled::message::schema::shared_ptr Schema::doGetSchemaDefinition(const identifier::Schema &schema_identifier)
116
bool Schema::doGetSchemaDefinition(const SchemaIdentifier &schema_identifier, message::SchemaPtr &schema_message)
121
118
mutex.lock_shared();
122
119
SchemaCache::iterator iter= schema_cache.find(schema_identifier.getPath());
124
121
if (iter != schema_cache.end())
126
drizzled::message::schema::shared_ptr schema_message;
127
123
schema_message= (*iter).second;
128
124
mutex.unlock_shared();
130
return schema_message;
132
127
mutex.unlock_shared();
134
return drizzled::message::schema::shared_ptr();
138
133
bool Schema::doCreateSchema(const drizzled::message::Schema &schema_message)
140
identifier::Schema schema_identifier(schema_message.name());
135
SchemaIdentifier schema_identifier(schema_message.name());
142
137
if (mkdir(schema_identifier.getPath().c_str(), 0777) == -1)
144
sql_perror(schema_identifier.getPath().c_str());
148
140
if (not writeSchemaFile(schema_identifier, schema_message))
163
155
abort(); // If this has happened, something really bad is going down.
160
TransactionServices &transaction_services= TransactionServices::singleton();
161
transaction_services.allocateNewTransactionId();
170
bool Schema::doDropSchema(const identifier::Schema &schema_identifier)
166
bool Schema::doDropSchema(const SchemaIdentifier &schema_identifier)
168
message::SchemaPtr schema_message;
172
170
string schema_file(schema_identifier.getPath());
173
171
schema_file.append(1, FN_LIBCHAR);
174
172
schema_file.append(MY_DB_OPT_FILE);
176
if (not doGetSchemaDefinition(schema_identifier))
174
if (not doGetSchemaDefinition(schema_identifier, schema_message))
179
177
// No db.opt file, no love from us.
180
178
if (access(schema_file.c_str(), F_OK))
182
sql_perror(schema_file.c_str());
180
perror(schema_file.c_str());
186
184
if (unlink(schema_file.c_str()))
188
sql_perror(schema_file.c_str());
186
perror(schema_file.c_str());
192
190
if (rmdir(schema_identifier.getPath().c_str()))
194
sql_perror(schema_identifier.getPath().c_str());
192
perror(schema_identifier.getPath().c_str());
195
193
//@todo If this happens, we want a report of it. For the moment I dump
196
194
//to stderr so I can catch it in Hudson.
197
195
CachedDirectory dir(schema_identifier.getPath());
201
boost::unique_lock<boost::shared_mutex> scopedLock(mutex);
202
200
schema_cache.erase(schema_identifier.getPath());
203
TransactionServices &transaction_services= TransactionServices::singleton();
204
transaction_services.allocateNewTransactionId();
207
209
bool Schema::doAlterSchema(const drizzled::message::Schema &schema_message)
209
identifier::Schema schema_identifier(schema_message.name());
211
SchemaIdentifier schema_identifier(schema_message.name());
211
213
if (access(schema_identifier.getPath().c_str(), F_OK))
214
216
if (writeSchemaFile(schema_identifier, schema_message))
216
boost::unique_lock<boost::shared_mutex> scopedLock(mutex);
217
schema_cache.erase(schema_identifier.getPath());
219
pair<SchemaCache::iterator, bool> ret=
220
schema_cache.insert(make_pair(schema_identifier.getPath(), new message::Schema(schema_message)));
222
if (ret.second == false)
224
abort(); // If this has happened, something really bad is going down.
220
schema_cache.erase(schema_identifier.getPath());
222
pair<SchemaCache::iterator, bool> ret=
223
schema_cache.insert(make_pair(schema_identifier.getPath(), new message::Schema(schema_message)));
225
if (ret.second == false)
227
abort(); // If this has happened, something really bad is going down.
232
TransactionServices &transaction_services= TransactionServices::singleton();
233
transaction_services.allocateNewTransactionId();
302
bool Schema::readSchemaFile(const drizzled::identifier::Schema &schema_identifier, drizzled::message::Schema &schema)
310
bool Schema::readSchemaFile(const drizzled::SchemaIdentifier &schema_identifier, drizzled::message::Schema &schema)
304
return readSchemaFile(schema_identifier.getPath(), schema);
312
string db_opt_path(schema_identifier.getPath());
307
bool Schema::readSchemaFile(std::string db_opt_path, drizzled::message::Schema &schema)
310
315
Pass an empty file name, and the database options file name as extension
311
316
to avoid table name to file name encoding.
335
sql_perror(db_opt_path.c_str());
340
perror(db_opt_path.c_str());
346
bool Schema::doCanCreateTable(const drizzled::TableIdentifier &identifier)
349
// This should always be the same value as GLOBAL_TEMPORARY_EXT but be
352
// This needs to be done static in here for ordering reasons
353
static SchemaIdentifier TEMPORARY_IDENTIFIER(".TEMPORARY");
354
if (static_cast<const SchemaIdentifier&>(identifier) == TEMPORARY_IDENTIFIER)
341
362
void Schema::doGetTableIdentifiers(drizzled::CachedDirectory&,
342
const drizzled::identifier::Schema&,
343
drizzled::identifier::Table::vector&)
363
const drizzled::SchemaIdentifier&,
364
drizzled::TableIdentifiers&)