~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/db.cc

  • Committer: Brian Aker
  • Date: 2010-05-18 15:49:50 UTC
  • Revision ID: brian@gaz-20100518154950-qhctxu6lvvnyxy9i
Simplify out Open_tables_state() backup/restore (no longer needed).

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
13
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
16
 
17
17
/* create and drop of databases */
44
44
#include "drizzled/pthread_globals.h"
45
45
#include "drizzled/charset.h"
46
46
 
47
 
#include <boost/thread/mutex.hpp>
48
 
 
49
 
boost::mutex LOCK_create_db;
50
 
 
51
47
#include "drizzled/internal/my_sys.h"
52
48
 
53
49
#define MAX_DROP_TABLE_Q_LEN      1024
57
53
namespace drizzled
58
54
{
59
55
 
60
 
static long drop_tables_via_filenames(Session *session,
 
56
static long mysql_rm_known_files(Session *session,
61
57
                                 SchemaIdentifier &schema_identifier,
62
 
                                 TableIdentifier::vector &dropped_tables);
 
58
                                 plugin::TableNameList &dropped_tables);
63
59
static void mysql_change_db_impl(Session *session);
64
60
static void mysql_change_db_impl(Session *session, SchemaIdentifier &schema_identifier);
65
61
 
101
97
    has the global read lock and refuses the operation with
102
98
    ER_CANT_UPDATE_WITH_READLOCK if applicable.
103
99
  */
104
 
  if (session->wait_if_global_read_lock(false, true))
 
100
  if (wait_if_global_read_lock(session, 0, 1))
105
101
  {
106
102
    return false;
107
103
  }
110
106
  assert(schema_message.has_collation());
111
107
 
112
108
  // @todo push this lock down into the engine
 
109
  pthread_mutex_lock(&LOCK_create_db);
 
110
 
 
111
  // Check to see if it exists already.  
 
112
  SchemaIdentifier schema_identifier(schema_message.name());
 
113
  if (plugin::StorageEngine::doesSchemaExist(schema_identifier))
113
114
  {
114
 
    boost::mutex::scoped_lock scopedLock(LOCK_create_db);
115
 
 
116
 
    // Check to see if it exists already.  
117
 
    SchemaIdentifier schema_identifier(schema_message.name());
118
 
    if (plugin::StorageEngine::doesSchemaExist(schema_identifier))
119
 
    {
120
 
      if (not is_if_not_exists)
121
 
      {
122
 
        my_error(ER_DB_CREATE_EXISTS, MYF(0), schema_message.name().c_str());
123
 
        error= true;
124
 
      }
125
 
      else
126
 
      {
127
 
        push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
128
 
                            ER_DB_CREATE_EXISTS, ER(ER_DB_CREATE_EXISTS),
129
 
                            schema_message.name().c_str());
130
 
        session->my_ok();
131
 
      }
132
 
    }
133
 
    else if (not plugin::StorageEngine::createSchema(schema_message)) // Try to create it 
134
 
    {
135
 
      my_error(ER_CANT_CREATE_DB, MYF(0), schema_message.name().c_str(), errno);
 
115
    if (not is_if_not_exists)
 
116
    {
 
117
      my_error(ER_DB_CREATE_EXISTS, MYF(0), schema_message.name().c_str());
136
118
      error= true;
137
119
    }
138
 
    else // Created !
 
120
    else
139
121
    {
140
 
      transaction_services.createSchema(session, schema_message);
141
 
      session->my_ok(1);
 
122
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
 
123
                          ER_DB_CREATE_EXISTS, ER(ER_DB_CREATE_EXISTS),
 
124
                          schema_message.name().c_str());
 
125
      session->my_ok();
142
126
    }
143
127
  }
144
 
  session->startWaitingGlobalReadLock();
 
128
  else if (not plugin::StorageEngine::createSchema(schema_message)) // Try to create it 
 
129
  {
 
130
    my_error(ER_CANT_CREATE_DB, MYF(0), schema_message.name().c_str(), errno);
 
131
    error= true;
 
132
  }
 
133
  else // Created !
 
134
  {
 
135
    transaction_services.createSchema(session, schema_message);
 
136
    session->my_ok(1);
 
137
  }
 
