~drizzle-trunk/drizzle/development

1340.1.5 by Brian Aker
Update, we now have all of the ANSI INFORMATION_SCHEMA listed.
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
4
 *  Copyright (C) 2010 Brian Aker
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; either version 2 of the License, or
9
 *  (at your option) any later version.
10
 *
11
 *  This program is distributed in the hope that it will be useful,
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *  GNU General Public License for more details.
15
 *
16
 *  You should have received a copy of the GNU General Public License
17
 *  along with this program; if not, write to the Free Software
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
 */
20
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
21
#include <config.h>
22
#include <plugin/information_schema_dictionary/dictionary.h>
1340.1.5 by Brian Aker
Update, we now have all of the ANSI INFORMATION_SCHEMA listed.
23
24
using namespace std;
25
using namespace drizzled;
26
27
KeyColumnUsage::KeyColumnUsage() :
28
  InformationSchema("KEY_COLUMN_USAGE")
29
{
1652.1.1 by Brian Aker
Added key_column_usage
30
  add_field("CONSTRAINT_CATALOG", plugin::TableFunction::STRING, MAXIMUM_IDENTIFIER_LENGTH, false);
31
  add_field("CONSTRAINT_SCHEMA", plugin::TableFunction::STRING, MAXIMUM_IDENTIFIER_LENGTH, false);
32
  add_field("CONSTRAINT_NAME", plugin::TableFunction::STRING, MAXIMUM_IDENTIFIER_LENGTH, false);
33
  add_field("TABLE_CATALOG", plugin::TableFunction::STRING, MAXIMUM_IDENTIFIER_LENGTH, false);
34
  add_field("TABLE_SCHEMA", plugin::TableFunction::STRING, MAXIMUM_IDENTIFIER_LENGTH, false);
35
  add_field("TABLE_NAME", plugin::TableFunction::STRING, MAXIMUM_IDENTIFIER_LENGTH, false);
36
  add_field("COLUMN_NAME", plugin::TableFunction::STRING, MAXIMUM_IDENTIFIER_LENGTH, false);
37
  add_field("ORDINAL_POSITION", plugin::TableFunction::NUMBER, 0, false);
1340.1.5 by Brian Aker
Update, we now have all of the ANSI INFORMATION_SCHEMA listed.
38
}
39
40
KeyColumnUsage::Generator::Generator(drizzled::Field **arg) :
41
  InformationSchema::Generator(arg),
1652.1.1 by Brian Aker
Added key_column_usage
42
  generator(getSession()),
43
  index_iterator(0),
1812.4.1 by Brian Aker
This fixes the IS key_column_usage table to correctly respond to FK.
44
  index_part_iterator(0),
45
  fk_constraint_iterator(0),
46
  fk_constraint_column_name_iterator(0)
1340.1.5 by Brian Aker
Update, we now have all of the ANSI INFORMATION_SCHEMA listed.
47
{
1657.1.3 by Monty Taylor
Fix empty-body warning on OSX.
48
  while (not (table_message= generator))
49
  { };
1340.1.5 by Brian Aker
Update, we now have all of the ANSI INFORMATION_SCHEMA listed.
50
}
51
52
bool KeyColumnUsage::Generator::populate()
53
{
1652.1.1 by Brian Aker
Added key_column_usage
54
  if (not table_message)
1340.1.5 by Brian Aker
Update, we now have all of the ANSI INFORMATION_SCHEMA listed.
55
    return false;
56
1652.1.1 by Brian Aker
Added key_column_usage
57
  do 
58
  {
59
    if (index_iterator != table_message->indexes_size())
60
    {
61
      drizzled::message::Table::Index index= table_message->indexes(index_iterator);
62
63
      if (index.is_primary() || index.is_unique())
64
      {
65
        /* Constraints live in the same catalog.schema as the table they refer too. */
66
        /* CONSTRAINT_CATALOG */
67
        push(table_message->catalog());
68
69
        /* CONSTRAINT_SCHEMA */
70
        push(table_message->schema());
71
72
        /* CONSTRAINT_NAME */
73
        push(index.name());
74
75
        /* TABLE_CATALOG */
76
        push(table_message->catalog());
77
78
        /* TABLE_SCHEMA */
79
        push(table_message->schema());
80
81
        /* TABLE_NAME */
82
        push(table_message->name());
83
84
        /* COLUMN_NAME */
85
        int32_t fieldnr= index.index_part(index_part_iterator).fieldnr();
86
        push(table_message->field(fieldnr).name());
87
88
        /* ORDINAL_POSITION */
1812.4.1 by Brian Aker
This fixes the IS key_column_usage table to correctly respond to FK.
89
        push(static_cast<int64_t>((index_part_iterator +1)));
1652.1.1 by Brian Aker
Added key_column_usage
90
91
        if (index_part_iterator == index.index_part_size() -1)
92
        {
93
          index_iterator++;
94
          index_part_iterator= 0;
95
        }
96
        else
97
        {
98
          index_part_iterator++;
99
        }
100
101
        return true;
102
      }
103
    }
104
1812.4.1 by Brian Aker
This fixes the IS key_column_usage table to correctly respond to FK.
105
    if (fk_constraint_iterator != table_message->fk_constraint_size())
106
    {
107
      drizzled::message::Table::ForeignKeyConstraint foreign_key= table_message->fk_constraint(fk_constraint_iterator);
108
109
      {
110
        /* Constraints live in the same catalog.schema as the table they refer too. */
111
        /* CONSTRAINT_CATALOG */
112
        push(table_message->catalog());
113
114
        /* CONSTRAINT_SCHEMA */
115
        push(table_message->schema());
116
117
        /* CONSTRAINT_NAME */
118
        push(foreign_key.name());
119
120
        /* TABLE_CATALOG */
121
        push(table_message->catalog());
122
123
        /* TABLE_SCHEMA */
124
        push(table_message->schema());
125
126
        /* TABLE_NAME */
127
        push(table_message->name());
128
129
        /* COLUMN_NAME */
130
        push(foreign_key.column_names(fk_constraint_column_name_iterator));
131
132
        /* ORDINAL_POSITION */
133
        push(static_cast<int64_t>((fk_constraint_column_name_iterator + 1)));
134
135
        if (fk_constraint_column_name_iterator == foreign_key.column_names_size() -1)
136
        {
137
          fk_constraint_iterator++;
138
          fk_constraint_column_name_iterator= 0;
139
        }
140
        else
141
        {
142
          fk_constraint_column_name_iterator++;
143
        }
144
145
        return true;
146
      }
147
    }
148
1652.1.1 by Brian Aker
Added key_column_usage
149
    index_iterator= 0;
150
    index_part_iterator= 0;
151
1812.4.1 by Brian Aker
This fixes the IS key_column_usage table to correctly respond to FK.
152
    fk_constraint_iterator= 0;
153
    fk_constraint_column_name_iterator= 0;
154
1652.1.1 by Brian Aker
Added key_column_usage
155
  } while ((table_message= generator));
156
157
  return false;
1340.1.5 by Brian Aker
Update, we now have all of the ANSI INFORMATION_SCHEMA listed.
158
}