OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "sql/connection.h" | 9 #include "sql/connection.h" |
10 #include "sql/meta_table.h" | 10 #include "sql/meta_table.h" |
(...skipping 653 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
664 // of journal_mode PERSIST. | 664 // of journal_mode PERSIST. |
665 base::FilePath journal(db_path().value() + FILE_PATH_LITERAL("-journal")); | 665 base::FilePath journal(db_path().value() + FILE_PATH_LITERAL("-journal")); |
666 ASSERT_TRUE(base::PathExists(db_path())); | 666 ASSERT_TRUE(base::PathExists(db_path())); |
667 ASSERT_TRUE(base::PathExists(journal)); | 667 ASSERT_TRUE(base::PathExists(journal)); |
668 | 668 |
669 sql::Connection::Delete(db_path()); | 669 sql::Connection::Delete(db_path()); |
670 EXPECT_FALSE(base::PathExists(db_path())); | 670 EXPECT_FALSE(base::PathExists(db_path())); |
671 EXPECT_FALSE(base::PathExists(journal)); | 671 EXPECT_FALSE(base::PathExists(journal)); |
672 } | 672 } |
673 | 673 |
| 674 // Test that errors start happening once Poison() is called. |
| 675 TEST_F(SQLConnectionTest, Poison) { |
| 676 EXPECT_TRUE(db().Execute("CREATE TABLE x (x)")); |
| 677 |
| 678 // Before the Poison() call, things generally work. |
| 679 EXPECT_TRUE(db().IsSQLValid("INSERT INTO x VALUES ('x')")); |
| 680 EXPECT_TRUE(db().Execute("INSERT INTO x VALUES ('x')")); |
| 681 { |
| 682 sql::Statement s(db().GetUniqueStatement("SELECT COUNT(*) FROM x")); |
| 683 ASSERT_TRUE(s.is_valid()); |
| 684 ASSERT_TRUE(s.Step()); |
| 685 } |
| 686 |
| 687 // Get a statement which is valid before and will exist across Poison(). |
| 688 sql::Statement valid_statement( |
| 689 db().GetUniqueStatement("SELECT COUNT(*) FROM sqlite_master")); |
| 690 ASSERT_TRUE(valid_statement.is_valid()); |
| 691 ASSERT_TRUE(valid_statement.Step()); |
| 692 valid_statement.Reset(true); |
| 693 |
| 694 db().Poison(); |
| 695 |
| 696 // After the Poison() call, things fail. |
| 697 EXPECT_FALSE(db().IsSQLValid("INSERT INTO x VALUES ('x')")); |
| 698 EXPECT_FALSE(db().Execute("INSERT INTO x VALUES ('x')")); |
| 699 { |
| 700 sql::Statement s(db().GetUniqueStatement("SELECT COUNT(*) FROM x")); |
| 701 ASSERT_FALSE(s.is_valid()); |
| 702 ASSERT_FALSE(s.Step()); |
| 703 } |
| 704 |
| 705 // The existing statement has become invalid. |
| 706 ASSERT_FALSE(valid_statement.is_valid()); |
| 707 ASSERT_FALSE(valid_statement.Step()); |
| 708 } |
| 709 |
| 710 // Test attaching and detaching databases from the connection. |
| 711 TEST_F(SQLConnectionTest, Attach) { |
| 712 EXPECT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); |
| 713 |
| 714 // Create a database to attach to. |
| 715 base::FilePath attach_path = |
| 716 db_path().DirName().AppendASCII("SQLConnectionAttach.db"); |
| 717 const char kAttachmentPoint[] = "other"; |
| 718 { |
| 719 sql::Connection other_db; |
| 720 ASSERT_TRUE(other_db.Open(attach_path)); |
| 721 EXPECT_TRUE(other_db.Execute("CREATE TABLE bar (a, b)")); |
| 722 EXPECT_TRUE(other_db.Execute("INSERT INTO bar VALUES ('hello', 'world')")); |
| 723 } |
| 724 |
| 725 // Cannot see the attached database, yet. |
| 726 EXPECT_FALSE(db().IsSQLValid("SELECT count(*) from other.bar")); |
| 727 |
| 728 // Attach fails in a transaction. |
| 729 EXPECT_TRUE(db().BeginTransaction()); |
| 730 { |
| 731 sql::ScopedErrorIgnorer ignore_errors; |
| 732 ignore_errors.IgnoreError(SQLITE_ERROR); |
| 733 EXPECT_FALSE(db().AttachDatabase(attach_path, kAttachmentPoint)); |
| 734 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); |
| 735 } |
| 736 |
| 737 // Attach succeeds when the transaction is closed. |
| 738 db().RollbackTransaction(); |
| 739 EXPECT_TRUE(db().AttachDatabase(attach_path, kAttachmentPoint)); |
| 740 EXPECT_TRUE(db().IsSQLValid("SELECT count(*) from other.bar")); |
| 741 |
| 742 // Queries can touch both databases. |
| 743 EXPECT_TRUE(db().Execute("INSERT INTO foo SELECT a, b FROM other.bar")); |
| 744 { |
| 745 sql::Statement s(db().GetUniqueStatement("SELECT COUNT(*) FROM foo")); |
| 746 ASSERT_TRUE(s.Step()); |
| 747 EXPECT_EQ(1, s.ColumnInt(0)); |
| 748 } |
| 749 |
| 750 // Detach also fails in a transaction. |
| 751 EXPECT_TRUE(db().BeginTransaction()); |
| 752 { |
| 753 sql::ScopedErrorIgnorer ignore_errors; |
| 754 ignore_errors.IgnoreError(SQLITE_ERROR); |
| 755 EXPECT_FALSE(db().DetachDatabase(kAttachmentPoint)); |
| 756 EXPECT_TRUE(db().IsSQLValid("SELECT count(*) from other.bar")); |
| 757 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); |
| 758 } |
| 759 |
| 760 // Detach succeeds outside of a transaction. |
| 761 db().RollbackTransaction(); |
| 762 EXPECT_TRUE(db().DetachDatabase(kAttachmentPoint)); |
| 763 |
| 764 EXPECT_FALSE(db().IsSQLValid("SELECT count(*) from other.bar")); |
| 765 } |
| 766 |
674 } // namespace | 767 } // namespace |
OLD | NEW |