28
#include <drizzled/error.h>
28
#include "drizzled/error.h"
29
29
#include <drizzled/gettext.h>
30
30
#include <drizzled/my_hash.h>
31
#include <drizzled/internal/m_string.h>
31
#include "drizzled/internal/m_string.h"
32
32
#include <drizzled/session.h>
33
#include <drizzled/schema.h>
33
#include <drizzled/db.h>
34
34
#include <drizzled/sql_base.h>
35
35
#include <drizzled/lock.h>
36
36
#include <drizzled/errmsg_print.h>
37
37
#include <drizzled/transaction_services.h>
38
38
#include <drizzled/message/schema.pb.h>
39
#include <drizzled/sql_table.h>
40
#include <drizzled/plugin/storage_engine.h>
41
#include <drizzled/plugin/authorization.h>
42
#include <drizzled/global_charset_info.h>
43
#include <drizzled/pthread_globals.h>
44
#include <drizzled/charset.h>
45
#include <drizzled/internal/my_sys.h>
39
#include "drizzled/sql_table.h"
40
#include "drizzled/plugin/storage_engine.h"
41
#include "drizzled/plugin/authorization.h"
42
#include "drizzled/global_charset_info.h"
43
#include "drizzled/pthread_globals.h"
44
#include "drizzled/charset.h"
47
46
#include <boost/thread/mutex.hpp>
48
#include "drizzled/internal/my_sys.h"
49
50
#define MAX_DROP_TABLE_Q_LEN 1024
51
52
using namespace std;
59
static void change_db_impl(Session &session);
60
static void change_db_impl(Session &session, identifier::Schema &schema_identifier);
57
static void change_db_impl(Session *session);
58
static void change_db_impl(Session *session, identifier::Schema &schema_identifier);
83
bool create(Session &session, const message::Schema &schema_message, const bool is_if_not_exists)
81
bool create_db(Session *session, const message::Schema &schema_message, const bool is_if_not_exists)
85
83
TransactionServices &transaction_services= TransactionServices::singleton();
97
95
has the global read lock and refuses the operation with
98
96
ER_CANT_UPDATE_WITH_READLOCK if applicable.
100
if (session.wait_if_global_read_lock(false, true))
98
if (session->wait_if_global_read_lock(false, true))
108
106
// @todo push this lock down into the engine
110
boost::mutex::scoped_lock scopedLock(session.catalog().schemaLock());
108
boost::mutex::scoped_lock scopedLock(session->catalog().schemaLock());
112
110
// Check to see if it exists already.
113
111
identifier::Schema schema_identifier(schema_message.name());
123
push_warning_printf(&session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
121
push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
124
122
ER_DB_CREATE_EXISTS, ER(ER_DB_CREATE_EXISTS),
125
123
schema_message.name().c_str());
129
127
else if (not plugin::StorageEngine::createSchema(schema_message)) // Try to create it
134
132
else // Created !
136
transaction_services.createSchema(session, schema_message);
134
transaction_services.createSchema(*session, schema_message);
140
session.startWaitingGlobalReadLock();
138
session->startWaitingGlobalReadLock();
146
144
/* db-name is already validated when we come here */
148
bool alter(Session &session,
149
const message::Schema &schema_message,
150
const message::Schema &original_schema)
146
bool alter_db(Session *session,
147
const message::Schema &schema_message,
148
const message::schema::shared_ptr &original_schema)
152
150
TransactionServices &transaction_services= TransactionServices::singleton();
163
161
has the global read lock and refuses the operation with
164
162
ER_CANT_UPDATE_WITH_READLOCK if applicable.
166
if ((session.wait_if_global_read_lock(false, true)))
164
if ((session->wait_if_global_read_lock(false, true)))
171
boost::mutex::scoped_lock scopedLock(session.catalog().schemaLock());
169
boost::mutex::scoped_lock scopedLock(session->catalog().schemaLock());
173
171
identifier::Schema schema_idenifier(schema_message.name());
174
172
if (not plugin::StorageEngine::doesSchemaExist(schema_idenifier))
185
transaction_services.alterSchema(session, original_schema, schema_message);
183
transaction_services.alterSchema(*session, original_schema, schema_message);
190
188
my_error(ER_ALTER_SCHEMA, schema_idenifier);
193
session.startWaitingGlobalReadLock();
191
session->startWaitingGlobalReadLock();
229
227
has the global read lock and refuses the operation with
230
228
ER_CANT_UPDATE_WITH_READLOCK if applicable.
232
if (session.wait_if_global_read_lock(false, true))
230
if (session->wait_if_global_read_lock(false, true))
239
boost::mutex::scoped_lock scopedLock(session.catalog().schemaLock());
240
message::schema::shared_ptr message= plugin::StorageEngine::getSchemaDefinition(schema_identifier);
237
boost::mutex::scoped_lock scopedLock(session->catalog().schemaLock());
242
239
/* See if the schema exists */
240
if (not plugin::StorageEngine::doesSchemaExist(schema_identifier))
247
244
std::string path;
248
245
schema_identifier.getSQLPath(path);
250
push_warning_printf(&session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
247
push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
251
248
ER_DB_DROP_EXISTS, ER(ER_DB_DROP_EXISTS),
271
268
SELECT DATABASE() in the future). For this we free() session->db and set
274
if (not error and schema_identifier.compare(*session.schema()))
271
if (not error and schema_identifier.compare(*session->schema()))
275
272
change_db_impl(session);
277
session.startWaitingGlobalReadLock();
274
session->startWaitingGlobalReadLock();
341
338
@retval true Error
344
bool change(Session &session, identifier::Schema &schema_identifier)
341
bool change_db(Session *session, identifier::Schema &schema_identifier)
347
if (not plugin::Authorization::isAuthorized(*session.user(), schema_identifier))
344
if (not plugin::Authorization::isAuthorized(session->user(), schema_identifier))
349
346
/* Error message is set in isAuthorized */
353
if (not check(session, schema_identifier))
350
if (not check_db_name(session, schema_identifier))
355
352
my_error(ER_WRONG_DB_NAME, schema_identifier);
382
379
@param new_db_charset Character set of the new database.
385
static void change_db_impl(Session &session, identifier::Schema &schema_identifier)
382
static void change_db_impl(Session *session, identifier::Schema &schema_identifier)
387
384
/* 1. Change current database in Session. */
405
402
the previous database name, we should do it explicitly.
408
session.set_db(schema_identifier.getSchemaName());
412
static void change_db_impl(Session &session)
414
session.set_db(string());
418
Check if database name is valid
422
org_name Name of database and length
429
bool check(Session &session, identifier::Schema &schema_identifier)
431
if (not plugin::Authorization::isAuthorized(*session.user(), schema_identifier))
436
return schema_identifier.isValid();
439
} /* namespace schema */
405
session->set_db(schema_identifier.getSchemaName());
409
static void change_db_impl(Session *session)
411
session->set_db(string());
441
414
} /* namespace drizzled */