Drizzled Public API Documentation

create_index.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 
00023 #include <drizzled/show.h>
00024 #include <drizzled/session.h>
00025 #include <drizzled/statement/create_index.h>
00026 #include <drizzled/statement/alter_table.h>
00027 #include <drizzled/plugin/storage_engine.h>
00028 
00029 namespace drizzled
00030 {
00031 
00032 namespace statement
00033 {
00034 
00035 CreateIndex::CreateIndex(Session *in_session, const drizzled::ha_build_method method_arg) :
00036   CreateTable(in_session)
00037   {
00038     set_command(SQLCOM_CREATE_INDEX);
00039     alter_info.flags.set(ALTER_ADD_INDEX);
00040     alter_info.build_method= method_arg;
00041     lex().col_list.clear();
00042   }
00043 
00044 bool statement::CreateIndex::execute()
00045 {
00046   TableList *first_table= (TableList *) lex().select_lex.table_list.first;
00047   TableList *all_tables= lex().query_tables;
00048 
00049   /* Chicken/Egg... we need to search for the table, to know if the table exists, so we can build a full identifier from it */
00050   message::table::shared_ptr original_table_message;
00051   {
00052     identifier::Table identifier(first_table->getSchemaName(), first_table->getTableName());
00053     if (not (original_table_message= plugin::StorageEngine::getTableMessage(session(), identifier)))
00054     {
00055       my_error(ER_BAD_TABLE_ERROR, identifier);
00056       return true;
00057     }
00058   }
00059 
00060   /*
00061     CREATE INDEX and DROP INDEX are implemented by calling ALTER
00062     TABLE with proper arguments.
00063 
00064     In the future ALTER TABLE will notice that the request is to
00065     only add indexes and create these one by one for the existing
00066     table without having to do a full rebuild.
00067   */
00068 
00069   assert(first_table == all_tables && first_table != 0);
00070   if (session().inTransaction())
00071   {
00072     my_error(ER_TRANSACTIONAL_DDL_NOT_SUPPORTED, MYF(0));
00073     return true;
00074   }
00075 
00076   bool res;
00077   if (original_table_message->type() == message::Table::STANDARD )
00078   {
00079     identifier::Table identifier(first_table->getSchemaName(), first_table->getTableName());
00080     create_info().default_table_charset= plugin::StorageEngine::getSchemaCollation(identifier);
00081 
00082     res= alter_table(&session(), 
00083                      identifier,
00084                      identifier,
00085                      &create_info(), 
00086                      *original_table_message,
00087                      createTableMessage(), 
00088                      first_table,
00089                      &alter_info,
00090                      0, (Order*) 0, 0);
00091   }
00092   else
00093   {
00094     identifier::Table catch22(first_table->getSchemaName(), first_table->getTableName());
00095     Table *table= session().find_temporary_table(catch22);
00096     assert(table);
00097     {
00098       identifier::Table identifier(first_table->getSchemaName(), first_table->getTableName(), table->getMutableShare()->getPath());
00099       create_info().default_table_charset= plugin::StorageEngine::getSchemaCollation(identifier);
00100 
00101       res= alter_table(&session(), 
00102                        identifier,
00103                        identifier,
00104                        &create_info(), 
00105                        *original_table_message,
00106                        createTableMessage(), 
00107                        first_table,
00108                        &alter_info,
00109                        0, (Order*) 0, 0);
00110     }
00111   }
00112 
00113   return res;
00114 }
00115 
00116 } /* namespace statement */
00117 } /* namespace drizzled */