Drizzled Public API Documentation

create_schema.cc
00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2009 Sun Microsystems, Inc.
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
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 // We don't actually test anything at this point, we assume it is all bad.
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 } /* namespace drizzled */