~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/statement/create_table.cc

  • Committer: Olaf van der Spek
  • Date: 2011-03-23 10:31:37 UTC
  • mto: (2247.1.1 build)
  • mto: This revision was merged to the branch mainline in revision 2248.
  • Revision ID: olafvdspek@gmail.com-20110323103137-lwevis2tfchgu18u
Propogate return void

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 <config.h>
 
22
 
22
23
#include <drizzled/show.h>
23
24
#include <drizzled/lock.h>
24
25
#include <drizzled/session.h>
25
26
#include <drizzled/statement/create_table.h>
26
27
#include <drizzled/message.h>
27
28
#include <drizzled/identifier.h>
 
29
#include <drizzled/plugin/storage_engine.h>
 
30
#include <drizzled/select_create.h>
 
31
#include <drizzled/table_ident.h>
28
32
 
29
33
#include <iostream>
30
34
 
31
35
namespace drizzled
32
36
{
33
37
 
 
38
namespace statement {
 
39
 
 
40
CreateTable::CreateTable(Session *in_session, Table_ident *ident, bool is_temporary) :
 
41
  Statement(in_session),
 
42
  change(NULL),
 
43
  default_value(NULL),
 
44
  on_update_value(NULL),
 
45
  is_engine_set(false),
 
46
  is_create_table_like(false),
 
47
  lex_identified_temp_table(false),
 
48
  link_to_local(false),
 
49
  create_table_list(NULL)
 
50
{
 
51
  set_command(SQLCOM_CREATE_TABLE);
 
52
  createTableMessage().set_name(ident->table.str, ident->table.length);
 
53
#if 0
 
54
  createTableMessage().set_schema(ident->db.str, ident->db.length);
 
55
#endif
 
56
 
 
57
  if (is_temporary)
 
58
  {
 
59
    createTableMessage().set_type(message::Table::TEMPORARY);
 
60
  }
 
61
  else
 
62
  {
 
63
    createTableMessage().set_type(message::Table::STANDARD);
 
64
  }
 
65
}
 
66
 
 
67
CreateTable::CreateTable(Session *in_session) :
 
68
  Statement(in_session),
 
69
  change(NULL),
 
70
  default_value(NULL),
 
71
  on_update_value(NULL),
 
72
  is_engine_set(false),
 
73
  is_create_table_like(false),
 
74
  lex_identified_temp_table(false),
 
75
  link_to_local(false),
 
76
  create_table_list(NULL)
 
77
{
 
78
  set_command(SQLCOM_CREATE_TABLE);
 
79
}
 
80
 
 
81
} // namespace statement
 
82
 
34
83
bool statement::CreateTable::execute()
35
84
{
36
 
  TableList *first_table= (TableList *) session->lex->select_lex.table_list.first;
37
 
  TableList *all_tables= session->lex->query_tables;
 
85
  TableList *first_table= (TableList *) lex().select_lex.table_list.first;
 
86
  TableList *all_tables= lex().query_tables;
38
87
  assert(first_table == all_tables && first_table != 0);
39
 
  Select_Lex *select_lex= &session->lex->select_lex;
40
 
  Select_Lex_Unit *unit= &session->lex->unit;
41
88
  bool need_start_waiting= false;
42
 
  bool res= false;
43
 
  bool link_to_local= false;
44
 
  bool lex_identified_temp_table= 
45
 
    create_table_message.type() == message::Table::TEMPORARY;
 
89
  lex_identified_temp_table= createTableMessage().type() == message::Table::TEMPORARY;
 
90
 
 
91
  is_engine_set= not createTableMessage().engine().name().empty();
46
92
 
47
93
  if (is_engine_set)
48
94
  {
49
 
    create_info.db_type= 
50
 
      plugin::StorageEngine::findByName(*session, create_table_message.engine().name());
 
95
    create_info().db_type= 
 
96
      plugin::StorageEngine::findByName(session(), createTableMessage().engine().name());
51
97
 
52
 
    if (create_info.db_type == NULL)
 
98
    if (create_info().db_type == NULL)
53
99
    {
54
100
      my_error(ER_UNKNOWN_STORAGE_ENGINE, MYF(0), 
55
 
               create_table_message.engine().name().c_str());
 
101
               createTableMessage().engine().name().c_str());
56
102
 
57
103
      return true;
58
104
    }
59
105
  }
60
106
  else /* We now get the default, place it in create_info, and put the engine name in table proto */
61
107
  {
62
 
    create_info.db_type= session->getDefaultStorageEngine();
 
108
    create_info().db_type= session().getDefaultStorageEngine();
63
109
  }
64
110
 
65
111
  if (not validateCreateTableOption())
67
113
    return true;
68
114
  }
