00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <config.h>
00022
00023 #include <drizzled/show.h>
00024 #include <drizzled/session.h>
00025 #include <drizzled/statement/create_schema.h>
00026 #include <drizzled/schema.h>
00027 #include <drizzled/plugin/event_observer.h>
00028 #include <drizzled/message.h>
00029 #include <drizzled/plugin/storage_engine.h>
00030 #include <drizzled/sql_lex.h>
00031 #include <drizzled/plugin/authorization.h>
00032
00033 #include <string>
00034
00035 using namespace std;
00036
00037 namespace drizzled {
00038
00039 bool statement::CreateSchema::execute()
00040 {
00041 if (not validateSchemaOptions())
00042 return true;
00043
00044 if (session().inTransaction())
00045 {
00046 my_error(ER_TRANSACTIONAL_DDL_NOT_SUPPORTED, MYF(0));
00047 return true;
00048 }
00049
00050 identifier::Schema schema_identifier(to_string(lex().name));
00051 if (not check(schema_identifier))
00052 return false;
00053
00054 drizzled::message::schema::init(schema_message, lex().name.data());
00055
00056 message::set_definer(schema_message, *session().user());
00057
00058 bool res = false;
00059 std::string path = schema_identifier.getSQLPath();
00060
00061 if (unlikely(plugin::EventObserver::beforeCreateDatabase(session(), path)))
00062 {
00063 my_error(ER_EVENT_OBSERVER_PLUGIN, MYF(0), path.c_str());
00064 }
00065 else
00066 {
00067 res= schema::create(session(), schema_message, lex().exists());
00068 if (unlikely(plugin::EventObserver::afterCreateDatabase(session(), path, res)))
00069 {
00070 my_error(ER_EVENT_OBSERVER_PLUGIN, schema_identifier);
00071 res = false;
00072 }
00073
00074 }
00075
00076 return not res;
00077 }
00078
00079 bool statement::CreateSchema::check(const identifier::Schema &identifier)
00080 {
00081 if (not identifier.isValid())
00082 return false;
00083
00084 if (not plugin::Authorization::isAuthorized(*session().user(), identifier))
00085 return false;
00086
00087 if (not lex().exists())
00088 {
00089 if (plugin::StorageEngine::doesSchemaExist(identifier))
00090 {
00091 my_error(ER_DB_CREATE_EXISTS, identifier);
00092
00093 return false;
00094 }
00095 }
00096
00097 return true;
00098 }
00099
00100
00101 bool statement::CreateSchema::validateSchemaOptions()
00102 {
00103 size_t num_engine_options= schema_message.engine().options_size();
00104 bool rc= num_engine_options ? false : true;
00105
00106 for (size_t y= 0; y < num_engine_options; ++y)
00107 {
00108 my_error(ER_UNKNOWN_SCHEMA_OPTION, MYF(0),
00109 schema_message.engine().options(y).name().c_str(),
00110 schema_message.engine().options(y).state().c_str());
00111
00112 rc= false;
00113 }
00114
00115 return rc;
00116 }
00117
00118 }