Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(97)

Side by Side Diff: sql/statement_unittest.cc

Issue 11111021: Remove ref counting on sql::ErrorDelegate (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Changes as requested Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « sql/sqlite_features_unittest.cc ('k') | webkit/appcache/appcache_database_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/file_util.h" 7 #include "base/file_util.h"
8 #include "base/scoped_temp_dir.h" 8 #include "base/scoped_temp_dir.h"
9 #include "sql/connection.h" 9 #include "sql/connection.h"
10 #include "sql/statement.h" 10 #include "sql/statement.h"
11 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "third_party/sqlite/sqlite3.h" 12 #include "third_party/sqlite/sqlite3.h"
13 13
14 namespace {
15
14 class StatementErrorHandler : public sql::ErrorDelegate { 16 class StatementErrorHandler : public sql::ErrorDelegate {
15 public: 17 public:
16 StatementErrorHandler() : error_(SQLITE_OK) {} 18 StatementErrorHandler(int* error, std::string* sql_text)
19 : error_(error),
20 sql_text_(sql_text) {}
21
22 virtual ~StatementErrorHandler() {}
17 23
18 virtual int OnError(int error, sql::Connection* connection, 24 virtual int OnError(int error, sql::Connection* connection,
19 sql::Statement* stmt) OVERRIDE { 25 sql::Statement* stmt) OVERRIDE {
20 error_ = error; 26 *error_ = error;
21 const char* sql_txt = stmt ? stmt->GetSQLStatement() : NULL; 27 const char* sql_txt = stmt ? stmt->GetSQLStatement() : NULL;
22 sql_text_ = sql_txt ? sql_txt : "no statement available"; 28 *sql_text_ = sql_txt ? sql_txt : "no statement available";
23 return error; 29 return error;
24 } 30 }
25 31
26 int error() const { return error_; } 32 private:
33 int* error_;
34 std::string* sql_text_;
27 35
28 void reset_error() { 36 DISALLOW_COPY_AND_ASSIGN(StatementErrorHandler);
29 sql_text_.clear();
30 error_ = SQLITE_OK;
31 }
32
33 const char* sql_statement() const { return sql_text_.c_str(); }
34
35 protected:
36 virtual ~StatementErrorHandler() {}
37
38 private:
39 int error_;
40 std::string sql_text_;
41 }; 37 };
42 38
43 class SQLStatementTest : public testing::Test { 39 class SQLStatementTest : public testing::Test {
44 public: 40 public:
45 SQLStatementTest() : error_handler_(new StatementErrorHandler) {} 41 SQLStatementTest() : error_(SQLITE_OK) {}
46 42
47 void SetUp() { 43 void SetUp() {
48 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 44 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
49 ASSERT_TRUE(db_.Open(temp_dir_.path().AppendASCII("SQLStatementTest.db"))); 45 ASSERT_TRUE(db_.Open(temp_dir_.path().AppendASCII("SQLStatementTest.db")));
50 46 // The error delegate will set |error_| and |sql_text_| when any sqlite
51 // The |error_handler_| will be called if any sqlite statement operation 47 // statement operation returns an error code.
52 // returns an error code. 48 db_.set_error_delegate(new StatementErrorHandler(&error_, &sql_text_));
53 db_.set_error_delegate(error_handler_);
54 } 49 }
55 50
56 void TearDown() { 51 void TearDown() {
57 // If any error happened the original sql statement can be found in 52 // If any error happened the original sql statement can be found in
58 // error_handler_->sql_statement(). 53 // |sql_text_|.
59 EXPECT_EQ(SQLITE_OK, error_handler_->error()); 54 EXPECT_EQ(SQLITE_OK, error_);
60 db_.Close(); 55 db_.Close();
61 } 56 }
62 57
63 sql::Connection& db() { return db_; } 58 sql::Connection& db() { return db_; }
64 59
65 int sqlite_error() const { return error_handler_->error(); } 60 int sqlite_error() const { return error_; }
66 void reset_error() const { error_handler_->reset_error(); } 61
62 void ResetError() {
63 error_ = SQLITE_OK;
64 sql_text_.clear();
65 }
67 66
68 private: 67 private:
69 ScopedTempDir temp_dir_; 68 ScopedTempDir temp_dir_;
70 sql::Connection db_; 69 sql::Connection db_;
71 scoped_refptr<StatementErrorHandler> error_handler_; 70
71 // The error code of the most recent error.
72 int error_;
73 // Original statement which caused the error.
74 std::string sql_text_;
72 }; 75 };
73 76
77 } // namespace
78
74 TEST_F(SQLStatementTest, Assign) { 79 TEST_F(SQLStatementTest, Assign) {
75 sql::Statement s; 80 sql::Statement s;
76 EXPECT_FALSE(s.is_valid()); 81 EXPECT_FALSE(s.is_valid());
77 82
78 s.Assign(db().GetUniqueStatement("CREATE TABLE foo (a, b)")); 83 s.Assign(db().GetUniqueStatement("CREATE TABLE foo (a, b)"));
79 EXPECT_TRUE(s.is_valid()); 84 EXPECT_TRUE(s.is_valid());
80 } 85 }
81 86
82 TEST_F(SQLStatementTest, Run) { 87 TEST_F(SQLStatementTest, Run) {
83 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); 88 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
(...skipping 30 matching lines...) Expand all
114 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a INTEGER PRIMARY KEY, b)")); 119 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a INTEGER PRIMARY KEY, b)"));
115 EXPECT_EQ(SQLITE_OK, sqlite_error()); 120 EXPECT_EQ(SQLITE_OK, sqlite_error());
116 // Insert in the foo table the primary key. It is an error to insert 121 // Insert in the foo table the primary key. It is an error to insert
117 // something other than an number. This error causes the error callback 122 // something other than an number. This error causes the error callback
118 // handler to be called with SQLITE_MISMATCH as error code. 123 // handler to be called with SQLITE_MISMATCH as error code.
119 sql::Statement s(db().GetUniqueStatement("INSERT INTO foo (a) VALUES (?)")); 124 sql::Statement s(db().GetUniqueStatement("INSERT INTO foo (a) VALUES (?)"));
120 EXPECT_TRUE(s.is_valid()); 125 EXPECT_TRUE(s.is_valid());
121 s.BindCString(0, "bad bad"); 126 s.BindCString(0, "bad bad");
122 EXPECT_FALSE(s.Run()); 127 EXPECT_FALSE(s.Run());
123 EXPECT_EQ(SQLITE_MISMATCH, sqlite_error()); 128 EXPECT_EQ(SQLITE_MISMATCH, sqlite_error());
124 reset_error(); 129 ResetError();
125 } 130 }
126 131
127 TEST_F(SQLStatementTest, Reset) { 132 TEST_F(SQLStatementTest, Reset) {
128 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); 133 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
129 ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (3, 12)")); 134 ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (3, 12)"));
130 ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (4, 13)")); 135 ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (4, 13)"));
131 136
132 sql::Statement s(db().GetUniqueStatement( 137 sql::Statement s(db().GetUniqueStatement(
133 "SELECT b FROM foo WHERE a = ? ")); 138 "SELECT b FROM foo WHERE a = ? "));
134 s.BindInt(0, 3); 139 s.BindInt(0, 3);
135 ASSERT_TRUE(s.Step()); 140 ASSERT_TRUE(s.Step());
136 EXPECT_EQ(12, s.ColumnInt(0)); 141 EXPECT_EQ(12, s.ColumnInt(0));
137 ASSERT_FALSE(s.Step()); 142 ASSERT_FALSE(s.Step());
138 143
139 s.Reset(false); 144 s.Reset(false);
140 // Verify that we can get all rows again. 145 // Verify that we can get all rows again.
141 ASSERT_TRUE(s.Step()); 146 ASSERT_TRUE(s.Step());
142 EXPECT_EQ(12, s.ColumnInt(0)); 147 EXPECT_EQ(12, s.ColumnInt(0));
143 EXPECT_FALSE(s.Step()); 148 EXPECT_FALSE(s.Step());
144 149
145 s.Reset(true); 150 s.Reset(true);
146 ASSERT_FALSE(s.Step()); 151 ASSERT_FALSE(s.Step());
147 } 152 }
OLDNEW
« no previous file with comments | « sql/sqlite_features_unittest.cc ('k') | webkit/appcache/appcache_database_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698