~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_table.cc

  • Committer: Brian Aker
  • Date: 2009-10-07 21:45:49 UTC
  • Revision ID: brian@gaz-20091007214549-nsf3usp3ppqeo1xq
Update to delete table to centralize the replication logic.

Show diffs side-by-side

added added

removed removed

Lines of Context:
314
314
  SYNOPSIS
315
315
    write_bin_log()
316
316
    session                           Thread object
317
 
    clear_error                   is clear_error to be called
318
317
    query                         Query to log
319
318
    query_length                  Length of query
320
319
 
326
325
    file
327
326
*/
328
327
 
329
 
void write_bin_log(Session *session, bool,
 
328
void write_bin_log(Session *session,
330
329
                   char const *query, size_t query_length)
331
330
{
332
331
  ReplicationServices &replication_services= ReplicationServices::singleton();
334
333
}
335
334
 
336
335
 
 
336
/* Should should be refactored to go away */
 
337
static void write_bin_log_drop_table(Session *session, bool if_exists, const char *db_name, const char *table_name)
 
338
{
 
339
  ReplicationServices &replication_services= ReplicationServices::singleton();
 
340
  string built_query;
 
341
 
 
342
  if (if_exists)
 
343
    built_query.append("DROP Table IF EXISTS ");
 
344
  else
 
345
    built_query.append("DROP Table ");
 
346
 
 
347
  built_query.append("`");
 
348
  if (session->db == NULL || strcmp(db_name ,session->db) != 0)
 
349
  {
 
350
    built_query.append(db_name);
 
351
    built_query.append("`.`");
 
352
  }
 
353
 
 
354
  built_query.append(table_name);
 
355
  built_query.append("`");
 
356
  replication_services.rawStatement(session, built_query.c_str(), built_query.length());
 
357
}
 
358
 
 
359
 
337
360
/*
338
361
 delete (drop) tables.
339
362
 
381
404
    LOCK_open during wait_if_global_read_lock(), other threads could not
382
405
    close their tables. This would make a pretty deadlock.
383
406
  */
384
 
  error= mysql_rm_table_part2(session, tables, if_exists, drop_temporary, 0);
 
407
  error= mysql_rm_table_part2(session, tables, if_exists, drop_temporary, false);
385
408
 
386
409
  if (need_start_waiting)
387
410
    start_waiting_global_read_lock(session);
402
425
    if_exists           If set, don't give an error if table doesn't exists.
403
426
                        In this case we give an warning of level 'NOTE'
404
427
    drop_temporary      Only drop temporary tables
405
 
    drop_view           Allow to delete VIEW .frm
406
428
    dont_log_query      Don't write query to log files. This will also not
407
429
                        generate warnings if the handler files doesn't exists
408
430
 
430
452
  uint32_t path_length= 0;
431
453
  String wrong_tables;
432
454
  int error= 0;
433
 
  int non_temp_tables_count= 0;
434
 
  bool some_tables_deleted=0, tmp_table_deleted=0, foreign_key_error=0;
435
 
  String built_query;
436
 
 
437
 
  if (!dont_log_query)
438
 
  {
439
 
    built_query.set_charset(system_charset_info);
440
 
    if (if_exists)
441
 
      built_query.append("DROP Table IF EXISTS ");
442
 
    else
443
 
      built_query.append("DROP Table ");
444
 
  }
 
455
  bool foreign_key_error= false;
445
456
 
446
457
  pthread_mutex_lock(&LOCK_open); /* Part 2 of rm a table */
447
458
 
478
489
    switch (error) {
479
490
    case  0:
480
491
      // removed temporary table
481
 
      tmp_table_deleted= 1;
482
492
      continue;
483
493
    case -1:
484
494
      error= 1;
488
498
      error= 0;
489
499
    }
490
500
 
491
 
    /*
492
 
      If row-based replication is used and the table is not a
493
 
      temporary table, we add the table name to the drop statement
494
 
      being built.  The string always end in a comma and the comma
495
 
      will be chopped off before being written to the binary log.
496
 
      */
497
 
    if (!dont_log_query)
498
 
    {
499
 
      non_temp_tables_count++;
500
 
      /*
501
 
        Don't write the database name if it is the current one (or if
502
 
        session->db is NULL).
503
 
      */
504
 
      built_query.append("`");
505
 
      if (session->db == NULL || strcmp(db,session->db) != 0)
506
 
      {
507
 
        built_query.append(db);
508
 
        built_query.append("`.`");
509
 
      }
510
 
 
511
 
      built_query.append(table->table_name);
512
 
      built_query.append("`,");
513
 
    }
514
 
 
515
501
    table_type= table->db_type;
516
502
    if (!drop_temporary)
517
503
    {
535
521
      /* remove .frm file and engine files */
536
522
      path_length= build_table_filename(path, sizeof(path), db, table->table_name, table->internal_tmp_table);
537
523
    }
 
524
 
538
525
    if (drop_temporary ||
539
526
        ((table_type == NULL
540
527
          && (plugin::StorageEngine::getTableProto(path, NULL) != EEXIST))))
552
539
      error= plugin::StorageEngine::deleteTable(session, path, db,
553
540
                                                table->table_name,
554
541
                                                ! dont_log_query);
555
 
      if ((error == ENOENT || error == HA_ERR_NO_SUCH_TABLE) &&
556
 
          if_exists)
 
542
      if ((error == ENOENT || error == HA_ERR_NO_SUCH_TABLE) && if_exists)
557
543
      {
558
544
        error= 0;
559
545
        session->clear_error();
560
546
      }
 
547
 
561
548
      if (error == HA_ERR_ROW_IS_REFERENCED)
562
549
      {
563
550
        /* the table is referenced by a foreign key constraint */
564
 
        foreign_key_error=1;
565
 
      }
566
 
      if (error == 0)
567
 
      {
568
 
          some_tables_deleted=1;
 
551
        foreign_key_error= true;
569
552
      }
570
553
    }
 
