~drizzle-trunk/drizzle/development

1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
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"
1273.13.38 by Brian Aker
Add in new show work.
26
#include "drizzled/session.h"
27
#include "drizzled/current_session.h"
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
28
29
#include <vector>
30
31
using namespace std;
32
1273.14.5 by Monty Taylor
Merged trunk.
33
namespace drizzled
34
{
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
35
36
static TableFunctionContainer table_functions;
37
38
void plugin::TableFunction::init()
39
{
1502.1.31 by Brian Aker
Merge engine options for schema/table.
40
  drizzled::message::Engine *engine;
1638.10.50 by Stewart Smith
revert patch that removse explicitly setting collation for talbe_function tables.
41
  drizzled::message::Table::TableOptions *table_options;
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
42
1340.1.5 by Brian Aker
Update, we now have all of the ANSI INFORMATION_SCHEMA listed.
43
  proto.set_name(getTableLabel());
1337.2.2 by Stewart Smith
Add Schema to table message. modify alter table to correctly set a schema when running ALTER TABLE. Also update show_table_message test results to reflect the new field.
44
  proto.set_schema(identifier.getSchemaName());
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
45
  proto.set_type(drizzled::message::Table::FUNCTION);
1340.1.4 by Brian Aker
A first pass through adding a timestamp to our proto.
46
  proto.set_creation_timestamp(0);
47
  proto.set_update_timestamp(0);
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
48
1638.10.50 by Stewart Smith
revert patch that removse explicitly setting collation for talbe_function tables.
49
  table_options= proto.mutable_options();
50
  table_options->set_collation_id(default_charset_info->number);
51
  table_options->set_collation(default_charset_info->name);
52
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
53
  engine= proto.mutable_engine();
54
  engine->set_name("FunctionEngine");
55
}
56
57
bool plugin::TableFunction::addPlugin(plugin::TableFunction *tool)
58
{
59
  assert(tool != NULL);
60
  table_functions.addFunction(tool); 
61
  return false;
62
}
63
64
plugin::TableFunction *plugin::TableFunction::getFunction(const string &arg)
65
{
66
  return table_functions.getFunction(arg);
67
}
68
69
void plugin::TableFunction::getNames(const string &arg,
70
                                     set<std::string> &set_of_names)
71
{
72
  table_functions.getNames(arg, set_of_names);
73
}
74
75
plugin::TableFunction::Generator *plugin::TableFunction::generator(Field **arg)
76
{
77
  return new Generator(arg);
78
}
79
80
void plugin::TableFunction::add_field(const char *label,
1273.13.44 by Brian Aker
Updates/bug fix for columns. Assert now for checking that we filled in all
81
                                      uint32_t field_length)
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
82
{
83
  add_field(label, TableFunction::STRING, field_length);
84
}
85
86
void plugin::TableFunction::add_field(const char *label,
87
                              TableFunction::ColumnType type,
88
                              bool is_default_null)
89
{
90
  add_field(label, type, 5, is_default_null);
91
}
92
93
void plugin::TableFunction::add_field(const char *label,
1643.3.10 by Brian Aker
Column support, clean up of IS/DD for NULL type.
94
                                      TableFunction::ColumnType type,
95
                                      uint32_t field_length,
96
                                      bool is_default_null)
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
97
{
98
  drizzled::message::Table::Field *field;
99
  drizzled::message::Table::Field::FieldOptions *field_options;
100
  drizzled::message::Table::Field::FieldConstraints *field_constraints;
101
102
  field= proto.add_field();
103
  field->set_name(label);
104
105
  field_options= field->mutable_options();
106
  field_constraints= field->mutable_constraints();
107
  field_options->set_default_null(is_default_null);
108
  field_constraints->set_is_nullable(is_default_null);
109
110
  switch (type) 
111
  {
112
  default:
1273.13.45 by Brian Aker
Update for test split.
113
  case TableFunction::BOOLEAN: // Currently BOOLEAN always has a value
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
114
    field_length= 5;
1273.13.45 by Brian Aker
Update for test split.
115
    field_options->set_default_null(false);
116
    field_constraints->set_is_nullable(false);
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
117
  case TableFunction::STRING:
1812.5.5 by Brian Aker
Added in possibility of having a blob type returned by a TableFunction.
118
    {
119
      drizzled::message::Table::Field::StringFieldOptions *string_field_options;
120
      if (field_length >= TABLE_FUNCTION_BLOB_SIZE)
121
      {
122
        field->set_type(drizzled::message::Table::Field::BLOB);
123
        string_field_options= field->mutable_string_options();
124
        string_field_options->set_collation_id(default_charset_info->number);
125
        string_field_options->set_collation(default_charset_info->name);
126
      }
127
      else
128
      {
129
        field->set_type(drizzled::message::Table::Field::VARCHAR);
130
        string_field_options= field->mutable_string_options();
131
        string_field_options->set_length(field_length);
132
      }
133
    }
1337.1.2 by Stewart Smith
make DATA_DICTIONARY.COLUMNS.COLUMN_DEFAULT be a VARBINARY column that can be null. Add a test for binary default values as well as non-binary default values.
134
    break;
135
  case TableFunction::VARBINARY:
1812.5.5 by Brian Aker
Added in possibility of having a blob type returned by a TableFunction.
136
    {
137
      drizzled::message::Table::Field::StringFieldOptions *string_field_options;
138
      field->set_type(drizzled::message::Table::Field::VARCHAR);
1337.1.2 by Stewart Smith
make DATA_DICTIONARY.COLUMNS.COLUMN_DEFAULT be a VARBINARY column that can be null. Add a test for binary default values as well as non-binary default values.
139
1812.5.5 by Brian Aker
Added in possibility of having a blob type returned by a TableFunction.
140
      string_field_options= field->mutable_string_options();
141
      string_field_options->set_length(field_length);
142
      string_field_options->set_collation(my_charset_bin.csname);
143
      string_field_options->set_collation_id(my_charset_bin.number);
144
    }
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
145
    break;
1273.13.45 by Brian Aker
Update for test split.
146
  case TableFunction::NUMBER: // Currently NUMBER always has a value
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
147
    field->set_type(drizzled::message::Table::Field::BIGINT);
148
    break;
149
  }
150
}
151
152
plugin::TableFunction::Generator::Generator(Field **arg) :
1436 by Brian Aker
Move toward not having to call current_session (first pass).
153
  columns(arg),
154
  session(current_session)
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
155
{
156
  scs= system_charset_info;
157
}
158
1273.13.44 by Brian Aker
Updates/bug fix for columns. Assert now for checking that we filled in all
159
bool plugin::TableFunction::Generator::sub_populate(uint32_t field_size)
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
160
{
1273.13.44 by Brian Aker
Updates/bug fix for columns. Assert now for checking that we filled in all
161
  bool ret;
162
  uint64_t difference;
163
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
164
  columns_iterator= columns;
1273.13.44 by Brian Aker
Updates/bug fix for columns. Assert now for checking that we filled in all
165
  ret= populate();
166
  difference= columns_iterator - columns;
167
168
  if (ret == true)
169
  {
170
    assert(difference == field_size);
171
  }
172
173
  return ret;
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
174
}
175
1302.2.1 by Monty Taylor
Fixed the OSX build issues.
176
void plugin::TableFunction::Generator::push(uint64_t arg)
177
{
1273.13.82 by Brian Aker
Revert OSX fix, requiring more cast.
178
  (*columns_iterator)->store(static_cast<int64_t>(arg), true);
1337.1.1 by Stewart Smith
fix setting notnull for NUMBER columns in a TableFunction.
179
  (*columns_iterator)->set_notnull();
1302.2.1 by Monty Taylor
Fixed the OSX build issues.
180
  columns_iterator++;
181
}
182
183
void plugin::TableFunction::Generator::push(int64_t arg)
184
{
185
  (*columns_iterator)->store(arg, false);
1337.1.1 by Stewart Smith
fix setting notnull for NUMBER columns in a TableFunction.
186
  (*columns_iterator)->set_notnull();
1302.2.1 by Monty Taylor
Fixed the OSX build issues.
187
  columns_iterator++;
188
}
189
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
190
void plugin::TableFunction::Generator::push(const char *arg, uint32_t length)
191
{
192
  assert(columns_iterator);
193
  assert(*columns_iterator);
194
  assert(arg);
1273.13.44 by Brian Aker
Updates/bug fix for columns. Assert now for checking that we filled in all
195
  length= length ? length : strlen(arg);
196
1273.13.45 by Brian Aker
Update for test split.
197
  (*columns_iterator)->store(arg, length, scs);
198
  (*columns_iterator)->set_notnull();
199
  columns_iterator++;
200
}
201
202
void plugin::TableFunction::Generator::push()
203
{
1643.3.10 by Brian Aker
Column support, clean up of IS/DD for NULL type.
204
#if 0 // @note this needs to be rewritten such that a drizzled::Field object can determine if it should ever be null
205
  assert((*columns_iterator)->getTable()->getShare()->getTableProto()->field((*columns_iterator)->getTable()->getFields() - columns_iterator).constraints().is_nullable());
206
#endif
1273.13.45 by Brian Aker
Update for test split.
207
  (*columns_iterator)->set_null();
1273.13.32 by Brian Aker
Big ole patch. This covers moving information_schema to old_* table names
208
  columns_iterator++;
209
}
210
1302.2.1 by Monty Taylor
Fixed the OSX build issues.
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
  {
1660 by Brian Aker
MErge in change to do YES/NO like standard requires.
220
    (*columns_iterator)->store("YES", 3, scs);
1302.2.1 by Monty Taylor
Fixed the OSX build issues.
221
  }
222
  else
223
  {
1660 by Brian Aker
MErge in change to do YES/NO like standard requires.
224
    (*columns_iterator)->store("NO", 2, scs);
1302.2.1 by Monty Taylor
Fixed the OSX build issues.
225
  }
226
227
  columns_iterator++;
228
}
229
1273.13.38 by Brian Aker
Add in new show work.
230
bool plugin::TableFunction::Generator::isWild(const std::string &predicate)
231
{
1436 by Brian Aker
Move toward not having to call current_session (first pass).
232
  if (not getSession().lex->wild)
1273.13.38 by Brian Aker
Add in new show work.
233
    return false;
234
1273.13.61 by Brian Aker
Fixes a memory leak. sql_string() sucks.
235
  bool match= wild_case_compare(system_charset_info,
236
                                predicate.c_str(),
1436 by Brian Aker
Move toward not having to call current_session (first pass).
237
                                getSession().lex->wild->ptr());
1273.13.38 by Brian Aker
Add in new show work.
238
239
  return match;
240
}
1273.14.5 by Monty Taylor
Merged trunk.
241
242
} /* namespace drizzled */