138
 
 
139
  pthread_mutex_unlock(&LOCK_create_db);
 
140
  start_waiting_global_read_lock(session);
145
141
 
146
142
  return error;
147
143
}
165
161
    has the global read lock and refuses the operation with
166
162
    ER_CANT_UPDATE_WITH_READLOCK if applicable.
167
163
  */
168
 
  if ((session->wait_if_global_read_lock(false, true)))
169
 
    return false;
170
 
 
171
 
  bool success;
172
 
  {
173
 
    boost::mutex::scoped_lock scopedLock(LOCK_create_db);
174
 
 
175
 
    SchemaIdentifier schema_idenifier(schema_message.name());
176
 
    if (not plugin::StorageEngine::doesSchemaExist(schema_idenifier))
177
 
    {
178
 
      my_error(ER_SCHEMA_DOES_NOT_EXIST, MYF(0), schema_message.name().c_str());
179
 
      return false;
180
 
    }
181
 
 
182
 
    /* Change options if current database is being altered. */
183
 
    success= plugin::StorageEngine::alterSchema(schema_message);
184
 
 
185
 
    if (success)
186
 
    {
187
 
      transaction_services.rawStatement(session, *session->getQueryString());
188
 
      session->my_ok(1);
189
 
    }
190
 
    else
191
 
    {
192
 
      my_error(ER_ALTER_SCHEMA, MYF(0), schema_message.name().c_str());
193
 
    }
194
 
  }
195
 
  session->startWaitingGlobalReadLock();
 
164
  if ((wait_if_global_read_lock(session, 0, 1)))
 
165
    return false;
 
166
 
 
167
  pthread_mutex_lock(&LOCK_create_db);
 
168
 
 
169
  SchemaIdentifier schema_idenifier(schema_message.name());
 
170
  if (not plugin::StorageEngine::doesSchemaExist(schema_idenifier))
 
171
  {
 
172
    my_error(ER_SCHEMA_DOES_NOT_EXIST, MYF(0), schema_message.name().c_str());
 
173
    return false;
 
174
  }
 
175
 
 
176
  /* Change options if current database is being altered. */
 
177
  bool success= plugin::StorageEngine::alterSchema(schema_message);
 
178
 
 
179
  if (success)
 
180
  {
 
181
    transaction_services.rawStatement(session, session->getQueryString());
 
182
    session->my_ok(1);
 
183
  }
 
184
  else
 
185
  {
 
186
    my_error(ER_ALTER_SCHEMA, MYF(0), schema_message.name().c_str());
 
187
  }
 
188
 
 
189
  pthread_mutex_unlock(&LOCK_create_db);
 
190
  start_waiting_global_read_lock(session);
196
191
 
197
192
  return success;
198
193
}
219
214
{
220
215
  long deleted=0;
221
216
  int error= false;
222
 
  TableIdentifier::vector dropped_tables;
 
217
  plugin::TableNameList dropped_tables;
223
218
  message::Schema schema_proto;
224
219
 
225
220
  /*
234
229
    has the global read lock and refuses the operation with
235
230
    ER_CANT_UPDATE_WITH_READLOCK if applicable.
236
231
  */
237
 
  if (session->wait_if_global_read_lock(false, true))
 
232
  if (wait_if_global_read_lock(session, 0, 1))
238
233
  {
239
234
    return -1;
240
235
  }
250
245
    session->close_temporary_table(table);
251
246
  }
252
247
 
 
248
  pthread_mutex_lock(&LOCK_create_db);
 
249
 
 
250
 
 
251
  /* See if the schema exists */
 
252
  if (not plugin::StorageEngine::doesSchemaExist(schema_identifier))
253
253
  {
254
 
    boost::mutex::scoped_lock scopedLock(LOCK_create_db);
255
 
 
256
 
    /* See if the schema exists */
257
 
    if (not plugin::StorageEngine::doesSchemaExist(schema_identifier))
 
254
    if (if_exists)
258
255
    {
259
 
      std::string path;
260
 
      schema_identifier.getSQLPath(path);
261
 
 
262
 
      if (if_exists)
263
 
      {
264
 
        push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
265
 
                            ER_DB_DROP_EXISTS, ER(ER_DB_DROP_EXISTS),
266
 
                            path.c_str());
267
 
      }
268
 
      else
269
 
      {
270
 
        error= -1;
271
 
        my_error(ER_DB_DROP_EXISTS, MYF(0), path.c_str());
272
 
        goto exit;
273
 
      }
 
256
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
 
257
                          ER_DB_DROP_EXISTS, ER(ER_DB_DROP_EXISTS),
 
258
                          schema_identifier.getSQLPath().c_str());
