00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <config.h>
00019
00020 #include <fcntl.h>
00021 #include <sys/stat.h>
00022 #include <sys/types.h>
00023
00024 #include <set>
00025 #include <string>
00026 #include <fstream>
00027
00028 #include <drizzled/error.h>
00029 #include <drizzled/gettext.h>
00030 #include <drizzled/internal/m_string.h>
00031 #include <drizzled/session.h>
00032 #include <drizzled/schema.h>
00033 #include <drizzled/sql_base.h>
00034 #include <drizzled/lock.h>
00035 #include <drizzled/errmsg_print.h>
00036 #include <drizzled/transaction_services.h>
00037 #include <drizzled/message/schema.pb.h>
00038 #include <drizzled/sql_table.h>
00039 #include <drizzled/plugin/storage_engine.h>
00040 #include <drizzled/plugin/authorization.h>
00041 #include <drizzled/global_charset_info.h>
00042 #include <drizzled/pthread_globals.h>
00043 #include <drizzled/charset.h>
00044 #include <drizzled/internal/my_sys.h>
00045
00046 #include <boost/thread/mutex.hpp>
00047
00048 #define MAX_DROP_TABLE_Q_LEN 1024
00049
00050 using namespace std;
00051
00052 namespace drizzled
00053 {
00054
00055 namespace schema
00056 {
00057
00058 static void change_db_impl(Session &session);
00059 static void change_db_impl(Session &session, identifier::Schema &schema_identifier);
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082 bool create(Session &session, const message::Schema &schema_message, const bool is_if_not_exists)
00083 {
00084 TransactionServices &transaction_services= TransactionServices::singleton();
00085 bool error= false;
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099 if (session.wait_if_global_read_lock(false, true))
00100 {
00101 return false;
00102 }
00103
00104 assert(schema_message.has_name());
00105 assert(schema_message.has_collation());
00106
00107
00108 {
00109 boost::mutex::scoped_lock scopedLock(session.catalog().schemaLock());
00110
00111
00112 identifier::Schema schema_identifier(schema_message.name());
00113 if (plugin::StorageEngine::doesSchemaExist(schema_identifier))
00114 {
00115 if (not is_if_not_exists)
00116 {
00117 my_error(ER_DB_CREATE_EXISTS, schema_identifier);
00118 error= true;
00119 }
00120 else
00121 {
00122 push_warning_printf(&session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
00123 ER_DB_CREATE_EXISTS, ER(ER_DB_CREATE_EXISTS),
00124 schema_message.name().c_str());
00125 session.my_ok();
00126 }
00127 }
00128 else if (not plugin::StorageEngine::createSchema(schema_message))
00129 {
00130 my_error(ER_CANT_CREATE_DB, MYF(0), schema_message.name().c_str(), errno);
00131 error= true;
00132 }
00133 else
00134 {
00135 transaction_services.createSchema(session, schema_message);
00136 session.my_ok(1);
00137 }
00138 }
00139 session.startWaitingGlobalReadLock();
00140
00141 return error;
00142 }
00143
00144
00145
00146
00147 bool alter(Session &session,
00148 const message::Schema &schema_message,
00149 const message::Schema &original_schema)
00150 {
00151 TransactionServices &transaction_services= TransactionServices::singleton();
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165 if ((session.wait_if_global_read_lock(false, true)))
00166 return false;
00167
00168 bool success;
00169 {
00170 boost::mutex::scoped_lock scopedLock(session.catalog().schemaLock());
00171
00172 identifier::Schema schema_idenifier(schema_message.name());
00173 if (not plugin::StorageEngine::doesSchemaExist(schema_idenifier))
00174 {
00175 my_error(ER_SCHEMA_DOES_NOT_EXIST, schema_idenifier);
00176 return false;
00177 }
00178
00179
00180 success= plugin::StorageEngine::alterSchema(schema_message);
00181
00182 if (success)
00183 {
00184 transaction_services.alterSchema(session, original_schema, schema_message);
00185 session.my_ok(1);
00186 }
00187 else
00188 {
00189 my_error(ER_ALTER_SCHEMA, schema_idenifier);
00190 }
00191 }
00192 session.startWaitingGlobalReadLock();
00193
00194 return success;
00195 }
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215 bool drop(Session &session, identifier::Schema &schema_identifier, const bool if_exists)
00216 {
00217 bool error= false;
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231 if (session.wait_if_global_read_lock(false, true))
00232 {
00233 return true;
00234 }
00235
00236 do
00237 {
00238 boost::mutex::scoped_lock scopedLock(session.catalog().schemaLock());
00239 message::schema::shared_ptr message= plugin::StorageEngine::getSchemaDefinition(schema_identifier);
00240
00241
00242 if (not message)
00243 {
00244 if (if_exists)
00245 {
00246 std::string path;
00247 schema_identifier.getSQLPath(path);
00248
00249 push_warning_printf(&session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
00250 ER_DB_DROP_EXISTS, ER(ER_DB_DROP_EXISTS),
00251 path.c_str());
00252 }
00253 else
00254 {
00255 error= true;
00256 my_error(ER_DB_DROP_EXISTS, schema_identifier);
00257 break;
00258 }
00259 }
00260 else
00261 {
00262 error= plugin::StorageEngine::dropSchema(session, schema_identifier, *message);
00263 }
00264
00265 } while (0);
00266
00267
00268
00269
00270
00271
00272
00273 if (not error and schema_identifier.compare(*session.schema()))
00274 change_db_impl(session);
00275
00276 session.startWaitingGlobalReadLock();
00277
00278 return error;
00279 }
00280
00343 bool change(Session &session, identifier::Schema &schema_identifier)
00344 {
00345
00346 if (not plugin::Authorization::isAuthorized(*session.user(), schema_identifier))
00347 {
00348
00349 return true;
00350 }
00351
00352 if (not check(session, schema_identifier))
00353 {
00354 my_error(ER_WRONG_DB_NAME, schema_identifier);
00355
00356 return true;
00357 }
00358
00359 if (not plugin::StorageEngine::doesSchemaExist(schema_identifier))
00360 {
00361 my_error(ER_BAD_DB_ERROR, schema_identifier);
00362
00363
00364
00365 return true;
00366 }
00367
00368 change_db_impl(session, schema_identifier);
00369
00370 return false;
00371 }
00372
00384 static void change_db_impl(Session &session, identifier::Schema &schema_identifier)
00385 {
00386
00387
00388 #if 0
00389 if (new_db_name == NULL)
00390 {
00391
00392
00393
00394
00395
00396 session->set_db(NULL, 0);
00397 }
00398 else
00399 #endif
00400 {
00401
00402
00403
00404
00405
00406
00407 session.set_db(schema_identifier.getSchemaName());
00408 }
00409 }
00410
00411 static void change_db_impl(Session &session)
00412 {
00413 session.set_db(string());
00414 }
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428 bool check(Session &session, identifier::Schema &schema_identifier)
00429 {
00430 if (not plugin::Authorization::isAuthorized(*session.user(), schema_identifier))
00431 {
00432 return false;
00433 }
00434
00435 return schema_identifier.isValid();
00436 }
00437
00438 }
00439
00440 }