~drizzle-trunk/drizzle/development

1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
4
 *  Copyright (C) 2010 Monty Taylor
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; version 2 of the License.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License
16
 *  along with this program; if not, write to the Free Software
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 */
19
20
#include "config.h"
21
22
#include <drizzled/plugin/table_function.h>
23
#include <drizzled/table_function_container.h>
24
#include <drizzled/gettext.h>
25
#include "drizzled/global_charset_info.h"
1273.13.38 by Brian Aker
Add in new show work.
26
#include "drizzled/session.h"
27
#include "drizzled/current_session.h"
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
28
29
#include <vector>
30
1273.14.5 by Monty Taylor
Merged trunk.
31
namespace drizzled
32
{
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
33
34
static TableFunctionContainer table_functions;
35
36
void plugin::TableFunction::init()
37
{
1502.1.31 by Brian Aker
Merge engine options for schema/table.
38
  drizzled::message::Engine *engine;
1638.10.50 by Stewart Smith
revert patch that removse explicitly setting collation for talbe_function tables.
39
  drizzled::message::Table::TableOptions *table_options;
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
40
1340.1.5 by Brian Aker
Update, we now have all of the ANSI INFORMATION_SCHEMA listed.
41
  proto.set_name(getTableLabel());
1337.2.2 by Stewart Smith
Add Schema to table message. modify alter table to correctly set a schema when running ALTER TABLE. Also update show_table_message test results to reflect the new field.
42
  proto.set_schema(identifier.getSchemaName());
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
43
  proto.set_type(drizzled::message::Table::FUNCTION);
1340.1.4 by Brian Aker
A first pass through adding a timestamp to our proto.
44
  proto.set_creation_timestamp(0);
45
  proto.set_update_timestamp(0);
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
46
1638.10.50 by Stewart Smith
revert patch that removse explicitly setting collation for talbe_function tables.
47
  table_options= proto.mutable_options();
48
  table_options->set_collation_id(default_charset_info->number);
49
  table_options->set_collation(default_charset_info->name);
50
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
51
  engine= proto.mutable_engine();
52
  engine->set_name("FunctionEngine");
53
}
54
55
bool plugin::TableFunction::addPlugin(plugin::TableFunction *tool)
56
{
57
  assert(tool != NULL);
58
  table_functions.addFunction(tool); 
59
  return false;
60
}
61
1966.2.9 by Brian Aker
Remove the use of "using std" from the plugin interface .cc files.
62
plugin::TableFunction *plugin::TableFunction::getFunction(const std::string &arg)
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
63
{
64
  return table_functions.getFunction(arg);
65
}
66
1966.2.9 by Brian Aker
Remove the use of "using std" from the plugin interface .cc files.
67
void plugin::TableFunction::getNames(const std::string &arg,
68
                                     std::set<std::string> &set_of_names)
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
69
{
70
  table_functions.getNames(arg, set_of_names);
71
}
72
73
plugin::TableFunction::Generator *plugin::TableFunction::generator(Field **arg)
74
{
75
  return new Generator(arg);
76
}
77
78
void plugin::TableFunction::add_field(const char *label,
1273.13.44 by Brian Aker
Updates/bug fix for columns. Assert now for checking that we filled in all
79
                                      uint32_t field_length)
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
80
{
81
  add_field(label, TableFunction::STRING, field_length);
82
}
83
84
void plugin::TableFunction::add_field(const char *label,
85
                              TableFunction::ColumnType type,
86
                              bool is_default_null)
87
{
88
  add_field(label, type, 5, is_default_null);
89
}
90
91
void plugin::TableFunction::add_field(const char *label,
1643.3.10 by Brian Aker
Column support, clean up of IS/DD for NULL type.
92
                                      TableFunction::ColumnType type,
93
                                      uint32_t field_length,
94
                                      bool is_default_null)
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
95
{
96
  drizzled::message::Table::Field *field;
97
  drizzled::message::Table::Field::FieldOptions *field_options;
98
  drizzled::message::Table::Field::FieldConstraints *field_constraints;
99
100
  field= proto.add_field();
101
  field->set_name(label);
102
103
  field_options= field->mutable_options();
104
  field_constraints= field->mutable_constraints();
105
  field_options->set_default_null(is_default_null);
106
  field_constraints->set_is_nullable(is_default_null);
107
108
  switch (type) 
109
  {
110
  case TableFunction::STRING:
1812.5.5 by Brian Aker
Added in possibility of having a blob type returned by a TableFunction.
111
    {
112
      drizzled::message::Table::Field::StringFieldOptions *string_field_options;
113
      if (field_length >= TABLE_FUNCTION_BLOB_SIZE)
114
      {
115
        field->set_type(drizzled::message::Table::Field::BLOB);
116
        string_field_options= field->mutable_string_options();
117
        string_field_options->set_collation_id(default_charset_info->number);
118
        string_field_options->set_collation(default_charset_info->name);
119
      }
120
      else
121
      {
122
        field->set_type(drizzled::message::Table::Field::VARCHAR);
123
        string_field_options= field->mutable_string_options();
124
        string_field_options->set_length(field_length);
125
      }
126
    }
1337.1.2 by Stewart Smith
make DATA_DICTIONARY.COLUMNS.COLUMN_DEFAULT be a VARBINARY column that can be null. Add a test for binary default values as well as non-binary default values.
127
    break;
128
  case TableFunction::VARBINARY:
1812.5.5 by Brian Aker
Added in possibility of having a blob type returned by a TableFunction.
129
    {
130
      drizzled::message::Table::Field::StringFieldOptions *string_field_options;
131
      field->set_type(drizzled::message::Table::Field::VARCHAR);
1337.1.2 by Stewart Smith
make DATA_DICTIONARY.COLUMNS.COLUMN_DEFAULT be a VARBINARY column that can be null. Add a test for binary default values as well as non-binary default values.
132
1812.5.5 by Brian Aker
Added in possibility of having a blob type returned by a TableFunction.
133
      string_field_options= field->mutable_string_options();
134
      string_field_options->set_length(field_length);
135
      string_field_options->set_collation(my_charset_bin.csname);
136
      string_field_options->set_collation_id(my_charset_bin.number);
137
    }
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
138
    break;
2008.2.4 by Brian Aker
Merge in additional fixes for sign, plus alter table, plus TIME on
139
  case TableFunction::NUMBER:
140
    field->set_type(drizzled::message::Table::Field::BIGINT);
141
    break;
142
  case TableFunction::SIZE:
143
    field->set_type(drizzled::message::Table::Field::BIGINT);
144
    field_constraints->set_is_unsigned(true);
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
145
    break;
2023.2.2 by Brian Aker
Merge in BOOL change to BOOLEAN.
146
  case TableFunction::BOOLEAN: // Currently BOOLEAN always has a value
147
    field->set_type(drizzled::message::Table::Field::BOOLEAN);
148
    field_constraints->set_is_unsigned(true);
149
    break;
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
150
  }
151
}
152
153
plugin::TableFunction::Generator::Generator(Field **arg) :
1436 by Brian Aker
Move toward not having to call current_session (first pass).
154
  columns(arg),
155
  session(current_session)
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
156
{
157
  scs= system_charset_info;
158
}
159
1273.13.44 by Brian Aker
Updates/bug fix for columns. Assert now for checking that we filled in all
160
bool plugin::TableFunction::Generator::sub_populate(uint32_t field_size)
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
161
{
1273.13.44 by Brian Aker
Updates/bug fix for columns. Assert now for checking that we filled in all
162
  bool ret;
163
  uint64_t difference;
164
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
165
  columns_iterator= columns;
1273.13.44 by Brian Aker
Updates/bug fix for columns. Assert now for checking that we filled in all
166
  ret= populate();
167
  difference= columns_iterator - columns;
168
169
  if (ret == true)
170
  {
171
    assert(difference == field_size);
172
  }
173
174
  return ret;
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
175
}
176
1302.2.1 by Monty Taylor
Fixed the OSX build issues.
177
void plugin::TableFunction::Generator::push(uint64_t arg)
178
{
1273.13.82 by Brian Aker
Revert OSX fix, requiring more cast.
179
  (*columns_iterator)->store(static_cast<int64_t>(arg), true);
1337.1.1 by Stewart Smith
fix setting notnull for NUMBER columns in a TableFunction.
180
  (*columns_iterator)->set_notnull();
1302.2.1 by Monty Taylor
Fixed the OSX build issues.
181
  columns_iterator++;
182
}
183
184
void plugin::TableFunction::Generator::push(int64_t arg)
185
{
186
  (*columns_iterator)->store(arg, false);
1337.1.1 by Stewart Smith
fix setting notnull for NUMBER columns in a TableFunction.
187
  (*columns_iterator)->set_notnull();
1302.2.1 by Monty Taylor
Fixed the OSX build issues.
188
  columns_iterator++;
189
}
190
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
191
void plugin::TableFunction::Generator::push(const char *arg, uint32_t length)
192
{
193
  assert(columns_iterator);
194
  assert(*columns_iterator);
195
  assert(arg);
1273.13.44 by Brian Aker
Updates/bug fix for columns. Assert now for checking that we filled in all
196
  length= length ? length : strlen(arg);
197
1933.2.6 by Brian Aker
Enforce table share return lengths on string types.
198
  if ((*columns_iterator)->char_length() < length)
199
    length= (*columns_iterator)->char_length();
200
1273.13.45 by Brian Aker
Update for test split.
201
  (*columns_iterator)->store(arg, length, scs);
202
  (*columns_iterator)->set_notnull();
203
  columns_iterator++;
204
}
205
206
void plugin::TableFunction::Generator::push()
207
{
1993.3.1 by Andrew Hutchings
Enforce TableFunction::Generator::push() should only be used on a nullable field
208
  /* Only accept NULLs */
209
  assert((*columns_iterator)->maybe_null());
1273.13.45 by Brian Aker
Update for test split.
210
  (*columns_iterator)->set_null();
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
211
  columns_iterator++;
212
}
213
1302.2.1 by Monty Taylor
Fixed the OSX build issues.
214
void plugin::TableFunction::Generator::push(const std::string& arg)
215
{
216
  push(arg.c_str(), arg.length());
217
}
218
219
void plugin::TableFunction::Generator::push(bool arg)
220
{
221
  if (arg)
222
  {
1660 by Brian Aker
MErge in change to do YES/NO like standard requires.
223
    (*columns_iterator)->store("YES", 3, scs);
1302.2.1 by Monty Taylor
Fixed the OSX build issues.
224
  }
225
  else
226
  {
1660 by Brian Aker
MErge in change to do YES/NO like standard requires.
227
    (*columns_iterator)->store("NO", 2, scs);
1302.2.1 by Monty Taylor
Fixed the OSX build issues.
228
  }
229
230
  columns_iterator++;
231
}
232
1273.13.38 by Brian Aker
Add in new show work.
233
bool plugin::TableFunction::Generator::isWild(const std::string &predicate)
234
{
1436 by Brian Aker
Move toward not having to call current_session (first pass).
235
  if (not getSession().lex->wild)
1273.13.38 by Brian Aker
Add in new show work.
236
    return false;
237
1273.13.61 by Brian Aker
Fixes a memory leak. sql_string() sucks.
238
  bool match= wild_case_compare(system_charset_info,
239
                                predicate.c_str(),
1436 by Brian Aker
Move toward not having to call current_session (first pass).
240
                                getSession().lex->wild->ptr());
1273.13.38 by Brian Aker
Add in new show work.
241
242
  return match;
243
}
1273.14.5 by Monty Taylor
Merged trunk.
244
245
} /* namespace drizzled */