69
115
 
70
 
 
71
 
  /* If CREATE TABLE of non-temporary table, do implicit commit */
72
116
  if (not lex_identified_temp_table)
73
117
  {
74
 
    if (not session->endActiveTransaction())
 
118
    if (session().inTransaction())
75
119
    {
 
120
      my_error(ER_TRANSACTIONAL_DDL_NOT_SUPPORTED, MYF(0));
76
121
      return true;
77
122
    }
78
123
  }
79
124
  /* Skip first table, which is the table we are creating */
80
 
  TableList *create_table= session->lex->unlink_first_table(&link_to_local);
81
 
  TableList *select_tables= session->lex->query_tables;
82
 
 
83
 
  drizzled::message::init(create_table_message, create_table_message.name(), create_table->getSchemaName(), create_info.db_type->getName());
84
 
 
85
 
  TableIdentifier new_table_identifier(create_table->getSchemaName(),
86
 
                                       create_table->getTableName(),
87
 
                                       create_table_message.type());
 
125
  create_table_list= lex().unlink_first_table(&link_to_local);
 
126
 
 
127
  drizzled::message::table::init(createTableMessage(), createTableMessage().name(), create_table_list->getSchemaName(), create_info().db_type->getName());
 
128
 
 
129
  identifier::Table new_table_identifier(create_table_list->getSchemaName(),
 
130
                                       create_table_list->getTableName(),
 
131
                                       createTableMessage().type());
88
132
 
89
133
  if (not check(new_table_identifier))
90
134
  {
91
135
    /* put tables back for PS rexecuting */
92
 
    session->lex->link_first_table_back(create_table, link_to_local);
 
136
    lex().link_first_table_back(create_table_list, link_to_local);
93
137
    return true;
94
138
  }
95
139
 
96
140
  /* Might have been updated in create_table_precheck */
97
 
  create_info.alias= create_table->alias;
 
141
  create_info().alias= create_table_list->alias;
98
142
 
99
143
  /*
100
144
     The create-select command will open and read-lock the select table
109
153
     TABLE in the same way. That way we avoid that a new table is
110
154
     created during a gobal read lock.
111
155
   */
112
 
  if (! (need_start_waiting= not session->wait_if_global_read_lock(0, 1)))
 
156
  if (! (need_start_waiting= not session().wait_if_global_read_lock(0, 1)))
113
157
  {
114
158
    /* put tables back for PS rexecuting */
115
 
    session->lex->link_first_table_back(create_table, link_to_local);
 
159
    lex().link_first_table_back(create_table_list, link_to_local);
116
160
    return true;
117
161
  }
118
162
 
119
 
  if (select_lex->item_list.elements)           // With select
 
163
  bool res= executeInner(new_table_identifier);
 
164
 
 
165
  /*
 
166
    Release the protection against the global read lock and wake
 
167
    everyone, who might want to set a global read lock.
 
168
  */
 
169
  session().startWaitingGlobalReadLock();
 
170
 
 
171
  return res;
 
172
}
 
173
 
 
174
bool statement::CreateTable::executeInner(identifier::Table::const_reference new_table_identifier)
 
