Drizzled Public API Documentation

message.cc
1 /* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3  *
4  * Copyright (C) 2010 Brian Aker
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; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <config.h>
22 
23 #include <drizzled/show.h>
24 #include <drizzled/session.h>
25 #include <drizzled/schema.h>
26 #include <drizzled/plugin/event_observer.h>
27 #include <drizzled/message.h>
28 
29 #include <string>
30 
31 namespace drizzled {
32 namespace message {
33 
34 static const std::string PROGRAM_ERROR("PROGRAM_ERROR");
35 
36 // These are used to generate strings for types
37 static const std::string VARCHAR("VARCHAR");
38 static const std::string VARBINARY("VARBINARY");
39 static const std::string DOUBLE("DOUBLE");
40 static const std::string TEXT("TEXT");
41 static const std::string BLOB("BLOB");
42 static const std::string ENUM("ENUM");
43 static const std::string INTEGER("INTEGER");
44 static const std::string BIGINT("BIGINT");
45 static const std::string DECIMAL("DECIMAL");
46 static const std::string DATE("DATE");
47 static const std::string EPOCH("EPOCH");
48 static const std::string TIMESTAMP("TIMESTAMP");
49 static const std::string MICROTIME("MICROTIME");
50 static const std::string DATETIME("DATETIME");
51 static const std::string TIME("TIME");
52 static const std::string UUID("UUID");
53 static const std::string BOOLEAN("BOOLEAN");
54 static const std::string IPV6("IPV6");
55 
56 static const std::string UNDEFINED("UNDEFINED");
57 static const std::string RESTRICT("RESTRICT");
58 static const std::string CASCADE("CASCADE");
59 static const std::string SET_NULL("SET NULL");
60 static const std::string NO_ACTION("NO ACTION");
61 static const std::string SET_DEFAULT("SET DEFAULT");
62 
63 static const std::string YES("YES");
64 static const std::string NO("NO");
65 
66 static const std::string UNKNOWN_INDEX("UNKNOWN_INDEX");
67 static const std::string BTREE("BTREE");
68 static const std::string RTREE("RTREE");
69 static const std::string HASH("HASH");
70 static const std::string FULLTEXT("FULLTEXT");
71 
72 static const std::string MATCH_FULL("FULL");
73 static const std::string MATCH_PARTIAL("PARTIAL");
74 static const std::string MATCH_SIMPLE("SIMPLE");
75 
76 const static std::string STANDARD_STRING("STANDARD");
77 const static std::string TEMPORARY_STRING("TEMPORARY");
78 const static std::string INTERNAL_STRING("INTERNAL");
79 const static std::string FUNCTION_STRING("FUNCTION");
80 
81 void update(drizzled::message::Schema &arg)
82 {
83  arg.set_version(arg.version() + 1);
84  arg.set_update_timestamp(time(NULL));
85 }
86 
87 void update(drizzled::message::Table &arg)
88 {
89  arg.set_version(arg.version() + 1);
90  arg.set_update_timestamp(time(NULL));
91 }
92 
93 bool is_numeric(const message::Table::Field &field)
94 {
95  message::Table::Field::FieldType type= field.type();
96 
97  switch (type)
98  {
99  case message::Table::Field::DOUBLE:
100  case message::Table::Field::INTEGER:
101  case message::Table::Field::BIGINT:
102  case message::Table::Field::DECIMAL:
103  return true;
104  case message::Table::Field::BLOB:
105  case message::Table::Field::VARCHAR:
106  case message::Table::Field::ENUM:
107  case message::Table::Field::DATE:
108  case message::Table::Field::EPOCH:
109  case message::Table::Field::DATETIME:
110  case message::Table::Field::TIME:
111  case message::Table::Field::UUID:
112  case message::Table::Field::BOOLEAN:
113  case message::Table::Field::IPV6:
114  break;
115  }
116 
117  return false;
118 }
119 
120 const std::string &type(const message::Table::Field &field)
121 {
122  message::Table::Field::FieldType type= field.type();
123 
124  switch (type)
125  {
126  case message::Table::Field::VARCHAR:
127  return field.string_options().collation().compare("binary") ? VARCHAR : VARBINARY;
128  case message::Table::Field::DOUBLE:
129  return DOUBLE;
130  case message::Table::Field::BLOB:
131  return field.string_options().collation().compare("binary") ? TEXT : BLOB;
132  case message::Table::Field::ENUM:
133  return ENUM;
134  case message::Table::Field::INTEGER:
135  return INTEGER;
136  case message::Table::Field::BIGINT:
137  return BIGINT;
138  case message::Table::Field::DECIMAL:
139  return DECIMAL;
140  case message::Table::Field::DATE:
141  return DATE;
142  case message::Table::Field::EPOCH:
143  return TIMESTAMP;
144  case message::Table::Field::DATETIME:
145  return DATETIME;
146  case message::Table::Field::TIME:
147  return TIME;
148  case message::Table::Field::UUID:
149  return UUID;
150  case message::Table::Field::BOOLEAN:
151  return BOOLEAN;
152  case message::Table::Field::IPV6:
153  return IPV6;
154  }
155 
156  abort();
157 }
158 
159 const std::string &type(drizzled::message::Table::Field::FieldType type)
160 {
161  switch (type)
162  {
163  case message::Table::Field::VARCHAR:
164  return VARCHAR;
165  case message::Table::Field::DOUBLE:
166  return DOUBLE;
167  case message::Table::Field::BLOB:
168  return BLOB;
169  case message::Table::Field::ENUM:
170  return ENUM;
171  case message::Table::Field::INTEGER:
172  return INTEGER;
173  case message::Table::Field::BIGINT:
174  return BIGINT;
175  case message::Table::Field::DECIMAL:
176  return DECIMAL;
177  case message::Table::Field::DATE:
178  return DATE;
179  case message::Table::Field::EPOCH:
180  return EPOCH;
181  case message::Table::Field::DATETIME:
182  return DATETIME;
183  case message::Table::Field::TIME:
184  return TIME;
185  case message::Table::Field::UUID:
186  return UUID;
187  case message::Table::Field::BOOLEAN:
188  return BOOLEAN;
189  case message::Table::Field::IPV6:
190  return IPV6;
191  }
192 
193  abort();
194 }
195 
196 const std::string &type(drizzled::message::Table::ForeignKeyConstraint::ForeignKeyOption type)
197 {
198  switch (type)
199  {
200  case message::Table::ForeignKeyConstraint::OPTION_RESTRICT:
201  return RESTRICT;
202  case message::Table::ForeignKeyConstraint::OPTION_CASCADE:
203  return CASCADE;
204  case message::Table::ForeignKeyConstraint::OPTION_SET_NULL:
205  return SET_NULL;
206  case message::Table::ForeignKeyConstraint::OPTION_UNDEF:
207  case message::Table::ForeignKeyConstraint::OPTION_NO_ACTION:
208  return NO_ACTION;
209  case message::Table::ForeignKeyConstraint::OPTION_SET_DEFAULT:
210  return SET_DEFAULT;
211  }
212 
213  return NO_ACTION;
214 }
215 
216 const std::string &type(drizzled::message::Table::Index::IndexType type)
217 {
218  switch (type)
219  {
220  case message::Table::Index::UNKNOWN_INDEX:
221  return UNKNOWN_INDEX;
222  case message::Table::Index::BTREE:
223  return BTREE;
224  case message::Table::Index::RTREE:
225  return RTREE;
226  case message::Table::Index::HASH:
227  return HASH;
228  case message::Table::Index::FULLTEXT:
229  return FULLTEXT;
230  }
231 
232  assert(0);
233  return PROGRAM_ERROR;
234 }
235 
236 const std::string &type(drizzled::message::Table::ForeignKeyConstraint::ForeignKeyMatchOption type)
237 {
238  switch (type)
239  {
240  case message::Table::ForeignKeyConstraint::MATCH_FULL:
241  return MATCH_FULL;
242  case message::Table::ForeignKeyConstraint::MATCH_PARTIAL:
243  return MATCH_PARTIAL;
244  case message::Table::ForeignKeyConstraint::MATCH_UNDEFINED:
245  case message::Table::ForeignKeyConstraint::MATCH_SIMPLE:
246  return MATCH_SIMPLE;
247  }
248 
249  return MATCH_SIMPLE;
250 }
251 
252 const std::string &type(drizzled::message::Table::TableType type)
253 {
254  switch (type)
255  {
256  case message::Table::STANDARD:
257  return STANDARD_STRING;
258  case message::Table::TEMPORARY:
259  return TEMPORARY_STRING;
260  case message::Table::INTERNAL:
261  return INTERNAL_STRING;
262  case message::Table::FUNCTION:
263  return FUNCTION_STRING;
264  }
265 
266  assert(0);
267  return PROGRAM_ERROR;
268 }
269 
270 #if 0
271 std::ostream& operator<<(std::ostream& output, const message::Transaction &message)
272 {
273  std::string buffer;
274 
275  google::protobuf::TextFormat::PrintToString(message, &buffer);
276  output << buffer;
277 
278  return output;
279 }
280 
281 std::ostream& operator<<(std::ostream& output, const message::Table &message)
282 {
283  std::string buffer;
284 
285  google::protobuf::TextFormat::PrintToString(message, &buffer);
286  output << buffer;
287 
288  return output;
289 }
290 #endif
291 
292 
293 } /* namespace message */
294 } /* namespace drizzled */