Drizzled Public API Documentation

table_function.cc
00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2010 Monty Taylor
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; version 2 of the License.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  */
00019 
00020 #include <config.h>
00021 
00022 #include <drizzled/current_session.h>
00023 #include <drizzled/gettext.h>
00024 #include <drizzled/charset.h>
00025 #include <drizzled/plugin/table_function.h>
00026 #include <drizzled/session.h>
00027 #include <drizzled/show.h>
00028 #include <drizzled/table_function_container.h>
00029 #include <drizzled/sql_lex.h>
00030 
00031 #include <vector>
00032 
00033 namespace drizzled {
00034 
00035 static TableFunctionContainer table_functions;
00036 
00037 void plugin::TableFunction::init()
00038 {
00039   drizzled::message::table::init(proto, getTableLabel(), identifier.getSchemaName(), "FunctionEngine");
00040   proto.set_type(drizzled::message::Table::FUNCTION);
00041   proto.set_creation_timestamp(0);
00042   proto.set_update_timestamp(0);
00043   message::set_is_replicated(proto, false);
00044   message::set_definer(proto, SYSTEM_USER);
00045 }
00046 
00047 bool plugin::TableFunction::addPlugin(plugin::TableFunction *tool)
00048 {
00049   assert(tool != NULL);
00050   table_functions.addFunction(tool);
00051   return false;
00052 }
00053 
00054 plugin::TableFunction *plugin::TableFunction::getFunction(const std::string &arg)
00055 {
00056   return table_functions.getFunction(arg);
00057 }
00058 
00059 void plugin::TableFunction::getNames(const std::string &arg,
00060                                      std::set<std::string> &set_of_names)
00061 {
00062   table_functions.getNames(arg, set_of_names);
00063 }
00064 
00065 LEX& plugin::TableFunction::Generator::lex()
00066 {
00067   return getSession().lex();
00068 }
00069 
00070 statement::Statement& plugin::TableFunction::Generator::statement()
00071 {
00072   return *lex().statement;
00073 }
00074 
00075 plugin::TableFunction::Generator *plugin::TableFunction::generator(Field **arg)
00076 {
00077   return new Generator(arg);
00078 }
00079 
00080 void plugin::TableFunction::add_field(const char *label,
00081                                       uint32_t field_length)
00082 {
00083   add_field(label, TableFunction::STRING, field_length);
00084 }
00085 
00086 void plugin::TableFunction::add_field(const char *label,
00087                               TableFunction::ColumnType type,
00088                               bool is_default_null)
00089 {
00090   add_field(label, type, 5, is_default_null);
00091 }
00092 
00093 void plugin::TableFunction::add_field(const char *label,
00094                                       TableFunction::ColumnType type,
00095                                       uint32_t field_length,
00096                                       bool is_default_null)
00097 {
00098   drizzled::message::Table::Field *field;
00099   drizzled::message::Table::Field::FieldOptions *field_options;
00100   drizzled::message::Table::Field::FieldConstraints *field_constraints;
00101 
00102   field= proto.add_field();
00103   field->set_name(label);
00104 
00105   field_options= field->mutable_options();
00106   field_constraints= field->mutable_constraints();
00107   field_options->set_default_null(is_default_null);
00108   field_constraints->set_is_notnull(not is_default_null);
00109 
00110   switch (type)
00111   {
00112   case TableFunction::STRING:
00113     {
00114       drizzled::message::Table::Field::StringFieldOptions *string_field_options;
00115       if (field_length >= TABLE_FUNCTION_BLOB_SIZE)
00116       {
00117         field->set_type(drizzled::message::Table::Field::BLOB);
00118         string_field_options= field->mutable_string_options();
00119         string_field_options->set_collation_id(default_charset_info->number);
00120         string_field_options->set_collation(default_charset_info->name);
00121       }
00122       else
00123       {
00124         field->set_type(drizzled::message::Table::Field::VARCHAR);
00125         string_field_options= field->mutable_string_options();
00126         string_field_options->set_length(field_length);
00127       }
00128     }
00129     break;
00130   case TableFunction::VARBINARY:
00131     {
00132       drizzled::message::Table::Field::StringFieldOptions *string_field_options;
00133       field->set_type(drizzled::message::Table::Field::VARCHAR);
00134 
00135       string_field_options= field->mutable_string_options();
00136       string_field_options->set_length(field_length);
00137       string_field_options->set_collation(my_charset_bin.csname);
00138       string_field_options->set_collation_id(my_charset_bin.number);
00139     }
00140     break;
00141   case TableFunction::NUMBER:
00142     field->set_type(drizzled::message::Table::Field::BIGINT);
00143     break;
00144   case TableFunction::SIZE:
00145     field->set_type(drizzled::message::Table::Field::BIGINT);
00146     field_constraints->set_is_unsigned(true);
00147     break;
00148   case TableFunction::BOOLEAN: // Currently BOOLEAN always has a value
00149     field->set_type(drizzled::message::Table::Field::BOOLEAN);
00150     field_constraints->set_is_unsigned(true);
00151     break;
00152   }
00153 }
00154 
00155 plugin::TableFunction::Generator::Generator(Field **arg) :
00156   columns(arg),
00157   session(current_session)
00158 {
00159   scs= system_charset_info;
00160 }
00161 
00162 bool plugin::TableFunction::Generator::sub_populate(uint32_t field_size)
00163 {
00164   columns_iterator= columns;
00165   bool ret= populate();
00166   if (ret)
00167     assert(columns_iterator == columns + field_size);
00168   return ret;
00169 }
00170 
00171 void plugin::TableFunction::Generator::push(uint64_t arg)
00172 {
00173   (*columns_iterator)->store(static_cast<int64_t>(arg), true);
00174   (*columns_iterator)->set_notnull();
00175   columns_iterator++;
00176 }
00177 
00178 void plugin::TableFunction::Generator::push(int64_t arg)
00179 {
00180   (*columns_iterator)->store(arg, false);
00181   (*columns_iterator)->set_notnull();
00182   columns_iterator++;
00183 }
00184 
00185 void plugin::TableFunction::Generator::push(const char *arg, uint32_t length)
00186 {
00187   assert(columns_iterator);
00188   assert(*columns_iterator);
00189   if (arg && not length)
00190     length= strlen(arg);
00191 
00192   if ((*columns_iterator)->char_length() < length)
00193     length= (*columns_iterator)->char_length();
00194 
00195   (*columns_iterator)->store(arg, length, scs);
00196   (*columns_iterator)->set_notnull();
00197   columns_iterator++;
00198 }
00199 
00200 void plugin::TableFunction::Generator::push()
00201 {
00202   /* Only accept NULLs */
00203   assert((*columns_iterator)->maybe_null());
00204   (*columns_iterator)->set_null();
00205   columns_iterator++;
00206 }
00207 
00208 void plugin::TableFunction::Generator::push(str_ref arg)
00209 {
00210   push(arg.data(), arg.size());
00211 }
00212 
00213 void plugin::TableFunction::Generator::push(bool arg)
00214 {
00215   if (arg)
00216     (*columns_iterator)->store("YES", 3, scs);
00217   else
00218     (*columns_iterator)->store("NO", 2, scs);
00219   columns_iterator++;
00220 }
00221 
00222 bool plugin::TableFunction::Generator::isWild(const std::string &predicate)
00223 {
00224   return lex().wild ? wild_case_compare(system_charset_info, predicate.c_str(), lex().wild->ptr()) : false;
00225 }
00226 
00227 } /* namespace drizzled */