00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
00101 push(getTableMessage().schema());
00102
00103
00104 push(getTableMessage().name());
00105
00106
00107 push(fkey.name());
00108
00109
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
00124 push(fkey.references_table_name());
00125
00126
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
00141 push(drizzled::message::type(fkey.match()));
00142
00143
00144 push(drizzled::message::type(fkey.update_option()));
00145
00146
00147 push(drizzled::message::type(fkey.delete_option()));
00148
00149 }