Drizzled Public API Documentation

alter_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 #include <drizzled/show.h>
23 #include <drizzled/session.h>
24 #include <drizzled/statement/alter_schema.h>
25 #include <drizzled/plugin/storage_engine.h>
26 #include <drizzled/schema.h>
27 #include <drizzled/message.h>
28 #include <drizzled/sql_lex.h>
29 
30 #include <drizzled/catalog/instance.h>
31 
32 #include <string>
33 
34 using namespace std;
35 
36 namespace drizzled {
37 
38 bool statement::AlterSchema::execute()
39 {
40  if (not validateSchemaOptions())
41  return true;
42 
43  identifier::Schema schema_identifier(session().catalog().identifier(),
44  lex().name);
45 
46  if (not schema::check(session(), schema_identifier))
47  {
48  my_error(ER_WRONG_DB_NAME, schema_identifier);
49  return false;
50  }
51 
52  identifier::Schema identifier(session().catalog().identifier(), lex().name);
53  message::schema::shared_ptr old_definition= plugin::StorageEngine::getSchemaDefinition(identifier);
54  if (not old_definition)
55  {
56  my_error(ER_SCHEMA_DOES_NOT_EXIST, identifier);
57  return true;
58  }
59 
60  if (session().inTransaction())
61  {
62  my_error(ER_TRANSACTIONAL_DDL_NOT_SUPPORTED, MYF(0));
63  return true;
64  }
65  /*
66  @todo right now the logic for alter schema is just sitting here, at some point this should be packaged up in a class/etc.
67  */
68 
69  // First initialize the schema message
70  drizzled::message::schema::init(schema_message, identifier);
71 
72  // We set the name from the old version to keep case preference
73  schema_message.set_version(old_definition->version());
74  schema_message.set_uuid(old_definition->uuid());
75  schema_message.mutable_engine()->set_name(old_definition->engine().name());
76 
77  // We need to make sure we don't destroy any collation that might have
78  // been changed.
79  if (not schema_message.has_collation())
80  {
81  schema_message.set_collation(old_definition->collation());
82  }
83 
84  drizzled::message::update(schema_message);
85 
86  return not schema::alter(session(), schema_message, *old_definition);
87 }
88 
89 } /* namespace drizzled */