~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 *
 *  Copyright (C) 2010 Monty Taylor
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "config.h"

#include <drizzled/plugin/table_function.h>
#include <drizzled/table_function_container.h>
#include <drizzled/gettext.h>
#include "drizzled/global_charset_info.h"
#include "drizzled/session.h"
#include "drizzled/current_session.h"

#include <vector>

using namespace std;

namespace drizzled
{

static TableFunctionContainer table_functions;

void plugin::TableFunction::init()
{
  drizzled::message::Engine *engine;
  drizzled::message::Table::TableOptions *table_options;

  proto.set_name(getTableLabel());
  proto.set_schema(identifier.getSchemaName());
  proto.set_type(drizzled::message::Table::FUNCTION);
  proto.set_creation_timestamp(0);
  proto.set_update_timestamp(0);

  table_options= proto.mutable_options();
  table_options->set_collation_id(default_charset_info->number);
  table_options->set_collation(default_charset_info->name);

  engine= proto.mutable_engine();
  engine->set_name("FunctionEngine");
}

bool plugin::TableFunction::addPlugin(plugin::TableFunction *tool)
{
  assert(tool != NULL);
  table_functions.addFunction(tool); 
  return false;
}

plugin::TableFunction *plugin::TableFunction::getFunction(const string &arg)
{
  return table_functions.getFunction(arg);
}

void plugin::TableFunction::getNames(const string &arg,
                                     set<std::string> &set_of_names)
{
  table_functions.getNames(arg, set_of_names);
}

plugin::TableFunction::Generator *plugin::TableFunction::generator(Field **arg)
{
  return new Generator(arg);
}

void plugin::TableFunction::add_field(const char *label,
                                      uint32_t field_length)
{
  add_field(label, TableFunction::STRING, field_length);
}

void plugin::TableFunction::add_field(const char *label,
                              TableFunction::ColumnType type,
                              bool is_default_null)
{
  add_field(label, type, 5, is_default_null);
}

void plugin::TableFunction::add_field(const char *label,
                                      TableFunction::ColumnType type,
                                      uint32_t field_length,
                                      bool is_default_null)
{
  drizzled::message::Table::Field *field;
  drizzled::message::Table::Field::FieldOptions *field_options;
  drizzled::message::Table::Field::FieldConstraints *field_constraints;

  field= proto.add_field();
  field->set_name(label);

  field_options= field->mutable_options();
  field_constraints= field->mutable_constraints();
  field_options->set_default_null(is_default_null);
  field_constraints->set_is_nullable(is_default_null);

  switch (type) 
  {
  default:
  case TableFunction::BOOLEAN: // Currently BOOLEAN always has a value
    field_length= 5;
    field_options->set_default_null(false);
    field_constraints->set_is_nullable(false);
  case TableFunction::STRING:
    {
      drizzled::message::Table::Field::StringFieldOptions *string_field_options;
      if (field_length >= TABLE_FUNCTION_BLOB_SIZE)
      {
        field->set_type(drizzled::message::Table::Field::BLOB);
        string_field_options= field->mutable_string_options();
        string_field_options->set_collation_id(default_charset_info->number);
        string_field_options->set_collation(default_charset_info->name);
      }
      else
      {
        field->set_type(drizzled::message::Table::Field::VARCHAR);
        string_field_options= field->mutable_string_options();
        string_field_options->set_length(field_length);
      }
    }
    break;
  case TableFunction::VARBINARY:
    {
      drizzled::message::Table::Field::StringFieldOptions *string_field_options;
      field->set_type(drizzled::message::Table::Field::VARCHAR);

      string_field_options= field->mutable_string_options();
      string_field_options->set_length(field_length);
      string_field_options->set_collation(my_charset_bin.csname);
      string_field_options->set_collation_id(my_charset_bin.number);
    }
    break;
  case TableFunction::NUMBER: // Currently NUMBER always has a value
    field->set_type(drizzled::message::Table::Field::BIGINT);
    break;
  }
}

plugin::TableFunction::Generator::Generator(Field **arg) :
  columns(arg),
  session(current_session)
{
  scs= system_charset_info;
}

bool plugin::TableFunction::Generator::sub_populate(uint32_t field_size)
{
  bool ret;
  uint64_t difference;

  columns_iterator= columns;
  ret= populate();
  difference= columns_iterator - columns;

  if (ret == true)
  {
    assert(difference == field_size);
  }

  return ret;
}

void plugin::TableFunction::Generator::push(uint64_t arg)
{
  (*columns_iterator)->store(static_cast<int64_t>(arg), true);
  (*columns_iterator)->set_notnull();
  columns_iterator++;
}

void plugin::TableFunction::Generator::push(int64_t arg)
{
  (*columns_iterator)->store(arg, false);
  (*columns_iterator)->set_notnull();
  columns_iterator++;
}

void plugin::TableFunction::Generator::push(const char *arg, uint32_t length)
{
  assert(columns_iterator);
  assert(*columns_iterator);
  assert(arg);
  length= length ? length : strlen(arg);

  (*columns_iterator)->store(arg, length, scs);
  (*columns_iterator)->set_notnull();
  columns_iterator++;
}

void plugin::TableFunction::Generator::push()
{
#if 0 // @note this needs to be rewritten such that a drizzled::Field object can determine if it should ever be null
  assert((*columns_iterator)->getTable()->getShare()->getTableProto()->field((*columns_iterator)->getTable()->getFields() - columns_iterator).constraints().is_nullable());
#endif
  (*columns_iterator)->set_null();
  columns_iterator++;
}

void plugin::TableFunction::Generator::push(const std::string& arg)
{
  push(arg.c_str(), arg.length());
}

void plugin::TableFunction::Generator::push(bool arg)
{
  if (arg)
  {
    (*columns_iterator)->store("YES", 3, scs);
  }
  else
  {
    (*columns_iterator)->store("NO", 2, scs);
  }

  columns_iterator++;
}

bool plugin::TableFunction::Generator::isWild(const std::string &predicate)
{
  if (not getSession().lex->wild)
    return false;

  bool match= wild_case_compare(system_charset_info,
                                predicate.c_str(),
                                getSession().lex->wild->ptr());

  return match;
}

} /* namespace drizzled */