OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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" |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (3, 12)")); | 81 ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (3, 12)")); |
82 | 82 |
83 sql::Statement s(db().GetUniqueStatement("SELECT b FROM foo WHERE a=?")); | 83 sql::Statement s(db().GetUniqueStatement("SELECT b FROM foo WHERE a=?")); |
84 EXPECT_FALSE(s.Succeeded()); | 84 EXPECT_FALSE(s.Succeeded()); |
85 | 85 |
86 // Stepping it won't work since we haven't bound the value. | 86 // Stepping it won't work since we haven't bound the value. |
87 EXPECT_FALSE(s.Step()); | 87 EXPECT_FALSE(s.Step()); |
88 | 88 |
89 // Run should fail since this produces output, and we should use Step(). This | 89 // Run should fail since this produces output, and we should use Step(). This |
90 // gets a bit wonky since sqlite says this is OK so succeeded is set. | 90 // gets a bit wonky since sqlite says this is OK so succeeded is set. |
91 s.Reset(); | 91 s.Reset(true); |
92 s.BindInt(0, 3); | 92 s.BindInt(0, 3); |
93 EXPECT_FALSE(s.Run()); | 93 EXPECT_FALSE(s.Run()); |
94 EXPECT_EQ(SQLITE_ROW, db().GetErrorCode()); | 94 EXPECT_EQ(SQLITE_ROW, db().GetErrorCode()); |
95 EXPECT_TRUE(s.Succeeded()); | 95 EXPECT_TRUE(s.Succeeded()); |
96 | 96 |
97 // Resetting it should put it back to the previous state (not runnable). | 97 // Resetting it should put it back to the previous state (not runnable). |
98 s.Reset(); | 98 s.Reset(true); |
99 EXPECT_FALSE(s.Succeeded()); | 99 EXPECT_FALSE(s.Succeeded()); |
100 | 100 |
101 // Binding and stepping should produce one row. | 101 // Binding and stepping should produce one row. |
102 s.BindInt(0, 3); | 102 s.BindInt(0, 3); |
103 EXPECT_TRUE(s.Step()); | 103 EXPECT_TRUE(s.Step()); |
104 EXPECT_TRUE(s.Succeeded()); | 104 EXPECT_TRUE(s.Succeeded()); |
105 EXPECT_EQ(12, s.ColumnInt(0)); | 105 EXPECT_EQ(12, s.ColumnInt(0)); |
106 EXPECT_FALSE(s.Step()); | 106 EXPECT_FALSE(s.Step()); |
107 EXPECT_TRUE(s.Succeeded()); | 107 EXPECT_TRUE(s.Succeeded()); |
108 } | 108 } |
109 | 109 |
110 TEST_F(SQLStatementTest, BasicErrorCallback) { | 110 TEST_F(SQLStatementTest, BasicErrorCallback) { |
111 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a INTEGER PRIMARY KEY, b)")); | 111 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a INTEGER PRIMARY KEY, b)")); |
112 EXPECT_EQ(SQLITE_OK, sqlite_error()); | 112 EXPECT_EQ(SQLITE_OK, sqlite_error()); |
113 // Insert in the foo table the primary key. It is an error to insert | 113 // Insert in the foo table the primary key. It is an error to insert |
114 // something other than an number. This error causes the error callback | 114 // something other than an number. This error causes the error callback |
115 // handler to be called with SQLITE_MISMATCH as error code. | 115 // handler to be called with SQLITE_MISMATCH as error code. |
116 sql::Statement s(db().GetUniqueStatement("INSERT INTO foo (a) VALUES (?)")); | 116 sql::Statement s(db().GetUniqueStatement("INSERT INTO foo (a) VALUES (?)")); |
117 EXPECT_TRUE(s.is_valid()); | 117 EXPECT_TRUE(s.is_valid()); |
118 s.BindCString(0, "bad bad"); | 118 s.BindCString(0, "bad bad"); |
119 EXPECT_FALSE(s.Run()); | 119 EXPECT_FALSE(s.Run()); |
120 EXPECT_EQ(SQLITE_MISMATCH, sqlite_error()); | 120 EXPECT_EQ(SQLITE_MISMATCH, sqlite_error()); |
121 reset_error(); | 121 reset_error(); |
122 } | 122 } |
| 123 |
| 124 TEST_F(SQLStatementTest, Reset) { |
| 125 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); |
| 126 ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (3, 12)")); |
| 127 ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (4, 13)")); |
| 128 |
| 129 sql::Statement s(db().GetUniqueStatement( |
| 130 "SELECT b FROM foo WHERE a = ? ")); |
| 131 s.BindInt(0, 3); |
| 132 ASSERT_TRUE(s.Step()); |
| 133 EXPECT_EQ(12, s.ColumnInt(0)); |
| 134 ASSERT_FALSE(s.Step()); |
| 135 |
| 136 s.Reset(false); |
| 137 // Verify that we can get all rows again. |
| 138 ASSERT_TRUE(s.Step()); |
| 139 EXPECT_EQ(12, s.ColumnInt(0)); |
| 140 EXPECT_FALSE(s.Step()); |
| 141 |
| 142 s.Reset(true); |
| 143 ASSERT_FALSE(s.Step()); |
| 144 } |
OLD | NEW |