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/plugin/registry.h"
26
#include "drizzled/global_charset_info.h"
27
#include "drizzled/session.h"
28
#include "drizzled/current_session.h"
37
static TableFunctionContainer table_functions;
39
void plugin::TableFunction::init()
41
drizzled::message::Table::StorageEngine *engine;
42
drizzled::message::Table::TableOptions *table_options;
44
proto.set_name(getTableLabel());
45
proto.set_schema(identifier.getSchemaName());
46
proto.set_type(drizzled::message::Table::FUNCTION);
47
proto.set_creation_timestamp(0);
48
proto.set_update_timestamp(0);
50
table_options= proto.mutable_options();
51
table_options->set_collation_id(default_charset_info->number);
52
table_options->set_collation(default_charset_info->name);
54
engine= proto.mutable_engine();
55
engine->set_name("FunctionEngine");
58
bool plugin::TableFunction::addPlugin(plugin::TableFunction *tool)
61
table_functions.addFunction(tool);
65
plugin::TableFunction *plugin::TableFunction::getFunction(const string &arg)
67
return table_functions.getFunction(arg);
70
void plugin::TableFunction::getNames(const string &arg,
71
set<std::string> &set_of_names)
73
table_functions.getNames(arg, set_of_names);
76
plugin::TableFunction::Generator *plugin::TableFunction::generator(Field **arg)
78
return new Generator(arg);
81
void plugin::TableFunction::add_field(const char *label,
82
uint32_t field_length)
84
add_field(label, TableFunction::STRING, field_length);
87
void plugin::TableFunction::add_field(const char *label,
88
TableFunction::ColumnType type,
91
add_field(label, type, 5, is_default_null);
94
void plugin::TableFunction::add_field(const char *label,
95
TableFunction::ColumnType type,
96
uint32_t field_length,
99
drizzled::message::Table::Field *field;
100
drizzled::message::Table::Field::FieldOptions *field_options;
101
drizzled::message::Table::Field::FieldConstraints *field_constraints;
103
field= proto.add_field();
104
field->set_name(label);
106
field_options= field->mutable_options();
107
field_constraints= field->mutable_constraints();
108
field_options->set_default_null(is_default_null);
109
field_constraints->set_is_nullable(is_default_null);
114
case TableFunction::BOOLEAN: // Currently BOOLEAN always has a value
116
field_options->set_default_null(false);
117
field_constraints->set_is_nullable(false);
118
case TableFunction::STRING:
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);
127
case TableFunction::VARBINARY:
129
drizzled::message::Table::Field::StringFieldOptions *string_field_options;
130
field->set_type(drizzled::message::Table::Field::VARCHAR);
132
string_field_options= field->mutable_string_options();
133
string_field_options->set_length(field_length);
134
string_field_options->set_collation(my_charset_bin.csname);
135
string_field_options->set_collation_id(my_charset_bin.number);
138
case TableFunction::NUMBER: // Currently NUMBER always has a value
139
field->set_type(drizzled::message::Table::Field::BIGINT);
140
field_options->set_default_null(false);
141
field_constraints->set_is_nullable(false);
146
plugin::TableFunction::Generator::Generator(Field **arg) :
148
session(current_session)
150
scs= system_charset_info;
153
bool plugin::TableFunction::Generator::sub_populate(uint32_t field_size)
158
columns_iterator= columns;
160
difference= columns_iterator - columns;
164
assert(difference == field_size);
170
void plugin::TableFunction::Generator::push(uint64_t arg)
172
(*columns_iterator)->store(static_cast<int64_t>(arg), true);
173
(*columns_iterator)->set_notnull();
177
void plugin::TableFunction::Generator::push(int64_t arg)
179
(*columns_iterator)->store(arg, false);
180
(*columns_iterator)->set_notnull();
184
void plugin::TableFunction::Generator::push(const char *arg, uint32_t length)
186
assert(columns_iterator);
187
assert(*columns_iterator);
189
length= length ? length : strlen(arg);
191
(*columns_iterator)->store(arg, length, scs);
192
(*columns_iterator)->set_notnull();
196
void plugin::TableFunction::Generator::push()
198
assert((*columns_iterator)->type() == DRIZZLE_TYPE_VARCHAR);
199
(*columns_iterator)->set_null();
203
void plugin::TableFunction::Generator::push(const std::string& arg)
205
push(arg.c_str(), arg.length());
208
void plugin::TableFunction::Generator::push(bool arg)
212
(*columns_iterator)->store("TRUE", 4, scs);
216
(*columns_iterator)->store("FALSE", 5, scs);
222
bool plugin::TableFunction::Generator::isWild(const std::string &predicate)
224
if (not getSession().lex->wild)
227
bool match= wild_case_compare(system_charset_info,
229
getSession().lex->wild->ptr());
234
} /* namespace drizzled */