Drizzled Public API Documentation

show_create_table.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 <google/protobuf/text_format.h>
00027 #include <drizzled/plugin/authorization.h>
00028 #include <string>
00029 
00030 using namespace std;
00031 using namespace drizzled;
00032 
00033 ShowCreateTable::ShowCreateTable() :
00034   show_dictionary::Show("TABLE_SQL_DEFINITION")
00035 {
00036   add_field("TABLE_NAME", plugin::TableFunction::STRING, MAXIMUM_IDENTIFIER_LENGTH, false);
00037   add_field("TABLE_SQL_DEFINITION", plugin::TableFunction::STRING, TABLE_FUNCTION_BLOB_SIZE, false);
00038 }
00039 
00040 ShowCreateTable::Generator::Generator(Field **arg) :
00041   show_dictionary::Show::Generator(arg),
00042   is_primed(false)
00043 {
00044   if (not isShowQuery())
00045    return;
00046 
00047   statement::Show& select= static_cast<statement::Show&>(statement());
00048 
00049   if (not select.getShowTable().empty() && not select.getShowSchema().empty())
00050   {
00051     identifier::Table identifier(select.getShowSchema(), select.getShowTable());
00052 
00053     if (not plugin::Authorization::isAuthorized(*getSession().user(),
00054                                             identifier, false))
00055     {
00056       drizzled::error::access(*getSession().user(), identifier);
00057       return;
00058     }
00059 
00060     table_message= plugin::StorageEngine::getTableMessage(getSession(),
00061                                                           identifier);
00062 
00063     if (table_message)
00064       is_primed= true;
00065   }
00066 }
00067 
00068 bool ShowCreateTable::Generator::populate()
00069 {
00070   enum drizzled::message::TransformSqlError transform_err;
00071 
00072   if (not is_primed)
00073     return false;
00074 
00075   std::string create_sql;
00076   transform_err= message::transformTableDefinitionToSql(*table_message,
00077                                                         create_sql,
00078                                                         message::DRIZZLE,
00079                                                         false);
00080   if (transform_err != drizzled::message::NONE)
00081   {
00082     return false;
00083   }
00084 
00085   push(table_message->name());
00086   push(create_sql);
00087   is_primed= false;
00088 
00089   return true;
00090 }