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 
00041 namespace statement { class Statement; }
00042 
00043 class LEX;
00044 
00045 namespace plugin
00046 {
00047 
00048 #define TABLE_FUNCTION_BLOB_SIZE 2049
00049 
00050 // Not thread safe, but plugins are just loaded in a single thread right
00051 // now.
00052 static const char *local_string_append(const char *arg1, const char *arg2)
00053 {
00054   static char buffer[1024];
00055   char *buffer_ptr= buffer;
00056   strcpy(buffer_ptr, arg1);
00057   buffer_ptr+= strlen(arg1);
00058   buffer_ptr[0]= '-';
00059   buffer_ptr++;
00060   strcpy(buffer_ptr, arg2);
00061 
00062   return buffer;
00063 }
00064 
00065 class DRIZZLED_API TableFunction : public Plugin
00066 {
00067   TableFunction();
00068   TableFunction(const TableFunction &);
00069   TableFunction& operator=(const TableFunction &);
00070 
00071   message::Table proto;
00072   identifier::Table identifier;
00073   std::string local_path;
00074   std::string original_table_label;
00075 
00076   void setName(); // init name
00077 
00078   void init();
00079 
00080 
00081 public:
00082   TableFunction(const char *schema_arg, const char *table_arg) :
00083     Plugin(local_string_append(schema_arg, table_arg) , "TableFunction"),
00084     identifier(schema_arg, table_arg),
00085     original_table_label(table_arg)
00086   {
00087     init();
00088   }
00089 
00090   virtual ~TableFunction() {}
00091 
00092   static bool addPlugin(TableFunction *function);
00093   static void removePlugin(TableFunction *)
00094   { }
00095   static TableFunction *getFunction(const std::string &arg);
00096   static void getNames(const std::string &arg,
00097                        std::set<std::string> &set_of_names);
00098 
00099   enum ColumnType {
00100     BOOLEAN,
00101     NUMBER,
00102     STRING,
00103     VARBINARY,
00104     SIZE
00105   };
00106 
00107   class Generator
00108   {
00109     Field **columns;
00110     Field **columns_iterator;
00111     Session *session;
00112 
00113   protected:
00114     LEX& lex();
00115     statement::Statement& statement();
00116 
00117     drizzled::Session &getSession()
00118     {
00119       return *session;
00120     }
00121 
00122   public:
00123     const CHARSET_INFO *scs;
00124 
00125     Generator(Field **arg);
00126     virtual ~Generator()
00127     { }
00128 
00129     /*
00130       Return type is bool meaning "are there more rows".
00131     */
00132     bool sub_populate(uint32_t field_size);
00133 
00134     virtual bool populate()
00135     {
00136       return false;
00137     }
00138 
00139     void push(uint64_t arg);
00140     void push(int64_t arg);
00141     void push(const char *arg, uint32_t length= 0);
00142     void push(const std::string& arg);
00143     void push(bool arg);
00144     void push();
00145 
00146     bool isWild(const std::string &predicate);
00147   };
00148 
00149   void define(message::Table &arg)
00150   {
00151     arg.CopyFrom(proto);
00152   }
00153 
00154   const std::string &getTableLabel()
00155   {
00156     return original_table_label;
00157   }
00158 
00159   const std::string &getIdentifierTableName()
00160   {
00161     return identifier.getTableName();
00162   }
00163 
00164   const std::string &getSchemaHome()
00165   {
00166     return identifier.getSchemaName();
00167   }
00168 
00169   const std::string &getPath()
00170   {
00171     return identifier.getPath();
00172   }
00173 
00174   virtual Generator *generator(Field **arg);
00175 
00176   void add_field(const char *label,
00177                  message::Table::Field::FieldType type,
00178                  uint32_t length= 0);
00179 
00180   void add_field(const char *label,
00181                  uint32_t field_length= MAXIMUM_IDENTIFIER_LENGTH);
00182 
00183   void add_field(const char *label,
00184                  TableFunction::ColumnType type,
00185                  bool is_default_null= true);
00186 
00187   void add_field(const char *label,
00188                  TableFunction::ColumnType type,
00189                  uint32_t field_length,
00190                  bool is_default_null= false);
00191 
00192   virtual bool visible() const { return true; }
00193 };
00194 
00195 } /* namespace plugin */
00196 } /* namespace drizzled */
00197