~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
{
2017.3.1 by Brian Aker
Merge catalog with current trunk.
38
  drizzled::message::table::init(proto, getTableLabel(), identifier.getSchemaName(), "FunctionEngine");
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
39
  proto.set_type(drizzled::message::Table::FUNCTION);
1340.1.4 by Brian Aker
A first pass through adding a timestamp to our proto.
40
  proto.set_creation_timestamp(0);
41
  proto.set_update_timestamp(0);
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
42
}
43
44
bool plugin::TableFunction::addPlugin(plugin::TableFunction *tool)
45
{
46
  assert(tool != NULL);
47
  table_functions.addFunction(tool); 
48
  return false;
49
}
50
1966.2.9 by Brian Aker
Remove the use of "using std" from the plugin interface .cc files.
51
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
52
{
53
  return table_functions.getFunction(arg);
54
}
55
1966.2.9 by Brian Aker
Remove the use of "using std" from the plugin interface .cc files.
56
void plugin::TableFunction::getNames(const std::string &arg,
57
                                     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
58
{
59
  table_functions.getNames(arg, set_of_names);
60
}
61
62
plugin::TableFunction::Generator *plugin::TableFunction::generator(Field **arg)
63
{
64
  return new Generator(arg);
65
}
66
67
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
68
                                      uint32_t field_length)
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
69
{
70
  add_field(label, TableFunction::STRING, field_length);
71
}
72
73
void plugin::TableFunction::add_field(const char *label,
74
                              TableFunction::ColumnType type,
75
                              bool is_default_null)
76
{
77
  add_field(label, type, 5, is_default_null);
78
}
79
80
void plugin::TableFunction::add_field(const char *label,
1643.3.10 by Brian Aker
Column support, clean up of IS/DD for NULL type.
81
                                      TableFunction::ColumnType type,
82
                                      uint32_t field_length,
83
                                      bool is_default_null)
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
84
{
85
  drizzled::message::Table::Field *field;
86
  drizzled::message::Table::Field::FieldOptions *field_options;
87
  drizzled::message::Table::Field::FieldConstraints *field_constraints;
88
89
  field= proto.add_field();
90
  field->set_name(label);
91
92
  field_options= field->mutable_options();
93
  field_constraints= field->mutable_constraints();
94
  field_options->set_default_null(is_default_null);
2064.2.1 by Brian Aker
Fixes naming conventions and issues around notnull being "true" by default.
95
  field_constraints->set_is_notnull(not is_default_null);
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
96
97
  switch (type) 
98
  {
99
  case TableFunction::STRING:
1812.5.5 by Brian Aker
Added in possibility of having a blob type returned by a TableFunction.
100
    {
101
      drizzled::message::Table::Field::StringFieldOptions *string_field_options;
102
      if (field_length >= TABLE_FUNCTION_BLOB_SIZE)
103
      {
104
        field->set_type(drizzled::message::Table::Field::BLOB);
105
        string_field_options= field->mutable_string_options();
106
        string_field_options->set_collation_id(default_charset_info->number);
107
        string_field_options->set_collation(default_charset_info->name);
108
      }
109
      else
110
      {
111
        field->set_type(drizzled::message::Table::Field::VARCHAR);
112
        string_field_options= field->mutable_string_options();
113
        string_field_options->set_length(field_length);
114
      }
115
    }
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.
116
    break;
117
  case TableFunction::VARBINARY:
1812.5.5 by Brian Aker
Added in possibility of having a blob type returned by a TableFunction.
118
    {
119
      drizzled::message::Table::Field::StringFieldOptions *string_field_options;
120
      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.
121
1812.5.5 by Brian Aker
Added in possibility of having a blob type returned by a TableFunction.
122
      string_field_options= field->mutable_string_options();
123
      string_field_options->set_length(field_length);
124
      string_field_options->set_collation(my_charset_bin.csname);
125
      string_field_options->set_collation_id(my_charset_bin.number);
126
    }
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
127
    break;
2008.2.4 by Brian Aker
Merge in additional fixes for sign, plus alter table, plus TIME on
128
  case TableFunction::NUMBER:
129
    field->set_type(drizzled::message::Table::Field::BIGINT);
130
    break;
131
  case TableFunction::SIZE:
132
    field->set_type(drizzled::message::Table::Field::BIGINT);
133
    field_constraints->set_is_unsigned(true);
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
134
    break;
2023.2.2 by Brian Aker
Merge in BOOL change to BOOLEAN.
135
  case TableFunction::BOOLEAN: // Currently BOOLEAN always has a value
136
    field->set_type(drizzled::message::Table::Field::BOOLEAN);
137
    field_constraints->set_is_unsigned(true);
138
    break;
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
139
  }
140
}
141
142
plugin::TableFunction::Generator::Generator(Field **arg) :
1436 by Brian Aker
Move toward not having to call current_session (first pass).
143
  columns(arg),
