Drizzled Public API Documentation

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) 2009 Sun Microsystems, Inc.
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/statement/create_schema.h>
26 #include <drizzled/schema.h>
27 #include <drizzled/plugin/event_observer.h>
28 #include <drizzled/message.h>
29 #include <drizzled/plugin/storage_engine.h>
30 #include <drizzled/sql_lex.h>
31 #include <drizzled/plugin/authorization.h>
32 
33 #include <string>
34 
35 using namespace std;
36 
37 namespace drizzled {
38 
39 bool statement::CreateSchema::execute()
40 {
41  if (not validateSchemaOptions())
42  return true;
43 
44  if (session().inTransaction())
45  {
46  my_error(ER_TRANSACTIONAL_DDL_NOT_SUPPORTED, MYF(0));
47  return true;
48  }
49 
50  identifier::Schema schema_identifier(to_string(lex().name));
51  if (not check(schema_identifier))
52  return false;
53 
54  drizzled::message::schema::init(schema_message, lex().name.data());
55 
56  message::set_definer(schema_message, *session().user());
57 
58  bool res = false;
59  std::string path = schema_identifier.getSQLPath();
60 
61  if (unlikely(plugin::EventObserver::beforeCreateDatabase(session(), path)))
62  {
63  my_error(ER_EVENT_OBSERVER_PLUGIN, MYF(0), path.c_str());
64  }
65  else
66  {
67  res= schema::create(session(), schema_message, lex().exists());
68  if (unlikely(plugin::EventObserver::afterCreateDatabase(session(), path, res)))
69  {
70  my_error(ER_EVENT_OBSERVER_PLUGIN, schema_identifier);
71  res = false;
72  }
73 
74  }
75 
76  return not res;
77 }
78 
79 bool statement::CreateSchema::check(const identifier::Schema &identifier)
80 {
81  if (not identifier.isValid())
82  return false;
83 
84  if (not plugin::Authorization::isAuthorized(*session().user(), identifier))
85  return false;
86 
87  if (not lex().exists())
88  {
89  if (plugin::StorageEngine::doesSchemaExist(identifier))
90  {
91  my_error(ER_DB_CREATE_EXISTS, identifier);
92 
93  return false;
94  }
95  }
96 
97  return true;
98 }
99 
100 // We don't actually test anything at this point, we assume it is all bad.
101 bool statement::CreateSchema::validateSchemaOptions()
102 {
103  size_t num_engine_options= schema_message.engine().options_size();
104  bool rc= num_engine_options ? false : true;
105 
106  for (size_t y= 0; y < num_engine_options; ++y)
107  {
108  my_error(ER_UNKNOWN_SCHEMA_OPTION, MYF(0),
109  schema_message.engine().options(y).name().c_str(),
110  schema_message.engine().options(y).state().c_str());
111 
112  rc= false;
113  }
114 
115  return rc;
116 }
117 
118 } /* namespace drizzled */