274
259
    }
275
260
    else
276
261
    {
277
 
      /* After deleting database, remove all cache entries related to schema */
278
 
      table::Cache::singleton().removeSchema(schema_identifier);
279
 
 
280
 
 
281
262
      error= -1;
282
 
      deleted= drop_tables_via_filenames(session, schema_identifier, dropped_tables);
283
 
      if (deleted >= 0)
284
 
      {
285
 
        error= 0;
286
 
      }
 
263
      my_error(ER_DB_DROP_EXISTS, MYF(0), schema_identifier.getSQLPath().c_str());
 
264
      goto exit;
287
265
    }
 
266
  }
 
267
  else
 
268
  {
 
269
    pthread_mutex_lock(&LOCK_open); /* After deleting database, remove all cache entries related to schema */
 
270
    remove_db_from_cache(schema_identifier);
 
271
    pthread_mutex_unlock(&LOCK_open);
 
272
 
 
273
 
 
274
    error= -1;
 
275
    deleted= mysql_rm_known_files(session, schema_identifier, dropped_tables);
288
276
    if (deleted >= 0)
289
277
    {
290
 
      assert(not session->getQueryString()->empty());
291
 
 
292
 
      TransactionServices &transaction_services= TransactionServices::singleton();
293
 
      transaction_services.dropSchema(session, schema_identifier.getSchemaName());
294
 
      session->clear_error();
295
 
      session->server_status|= SERVER_STATUS_DB_DROPPED;
296
 
      session->my_ok((uint32_t) deleted);
297
 
      session->server_status&= ~SERVER_STATUS_DB_DROPPED;
 
278
      error= 0;
298
279
    }
299
 
    else
 
280
  }
 
281
  if (deleted >= 0)
 
282
  {
 
283
    assert(! session->query.empty());
 
284
 
 
285
    TransactionServices &transaction_services= TransactionServices::singleton();
 
286
    transaction_services.dropSchema(session, schema_identifier.getSchemaName());
 
287
    session->clear_error();
 
288
    session->server_status|= SERVER_STATUS_DB_DROPPED;
 
289
    session->my_ok((uint32_t) deleted);
 
290
    session->server_status&= ~SERVER_STATUS_DB_DROPPED;
 
291
  }
 
292
  else
 
