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

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: 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 class StatementErrorHandler : public sql::ErrorDelegate { 14 class StatementErrorHandler : public sql::ErrorDelegate {
15 public: 15 public:
16 StatementErrorHandler() : error_(SQLITE_OK) {} 16 StatementErrorHandler(int* error, std::string* sql_text)
17 : error_(error),
18 sql_text_(sql_text) {}
19
20 virtual ~StatementErrorHandler() {}
17 21
18 virtual int OnError(int error, sql::Connection* connection, 22 virtual int OnError(int error, sql::Connection* connection,
19 sql::Statement* stmt) OVERRIDE { 23 sql::Statement* stmt) OVERRIDE {
20 error_ = error; 24 *error_ = error;
21 const char* sql_txt = stmt ? stmt->GetSQLStatement() : NULL; 25 const char* sql_txt = stmt ? stmt->GetSQLStatement() : NULL;
22 sql_text_ = sql_txt ? sql_txt : "no statement available"; 26 *sql_text_ = sql_txt ? sql_txt : "no statement available";
23 return error; 27 return error;
24 } 28 }
25 29
26 int error() const { return error_; } 30 private:
31 int* error_;
32 std::string* sql_text_;
27 33
28 void reset_error() { 34 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 }; 35 };
42 36
43 class SQLStatementTest : public testing::Test { 37 class SQLStatementTest : public testing::Test {
44 public: 38 public:
45 SQLStatementTest() : error_handler_(new StatementErrorHandler) {} 39 SQLStatementTest() : error_(SQLITE_OK) {}
46 40
47 void SetUp() { 41 void SetUp() {
48 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 42 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
49 ASSERT_TRUE(db_.Open(temp_dir_.path().AppendASCII("SQLStatementTest.db"))); 43 ASSERT_TRUE(db_.Open(temp_dir_.path().AppendASCII("SQLStatementTest.db")));
50 44 // The error delegate will set |error_| and |sql_text_| when any sqlite
51 // The |error_handler_| will be called if any sqlite statement operation 45 // statement operation returns an error code.
52 // returns an error code. 46 db_.set_error_delegate(new StatementErrorHandler(&error_, &sql_text_));
53 db_.set_error_delegate(error_handler_);
54 } 47 }
55 48
56 void TearDown() { 49 void TearDown() {
57 // If any error happened the original sql statement can be found in 50 // If any error happened the original sql statement can be found in
58 // error_handler_->sql_statement(). 51 // |sql_text_|.
59 EXPECT_EQ(SQLITE_OK, error_handler_->error()); 52 EXPECT_EQ(SQLITE_OK, error_);
60 db_.Close(); 53 db_.Close();
61 } 54 }
62 55
63 sql::Connection& db() { return db_; } 56 sql::Connection& db() { return db_; }
64 57
65 int sqlite_error() const { return error_handler_->error(); } 58 int sqlite_error() const { return error_; }
Scott Hess - ex-Googler 2012/10/16 22:32:25 Either sqlite_error() or get_sqlite_error() both h
66 void reset_error() const { error_handler_->reset_error(); } 59
60 void ResetError() {
61 error_ = SQLITE_OK;
62 sql_text_.clear();
63 }
67 64
68 private: 65 private:
69 ScopedTempDir temp_dir_; 66 ScopedTempDir temp_dir_;
70 sql::Connection db_; 67 sql::Connection db_;
71 scoped_refptr<StatementErrorHandler> error_handler_; 68
69 // The error code of the most recent error.
70 int error_;
71 // Original statement which caused the error.
72 std::string sql_text_;
72 }; 73 };
73 74
74 TEST_F(SQLStatementTest, Assign) { 75 TEST_F(SQLStatementTest, Assign) {
75 sql::Statement s; 76 sql::Statement s;
76 EXPECT_FALSE(s.is_valid()); 77 EXPECT_FALSE(s.is_valid());
77 78
78 s.Assign(db().GetUniqueStatement("CREATE TABLE foo (a, b)")); 79 s.Assign(db().GetUniqueStatement("CREATE TABLE foo (a, b)"));
79 EXPECT_TRUE(s.is_valid()); 80 EXPECT_TRUE(s.is_valid());
80 } 81 }
81 82
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a INTEGER PRIMARY KEY, b)")); 115 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a INTEGER PRIMARY KEY, b)"));
115 EXPECT_EQ(SQLITE_OK, sqlite_error()); 116 EXPECT_EQ(SQLITE_OK, sqlite_error());
116 // Insert in the foo table the primary key. It is an error to insert 117 // 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 118 // something other than an number. This error causes the error callback
118 // handler to be called with SQLITE_MISMATCH as error code. 119 // handler to be called with SQLITE_MISMATCH as error code.
119 sql::Statement s(db().GetUniqueStatement("INSERT INTO foo (a) VALUES (?)")); 120 sql::Statement s(db().GetUniqueStatement("INSERT INTO foo (a) VALUES (?)"));
120 EXPECT_TRUE(s.is_valid()); 121 EXPECT_TRUE(s.is_valid());
121 s.BindCString(0, "bad bad"); 122 s.BindCString(0, "bad bad");
122 EXPECT_FALSE(s.Run()); 123 EXPECT_FALSE(s.Run());
123 EXPECT_EQ(SQLITE_MISMATCH, sqlite_error()); 124 EXPECT_EQ(SQLITE_MISMATCH, sqlite_error());
124 reset_error(); 125 ResetError();
Scott Hess - ex-Googler 2012/10/16 22:32:25 This shouldn't be necessary unless SQLStatementTes
pkotwicz 2012/10/17 13:39:04 This is actually still necessary for EXPECT_EQ(SQL
Scott Hess - ex-Googler 2012/10/17 14:53:49 Doh - makes sense. If all the sql:: calls are che
125 } 126 }
126 127
127 TEST_F(SQLStatementTest, Reset) { 128 TEST_F(SQLStatementTest, Reset) {
128 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); 129 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
129 ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (3, 12)")); 130 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)")); 131 ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (4, 13)"));
131 132
132 sql::Statement s(db().GetUniqueStatement( 133 sql::Statement s(db().GetUniqueStatement(
133 "SELECT b FROM foo WHERE a = ? ")); 134 "SELECT b FROM foo WHERE a = ? "));
134 s.BindInt(0, 3); 135 s.BindInt(0, 3);
135 ASSERT_TRUE(s.Step()); 136 ASSERT_TRUE(s.Step());
136 EXPECT_EQ(12, s.ColumnInt(0)); 137 EXPECT_EQ(12, s.ColumnInt(0));
137 ASSERT_FALSE(s.Step()); 138 ASSERT_FALSE(s.Step());
138 139
139 s.Reset(false); 140 s.Reset(false);
140 // Verify that we can get all rows again. 141 // Verify that we can get all rows again.
141 ASSERT_TRUE(s.Step()); 142 ASSERT_TRUE(s.Step());
142 EXPECT_EQ(12, s.ColumnInt(0)); 143 EXPECT_EQ(12, s.ColumnInt(0));
143 EXPECT_FALSE(s.Step()); 144 EXPECT_FALSE(s.Step());
144 145
145 s.Reset(true); 146 s.Reset(true);
146 ASSERT_FALSE(s.Step()); 147 ASSERT_FALSE(s.Step());
147 } 148 }
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