~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/statement/create_table.cc

  • Committer: Brian Aker
  • Date: 2010-12-26 00:07:57 UTC
  • Revision ID: brian@tangent.org-20101226000757-ysntnltqywxoe1em
MErge in the include changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
  TableList *first_table= (TableList *) session->lex->select_lex.table_list.first;
37
37
  TableList *all_tables= session->lex->query_tables;
38
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;
39
41
  bool need_start_waiting= false;
40
 
  lex_identified_temp_table= createTableMessage().type() == message::Table::TEMPORARY;
41
 
 
42
 
  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;
43
46
 
44
47
  if (is_engine_set)
45
48
  {
46
 
    create_info().db_type= 
47
 
      plugin::StorageEngine::findByName(*session, createTableMessage().engine().name());
 
49
    create_info.db_type= 
 
50
      plugin::StorageEngine::findByName(*session, create_table_message.engine().name());
48
51
 
49
 
    if (create_info().db_type == NULL)
 
52
    if (create_info.db_type == NULL)
50
53
    {
51
54
      my_error(ER_UNKNOWN_STORAGE_ENGINE, MYF(0), 
52
 
               createTableMessage().engine().name().c_str());
 
55
               create_table_message.engine().name().c_str());
53
56
 
54
57
      return true;
55
58
    }
56
59
  }
57
60
  else /* We now get the default, place it in create_info, and put the engine name in table proto */
58
61
  {
59
 
    create_info().db_type= session->getDefaultStorageEngine();
 
62
    create_info.db_type= session->getDefaultStorageEngine();
60
63
  }
61
64
 
62
65
  if (not validateCreateTableOption())
74
77
    }
75
78
  }
76
79
  /* Skip first table, which is the table we are creating */
77
 
  create_table_list= session->lex->unlink_first_table(&link_to_local);
78
 
 
79
 
  drizzled::message::table::init(createTableMessage(), createTableMessage().name(), create_table_list->getSchemaName(), create_info().db_type->getName());
80
 
 
81
 
  identifier::Table new_table_identifier(create_table_list->getSchemaName(),
 
80
  TableList *create_table_list= 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_list->getSchemaName(), create_info.db_type->getName());
 
84
 
 
85
  TableIdentifier new_table_identifier(create_table_list->getSchemaName(),
82
86
                                       create_table_list->getTableName(),
83
 
                                       createTableMessage().type());
 
87
                                       create_table_message.type());
84
88
 
85
89
  if (not check(new_table_identifier))
86
90
  {
90
94
  }
91
95
 
92
96
  /* Might have been updated in create_table_precheck */
93
 
  create_info().alias= create_table_list->alias;
 
97
  create_info.alias= create_table_list->alias;
94
98
 
95
99
  /*
96
100
     The create-select command will open and read-lock the select table
112
116
    return true;
113
117
  }
114
118
 
115
 
  bool res= executeInner(new_table_identifier);
116
 
 
117
 
  /*
118
 
    Release the protection against the global read lock and wake
119
 
    everyone, who might want to set a global read lock.
120
 
  */
121
 
  session->startWaitingGlobalReadLock();
122
 
 
123
 
  return res;
124
 
}
125
 
 
126
 
bool statement::CreateTable::executeInner(identifier::Table::const_reference new_table_identifier)
127
 
