Drizzled Public API Documentation

table_function.h
00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Definitions required for TableFunction plugin
00005  *
00006  *  Copyright (C) 2010 Sun Microsystems, Inc.
00007  *  Copyright (C) 2010 Monty Taylor
00008  *
00009  *  This program is free software; you can redistribute it and/or modify
00010  *  it under the terms of the GNU General Public License as published by
00011  *  the Free Software Foundation; version 2 of the License.
00012  *
00013  *  This program is distributed in the hope that it will be useful,
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  *  GNU General Public License for more details.
00017  *
00018  *  You should have received a copy of the GNU General Public License
00019  *  along with this program; if not, write to the Free Software
00020  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00021  */
00022 
00023 #pragma once
00024 
00025 #include <drizzled/definitions.h>
00026 #include <drizzled/plugin.h>
00027 #include <drizzled/plugin/plugin.h>
00028 #include <drizzled/identifier.h>
00029 #include <drizzled/message/table.pb.h>
00030 #include <drizzled/charset.h>
00031 #include <drizzled/field.h>
00032 
00033 #include <string>
00034 #include <set>
00035 #include <algorithm>
00036 
00037 #include <drizzled/visibility.h>
00038 
00039 namespace drizzled {
00040 namespace plugin {
00041 
00042 #define TABLE_FUNCTION_BLOB_SIZE 2049
00043 
00044 // Not thread safe, but plugins are just loaded in a single thread right
00045 // now.
00046 static const char *local_string_append(const char *arg1, const char *arg2)
00047 {
00048   static char buffer[1024];
00049   char *buffer_ptr= buffer;
00050   strcpy(buffer_ptr, arg1);
00051   buffer_ptr+= strlen(arg1);
00052   buffer_ptr[0]= '-';
00053   buffer_ptr++;
00054   strcpy(buffer_ptr, arg2);
00055 
00056   return buffer;
00057 }
00058 
00059 class DRIZZLED_API TableFunction : public Plugin
00060 {
00061   message::Table proto;
00062   identifier::Table identifier;
00063   std::string local_path;
00064   std::string original_table_label;
00065 
00066   void setName(); // init name
00067   void init();
00068 
00069 public:
00070   TableFunction(const char *schema_arg, const char *table_arg) :
00071     Plugin(local_string_append(schema_arg, table_arg) , "TableFunction"),
00072     identifier(schema_arg, table_arg),
00073     original_table_label(table_arg)
00074   {
00075     init();
00076   }
00077 
00078   static bool addPlugin(TableFunction *function);
00079   static void removePlugin(TableFunction *)
00080   { }
00081   static TableFunction *getFunction(const std::string &arg);
00082   static void getNames(const std::string &arg,
00083                        std::set<std::string> &set_of_names);
00084 
00085   enum ColumnType {
00086     BOOLEAN,
00087     NUMBER,
00088     STRING,
00089     VARBINARY,
00090     SIZE
00091   };
00092 
00093   class Generator
00094   {
00095     Field **columns;
00096     Field **columns_iterator;
00097     Session *session;
00098 
00099   protected:
00100     LEX& lex();
00101     statement::Statement& statement();
00102 
00103     drizzled::Session &getSession()
00104     {
00105       return *session;
00106     }
00107 
00108   public:
00109     const charset_info_st *scs;
00110 
00111     Generator(Field **arg);
00112     virtual ~Generator()
00113     { }
00114 
00115     /*
00116       Return type is bool meaning "are there more rows".
00117     */
00118     bool sub_populate(uint32_t field_size);
00119 
00120     virtual bool populate()
00121     {
00122       return false;
00123     }
00124 
00125     void push(uint64_t arg);
00126     void push(int64_t arg);
00127     void push(const char *arg, uint32_t length= 0);
00128     void push(str_ref);
00129     void push(bool arg);
00130     void push();
00131 
00132     bool isWild(const std::string &predicate);
00133   };
00134 
00135   void define(message::Table &arg)
00136   {
00137     arg.CopyFrom(proto);
00138   }
00139 
00140   const std::string &getTableLabel()
00141   {
00142     return original_table_label;
00143   }
00144 
00145   const std::string &getIdentifierTableName()
00146   {
00147     return identifier.getTableName();
00148   }
00149 
00150   const std::string &getSchemaHome()
00151   {
00152     return identifier.getSchemaName();
00153   }
00154 
00155   const std::string &getPath()
00156   {
00157     return identifier.getPath();
00158   }
00159 
00160   virtual Generator *generator(Field **arg);
00161 
00162   void add_field(const char *label,
00163                  message::Table::Field::FieldType type,
00164                  uint32_t length= 0);
00165 
00166   void add_field(const char *label,
00167                  uint32_t field_length= MAXIMUM_IDENTIFIER_LENGTH);
00168 
00169   void add_field(const char *label,
00170                  TableFunction::ColumnType type,
00171                  bool is_default_null= true);
00172 
00173   void add_field(const char *label,
00174                  TableFunction::ColumnType type,
00175                  uint32_t field_length,
00176                  bool is_default_null= false);
00177 
00178   virtual bool visible() const { return true; }
00179 };
00180 
00181 } /* namespace plugin */
00182 } /* namespace drizzled */
00183