~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 *
 *  Copyright (C) 2010 Sun Microsystems
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "config.h"
#include "plugin/schema_dictionary/dictionary.h"

using namespace std;
using namespace drizzled;


ColumnsTool::ColumnsTool() :
  TablesTool("COLUMNS")
{
  add_field("TABLE_SCHEMA");
  add_field("TABLE_NAME");

  add_field("COLUMN_NAME");
  add_field("COLUMN_TYPE");
  add_field("ORDINAL_POSITION", plugin::TableFunction::NUMBER);
  add_field("COLUMN_DEFAULT", plugin::TableFunction::VARBINARY, 65535, true);
  add_field("COLUMN_DEFAULT_IS_NULL", plugin::TableFunction::BOOLEAN);
  add_field("COLUMN_DEFAULT_UPDATE");
  add_field("IS_NULLABLE", plugin::TableFunction::BOOLEAN);
  add_field("IS_INDEXED", plugin::TableFunction::BOOLEAN);
  add_field("IS_USED_IN_PRIMARY", plugin::TableFunction::BOOLEAN);
  add_field("IS_UNIQUE", plugin::TableFunction::BOOLEAN);
  add_field("IS_MULTI", plugin::TableFunction::BOOLEAN);
  add_field("IS_FIRST_IN_MULTI", plugin::TableFunction::BOOLEAN);
  add_field("INDEXES_FOUND_IN", plugin::TableFunction::NUMBER);
  add_field("DATA_TYPE");

  add_field("CHARACTER_MAXIMUM_LENGTH", plugin::TableFunction::NUMBER);
  add_field("CHARACTER_OCTET_LENGTH", plugin::TableFunction::NUMBER);
  add_field("NUMERIC_PRECISION", plugin::TableFunction::NUMBER);
  add_field("NUMERIC_SCALE", plugin::TableFunction::NUMBER);

  add_field("COLLATION_NAME");

  add_field("COLUMN_COMMENT", 1024);
}


ColumnsTool::Generator::Generator(Field **arg) :
  TablesTool::Generator(arg),
  column_iterator(0),
  is_columns_primed(false)
{
}


bool ColumnsTool::Generator::nextColumnCore()
{
  if (is_columns_primed)
  {
    column_iterator++;
  }
  else
  {
    if (not isTablesPrimed())
      return false;

    column_iterator= 0;
    is_columns_primed= true;
  }

  if (column_iterator >= getTableProto().field_size())
    return false;

  column= getTableProto().field(column_iterator);

  return true;
}


bool ColumnsTool::Generator::nextColumn()
{
  while (not nextColumnCore())
  {
    if (not nextTable())
      return false;
    is_columns_primed= false;
  }

  return true;
}

bool ColumnsTool::Generator::populate()
{

  if (not nextColumn())
    return false;

  fill();

  return true;
}

void ColumnsTool::Generator::fill()
{
  /* TABLE_SCHEMA */
  push(getTableProto().schema());

  /* TABLE_NAME */
  push(getTableProto().name());

  /* COLUMN_NAME */
  push(column.name());

  /* COLUMN_TYPE */
  pushType(column.type());

  /* ORDINAL_POSITION */
  push(static_cast<int64_t>(column_iterator));

  /* COLUMN_DEFAULT */
  if (column.options().has_default_value())
    push(column.options().default_value());
  else if (column.options().has_default_bin_value())
    push(column.options().default_bin_value().c_str(), column.options().default_bin_value().length());
  else
    push();

  /* COLUMN_DEFAULT_IS_NULL */
  push(column.options().default_null());

  /* COLUMN_DEFAULT_UPDATE */
  push(column.options().update_value());

  /* IS_NULLABLE */
  push(column.constraints().is_nullable());

  /* IS_INDEXED, IS_USED_IN_PRIMARY, IS_UNIQUE, IS_MULTI, IS_FIRST_IN_MULTI, INDEXES_FOUND_IN */
  bool is_indexed= false;
  bool is_primary= false;
  bool is_unique= false;
  bool is_multi= false;
  bool is_multi_first= false;
  int64_t indexes_found_in= 0;
  for (int32_t x= 0; x < getTableProto().indexes_size() ; x++)
  {
    drizzled::message::Table::Index index=
      getTableProto().indexes(x);

    for (int32_t y= 0; y < index.index_part_size() ; y++)
    {
      drizzled::message::Table::Index::IndexPart index_part=
        index.index_part(y);

      if (static_cast<int32_t>(index_part.fieldnr()) == column_iterator)
      {
        indexes_found_in++;
        is_indexed= true;

        if (index.is_primary())
          is_primary= true;

        if (index.is_unique())
          is_unique= true;

        if (index.index_part_size() > 1)
        {
          is_multi= true;

          if (y == 0)
            is_multi_first= true;
        }
      }
    }
  }
  /* ...IS_INDEXED, IS_USED_IN_PRIMARY, IS_UNIQUE, IS_MULTI, IS_FIRST_IN_MULTI, INDEXES_FOUND_IN */
  push(is_indexed);
  push(is_primary);
  push(is_unique);
  push(is_multi);
  push(is_multi_first);
  push(indexes_found_in);

  /* DATATYPE */
  pushType(column.type());

 /* "CHARACTER_MAXIMUM_LENGTH" */
  push(static_cast<int64_t>(column.string_options().length()));

 /* "CHARACTER_OCTET_LENGTH" */
  push(static_cast<int64_t>(column.string_options().length()) * 4);

 /* "NUMERIC_PRECISION" */
  push(static_cast<int64_t>(column.numeric_options().precision()));

 /* "NUMERIC_SCALE" */
  push(static_cast<int64_t>(column.numeric_options().scale()));

 /* "COLLATION_NAME" */
  push(column.string_options().collation());

 /* "COLUMN_COMMENT" */
  push(column.comment());
}