~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/statement/create_table.cc

Merge trunk and resolve all conflicts.

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 <drizzled/server_includes.h>
22
22
#include <drizzled/show.h>
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>
30
26
 
31
27
namespace drizzled
32
28
{
41
37
  bool need_start_waiting= false;
42
38
  bool res= false;
43
39
  bool link_to_local= false;
44
 
  bool lex_identified_temp_table= 
45
 
    create_table_message.type() == message::Table::TEMPORARY;
46
40
 
47
 
  if (is_engine_set)
 
41
  if (create_info.used_fields & HA_CREATE_USED_ENGINE)
48
42
  {
 
43
 
49
44
    create_info.db_type= 
50
 
      plugin::StorageEngine::findByName(*session, create_table_message.engine().name());
 
45
      plugin::StorageEngine::findByName(session, create_table_proto.engine().name());
51
46
 
52
47
    if (create_info.db_type == NULL)
53
48
    {
54
49
      my_error(ER_UNKNOWN_STORAGE_ENGINE, MYF(0), 
55
 
               create_table_message.engine().name().c_str());
 
50
               create_table_proto.name().c_str());
56
51
 
57
52
      return true;
58
53
    }
62
57
    create_info.db_type= session->getDefaultStorageEngine();
63
58
  }
64
59
 
65
 
  if (not validateCreateTableOption())
 
60
 
 
61
 
 
62
  /* 
 
63
    Now we set the name in our Table proto so that it will match 
 
64
    create_info.db_type.
 
65
  */
66
66
  {
67
 
    return true;
 
67
    message::Table::StorageEngine *protoengine;
 
68
 
 
69
    protoengine= create_table_proto.mutable_engine();
 
70
    protoengine->set_name(create_info.db_type->getName());
68
71
  }
69
72
 
70
73
 
71
74
  /* If CREATE TABLE of non-temporary table, do implicit commit */
72
 
  if (not lex_identified_temp_table)
 
75
  if (! (create_info.options & HA_LEX_CREATE_TMP_TABLE))
73
76
  {
74
 
    if (not session->endActiveTransaction())
 
77
    if (! session->endActiveTransaction())
75
78
    {
76
79
      return true;
77
80
    }
80
83
  TableList *create_table= session->lex->unlink_first_table(&link_to_local);
81
84
  TableList *select_tables= session->lex->query_tables;
82
85
 
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))
 
86
  if (create_table_precheck(session, select_tables, create_table))
90
87
  {
91
88
    /* put tables back for PS rexecuting */
92
89
    session->lex->link_first_table_back(create_table, link_to_local);
109
106
     TABLE in the same way. That way we avoid that a new table is
110
107
     created during a gobal read lock.
111
108
   */
112
 
  if (! (need_start_waiting= not session->wait_if_global_read_lock(0, 1)))
 
109
  if (! (need_start_waiting= ! wait_if_global_read_lock(session, 0, 1)))
113
110
  {
114
111
    /* put tables back for PS rexecuting */
115
112
    session->lex->link_first_table_back(create_table, link_to_local);
123
120
    select_lex->options|= SELECT_NO_UNLOCK;
124
121
    unit->set_limit(select_lex);
125
122
 
126
 
    if (not lex_identified_temp_table)
 
123
    if (! (create_info.options & HA_LEX_CREATE_TMP_TABLE))
127
124
    {
128
125
      session->lex->link_first_table_back(create_table, link_to_local);
129
 
      create_table->setCreate(true);
 
126
      create_table->create= true;
130
127
    }
131
128
 
132
 
    if (not (res= session->openTablesLock(session->lex->query_tables)))
 
129
    if (! (res= session->openTablesLock(session->lex->query_tables)))
133
130
    {
134
131
      /*
135
132
         Is table which we are changing used somewhere in other parts
136
133
         of query
137
134
       */
138
 
      if (not lex_identified_temp_table)
 
135
      if (! (create_info.options & HA_LEX_CREATE_TMP_TABLE))
139
136
      {
140
137
        TableList *duplicate= NULL;
141
138
        create_table= session->lex->unlink_first_table(&link_to_local);
142
 
        if ((duplicate= unique_table(create_table, select_tables)))
 
139
        if ((duplicate= unique_table(session, create_table, select_tables, 0)))
143
140
        {
144
141
          my_error(ER_UPDATE_TABLE_USED, MYF(0), create_table->alias);
145
142
          /*
146
143
             Release the protection against the global read lock and wake
147
144
             everyone, who might want to set a global read lock.
148
145
           */
149
 
          session->startWaitingGlobalReadLock();
 
146
          start_waiting_global_read_lock(session);
150
147
          /* put tables back for PS rexecuting */
151
148
          session->lex->link_first_table_back(create_table, link_to_local);
152
 
 
153
149
          return true;
154
150
        }
155
151
      }
159
155
         needs to be created for every execution of a PS/SP.
160
156
       */
161
157
      if ((result= new select_create(create_table,
162
 
                                     is_if_not_exists,
163
158
                                     &create_info,
164
 
                                     create_table_message,
 
159
                                     &create_table_proto,
165
160
                                     &alter_info,
166
161
                                     select_lex->item_list,
167
162
                                     session->lex->duplicates,
168
163
                                     session->lex->ignore,
169
 
                                     select_tables,
170
 
                                     new_table_identifier)))
 
164
                                     select_tables)))
171
165
      {
172
166
        /*
173
167
           CREATE from SELECT give its Select_Lex for SELECT,
177
171
        delete result;
178
172
      }
179
173
    }
180
 
    else if (not lex_identified_temp_table)
 
174
    else if (! (create_info.options & HA_LEX_CREATE_TMP_TABLE))
181
175
    {
182
176
      create_table= session->lex->unlink_first_table(&link_to_local);
183
177
    }
185
179
  else
186
180
  {
187
181
    /* regular create */
188
 
    if (is_create_table_like)
 
182
    if (create_info.options & HA_LEX_CREATE_TABLE_LIKE)
189
183
    {
190
184
      res= mysql_create_like_table(session, 
191
 
                                   new_table_identifier,
192
185
                                   create_table, 
193
186
                                   select_tables,
194
 
                                   create_table_message,
195
 
                                   is_if_not_exists,
196
 
                                   is_engine_set);
 
187
                                   create_table_proto,
 
188
                                   &create_info);
197
189
    }
198
190
    else
199
191
    {
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
192
      res= mysql_create_table(session, 
209
 
                              new_table_identifier,
 
193
                              create_table->db,
 
194
                              create_table->table_name, 
210
195
                              &create_info,
211
 
                              create_table_message,
 
196
                              &create_table_proto,
212
197
                              &alter_info, 
213
 
                              false, 
214
 
                              0,
215
 
                              is_if_not_exists);
 
198
                              0, 
 
199
                              0);
216
200
    }
217
 
 
218
 
    if (not res)
 
201
    if (! res)
219
202
    {
220
203
      session->my_ok();
221
204
    }
225
208
     Release the protection against the global read lock and wake
226
209
     everyone, who might want to set a global read lock.
227
210
   */
228
 
  session->startWaitingGlobalReadLock();
 
211
  start_waiting_global_read_lock(session);
229
212
 
230
213
  return res;
231
214
}
232
215
 
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
216
} /* namespace drizzled */
285
217