Drizzled Public API Documentation

show.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  * Copyright (C) 2008 Sun Microsystems, Inc.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; version 2 of the License.
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 
22 /* Function with list databases, tables or fields */
23 #include <config.h>
24 
25 #include <drizzled/data_home.h>
26 #include <drizzled/error.h>
27 #include <drizzled/internal/my_sys.h>
28 #include <drizzled/plugin/storage_engine.h>
29 #include <drizzled/session.h>
30 #include <drizzled/show.h>
31 #include <drizzled/sql_select.h>
32 #include <drizzled/statement/show.h>
33 #include <drizzled/statement/show_errors.h>
34 #include <drizzled/statement/show_warnings.h>
35 #include <drizzled/sql_lex.h>
36 #include <drizzled/table_ident.h>
37 
38 #include <sys/stat.h>
39 
40 #include <string>
41 #include <iostream>
42 #include <sstream>
43 #include <vector>
44 #include <algorithm>
45 
46 using namespace std;
47 
48 namespace drizzled {
49 
50 inline const char* str_or_nil(const char *str)
51 {
52  return str ? str : "<nil>";
53 }
54 
55 /*
56  Get the quote character for displaying an identifier.
57 
58  SYNOPSIS
59  get_quote_char_for_identifier()
60 
61  IMPLEMENTATION
62  Force quoting in the following cases:
63  - name is empty (for one, it is possible when we use this function for
64  quoting user and host names for DEFINER clause);
65  - name is a keyword;
66  - name includes a special character;
67  Otherwise identifier is quoted only if the option OPTION_QUOTE_SHOW_CREATE
68  is set.
69 
70  RETURN
71  EOF No quote character is needed
72  # Quote character
73 */
74 
75 int get_quote_char_for_identifier()
76 {
77  return '`';
78 }
79 
80 namespace show {
81 
82 bool buildSchemas(Session *session)
83 {
84  session->lex().sql_command= SQLCOM_SELECT;
85  session->lex().statement= new statement::Show(session);
86 
87  std::string column_name= "Database";
88  if (session->lex().wild)
89  {
90  column_name.append(" (");
91  column_name.append(session->lex().wild->ptr());
92  column_name.append(")");
93  }
94 
95  if (prepare_new_schema_table(session, session->lex(), session->lex().current_select->where ? "SCHEMAS" : "SHOW_SCHEMAS"))
96  return false;
97 
98  Item_field *my_field= new Item_field(&session->lex().current_select->context, NULL, NULL, "SCHEMA_NAME");
99  my_field->is_autogenerated_name= false;
100  my_field->set_name(column_name);
101 
102  session->add_item_to_list(my_field);
103  session->add_order_to_list(my_field, true);
104  return true;
105 }
106 
107 bool buildTables(Session *session, const char *ident)
108 {
109  session->lex().sql_command= SQLCOM_SELECT;
110 
111  drizzled::statement::Show *select= new statement::Show(session);
112  session->lex().statement= select;
113 
114  std::string column_name= "Tables_in_";
115 
116  util::string::ptr schema(session->schema());
117  if (ident)
118  {
119  identifier::Schema identifier= str_ref(ident);
120  column_name.append(ident);
121  session->lex().select_lex.db= ident;
122  if (not plugin::StorageEngine::doesSchemaExist(identifier))
123  {
124  my_error(ER_BAD_DB_ERROR, identifier);
125  }
126  select->setShowPredicate(ident, "");
127  }
128  else if (schema and not schema->empty())
129  {
130  column_name.append(*schema);
131  select->setShowPredicate(*schema, "");
132  }
133  else
134  {
135  my_error(ER_NO_DB_ERROR, MYF(0));
136  return false;
137  }
138 
139 
140  if (session->lex().wild)
141  {
142  column_name.append(" (");
143  column_name.append(session->lex().wild->ptr());
144  column_name.append(")");
145  }
146 
147  if (prepare_new_schema_table(session, session->lex(), "SHOW_TABLES"))
148  return false;
149 
150  Item_field *my_field= new Item_field(&session->lex().current_select->context, NULL, NULL, "TABLE_NAME");
151  my_field->is_autogenerated_name= false;
152  my_field->set_name(column_name);
153 
154  session->add_item_to_list(my_field);
155  session->add_order_to_list(my_field, true);
156  return true;
157 }
158 
159 bool buildTemporaryTables(Session *session)
160 {
161  session->lex().sql_command= SQLCOM_SELECT;
162  session->lex().statement= new statement::Show(session);
163 
164  if (prepare_new_schema_table(session, session->lex(), "SHOW_TEMPORARY_TABLES"))
165  return false;
166 
167  session->add_item_to_list( new Item_field(&session->lex().current_select->context, NULL, NULL, "*"));
168  session->lex().current_select->with_wild++;
169  return true;
170 }
171 
172 bool buildTableStatus(Session *session, const char *ident)
173 {
174  session->lex().sql_command= SQLCOM_SELECT;
175  drizzled::statement::Show *select= new statement::Show(session);
176  session->lex().statement= select;
177 
178  std::string column_name= "Tables_in_";
179 
180  util::string::ptr schema(session->schema());
181  if (ident)
182  {
183  session->lex().select_lex.db= ident;
184 
185  identifier::Schema identifier= str_ref(ident);
186  if (not plugin::StorageEngine::doesSchemaExist(identifier))
187  {
188  my_error(ER_BAD_DB_ERROR, identifier);
189  }
190 
191  select->setShowPredicate(ident, "");
192  }
193  else if (schema)
194  {
195  select->setShowPredicate(*schema, "");
196  }
197  else
198  {
199  my_error(ER_NO_DB_ERROR, MYF(0));
200  return false;
201  }
202 
203  if (prepare_new_schema_table(session, session->lex(), "SHOW_TABLE_STATUS"))
204  return false;
205 
206  session->add_item_to_list( new Item_field(&session->lex().current_select->context, NULL, NULL, "*"));
207  session->lex().current_select->with_wild++;
208  return true;
209 }
210 
211 bool buildEngineStatus(Session *session, str_ref)
212 {
213  session->lex().sql_command= SQLCOM_SELECT;
214  drizzled::statement::Show *select= new statement::Show(session);
215  session->lex().statement= select;
216 
217  my_error(ER_USE_DATA_DICTIONARY);
218  return false;
219 }
220 
221 bool buildColumns(Session *session, const char *schema_ident, Table_ident *table_ident)
222 {
223  session->lex().sql_command= SQLCOM_SELECT;
224 
225  drizzled::statement::Show *select= new statement::Show(session);
226  session->lex().statement= select;
227 
228  util::string::ptr schema(session->schema());
229  if (schema_ident)
230  {
231  select->setShowPredicate(schema_ident, table_ident->table.data());
232  }
233  else if (table_ident->db.data())
234  {
235  select->setShowPredicate(table_ident->db.data(), table_ident->table.data());
236  }
237  else if (schema)
238  {
239  select->setShowPredicate(*schema, table_ident->table.data());
240  }
241  else
242  {
243  my_error(ER_NO_DB_ERROR, MYF(0));
244  return false;
245  }
246 
247  {
248  drizzled::identifier::Table identifier(select->getShowSchema(), table_ident->table.data());
249  if (not plugin::StorageEngine::doesTableExist(*session, identifier))
250  {
251  my_error(ER_TABLE_UNKNOWN, identifier);
252  }
253  }
254 
255  if (prepare_new_schema_table(session, session->lex(), "SHOW_COLUMNS"))
256  return false;
257 
258  session->add_item_to_list( new Item_field(&session->lex().current_select->context, NULL, NULL, "*"));
259  session->lex().current_select->with_wild++;
260  return true;
261 }
262 
263 void buildSelectWarning(Session *session)
264 {
265  (void) create_select_for_variable(session, "warning_count");
266  session->lex().statement= new statement::Show(session);
267 }
268 
269 void buildSelectError(Session *session)
270 {
271  (void) create_select_for_variable(session, "error_count");
272  session->lex().statement= new statement::Show(session);
273 }
274 
275 void buildWarnings(Session *session)
276 {
277  session->lex().statement= new statement::ShowWarnings(session);
278 }
279 
280 void buildErrors(Session *session)
281 {
282  session->lex().statement= new statement::ShowErrors(session);
283 }
284 
285 bool buildIndex(Session *session, const char *schema_ident, Table_ident *table_ident)
286 {
287  session->lex().sql_command= SQLCOM_SELECT;
288  drizzled::statement::Show *select= new statement::Show(session);
289  session->lex().statement= select;
290 
291  util::string::ptr schema(session->schema());
292  if (schema_ident)
293  {
294  select->setShowPredicate(schema_ident, table_ident->table.data());
295  }
296  else if (table_ident->db.data())
297  {
298  select->setShowPredicate(table_ident->db.data(), table_ident->table.data());
299  }
300  else if (schema)
301  {
302  select->setShowPredicate(*schema, table_ident->table.data());
303  }
304  else
305  {
306  my_error(ER_NO_DB_ERROR, MYF(0));
307  return false;
308  }
309 
310  {
311  drizzled::identifier::Table identifier(select->getShowSchema(), table_ident->table.data());
312  if (not plugin::StorageEngine::doesTableExist(*session, identifier))
313  {
314  my_error(ER_TABLE_UNKNOWN, identifier);
315  }
316  }
317 
318  if (prepare_new_schema_table(session, session->lex(), "SHOW_INDEXES"))
319  return false;
320 
321  session->add_item_to_list(new Item_field(&session->lex().current_select->context, NULL, NULL, "*"));
322  session->lex().current_select->with_wild++;
323  return true;
324 }
325 
326 bool buildStatus(Session *session, const drizzled::sql_var_t is_global)
327 {
328  session->lex().sql_command= SQLCOM_SELECT;
329  session->lex().statement= new statement::Show(session);
330 
331  if (prepare_new_schema_table(session, session->lex(), is_global == OPT_GLOBAL ? "GLOBAL_STATUS" : "SESSION_STATUS"))
332  return false;
333 
334  Item_field *my_field= new Item_field(&session->lex().current_select->context, NULL, NULL, "VARIABLE_NAME");
335  my_field->is_autogenerated_name= false;
336  my_field->set_name("Variable_name");
337  session->add_item_to_list(my_field);
338  my_field= new Item_field(&session->lex().current_select->context, NULL, NULL, "VARIABLE_VALUE");
339  my_field->is_autogenerated_name= false;
340  my_field->set_name("Value");
341  session->add_item_to_list(my_field);
342  return true;
343 }
344 
345 bool buildCreateTable(Session *session, Table_ident *ident)
346 {
347  session->lex().sql_command= SQLCOM_SELECT;
348  statement::Show *select= new statement::Show(session);
349  session->lex().statement= select;
350 
351  if (session->lex().statement == NULL)
352  return false;
353 
354  if (prepare_new_schema_table(session, session->lex(), "TABLE_SQL_DEFINITION"))
355  return false;
356 
357  util::string::ptr schema(session->schema());
358  if (ident->db.data())
359  {
360  select->setShowPredicate(ident->db.data(), ident->table.data());
361  }
362  else if (schema)
363  {
364  select->setShowPredicate(*schema, ident->table.data());
365  }
366  else
367  {
368  my_error(ER_NO_DB_ERROR, MYF(0));
369  return false;
370  }
371 
372  Item_field *my_field= new Item_field(&session->lex().current_select->context, NULL, NULL, "TABLE_NAME");
373  my_field->is_autogenerated_name= false;
374  my_field->set_name("Table");
375  session->add_item_to_list(my_field);
376  my_field= new Item_field(&session->lex().current_select->context, NULL, NULL, "TABLE_SQL_DEFINITION");
377  my_field->is_autogenerated_name= false;
378  my_field->set_name("Create Table");
379  session->add_item_to_list(my_field);
380  return true;
381 }
382 
383 bool buildProcesslist(Session *session)
384 {
385  session->lex().sql_command= SQLCOM_SELECT;
386  session->lex().statement= new statement::Show(session);
387 
388  if (prepare_new_schema_table(session, session->lex(), "PROCESSLIST"))
389  return false;
390 
391  session->add_item_to_list( new Item_field(&session->lex().current_select->context, NULL, NULL, "*"));
392  session->lex().current_select->with_wild++;
393  return true;
394 }
395 
396 bool buildVariables(Session *session, const drizzled::sql_var_t is_global)
397 {
398  session->lex().sql_command= SQLCOM_SELECT;
399  session->lex().statement= new statement::Show(session);
400 
401  if (is_global == OPT_GLOBAL)
402  {
403  if (prepare_new_schema_table(session, session->lex(), "GLOBAL_VARIABLES"))
404  return false;
405  }
406  else
407  {
408  if (prepare_new_schema_table(session, session->lex(), "SESSION_VARIABLES"))
409  return false;
410  }
411 
412  Item_field *my_field= new Item_field(&session->lex().current_select->context, NULL, NULL, "VARIABLE_NAME");
413  my_field->is_autogenerated_name= false;
414  my_field->set_name("Variable_name");
415  session->add_item_to_list(my_field);
416  my_field= new Item_field(&session->lex().current_select->context, NULL, NULL, "VARIABLE_VALUE");
417  my_field->is_autogenerated_name= false;
418  my_field->set_name("Value");
419 
420  session->add_item_to_list(my_field);
421  return true;
422 }
423 
424 bool buildCreateSchema(Session *session, str_ref ident)
425 {
426  session->lex().sql_command= SQLCOM_SELECT;
427  drizzled::statement::Show *select= new statement::Show(session);
428  session->lex().statement= select;
429 
430  if (prepare_new_schema_table(session, session->lex(), "SCHEMA_SQL_DEFINITION"))
431  return false;
432 
433  util::string::ptr schema(session->schema());
434  if (ident.data())
435  {
436  select->setShowPredicate(ident.data());
437  }
438  else if (schema)
439  {
440  select->setShowPredicate(*schema);
441  }
442  else
443  {
444  my_error(ER_NO_DB_ERROR, MYF(0));
445  return false;
446  }
447 
448  Item_field *my_field= new Item_field(&session->lex().current_select->context, NULL, NULL, "SCHEMA_NAME");
449  my_field->is_autogenerated_name= false;
450  my_field->set_name("Database");
451  session->add_item_to_list(my_field);
452 
453  my_field= new Item_field(&session->lex().current_select->context, NULL, NULL, "SCHEMA_SQL_DEFINITION");
454  my_field->is_autogenerated_name= false;
455  my_field->set_name("Create Database");
456  session->add_item_to_list(my_field);
457  return true;
458 }
459 
460 bool buildDescribe(Session *session, Table_ident *ident)
461 {
462  session->lex().lock_option= TL_READ;
463  init_select(&session->lex());
464  session->lex().current_select->parsing_place= SELECT_LIST;
465  session->lex().sql_command= SQLCOM_SELECT;
466  drizzled::statement::Show *select= new statement::Show(session);
467  session->lex().statement= select;
468  session->lex().select_lex.db= 0;
469 
470  util::string::ptr schema(session->schema());
471  if (ident->db.data())
472  {
473  select->setShowPredicate(ident->db.data(), ident->table.data());
474  }
475  else if (schema)
476  {
477  select->setShowPredicate(*schema, ident->table.data());
478  }
479  else
480  {
481  my_error(ER_NO_DB_ERROR, MYF(0));
482  return false;
483  }
484 
485  {
486  drizzled::identifier::Table identifier(select->getShowSchema(), ident->table.data());
487  if (not plugin::StorageEngine::doesTableExist(*session, identifier))
488  {
489  my_error(ER_TABLE_UNKNOWN, identifier);
490  }
491  }
492 
493  if (prepare_new_schema_table(session, session->lex(), "SHOW_COLUMNS"))
494  {
495  return false;
496  }
497  session->add_item_to_list( new Item_field(&session->lex().current_select->context, NULL, NULL, "*"));
498  session->lex().current_select->with_wild++;
499  return true;
500 }
501 
502 }
503 } /* namespace drizzled */