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
00032 #include <string>
00033
00034 using namespace std;
00035
00036 namespace drizzled
00037 {
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(string(lex().name.str, lex().name.length));
00051 if (not check(schema_identifier))
00052 return false;
00053
00054 drizzled::message::schema::init(schema_message, lex().name.str);
00055
00056 bool res = false;
00057 std::string path;
00058 schema_identifier.getSQLPath(path);
00059
00060 if (unlikely(plugin::EventObserver::beforeCreateDatabase(session(), path)))
00061 {
00062 my_error(ER_EVENT_OBSERVER_PLUGIN, MYF(0), path.c_str());
00063 }
00064 else
00065 {
00066 res= schema::create(session(), schema_message, lex().exists());
00067 if (unlikely(plugin::EventObserver::afterCreateDatabase(session(), path, res)))
00068 {
00069 my_error(ER_EVENT_OBSERVER_PLUGIN, schema_identifier);
00070 res = false;
00071 }
00072
00073 }
00074
00075 return not res;
00076 }
00077
00078 bool statement::CreateSchema::check(const identifier::Schema &identifier)
00079 {
00080 if (not identifier.isValid())
00081 return false;
00082
00083 if (not plugin::Authorization::isAuthorized(*session().user(), identifier))
00084 return false;
00085
00086 if (not lex().exists())
00087 {
00088 if (plugin::StorageEngine::doesSchemaExist(identifier))
00089 {
00090 my_error(ER_DB_CREATE_EXISTS, identifier);
00091
00092 return false;
00093 }
00094 }
00095
00096 return true;
00097 }
00098
00099
00100 bool statement::CreateSchema::validateSchemaOptions()
00101 {
00102 size_t num_engine_options= schema_message.engine().options_size();
00103 bool rc= num_engine_options ? false : true;
00104
00105 for (size_t y= 0; y < num_engine_options; ++y)
00106 {
00107 my_error(ER_UNKNOWN_SCHEMA_OPTION, MYF(0),
00108 schema_message.engine().options(y).name().c_str(),
00109 schema_message.engine().options(y).state().c_str());
00110
00111 rc= false;
00112 }
00113
00114 return rc;
00115 }
00116
00117 }
00118