Drizzled Public API Documentation

alter_schema.cc
00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2009 Sun Microsystems, Inc.
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019  */
00020 
00021 #include <config.h>
00022 #include <drizzled/show.h>
00023 #include <drizzled/session.h>
00024 #include <drizzled/statement/alter_schema.h>
00025 #include <drizzled/plugin/storage_engine.h>
00026 #include <drizzled/schema.h>
00027 #include <drizzled/message.h>
00028 #include <drizzled/sql_lex.h>
00029 
00030 #include <string>
00031 
00032 using namespace std;
00033 
00034 namespace drizzled {
00035 
00036 bool statement::AlterSchema::execute()
00037 {
00038   if (not validateSchemaOptions())
00039     return true;
00040 
00041   identifier::Schema schema_identifier(lex().name);
00042 
00043   if (not schema::check(session(), schema_identifier))
00044   {
00045     my_error(ER_WRONG_DB_NAME, schema_identifier);
00046     return false;
00047   }
00048 
00049   identifier::Schema identifier(lex().name);
00050   message::schema::shared_ptr old_definition= plugin::StorageEngine::getSchemaDefinition(identifier);
00051   if (not old_definition)
00052   {
00053     my_error(ER_SCHEMA_DOES_NOT_EXIST, identifier); 
00054     return true;
00055   }
00056 
00057   if (session().inTransaction())
00058   {
00059     my_error(ER_TRANSACTIONAL_DDL_NOT_SUPPORTED, MYF(0));
00060     return true;
00061   }
00062   /*
00063     @todo right now the logic for alter schema is just sitting here, at some point this should be packaged up in a class/etc.
00064   */
00065 
00066   // First initialize the schema message
00067   drizzled::message::schema::init(schema_message, old_definition->name());
00068 
00069   // We set the name from the old version to keep case preference
00070   schema_message.set_version(old_definition->version());
00071   schema_message.set_uuid(old_definition->uuid());
00072   schema_message.mutable_engine()->set_name(old_definition->engine().name());
00073 
00074   // We need to make sure we don't destroy any collation that might have
00075   // been changed.
00076   if (not schema_message.has_collation())
00077   {
00078     schema_message.set_collation(old_definition->collation());
00079   }
00080   
00081   drizzled::message::update(schema_message);
00082 
00083   return not schema::alter(session(), schema_message, *old_definition);
00084 }
00085 
00086 } /* namespace drizzled */