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/plugin/table_function.h>
23
#include <drizzled/table_function_container.h>
24
#include <drizzled/gettext.h>
25
#include "drizzled/global_charset_info.h"
26
#include "drizzled/session.h"
27
#include "drizzled/current_session.h"
34
static TableFunctionContainer table_functions;
36
void plugin::TableFunction::init()
38
drizzled::message::Engine *engine;
39
drizzled::message::Table::TableOptions *table_options;
41
proto.set_name(getTableLabel());
42
proto.set_schema(identifier.getSchemaName());
43
proto.set_type(drizzled::message::Table::FUNCTION);
44
proto.set_creation_timestamp(0);
45
proto.set_update_timestamp(0);
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);
51
engine= proto.mutable_engine();
52
engine->set_name("FunctionEngine");
55
bool plugin::TableFunction::addPlugin(plugin::TableFunction *tool)
58
table_functions.addFunction(tool);
62
plugin::TableFunction *plugin::TableFunction::getFunction(const std::string &arg)
64
return table_functions.getFunction(arg);
67
void plugin::TableFunction::getNames(const std::string &arg,
68
std::set<std::string> &set_of_names)
70
table_functions.getNames(arg, set_of_names);
73
plugin::TableFunction::Generator *plugin::TableFunction::generator(Field **arg)
75
return new Generator(arg);
78
void plugin::TableFunction::add_field(const char *label,
79
uint32_t field_length)
81
add_field(label, TableFunction::STRING, field_length);
84
void plugin::TableFunction::add_field(const char *label,
85
TableFunction::ColumnType type,
88
add_field(label, type, 5, is_default_null);
91
void plugin::TableFunction::add_field(const char *label,
92
TableFunction::ColumnType type,
93
uint32_t field_length,
96
drizzled::message::Table::Field *field;
97
drizzled::message::Table::Field::FieldOptions *field_options;
98
drizzled::message::Table::Field::FieldConstraints *field_constraints;
100
field= proto.add_field();
101
field->set_name(label);
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);
111
case TableFunction::BOOLEAN: // Currently BOOLEAN always has a value
113
field_options->set_default_null(false);
114
field_constraints->set_is_nullable(false);
115
case TableFunction::STRING:
117
drizzled::message::Table::Field::StringFieldOptions *string_field_options;
118
if (field_length >= TABLE_FUNCTION_BLOB_SIZE)
120
field->set_type(drizzled::message::Table::Field::BLOB);
121
string_field_options= field->mutable_string_options();
122
string_field_options->set_collation_id(default_charset_info->number);
123
string_field_options->set_collation(default_charset_info->name);
127
field->set_type(drizzled::message::Table::Field::VARCHAR);
128
string_field_options= field->mutable_string_options();
129
string_field_options->set_length(field_length);
133
case TableFunction::VARBINARY:
135
drizzled::message::Table::Field::StringFieldOptions *string_field_options;
136
field->set_type(drizzled::message::Table::Field::VARCHAR);
138
string_field_options= field->mutable_string_options();
139
string_field_options->set_length(field_length);
140
string_field_options->set_collation(my_charset_bin.csname);
141
string_field_options->set_collation_id(my_charset_bin.number);
144
case TableFunction::NUMBER: // Currently NUMBER always has a value
145
field->set_type(drizzled::message::Table::Field::BIGINT);
150
plugin::TableFunction::Generator::Generator(Field **arg) :
152
session(current_session)
154
scs= system_charset_info;
157
bool plugin::TableFunction::Generator::sub_populate(uint32_t field_size)
162
columns_iterator= columns;
164
difference= columns_iterator - columns;
168
assert(difference == field_size);
174
void plugin::TableFunction::Generator::push(uint64_t arg)
176
(*columns_iterator)->store(static_cast<int64_t>(arg), true);
177
(*columns_iterator)->set_notnull();
181
void plugin::TableFunction::Generator::push(int64_t arg)
183
(*columns_iterator)->store(arg, false);
184
(*columns_iterator)->set_notnull();
188
void plugin::TableFunction::Generator::push(const char *arg, uint32_t length)
190
assert(columns_iterator);
191
assert(*columns_iterator);
193
length= length ? length : strlen(arg);
195
if ((*columns_iterator)->char_length() < length)
196
length= (*columns_iterator)->char_length();
198
(*columns_iterator)->store(arg, length, scs);
199
(*columns_iterator)->set_notnull();
203
void plugin::TableFunction::Generator::push()
205
/* Only accept NULLs */
206
assert((*columns_iterator)->maybe_null());
207
(*columns_iterator)->set_null();
211
void plugin::TableFunction::Generator::push(const std::string& arg)
213
push(arg.c_str(), arg.length());
216
void plugin::TableFunction::Generator::push(bool arg)
220
(*columns_iterator)->store("YES", 3, scs);
224
(*columns_iterator)->store("NO", 2, scs);
230
bool plugin::TableFunction::Generator::isWild(const std::string &predicate)
232
if (not getSession().lex->wild)
235
bool match= wild_case_compare(system_charset_info,
237
getSession().lex->wild->ptr());
242
} /* namespace drizzled */