~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/statement/create_table.cc

  • Committer: Brian Aker
  • Date: 2011-02-12 08:10:17 UTC
  • mto: This revision was merged to the branch mainline in revision 2161.
  • Revision ID: brian@tangent.org-20110212081017-7793i41ybt7gp5ty
More removal of session from includes.

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>
22
 
 
 
21
#include "config.h"
23
22
#include <drizzled/show.h>
24
23
#include <drizzled/lock.h>
25
24
#include <drizzled/session.h>
27
26
#include <drizzled/message.h>
28
27
#include <drizzled/identifier.h>
29
28
#include <drizzled/plugin/storage_engine.h>
30
 
#include <drizzled/select_create.h>
31
 
#include <drizzled/table_ident.h>
32
29
 
33
30
#include <iostream>
34
31
 
48
45
  link_to_local(false),
49
46
  create_table_list(NULL)
50
47
{
51
 
  set_command(SQLCOM_CREATE_TABLE);
 
48
  getSession()->getLex()->sql_command= SQLCOM_CREATE_TABLE;
52
49
  createTableMessage().set_name(ident->table.str, ident->table.length);
53
50
#if 0
54
51
  createTableMessage().set_schema(ident->db.str, ident->db.length);
75
72
  link_to_local(false),
76
73
  create_table_list(NULL)
77
74
{
78
 
  set_command(SQLCOM_CREATE_TABLE);
 
75
  getSession()->getLex()->sql_command= SQLCOM_CREATE_TABLE;
79
76
}
80
77
 
81
78
} // namespace statement
82
79
 
