Drizzled Public API Documentation

show_columns.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 <string>
00025 
00026 using namespace std;
00027 using namespace drizzled;
00028 
00029 ShowColumns::ShowColumns() :
00030   show_dictionary::Show("SHOW_COLUMNS")
00031 {
00032   add_field("Field");
00033   add_field("Type");
00034   add_field("Null", plugin::TableFunction::BOOLEAN, 0 , false);
00035   add_field("Default");
00036   add_field("Default_is_NULL", plugin::TableFunction::BOOLEAN, 0, false);
00037   add_field("On_Update");
00038 }
00039 
00040 ShowColumns::Generator::Generator(Field **arg) :
00041   show_dictionary::Show::Generator(arg),
00042   is_tables_primed(false),
00043   is_columns_primed(false),
00044   column_iterator(0)
00045 {
00046   if (not isShowQuery())
00047    return;
00048 
00049   statement::Show& select= static_cast<statement::Show&>(statement());
00050 
00051   if (not select.getShowTable().empty() && not select.getShowSchema().empty())
00052   {
00053     table_name.append(select.getShowTable().c_str());
00054     identifier::Table identifier(select.getShowSchema().c_str(), select.getShowTable().c_str());
00055 
00056     if (not plugin::Authorization::isAuthorized(*getSession().user(),
00057                                             identifier, false))
00058     {
00059       drizzled::error::access(*getSession().user(), identifier);
00060       return;
00061     }
00062 
00063     table_proto= plugin::StorageEngine::getTableMessage(getSession(), identifier);
00064 
00065     if (table_proto)
00066       is_tables_primed= true;
00067   }
00068 }
00069 
00070 bool ShowColumns::Generator::nextColumnCore()
00071 {
00072   if (is_columns_primed)
00073   {
00074     column_iterator++;
00075   }
00076   else
00077   {
00078     if (not isTablesPrimed())
00079       return false;
00080 
00081     column_iterator= 0;
00082     is_columns_primed= true;
00083   }
00084 
00085   if (column_iterator >= getTableProto()->field_size())
00086     return false;
00087 
00088   column= getTableProto()->field(column_iterator);
00089 
00090   return true;
00091 }
00092 
00093 
00094 bool ShowColumns::Generator::nextColumn()
00095 {
00096   while (not nextColumnCore())
00097   {
00098     return false;
00099   }
00100 
00101   return true;
00102 }
00103 
00104 bool ShowColumns::Generator::populate()
00105 {
00106 
00107   if (not nextColumn())
00108     return false;
00109 
00110   fill();
00111 
00112   return true;
00113 }
00114 
00115 
00116 void ShowColumns::Generator::fill()
00117 {
00118   /* Field */
00119   push(column.name());
00120 
00121   /* Type */
00122   push(drizzled::message::type(column));
00123 
00124   /* Null */
00125   push(not column.constraints().is_notnull());
00126 
00127   /* Default */
00128   if (column.options().has_default_value())
00129     push(column.options().default_value());
00130   else if (column.options().has_default_expression())
00131     push(column.options().default_expression());
00132   else
00133     push(column.options().default_bin_value());
00134 
00135   /* Default_is_NULL */
00136   push(column.options().default_null());
00137 
00138   /* On_Update */
00139   push(column.options().update_expression());
00140 }