29
29
#include "drizzled/cursor.h"
30
30
#include "drizzled/data_home.h"
32
#include <drizzled/pthread_globals.h>
34
#include <drizzled/execute.h>
36
32
#include "drizzled/internal/my_sys.h"
33
#include "drizzled/transaction_services.h"
39
36
#include <sys/stat.h>
73
71
CachedDirectory directory(getDataHomeCatalog().file_string(), CachedDirectory::DIRECTORY);
74
72
CachedDirectory::Entries files= directory.getEntries();
75
boost::unique_lock<boost::shared_mutex> scopedLock(mutex);
77
76
for (CachedDirectory::Entries::iterator fileIter= files.begin();
78
77
fileIter != files.end(); fileIter++)
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(SchemaIdentifier::vector &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
bool Schema::doGetSchemaDefinition(const identifier::Schema &schema_identifier, message::schema::shared_ptr &schema_message)
116
bool Schema::doGetSchemaDefinition(const SchemaIdentifier &schema_identifier, message::schema::shared_ptr &schema_message)
121
118
mutex.lock_shared();
122
119
SchemaCache::iterator iter= schema_cache.find(schema_identifier.getPath());
137
133
bool Schema::doCreateSchema(const drizzled::message::Schema &schema_message)
139
identifier::Schema schema_identifier(schema_message.name());
135
SchemaIdentifier schema_identifier(schema_message.name());
141
137
if (mkdir(schema_identifier.getPath().c_str(), 0777) == -1)
143
sql_perror(schema_identifier.getPath().c_str());
147
140
if (not writeSchemaFile(schema_identifier, schema_message))
162
155
abort(); // If this has happened, something really bad is going down.
160
TransactionServices &transaction_services= TransactionServices::singleton();
161
transaction_services.allocateNewTransactionId();
169
bool Schema::doDropSchema(const identifier::Schema &schema_identifier)
166
bool Schema::doDropSchema(const SchemaIdentifier &schema_identifier)
171
168
message::schema::shared_ptr schema_message;
180
177
// No db.opt file, no love from us.
181
178
if (access(schema_file.c_str(), F_OK))
183
sql_perror(schema_file.c_str());
180
perror(schema_file.c_str());
187
184
if (unlink(schema_file.c_str()))
189
sql_perror(schema_file.c_str());
186
perror(schema_file.c_str());
193
190
if (rmdir(schema_identifier.getPath().c_str()))
195
sql_perror(schema_identifier.getPath().c_str());
192
perror(schema_identifier.getPath().c_str());
196
193
//@todo If this happens, we want a report of it. For the moment I dump
197
194
//to stderr so I can catch it in Hudson.
198
195
CachedDirectory dir(schema_identifier.getPath());
202
boost::unique_lock<boost::shared_mutex> scopedLock(mutex);
203
200
schema_cache.erase(schema_identifier.getPath());
203
TransactionServices &transaction_services= TransactionServices::singleton();
204
transaction_services.allocateNewTransactionId();
208
209
bool Schema::doAlterSchema(const drizzled::message::Schema &schema_message)
210
identifier::Schema schema_identifier(schema_message.name());
211
SchemaIdentifier schema_identifier(schema_message.name());
212
213
if (access(schema_identifier.getPath().c_str(), F_OK))
215
216
if (writeSchemaFile(schema_identifier, schema_message))
217
boost::unique_lock<boost::shared_mutex> scopedLock(mutex);
218
schema_cache.erase(schema_identifier.getPath());
220
pair<SchemaCache::iterator, bool> ret=
221
schema_cache.insert(make_pair(schema_identifier.getPath(), new message::Schema(schema_message)));
223
if (ret.second == false)
225
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();
235
242
@note we do the rename to make it crash safe.
237
bool Schema::writeSchemaFile(const identifier::Schema &schema_identifier, const message::Schema &db)
244
bool Schema::writeSchemaFile(const SchemaIdentifier &schema_identifier, const message::Schema &db)
239
246
char schema_file_tmp[FN_REFLEN];
240
247
string schema_file(schema_identifier.getPath());
270
277
db.InitializationErrorString().empty() ? "unknown" : db.InitializationErrorString().c_str());
272
279
if (close(fd) == -1)
273
sql_perror(schema_file_tmp);
280
perror(schema_file_tmp);
275
282
if (unlink(schema_file_tmp))
276
sql_perror(schema_file_tmp);
283
perror(schema_file_tmp);
281
288
if (close(fd) == -1)
283
sql_perror(schema_file_tmp);
290
perror(schema_file_tmp);
285
292
if (unlink(schema_file_tmp))
286
sql_perror(schema_file_tmp);
293
perror(schema_file_tmp);
303
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)
305
return readSchemaFile(schema_identifier.getPath(), schema);
312
string db_opt_path(schema_identifier.getPath());
308
bool Schema::readSchemaFile(std::string db_opt_path, drizzled::message::Schema &schema)
311
315
Pass an empty file name, and the database options file name as extension
312
316
to avoid table name to file name encoding.
336
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)
342
362
void Schema::doGetTableIdentifiers(drizzled::CachedDirectory&,
343
const drizzled::identifier::Schema&,
344
drizzled::identifier::Table::vector&)
363
const drizzled::SchemaIdentifier&,
364
drizzled::TableIdentifier::vector&)