554
 
 
555
    if (error == 0 || (if_exists && foreign_key_error == false))
 
556
        write_bin_log_drop_table(session, if_exists, db, table->table_name);
 
557
 
571
558
    if (error)
572
559
    {
573
560
      if (wrong_tables.length())
593
580
    error= 1;
594
581
  }
595
582
 
596
 
  if (some_tables_deleted || tmp_table_deleted || !error)
597
 
  {
598
 
    if (!dont_log_query)
599
 
    {
600
 
      if ((non_temp_tables_count > 0 && !tmp_table_deleted))
601
 
      {
602
 
        /*
603
 
          In this case, we are either using statement-based
604
 
          replication or using row-based replication but have only
605
 
          deleted one or more non-temporary tables (and no temporary
606
 
          tables).  In this case, we can write the original query into
607
 
          the binary log.
608
 
         */
609
 
        write_bin_log(session, !error, session->query, session->query_length);
610
 
      }
611
 
      else if (non_temp_tables_count > 0 &&
612
 
               tmp_table_deleted)
613
 
      {
614
 
        /*
615
 
          In this case we have deleted both temporary and
616
 
          non-temporary tables, so:
617
 
          - since we have deleted a non-temporary table we have to
618
 
            binlog the statement, but
619
 
          - since we have deleted a temporary table we cannot binlog
620
 
            the statement (since the table has not been created on the
621
 
            slave, this might cause the slave to stop).
622
 
 
623
 
          Instead, we write a built statement, only containing the
624
 
          non-temporary tables, to the binary log
625
 
        */
626
 
        built_query.chop();                  // Chop of the last comma
627
 
        built_query.append(" /* generated by server */");
628
 
        write_bin_log(session, !error, built_query.ptr(), built_query.length());
629
 
      }
630
 
      /*
631
 
        The remaining cases are:
632
 
        - no tables where deleted and
633
 
        - only temporary tables where deleted and row-based
634
 
          replication is used.
635
 
        In both these cases, nothing should be written to the binary
636
 
        log.
637
 
      */
638
 
    }
639
 
  }
640
583
  pthread_mutex_lock(&LOCK_open); /* final bit in rm table lock */
641
584
err_with_placeholders:
642
585
  unlock_table_names(tables, NULL);
1827
1770
   */
1828
1771
  if (!internal_tmp_table &&
1829
1772
      ((!(create_info->options & HA_LEX_CREATE_TMP_TABLE))))
1830
 
    write_bin_log(session, true, session->query, session->query_length);
 
1773
    write_bin_log(session, session->query, session->query_length);
1831
1774
  error= false;
1832
1775
unlock_and_end:
1833
1776
  pthread_mutex_unlock(&LOCK_open);
2606
2549
        int result= store_create_info(table, &query, create_info);
2607
2550
 
2608
2551
        assert(result == 0); // store_create_info() always return 0
2609
 
        write_bin_log(session, true, query.ptr(), query.length());
 
2552
        write_bin_log(session, query.ptr(), query.length());
2610
2553
      }
2611
2554
      else                                      // Case 1
2612
 
        write_bin_log(session, true, session->query, session->query_length);
 
2555
        write_bin_log(session, session->query, session->query_length);
2613
2556
    }
2614
2557
  }
2615
2558