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