144
  session(current_session)
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
145
{
146
  scs= system_charset_info;
147
}
148
1273.13.44 by Brian Aker
Updates/bug fix for columns. Assert now for checking that we filled in all
149
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
150
{
1273.13.44 by Brian Aker
Updates/bug fix for columns. Assert now for checking that we filled in all
151
  bool ret;
152
  uint64_t difference;
153
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
154
  columns_iterator= columns;
1273.13.44 by Brian Aker
Updates/bug fix for columns. Assert now for checking that we filled in all
155
  ret= populate();
156
  difference= columns_iterator - columns;
157
158
  if (ret == true)
159
  {
160
    assert(difference == field_size);
161
  }
162
163
  return ret;
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
164
}
165
1302.2.1 by Monty Taylor
Fixed the OSX build issues.
166
void plugin::TableFunction::Generator::push(uint64_t arg)
167
{
1273.13.82 by Brian Aker
Revert OSX fix, requiring more cast.
168
  (*columns_iterator)->store(static_cast<int64_t>(arg), true);
1337.1.1 by Stewart Smith
fix setting notnull for NUMBER columns in a TableFunction.
169
  (*columns_iterator)->set_notnull();
1302.2.1 by Monty Taylor
Fixed the OSX build issues.
170
  columns_iterator++;
171
}
172
173
void plugin::TableFunction::Generator::push(int64_t arg)
174
{
175
  (*columns_iterator)->store(arg, false);
1337.1.1 by Stewart Smith
fix setting notnull for NUMBER columns in a TableFunction.
176
  (*columns_iterator)->set_notnull();
1302.2.1 by Monty Taylor
Fixed the OSX build issues.
177
  columns_iterator++;
178
}
179
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
180
void plugin::TableFunction::Generator::push(const char *arg, uint32_t length)
181
{
182
  assert(columns_iterator);
183
  assert(*columns_iterator);
184
  assert(arg);
1273.13.44 by Brian Aker
Updates/bug fix for columns. Assert now for checking that we filled in all
185
  length= length ? length : strlen(arg);
186
1933.2.6 by Brian Aker
Enforce table share return lengths on string types.
187
  if ((*columns_iterator)->char_length() < length)
188
    length= (*columns_iterator)->char_length();
189
1273.13.45 by Brian Aker
Update for test split.
190
  (*columns_iterator)->store(arg, length, scs);
191
  (*columns_iterator)->set_notnull();
192
  columns_iterator++;
193
}
194
195
void plugin::TableFunction::Generator::push()
196
{
1993.3.1 by Andrew Hutchings
Enforce TableFunction::Generator::push() should only be used on a nullable field
197
  /* Only accept NULLs */
198
  assert((*columns_iterator)->maybe_null());
1273.13.45 by Brian Aker
Update for test split.
199
  (*columns_iterator)->set_null();
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
200
  columns_iterator++;
201
}
202
1302.2.1 by Monty Taylor
Fixed the OSX build issues.
203
void plugin::TableFunction::Generator::push(const std::string& arg)
204
{
205
  push(arg.c_str(), arg.length());
206
}
207
208
void plugin::TableFunction::Generator::push(bool arg)
209
{
210
  if (arg)
211
  {
1660 by Brian Aker
MErge in change to do YES/NO like standard requires.
212
    (*columns_iterator)->store("YES", 3, scs);
1302.2.1 by Monty Taylor
Fixed the OSX build issues.
213
  }
214
  else
215
  {
1660 by Brian Aker
MErge in change to do YES/NO like standard requires.
216
    (*columns_iterator)->store("NO", 2, scs);
1302.2.1 by Monty Taylor
Fixed the OSX build issues.
217
  }
218
219
  columns_iterator++;
220
}
221
1273.13.38 by Brian Aker
Add in new show work.
222
bool plugin::TableFunction::Generator::isWild(const std::string &predicate)
223
{
1436 by Brian Aker
Move toward not having to call current_session (first pass).
224
  if (not getSession().lex->wild)
1273.13.38 by Brian Aker
Add in new show work.
225
    return false;
226
1273.13.61 by Brian Aker
Fixes a memory leak. sql_string() sucks.
227
  bool match= wild_case_compare(system_charset_info,
228
                                predicate.c_str(),
1436 by Brian Aker
Move toward not having to call current_session (first pass).
229
                                getSession().lex->wild->ptr());
1273.13.38 by Brian Aker
Add in new show work.
230
231
  return match;
232
}
1273.14.5 by Monty Taylor
Merged trunk.
233
234
} /* namespace drizzled */