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"
39
35
#include <sys/stat.h>
73
70
CachedDirectory directory(getDataHomeCatalog().file_string(), CachedDirectory::DIRECTORY);
74
71
CachedDirectory::Entries files= directory.getEntries();
75
boost::unique_lock<boost::shared_mutex> scopedLock(mutex);
77
75
for (CachedDirectory::Entries::iterator fileIter= files.begin();
78
76
fileIter != files.end(); fileIter++)
83
81
if (not entry->filename.compare(GLOBAL_TEMPORARY_EXT))
86
if (readSchemaFile(entry->filename, schema_message))
84
SchemaIdentifier filename(entry->filename);
85
if (readSchemaFile(filename, schema_message))
88
identifier::Schema schema_identifier(schema_message.name());
87
SchemaIdentifier schema_identifier(schema_message.name());
90
89
pair<SchemaCache::iterator, bool> ret=
91
schema_cache.insert(make_pair(schema_identifier.getPath(), new message::Schema(schema_message)));
90
schema_cache.insert(make_pair(schema_identifier.getPath(), schema_message));
93
92
if (ret.second == false)
95
94
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)
101
void Schema::doGetSchemaIdentifiers(SchemaIdentifiers &set_of_names)
107
103
mutex.lock_shared();
110
106
iter != schema_cache.end();
113
set_of_names.push_back(identifier::Schema((*iter).second->name()));
109
set_of_names.push_back(SchemaIdentifier((*iter).second.name()));
116
112
mutex.unlock_shared();
119
drizzled::message::schema::shared_ptr Schema::doGetSchemaDefinition(const identifier::Schema &schema_identifier)
115
bool Schema::doGetSchemaDefinition(const SchemaIdentifier &schema_identifier, message::Schema &schema_message)
121
117
mutex.lock_shared();
122
118
SchemaCache::iterator iter= schema_cache.find(schema_identifier.getPath());
124
120
if (iter != schema_cache.end())
126
drizzled::message::schema::shared_ptr schema_message;
127
schema_message= (*iter).second;
122
schema_message.CopyFrom(((*iter).second));
128
123
mutex.unlock_shared();
130
return schema_message;
132
126
mutex.unlock_shared();
134
return drizzled::message::schema::shared_ptr();
138
132
bool Schema::doCreateSchema(const drizzled::message::Schema &schema_message)
140
identifier::Schema schema_identifier(schema_message.name());
134
SchemaIdentifier schema_identifier(schema_message.name());
142
136
if (mkdir(schema_identifier.getPath().c_str(), 0777) == -1)
144
sql_perror(schema_identifier.getPath().c_str());
148
139
if (not writeSchemaFile(schema_identifier, schema_message))
163
154
abort(); // If this has happened, something really bad is going down.
170
bool Schema::doDropSchema(const identifier::Schema &schema_identifier)
162
bool Schema::doDropSchema(const SchemaIdentifier &schema_identifier)
164
message::Schema schema_message;
172
166
string schema_file(schema_identifier.getPath());
173
167
schema_file.append(1, FN_LIBCHAR);
174
168
schema_file.append(MY_DB_OPT_FILE);
176
if (not doGetSchemaDefinition(schema_identifier))
170
if (not doGetSchemaDefinition(schema_identifier, schema_message))
179
173
// No db.opt file, no love from us.
180
174
if (access(schema_file.c_str(), F_OK))
182
sql_perror(schema_file.c_str());
176
perror(schema_file.c_str());
186
180
if (unlink(schema_file.c_str()))
188
sql_perror(schema_file.c_str());
182
perror(schema_file.c_str());
192
186
if (rmdir(schema_identifier.getPath().c_str()))
194
sql_perror(schema_identifier.getPath().c_str());
188
perror(schema_identifier.getPath().c_str());
195
189
//@todo If this happens, we want a report of it. For the moment I dump
196
190
//to stderr so I can catch it in Hudson.
197
191
CachedDirectory dir(schema_identifier.getPath());
201
boost::unique_lock<boost::shared_mutex> scopedLock(mutex);
202
196
schema_cache.erase(schema_identifier.getPath());
207
202
bool Schema::doAlterSchema(const drizzled::message::Schema &schema_message)
209
identifier::Schema schema_identifier(schema_message.name());
204
SchemaIdentifier schema_identifier(schema_message.name());
211
206
if (access(schema_identifier.getPath().c_str(), F_OK))
214
209
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.
213
schema_cache.erase(schema_identifier.getPath());
215
pair<SchemaCache::iterator, bool> ret=
216
schema_cache.insert(make_pair(schema_identifier.getPath(), schema_message));
218
if (ret.second == false)
220
abort(); // If this has happened, something really bad is going down.
234
232
@note we do the rename to make it crash safe.
236
bool Schema::writeSchemaFile(const identifier::Schema &schema_identifier, const message::Schema &db)
234
bool Schema::writeSchemaFile(const SchemaIdentifier &schema_identifier, const message::Schema &db)
238
236
char schema_file_tmp[FN_REFLEN];
239
237
string schema_file(schema_identifier.getPath());
269
267
db.InitializationErrorString().empty() ? "unknown" : db.InitializationErrorString().c_str());
271
269
if (close(fd) == -1)
272
sql_perror(schema_file_tmp);
270
perror(schema_file_tmp);
274
272
if (unlink(schema_file_tmp))
275
sql_perror(schema_file_tmp);
273
perror(schema_file_tmp);
280
278
if (close(fd) == -1)
282
sql_perror(schema_file_tmp);
280
perror(schema_file_tmp);
284
282
if (unlink(schema_file_tmp))
285
sql_perror(schema_file_tmp);
283
perror(schema_file_tmp);
302
bool Schema::readSchemaFile(const drizzled::identifier::Schema &schema_identifier, drizzled::message::Schema &schema)
300
bool Schema::readSchemaFile(const drizzled::SchemaIdentifier &schema_identifier, drizzled::message::Schema &schema)
304
return readSchemaFile(schema_identifier.getPath(), schema);
302
string db_opt_path(schema_identifier.getPath());
307
bool Schema::readSchemaFile(std::string db_opt_path, drizzled::message::Schema &schema)
310
305
Pass an empty file name, and the database options file name as extension
311
306
to avoid table name to file name encoding.
335
sql_perror(db_opt_path.c_str());
330
perror(db_opt_path.c_str());
336
bool Schema::doCanCreateTable(const drizzled::TableIdentifier &identifier)
339
// This should always be the same value as GLOBAL_TEMPORARY_EXT but be
342
// This needs to be done static in here for ordering reasons
343
static SchemaIdentifier TEMPORARY_IDENTIFIER(".TEMPORARY");
344
if (static_cast<const SchemaIdentifier&>(identifier) == TEMPORARY_IDENTIFIER)
341
352
void Schema::doGetTableIdentifiers(drizzled::CachedDirectory&,
342
const drizzled::identifier::Schema&,
343
drizzled::identifier::Table::vector&)
353
const drizzled::SchemaIdentifier&,
354
drizzled::TableIdentifiers&)