drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
plugin/json_server/ddl/schema.cc
1/* mode: c; c-basic-offset: 2; indent-tabs-mode: nil;
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2011-2013 Stewart Smith, Henrik Ingo, Mohit Srivastava
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
23
24 #include <config.h>
25
26 #include <drizzled/show.h>
27 #include <drizzled/session.h>
28 #include <drizzled/schema.h>
29 #include <drizzled/message.h>
30 #include <drizzled/sql_lex.h>
31 #include <drizzled/plugin/event_observer.h>
32 #include <drizzled/catalog/instance.h>
33 #include <plugin/json_server/ddl/schema.h>
34 #include <drizzled/plugin/authorization.h>
35 #include <drizzled/plugin/storage_engine.h>
36
37 #include <string>
38
39
40 using namespace std;
41 using namespace drizzled;
42
43 namespace drizzle_plugin {
44 namespace json_server {
45
47 {
48 if (not validateSchemaOptions())
49 return true;
50
51 if (session().inTransaction())
52 {
53 my_error(ER_TRANSACTIONAL_DDL_NOT_SUPPORTED, MYF(0));
54 return true;
55 }
56
57 drizzled::identifier::Schema schema_identifier(session().catalog().identifier(),_db_name);
58
59 if (not check(schema_identifier))
60 return false;
61
62 drizzled::message::schema::init(schema_message, schema_identifier);
63 message::set_definer(schema_message, *session().user());
64
65 bool res =false;
66 std::string path = schema_identifier.getSQLPath();
67
68 if (DRIZZLE_UNLIKELY(plugin::EventObserver::beforeCreateDatabase(session(), path)))
69 {
70 my_error(ER_EVENT_OBSERVER_PLUGIN, MYF(0), path.c_str());
71 }
72 else
73 {
74 res= schema::create(session(), schema_message, false);
75 if (DRIZZLE_UNLIKELY(plugin::EventObserver::afterCreateDatabase(session(), path, res)))
76 {
77 my_error(ER_EVENT_OBSERVER_PLUGIN, schema_identifier);
78 res = false;
79 }
80 }
81 return not res;
82 }
83
85 {
86 if (session().inTransaction())
87 {
88 my_error(ER_TRANSACTIONAL_DDL_NOT_SUPPORTED, MYF(0));
89 return true;
90 }
91
92 drizzled::identifier::Schema schema_identifier(session().catalog().identifier(),_db_name);
93
94 if (not schema::check(session(),schema_identifier))
95 {
96 my_error(ER_WRONG_DB_NAME, schema_identifier);
97 return false;
98 }
99
100 if (session().inTransaction())
101 {
102 my_message(ER_LOCK_OR_ACTIVE_TRANSACTION, ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
103 return true;
104 }
105
106 bool res = true;
107 std::string path = schema_identifier.getSQLPath();
108 if (DRIZZLE_UNLIKELY(plugin::EventObserver::beforeDropDatabase(session(), path)))
109 {
110 my_error(ER_EVENT_OBSERVER_PLUGIN, schema_identifier);
111 }
112 else
113 {
114 res= schema::drop(session(), schema_identifier, false);
115 if (DRIZZLE_UNLIKELY(plugin::EventObserver::afterDropDatabase(session(), path, res)))
116 {
117 my_error(ER_EVENT_OBSERVER_PLUGIN, MYF(0), path.c_str());
118 res = false;
119 }
120 }
121
122 return res;
123
124 }
125
127 {
128 size_t num_engine_options= schema_message.engine().options_size();
129 bool rc= num_engine_options ? false : true;
130
131 for (size_t y= 0; y < num_engine_options; ++y)
132 {
133 my_error(ER_UNKNOWN_SCHEMA_OPTION, MYF(0),schema_message.engine().options(y).name().c_str(),schema_message.engine().options(y).state().c_str());
134 rc= false;
135 }
136 return rc;
137
138 }
139
140 bool Schema::check(const identifier::Schema &identifier)
141 {
142 if (not identifier.isValid())
143 return false;
144
145 if (not plugin::Authorization::isAuthorized(*session().user(), identifier))
146 return false;
147
148 if (plugin::StorageEngine::doesSchemaExist(identifier))
149 {
150 my_error(ER_DB_CREATE_EXISTS, identifier);
151 return false;
152 }
153
154 return true;
155 }
156}
157}
158
bool check(const identifier::Schema &identifier)
static bool isAuthorized(const drizzled::identifier::User &user_ctx, const identifier::Schema &schema_identifier, bool send_error=true)
TODO: Rename this file - func.h is stupid.
STL namespace.