Drizzled Public API Documentation

show_create_schema.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 #include <plugin/show_dictionary/dictionary.h>
23 #include <drizzled/identifier.h>
24 #include <drizzled/message.h>
26 #include <string>
27 
28 using namespace std;
29 using namespace drizzled;
30 
31 ShowCreateSchema::ShowCreateSchema() :
32  show_dictionary::Show("SCHEMA_SQL_DEFINITION")
33 {
34  add_field("SCHEMA_NAME", plugin::TableFunction::STRING, MAXIMUM_IDENTIFIER_LENGTH, false);
35  add_field("SCHEMA_SQL_DEFINITION", plugin::TableFunction::STRING, TABLE_FUNCTION_BLOB_SIZE, false);
36 }
37 
38 ShowCreateSchema::Generator::Generator(Field **arg) :
39  show_dictionary::Show::Generator(arg),
40  if_not_exists(false)
41 {
42  if (not isShowQuery())
43  return;
44 
45  statement::Show& select= static_cast<statement::Show&>(statement());
46 
47  if (not select.getShowSchema().empty())
48  {
49  schema_name.append(select.getShowTable());
50  identifier::Schema identifier(select.getShowSchema());
51 
52  if (not plugin::Authorization::isAuthorized(*getSession().user(),
53  identifier, false))
54  {
55  drizzled::error::access(*getSession().user(), identifier);
56  return;
57  }
58 
59  schema_message= plugin::StorageEngine::getSchemaDefinition(identifier);
60 
61  if_not_exists= select.getShowExists();
62  }
63 }
64 
65 bool ShowCreateSchema::Generator::populate()
66 {
67  if (not schema_message)
68  return false;
69 
70  std::string buffer;
71 
72  /* This needs to be moved out to its own function */
73  {
74  buffer.append("CREATE DATABASE ");
75 
76  if (if_not_exists)
77  buffer.append("IF NOT EXISTS ");
78 
79  buffer.append("`");
80  buffer.append(schema_message->name());
81  buffer.append("`");
82 
83  if (schema_message->has_collation())
84  {
85  buffer.append(" COLLATE = ");
86  buffer.append(schema_message->collation());
87  }
88 
89  if (not message::is_replicated(*schema_message))
90  {
91  buffer.append(" REPLICATE = FALSE");
92  }
93  }
94 
95  push(schema_message->name());
96  push(buffer);
97 
98  schema_message.reset();
99 
100  return true;
101 }