293
  {
 
294
    char *query, *query_pos, *query_end, *query_data_start;
 
295
 
 
296
    if (!(query= (char*) session->alloc(MAX_DROP_TABLE_Q_LEN)))
 
297
      goto exit; /* not much else we can do */
 
298
    query_pos= query_data_start= strcpy(query,"drop table ")+11;
 
299
    query_end= query + MAX_DROP_TABLE_Q_LEN;
 
300
 
 
301
    TransactionServices &transaction_services= TransactionServices::singleton();
 
302
    for (plugin::TableNameList::iterator it= dropped_tables.begin();
 
303
         it != dropped_tables.end();
 
304
         it++)
300
305
    {
301
 
      char *query, *query_pos, *query_end, *query_data_start;
302
 
 
303
 
      if (!(query= (char*) session->alloc(MAX_DROP_TABLE_Q_LEN)))
304
 
        goto exit; /* not much else we can do */
305
 
      query_pos= query_data_start= strcpy(query,"drop table ")+11;
306
 
      query_end= query + MAX_DROP_TABLE_Q_LEN;
307
 
 
308
 
      TransactionServices &transaction_services= TransactionServices::singleton();
309
 
      for (TableIdentifier::vector::iterator it= dropped_tables.begin();
310
 
           it != dropped_tables.end();
311
 
           it++)
312
 
      {
313
 
        uint32_t tbl_name_len;
314
 
 
315
 
        /* 3 for the quotes and the comma*/
316
 
        tbl_name_len= (*it).getTableName().length() + 3;
317
 
        if (query_pos + tbl_name_len + 1 >= query_end)
318
 
        {
319
 
          /* These DDL methods and logging protected with LOCK_create_db */
320
 
          transaction_services.rawStatement(session, query);
321
 
          query_pos= query_data_start;
322
 
        }
323
 
 
324
 
        *query_pos++ = '`';
325
 
        query_pos= strcpy(query_pos, (*it).getTableName().c_str()) + (tbl_name_len-3);
326
 
        *query_pos++ = '`';
327
 
        *query_pos++ = ',';
328
 
      }
329
 
 
330
 
      if (query_pos != query_data_start)
 
306
      uint32_t tbl_name_len;
 
307
 
 
308
      /* 3 for the quotes and the comma*/
 
309
      tbl_name_len= (*it).length() + 3;
 
310
      if (query_pos + tbl_name_len + 1 >= query_end)
331
311
      {
332
312
        /* These DDL methods and logging protected with LOCK_create_db */
333
313
        transaction_services.rawStatement(session, query);
 
314
        query_pos= query_data_start;
334
315
      }
335
 
    }
 
316
 
 
317
      *query_pos++ = '`';
 
318
      query_pos= strcpy(query_pos, (*it).c_str()) + (tbl_name_len-3);
 
319
      *query_pos++ = '`';
 
320
      *query_pos++ = ',';
 
321
    }
 
322
 
 
323
    if (query_pos != query_data_start)
 
324
    {
 
325
      /* These DDL methods and logging protected with LOCK_create_db */
 
326
      transaction_services.rawStatement(session, query);
 
327
    }
 
328
  }
336
329
 
337
330
exit:
338
 
    /*
339
 
      If this database was the client's selected database, we silently
340
 
      change the client's selected database to nothing (to have an empty
341
 
      SELECT DATABASE() in the future). For this we free() session->db and set
342
 
      it to 0.
343
 
    */
344
 
    if (schema_identifier.compare(*session->schema()))
345
 
      mysql_change_db_impl(session);
346
 
  }
347
 
 
348
 
  session->startWaitingGlobalReadLock();
 
331
  /*
 
332
    If this database was the client's selected database, we silently
 
333
    change the client's selected database to nothing (to have an empty
 
334
    SELECT DATABASE() in the future). For this we free() session->db and set
 
335
    it to 0.
 
336
  */
 
337
  if (schema_identifier.compare(session->db))
 
338
    mysql_change_db_impl(session);
 
339
  pthread_mutex_unlock(&LOCK_create_db);
 
340
  start_waiting_global_read_lock(session);
349
341
 
350
342
  return error;
351
343
}
353
345
 
