Drizzled Public API Documentation

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 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 
00024 using namespace std;
00025 using namespace drizzled;
00026 
00027 
00028 ColumnsTool::ColumnsTool() :
00029   DataDictionary("COLUMNS")
00030 {
00031   add_field("TABLE_SCHEMA");
00032   add_field("TABLE_NAME");
00033 
00034   add_field("COLUMN_NAME");
00035   add_field("COLUMN_TYPE");
00036   add_field("ORDINAL_POSITION", plugin::TableFunction::NUMBER, 0, false);
00037   add_field("COLUMN_DEFAULT", plugin::TableFunction::VARBINARY, 65535, true);
00038   add_field("COLUMN_DEFAULT_IS_NULL", plugin::TableFunction::BOOLEAN, 0, false);
00039   add_field("COLUMN_DEFAULT_UPDATE");
00040   add_field("IS_SIGNED", plugin::TableFunction::BOOLEAN, 0, true);
00041   add_field("IS_AUTO_INCREMENT", plugin::TableFunction::BOOLEAN, 0, false);
00042   add_field("IS_NULLABLE", plugin::TableFunction::BOOLEAN, 0, false);
00043   add_field("IS_INDEXED", plugin::TableFunction::BOOLEAN, 0, false);
00044   add_field("IS_USED_IN_PRIMARY", plugin::TableFunction::BOOLEAN, 0, false);
00045   add_field("IS_UNIQUE", plugin::TableFunction::BOOLEAN, 0, false);
00046   add_field("IS_MULTI", plugin::TableFunction::BOOLEAN, 0, false);
00047   add_field("IS_FIRST_IN_MULTI", plugin::TableFunction::BOOLEAN, 0, false);
00048   add_field("INDEXES_FOUND_IN", plugin::TableFunction::NUMBER, 0, false);
00049   add_field("DATA_TYPE");
00050   add_field("DATA_ARCHETYPE");
00051   add_field("CHARACTER_MAXIMUM_LENGTH", plugin::TableFunction::NUMBER);
00052   add_field("CHARACTER_OCTET_LENGTH", plugin::TableFunction::NUMBER);
00053   add_field("NUMERIC_PRECISION", plugin::TableFunction::NUMBER);
00054   add_field("NUMERIC_SCALE", plugin::TableFunction::NUMBER);
00055 
00056   add_field("ENUM_VALUES", plugin::TableFunction::STRING, 1024, true);
00057 
00058   add_field("COLLATION_NAME");
00059 
00060   add_field("COLUMN_COMMENT", plugin::TableFunction::STRING, 1024, true);
00061 }
00062 
00063 
00064 ColumnsTool::Generator::Generator(Field **arg) :
00065   DataDictionary::Generator(arg),
00066   field_generator(getSession())
00067 {
00068 }
00069 
00070 bool ColumnsTool::Generator::populate()
00071 {
00072   drizzled::generator::FieldPair field_pair;
00073 
00074   while (!!(field_pair= field_generator))
00075   {
00076     const drizzled::message::Table *table_message= field_pair.first;
00077     int32_t field_iterator= field_pair.second;
00078     const message::Table::Field &column(table_message->field(field_pair.second));
00079 
00080     /* TABLE_SCHEMA */
00081     push(table_message->schema());
00082 
00083     /* TABLE_NAME */
00084     push(table_message->name());
00085 
00086     /* COLUMN_NAME */
00087     push(column.name());
00088 
00089     /* COLUMN_TYPE */
00090     push(drizzled::message::type(column.type()));
00091 
00092     /* ORDINAL_POSITION */
00093     push(static_cast<int64_t>(field_iterator));
00094 
00095     /* COLUMN_DEFAULT */
00096     if (column.options().has_default_value())
00097     {
00098       push(column.options().default_value());
00099     }
00100     else if (column.options().has_default_bin_value())
00101     {
00102       push(column.options().default_bin_value().c_str(), column.options().default_bin_value().length());
00103     }
00104     else if (column.options().has_default_expression())
00105     {
00106       push(column.options().default_expression());
00107     }
00108     else
00109     {
00110       push();
00111     }
00112 
00113     /* COLUMN_DEFAULT_IS_NULL */
00114     push(column.options().default_null());
00115 
00116     /* COLUMN_DEFAULT_UPDATE */
00117     push(column.options().update_expression());
00118 
00119     /* IS_SIGNED */
00120     if (drizzled::message::is_numeric(column))
00121     {
00122       push(true);
00123     }
00124     else 
00125     {
00126       push();
00127     }
00128 
00129     /* IS_AUTO_INCREMENT */
00130     push(column.numeric_options().is_autoincrement());
00131 
00132     /* IS_NULLABLE */
00133     push(not column.constraints().is_notnull());
00134 
00135     /* IS_INDEXED, IS_USED_IN_PRIMARY, IS_UNIQUE, IS_MULTI, IS_FIRST_IN_MULTI, INDEXES_FOUND_IN */
00136     bool is_indexed= false;
00137     bool is_primary= false;
00138     bool is_unique= false;
00139     bool is_multi= false;
00140     bool is_multi_first= false;
00141     int64_t indexes_found_in= 0;
00142     for (int32_t x= 0; x < table_message->indexes_size() ; x++)
00143     {
00144       const drizzled::message::Table::Index &index(table_message->indexes(x));
00145 
00146       for (int32_t y= 0; y < index.index_part_size() ; y++)
00147       {
00148         const drizzled::message::Table::Index::IndexPart &index_part(index.index_part(y));
00149 
00150         if (static_cast<int32_t>(index_part.fieldnr()) == field_iterator)
00151         {
00152           indexes_found_in++;
00153           is_indexed= true;
00154 
00155           if (index.is_primary())
00156             is_primary= true;
00157 
00158           if (index.is_unique())
00159             is_unique= true;
00160 
00161           if (index.index_part_size() > 1)
00162           {
00163             is_multi= true;
00164 
00165             if (y == 0)
00166               is_multi_first= true;
00167           }
00168         }
00169       }
00170     }
00171     /* ...IS_INDEXED, IS_USED_IN_PRIMARY, IS_UNIQUE, IS_MULTI, IS_FIRST_IN_MULTI, INDEXES_FOUND_IN */
00172     push(is_indexed);
00173     push(is_primary);
00174     push(is_unique);
00175     push(is_multi);
00176     push(is_multi_first);
00177     push(indexes_found_in);
00178 
00179     /* DATA_TYPE <-- display the type that the user is going to expect, which is not the same as the type we store internally */
00180     push(drizzled::message::type(column));
00181 
00182     /* DATA_ARCHETYPE */
00183     push(drizzled::message::type(column.type()));
00184 
00185     /* "CHARACTER_MAXIMUM_LENGTH" */
00186     push(static_cast<int64_t>(column.string_options().length()));
00187 
00188     /* "CHARACTER_OCTET_LENGTH" */
00189     push(static_cast<int64_t>(column.string_options().length()) * 4);
00190 
00191     /* "NUMERIC_PRECISION" */
00192     push(static_cast<int64_t>(column.numeric_options().precision()));
00193 
00194     /* "NUMERIC_SCALE" */
00195     push(static_cast<int64_t>(column.numeric_options().scale()));
00196 
00197     /* "ENUM_VALUES" */
00198     if (column.type() == drizzled::message::Table::Field::ENUM)
00199     {
00200       string destination;
00201       size_t num_field_values= column.enumeration_values().field_value_size();
00202       for (size_t x= 0; x < num_field_values; ++x)
00203       {
00204         const string &type= column.enumeration_values().field_value(x);
00205 
00206         if (x != 0)
00207           destination.push_back(',');
00208 
00209         destination.push_back('\'');
00210         destination.append(type);
00211         destination.push_back('\'');
00212       }
00213       push(destination);
00214     }
00215     else
00216     {
00217       push();
00218     }
00219 
00220     /* "COLLATION_NAME" */
00221     push(column.string_options().collation());
00222 
00223     /* "COLUMN_COMMENT" */
00224     if (column.has_comment())
00225     {
00226       push(column.comment());
00227     }
00228     else
00229     {
00230       push();
00231     }
00232 
00233     return true;
00234   }
00235 
00236   return false;
00237 }