{
128
 
  bool res= false;
129
 
  Select_Lex *select_lex= &session->lex->select_lex;
130
 
  TableList *select_tables= session->lex->query_tables;
131
 
 
132
 
  do 
 
119
  if (select_lex->item_list.elements)           // With select
133
120
  {
134
 
    if (select_lex->item_list.elements)         // With select
135
 
    {
136
 
      Select_Lex_Unit *unit= &session->lex->unit;
137
 
      select_result *result;
138
 
 
139
 
      select_lex->options|= SELECT_NO_UNLOCK;
140
 
      unit->set_limit(select_lex);
141
 
 
 
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_list, link_to_local);
 
129
      create_table_list->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
       */
142
138
      if (not lex_identified_temp_table)
143
139
      {
144
 
        session->lex->link_first_table_back(create_table_list, link_to_local);
145
 
        create_table_list->setCreate(true);
146
 
      }
147
 
 
148
 
      if (not (res= session->openTablesLock(session->lex->query_tables)))
149
 
      {
150
 
        /*
151
 
          Is table which we are changing used somewhere in other parts
152
 
          of query
153
 
        */
154
 
        if (not lex_identified_temp_table)
155
 
        {
156
 
          TableList *duplicate= NULL;
157
 
          create_table_list= session->lex->unlink_first_table(&link_to_local);
158
 
 
159
 
          if ((duplicate= unique_table(create_table_list, select_tables)))
160
 
          {
161
 
            my_error(ER_UPDATE_TABLE_USED, MYF(0), create_table_list->alias);
162
 
            /* put tables back for PS rexecuting */
163
 
            session->lex->link_first_table_back(create_table_list, link_to_local);
164
 
 
165
 
            res= true;
166
 
            break;
167
 
          }
168
 
        }
169
 
 
170
 
        /*
171
 
          select_create is currently not re-execution friendly and
172
 
          needs to be created for every execution of a PS/SP.
173
 
        */
174
 
        if ((result= new select_create(create_table_list,
175
 
                                       session->getLex()->exists(),
176
 
                                       &create_info(),
177
 
                                       createTableMessage(),
178
 
                                       &alter_info,
179
 
                                       select_lex->item_list,
180
 
                                       session->lex->duplicates,
181
 
                                       session->lex->ignore,
182
 
                                       select_tables,
183
 
                                       new_table_identifier)))
184
 
        {
 
140
        TableList *duplicate= NULL;
 
141
        create_table_list= session->lex->unlink_first_table(&link_to_local);
 
142
        if ((duplicate= unique_table(create_table_list, select_tables)))
 
143
        {
 
144
          my_error(ER_UPDATE_TABLE_USED, MYF(0), create_table_list->alias);
185
145
          /*
186
 
            CREATE from SELECT give its Select_Lex for SELECT,
187
 
            and item_list belong to SELECT
188
 
          */
189
 
          res= handle_select(session, session->lex, result, 0);
190
 
          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_list, link_to_local);
 
152
 
 
153
          return true;
191
154
        }
192
155
      }
193
 
      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_list,
 
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)))
194
171
      {
195
 
        create_table_list= session->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;
196
178
      }
197
179
    }
 
180
    else if (not lex_identified_temp_table)
 
181
    {
 
182
      create_table_list= 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= create_like_table(session, 
 
191
                                   new_table_identifier,
 
192
                                   create_table_list, 
 
193
                                   select_tables,
 
194
                                   create_table_message,
 
195
                                   is_if_not_exists,
 
196
                                   is_engine_set);
 
197
    }
198
198
    else
199
199
    {
200
 
      /* regular create */
201
 
      if (is_create_table_like)
202
 
      {
203
 
        res= create_like_table(session, 
204
 
                               new_table_identifier,
205
 
                               identifier::Table(select_tables->getSchemaName(),
206
 
                                                 select_tables->getTableName()),
207
 
                               createTableMessage(),
208
 
                               session->getLex()->exists(),
209
 
                               is_engine_set);
210
 
      }
211
 
      else
212
 
      {
213
 
 
214
 
        for (int32_t x= 0; x < alter_info.alter_proto.added_field_size(); x++)
215
 
        {
216
 
          message::Table::Field *field= createTableMessage().add_field();
217
 
 
218
 
          *field= alter_info.alter_proto.added_field(x);
219
 
        }
220
 
 
221
 
        res= create_table(session, 
222
 
                          new_table_identifier,
223
 
                          &create_info(),
224
 
                          createTableMessage(),
225
 
                          &alter_info, 
226
 
                          false, 
227
 
                          0,
228
 
                          session->getLex()->exists());
229
 
      }
230
 
 
231
 
      if (not res)
232
 
      {
233
 
        session->my_ok();
234
 
      }
235
 
    }
236
 
  } 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= 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();
237
229
 
238
230
  return res;
239
231
}
240
232
 
241
 
bool statement::CreateTable::check(const identifier::Table &identifier)
 
233
bool statement::CreateTable::check(const TableIdentifier &identifier)
242
234
{
243
235
  // Check table name for validity
244
236
  if (not identifier.isValid())
267
259
bool statement::CreateTable::validateCreateTableOption()
268
260
{
269
261
  bool rc= true;
270
 
  size_t num_engine_options= createTableMessage().engine().options_size();
 
262
  size_t num_engine_options= create_table_message.engine().options_size();
271
263
 
272
 
  assert(create_info().db_type);
 
264
  assert(create_info.db_type);
273
265
 
274
266
  for (size_t y= 0; y < num_engine_options; ++y)
275
267
  {
276
 
    bool valid= create_info().db_type->validateCreateTableOption(createTableMessage().engine().options(y).name(),
277
 
                                                                 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());
278
270
 
279
271
    if (not valid)
280
272
    {
281
273
      my_error(ER_UNKNOWN_ENGINE_OPTION, MYF(0),
282
 
               createTableMessage().engine().options(y).name().c_str(),
283
 
               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());
284
276
 
285
277
      rc= false;
286
278
    }