~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/statement/create_table.cc

Fixed a style variance.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
#include <drizzled/lock.h>
24
24
#include <drizzled/session.h>
25
25
#include <drizzled/statement/create_table.h>
26
 
#include <drizzled/message.h>
27
 
#include <drizzled/identifier.h>
28
 
 
29
 
#include <iostream>
 
26
#include <drizzled/table_identifier.h>
30
27
 
31
28
namespace drizzled
32
29
{
42
39
  bool res= false;
43
40
  bool link_to_local= false;
44
41
  bool lex_identified_temp_table= 
45
 
    create_table_message.type() == message::Table::TEMPORARY;
 
42
    create_table_proto.type() == message::Table::TEMPORARY;
46
43
 
47
44
  if (is_engine_set)
48
45
  {
49
46
    create_info.db_type= 
50
 
      plugin::StorageEngine::findByName(*session, create_table_message.engine().name());
 
47
      plugin::StorageEngine::findByName(*session, create_table_proto.engine().name());
51
48
 
52
49
    if (create_info.db_type == NULL)
53
50
    {
54
51
      my_error(ER_UNKNOWN_STORAGE_ENGINE, MYF(0), 
55
 
               create_table_message.engine().name().c_str());
 
52
               create_table_proto.name().c_str());
56
53
 
57
54
      return true;
58
55
    }
62
59
    create_info.db_type= session->getDefaultStorageEngine();
63
60
  }
64
61
 
65
 
  if (not validateCreateTableOption())
 
62
 
 
63
  /* 
 
64
    Now we set the name in our Table proto so that it will match 
 
65
    create_info.db_type.
 
66
  */
66
67
  {
67
 
    return true;
 
68
    message::Table::StorageEngine *protoengine;
 
69
 
 
70
    protoengine= create_table_proto.mutable_engine();
 
71
    protoengine->set_name(create_info.db_type->getName());
68
72
  }
69
73
 
70
74
 
71
75
  /* If CREATE TABLE of non-temporary table, do implicit commit */
72
 
  if (not lex_identified_temp_table)
 
76
  if (! lex_identified_temp_table)
73
77
  {
74
 
    if (not session->endActiveTransaction())
 
78
    if (! session->endActiveTransaction())
75
79
    {
76
80
      return true;
77
81
    }
80
84
  TableList *create_table= session->lex->unlink_first_table(&link_to_local);
81
85
  TableList *select_tables= session->lex->query_tables;
82
86
 
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());
88
 
 
89
 
  if (not check(new_table_identifier))
 
87
 
 
88
  /*
 
89
    Now that we have the engine, we can figure out the table identifier. We need the engine in order
 
90
    to determine if the table is transactional or not if it is temp.
 
91
  */
 
92
  TableIdentifier new_table_identifier(create_table->db,
 
93
                                       create_table->table_name,
 
94
                                       create_table_proto.type() != message::Table::TEMPORARY ? NO_TMP_TABLE : TEMP_TABLE);
 
95
 
 
96
  if (create_table_precheck(new_table_identifier))
90
97
  {
91
98
    /* put tables back for PS rexecuting */
92
99
    session->lex->link_first_table_back(create_table, link_to_local);
109
116
     TABLE in the same way. That way we avoid that a new table is
110
117
     created during a gobal read lock.
111
118
   */
112
 
  if (! (need_start_waiting= not session->wait_if_global_read_lock(0, 1)))
 
119
  if (! (need_start_waiting= ! wait_if_global_read_lock(session, 0, 1)))
113
120
  {
114
121
    /* put tables back for PS rexecuting */
115
122
    session->lex->link_first_table_back(create_table, link_to_local);
123
130
    select_lex->options|= SELECT_NO_UNLOCK;
124
131
    unit->set_limit(select_lex);
125
132
 
126
 
    if (not lex_identified_temp_table)
 
133
    if (! lex_identified_temp_table)
127
134
    {
128
135
      session->lex->link_first_table_back(create_table, link_to_local);
129
 
      create_table->setCreate(true);
 
136
      create_table->create= true;
130
137
    }
131
138
 
132
 
    if (not (res= session->openTablesLock(session->lex->query_tables)))
 
139
    if (! (res= session->openTablesLock(session->lex->query_tables)))
133
140
    {
134
141
      /*
135
142
         Is table which we are changing used somewhere in other parts
136
143
         of query
137
144
       */
138
 
      if (not lex_identified_temp_table)
 
145
      if (! lex_identified_temp_table)
139
146
      {
140
147
        TableList *duplicate= NULL;
141
148
        create_table= session->lex->unlink_first_table(&link_to_local);
146
153
             Release the protection against the global read lock and wake
147
154
             everyone, who might want to set a global read lock.
148
155
           */
149
 
          session->startWaitingGlobalReadLock();
 
156
          start_waiting_global_read_lock(session);
150
157
          /* put tables back for PS rexecuting */
151
158
          session->lex->link_first_table_back(create_table, link_to_local);
152
 
 
153
159
          return true;
154
160
        }
155
161
      }
161
167
      if ((result= new select_create(create_table,
162
168
                                     is_if_not_exists,
163
169
                                     &create_info,
164
 
                                     create_table_message,
 
170
                                     &create_table_proto,
165
171
                                     &alter_info,
166
172
                                     select_lex->item_list,
167
173
                                     session->lex->duplicates,
168
174
                                     session->lex->ignore,
169
 
                                     select_tables,
170
 
                                     new_table_identifier)))
 
175
                                     select_tables)))
