Drizzled Public API Documentation

foreign_keys.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  *  Copyright (C) 2010 Andrew Hutchings
00006  *
00007  *  This program is free software; you can redistribute it and/or modify
00008  *  it under the terms of the GNU General Public License as published by
00009  *  the Free Software Foundation; either version 2 of the License, or
00010  *  (at your option) any later version.
00011  *
00012  *  This program is distributed in the hope that it will be useful,
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *  GNU General Public License for more details.
00016  *
00017  *  You should have received a copy of the GNU General Public License
00018  *  along with this program; if not, write to the Free Software
00019  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020  */
00021 
00022 #include <config.h>
00023 #include <plugin/schema_dictionary/dictionary.h>
00024 
00025 using namespace std;
00026 using namespace drizzled;
00027 
00028 ForeignKeysTool::ForeignKeysTool() :
00029   plugin::TableFunction("DATA_DICTIONARY", "FOREIGN_KEYS")
00030 {
00031   add_field("CONSTRAINT_SCHEMA");
00032   add_field("CONSTRAINT_TABLE");
00033   add_field("CONSTRAINT_NAME");
00034   add_field("CONSTRAINT_COLUMNS");
00035 
00036   add_field("REFERENCED_TABLE_NAME");
00037   add_field("REFERENCED_TABLE_COLUMNS");
00038 
00039   add_field("MATCH_OPTION");
00040   add_field("UPDATE_RULE");
00041   add_field("DELETE_RULE");
00042 }
00043 
00044 ForeignKeysTool::Generator::Generator(Field **arg) :
00045   plugin::TableFunction::Generator(arg),
00046   all_tables_generator(getSession()),
00047   keyPos(0),
00048   firstFill(true)
00049 {
00050 }
00051 
00052 bool ForeignKeysTool::Generator::nextTable()
00053 {
00054   drizzled::message::table::shared_ptr table_ptr;
00055   while ((table_ptr= all_tables_generator))
00056   {
00057     table_message.CopyFrom(*table_ptr);
00058     return true;
00059   }
00060 
00061   return false;
00062 }
00063 
00064 bool ForeignKeysTool::Generator::fillFkey()
00065 {
00066   if (firstFill)
00067   {
00068     firstFill= false;
00069     nextTable();
00070   }
00071 
00072   while(true)
00073   {
00074     if (keyPos < getTableMessage().fk_constraint_size())
00075     { 
00076       fkey= getTableMessage().fk_constraint(keyPos);
00077       keyPos++;
00078       fill();
00079       return true;
00080     }
00081     else if (nextTable())
00082     {
00083       keyPos= 0;
00084     }
00085     else
00086       return false;
00087   }
00088 }
00089 
00090 bool ForeignKeysTool::Generator::populate()
00091 {
00092   if (fillFkey())
00093     return true;
00094 
00095   return false;
00096 }
00097 
00098 void ForeignKeysTool::Generator::fill()
00099 {
00100   /* CONSTRAINT_SCHEMA */
00101   push(getTableMessage().schema());
00102 
00103   /* CONSTRAINT_TABLE */
00104   push(getTableMessage().name());
00105 
00106   /* CONSTRAINT_NAME */
00107   push(fkey.name());
00108 
00109   /* CONSTRAINT_COLUMNS */
00110   std::string source;
00111 
00112   for (ssize_t x= 0; x < fkey.column_names_size(); ++x)
00113   {
00114     if (x != 0)
00115       source.append(", ");
00116     source.push_back('`');
00117     source.append(fkey.column_names(x));
00118     source.push_back('`');
00119   }
00120 
00121   push(source);
00122 
00123   /* REFERENCED_TABLE_NAME */
00124   push(fkey.references_table_name());
00125 
00126   /* REFERENCED_TABLE_COLUMNS */
00127   std::string destination;
00128 
00129   for (ssize_t x= 0; x < fkey.references_columns_size(); ++x)
00130   {
00131     if (x != 0)
00132       destination.append(", ");
00133     destination.push_back('`'); 
00134     destination.append(fkey.references_columns(x));
00135     destination.push_back('`');
00136   }
00137 
00138   push(destination);
00139 
00140   /* MATCH_OPTION */
00141   push(drizzled::message::type(fkey.match()));
00142 
00143   /* UPDATE_RULE */
00144   push(drizzled::message::type(fkey.update_option()));
00145 
00146   /* DELETE_RULE */
00147   push(drizzled::message::type(fkey.delete_option()));
00148 
00149 }