~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/db.cc

  • Committer: Monty Taylor
  • Date: 2010-11-25 01:53:19 UTC
  • mto: (1953.1.2 build)
  • mto: This revision was merged to the branch mainline in revision 1955.
  • Revision ID: mordred@inaugust.com-20101125015319-ia85msn25uemopgc
Re-enabled -Wformat and then cleaned up the carnage.

Show diffs side-by-side

added added

removed removed

Lines of Context:
59
59
 
60
60
static long drop_tables_via_filenames(Session *session,
61
61
                                 SchemaIdentifier &schema_identifier,
62
 
                                 TableIdentifier::vector &dropped_tables);
 
62
                                 TableIdentifiers &dropped_tables);
63
63
static void mysql_change_db_impl(Session *session);
64
64
static void mysql_change_db_impl(Session *session, SchemaIdentifier &schema_identifier);
65
65
 
219
219
{
220
220
  long deleted=0;
221
221
  int error= false;
222
 
  TableIdentifier::vector dropped_tables;
 
222
  TableIdentifiers dropped_tables;
223
223
  message::Schema schema_proto;
224
224
 
225
225
  /*
256
256
    /* See if the schema exists */
257
257
    if (not plugin::StorageEngine::doesSchemaExist(schema_identifier))
258
258
    {
259
 
      std::string path;
260
 
      schema_identifier.getSQLPath(path);
261
 
 
262
259
      if (if_exists)
263
260
      {
264
261
        push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
265
262
                            ER_DB_DROP_EXISTS, ER(ER_DB_DROP_EXISTS),
266
 
                            path.c_str());
 
263
                            schema_identifier.getSQLPath().c_str());
267
264
      }
268
265
      else
269
266
      {
270
267
        error= -1;
271
 
        my_error(ER_DB_DROP_EXISTS, MYF(0), path.c_str());
 
268
        my_error(ER_DB_DROP_EXISTS, MYF(0), schema_identifier.getSQLPath().c_str());
272
269
        goto exit;
273
270
      }
274
271
    }
306
303
      query_end= query + MAX_DROP_TABLE_Q_LEN;
307
304
 
308
305
      TransactionServices &transaction_services= TransactionServices::singleton();
309
 
      for (TableIdentifier::vector::iterator it= dropped_tables.begin();
 
306
      for (TableIdentifiers::iterator it= dropped_tables.begin();
310
307
           it != dropped_tables.end();
311
308
           it++)
312
309
      {
341
338
      SELECT DATABASE() in the future). For this we free() session->db and set
342
339
      it to 0.
343
340
    */
344
 
    if (schema_identifier.compare(*session->schema()))
 
341
    if (schema_identifier.compare(session->db))
345
342
      mysql_change_db_impl(session);
346
343
  }
347
344
 
360
357
  int error= 0;
361
358
  bool foreign_key_error= false;
362
359
 
