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(session.catalog().identifier(),
103 schema_message.name());
104 if (plugin::StorageEngine::doesSchemaExist(schema_identifier))
106 if (not is_if_not_exists)
108 my_error(ER_DB_CREATE_EXISTS, schema_identifier);
113 push_warning_printf(&session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
114 ER_DB_CREATE_EXISTS, ER(ER_DB_CREATE_EXISTS),
115 schema_message.name().c_str());
119 else if (not plugin::StorageEngine::createSchema(schema_message))
121 my_error(ER_CANT_CREATE_DB, MYF(0), schema_message.name().c_str(), errno);
126 TransactionServices::createSchema(session, schema_message);
130 session.startWaitingGlobalReadLock();
138 bool alter(Session &session,
139 const message::Schema &schema_message,
140 const message::Schema &original_schema)
154 if ((session.wait_if_global_read_lock(
false,
true)))
159 boost::mutex::scoped_lock scopedLock(session.catalog().schemaLock());
161 identifier::Schema schema_idenifier(session.catalog().identifier(),
162 schema_message.name());
163 if (not plugin::StorageEngine::doesSchemaExist(schema_idenifier))
165 my_error(ER_SCHEMA_DOES_NOT_EXIST, schema_idenifier);
170 success= plugin::StorageEngine::alterSchema(schema_message);
174 TransactionServices::alterSchema(session, original_schema, schema_message);
179 my_error(ER_ALTER_SCHEMA, schema_idenifier);
182 session.startWaitingGlobalReadLock();
205 bool drop(Session &session,
const identifier::Schema &schema_identifier,
bool if_exists)
219 if (session.wait_if_global_read_lock(
false,
true))
226 boost::mutex::scoped_lock scopedLock(session.catalog().schemaLock());
227 if (message::schema::shared_ptr message= plugin::StorageEngine::getSchemaDefinition(schema_identifier))
229 error= plugin::StorageEngine::dropSchema(session, schema_identifier, *message);
233 push_warning_printf(&session, DRIZZLE_ERROR::WARN_LEVEL_NOTE, ER_DB_DROP_EXISTS, ER(ER_DB_DROP_EXISTS),
234 schema_identifier.getSQLPath().c_str());
239 my_error(ER_DB_DROP_EXISTS, schema_identifier);
249 if (not error and schema_identifier.compare(*session.schema()))
250 session.set_schema(
"");
252 session.startWaitingGlobalReadLock();
319 bool change(Session &session,
const identifier::Schema &schema_identifier)
322 if (not plugin::Authorization::isAuthorized(*session.user(), schema_identifier))
328 if (not check(session, schema_identifier))
330 my_error(ER_WRONG_DB_NAME, schema_identifier);
335 if (not plugin::StorageEngine::doesSchemaExist(schema_identifier))
337 my_error(ER_BAD_DB_ERROR, schema_identifier);
344 session.set_schema(schema_identifier.getSchemaName());
373 bool check(Session &session,
const identifier::Schema &schema_identifier)
375 if (not plugin::Authorization::isAuthorized(*session.user(), schema_identifier))
377 return schema_identifier.isValid();
TODO: Rename this file - func.h is stupid.