Drizzled Public API Documentation

key_column_usage.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/information_schema_dictionary/dictionary.h>
00023 
00024 using namespace std;
00025 using namespace drizzled;
00026 
00027 KeyColumnUsage::KeyColumnUsage() :
00028   InformationSchema("KEY_COLUMN_USAGE")
00029 {
00030   add_field("CONSTRAINT_CATALOG", plugin::TableFunction::STRING, MAXIMUM_IDENTIFIER_LENGTH, false);
00031   add_field("CONSTRAINT_SCHEMA", plugin::TableFunction::STRING, MAXIMUM_IDENTIFIER_LENGTH, false);
00032   add_field("CONSTRAINT_NAME", plugin::TableFunction::STRING, MAXIMUM_IDENTIFIER_LENGTH, false);
00033   add_field("TABLE_CATALOG", plugin::TableFunction::STRING, MAXIMUM_IDENTIFIER_LENGTH, false);
00034   add_field("TABLE_SCHEMA", plugin::TableFunction::STRING, MAXIMUM_IDENTIFIER_LENGTH, false);
00035   add_field("TABLE_NAME", plugin::TableFunction::STRING, MAXIMUM_IDENTIFIER_LENGTH, false);
00036   add_field("COLUMN_NAME", plugin::TableFunction::STRING, MAXIMUM_IDENTIFIER_LENGTH, false);
00037   add_field("ORDINAL_POSITION", plugin::TableFunction::NUMBER, 0, false);
00038 }
00039 
00040 KeyColumnUsage::Generator::Generator(drizzled::Field **arg) :
00041   InformationSchema::Generator(arg),
00042   generator(getSession()),
00043   index_iterator(0),
00044   index_part_iterator(0),
00045   fk_constraint_iterator(0),
00046   fk_constraint_column_name_iterator(0)
00047 {
00048   while (not (table_message= generator))
00049   { };
00050 }
00051 
00052 bool KeyColumnUsage::Generator::populate()
00053 {
00054   if (not table_message)
00055     return false;
00056 
00057   do 
00058   {
00059     if (index_iterator != table_message->indexes_size())
00060     {
00061       drizzled::message::Table::Index index= table_message->indexes(index_iterator);
00062 
00063       if (index.is_primary() || index.is_unique())
00064       {
00065         /* Constraints live in the same catalog.schema as the table they refer too. */
00066         /* CONSTRAINT_CATALOG */
00067         push(table_message->catalog());
00068 
00069         /* CONSTRAINT_SCHEMA */
00070         push(table_message->schema());
00071 
00072         /* CONSTRAINT_NAME */
00073         push(index.name());
00074 
00075         /* TABLE_CATALOG */
00076         push(table_message->catalog());
00077 
00078         /* TABLE_SCHEMA */
00079         push(table_message->schema());
00080 
00081         /* TABLE_NAME */
00082         push(table_message->name());
00083 
00084         /* COLUMN_NAME */
00085         int32_t fieldnr= index.index_part(index_part_iterator).fieldnr();
00086         push(table_message->field(fieldnr).name());
00087 
00088         /* ORDINAL_POSITION */
00089         push(static_cast<int64_t>((index_part_iterator +1)));
00090 
00091         if (index_part_iterator == index.index_part_size() -1)
00092         {
00093           index_iterator++;
00094           index_part_iterator= 0;
00095         }
00096         else
00097         {
00098           index_part_iterator++;
00099         }
00100 
00101         return true;
00102       }
00103     }
00104 
00105     if (fk_constraint_iterator != table_message->fk_constraint_size())
00106     {
00107       drizzled::message::Table::ForeignKeyConstraint foreign_key= table_message->fk_constraint(fk_constraint_iterator);
00108 
00109       {
00110         /* Constraints live in the same catalog.schema as the table they refer too. */
00111         /* CONSTRAINT_CATALOG */
00112         push(table_message->catalog());
00113 
00114         /* CONSTRAINT_SCHEMA */
00115         push(table_message->schema());
00116 
00117         /* CONSTRAINT_NAME */
00118         push(foreign_key.name());
00119 
00120         /* TABLE_CATALOG */
00121         push(table_message->catalog());
00122 
00123         /* TABLE_SCHEMA */
00124         push(table_message->schema());
00125 
00126         /* TABLE_NAME */
00127         push(table_message->name());
00128 
00129         /* COLUMN_NAME */
00130         push(foreign_key.column_names(fk_constraint_column_name_iterator));
00131 
00132         /* ORDINAL_POSITION */
00133         push(static_cast<int64_t>((fk_constraint_column_name_iterator + 1)));
00134 
00135         if (fk_constraint_column_name_iterator == foreign_key.column_names_size() -1)
00136         {
00137           fk_constraint_iterator++;
00138           fk_constraint_column_name_iterator= 0;
00139         }
00140         else
00141         {
00142           fk_constraint_column_name_iterator++;
00143         }
00144 
00145         return true;
00146       }
00147     }
00148 
00149     index_iterator= 0;
00150     index_part_iterator= 0;
00151 
00152     fk_constraint_iterator= 0;
00153     fk_constraint_column_name_iterator= 0;
00154 
00155   } while ((table_message= generator));
00156 
00157   return false;
00158 }