363
 
  {
364
 
    table::Cache::singleton().mutex().lock(); /* Part 2 of rm a table */
365
 
 
366
 
    if (session->lock_table_names_exclusively(tables))
367
 
    {
 
360
  table::Cache::singleton().mutex().lock(); /* Part 2 of rm a table */
 
361
 
 
362
  if (session->lock_table_names_exclusively(tables))
 
363
  {
 
364
    table::Cache::singleton().mutex().unlock();
 
365
    return 1;
 
366
  }
 
367
 
 
368
  /* Don't give warnings for not found errors, as we already generate notes */
 
369
  session->no_warnings_for_error= 1;
 
370
 
 
371
  for (table= tables; table; table= table->next_local)
 
372
  {
 
373
    const char *db=table->getSchemaName();
 
374
    TableIdentifier identifier(table->getSchemaName(), table->getTableName());
 
375
 
 
376
    plugin::StorageEngine *table_type;
 
377
 
 
378
    error= session->drop_temporary_table(identifier);
 
379
 
 
380
    switch (error) {
 
381
    case  0:
 
382
      // removed temporary table
 
383
      continue;
 
384
    case -1:
 
385
      error= 1;
 
386
      tables->unlock_table_names();
368
387
      table::Cache::singleton().mutex().unlock();
369
 
      return 1;
 
388
      session->no_warnings_for_error= 0;
 
389
 
 
390
      return(error);
 
391
    default:
 
392
      // temporary table not found
 
393
      error= 0;
370
394
    }
371
395
 
372
 
    /* Don't give warnings for not found errors, as we already generate notes */
373
 
    session->no_warnings_for_error= 1;
 
396
    table_type= table->getDbType();
374
397
 
375
 
    for (table= tables; table; table= table->next_local)
376
398
    {
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;
 
399
      Table *locked_table;
 
400
      abort_locked_tables(session, identifier);
 
401
      table::Cache::singleton().removeTable(session, identifier,
 
402
                                            RTFC_WAIT_OTHER_THREAD_FLAG |
 
403
                                            RTFC_CHECK_KILLED_FLAG);
 
404
      /*
 
405
        If the table was used in lock tables, remember it so that
 
406
        unlock_table_names can free it
 
407
      */
 
408
      if ((locked_table= drop_locked_tables(session, identifier)))
 
409
        table->table= locked_table;
 
410
 
 
411
      if (session->getKilled())
 
412
      {
 
413
        error= -1;
390
414
        tables->unlock_table_names();
391
415
        table::Cache::singleton().mutex().unlock();
392
416
        session->no_warnings_for_error= 0;
393
417
 
394
418
        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();
 
419
      }
 
420
    }
 
421
    identifier.getPath();
 
422
 
 
423
    if (table_type == NULL && not plugin::StorageEngine::doesTableExist(*session, identifier))
 
424
    {
 
425
      // Table was not found on disk and table can't be created from engine
 
426
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
 
427
                          ER_BAD_TABLE_ERROR, ER(ER_BAD_TABLE_ERROR),
 
428
                          table->getTableName());
 
429
    }
 
430
    else
 
431
    {
 
432
      error= plugin::StorageEngine::dropTable(*session, identifier);
 
433
 
 
434
      if ((error == ENOENT || error == HA_ERR_NO_SUCH_TABLE))
 
435
      {
 
436
        error= 0;
 
437
        session->clear_error();
 
438
      }
 
439
 
 
440
      if (error == HA_ERR_ROW_IS_REFERENCED)
 
441
      {
 
442
        /* the table is referenced by a foreign key constraint */
 
443
        foreign_key_error= true;
 
444
      }
 
445
    }
 
446
 
 
447
    if (error == 0 || (foreign_key_error == false))
 
448
    {
 
449
      transaction_services.dropTable(session, string(db), string(table->getTableName()), true);
 
450
    }
 
451
 
 
452
    if (error)
 
453
    {
 
454
      if (wrong_tables.length())
 
455
        wrong_tables.append(',');
 
456
      wrong_tables.append(String(table->getTableName(),system_charset_info));
 
457
    }
468
458
  }
469
 
 
 
459
  /*
 
460
    It's safe to unlock table::Cache::singleton().mutex(): we have an exclusive lock
 
461
    on the table name.
 
462
  */
 
463
  table::Cache::singleton().mutex().unlock();
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
  table::Cache::singleton().mutex().lock(); /* final bit in rm table lock */
 
478
  tables->unlock_table_names();
 
479
  table::Cache::singleton().mutex().unlock();
487
480
  session->no_warnings_for_error= 0;
488
481
 
489
482
  return(error);
496
489
 
497
490
static long drop_tables_via_filenames(Session *session,
498
491
                                      SchemaIdentifier &schema_identifier,
499
 
                                      TableIdentifier::vector &dropped_tables)
 
492
                                      TableIdentifiers &dropped_tables)
500
493
{
501
494
  long deleted= 0;
502
495
  TableList *tot_list= NULL, **tot_list_next;
505
498
 
506
499
  plugin::StorageEngine::getIdentifiers(*session, schema_identifier, dropped_tables);
507
500
 
508
 
  for (TableIdentifier::vector::iterator it= dropped_tables.begin();
 
501
  for (TableIdentifiers::iterator it= dropped_tables.begin();
509
502
       it != dropped_tables.end();
510
503
       it++)
511
504
  {
544
537
 
545
538
  if (not plugin::StorageEngine::dropSchema(schema_identifier))
546
539
  {
547
 
    std::string path;
548
 
    schema_identifier.getSQLPath(path);
549
 
    my_error(ER_DROP_SCHEMA, MYF(0), path.c_str());
550
 
 
 
540
    my_error(ER_DROP_SCHEMA, MYF(0), schema_identifier.getSQLPath().c_str());
551
541
    return -1;
552
542
  }
553
543
 
627
617
 
628
618
  if (not check_db_name(session, schema_identifier))
629
619
  {
630
 
    std::string path;
631
 
    schema_identifier.getSQLPath(path);
632
 
    my_error(ER_WRONG_DB_NAME, MYF(0), path.c_str());
 
620
    my_error(ER_WRONG_DB_NAME, MYF(0), schema_identifier.getSQLPath().c_str());
633
621
 
634
622
    return true;
635
623
  }
637
625
  if (not plugin::StorageEngine::doesSchemaExist(schema_identifier))
638
626
  {
639
627
    /* Report an error and free new_db_file_name. */
640
 
    std::string path;
641
 
    schema_identifier.getSQLPath(path);
642
628
 
643
 
    my_error(ER_BAD_DB_ERROR, MYF(0), path.c_str());
 
629
    my_error(ER_BAD_DB_ERROR, MYF(0), schema_identifier.getSQLPath().c_str());
644
630
 
645
631
    /* The operation failed. */
646
632