175
{
 
176
  bool res= false;
 
177
  Select_Lex *select_lex= &lex().select_lex;
 
178
  TableList *select_tables= lex().query_tables;
 
179
 
 
180
  do 
120
181
  {
121
 
    select_result *result;
122
 
 
123
 
    select_lex->options|= SELECT_NO_UNLOCK;
124
 
    unit->set_limit(select_lex);
125
 
 
126
 
    if (not lex_identified_temp_table)
127
 
    {
128
 
      session->lex->link_first_table_back(create_table, link_to_local);
129
 
      create_table->setCreate(true);
130
 
    }
131
 
 
132
 
    if (not (res= session->openTablesLock(session->lex->query_tables)))
133
 
    {
134
 
      /*
135
 
         Is table which we are changing used somewhere in other parts
136
 
         of query
137
 
       */
 
182
    if (select_lex->item_list.size())           // With select
 
183
    {
 
184
      Select_Lex_Unit *unit= &lex().unit;
 
185
      select_result *result;
 
186
 
 
187
      select_lex->options|= SELECT_NO_UNLOCK;
 
188
      unit->set_limit(select_lex);
 
189
 
138
190
      if (not lex_identified_temp_table)
139
191
      {
140
 
        TableList *duplicate= NULL;
141
 
        create_table= session->lex->unlink_first_table(&link_to_local);
142
 
        if ((duplicate= unique_table(create_table, select_tables)))
143
 
        {
144
 
          my_error(ER_UPDATE_TABLE_USED, MYF(0), create_table->alias);
 
192
        lex().link_first_table_back(create_table_list, link_to_local);
 
193
        create_table_list->setCreate(true);
 
194
      }
 
195
 
 
196
      if (not (res= session().openTablesLock(lex().query_tables)))
 
197
      {
 
198
        /*
 
199
          Is table which we are changing used somewhere in other parts
 
200
          of query
 
201
        */
 
202
        if (not lex_identified_temp_table)
 
203
        {
 
204
          TableList *duplicate= NULL;
 
205
          create_table_list= lex().unlink_first_table(&link_to_local);
 
206
 
 
207
          if ((duplicate= unique_table(create_table_list, select_tables)))
 
208
          {
 
209
            my_error(ER_UPDATE_TABLE_USED, MYF(0), create_table_list->alias);
 
210
            /* put tables back for PS rexecuting */
 
211
            lex().link_first_table_back(create_table_list, link_to_local);
 
212
 
 
213
            res= true;
 
214
            break;
 
215
          }
 
216
        }
 
217
 
 
218
        /*
 
219
          select_create is currently not re-execution friendly and
 
220
          needs to be created for every execution of a PS/SP.
 
221
        */
 
222
        if ((result= new select_create(create_table_list,
 
223
                                       lex().exists(),
 
224
                                       &create_info(),
 
225
                                       createTableMessage(),
 
226
                                       &alter_info,
 
227
                                       select_lex->item_list,
 
228
                                       lex().duplicates,
 
229
                                       lex().ignore,
 
230
                                       select_tables,
 
231
                                       new_table_identifier)))
 
232
        {
145
233
          /*
146
 
             Release the protection against the global read lock and wake
147
 
             everyone, who might want to set a global read lock.
148
 
           */
149
 
          session->startWaitingGlobalReadLock();
150
 
          /* put tables back for PS rexecuting */
151
 
          session->lex->link_first_table_back(create_table, link_to_local);
152
 
 
153
 
          return true;
 
234
            CREATE from SELECT give its Select_Lex for SELECT,
 
235
            and item_list belong to SELECT
 
236
          */
 
237
          res= handle_select(&session(), &lex(), result, 0);
 
238
          delete result;
154
239
        }
155
240
      }
156
 
 
157
 
      /*
158
 
         select_create is currently not re-execution friendly and
159
 
         needs to be created for every execution of a PS/SP.
160
 
       */
161
 
      if ((result= new select_create(create_table,
162
 
                                     is_if_not_exists,
163
 
                                     &create_info,
164
 
                                     create_table_message,
165
 
                                     &alter_info,
166
 
                                     select_lex->item_list,
167
 
                                     session->lex->duplicates,
168
 
                                     session->lex->ignore,
169
 
                                     select_tables,
170
 
                                     new_table_identifier)))
 
241
      else if (not lex_identified_temp_table)
171
242
      {
172
 
        /*
173
 
           CREATE from SELECT give its Select_Lex for SELECT,
174
 
           and item_list belong to SELECT
175
 
         */
176
 
        res= handle_select(session, session->lex, result, 0);
177
 
        delete result;
 
243
        create_table_list= lex().unlink_first_table(&link_to_local);
178
244
      }
179
245
    }
180
 
    else if (not lex_identified_temp_table)
181
 
    {
182
 
      create_table= session->lex->unlink_first_table(&link_to_local);
183
 
    }
184
 
  }
185
 
  else
186
 
  {
187
 
    /* regular create */
188
 
    if (is_create_table_like)
189
 
    {
190
 
      res= mysql_create_like_table(session, 
191
 
                                   new_table_identifier,
192
 
                                   create_table, 
193
 
                                   select_tables,
194
 
                                   create_table_message,
195
 
                                   is_if_not_exists,
196
 
                                   is_engine_set);
197
 
    }
198
246
    else
199
247
    {
200
 
 
201
 
      for (int32_t x= 0; x < alter_info.alter_proto.added_field_size(); x++)
202
 
      {
203
 
        message::Table::Field *field= create_table_message.add_field();
204
 
 
205
 
        *field= alter_info.alter_proto.added_field(x);
206
 
      }
207
 
 
208
 
      res= mysql_create_table(session, 
209
 
                              new_table_identifier,
210
 
                              &create_info,
211
 
                              create_table_message,
212
 
                              &alter_info, 
213
 
                              false, 
214
 
                              0,
215
 
                              is_if_not_exists);
216
 
    }
217
 
 
218
 
    if (not res)
219
 
    {
220
 
      session->my_ok();
221
 
    }
222
 
  }
223
 
 
224
 
  /*
225
 
     Release the protection against the global read lock and wake
226
 
     everyone, who might want to set a global read lock.
227
 
   */
228
 
  session->startWaitingGlobalReadLock();
 
248
      /* regular create */
 
249
      if (is_create_table_like)
 
250
      {
 
251
        res= create_like_table(&session(), 
 
252
                               new_table_identifier,
 
253
                               identifier::Table(select_tables->getSchemaName(),
 
254
                                                 select_tables->getTableName()),
 
255
                               createTableMessage(),
 
256
                               lex().exists(),
 
257
                               is_engine_set);
 
258
      }
 
259
      else
 
260
      {
 
261
 
 
262
        for (int32_t x= 0; x < alter_info.added_fields_proto.added_field_size(); x++)
 
263
        {
 
264
          message::Table::Field *field= createTableMessage().add_field();
 
265
 
 
266
          *field= alter_info.added_fields_proto.added_field(x);
 
267
        }
 
268
 
 
269
        res= create_table(&session(), 
 
270
                          new_table_identifier,
 
271
                          &create_info(),
 
272
                          createTableMessage(),
 
273
                          &alter_info, 
 
274
                          false, 
 
275
                          0,
 
276
                          lex().exists());
 
277
      }
 
278
 
 
279
      if (not res)
 
280
      {
 
281
        session().my_ok();
 
282
      }
 
283
    }
 
284
  } while (0);
229
285
 
230
286
  return res;
231
287
}
232
288
 
