18
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
23
#include <drizzled/show.h>
23
24
#include <drizzled/session.h>
24
25
#include <drizzled/statement/create_schema.h>
25
#include <drizzled/db.h>
26
#include <drizzled/schema.h>
26
27
#include <drizzled/plugin/event_observer.h>
27
28
#include <drizzled/message.h>
29
#include <drizzled/plugin/storage_engine.h>
30
#include <drizzled/sql_lex.h>
31
#include <drizzled/plugin/authorization.h>
31
35
using namespace std;
36
39
bool statement::CreateSchema::execute()
38
41
if (not validateSchemaOptions())
41
if (not session->endActiveTransaction())
44
if (session().inTransaction())
46
my_error(ER_TRANSACTIONAL_DDL_NOT_SUPPORTED, MYF(0));
46
SchemaIdentifier schema_identifier(string(session->lex->name.str, session->lex->name.length));
50
identifier::Schema schema_identifier(to_string(lex().name));
47
51
if (not check(schema_identifier))
50
drizzled::message::init(schema_message, session->lex->name.str);
54
drizzled::message::schema::init(schema_message, lex().name.data());
56
message::set_definer(schema_message, *session().user());
54
schema_identifier.getSQLPath(path);
59
std::string path = schema_identifier.getSQLPath();
56
if (unlikely(plugin::EventObserver::beforeCreateDatabase(*session, path)))
61
if (unlikely(plugin::EventObserver::beforeCreateDatabase(session(), path)))
58
63
my_error(ER_EVENT_OBSERVER_PLUGIN, MYF(0), path.c_str());
62
res= mysql_create_db(session, schema_message, is_if_not_exists);
63
if (unlikely(plugin::EventObserver::afterCreateDatabase(*session, path, res)))
67
res= schema::create(session(), schema_message, lex().exists());
68
if (unlikely(plugin::EventObserver::afterCreateDatabase(session(), path, res)))
65
my_error(ER_EVENT_OBSERVER_PLUGIN, MYF(0), path.c_str());
70
my_error(ER_EVENT_OBSERVER_PLUGIN, schema_identifier);
74
bool statement::CreateSchema::check(const SchemaIdentifier &identifier)
79
bool statement::CreateSchema::check(const identifier::Schema &identifier)
76
81
if (not identifier.isValid())
79
if (not plugin::Authorization::isAuthorized(getSession()->user(), identifier))
84
if (not plugin::Authorization::isAuthorized(*session().user(), identifier))
82
if (not is_if_not_exists)
87
if (not lex().exists())
84
89
if (plugin::StorageEngine::doesSchemaExist(identifier))
88
identifier.getSQLPath(name);
89
my_error(ER_DB_CREATE_EXISTS, MYF(0), name.c_str());
91
my_error(ER_DB_CREATE_EXISTS, identifier);