~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/statement/create_index.cc

  • Committer: Brian Aker
  • Date: 2009-08-04 06:21:17 UTC
  • mfrom: (1108.2.2 merge)
  • Revision ID: brian@gaz-20090804062117-fef8x6y3ydzrvab3
Merge Brian

Show diffs side-by-side

added added

removed removed

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