233
 
bool statement::CreateTable::check(const TableIdentifier &identifier)
 
289
bool statement::CreateTable::check(const identifier::Table &identifier)
234
290
{
235
291
  // Check table name for validity
236
292
  if (not identifier.isValid())
239
295
  // See if any storage engine objects to the name of the file
240
296
  if (not plugin::StorageEngine::canCreateTable(identifier))
241
297
  {
242
 
    my_error(ER_DBACCESS_DENIED_ERROR, MYF(0), "", "", identifier.getSchemaName().c_str());
 
298
    identifier::Schema schema_identifier= identifier;
 
299
    error::access(*session().user(), schema_identifier);
243
300
 
244
301
    return false;
245
302
  }
248
305
  // create for the table.
249
306
  if (not plugin::StorageEngine::doesSchemaExist(identifier))
250
307
  {
251
 
    my_error(ER_BAD_DB_ERROR, MYF(0), identifier.getSchemaName().c_str());
 
308
    identifier::Schema schema_identifier= identifier;
 
309
    my_error(ER_BAD_DB_ERROR, schema_identifier);
252
310
 
253
311
    return false;
254
312
  }
259
317
bool statement::CreateTable::validateCreateTableOption()
260
318
{
261
319
  bool rc= true;
262
 
  size_t num_engine_options= create_table_message.engine().options_size();
 
320
  size_t num_engine_options= createTableMessage().engine().options_size();
263
321
 
264
 
  assert(create_info.db_type);
 
322
  assert(create_info().db_type);
265
323
 
266
324
  for (size_t y= 0; y < num_engine_options; ++y)
267
325
  {
268
 
    bool valid= create_info.db_type->validateCreateTableOption(create_table_message.engine().options(y).name(),
269
 
                                                               create_table_message.engine().options(y).state());
 
326
    bool valid= create_info().db_type->validateCreateTableOption(createTableMessage().engine().options(y).name(),
 
327
                                                                 createTableMessage().engine().options(y).state());
270
328
 
271
329
    if (not valid)
272
330
    {
273
331
      my_error(ER_UNKNOWN_ENGINE_OPTION, MYF(0),
274
 
               create_table_message.engine().options(y).name().c_str(),
275
 
               create_table_message.engine().options(y).state().c_str());
 
332
               createTableMessage().engine().options(y).name().c_str(),
 
333
               createTableMessage().engine().options(y).state().c_str());
276
334
 
277
335
      rc= false;
278
336
    }