~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/schema_dictionary/columns.cc

  • Committer: lbieber
  • Date: 2010-10-05 22:23:12 UTC
  • mfrom: (1813.1.4 build)
  • Revision ID: lbieber@orisndriz08-20101005222312-weuq0ardk3gcryau
Merge Travis - 621861 - convert structs to classes
Merge Billy - 621331 - Replace use of stringstream with boost::lexical_cast
Merge Travis - 621861 = To change C structs to C++ classes in Drizzle
Merge Andrew - fix bug 653300 - Syntax error on inport of a SQL file produced by drizzledump

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2010 Sun Microsystems, Inc.
 
4
 *  Copyright (C) 2010 Sun Microsystems
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
26
26
 
27
27
 
28
28
ColumnsTool::ColumnsTool() :
29
 
  DataDictionary("COLUMNS")
 
29
  TablesTool("COLUMNS")
30
30
{
31
31
  add_field("TABLE_SCHEMA");
32
32
  add_field("TABLE_NAME");
37
37
  add_field("COLUMN_DEFAULT", plugin::TableFunction::VARBINARY, 65535, true);
38
38
  add_field("COLUMN_DEFAULT_IS_NULL", plugin::TableFunction::BOOLEAN, 0, false);
39
39
  add_field("COLUMN_DEFAULT_UPDATE");
40
 
  add_field("IS_SIGNED", plugin::TableFunction::BOOLEAN, 0, true);
41
40
  add_field("IS_AUTO_INCREMENT", plugin::TableFunction::BOOLEAN, 0, false);
42
41
  add_field("IS_NULLABLE", plugin::TableFunction::BOOLEAN, 0, false);
43
42
  add_field("IS_INDEXED", plugin::TableFunction::BOOLEAN, 0, false);
47
46
  add_field("IS_FIRST_IN_MULTI", plugin::TableFunction::BOOLEAN, 0, false);
48
47
  add_field("INDEXES_FOUND_IN", plugin::TableFunction::NUMBER, 0, false);
49
48
  add_field("DATA_TYPE");
50
 
  add_field("DATA_ARCHETYPE");
 
49
 
51
50
  add_field("CHARACTER_MAXIMUM_LENGTH", plugin::TableFunction::NUMBER);
52
51
  add_field("CHARACTER_OCTET_LENGTH", plugin::TableFunction::NUMBER);
53
52
  add_field("NUMERIC_PRECISION", plugin::TableFunction::NUMBER);
62
61
 
63
62
 
64
63
ColumnsTool::Generator::Generator(Field **arg) :
65
 
  DataDictionary::Generator(arg),
66
 
  field_generator(getSession())
67
 
{
 
64
  TablesTool::Generator(arg),
 
65
  column_iterator(0),
 
66
  is_columns_primed(false)
 
67
{
 
68
}
 
69
 
 
70
 
 
71
bool ColumnsTool::Generator::nextColumnCore()
 
72
{
 
73
  if (is_columns_primed)
 
74
  {
 
75
    column_iterator++;
 
76
  }
 
77
  else
 
78
  {
 
79
    if (not isTablesPrimed())
 
80
      return false;
 
81
 
 
82
    column_iterator= 0;
 
83
    is_columns_primed= true;
 
84
  }
 
85
 
 
86
  if (column_iterator >= getTableProto().field_size())
 
87
    return false;
 
88
 
 
89
  column= getTableProto().field(column_iterator);
 
90
 
 
91
  return true;
 
92
}
 
93
 
 
94
 
 
95
bool ColumnsTool::Generator::nextColumn()
 
96
{
 
97
  while (not nextColumnCore())
 
98
  {
 
99
    if (not nextTable())
 
100
      return false;
 
101
    is_columns_primed= false;
 
102
  }
 
103
 
 
104
  return true;
68
105
}
69
106
 
70
107
bool ColumnsTool::Generator::populate()
71
108
{
72
 
  drizzled::generator::FieldPair field_pair;
73
 
 
74
 
  while (!!(field_pair= field_generator))
75
 
  {
76
 
    const drizzled::message::Table *table_message= field_pair.first;
77
 
    int32_t field_iterator= field_pair.second;
78
 
    const message::Table::Field &column(table_message->field(field_pair.second));
79
 
 
80
 
    /* TABLE_SCHEMA */
81
 
    push(table_message->schema());
82
 
 
83
 
    /* TABLE_NAME */
84
 
    push(table_message->name());
85
 
 
86
 
    /* COLUMN_NAME */
87
 
    push(column.name());
88
 
 
89
 
    /* COLUMN_TYPE */
90
 
    push(drizzled::message::type(column.type()));
91
 
 
92
 
    /* ORDINAL_POSITION */
93
 
    push(static_cast<int64_t>(field_iterator));
94
 
 
95
 
    /* COLUMN_DEFAULT */
96
 
    if (column.options().has_default_value())
97
 
    {
98
 
      push(column.options().default_value());
99
 
    }
100
 
    else if (column.options().has_default_bin_value())
101
 
    {
102
 
      push(column.options().default_bin_value().c_str(), column.options().default_bin_value().length());
103
 
    }
104
 
    else if (column.options().has_default_expression())
105
 
    {
106
 
      push(column.options().default_expression());
107
 
    }
108
 
    else
109
 
    {
110
 
      push();
111
 
    }
112
 
 
113
 
    /* COLUMN_DEFAULT_IS_NULL */
114
 
    push(column.options().default_null());
115
 
 
116
 
    /* COLUMN_DEFAULT_UPDATE */
117
 
    push(column.options().update_expression());
118
 
 
119
 
    /* IS_SIGNED */
120
 
    if (drizzled::message::is_numeric(column))
121
 
    {
122
 
      push(true);
123
 
    }
124
 
    else 
125
 
    {
126
 
      push();
127
 
    }
128
 
 
129
 
    /* IS_AUTO_INCREMENT */
130
 
    push(column.numeric_options().is_autoincrement());
131
 
 
132
 
    /* IS_NULLABLE */
133
 
    push(not column.constraints().is_notnull());
134
 
 
135
 
    /* IS_INDEXED, IS_USED_IN_PRIMARY, IS_UNIQUE, IS_MULTI, IS_FIRST_IN_MULTI, INDEXES_FOUND_IN */
136
 
    bool is_indexed= false;
137
 
    bool is_primary= false;
138
 
    bool is_unique= false;
139
 
    bool is_multi= false;
140
 
    bool is_multi_first= false;
141
 
    int64_t indexes_found_in= 0;
142
 
    for (int32_t x= 0; x < table_message->indexes_size() ; x++)
143
 
    {
144
 
      const drizzled::message::Table::Index &index(table_message->indexes(x));
145
 
 
146
 
      for (int32_t y= 0; y < index.index_part_size() ; y++)
 
109
 
 
110
  if (not nextColumn())
 
111
    return false;
 
112
 
 
113
  fill();
 
114
 
 
115
  return true;
 
116
}
 
117
 
 
118
void ColumnsTool::Generator::fill()
 
119
{
 
120
  /* TABLE_SCHEMA */
 
121
  assert(getTableProto().schema().length());
 
122
  assert(getTableProto().schema().c_str());
 
123
  push(getTableProto().schema());
 
124
 
 
125
  /* TABLE_NAME */
 
126
  push(getTableProto().name());
 
127
 
 
128
  /* COLUMN_NAME */
 
129
  push(column.name());
 
130
 
 
131
  /* COLUMN_TYPE */
 
132
  pushType(column.type());
 
133
 
 
134
  /* ORDINAL_POSITION */
 
135
  push(static_cast<int64_t>(column_iterator));
 
136
 
 
137
  /* COLUMN_DEFAULT */
 
138
  if (column.options().has_default_value())
 
139
  {
 
140
    push(column.options().default_value());
 
141
  }
 
142
  else if (column.options().has_default_bin_value())
 
143
  {
 
144
    push(column.options().default_bin_value().c_str(), column.options().default_bin_value().length());
 
145
  }
 
146
  else if (column.options().has_default_expression())
 
147
  {
 
148
    push(column.options().default_expression());
 
149
  }
 
150
  else
 
151
  {
 
152
    push();
 
153
  }
 
154
 
 
155
  /* COLUMN_DEFAULT_IS_NULL */
 
156
  push(column.options().default_null());
 
157
 
 
158
  /* COLUMN_DEFAULT_UPDATE */
 
159
  push(column.options().update_expression());
 
160
 
 
161
  /* IS_AUTO_INCREMENT */
 
162
  push(column.numeric_options().is_autoincrement());
 
163
 
 
164
  /* IS_NULLABLE */
 
165
  push(column.constraints().is_nullable());
 
166
 
 
167
  /* IS_INDEXED, IS_USED_IN_PRIMARY, IS_UNIQUE, IS_MULTI, IS_FIRST_IN_MULTI, INDEXES_FOUND_IN */
 
168
  bool is_indexed= false;
 
169
  bool is_primary= false;
 
170
  bool is_unique= false;
 
171
  bool is_multi= false;
 
172
  bool is_multi_first= false;
 
173
  int64_t indexes_found_in= 0;
 
174
  for (int32_t x= 0; x < getTableProto().indexes_size() ; x++)
 
175
  {
 
176
    drizzled::message::Table::Index index=
 
177
      getTableProto().indexes(x);
 
178
 
 
179
    for (int32_t y= 0; y < index.index_part_size() ; y++)
 
180
    {
 
181
      drizzled::message::Table::Index::IndexPart index_part=
 
182
        index.index_part(y);
 
183
 
 
184
      if (static_cast<int32_t>(index_part.fieldnr()) == column_iterator)
147
185
      {
148
 
        const drizzled::message::Table::Index::IndexPart &index_part(index.index_part(y));
149
 
 
150
 
        if (static_cast<int32_t>(index_part.fieldnr()) == field_iterator)
 
186
        indexes_found_in++;
 
187
        is_indexed= true;
 
188
 
 
189
        if (index.is_primary())
 
190
          is_primary= true;
 
191
 
 
192
        if (index.is_unique())
 
193
          is_unique= true;
 
194
 
 
195
        if (index.index_part_size() > 1)
151
196
        {
152
 
          indexes_found_in++;
153
 
          is_indexed= true;
154
 
 
155
 
          if (index.is_primary())
156
 
            is_primary= true;
157
 
 
158
 
          if (index.is_unique())
159
 
            is_unique= true;
160
 
 
161
 
          if (index.index_part_size() > 1)
162
 
          {
163
 
            is_multi= true;
164
 
 
165
 
            if (y == 0)
166
 
              is_multi_first= true;
167
 
          }
 
197
          is_multi= true;
 
198
 
 
199
          if (y == 0)
 
200
            is_multi_first= true;
168
201
        }
169
202
      }
170
203
    }
171
 
    /* ...IS_INDEXED, IS_USED_IN_PRIMARY, IS_UNIQUE, IS_MULTI, IS_FIRST_IN_MULTI, INDEXES_FOUND_IN */
172
 
    push(is_indexed);
173
 
    push(is_primary);
174
 
    push(is_unique);
175
 
    push(is_multi);
176
 
    push(is_multi_first);
177
 
    push(indexes_found_in);
178
 
 
179
 
    /* DATA_TYPE <-- display the type that the user is going to expect, which is not the same as the type we store internally */
180
 
    push(drizzled::message::type(column));
181
 
 
182
 
    /* DATA_ARCHETYPE */
183
 
    push(drizzled::message::type(column.type()));
184
 
 
185
 
    /* "CHARACTER_MAXIMUM_LENGTH" */
186
 
    push(static_cast<int64_t>(column.string_options().length()));
187
 
 
188
 
    /* "CHARACTER_OCTET_LENGTH" */
189
 
    push(static_cast<int64_t>(column.string_options().length()) * 4);
190
 
 
191
 
    /* "NUMERIC_PRECISION" */
192
 
    push(static_cast<int64_t>(column.numeric_options().precision()));
193
 
 
194
 
    /* "NUMERIC_SCALE" */
195
 
    push(static_cast<int64_t>(column.numeric_options().scale()));
196
 
 
197
 
    /* "ENUM_VALUES" */
198
 
    if (column.type() == drizzled::message::Table::Field::ENUM)
199
 
    {
200
 
      string destination;
201
 
      size_t num_field_values= column.enumeration_values().field_value_size();
202
 
      for (size_t x= 0; x < num_field_values; ++x)
203
 
      {
204
 
        const string &type= column.enumeration_values().field_value(x);
205
 
 
206
 
        if (x != 0)
207
 
          destination.push_back(',');
208
 
 
209
 
        destination.push_back('\'');
210
 
        destination.append(type);
211
 
        destination.push_back('\'');
212
 
      }
213
 
      push(destination);
214
 
    }
215
 
    else
216
 
    {
217
 
      push();
218
 
    }
219
 
 
220
 
    /* "COLLATION_NAME" */
221
 
    push(column.string_options().collation());
222
 
 
223
 
    /* "COLUMN_COMMENT" */
224
 
    if (column.has_comment())
225
 
    {
226
 
      push(column.comment());
227
 
    }
228
 
    else
229
 
    {
230
 
      push();
231
 
    }
232
 
 
233
 
    return true;
234
 
  }
235
 
 
236
 
  return false;
 
204
  }
 
205
  /* ...IS_INDEXED, IS_USED_IN_PRIMARY, IS_UNIQUE, IS_MULTI, IS_FIRST_IN_MULTI, INDEXES_FOUND_IN */
 
206
  push(is_indexed);
 
207
  push(is_primary);
 
208
  push(is_unique);
 
209
  push(is_multi);
 
210
  push(is_multi_first);
 
211
  push(indexes_found_in);
 
212
 
 
213
  /* DATATYPE */
 
214
  pushType(column.type());
 
215
 
 
216
 /* "CHARACTER_MAXIMUM_LENGTH" */
 
217
  push(static_cast<int64_t>(column.string_options().length()));
 
218
 
 
219
 /* "CHARACTER_OCTET_LENGTH" */
 
220
  push(static_cast<int64_t>(column.string_options().length()) * 4);
 
221
 
 
222
 /* "NUMERIC_PRECISION" */
 
223
  push(static_cast<int64_t>(column.numeric_options().precision()));
 
224
 
 
225
 /* "NUMERIC_SCALE" */
 
226
  push(static_cast<int64_t>(column.numeric_options().scale()));
 
227
 
 
228
 /* "ENUM_VALUES" */
 
229
  if (column.type() == drizzled::message::Table::Field::ENUM)
 
230
  {
 
231
    string destination;
 
232
    size_t num_field_values= column.enumeration_values().field_value_size();
 
233
    for (size_t x= 0; x < num_field_values; ++x)
 
234
    {
 
235
      const string &type= column.enumeration_values().field_value(x);
 
236
 
 
237
      if (x != 0)
 
238
        destination.push_back(',');
 
239
 
 
240
      destination.push_back('\'');
 
241
      destination.append(type);
 
242
      destination.push_back('\'');
 
243
    }
 
244
    push(destination);
 
245
  }
 
246
  else
 
247
    push();
 
248
 
 
249
 /* "COLLATION_NAME" */
 
250
  push(column.string_options().collation());
 
251
 
 
252
 /* "COLUMN_COMMENT" */
 
253
  if (column.has_comment())
 
254
  {
 
255
    push(column.comment());
 
256
  }
 
257
  else
 
258
  {
 
259
    push();
 
260
  }
237
261
}