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/db.h>
33
#include <drizzled/schema.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"
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>
46
47
#include <boost/thread/mutex.hpp>
48
#include "drizzled/internal/my_sys.h"
50
49
#define MAX_DROP_TABLE_Q_LEN 1024
52
51
using namespace std;
57
static void change_db_impl(Session *session);
58
static void change_db_impl(Session *session, identifier::Schema &schema_identifier);
59
static void change_db_impl(Session &session);
60
static void change_db_impl(Session &session, identifier::Schema &schema_identifier);
81
bool create_db(Session *session, const message::Schema &schema_message, const bool is_if_not_exists)
83
bool create(Session &session, const message::Schema &schema_message, const bool is_if_not_exists)
83
85
TransactionServices &transaction_services= TransactionServices::singleton();
95
97
has the global read lock and refuses the operation with
96
98
ER_CANT_UPDATE_WITH_READLOCK if applicable.
98
if (session->wait_if_global_read_lock(false, true))
100
if (session.wait_if_global_read_lock(false, true))
106
108
// @todo push this lock down into the engine
108
boost::mutex::scoped_lock scopedLock(session->catalog().schemaLock());
110
boost::mutex::scoped_lock scopedLock(session.catalog().schemaLock());
110
112
// Check to see if it exists already.
111
113
identifier::Schema schema_identifier(schema_message.name());
121
push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
123
push_warning_printf(&session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
122
124
ER_DB_CREATE_EXISTS, ER(ER_DB_CREATE_EXISTS),
123
125
schema_message.name().c_str());
127
129
else if (not plugin::StorageEngine::createSchema(schema_message)) // Try to create it
132
134
else // Created !
134
transaction_services.createSchema(*session, schema_message);
136
transaction_services.createSchema(session, schema_message);
138
session->startWaitingGlobalReadLock();
140
session.startWaitingGlobalReadLock();
144
146
/* db-name is already validated when we come here */
146
bool alter_db(Session *session,
147
const message::Schema &schema_message,
148
const message::schema::shared_ptr &original_schema)
148
bool alter(Session &session,
149
const message::Schema &schema_message,
150
const message::Schema &original_schema)
150
152
TransactionServices &transaction_services= TransactionServices::singleton();
161
163
has the global read lock and refuses the operation with
162
164
ER_CANT_UPDATE_WITH_READLOCK if applicable.
164
if ((session->wait_if_global_read_lock(false, true)))
166
if ((session.wait_if_global_read_lock(false, true)))
169
boost::mutex::scoped_lock scopedLock(session->catalog().schemaLock());
171
boost::mutex::scoped_lock scopedLock(session.catalog().schemaLock());
171
173
identifier::Schema schema_idenifier(schema_message.name());
172
174
if (not plugin::StorageEngine::doesSchemaExist(schema_idenifier))
183
transaction_services.alterSchema(*session, original_schema, schema_message);
185
transaction_services.alterSchema(session, original_schema, schema_message);
188
190
my_error(ER_ALTER_SCHEMA, schema_idenifier);
191
session->startWaitingGlobalReadLock();
193
session.startWaitingGlobalReadLock();
227
229
has the global read lock and refuses the operation with
228
230
ER_CANT_UPDATE_WITH_READLOCK if applicable.
230
if (session->wait_if_global_read_lock(false, true))
232
if (session.wait_if_global_read_lock(false, true))
237
boost::mutex::scoped_lock scopedLock(session->catalog().schemaLock());
239
boost::mutex::scoped_lock scopedLock(session.catalog().schemaLock());
239
241
/* See if the schema exists */
240
242
if (not plugin::StorageEngine::doesSchemaExist(schema_identifier))
244
246
std::string path;
245
247
schema_identifier.getSQLPath(path);
247
push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
249
push_warning_printf(&session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
248
250
ER_DB_DROP_EXISTS, ER(ER_DB_DROP_EXISTS),
268
270
SELECT DATABASE() in the future). For this we free() session->db and set
271
if (not error and schema_identifier.compare(*session->schema()))
273
if (not error and schema_identifier.compare(*session.schema()))
272
274
change_db_impl(session);
274
session->startWaitingGlobalReadLock();
276
session.startWaitingGlobalReadLock();
338
340
@retval true Error
341
bool change_db(Session *session, identifier::Schema &schema_identifier)
343
bool change(Session &session, identifier::Schema &schema_identifier)
344
if (not plugin::Authorization::isAuthorized(session->user(), schema_identifier))
346
if (not plugin::Authorization::isAuthorized(*session.user(), schema_identifier))
346
348
/* Error message is set in isAuthorized */
350
if (not check_db_name(session, schema_identifier))
352
if (not check(session, schema_identifier))
352
354
my_error(ER_WRONG_DB_NAME, schema_identifier);
379
381
@param new_db_charset Character set of the new database.
382
static void change_db_impl(Session *session, identifier::Schema &schema_identifier)
384
static void change_db_impl(Session &session, identifier::Schema &schema_identifier)
384
386
/* 1. Change current database in Session. */
402
404
the previous database name, we should do it explicitly.
405
session->set_db(schema_identifier.getSchemaName());
409
static void change_db_impl(Session *session)
411
session->set_db(string());
407
session.set_db(schema_identifier.getSchemaName());
411
static void change_db_impl(Session &session)
413
session.set_db(string());
417
Check if database name is valid
421
org_name Name of database and length
428
bool check(Session &session, identifier::Schema &schema_identifier)
430
if (not plugin::Authorization::isAuthorized(*session.user(), schema_identifier))
435
return schema_identifier.isValid();
438
} /* namespace schema */
414
440
} /* namespace drizzled */