[email protected] | 389e0a4 | 2012-04-25 21:36:41 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | faa604e | 2009-09-25 22:38:59 | [diff] [blame] | 5 | #include <string> |
| 6 | |
[email protected] | 526b466 | 2013-06-14 04:09:12 | [diff] [blame] | 7 | #include "base/bind.h" |
thestig | 22dfc401 | 2014-09-05 08:29:44 | [diff] [blame] | 8 | #include "base/files/file_util.h" |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 9 | #include "base/files/scoped_temp_dir.h" |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 10 | #include "sql/database.h" |
[email protected] | f0a54b2 | 2011-07-19 18:40:21 | [diff] [blame] | 11 | #include "sql/statement.h" |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 12 | #include "sql/test/error_callback_support.h" |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 13 | #include "sql/test/scoped_error_expecter.h" |
Scott Graham | 47ed2c3 | 2017-09-15 02:17:07 | [diff] [blame] | 14 | #include "sql/test/sql_test_base.h" |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 15 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | e33cba4 | 2010-08-18 23:37:03 | [diff] [blame] | 16 | #include "third_party/sqlite/sqlite3.h" |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 17 | |
[email protected] | 49dc4f2 | 2012-10-17 17:41:16 | [diff] [blame] | 18 | namespace { |
| 19 | |
erg | 102ceb41 | 2015-06-20 01:38:13 | [diff] [blame] | 20 | using SQLStatementTest = sql::SQLTestBase; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 21 | |
[email protected] | 49dc4f2 | 2012-10-17 17:41:16 | [diff] [blame] | 22 | } // namespace |
| 23 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 24 | TEST_F(SQLStatementTest, Assign) { |
| 25 | sql::Statement s; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 26 | EXPECT_FALSE(s.is_valid()); |
| 27 | |
| 28 | s.Assign(db().GetUniqueStatement("CREATE TABLE foo (a, b)")); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 29 | EXPECT_TRUE(s.is_valid()); |
| 30 | } |
| 31 | |
| 32 | TEST_F(SQLStatementTest, Run) { |
| 33 | ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); |
| 34 | ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (3, 12)")); |
| 35 | |
| 36 | sql::Statement s(db().GetUniqueStatement("SELECT b FROM foo WHERE a=?")); |
| 37 | EXPECT_FALSE(s.Succeeded()); |
| 38 | |
| 39 | // Stepping it won't work since we haven't bound the value. |
| 40 | EXPECT_FALSE(s.Step()); |
| 41 | |
| 42 | // Run should fail since this produces output, and we should use Step(). This |
| 43 | // gets a bit wonky since sqlite says this is OK so succeeded is set. |
[email protected] | 389e0a4 | 2012-04-25 21:36:41 | [diff] [blame] | 44 | s.Reset(true); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 45 | s.BindInt(0, 3); |
| 46 | EXPECT_FALSE(s.Run()); |
| 47 | EXPECT_EQ(SQLITE_ROW, db().GetErrorCode()); |
| 48 | EXPECT_TRUE(s.Succeeded()); |
| 49 | |
| 50 | // Resetting it should put it back to the previous state (not runnable). |
[email protected] | 389e0a4 | 2012-04-25 21:36:41 | [diff] [blame] | 51 | s.Reset(true); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 52 | EXPECT_FALSE(s.Succeeded()); |
| 53 | |
| 54 | // Binding and stepping should produce one row. |
| 55 | s.BindInt(0, 3); |
| 56 | EXPECT_TRUE(s.Step()); |
| 57 | EXPECT_TRUE(s.Succeeded()); |
| 58 | EXPECT_EQ(12, s.ColumnInt(0)); |
| 59 | EXPECT_FALSE(s.Step()); |
| 60 | EXPECT_TRUE(s.Succeeded()); |
| 61 | } |
[email protected] | faa604e | 2009-09-25 22:38:59 | [diff] [blame] | 62 | |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 63 | // Error callback called for error running a statement. |
| 64 | TEST_F(SQLStatementTest, ErrorCallback) { |
[email protected] | faa604e | 2009-09-25 22:38:59 | [diff] [blame] | 65 | ASSERT_TRUE(db().Execute("CREATE TABLE foo (a INTEGER PRIMARY KEY, b)")); |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 66 | |
| 67 | int error = SQLITE_OK; |
| 68 | sql::ScopedErrorCallback sec( |
tzik | d16d219 | 2018-03-07 08:58:36 | [diff] [blame] | 69 | &db(), base::BindRepeating(&sql::CaptureErrorCallback, &error)); |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 70 | |
[email protected] | faa604e | 2009-09-25 22:38:59 | [diff] [blame] | 71 | // Insert in the foo table the primary key. It is an error to insert |
| 72 | // something other than an number. This error causes the error callback |
| 73 | // handler to be called with SQLITE_MISMATCH as error code. |
| 74 | sql::Statement s(db().GetUniqueStatement("INSERT INTO foo (a) VALUES (?)")); |
| 75 | EXPECT_TRUE(s.is_valid()); |
| 76 | s.BindCString(0, "bad bad"); |
| 77 | EXPECT_FALSE(s.Run()); |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 78 | EXPECT_EQ(SQLITE_MISMATCH, error); |
| 79 | } |
| 80 | |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 81 | // Error expecter works for error running a statement. |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 82 | TEST_F(SQLStatementTest, ScopedIgnoreError) { |
| 83 | ASSERT_TRUE(db().Execute("CREATE TABLE foo (a INTEGER PRIMARY KEY, b)")); |
| 84 | |
| 85 | sql::Statement s(db().GetUniqueStatement("INSERT INTO foo (a) VALUES (?)")); |
| 86 | EXPECT_TRUE(s.is_valid()); |
| 87 | |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 88 | { |
| 89 | sql::test::ScopedErrorExpecter expecter; |
| 90 | expecter.ExpectError(SQLITE_MISMATCH); |
| 91 | s.BindCString(0, "bad bad"); |
| 92 | ASSERT_FALSE(s.Run()); |
| 93 | ASSERT_TRUE(expecter.SawExpectedErrors()); |
| 94 | } |
[email protected] | faa604e | 2009-09-25 22:38:59 | [diff] [blame] | 95 | } |
[email protected] | 389e0a4 | 2012-04-25 21:36:41 | [diff] [blame] | 96 | |
| 97 | TEST_F(SQLStatementTest, Reset) { |
| 98 | ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); |
| 99 | ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (3, 12)")); |
| 100 | ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (4, 13)")); |
| 101 | |
| 102 | sql::Statement s(db().GetUniqueStatement( |
| 103 | "SELECT b FROM foo WHERE a = ? ")); |
| 104 | s.BindInt(0, 3); |
| 105 | ASSERT_TRUE(s.Step()); |
| 106 | EXPECT_EQ(12, s.ColumnInt(0)); |
| 107 | ASSERT_FALSE(s.Step()); |
| 108 | |
| 109 | s.Reset(false); |
| 110 | // Verify that we can get all rows again. |
| 111 | ASSERT_TRUE(s.Step()); |
| 112 | EXPECT_EQ(12, s.ColumnInt(0)); |
| 113 | EXPECT_FALSE(s.Step()); |
| 114 | |
| 115 | s.Reset(true); |
| 116 | ASSERT_FALSE(s.Step()); |
| 117 | } |