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 static const string VARCHAR("VARCHAR");
00030 /* VARBINARY already defined elsewhere */
00031 static const string VARBIN("VARBINARY");
00032 static const string DOUBLE("DOUBLE");
00033 static const string BLOB("BLOB");
00034 static const string TEXT("TEXT");
00035 static const string ENUM("ENUM");
00036 static const string INTEGER("INTEGER");
00037 static const string BIGINT("BIGINT");
00038 static const string DECIMAL("DECIMAL");
00039 static const string DATE("DATE");
00040 static const string TIMESTAMP("TIMESTAMP");
00041 static const string DATETIME("DATETIME");
00042 
00043 ShowColumns::ShowColumns() :
00044   show_dictionary::Show("SHOW_COLUMNS")
00045 {
00046   add_field("Field");
00047   add_field("Type");
00048   add_field("Null", plugin::TableFunction::BOOLEAN, 0 , false);
00049   add_field("Default");
00050   add_field("Default_is_NULL", plugin::TableFunction::BOOLEAN, 0, false);
00051   add_field("On_Update");
00052 }
00053 
00054 ShowColumns::Generator::Generator(Field **arg) :
00055   show_dictionary::Show::Generator(arg),
00056   is_tables_primed(false),
00057   is_columns_primed(false),
00058   column_iterator(0)
00059 {
00060   if (not isShowQuery())
00061    return;
00062 
00063   statement::Show& select= static_cast<statement::Show&>(statement());
00064 
00065   if (not select.getShowTable().empty() && not select.getShowSchema().empty())
00066   {
00067     table_name.append(select.getShowTable().c_str());
00068     identifier::Table identifier(select.getShowSchema().c_str(), select.getShowTable().c_str());
00069 
00070     if (not plugin::Authorization::isAuthorized(*getSession().user(),
00071                                             identifier, false))
00072     {
00073       drizzled::error::access(*getSession().user(), identifier);
00074       return;
00075     }
00076 
00077     table_proto= plugin::StorageEngine::getTableMessage(getSession(), identifier);
00078 
00079     if (table_proto)
00080       is_tables_primed= true;
00081   }
00082 }
00083 
00084 bool ShowColumns::Generator::nextColumnCore()
00085 {
00086   if (is_columns_primed)
00087   {
00088     column_iterator++;
00089   }
00090   else
00091   {
00092     if (not isTablesPrimed())
00093       return false;
00094 
00095     column_iterator= 0;
00096     is_columns_primed= true;
00097   }
00098 
00099   if (column_iterator >= getTableProto()->field_size())
00100     return false;
00101 
00102   column= getTableProto()->field(column_iterator);
00103 
00104   return true;
00105 }
00106 
00107 
00108 bool ShowColumns::Generator::nextColumn()
00109 {
00110   while (not nextColumnCore())
00111   {
00112     return false;
00113   }
00114 
00115   return true;
00116 }
00117 
00118 bool ShowColumns::Generator::populate()
00119 {
00120 
00121   if (not nextColumn())
00122     return false;
00123 
00124   fill();
00125 
00126   return true;
00127 }
00128 
00129 void ShowColumns::Generator::pushType(message::Table::Field::FieldType type, const string collation)
00130 {
00131   switch (type)
00132   {
00133   default:
00134   case message::Table::Field::VARCHAR:
00135     push(collation.compare("binary") ? VARCHAR : VARBIN);
00136     break;
00137   case message::Table::Field::DOUBLE:
00138     push(DOUBLE);
00139     break;
00140   case message::Table::Field::BLOB:
00141     push(collation.compare("binary") ? TEXT : BLOB);
00142     break;
00143   case message::Table::Field::ENUM:
00144     push(ENUM);
00145     break;
00146   case message::Table::Field::INTEGER:
00147     push(INTEGER);
00148     break;
00149   case message::Table::Field::BIGINT:
00150     push(BIGINT);
00151     break;
00152   case message::Table::Field::DECIMAL:
00153     push(DECIMAL);
00154     break;
00155   case message::Table::Field::DATE:
00156     push(DATE);
00157     break;
00158   case message::Table::Field::EPOCH:
00159     push(TIMESTAMP);
00160     break;
00161   case message::Table::Field::DATETIME:
00162     push(DATETIME);
00163     break;
00164   }
00165 }
00166 
00167 
00168 void ShowColumns::Generator::fill()
00169 {
00170   /* Field */
00171   push(column.name());
00172 
00173   /* Type */
00174   pushType(column.type(), column.string_options().collation());
00175 
00176   /* Null */
00177   push(not column.constraints().is_notnull());
00178 
00179   /* Default */
00180   if (column.options().has_default_value())
00181     push(column.options().default_value());
00182   else if (column.options().has_default_expression())
00183     push(column.options().default_expression());
00184   else
00185     push(column.options().default_bin_value());
00186 
00187   /* Default_is_NULL */
00188   push(column.options().default_null());
00189 
00190   /* On_Update */
00191   push(column.options().update_expression());
00192 }