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 <string> | 5 #include <string> |
6 | 6 |
| 7 #include "base/bind.h" |
7 #include "base/file_util.h" | 8 #include "base/file_util.h" |
8 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
9 #include "sql/connection.h" | 10 #include "sql/connection.h" |
10 #include "sql/statement.h" | 11 #include "sql/statement.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
12 #include "third_party/sqlite/sqlite3.h" | 13 #include "third_party/sqlite/sqlite3.h" |
13 | 14 |
14 namespace { | 15 namespace { |
15 | 16 |
16 class StatementErrorHandler : public sql::ErrorDelegate { | 17 void CaptureErrorCallback(int* error_pointer, std::string* sql_text, |
17 public: | 18 int error, sql::Statement* stmt) { |
18 StatementErrorHandler(int* error, std::string* sql_text) | 19 *error_pointer = error; |
19 : error_(error), | 20 const char* text = stmt ? stmt->GetSQLStatement() : NULL; |
20 sql_text_(sql_text) {} | 21 *sql_text = text ? text : "no statement available"; |
21 | 22 } |
22 virtual ~StatementErrorHandler() {} | |
23 | |
24 virtual int OnError(int error, sql::Connection* connection, | |
25 sql::Statement* stmt) OVERRIDE { | |
26 *error_ = error; | |
27 const char* sql_txt = stmt ? stmt->GetSQLStatement() : NULL; | |
28 *sql_text_ = sql_txt ? sql_txt : "no statement available"; | |
29 return error; | |
30 } | |
31 | |
32 private: | |
33 int* error_; | |
34 std::string* sql_text_; | |
35 | |
36 DISALLOW_COPY_AND_ASSIGN(StatementErrorHandler); | |
37 }; | |
38 | 23 |
39 class SQLStatementTest : public testing::Test { | 24 class SQLStatementTest : public testing::Test { |
40 public: | 25 public: |
41 SQLStatementTest() : error_(SQLITE_OK) {} | 26 SQLStatementTest() : error_(SQLITE_OK) {} |
42 | 27 |
43 virtual void SetUp() { | 28 virtual void SetUp() { |
44 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 29 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
45 ASSERT_TRUE(db_.Open(temp_dir_.path().AppendASCII("SQLStatementTest.db"))); | 30 ASSERT_TRUE(db_.Open(temp_dir_.path().AppendASCII("SQLStatementTest.db"))); |
46 // The error delegate will set |error_| and |sql_text_| when any sqlite | 31 // The error delegate will set |error_| and |sql_text_| when any sqlite |
47 // statement operation returns an error code. | 32 // statement operation returns an error code. |
48 db_.set_error_delegate(new StatementErrorHandler(&error_, &sql_text_)); | 33 db_.set_error_callback(base::Bind(&CaptureErrorCallback, |
| 34 &error_, &sql_text_)); |
49 } | 35 } |
50 | 36 |
51 virtual void TearDown() { | 37 virtual void TearDown() { |
52 // If any error happened the original sql statement can be found in | 38 // If any error happened the original sql statement can be found in |
53 // |sql_text_|. | 39 // |sql_text_|. |
54 EXPECT_EQ(SQLITE_OK, error_); | 40 EXPECT_EQ(SQLITE_OK, error_); |
55 db_.Close(); | 41 db_.Close(); |
56 } | 42 } |
57 | 43 |
58 sql::Connection& db() { return db_; } | 44 sql::Connection& db() { return db_; } |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 | 129 |
144 s.Reset(false); | 130 s.Reset(false); |
145 // Verify that we can get all rows again. | 131 // Verify that we can get all rows again. |
146 ASSERT_TRUE(s.Step()); | 132 ASSERT_TRUE(s.Step()); |
147 EXPECT_EQ(12, s.ColumnInt(0)); | 133 EXPECT_EQ(12, s.ColumnInt(0)); |
148 EXPECT_FALSE(s.Step()); | 134 EXPECT_FALSE(s.Step()); |
149 | 135 |
150 s.Reset(true); | 136 s.Reset(true); |
151 ASSERT_FALSE(s.Step()); | 137 ASSERT_FALSE(s.Step()); |
152 } | 138 } |
OLD | NEW |