~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/plugin/table_function.cc

  • Committer: brian
  • Date: 2008-06-25 05:29:13 UTC
  • Revision ID: brian@localhost.localdomain-20080625052913-6upwo0jsrl4lnapl
clean slate

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