354
346
static int rm_table_part2(Session *session, TableList *tables)
355
347
{
356
 
  TransactionServices &transaction_services= TransactionServices::singleton();
357
 
 
358
348
  TableList *table;
359
349
  String wrong_tables;
360
350
  int error= 0;
361
351
  bool foreign_key_error= false;
362
352
 
363
 
  {
364
 
    table::Cache::singleton().mutex().lock(); /* Part 2 of rm a table */
365
 
 
366
 
    if (session->lock_table_names_exclusively(tables))
367
 
    {
368
 
      table::Cache::singleton().mutex().unlock();
369
 
      return 1;
370
 
    }
371
 
 
372
 
    /* Don't give warnings for not found errors, as we already generate notes */
373
 
    session->no_warnings_for_error= 1;
374
 
 
375
 
    for (table= tables; table; table= table->next_local)
376
 
    {
377
 
      const char *db=table->getSchemaName();
378
 
      TableIdentifier identifier(table->getSchemaName(), table->getTableName());
379
 
 
380
 
      plugin::StorageEngine *table_type;
381
 
 
382
 
      error= session->drop_temporary_table(identifier);
383
 
 
384
 
      switch (error) {
385
 
      case  0:
386
 
        // removed temporary table
387
 
        continue;
388
 
      case -1:
389
 
        error= 1;
390
 
        tables->unlock_table_names();
391
 
        table::Cache::singleton().mutex().unlock();
392
 
        session->no_warnings_for_error= 0;
393
 
 
394
 
        return(error);
395
 
      default:
396
 
        // temporary table not found
397
 
        error= 0;
398
 
      }
399
 
 
400
 
      table_type= table->getDbType();
401
 
 
402
 
      {
403
 
        Table *locked_table;
404
 
        abort_locked_tables(session, identifier);
405
 
        table::Cache::singleton().removeTable(session, identifier,
406
 
                                              RTFC_WAIT_OTHER_THREAD_FLAG |
407
 
                                              RTFC_CHECK_KILLED_FLAG);
408
 
        /*
409
 
          If the table was used in lock tables, remember it so that
410
 
          unlock_table_names can free it
411
 
        */
412
 
        if ((locked_table= drop_locked_tables(session, identifier)))
413
 
          table->table= locked_table;
414
 
 
415
 
        if (session->getKilled())
416
 
        {
417
 
          error= -1;
418
 
          tables->unlock_table_names();
419
 
          table::Cache::singleton().mutex().unlock();
420
 
          session->no_warnings_for_error= 0;
421
 
 
422
 
          return(error);
423
 
        }
424
 
      }
425
 
      identifier.getPath();
426
 
 
427
 
      if (table_type == NULL && not plugin::StorageEngine::doesTableExist(*session, identifier))
428
 
      {
429
 
        // Table was not found on disk and table can't be created from engine
430
 
        push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
431
 
                            ER_BAD_TABLE_ERROR, ER(ER_BAD_TABLE_ERROR),
432
 
                            table->getTableName());
433
 
      }
434
 
      else
435
 
      {
436
 
        error= plugin::StorageEngine::dropTable(*session, identifier);
437
 
 
438
 
        if ((error == ENOENT || error == HA_ERR_NO_SUCH_TABLE))
439
 
        {
440
 
          error= 0;
441
 
          session->clear_error();
442
 
        }
443
 
 
444
 
        if (error == HA_ERR_ROW_IS_REFERENCED)
445
 
        {
446
 
          /* the table is referenced by a foreign key constraint */
447
 
          foreign_key_error= true;
448
 
        }
449
 
      }
450
 
 
451
 
      if (error == 0 || (foreign_key_error == false))
452
 
      {
453
 
        transaction_services.dropTable(session, string(db), string(table->getTableName()), true);
454
 
      }
455
 
 
456
 
      if (error)
457
 
      {
458
 
        if (wrong_tables.length())
459
 
          wrong_tables.append(',');
460
 
        wrong_tables.append(String(table->getTableName(),system_charset_info));
461
 
      }
462
 
    }
463
 
    /*
464
 
      It's safe to unlock table::Cache::singleton().mutex(): we have an exclusive lock
465
 
      on the table name.
466
 
    */
467
 
    table::Cache::singleton().mutex().unlock();
468
 
  }
469
 
 
 
353
  pthread_mutex_lock(&LOCK_open); /* Part 2 of rm a table */
 
354
 
 
355
  /*
 
356
    If we have the table in the definition cache, we don't have to check the
 
357
    .frm cursor to find if the table is a normal table (not view) and what
 
358
    engine to use.
 
359
  */
 
360
 
 
361
  for (table= tables; table; table= table->next_local)
 
362
  {
 
363
    TableIdentifier identifier(table->db, table->table_name);
 
364
    TableShare *share;
 
365
    table->db_type= NULL;
 
366
    if ((share= TableShare::getShare(identifier)))
 
367
    {
 
368
      table->db_type= share->db_type();
 
369
    }
 
370
  }
 
371
 
 
372
  if (lock_table_names_exclusively(session, tables))
 
373
  {
 
374
    pthread_mutex_unlock(&LOCK_open);
 
375
    return 1;
 
376
  }
 
377
 
 
378
  /* Don't give warnings for not found errors, as we already generate notes */
 
379
  session->no_warnings_for_error= 1;
 
380
 
 
381
  for (table= tables; table; table= table->next_local)
 