83
80
bool statement::CreateTable::execute()
84
81
{
85
 
  TableList *first_table= (TableList *) lex().select_lex.table_list.first;
86
 
  TableList *all_tables= lex().query_tables;
 
82
  TableList *first_table= (TableList *) getSession()->lex->select_lex.table_list.first;
 
83
  TableList *all_tables= getSession()->lex->query_tables;
87
84
  assert(first_table == all_tables && first_table != 0);
88
85
  bool need_start_waiting= false;
89
86
  lex_identified_temp_table= createTableMessage().type() == message::Table::TEMPORARY;
93
90
  if (is_engine_set)
94
91
  {
95
92
    create_info().db_type= 
96
 
      plugin::StorageEngine::findByName(session(), createTableMessage().engine().name());
 
93
      plugin::StorageEngine::findByName(*getSession(), createTableMessage().engine().name());
97
94
 
98
95
    if (create_info().db_type == NULL)
99
96
    {
105
102
  }
106
103
  else /* We now get the default, place it in create_info, and put the engine name in table proto */
107
104
  {
108
 
    create_info().db_type= session().getDefaultStorageEngine();
 
105
    create_info().db_type= getSession()->getDefaultStorageEngine();
109
106
  }
110
107
 
111
108
  if (not validateCreateTableOption())
115
112
 
116
113
  if (not lex_identified_temp_table)
117
114
  {
118
 
    if (session().inTransaction())
 
115
    if (getSession()->inTransaction())
119
116
    {
120
117
      my_error(ER_TRANSACTIONAL_DDL_NOT_SUPPORTED, MYF(0));
121
118
      return true;
122
119
    }
123
120
  }
124
121
  /* Skip first table, which is the table we are creating */
125
 
  create_table_list= lex().unlink_first_table(&link_to_local);
 
122
  create_table_list= getSession()->lex->unlink_first_table(&link_to_local);
126
123
 
127
124
  drizzled::message::table::init(createTableMessage(), createTableMessage().name(), create_table_list->getSchemaName(), create_info().db_type->getName());
128
125
 
133
130
  if (not check(new_table_identifier))
134
131
  {
135
132
    /* put tables back for PS rexecuting */
136
 
    lex().link_first_table_back(create_table_list, link_to_local);
 
133
    getSession()->lex->link_first_table_back(create_table_list, link_to_local);
137
134
    return true;
138
135
  }
139
136
 
153
150
     TABLE in the same way. That way we avoid that a new table is
154
151
     created during a gobal read lock.
155
152
   */
156
 
  if (! (need_start_waiting= not session().wait_if_global_read_lock(0, 1)))
 
153
  if (! (need_start_waiting= not getSession()->wait_if_global_read_lock(0, 1)))
157
154
  {
158
155
    /* put tables back for PS rexecuting */
159
 
    lex().link_first_table_back(create_table_list, link_to_local);
 
156
    getSession()->lex->link_first_table_back(create_table_list, link_to_local);
160
157
    return true;
161
158
  }
162
159
 
166
163
    Release the protection against the global read lock and wake
167
164
    everyone, who might want to set a global read lock.
168
165
  */
169
 
  session().startWaitingGlobalReadLock();
 
166
  getSession()->startWaitingGlobalReadLock();
170
167
 
171
168
  return res;
172
169
}
173
170
 
174
 
bool statement::CreateTable::executeInner(const identifier::Table& new_table_identifier)
 
171
bool statement::CreateTable::executeInner(identifier::Table::const_reference new_table_identifier)
175
172
{
176
173
  bool res= false;
177
 
  Select_Lex *select_lex= &lex().select_lex;
178
 
  TableList *select_tables= lex().query_tables;
 
174
  Select_Lex *select_lex= &getSession()->lex->select_lex;
 
175
  TableList *select_tables= getSession()->lex->query_tables;
179
176
 
180
177
  do 
181
178
  {
182
 
    if (select_lex->item_list.size())           // With select
 
179
    if (select_lex->item_list.elements)         // With select
183
180
    {
184
 
      Select_Lex_Unit *unit= &lex().unit;
 
181
      Select_Lex_Unit *unit= &getSession()->lex->unit;
185
182
      select_result *result;
186
183
 
187
184
      select_lex->options|= SELECT_NO_UNLOCK;
189
186
 
190
187
      if (not lex_identified_temp_table)
191
188
      {
192
 
        lex().link_first_table_back(create_table_list, link_to_local);
 
189
        getSession()->lex->link_first_table_back(create_table_list, link_to_local);
193
190
        create_table_list->setCreate(true);
194
191
      }
195
192
 
196
 
      if (not (res= session().openTablesLock(lex().query_tables)))
 
193
      if (not (res= getSession()->openTablesLock(getSession()->lex->query_tables)))
197
194
      {
198
195
        /*
199
196
          Is table which we are changing used somewhere in other parts
202
199
        if (not lex_identified_temp_table)
203
200
        {
204
201
          TableList *duplicate= NULL;
205
 
          create_table_list= lex().unlink_first_table(&link_to_local);
 
202
          create_table_list= getSession()->lex->unlink_first_table(&link_to_local);
206
203
 
207
204
          if ((duplicate= unique_table(create_table_list, select_tables)))
208
205
          {
209
206
            my_error(ER_UPDATE_TABLE_USED, MYF(0), create_table_list->alias);
210
207
            /* put tables back for PS rexecuting */
211
 
            lex().link_first_table_back(create_table_list, link_to_local);
 
208
            getSession()->lex->link_first_table_back(create_table_list, link_to_local);
212
209
 
213
210
            res= true;
214
211
            break;
220
217
          needs to be created for every execution of a PS/SP.
221
218
        */
222
219
        if ((result= new select_create(create_table_list,
223
 
                                       lex().exists(),
 
220
                                       getSession()->getLex()->exists(),
224
221
                                       &create_info(),
225
222
                                       createTableMessage(),
226
223
                                       &alter_info,
227
224
                                       select_lex->item_list,
228
 
                                       lex().duplicates,
229
 
                                       lex().ignore,
 
225
                                       getSession()->lex->duplicates,
 
226
                                       getSession()->lex->ignore,
230
227
                                       select_tables,
231
228
                                       new_table_identifier)))
232
229
        {
234
231
            CREATE from SELECT give its Select_Lex for SELECT,
235
232
            and item_list belong to SELECT
236
233
          */
237
 
          res= handle_select(&session(), &lex(), result, 0);
 
234
          res= handle_select(getSession(), getSession()->lex, result, 0);
238
235
          delete result;
239
236
        }
240
237
      }
241
238
      else if (not lex_identified_temp_table)
242
239
      {
243
 
        create_table_list= lex().unlink_first_table(&link_to_local);
 
240
        create_table_list= getSession()->lex->unlink_first_table(&link_to_local);
244
241
      }
245
242
    }
246
243
    else
248
245
      /* regular create */
249
246
      if (is_create_table_like)
250
247
      {
251
 
        res= create_like_table(&session(), 
 
248
        res= create_like_table(getSession(), 
252
249
                               new_table_identifier,
253
250
                               identifier::Table(select_tables->getSchemaName(),
254
251
                                                 select_tables->getTableName()),
255
252
                               createTableMessage(),
256
 
                               lex().exists(),
 
253
                               getSession()->getLex()->exists(),
257
254
                               is_engine_set);
258
255
      }
259
256
      else
260
257
      {
261
258
 
262
 
        for (int32_t x= 0; x < alter_info.added_fields_proto.added_field_size(); x++)
 
259
        for (int32_t x= 0; x < alter_info.alter_proto.added_field_size(); x++)
263
260
        {
264
261
          message::Table::Field *field= createTableMessage().add_field();
265
262
 
266
 
          *field= alter_info.added_fields_proto.added_field(x);
 
263
          *field= alter_info.alter_proto.added_field(x);
267
264
        }
268
265
 
269
 
        res= create_table(&session(), 
 
266
        res= create_table(getSession(), 
270
267
                          new_table_identifier,
271
268
                          &create_info(),
272
269
                          createTableMessage(),
273
270
                          &alter_info, 
274
271
                          false, 
275
272
                          0,
276
 
                          lex().exists());
 
273
                          getSession()->getLex()->exists());
277
274
      }
278
275
 
279
276
      if (not res)
280
277
      {
281
 
        session().my_ok();
 
278
        getSession()->my_ok();
282
279
      }
283
280
    }
284
281
  } while (0);
295
292
  // See if any storage engine objects to the name of the file
296
293
  if (not plugin::StorageEngine::canCreateTable(identifier))
297
294
  {
298
 
    identifier::Schema schema_identifier= identifier;
299
 
    error::access(*session().user(), schema_identifier);
 
295
    my_error(ER_DBACCESS_DENIED_ERROR, MYF(0), "", "", identifier.getSchemaName().c_str());
300
296
 
301
297
    return false;
302
298
  }
305
301
  // create for the table.
306
302
  if (not plugin::StorageEngine::doesSchemaExist(identifier))
307
303
  {
308
 
    identifier::Schema schema_identifier= identifier;
309
 
    my_error(ER_BAD_DB_ERROR, schema_identifier);
 
304
    my_error(ER_BAD_DB_ERROR, MYF(0), identifier.getSchemaName().c_str());
310
305
 
311
306
    return false;
312
307
  }