~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
31
using namespace std;
32
1273.14.5 by Monty Taylor
Merged trunk.
33
namespace drizzled
34
{
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
35
36
static TableFunctionContainer table_functions;
37
38
void plugin::TableFunction::init()
39
{
1502.1.31 by Brian Aker
Merge engine options for schema/table.
40
  drizzled::message::Engine *engine;
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
41
  drizzled::message::Table::TableOptions *table_options;
42
1340.1.5 by Brian Aker
Update, we now have all of the ANSI INFORMATION_SCHEMA listed.
43
  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.
44
  proto.set_schema(identifier.getSchemaName());
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
45
  proto.set_type(drizzled::message::Table::FUNCTION);
1340.1.4 by Brian Aker
A first pass through adding a timestamp to our proto.
46
  proto.set_creation_timestamp(0);
47
  proto.set_update_timestamp(0);
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
48
49
  table_options= proto.mutable_options();
50
  table_options->set_collation_id(default_charset_info->number);
51
  table_options->set_collation(default_charset_info->name);
52
53
  engine= proto.mutable_engine();
54
  engine->set_name("FunctionEngine");
55
}
56
57
bool plugin::TableFunction::addPlugin(plugin::TableFunction *tool)
58
{
59
  assert(tool != NULL);
60
  table_functions.addFunction(tool); 
61
  return false;
62
}
63
64
plugin::TableFunction *plugin::TableFunction::getFunction(const string &arg)
65
{
66
  return table_functions.getFunction(arg);
67
}
68
69
void plugin::TableFunction::getNames(const string &arg,
70
                                     set<std::string> &set_of_names)
71
{
72
  table_functions.getNames(arg, set_of_names);
73
}
74
75
plugin::TableFunction::Generator *plugin::TableFunction::generator(Field **arg)
76
{
77
  return new Generator(arg);
78
}
79
80
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
81
                                      uint32_t field_length)
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
82
{
83
  add_field(label, TableFunction::STRING, field_length);
84
}
85
86
void plugin::TableFunction::add_field(const char *label,
87
                              TableFunction::ColumnType type,
88
                              bool is_default_null)
89
{
90
  add_field(label, type, 5, is_default_null);
91
}
92
93
void plugin::TableFunction::add_field(const char *label,
94
                              TableFunction::ColumnType type,
95
                              uint32_t field_length,
96
                              bool is_default_null)
97
{
98
  drizzled::message::Table::Field *field;
99
  drizzled::message::Table::Field::FieldOptions *field_options;
100
  drizzled::message::Table::Field::FieldConstraints *field_constraints;
101
102
  field= proto.add_field();
103
  field->set_name(label);
104
105
  field_options= field->mutable_options();
106
  field_constraints= field->mutable_constraints();
107
  field_options->set_default_null(is_default_null);
108
  field_constraints->set_is_nullable(is_default_null);
109
110
  switch (type) 
111
  {
112
  default:
1273.13.45 by Brian Aker
Update for test split.
113
  case TableFunction::BOOLEAN: // Currently BOOLEAN always has a value
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
114
    field_length= 5;
1273.13.45 by Brian Aker
Update for test split.
115
    field_options->set_default_null(false);
116
    field_constraints->set_is_nullable(false);
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
117
  case TableFunction::STRING:
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.
118
  {
119
    drizzled::message::Table::Field::StringFieldOptions *string_field_options;
120
    field->set_type(drizzled::message::Table::Field::VARCHAR);
121
122
    string_field_options= field->mutable_string_options();
123
    string_field_options->set_length(field_length);
124
  }
125
    break;
126
  case TableFunction::VARBINARY:
127
  {
128
    drizzled::message::Table::Field::StringFieldOptions *string_field_options;
129
    field->set_type(drizzled::message::Table::Field::VARCHAR);
130
131
    string_field_options= field->mutable_string_options();
132
    string_field_options->set_length(field_length);
133
    string_field_options->set_collation(my_charset_bin.csname);
134
    string_field_options->set_collation_id(my_charset_bin.number);
135
  }
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
136
    break;
1273.13.45 by Brian Aker
Update for test split.
137
  case TableFunction::NUMBER: // Currently NUMBER always has a value
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
138
    field->set_type(drizzled::message::Table::Field::BIGINT);
1273.13.45 by Brian Aker
Update for test split.
139
    field_options->set_default_null(false);
140
    field_constraints->set_is_nullable(false);
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
141
    break;
142
  }
143
}
144
145
plugin::TableFunction::Generator::Generator(Field **arg) :
1436 by Brian Aker
Move toward not having to call current_session (first pass).
146
  columns(arg),
