summaryrefslogtreecommitdiffstats
path: root/sql
diff options
context:
space:
mode:
Diffstat (limited to 'sql')
-rw-r--r--sql/connection_unittest.cc4
-rw-r--r--sql/statement.cc9
-rw-r--r--sql/statement.h6
-rw-r--r--sql/statement_unittest.cc28
4 files changed, 35 insertions, 12 deletions
diff --git a/sql/connection_unittest.cc b/sql/connection_unittest.cc
index 96cc25b..97dd152 100644
--- a/sql/connection_unittest.cc
+++ b/sql/connection_unittest.cc
@@ -195,7 +195,7 @@ TEST_F(SQLConnectionTest, RazePageSize) {
// After raze, page_size should still match the indicated value.
ASSERT_TRUE(db().Raze());
- s.Reset();
+ s.Reset(true);
ASSERT_TRUE(s.Step());
ASSERT_EQ(kPageSize, s.ColumnInt(0));
}
@@ -218,7 +218,7 @@ TEST_F(SQLConnectionTest, RazeMultiple) {
ASSERT_TRUE(db().Raze());
// The second connection sees the updated database.
- s.Reset();
+ s.Reset(true);
ASSERT_TRUE(s.Step());
ASSERT_EQ(0, s.ColumnInt(0));
}
diff --git a/sql/statement.cc b/sql/statement.cc
index 626c15b..3616dcc 100644
--- a/sql/statement.cc
+++ b/sql/statement.cc
@@ -28,11 +28,11 @@ Statement::~Statement() {
// Free the resources associated with this statement. We assume there's only
// one statement active for a given sqlite3_stmt at any time, so this won't
// mess with anything.
- Reset();
+ Reset(true);
}
void Statement::Assign(scoped_refptr<Connection::StatementRef> ref) {
- Reset();
+ Reset(true);
ref_ = ref;
}
@@ -61,12 +61,13 @@ bool Statement::Step() {
return CheckError(sqlite3_step(ref_->stmt())) == SQLITE_ROW;
}
-void Statement::Reset() {
+void Statement::Reset(bool clear_bound_vars) {
if (is_valid()) {
// We don't call CheckError() here because sqlite3_reset() returns
// the last error that Step() caused thereby generating a second
// spurious error callback.
- sqlite3_clear_bindings(ref_->stmt());
+ if (clear_bound_vars)
+ sqlite3_clear_bindings(ref_->stmt());
sqlite3_reset(ref_->stmt());
}
diff --git a/sql/statement.h b/sql/statement.h
index 92d1ef2..269684f 100644
--- a/sql/statement.h
+++ b/sql/statement.h
@@ -87,9 +87,9 @@ class SQL_EXPORT Statement {
// return s.Succeeded();
bool Step();
- // Resets the statement to its initial condition. This includes clearing all
- // the bound variables and any current result row.
- void Reset();
+ // Resets the statement to its initial condition. This includes any current
+ // result row, and also the bound variables if the |clear_bound_vars| is true.
+ void Reset(bool clear_bound_vars);
// Returns true if the last executed thing in this statement succeeded. If
// there was no last executed thing or the statement is invalid, this will
diff --git a/sql/statement_unittest.cc b/sql/statement_unittest.cc
index b6b6aa8..0028b9d 100644
--- a/sql/statement_unittest.cc
+++ b/sql/statement_unittest.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -88,14 +88,14 @@ TEST_F(SQLStatementTest, Run) {
// Run should fail since this produces output, and we should use Step(). This
// gets a bit wonky since sqlite says this is OK so succeeded is set.
- s.Reset();
+ s.Reset(true);
s.BindInt(0, 3);
EXPECT_FALSE(s.Run());
EXPECT_EQ(SQLITE_ROW, db().GetErrorCode());
EXPECT_TRUE(s.Succeeded());
// Resetting it should put it back to the previous state (not runnable).
- s.Reset();
+ s.Reset(true);
EXPECT_FALSE(s.Succeeded());
// Binding and stepping should produce one row.
@@ -120,3 +120,25 @@ TEST_F(SQLStatementTest, BasicErrorCallback) {
EXPECT_EQ(SQLITE_MISMATCH, sqlite_error());
reset_error();
}
+
+TEST_F(SQLStatementTest, Reset) {
+ ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
+ ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (3, 12)"));
+ ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (4, 13)"));
+
+ sql::Statement s(db().GetUniqueStatement(
+ "SELECT b FROM foo WHERE a = ? "));
+ s.BindInt(0, 3);
+ ASSERT_TRUE(s.Step());
+ EXPECT_EQ(12, s.ColumnInt(0));
+ ASSERT_FALSE(s.Step());
+
+ s.Reset(false);
+ // Verify that we can get all rows again.
+ ASSERT_TRUE(s.Step());
+ EXPECT_EQ(12, s.ColumnInt(0));
+ EXPECT_FALSE(s.Step());
+
+ s.Reset(true);
+ ASSERT_FALSE(s.Step());
+}