22 #include <sys/types.h>
28 #include <drizzled/error.h>
29 #include <drizzled/gettext.h>
30 #include <drizzled/internal/m_string.h>
31 #include <drizzled/session.h>
32 #include <drizzled/schema.h>
33 #include <drizzled/sql_base.h>
34 #include <drizzled/lock.h>
35 #include <drizzled/errmsg_print.h>
36 #include <drizzled/transaction_services.h>
37 #include <drizzled/message/schema.pb.h>
39 #include <drizzled/plugin/storage_engine.h>
40 #include <drizzled/plugin/authorization.h>
41 #include <drizzled/pthread_globals.h>
42 #include <drizzled/charset.h>
43 #include <drizzled/internal/my_sys.h>
44 #include <drizzled/catalog/instance.h>
45 #include <boost/thread/mutex.hpp>
73 bool create(Session &session,
const message::Schema &schema_message,
const bool is_if_not_exists)
89 if (session.wait_if_global_read_lock(
false,
true))
94 assert(schema_message.has_name());
95 assert(schema_message.has_collation());
99 boost::mutex::scoped_lock scopedLock(session.catalog().schemaLock());
102 identifier::Schema schema_identifier(schema_message.name());
103 if (plugin::StorageEngine::doesSchemaExist(schema_identifier))
105 if (not is_if_not_exists)
107 my_error(ER_DB_CREATE_EXISTS, schema_identifier);
112 push_warning_printf(&session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
113 ER_DB_CREATE_EXISTS, ER(ER_DB_CREATE_EXISTS),
114 schema_message.name().c_str());
118 else if (not plugin::StorageEngine::createSchema(schema_message))
120 my_error(ER_CANT_CREATE_DB, MYF(0), schema_message.name().c_str(), errno);
125 TransactionServices::createSchema(session, schema_message);
129 session.startWaitingGlobalReadLock();
137 bool alter(Session &session,
138 const message::Schema &schema_message,
139 const message::Schema &original_schema)
153 if ((session.wait_if_global_read_lock(
false,
true)))
158 boost::mutex::scoped_lock scopedLock(session.catalog().schemaLock());
160 identifier::Schema schema_idenifier(schema_message.name());
161 if (not plugin::StorageEngine::doesSchemaExist(schema_idenifier))
163 my_error(ER_SCHEMA_DOES_NOT_EXIST, schema_idenifier);
168 success= plugin::StorageEngine::alterSchema(schema_message);
172 TransactionServices::alterSchema(session, original_schema, schema_message);
177 my_error(ER_ALTER_SCHEMA, schema_idenifier);
180 session.startWaitingGlobalReadLock();
203 bool drop(Session &session,
const identifier::Schema &schema_identifier,
bool if_exists)
217 if (session.wait_if_global_read_lock(
false,
true))
224 boost::mutex::scoped_lock scopedLock(session.catalog().schemaLock());
225 if (message::schema::shared_ptr message= plugin::StorageEngine::getSchemaDefinition(schema_identifier))
227 error= plugin::StorageEngine::dropSchema(session, schema_identifier, *message);
231 push_warning_printf(&session, DRIZZLE_ERROR::WARN_LEVEL_NOTE, ER_DB_DROP_EXISTS, ER(ER_DB_DROP_EXISTS),
232 schema_identifier.getSQLPath().c_str());
237 my_error(ER_DB_DROP_EXISTS, schema_identifier);
247 if (not error and schema_identifier.compare(*session.schema()))
248 session.set_schema(
"");
250 session.startWaitingGlobalReadLock();
317 bool change(Session &session,
const identifier::Schema &schema_identifier)
320 if (not plugin::Authorization::isAuthorized(*session.user(), schema_identifier))
326 if (not check(session, schema_identifier))
328 my_error(ER_WRONG_DB_NAME, schema_identifier);
333 if (not plugin::StorageEngine::doesSchemaExist(schema_identifier))
335 my_error(ER_BAD_DB_ERROR, schema_identifier);
342 session.set_schema(schema_identifier.getSchemaName());
371 bool check(Session &session,
const identifier::Schema &schema_identifier)
373 if (not plugin::Authorization::isAuthorized(*session.user(), schema_identifier))
375 return schema_identifier.isValid();