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