Drizzled Public API Documentation

create_index.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_index.h>
26 #include <drizzled/statement/alter_table.h>
27 #include <drizzled/plugin/storage_engine.h>
28 #include <drizzled/open_tables_state.h>
29 
30 namespace drizzled
31 {
32 
33 namespace statement
34 {
35 
36 CreateIndex::CreateIndex(Session *in_session) :
37  CreateTable(in_session)
38  {
39  set_command(SQLCOM_CREATE_INDEX);
40  alter_info.flags.set(ALTER_ADD_INDEX);
41  lex().col_list.clear();
42  }
43 
45 {
46  TableList *first_table= (TableList *) lex().select_lex.table_list.first;
47  TableList *all_tables= lex().query_tables;
48 
49  /* Chicken/Egg... we need to search for the table, to know if the table exists, so we can build a full identifier from it */
50  message::table::shared_ptr original_table_message;
51  {
52  identifier::Table identifier(first_table->getSchemaName(), first_table->getTableName());
53  if (not (original_table_message= plugin::StorageEngine::getTableMessage(session(), identifier)))
54  {
55  my_error(ER_BAD_TABLE_ERROR, identifier);
56  return true;
57  }
58  }
59 
60  /*
61  CREATE INDEX and DROP INDEX are implemented by calling ALTER
62  TABLE with proper arguments.
63 
64  In the future ALTER TABLE will notice that the request is to
65  only add indexes and create these one by one for the existing
66  table without having to do a full rebuild.
67  */
68 
69  assert(first_table == all_tables && first_table != 0);
70  if (session().inTransaction())
71  {
72  my_error(ER_TRANSACTIONAL_DDL_NOT_SUPPORTED, MYF(0));
73  return true;
74  }
75 
76  bool res;
77  if (original_table_message->type() == message::Table::STANDARD )
78  {
79  identifier::Table identifier(first_table->getSchemaName(), first_table->getTableName());
80  create_info().default_table_charset= plugin::StorageEngine::getSchemaCollation(identifier);
81 
82  res= alter_table(&session(),
83  identifier,
84  identifier,
85  &create_info(),
86  *original_table_message,
87  createTableMessage(),
88  first_table,
89  &alter_info,
90  0, (Order*) 0, 0);
91  }
92  else
93  {
94  identifier::Table catch22(first_table->getSchemaName(), first_table->getTableName());
95  Table *table= session().open_tables.find_temporary_table(catch22);
96  assert(table);
97  {
98  identifier::Table identifier(first_table->getSchemaName(), first_table->getTableName(), table->getMutableShare()->getPath());
99  create_info().default_table_charset= plugin::StorageEngine::getSchemaCollation(identifier);
100 
101  res= alter_table(&session(),
102  identifier,
103  identifier,
104  &create_info(),
105  *original_table_message,
106  createTableMessage(),
107  first_table,
108  &alter_info,
109  0, (Order*) 0, 0);
110  }
111  }
112 
113  return res;
114 }
115 
116 } /* namespace statement */
117 } /* namespace drizzled */