drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
foreign_keys.cc
1/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2010 Sun Microsystems, Inc.
5 * Copyright (C) 2010 Andrew Hutchings
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include <config.h>
23#include <plugin/schema_dictionary/dictionary.h>
24
25using namespace std;
26using namespace drizzled;
27
28ForeignKeysTool::ForeignKeysTool() :
29 plugin::TableFunction("DATA_DICTIONARY", "FOREIGN_KEYS")
30{
31 add_field("CONSTRAINT_SCHEMA", plugin::TableFunction::STRING, MAXIMUM_IDENTIFIER_LENGTH, false);
32 add_field("CONSTRAINT_TABLE", plugin::TableFunction::STRING, MAXIMUM_IDENTIFIER_LENGTH, false);
33 add_field("CONSTRAINT_NAME");
34 add_field("CONSTRAINT_COLUMNS");
35
36 add_field("REFERENCED_TABLE_NAME");
37 add_field("REFERENCED_TABLE_COLUMNS");
38
39 add_field("MATCH_OPTION");
40 add_field("UPDATE_RULE");
41 add_field("DELETE_RULE");
42}
43
44ForeignKeysTool::Generator::Generator(Field **arg) :
45 plugin::TableFunction::Generator(arg),
46 all_tables_generator(getSession()),
47 keyPos(0),
48 firstFill(true)
49{
50}
51
52bool ForeignKeysTool::Generator::nextTable()
53{
54 drizzled::message::table::shared_ptr table_ptr;
55 while ((table_ptr= all_tables_generator))
56 {
57 table_message.CopyFrom(*table_ptr);
58 return true;
59 }
60
61 return false;
62}
63
64bool ForeignKeysTool::Generator::fillFkey()
65{
66 if (firstFill)
67 {
68 firstFill= false;
69 nextTable();
70 }
71
72 while(true)
73 {
74 if (keyPos < getTableMessage().fk_constraint_size())
75 {
76 fkey= getTableMessage().fk_constraint(keyPos);
77 keyPos++;
78 fill();
79 return true;
80 }
81 else if (nextTable())
82 {
83 keyPos= 0;
84 }
85 else
86 return false;
87 }
88}
89
90bool ForeignKeysTool::Generator::populate()
91{
92 if (fillFkey())
93 return true;
94
95 return false;
96}
97
98void ForeignKeysTool::Generator::fill()
99{
100 /* CONSTRAINT_SCHEMA */
101 push(getTableMessage().schema());
102
103 /* CONSTRAINT_TABLE */
104 push(getTableMessage().name());
105
106 /* CONSTRAINT_NAME */
107 push(fkey.name());
108
109 /* CONSTRAINT_COLUMNS */
110 std::string source;
111
112 for (ssize_t x= 0; x < fkey.column_names_size(); ++x)
113 {
114 if (x != 0)
115 source.append(", ");
116 source.push_back('`');
117 source.append(fkey.column_names(x));
118 source.push_back('`');
119 }
120
121 push(source);
122
123 /* REFERENCED_TABLE_NAME */
124 push(fkey.references_table_name());
125
126 /* REFERENCED_TABLE_COLUMNS */
127 std::string destination;
128
129 for (ssize_t x= 0; x < fkey.references_columns_size(); ++x)
130 {
131 if (x != 0)
132 destination.append(", ");
133 destination.push_back('`');
134 destination.append(fkey.references_columns(x));
135 destination.push_back('`');
136 }
137
138 push(destination);
139
140 /* MATCH_OPTION */
141 push(drizzled::message::type(fkey.match()));
142
143 /* UPDATE_RULE */
144 push(drizzled::message::type(fkey.update_option()));
145
146 /* DELETE_RULE */
147 push(drizzled::message::type(fkey.delete_option()));
148
149}
TODO: Rename this file - func.h is stupid.
STL namespace.