~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/statement/create_table.cc

  • Committer: Stewart Smith
  • Date: 2009-10-08 12:39:27 UTC
  • mto: This revision was merged to the branch mainline in revision 1179.
  • Revision ID: stewart@flamingspork.com-20091008123927-qpf9hog04w4xc5aj
make directory_file_name() static to mysys/my_lib.cc

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
19
 */
20
20
 
21
 
#include "config.h"
 
21
#include <drizzled/server_includes.h>
22
22
#include <drizzled/show.h>
23
23
#include <drizzled/lock.h>
24
24
#include <drizzled/session.h>
25
25
#include <drizzled/statement/create_table.h>
26
 
#include <drizzled/table_identifier.h>
27
 
 
28
 
#include <iostream>
29
 
 
30
 
namespace drizzled
31
 
{
 
26
 
 
27
using namespace drizzled;
32
28
 
33
29
bool statement::CreateTable::execute()
34
30
{
40
36
  bool need_start_waiting= false;
41
37
  bool res= false;
42
38
  bool link_to_local= false;
43
 
  bool lex_identified_temp_table= 
44
 
    create_table_message.type() == message::Table::TEMPORARY;
45
39
 
46
 
  if (is_engine_set)
 
40
  if (create_info.used_fields & HA_CREATE_USED_ENGINE)
47
41
  {
 
42
 
48
43
    create_info.db_type= 
49
 
      plugin::StorageEngine::findByName(*session, create_table_message.engine().name());
 
44
      plugin::StorageEngine::findByName(session, create_table_proto.engine().name());
50
45
 
51
46
    if (create_info.db_type == NULL)
52
47
    {
53
48
      my_error(ER_UNKNOWN_STORAGE_ENGINE, MYF(0), 
54
 
               create_table_message.name().c_str());
 
49
               create_table_proto.name().c_str());
55
50
 
56
51
      return true;
57
52
    }
58
53
  }
59
54
  else /* We now get the default, place it in create_info, and put the engine name in table proto */
60
55
  {
61
 
    create_info.db_type= session->getDefaultStorageEngine();
 
56
    create_info.db_type= ha_default_storage_engine(session);
62
57
  }
63
58
 
64
59
  /* 
68
63
  {
69
64
    message::Table::StorageEngine *protoengine;
70
65
 
71
 
    protoengine= create_table_message.mutable_engine();
 
66
    protoengine= create_table_proto.mutable_engine();
72
67
    protoengine->set_name(create_info.db_type->getName());
73
68
  }
74
69
 
75
 
 
76
70
  /* If CREATE TABLE of non-temporary table, do implicit commit */
77
 
  if (not lex_identified_temp_table)
 
71
  if (! (create_info.options & HA_LEX_CREATE_TMP_TABLE))
78
72
  {
79
 
    if (not session->endActiveTransaction())
 
73
    if (! session->endActiveTransaction())
80
74
    {
81
75
      return true;
82
76
    }
85
79
  TableList *create_table= session->lex->unlink_first_table(&link_to_local);
86
80
  TableList *select_tables= session->lex->query_tables;
87
81
 
88
 
 
89
 
  /*
90
 
    Now that we have the engine, we can figure out the table identifier. We need the engine in order
91
 
    to determine if the table is transactional or not if it is temp.
92
 
  */
93
 
 
94
 
  create_table_message.set_schema(create_table->db);
95
 
 
96
 
  TableIdentifier new_table_identifier(create_table->db,
97
 
                                       create_table->table_name,
98
 
                                       create_table_message.type());
99
 
 
100
 
  if (create_table_precheck(new_table_identifier))
 
82
  if (create_table_precheck(session, select_tables, create_table))
101
83
  {
102
84
    /* put tables back for PS rexecuting */
103
85
    session->lex->link_first_table_back(create_table, link_to_local);
126
108
    session->lex->link_first_table_back(create_table, link_to_local);
127
109
    return true;
128
110
  }
129
 
 
130
111
  if (select_lex->item_list.elements)           // With select
131
112
  {
132
113
    select_result *result;
134
115
    select_lex->options|= SELECT_NO_UNLOCK;
135
116
    unit->set_limit(select_lex);
136
117
 
137
 
    if (not lex_identified_temp_table)
 
118
    if (! (create_info.options & HA_LEX_CREATE_TMP_TABLE))
138
119
    {
139
120
      session->lex->link_first_table_back(create_table, link_to_local);
140
121
      create_table->create= true;
141
122
    }
142
123
 
143
 
    if (not (res= session->openTablesLock(session->lex->query_tables)))
 
124
    if (! (res= session->openTablesLock(session->lex->query_tables)))
144
125
    {
145
126
      /*
146
127
         Is table which we are changing used somewhere in other parts
147
128
         of query
148
129
       */
149
 
      if (not lex_identified_temp_table)
 
130
      if (! (create_info.options & HA_LEX_CREATE_TMP_TABLE))
150
131
      {
151
132
        TableList *duplicate= NULL;
152
133
        create_table= session->lex->unlink_first_table(&link_to_local);
153
 
        if ((duplicate= unique_table(create_table, select_tables)))
 
134
        if ((duplicate= unique_table(session, create_table, select_tables, 0)))
154
135
        {
155
136
          my_error(ER_UPDATE_TABLE_USED, MYF(0), create_table->alias);
156
137
          /*
169
150
         needs to be created for every execution of a PS/SP.
170
151
       */
171
152
      if ((result= new select_create(create_table,
172
 
                                     is_if_not_exists,
173
153
                                     &create_info,
174
 
                                     create_table_message,
 
154
                                     &create_table_proto,
175
155
                                     &alter_info,
176
156
                                     select_lex->item_list,
177
157
                                     session->lex->duplicates,
178
158
                                     session->lex->ignore,
179
 
                                     select_tables,
180
 
                                     new_table_identifier)))
 
159
                                     select_tables)))
181
160
      {
182
161
        /*
183
162
           CREATE from SELECT give its Select_Lex for SELECT,
187
166
        delete result;
188
167
      }
189
168
    }
190
 
    else if (not lex_identified_temp_table)
 
169
    else if (! (create_info.options & HA_LEX_CREATE_TMP_TABLE))
191
170
    {
192
171
      create_table= session->lex->unlink_first_table(&link_to_local);
193
172
    }
194
173
  }
195
174
  else
196
175
  {
 
176
    /* So that CREATE TEMPORARY TABLE gets to binlog at commit/rollback */
 
177
    if (create_info.options & HA_LEX_CREATE_TMP_TABLE)
 
178
      session->options|= OPTION_KEEP_LOG;
197
179
    /* regular create */
198
 
    if (is_create_table_like)
 
180
    if (create_info.options & HA_LEX_CREATE_TABLE_LIKE)
199
181
    {
200
182
      res= mysql_create_like_table(session, 
201
 
                                   new_table_identifier,
202
183
                                   create_table, 
203
184
                                   select_tables,
204
 
                                   create_table_message,
205
 
                                   is_if_not_exists,
206
 
                                   is_engine_set);
 
185
                                   &create_info);
207
186
    }
208
187
    else
209
188
    {
210
 
 
211
 
      for (int32_t x= 0; x < alter_info.alter_proto.added_field_size(); x++)
212
 
      {
213
 
        message::Table::Field *field= create_table_message.add_field();
214
 
 
215
 
        *field= alter_info.alter_proto.added_field(x);
216
 
      }
217
 
 
218
189
      res= mysql_create_table(session, 
219
 
                              new_table_identifier,
 
190
                              create_table->db,
 
191
                              create_table->table_name, 
220
192
                              &create_info,
221
 
                              create_table_message,
 
193
                              &create_table_proto,
222
194
                              &alter_info, 
223
 
                              false, 
224
 
                              0,
225
 
                              is_if_not_exists);
 
195
                              0, 
 
196
                              0);
226
197
    }
227
 
 
228
 
    if (not res)
 
198
    if (! res)
229
199
    {
230
200
      session->my_ok();
231
201
    }
239
209
 
240
210
  return res;
241
211
}
242
 
 
243
 
} /* namespace drizzled */
244