Drizzled Public API Documentation

tables.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 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 #include <plugin/schema_dictionary/dictionary.h>
00023 #include <drizzled/identifier.h>
00024 #include <drizzled/table_proto.h>
00025 
00026 using namespace std;
00027 using namespace drizzled;
00028 
00029 static const string STANDARD("STANDARD");
00030 static const string TEMPORARY("TEMPORARY");
00031 static const string INTERNAL("INTERNAL");
00032 static const string FUNCTION("FUNCTION");
00033 
00034 
00035 static const string VARCHAR("VARCHAR");
00036 static const string DOUBLE("DOUBLE");
00037 static const string BLOB("BLOB");
00038 static const string ENUM("ENUM");
00039 static const string INTEGER("INTEGER");
00040 static const string BIGINT("BIGINT");
00041 static const string DECIMAL("DECIMAL");
00042 static const string DATE("DATE");
00043 static const string TIMESTAMP("TIMESTAMP");
00044 static const string DATETIME("DATETIME");
00045 
00046 TablesTool::TablesTool() :
00047   plugin::TableFunction("DATA_DICTIONARY", "TABLES")
00048 {
00049   add_field("TABLE_SCHEMA", plugin::TableFunction::STRING, MAXIMUM_IDENTIFIER_LENGTH, false);
00050   add_field("TABLE_NAME", plugin::TableFunction::STRING, MAXIMUM_IDENTIFIER_LENGTH, false);
00051   add_field("TABLE_TYPE");
00052   add_field("TABLE_ARCHETYPE");
00053   add_field("ENGINE");
00054   add_field("ROW_FORMAT", 10);
00055   add_field("TABLE_COLLATION");
00056   add_field("TABLE_CREATION_TIME");
00057   add_field("TABLE_UPDATE_TIME");
00058   add_field("TABLE_COMMENT", plugin::TableFunction::STRING,
00059             TABLE_COMMENT_MAXLEN, true);
00060   add_field("AUTO_INCREMENT", plugin::TableFunction::NUMBER, 0, false);
00061   add_field("TABLE_UUID", plugin::TableFunction::STRING, 36, true);
00062   add_field("TABLE_VERSION", plugin::TableFunction::NUMBER, 0, true);
00063   add_field("IS_REPLICATED", plugin::TableFunction::BOOLEAN, 0, false);
00064   add_field("TABLE_DEFINER", plugin::TableFunction::STRING, 64, true);
00065 }
00066 
00067 TablesTool::Generator::Generator(Field **arg) :
00068   plugin::TableFunction::Generator(arg),
00069   all_tables_generator(getSession())
00070 {
00071 }
00072 
00073 bool TablesTool::Generator::nextTable()
00074 {
00075   drizzled::message::table::shared_ptr table_ptr;
00076   while ((table_ptr= all_tables_generator))
00077   {
00078     table_message.CopyFrom(*table_ptr);
00079     return true;
00080   }
00081 
00082   return false;
00083 }
00084 
00085 bool TablesTool::Generator::populate()
00086 {
00087   if (nextTable())
00088   {
00089     fill();
00090     return true;
00091   }
00092 
00093   return false;
00094 }
00095 
00096 void TablesTool::Generator::fill()
00097 {
00098 
00103   /* TABLE_SCHEMA */
00104   push(getTableMessage().schema());
00105 
00106   /* TABLE_NAME */
00107   push(getTableMessage().name());
00108 
00109   /* TABLE_TYPE */
00110   if (drizzled::identifier::Table::isView(getTableMessage().type()))
00111   {
00112     push("VIEW");
00113   }
00114   else
00115   {
00116     push("BASE");
00117   }
00118 
00119   /* TABLE_ARCHETYPE */
00120   {
00121     switch (getTableMessage().type())
00122     {
00123     default:
00124     case message::Table::STANDARD:
00125       push(STANDARD);
00126       break;
00127     case message::Table::TEMPORARY:
00128       push(TEMPORARY);
00129       break;
00130     case message::Table::INTERNAL:
00131       push(INTERNAL);
00132       break;
00133     case message::Table::FUNCTION:
00134       push(FUNCTION);
00135       break;
00136     }
00137   }
00138 
00139   /* ENGINE */
00140   const drizzled::message::Engine &engine= getTableMessage().engine();
00141   push(engine.name());
00142 
00143   /* ROW_FORMAT */
00144   bool row_format_sent= false;
00145   for (ssize_t it= 0; it < engine.options_size(); it++)
00146   {
00147     const drizzled::message::Engine::Option &opt= engine.options(it);
00148     if (opt.name().compare("ROW_FORMAT") == 0)
00149     {
00150       row_format_sent= true;
00151       push(opt.state());
00152       break;
00153     }
00154   }
00155 
00156   if (not row_format_sent)
00157     push("DEFAULT");
00158 
00159   /* TABLE_COLLATION */
00160   push(getTableMessage().options().collation());
00161 
00162   /* TABLE_CREATION_TIME */
00163   time_t time_arg= getTableMessage().creation_timestamp();
00164   char buffer[40];
00165   struct tm tm_buffer;
00166 
00167   localtime_r(&time_arg, &tm_buffer);
00168   strftime(buffer, sizeof(buffer), "%a %b %d %H:%M:%S %Y", &tm_buffer);
00169   push(buffer);
00170 
00171   /* TABLE_UPDATE_TIME */
00172   time_arg= getTableMessage().update_timestamp();
00173   localtime_r(&time_arg, &tm_buffer);
00174   strftime(buffer, sizeof(buffer), "%a %b %d %H:%M:%S %Y", &tm_buffer);
00175   push(buffer);
00176 
00177   /* TABLE_COMMENT */
00178   if (getTableMessage().options().has_comment())
00179   {
00180     push(getTableMessage().options().comment());
00181   }
00182   else
00183   {
00184     push();
00185   }
00186 
00187   /* AUTO_INCREMENT */
00188   push(getTableMessage().options().auto_increment_value());
00189 
00190   /* TABLE_UUID */
00191   push(getTableMessage().uuid());
00192 
00193   /* TABLE_VERSION */
00194   push(getTableMessage().version());
00195 
00196   /* IS_REPLICATED */
00197   push(message::is_replicated(getTableMessage()));
00198 
00199   /* _DEFINER */
00200   if (message::has_definer(getTableMessage()))
00201   {
00202     push(message::definer(getTableMessage()));
00203   }
00204   else
00205   {
00206     push();
00207   }
00208 }