~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/statement/create_table.cc

  • Committer: Lee Bieber
  • Date: 2010-12-23 23:11:00 UTC
  • mfrom: (2024.1.1 clean)
  • Revision ID: kalebral@gmail.com-20101223231100-0rqirgz7ugkl10yp
Merge Brian - session list cleanup

Show diffs side-by-side

added added

removed removed

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