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