18
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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/session.h"
34
#include "drizzled/transaction_services.h"
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>
37
39
#include <sys/stat.h>
83
83
if (not entry->filename.compare(GLOBAL_TEMPORARY_EXT))
86
SchemaIdentifier filename(entry->filename);
87
if (readSchemaFile(filename, schema_message))
86
if (readSchemaFile(entry->filename, schema_message))
89
SchemaIdentifier schema_identifier(schema_message.name());
88
identifier::Schema schema_identifier(schema_message.name());
91
90
pair<SchemaCache::iterator, bool> ret=
92
91
schema_cache.insert(make_pair(schema_identifier.getPath(), new message::Schema(schema_message)));
94
93
if (ret.second == false)
96
95
abort(); // If this has happened, something really bad is going down.
103
void Schema::doGetSchemaIdentifiers(SchemaIdentifiers &set_of_names)
101
void Schema::startup(drizzled::Session &)
105
void Schema::doGetSchemaIdentifiers(identifier::Schema::vector &set_of_names)
105
107
mutex.lock_shared();
108
110
iter != schema_cache.end();
111
set_of_names.push_back(SchemaIdentifier((*iter).second->name()));
113
set_of_names.push_back(identifier::Schema((*iter).second->name()));
114
116
mutex.unlock_shared();
117
bool Schema::doGetSchemaDefinition(const SchemaIdentifier &schema_identifier, message::SchemaPtr &schema_message)
119
drizzled::message::schema::shared_ptr Schema::doGetSchemaDefinition(const identifier::Schema &schema_identifier)
119
121
mutex.lock_shared();
120
122
SchemaCache::iterator iter= schema_cache.find(schema_identifier.getPath());
122
124
if (iter != schema_cache.end())
126
drizzled::message::schema::shared_ptr schema_message;
124
127
schema_message= (*iter).second;
125
128
mutex.unlock_shared();
130
return schema_message;
128
132
mutex.unlock_shared();
134
return drizzled::message::schema::shared_ptr();
134
bool Schema::doCreateSchema(const drizzled::message::Schema &schema_message, drizzled::Session &session)
138
bool Schema::doCreateSchema(const drizzled::message::Schema &schema_message)
136
SchemaIdentifier schema_identifier(schema_message.name());
140
identifier::Schema schema_identifier(schema_message.name());
138
142
if (mkdir(schema_identifier.getPath().c_str(), 0777) == -1)
144
sql_perror(schema_identifier.getPath().c_str());
141
148
if (not writeSchemaFile(schema_identifier, schema_message))
156
163
abort(); // If this has happened, something really bad is going down.
161
TransactionServices &transaction_services= TransactionServices::singleton();
162
transaction_services.allocateNewTransactionId(&session);
167
bool Schema::doDropSchema(const SchemaIdentifier &schema_identifier)
170
bool Schema::doDropSchema(const identifier::Schema &schema_identifier)
169
message::SchemaPtr schema_message;
171
172
string schema_file(schema_identifier.getPath());
172
173
schema_file.append(1, FN_LIBCHAR);
173
174
schema_file.append(MY_DB_OPT_FILE);
175
if (not doGetSchemaDefinition(schema_identifier, schema_message))
176
if (not doGetSchemaDefinition(schema_identifier))
178
179
// No db.opt file, no love from us.
179
180
if (access(schema_file.c_str(), F_OK))
181
perror(schema_file.c_str());
182
sql_perror(schema_file.c_str());
185
186
if (unlink(schema_file.c_str()))
187
perror(schema_file.c_str());
188
sql_perror(schema_file.c_str());
191
192
if (rmdir(schema_identifier.getPath().c_str()))
193
perror(schema_identifier.getPath().c_str());
194
sql_perror(schema_identifier.getPath().c_str());
194
195
//@todo If this happens, we want a report of it. For the moment I dump
195
196
//to stderr so I can catch it in Hudson.
196
197
CachedDirectory dir(schema_identifier.getPath());
201
boost::unique_lock<boost::shared_mutex> scopedLock(mutex);
201
202
schema_cache.erase(schema_identifier.getPath());
207
207
bool Schema::doAlterSchema(const drizzled::message::Schema &schema_message)
209
SchemaIdentifier schema_identifier(schema_message.name());
209
identifier::Schema schema_identifier(schema_message.name());
211
211
if (access(schema_identifier.getPath().c_str(), F_OK))
214
214
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)
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.
224
abort(); // If this has happened, something really bad is going down.
237
234
@note we do the rename to make it crash safe.
239
bool Schema::writeSchemaFile(const SchemaIdentifier &schema_identifier, const message::Schema &db)
236
bool Schema::writeSchemaFile(const identifier::Schema &schema_identifier, const message::Schema &db)
241
238
char schema_file_tmp[FN_REFLEN];
242
239
string schema_file(schema_identifier.getPath());
272
269
db.InitializationErrorString().empty() ? "unknown" : db.InitializationErrorString().c_str());
274
271
if (close(fd) == -1)
275
perror(schema_file_tmp);
272
sql_perror(schema_file_tmp);
277
274
if (unlink(schema_file_tmp))
278
perror(schema_file_tmp);
275
sql_perror(schema_file_tmp);
283
280
if (close(fd) == -1)
285
perror(schema_file_tmp);
282
sql_perror(schema_file_tmp);
287
284
if (unlink(schema_file_tmp))
288
perror(schema_file_tmp);
285
sql_perror(schema_file_tmp);
305
bool Schema::readSchemaFile(const drizzled::SchemaIdentifier &schema_identifier, drizzled::message::Schema &schema)
302
bool Schema::readSchemaFile(const drizzled::identifier::Schema &schema_identifier, drizzled::message::Schema &schema)
307
string db_opt_path(schema_identifier.getPath());
304
return readSchemaFile(schema_identifier.getPath(), schema);
307
bool Schema::readSchemaFile(std::string db_opt_path, drizzled::message::Schema &schema)
310
310
Pass an empty file name, and the database options file name as extension
311
311
to avoid table name to file name encoding.
335
perror(db_opt_path.c_str());
335
sql_perror(db_opt_path.c_str());
341
bool Schema::doCanCreateTable(const drizzled::TableIdentifier &identifier)
344
// This should always be the same value as GLOBAL_TEMPORARY_EXT but be
347
// This needs to be done static in here for ordering reasons
348
static SchemaIdentifier TEMPORARY_IDENTIFIER(".TEMPORARY");
349
if (static_cast<const SchemaIdentifier&>(identifier) == TEMPORARY_IDENTIFIER)
357
341
void Schema::doGetTableIdentifiers(drizzled::CachedDirectory&,
358
const drizzled::SchemaIdentifier&,
359
drizzled::TableIdentifiers&)
342
const drizzled::identifier::Schema&,
343
drizzled::identifier::Table::vector&)