382
  {
 
383
    char *db=table->db;
 
384
    plugin::StorageEngine *table_type;
 
385
 
 
386
    error= session->drop_temporary_table(table);
 
387
 
 
388
    switch (error) {
 
389
    case  0:
 
390
      // removed temporary table
 
391
      continue;
 
392
    case -1:
 
393
      error= 1;
 
394
      goto err_with_placeholders;
 
395
    default:
 
396
      // temporary table not found
 
397
      error= 0;
 
398
    }
 
399
 
 
400
    table_type= table->db_type;
 
401
 
 
402
    {
 
403
      Table *locked_table;
 
404
      abort_locked_tables(session, db, table->table_name);
 
405
      remove_table_from_cache(session, db, table->table_name,
 
406
                              RTFC_WAIT_OTHER_THREAD_FLAG |
 
407
                              RTFC_CHECK_KILLED_FLAG);
 
408
      /*
 
409
        If the table was used in lock tables, remember it so that
 
410
        unlock_table_names can free it
 
411
      */
 
412
      if ((locked_table= drop_locked_tables(session, db, table->table_name)))
 
413
        table->table= locked_table;
 
414
 
 
415
      if (session->killed)
 
416
      {
 
417
        error= -1;
 
418
        goto err_with_placeholders;
 
419
      }
 
420
    }
 
421
 
 
422
    TableIdentifier identifier(db, table->table_name);
 
423
    identifier.getPath();
 
424
 
 
425
    if (table_type == NULL && not plugin::StorageEngine::doesTableExist(*session, identifier))
 
426
    {
 
427
      // Table was not found on disk and table can't be created from engine
 
428
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
 
429
                          ER_BAD_TABLE_ERROR, ER(ER_BAD_TABLE_ERROR),
 
430
                          table->table_name);
 
431
    }
 
432
    else
 
433
    {
 
434
      error= plugin::StorageEngine::dropTable(*session, identifier);
 
435
 
 
436
      if ((error == ENOENT || error == HA_ERR_NO_SUCH_TABLE))
 
437
      {
 
438
        error= 0;
 
439
        session->clear_error();
 
440
      }
 
441
 
 
442
      if (error == HA_ERR_ROW_IS_REFERENCED)
 
443
      {
 
444
        /* the table is referenced by a foreign key constraint */
 
445
        foreign_key_error= true;
 
446
      }
 
447
    }
 
448
 
 
449
    if (error == 0 || (foreign_key_error == false))
 
450
        write_bin_log_drop_table(session, true, db, table->table_name);
 
451
 
 
452
    if (error)
 
453
    {
 
454
      if (wrong_tables.length())
 
455
        wrong_tables.append(',');
 
456
      wrong_tables.append(String(table->table_name,system_charset_info));
 
457
    }
 
458
  }
 
459
  /*
 
460
    It's safe to unlock LOCK_open: we have an exclusive lock
 
461
    on the table name.
 
462
  */
 
463
  pthread_mutex_unlock(&LOCK_open);
470
464
  error= 0;
471
465
  if (wrong_tables.length())
472
466
  {
480
474
    error= 1;
481
475
  }
482
476
 
483
 
  {
484
 
    boost::mutex::scoped_lock scopedLock(table::Cache::singleton().mutex()); /* final bit in rm table lock */
485
 
    tables->unlock_table_names();
486
 
  }
 
477
  pthread_mutex_lock(&LOCK_open); /* final bit in rm table lock */
 
478
err_with_placeholders:
 
479
  unlock_table_names(tables, NULL);
 
480
  pthread_mutex_unlock(&LOCK_open);
487
481
  session->no_warnings_for_error= 0;
488
482
 
489
483
  return(error);
494
488
  session MUST be set when calling this function!
495
489
*/
496
490
 
497
 
static long drop_tables_via_filenames(Session *session,
498
 
                                      SchemaIdentifier &schema_identifier,
499
 
                                      TableIdentifier::vector &dropped_tables)
 
491
static long mysql_rm_known_files(Session *session,
 
492
                                 SchemaIdentifier &schema_identifier,
 
493
                                 plugin::TableNameList &dropped_tables)
