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(getSession().catalog().identifier(),
51  select.getShowSchema());
52 
53  if (not plugin::Authorization::isAuthorized(*getSession().user(),
54  identifier, false))
55  {
56  drizzled::error::access(*getSession().user(), identifier);
57  return;
58  }
59 
60  schema_message= plugin::StorageEngine::getSchemaDefinition(identifier);
61 
62  if_not_exists= select.getShowExists();
63  }
64 }
65 
66 bool ShowCreateSchema::Generator::populate()
67 {
68  if (not schema_message)
69  return false;
70 
71  std::string buffer;
72 
73  /* This needs to be moved out to its own function */
74  {
75  buffer.append("CREATE DATABASE ");
76 
77  if (if_not_exists)
78  buffer.append("IF NOT EXISTS ");
79 
80  buffer.append("`");
81  buffer.append(schema_message->name());
82  buffer.append("`");
83 
84  if (schema_message->has_collation())
85  {
86  buffer.append(" COLLATE = ");
87  buffer.append(schema_message->collation());
88  }
89 
90  if (not message::is_replicated(*schema_message))
91  {
92  buffer.append(" REPLICATE = FALSE");
93  }
94  }
95 
96  push(schema_message->name());
97  push(buffer);
98 
99  schema_message.reset();
100 
101  return true;
102 }