Drizzled Public API Documentation

table_function.h
1 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3  *
4  * Definitions required for TableFunction plugin
5  *
6  * Copyright (C) 2010 Sun Microsystems, Inc.
7  * Copyright (C) 2010 Monty Taylor
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; version 2 of the License.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #pragma once
24 
25 #include <drizzled/definitions.h>
26 #include <drizzled/plugin.h>
27 #include <drizzled/plugin/plugin.h>
28 #include <drizzled/identifier.h>
29 #include <drizzled/message/table.pb.h>
30 #include <drizzled/charset.h>
31 #include <drizzled/field.h>
32 
33 #include <string>
34 #include <set>
35 #include <algorithm>
36 
37 #include <drizzled/visibility.h>
38 
39 namespace drizzled {
40 namespace plugin {
41 
42 #define TABLE_FUNCTION_BLOB_SIZE 2049
43 
44 // Not thread safe, but plugins are just loaded in a single thread right
45 // now.
46 static const char *local_string_append(const char *arg1, const char *arg2)
47 {
48  static char buffer[1024];
49  char *buffer_ptr= buffer;
50  strcpy(buffer_ptr, arg1);
51  buffer_ptr+= strlen(arg1);
52  buffer_ptr[0]= '-';
53  buffer_ptr++;
54  strcpy(buffer_ptr, arg2);
55 
56  return buffer;
57 }
58 
60 {
61  message::Table proto;
62  identifier::Table identifier;
63  std::string local_path;
64  std::string original_table_label;
65 
66  void setName(); // init name
67  void init();
68 
69 public:
70  TableFunction(const char *schema_arg, const char *table_arg) :
71  Plugin(local_string_append(schema_arg, table_arg) , "TableFunction"),
72  identifier(schema_arg, table_arg),
73  original_table_label(table_arg)
74  {
75  init();
76  }
77 
78  static bool addPlugin(TableFunction *function);
79  static void removePlugin(TableFunction *)
80  { }
81  static TableFunction *getFunction(const std::string &arg);
82  static void getNames(const std::string &arg,
83  std::set<std::string> &set_of_names);
84 
85  enum ColumnType {
86  BOOLEAN,
87  NUMBER,
88  STRING,
89  VARBINARY,
90  SIZE
91  };
92 
93  class Generator
94  {
95  Field **columns;
96  Field **columns_iterator;
97  Session *session;
98 
99  protected:
100  LEX& lex();
101  statement::Statement& statement();
102 
103  drizzled::Session &getSession()
104  {
105  return *session;
106  }
107 
108  public:
109  const charset_info_st *scs;
110 
111  Generator(Field **arg);
112  virtual ~Generator()
113  { }
114 
115  /*
116  Return type is bool meaning "are there more rows".
117  */
118  bool sub_populate(uint32_t field_size);
119 
120  virtual bool populate()
121  {
122  return false;
123  }
124 
125  void push(uint64_t arg);
126  void push(int64_t arg);
127  void push(const char *arg, uint32_t length= 0);
128  void push(str_ref);
129  void push(bool arg);
130  void push();
131 
132  bool isWild(const std::string &predicate);
133  };
134 
135  void define(message::Table &arg)
136  {
137  arg.CopyFrom(proto);
138  }
139 
140  const std::string &getTableLabel()
141  {
142  return original_table_label;
143  }
144 
145  const std::string &getIdentifierTableName()
146  {
147  return identifier.getTableName();
148  }
149 
150  const std::string &getSchemaHome()
151  {
152  return identifier.getSchemaName();
153  }
154 
155  const std::string &getPath()
156  {
157  return identifier.getPath();
158  }
159 
160  virtual Generator *generator(Field **arg);
161 
162  void add_field(const char *label,
163  message::Table::Field::FieldType type,
164  uint32_t length= 0);
165 
166  void add_field(const char *label,
167  uint32_t field_length= MAXIMUM_IDENTIFIER_LENGTH);
168 
169  void add_field(const char *label,
170  TableFunction::ColumnType type,
171  bool is_default_null= true);
172 
173  void add_field(const char *label,
174  TableFunction::ColumnType type,
175  uint32_t field_length,
176  bool is_default_null= false);
177 
178  virtual bool visible() const { return true; }
179 };
180 
181 } /* namespace plugin */
182 } /* namespace drizzled */
183