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