171
176
      {
172
177
        /*
173
178
           CREATE from SELECT give its Select_Lex for SELECT,
177
182
        delete result;
178
183
      }
179
184
    }
180
 
    else if (not lex_identified_temp_table)
 
185
    else if (! lex_identified_temp_table)
181
186
    {
182
187
      create_table= session->lex->unlink_first_table(&link_to_local);
183
188
    }
188
193
    if (is_create_table_like)
189
194
    {
190
195
      res= mysql_create_like_table(session, 
191
 
                                   new_table_identifier,
192
196
                                   create_table, 
193
197
                                   select_tables,
194
 
                                   create_table_message,
 
198
                                   create_table_proto,
 
199
                                   create_info.db_type, 
195
200
                                   is_if_not_exists,
196
201
                                   is_engine_set);
197
202
    }
200
205
 
201
206
      for (int32_t x= 0; x < alter_info.alter_proto.added_field_size(); x++)
202
207
      {
203
 
        message::Table::Field *field= create_table_message.add_field();
 
208
        message::Table::Field *field= create_table_proto.add_field();
204
209
 
205
210
        *field= alter_info.alter_proto.added_field(x);
206
211
      }
208
213
      res= mysql_create_table(session, 
209
214
                              new_table_identifier,
210
215
                              &create_info,
211
 
                              create_table_message,
 
216
                              &create_table_proto,
212
217
                              &alter_info, 
213
218
                              false, 
214
219
                              0,
215
220
                              is_if_not_exists);
216
221
    }
217
 
 
218
 
    if (not res)
 
222
    if (! res)
219
223
    {
220
224
      session->my_ok();
221
225
    }
225
229
     Release the protection against the global read lock and wake
226
230
     everyone, who might want to set a global read lock.
227
231
   */
228
 
  session->startWaitingGlobalReadLock();
 
232
  start_waiting_global_read_lock(session);
229
233
 
230
234
  return res;
231
235
}
232
236
 
233
 
bool statement::CreateTable::check(const TableIdentifier &identifier)
234
 
{
235
 
  // Check table name for validity
236
 
  if (not identifier.isValid())
237
 
    return false;
238
 
 
239
 
  // See if any storage engine objects to the name of the file
240
 
  if (not plugin::StorageEngine::canCreateTable(identifier))
241
 
  {
242
 
    my_error(ER_DBACCESS_DENIED_ERROR, MYF(0), "", "", identifier.getSchemaName().c_str());
243
 
 
244
 
    return false;
245
 
  }
246
 
 
247
 
  // Make sure the schema exists, we will do this again during the actual
248
 
  // create for the table.
249
 
  if (not plugin::StorageEngine::doesSchemaExist(identifier))
250
 
  {
251
 
    my_error(ER_BAD_DB_ERROR, MYF(0), identifier.getSchemaName().c_str());
252
 
 
253
 
    return false;
254
 
  }
255
 
 
256
 
  return true;
257
 
}
258
 
 
259
 
bool statement::CreateTable::validateCreateTableOption()
260
 
{
261
 
  bool rc= true;
262
 
  size_t num_engine_options= create_table_message.engine().options_size();
263
 
 
264
 
  assert(create_info.db_type);
265
 
 
266
 
  for (size_t y= 0; y < num_engine_options; ++y)
267
 
  {
268
 
    bool valid= create_info.db_type->validateCreateTableOption(create_table_message.engine().options(y).name(),
269
 
                                                               create_table_message.engine().options(y).state());
270
 
 
271
 
    if (not valid)
272
 
    {
273
 
      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());
276
 
 
277
 
      rc= false;
278
 
    }
279
 
  }
280
 
 
281
 
  return rc;
282
 
}
283
 
 
284
237
} /* namespace drizzled */
285
238