OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "sql/connection.h" | 10 #include "sql/connection.h" |
(...skipping 30 matching lines...) Expand all Loading... |
41 // Dump consistent human-readable representation of the database | 41 // Dump consistent human-readable representation of the database |
42 // schema. For tables or indices, this will contain the sql command | 42 // schema. For tables or indices, this will contain the sql command |
43 // to create the table or index. For certain automatic SQLite | 43 // to create the table or index. For certain automatic SQLite |
44 // structures with no sql, the name is used. | 44 // structures with no sql, the name is used. |
45 std::string GetSchema(sql::Connection* db) { | 45 std::string GetSchema(sql::Connection* db) { |
46 const char kSql[] = | 46 const char kSql[] = |
47 "SELECT COALESCE(sql, name) FROM sqlite_master ORDER BY 1"; | 47 "SELECT COALESCE(sql, name) FROM sqlite_master ORDER BY 1"; |
48 return ExecuteWithResults(db, kSql, "|", "\n"); | 48 return ExecuteWithResults(db, kSql, "|", "\n"); |
49 } | 49 } |
50 | 50 |
| 51 #if !defined(USE_SYSTEM_SQLITE) |
51 int GetPageSize(sql::Connection* db) { | 52 int GetPageSize(sql::Connection* db) { |
52 sql::Statement s(db->GetUniqueStatement("PRAGMA page_size")); | 53 sql::Statement s(db->GetUniqueStatement("PRAGMA page_size")); |
53 EXPECT_TRUE(s.Step()); | 54 EXPECT_TRUE(s.Step()); |
54 return s.ColumnInt(0); | 55 return s.ColumnInt(0); |
55 } | 56 } |
56 | 57 |
57 // Get |name|'s root page number in the database. | 58 // Get |name|'s root page number in the database. |
58 int GetRootPage(sql::Connection* db, const char* name) { | 59 int GetRootPage(sql::Connection* db, const char* name) { |
59 const char kPageSql[] = "SELECT rootpage FROM sqlite_master WHERE name = ?"; | 60 const char kPageSql[] = "SELECT rootpage FROM sqlite_master WHERE name = ?"; |
60 sql::Statement s(db->GetUniqueStatement(kPageSql)); | 61 sql::Statement s(db->GetUniqueStatement(kPageSql)); |
(...skipping 22 matching lines...) Expand all Loading... |
83 const char* buf, size_t page_size) { | 84 const char* buf, size_t page_size) { |
84 file_util::ScopedFILE file(file_util::OpenFile(path, "rb+")); | 85 file_util::ScopedFILE file(file_util::OpenFile(path, "rb+")); |
85 if (!file.get()) | 86 if (!file.get()) |
86 return false; | 87 return false; |
87 if (0 != fseek(file.get(), (page_no - 1) * page_size, SEEK_SET)) | 88 if (0 != fseek(file.get(), (page_no - 1) * page_size, SEEK_SET)) |
88 return false; | 89 return false; |
89 if (1u != fwrite(buf, page_size, 1, file.get())) | 90 if (1u != fwrite(buf, page_size, 1, file.get())) |
90 return false; | 91 return false; |
91 return true; | 92 return true; |
92 } | 93 } |
| 94 #endif // !defined(USE_SYSTEM_SQLITE) |
93 | 95 |
94 class SQLRecoveryTest : public testing::Test { | 96 class SQLRecoveryTest : public testing::Test { |
95 public: | 97 public: |
96 SQLRecoveryTest() {} | 98 SQLRecoveryTest() {} |
97 | 99 |
98 virtual void SetUp() { | 100 virtual void SetUp() { |
99 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 101 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
100 ASSERT_TRUE(db_.Open(db_path())); | 102 ASSERT_TRUE(db_.Open(db_path())); |
101 } | 103 } |
102 | 104 |
(...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
416 EXPECT_EQ("10", ExecuteWithResults(&db(), kCountSql, "|", ",")); | 418 EXPECT_EQ("10", ExecuteWithResults(&db(), kCountSql, "|", ",")); |
417 EXPECT_EQ("10", ExecuteWithResults(&db(), kDistinctSql, "|", ",")); | 419 EXPECT_EQ("10", ExecuteWithResults(&db(), kDistinctSql, "|", ",")); |
418 | 420 |
419 // The expected value was retained. | 421 // The expected value was retained. |
420 const char kSelectSql[] = "SELECT v FROM x WHERE id = 0"; | 422 const char kSelectSql[] = "SELECT v FROM x WHERE id = 0"; |
421 EXPECT_EQ("100", ExecuteWithResults(&db(), kSelectSql, "|", ",")); | 423 EXPECT_EQ("100", ExecuteWithResults(&db(), kSelectSql, "|", ",")); |
422 } | 424 } |
423 #endif // !defined(USE_SYSTEM_SQLITE) | 425 #endif // !defined(USE_SYSTEM_SQLITE) |
424 | 426 |
425 } // namespace | 427 } // namespace |
OLD | NEW |