500
494
{
501
495
  long deleted= 0;
502
496
  TableList *tot_list= NULL, **tot_list_next;
503
497
 
504
498
  tot_list_next= &tot_list;
505
499
 
506
 
  plugin::StorageEngine::getIdentifiers(*session, schema_identifier, dropped_tables);
 
500
  plugin::StorageEngine::getTableNames(*session, schema_identifier, dropped_tables);
507
501
 
508
 
  for (TableIdentifier::vector::iterator it= dropped_tables.begin();
 
502
  for (plugin::TableNameList::iterator it= dropped_tables.begin();
509
503
       it != dropped_tables.end();
510
504
       it++)
511
505
  {
515
509
    TableList *table_list=(TableList*)
516
510
      session->calloc(sizeof(*table_list) +
517
511
                      db_len + 1 +
518
 
                      (*it).getTableName().length() + 1);
 
512
                      (*it).length() + 1);
519
513
 
520
514
    if (not table_list)
521
515
      return -1;
522
516
 
523
 
    table_list->setSchemaName((char*) (table_list+1));
524
 
    table_list->setTableName(strcpy((char*) (table_list+1), schema_identifier.getSchemaName().c_str()) + db_len + 1);
525
 
    TableIdentifier::filename_to_tablename((*it).getTableName().c_str(), const_cast<char *>(table_list->getTableName()), (*it).getTableName().size() + 1);
526
 
    table_list->alias= table_list->getTableName();  // If lower_case_table_names=2
527
 
    table_list->setInternalTmpTable((strncmp((*it).getTableName().c_str(),
 
517
    table_list->db= (char*) (table_list+1);
 
518
    table_list->table_name= strcpy(table_list->db, schema_identifier.getSchemaName().c_str()) + db_len + 1;
 
519
    filename_to_tablename((*it).c_str(), table_list->table_name,
 
520
                          (*it).size() + 1);
 
521
    table_list->alias= table_list->table_name;  // If lower_case_table_names=2
 
522
    table_list->internal_tmp_table= (strncmp((*it).c_str(),
528
523
                                             TMP_FILE_PREFIX,
529
 
                                             strlen(TMP_FILE_PREFIX)) == 0));
 
524
                                             strlen(TMP_FILE_PREFIX)) == 0);
530
525
    /* Link into list */
531
526
    (*tot_list_next)= table_list;
532
527
    tot_list_next= &table_list->next_local;
533
528
    deleted++;
534
529
  }
535
 
  if (session->getKilled())
 
530
  if (session->killed)
536
531
    return -1;
537
532
 
538
533
  if (tot_list)
544
539
 
545
540
  if (not plugin::StorageEngine::dropSchema(schema_identifier))
546
541
  {
547
 
    std::string path;
548
 
    schema_identifier.getSQLPath(path);
549
 
    my_error(ER_DROP_SCHEMA, MYF(0), path.c_str());
550
 
 
 
542
    my_error(ER_DROP_SCHEMA, MYF(0), schema_identifier.getSQLPath().c_str());
551
543
    return -1;
552
544
  }
553
545
 
625
617
    return true;
626
618
  }
627
619
 
628
 
  if (not check_db_name(session, schema_identifier))
 
620
  if (not check_db_name(schema_identifier))
629
621
  {
630
 
    std::string path;
631
 
    schema_identifier.getSQLPath(path);
632
 
    my_error(ER_WRONG_DB_NAME, MYF(0), path.c_str());
 
622
    my_error(ER_WRONG_DB_NAME, MYF(0), schema_identifier.getSQLPath().c_str());
633
623
 
634
624
    return true;
635
625
  }
637
627
  if (not plugin::StorageEngine::doesSchemaExist(schema_identifier))
638
628
  {
639
629
    /* Report an error and free new_db_file_name. */
640
 
    std::string path;
641
 
    schema_identifier.getSQLPath(path);
642
630
 
643
 
    my_error(ER_BAD_DB_ERROR, MYF(0), path.c_str());
 
631
    my_error(ER_BAD_DB_ERROR, MYF(0), schema_identifier.getSQLPath().c_str());
644
632
 
645
633
    /* The operation failed. */
646
634
 
686
674
      the previous database name, we should do it explicitly.
687
675
    */
688
676
 
689
 
    session->set_db(schema_identifier.getSchemaName());
 
677
    session->set_db(schema_identifier.getLower());
690
678
  }
691
679
}
692
680