Drizzled Public API Documentation

show_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) 2010 Brian Aker
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 #include <plugin/show_dictionary/dictionary.h>
00023 #include <drizzled/identifier.h>
00024 #include <drizzled/message.h>
00025 #include <drizzled/message/statement_transform.h>
00026 #include <string>
00027 
00028 using namespace std;
00029 using namespace drizzled;
00030 
00031 ShowCreateSchema::ShowCreateSchema() :
00032   show_dictionary::Show("SCHEMA_SQL_DEFINITION")
00033 {
00034   add_field("SCHEMA_NAME", plugin::TableFunction::STRING, MAXIMUM_IDENTIFIER_LENGTH, false);
00035   add_field("SCHEMA_SQL_DEFINITION", plugin::TableFunction::STRING, TABLE_FUNCTION_BLOB_SIZE, false);
00036 }
00037 
00038 ShowCreateSchema::Generator::Generator(Field **arg) :
00039   show_dictionary::Show::Generator(arg),
00040   if_not_exists(false)
00041 {
00042   if (not isShowQuery())
00043    return;
00044 
00045   statement::Show& select= static_cast<statement::Show&>(statement());
00046 
00047   if (not select.getShowSchema().empty())
00048   {
00049     schema_name.append(select.getShowTable());
00050     identifier::Schema identifier(select.getShowSchema());
00051 
00052     if (not plugin::Authorization::isAuthorized(*getSession().user(),
00053                                                 identifier, false))
00054     {
00055       drizzled::error::access(*getSession().user(), identifier);
00056       return;
00057     }
00058 
00059     schema_message= plugin::StorageEngine::getSchemaDefinition(identifier);
00060 
00061     if_not_exists= select.getShowExists();
00062   }
00063 }
00064 
00065 bool ShowCreateSchema::Generator::populate()
00066 {
00067   if (not schema_message)
00068     return false;
00069 
00070   std::string buffer;
00071 
00072   /* This needs to be moved out to its own function */
00073   {
00074     buffer.append("CREATE DATABASE ");
00075 
00076     if (if_not_exists)
00077       buffer.append("IF NOT EXISTS ");
00078 
00079     buffer.append("`");
00080     buffer.append(schema_message->name());
00081     buffer.append("`");
00082 
00083     if (schema_message->has_collation())
00084     {
00085       buffer.append(" COLLATE = ");
00086       buffer.append(schema_message->collation());
00087     }
00088 
00089     if (not message::is_replicated(*schema_message))
00090     {
00091       buffer.append(" REPLICATE = FALSE");
00092     }
00093   }
00094 
00095   push(schema_message->name());
00096   push(buffer);
00097 
00098   schema_message.reset();
00099 
00100   return true;
00101 }