147
  session(current_session)
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
148
{
149
  scs= system_charset_info;
150
}
151
1273.13.44 by Brian Aker
Updates/bug fix for columns. Assert now for checking that we filled in all
152
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
153
{
1273.13.44 by Brian Aker
Updates/bug fix for columns. Assert now for checking that we filled in all
154
  bool ret;
155
  uint64_t difference;
156
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
157
  columns_iterator= columns;
1273.13.44 by Brian Aker
Updates/bug fix for columns. Assert now for checking that we filled in all
158
  ret= populate();
159
  difference= columns_iterator - columns;
160
161
  if (ret == true)
162
  {
163
    assert(difference == field_size);
164
  }
165
166
  return ret;
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
167
}
168
1302.2.1 by Monty Taylor
Fixed the OSX build issues.
169
void plugin::TableFunction::Generator::push(uint64_t arg)
170
{
1273.13.82 by Brian Aker
Revert OSX fix, requiring more cast.
171
  (*columns_iterator)->store(static_cast<int64_t>(arg), true);
1337.1.1 by Stewart Smith
fix setting notnull for NUMBER columns in a TableFunction.
172
  (*columns_iterator)->set_notnull();
1302.2.1 by Monty Taylor
Fixed the OSX build issues.
173
  columns_iterator++;
174
}
175
176
void plugin::TableFunction::Generator::push(int64_t arg)
177
{
178
  (*columns_iterator)->store(arg, false);
1337.1.1 by Stewart Smith
fix setting notnull for NUMBER columns in a TableFunction.
179
  (*columns_iterator)->set_notnull();
1302.2.1 by Monty Taylor
Fixed the OSX build issues.
180
  columns_iterator++;
181
}
182
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
183
void plugin::TableFunction::Generator::push(const char *arg, uint32_t length)
184
{
185
  assert(columns_iterator);
186
  assert(*columns_iterator);
187
  assert(arg);
1273.13.44 by Brian Aker
Updates/bug fix for columns. Assert now for checking that we filled in all
188
  length= length ? length : strlen(arg);
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
{
197
  assert((*columns_iterator)->type()  == DRIZZLE_TYPE_VARCHAR);
198
  (*columns_iterator)->set_null();
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
199
  columns_iterator++;
200
}
201
1302.2.1 by Monty Taylor
Fixed the OSX build issues.
202
void plugin::TableFunction::Generator::push(const std::string& arg)
203
{
204
  push(arg.c_str(), arg.length());
205
}
206
207
void plugin::TableFunction::Generator::push(bool arg)
208
{
209
  if (arg)
210
  {
211
    (*columns_iterator)->store("TRUE", 4, scs);
212
  }
213
  else
214
  {
215
    (*columns_iterator)->store("FALSE", 5, scs);
216
  }
217
218
  columns_iterator++;
219
}
220
1273.13.38 by Brian Aker
Add in new show work.
221
bool plugin::TableFunction::Generator::isWild(const std::string &predicate)
222
{
1436 by Brian Aker
Move toward not having to call current_session (first pass).
223
  if (not getSession().lex->wild)
1273.13.38 by Brian Aker
Add in new show work.
224
    return false;
225
1273.13.61 by Brian Aker
Fixes a memory leak. sql_string() sucks.
226
  bool match= wild_case_compare(system_charset_info,
227
                                predicate.c_str(),
1436 by Brian Aker
Move toward not having to call current_session (first pass).
228
                                getSession().lex->wild->ptr());
1273.13.38 by Brian Aker
Add in new show work.
229
230
  return match;
231
}
1273.14.5 by Monty Taylor
Merged trunk.
232
233
} /* namespace drizzled */