Drizzled Public API Documentation

table_constraints.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 TableConstraints::TableConstraints() :
00028   InformationSchema("TABLE_CONSTRAINTS")
00029 {
00030   add_field("CONSTRAINT_CATALOG");
00031   add_field("CONSTRAINT_SCHEMA");
00032   add_field("CONSTRAINT_NAME");
00033   add_field("TABLE_CATALOG");
00034   add_field("TABLE_SCHEMA");
00035   add_field("TABLE_NAME");
00036   add_field("CONSTRAINT_TYPE");
00037   add_field("IS_DEFERRABLE", plugin::TableFunction::BOOLEAN, 0, false);
00038   add_field("INITIALLY_DEFERRED", plugin::TableFunction::BOOLEAN, 0, false);
00039 }
00040 
00041 TableConstraints::Generator::Generator(drizzled::Field **arg) :
00042   InformationSchema::Generator(arg),
00043   generator(getSession()),
00044   index_iterator(0)
00045 {
00046   while (not (table_message= generator))
00047   { };
00048 }
00049 
00050 bool TableConstraints::Generator::populate()
00051 {
00052   if (not table_message)
00053     return false;
00054 
00055   do 
00056   {
00057     if (index_iterator != table_message->indexes_size())
00058     {
00059       drizzled::message::Table::Index index= table_message->indexes(index_iterator);
00060 
00061       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         /* CONSTRAINT_TYPE */
00085         if (index.is_primary())
00086         {
00087           push("PRIMARY KEY");
00088         }
00089         else if (index.is_unique())
00090         {
00091           push("UNIQUE");
00092         }
00093         else
00094         {
00095           assert(0);
00096           push("UNIQUE");
00097         }
00098 
00099         /* IS_DEFERRABLE */
00100         push(false);
00101 
00102         /* INITIALLY_DEFERRED */
00103         push(false);
00104 
00105         return true;
00106       }
00107     }
00108 
00109     index_iterator= 0;
00110 
00111   } while ((table_message= generator));
00112 
00113   return false;
00114 }