1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2010 Monty Taylor
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.
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.
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
22
#include <drizzled/current_session.h>
23
#include <drizzled/gettext.h>
24
#include <drizzled/global_charset_info.h>
25
#include <drizzled/plugin/table_function.h>
26
#include <drizzled/session.h>
27
#include <drizzled/show.h>
28
#include <drizzled/table_function_container.h>
35
static TableFunctionContainer table_functions;
37
void plugin::TableFunction::init()
39
drizzled::message::table::init(proto, getTableLabel(), identifier.getSchemaName(), "FunctionEngine");
40
proto.set_type(drizzled::message::Table::FUNCTION);
41
proto.set_creation_timestamp(0);
42
proto.set_update_timestamp(0);
45
bool plugin::TableFunction::addPlugin(plugin::TableFunction *tool)
48
table_functions.addFunction(tool);
52
plugin::TableFunction *plugin::TableFunction::getFunction(const std::string &arg)
54
return table_functions.getFunction(arg);
57
void plugin::TableFunction::getNames(const std::string &arg,
58
std::set<std::string> &set_of_names)
60
table_functions.getNames(arg, set_of_names);
63
plugin::TableFunction::Generator *plugin::TableFunction::generator(Field **arg)
65
return new Generator(arg);
68
void plugin::TableFunction::add_field(const char *label,
69
uint32_t field_length)
71
add_field(label, TableFunction::STRING, field_length);
74
void plugin::TableFunction::add_field(const char *label,
75
TableFunction::ColumnType type,
78
add_field(label, type, 5, is_default_null);
81
void plugin::TableFunction::add_field(const char *label,
82
TableFunction::ColumnType type,
83
uint32_t field_length,
86
drizzled::message::Table::Field *field;
87
drizzled::message::Table::Field::FieldOptions *field_options;
88
drizzled::message::Table::Field::FieldConstraints *field_constraints;
90
field= proto.add_field();
91
field->set_name(label);
93
field_options= field->mutable_options();
94
field_constraints= field->mutable_constraints();
95
field_options->set_default_null(is_default_null);
96
field_constraints->set_is_notnull(not is_default_null);
100
case TableFunction::STRING:
102
drizzled::message::Table::Field::StringFieldOptions *string_field_options;
103
if (field_length >= TABLE_FUNCTION_BLOB_SIZE)
105
field->set_type(drizzled::message::Table::Field::BLOB);
106
string_field_options= field->mutable_string_options();
107
string_field_options->set_collation_id(default_charset_info->number);
108
string_field_options->set_collation(default_charset_info->name);
112
field->set_type(drizzled::message::Table::Field::VARCHAR);
113
string_field_options= field->mutable_string_options();
114
string_field_options->set_length(field_length);
118
case TableFunction::VARBINARY:
120
drizzled::message::Table::Field::StringFieldOptions *string_field_options;
121
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
string_field_options->set_collation(my_charset_bin.csname);
126
string_field_options->set_collation_id(my_charset_bin.number);
129
case TableFunction::NUMBER:
130
field->set_type(drizzled::message::Table::Field::BIGINT);
132
case TableFunction::SIZE:
133
field->set_type(drizzled::message::Table::Field::BIGINT);
134
field_constraints->set_is_unsigned(true);
136
case TableFunction::BOOLEAN: // Currently BOOLEAN always has a value
137
field->set_type(drizzled::message::Table::Field::BOOLEAN);
138
field_constraints->set_is_unsigned(true);
143
plugin::TableFunction::Generator::Generator(Field **arg) :
145
session(current_session)
147
scs= system_charset_info;
150
bool plugin::TableFunction::Generator::sub_populate(uint32_t field_size)
155
columns_iterator= columns;
157
difference= columns_iterator - columns;
161
assert(difference == field_size);
167
void plugin::TableFunction::Generator::push(uint64_t arg)
169
(*columns_iterator)->store(static_cast<int64_t>(arg), true);
170
(*columns_iterator)->set_notnull();
174
void plugin::TableFunction::Generator::push(int64_t arg)
176
(*columns_iterator)->store(arg, false);
177
(*columns_iterator)->set_notnull();
181
void plugin::TableFunction::Generator::push(const char *arg, uint32_t length)
183
assert(columns_iterator);
184
assert(*columns_iterator);
186
length= length ? length : strlen(arg);
188
if ((*columns_iterator)->char_length() < length)
189
length= (*columns_iterator)->char_length();
191
(*columns_iterator)->store(arg, length, scs);
192
(*columns_iterator)->set_notnull();
196
void plugin::TableFunction::Generator::push()
198
/* Only accept NULLs */
199
assert((*columns_iterator)->maybe_null());
200
(*columns_iterator)->set_null();
204
void plugin::TableFunction::Generator::push(const std::string& arg)
206
push(arg.c_str(), arg.length());
209
void plugin::TableFunction::Generator::push(bool arg)
213
(*columns_iterator)->store("YES", 3, scs);
217
(*columns_iterator)->store("NO", 2, scs);
223
bool plugin::TableFunction::Generator::isWild(const std::string &predicate)
225
if (not getSession().lex->wild)
228
bool match= wild_case_compare(system_charset_info,
230
getSession().lex->wild->ptr());
235
} /* namespace drizzled */