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 
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 // We don't actually test anything at this point, we assume it is all bad.
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 } /* namespace drizzled */
00118