~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/plugin/table_function.cc

  • Committer: Stewart Smith
  • Date: 2008-10-15 04:21:24 UTC
  • mto: This revision was merged to the branch mainline in revision 516.
  • Revision ID: stewart@flamingspork.com-20081015042124-kdmb74bcbky1k1nz
remove my_pthread_[gs]etspecific

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