~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/plugin/table_function.cc

  • Committer: Monty Taylor
  • Date: 2009-12-25 08:50:15 UTC
  • mto: This revision was merged to the branch mainline in revision 1255.
  • Revision ID: mordred@inaugust.com-20091225085015-83sux5qsvy312gew
MEM_ROOT == memory::Root

Show diffs side-by-side

added added

removed removed

Lines of Context:
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/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>
29
 
 
30
 
#include <vector>
31
 
 
32
 
namespace drizzled
33
 
{
34
 
 
35
 
static TableFunctionContainer table_functions;
36
 
 
37
 
void plugin::TableFunction::init()
38
 
{
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);
43
 
}
44
 
 
45
 
bool plugin::TableFunction::addPlugin(plugin::TableFunction *tool)
46
 
{
47
 
  assert(tool != NULL);
48
 
  table_functions.addFunction(tool); 
49
 
  return false;
50
 
}
51
 
 
52
 
plugin::TableFunction *plugin::TableFunction::getFunction(const std::string &arg)
53
 
{
54
 
  return table_functions.getFunction(arg);
55
 
}
56
 
 
57
 
void plugin::TableFunction::getNames(const std::string &arg,
58
 
                                     std::set<std::string> &set_of_names)
59
 
{
60
 
  table_functions.getNames(arg, set_of_names);
61
 
}
62
 
 
63
 
plugin::TableFunction::Generator *plugin::TableFunction::generator(Field **arg)
64
 
{
65
 
  return new Generator(arg);
66
 
}
67
 
 
68
 
void plugin::TableFunction::add_field(const char *label,
69
 
                                      uint32_t field_length)
70
 
{
71
 
  add_field(label, TableFunction::STRING, field_length);
72
 
}
73
 
 
74
 
void plugin::TableFunction::add_field(const char *label,
75
 
                              TableFunction::ColumnType type,
76
 
                              bool is_default_null)
77
 
{
78
 
  add_field(label, type, 5, is_default_null);
79
 
}
80
 
 
81
 
void plugin::TableFunction::add_field(const char *label,
82
 
                                      TableFunction::ColumnType type,
83
 
                                      uint32_t field_length,
84
 
                                      bool is_default_null)
85
 
{
86
 
  drizzled::message::Table::Field *field;
87
 
  drizzled::message::Table::Field::FieldOptions *field_options;
88
 
  drizzled::message::Table::Field::FieldConstraints *field_constraints;
89
 
 
90
 
  field= proto.add_field();
91
 
  field->set_name(label);
92
 
 
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);
97
 
 
98
 
  switch (type) 
99
 
  {
100
 
  case TableFunction::STRING:
101
 
    {
102
 
      drizzled::message::Table::Field::StringFieldOptions *string_field_options;
103
 
      if (field_length >= TABLE_FUNCTION_BLOB_SIZE)
104
 
      {
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);
109
 
      }
110
 
      else
111
 
      {
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);
115
 
      }
116
 
    }
117
 
    break;
118
 
  case TableFunction::VARBINARY:
119
 
    {
120
 
      drizzled::message::Table::Field::StringFieldOptions *string_field_options;
121
 
      field->set_type(drizzled::message::Table::Field::VARCHAR);
122
 
 
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);
127
 
    }
128
 
    break;
129
 
  case TableFunction::NUMBER:
130
 
    field->set_type(drizzled::message::Table::Field::BIGINT);
131
 
    break;
132
 
  case TableFunction::SIZE:
133
 
    field->set_type(drizzled::message::Table::Field::BIGINT);
134
 
    field_constraints->set_is_unsigned(true);
135
 
    break;
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);
139
 
    break;
140
 
  }
141
 
}
142
 
 
143
 
plugin::TableFunction::Generator::Generator(Field **arg) :
144
 
  columns(arg),
145
 
  session(current_session)
146
 
{
147
 
  scs= system_charset_info;
148
 
}
149
 
 
150
 
bool plugin::TableFunction::Generator::sub_populate(uint32_t field_size)
151
 
{
152
 
  bool ret;
153
 
  uint64_t difference;
154
 
 
155
 
  columns_iterator= columns;
156
 
  ret= populate();
157
 
  difference= columns_iterator - columns;
158
 
 
159
 
  if (ret == true)
160
 
  {
161
 
    assert(difference == field_size);
162
 
  }
163
 
 
164
 
  return ret;
165
 
}
166
 
 
167
 
void plugin::TableFunction::Generator::push(uint64_t arg)
168
 
{
169
 
  (*columns_iterator)->store(static_cast<int64_t>(arg), true);
170
 
  (*columns_iterator)->set_notnull();
171
 
  columns_iterator++;
172
 
}
173
 
 
174
 
void plugin::TableFunction::Generator::push(int64_t arg)
175
 
{
176
 
  (*columns_iterator)->store(arg, false);
177
 
  (*columns_iterator)->set_notnull();
178
 
  columns_iterator++;
179
 
}
180
 
 
181
 
void plugin::TableFunction::Generator::push(const char *arg, uint32_t length)
182
 
{
183
 
  assert(columns_iterator);
184
 
  assert(*columns_iterator);
185
 
  assert(arg);
186
 
  length= length ? length : strlen(arg);
187
 
 
188
 
  if ((*columns_iterator)->char_length() < length)
189
 
    length= (*columns_iterator)->char_length();
190
 
 
191
 
  (*columns_iterator)->store(arg, length, scs);
192
 
  (*columns_iterator)->set_notnull();
193
 
  columns_iterator++;
194
 
}
195
 
 
196
 
void plugin::TableFunction::Generator::push()
197
 
{
198
 
  /* Only accept NULLs */
199
 
  assert((*columns_iterator)->maybe_null());
200
 
  (*columns_iterator)->set_null();
201
 
  columns_iterator++;
202
 
}
203
 
 
204
 
void plugin::TableFunction::Generator::push(const std::string& arg)
205
 
{
206
 
  push(arg.c_str(), arg.length());
207
 
}
208
 
 
209
 
void plugin::TableFunction::Generator::push(bool arg)
210
 
{
211
 
  if (arg)
212
 
  {
213
 
    (*columns_iterator)->store("YES", 3, scs);
214
 
  }
215
 
  else
216
 
  {
217
 
    (*columns_iterator)->store("NO", 2, scs);
218
 
  }
219
 
 
220
 
  columns_iterator++;
221
 
}
222
 
 
223
 
bool plugin::TableFunction::Generator::isWild(const std::string &predicate)
224
 
{
225
 
  if (not getSession().lex->wild)
226
 
    return false;
227
 
 
228
 
  bool match= wild_case_compare(system_charset_info,
229
 
                                predicate.c_str(),
230
 
                                getSession().lex->wild->ptr());
231
 
 
232
 
  return match;
233
 
}
234
 
 
235
 
} /* namespace drizzled */