[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" |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 8 | #include "base/file_util.h" |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 9 | #include "base/files/scoped_temp_dir.h" |
[email protected] | f0a54b2 | 2011-07-19 18:40:21 | [diff] [blame] | 10 | #include "sql/connection.h" |
| 11 | #include "sql/statement.h" |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 12 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | e33cba4 | 2010-08-18 23:37:03 | [diff] [blame] | 13 | #include "third_party/sqlite/sqlite3.h" |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 14 | |
[email protected] | 49dc4f2 | 2012-10-17 17:41:16 | [diff] [blame] | 15 | namespace { |
| 16 | |
[email protected] | 526b466 | 2013-06-14 04:09:12 | [diff] [blame^] | 17 | void CaptureErrorCallback(int* error_pointer, std::string* sql_text, |
| 18 | int error, sql::Statement* stmt) { |
| 19 | *error_pointer = error; |
| 20 | const char* text = stmt ? stmt->GetSQLStatement() : NULL; |
| 21 | *sql_text = text ? text : "no statement available"; |
| 22 | } |
[email protected] | faa604e | 2009-09-25 22:38:59 | [diff] [blame] | 23 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 24 | class SQLStatementTest : public testing::Test { |
| 25 | public: |
[email protected] | 49dc4f2 | 2012-10-17 17:41:16 | [diff] [blame] | 26 | SQLStatementTest() : error_(SQLITE_OK) {} |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 27 | |
[email protected] | 3dbbf7d | 2013-02-06 18:13:53 | [diff] [blame] | 28 | virtual void SetUp() { |
[email protected] | 3a305db | 2011-04-12 13:40:53 | [diff] [blame] | 29 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 30 | ASSERT_TRUE(db_.Open(temp_dir_.path().AppendASCII("SQLStatementTest.db"))); |
[email protected] | 49dc4f2 | 2012-10-17 17:41:16 | [diff] [blame] | 31 | // The error delegate will set |error_| and |sql_text_| when any sqlite |
| 32 | // statement operation returns an error code. |
[email protected] | 526b466 | 2013-06-14 04:09:12 | [diff] [blame^] | 33 | db_.set_error_callback(base::Bind(&CaptureErrorCallback, |
| 34 | &error_, &sql_text_)); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 35 | } |
| 36 | |
[email protected] | 3dbbf7d | 2013-02-06 18:13:53 | [diff] [blame] | 37 | virtual void TearDown() { |
[email protected] | faa604e | 2009-09-25 22:38:59 | [diff] [blame] | 38 | // If any error happened the original sql statement can be found in |
[email protected] | 49dc4f2 | 2012-10-17 17:41:16 | [diff] [blame] | 39 | // |sql_text_|. |
| 40 | EXPECT_EQ(SQLITE_OK, error_); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 41 | db_.Close(); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | sql::Connection& db() { return db_; } |
| 45 | |
[email protected] | 49dc4f2 | 2012-10-17 17:41:16 | [diff] [blame] | 46 | int sqlite_error() const { return error_; } |
| 47 | |
| 48 | void ResetError() { |
| 49 | error_ = SQLITE_OK; |
| 50 | sql_text_.clear(); |
| 51 | } |
[email protected] | faa604e | 2009-09-25 22:38:59 | [diff] [blame] | 52 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 53 | private: |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 54 | base::ScopedTempDir temp_dir_; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 55 | sql::Connection db_; |
[email protected] | 49dc4f2 | 2012-10-17 17:41:16 | [diff] [blame] | 56 | |
| 57 | // The error code of the most recent error. |
| 58 | int error_; |
| 59 | // Original statement which caused the error. |
| 60 | std::string sql_text_; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 61 | }; |
| 62 | |
[email protected] | 49dc4f2 | 2012-10-17 17:41:16 | [diff] [blame] | 63 | } // namespace |
| 64 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 65 | TEST_F(SQLStatementTest, Assign) { |
| 66 | sql::Statement s; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 67 | EXPECT_FALSE(s.is_valid()); |
| 68 | |
| 69 | s.Assign(db().GetUniqueStatement("CREATE TABLE foo (a, b)")); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 70 | EXPECT_TRUE(s.is_valid()); |
| 71 | } |
| 72 | |
| 73 | TEST_F(SQLStatementTest, Run) { |
| 74 | ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); |
| 75 | ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (3, 12)")); |
| 76 | |
| 77 | sql::Statement s(db().GetUniqueStatement("SELECT b FROM foo WHERE a=?")); |
| 78 | EXPECT_FALSE(s.Succeeded()); |
| 79 | |
| 80 | // Stepping it won't work since we haven't bound the value. |
| 81 | EXPECT_FALSE(s.Step()); |
| 82 | |
| 83 | // Run should fail since this produces output, and we should use Step(). This |
| 84 | // 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] | 85 | s.Reset(true); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 86 | s.BindInt(0, 3); |
| 87 | EXPECT_FALSE(s.Run()); |
| 88 | EXPECT_EQ(SQLITE_ROW, db().GetErrorCode()); |
| 89 | EXPECT_TRUE(s.Succeeded()); |
| 90 | |
| 91 | // Resetting it should put it back to the previous state (not runnable). |
[email protected] | 389e0a4 | 2012-04-25 21:36:41 | [diff] [blame] | 92 | s.Reset(true); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 93 | EXPECT_FALSE(s.Succeeded()); |
| 94 | |
| 95 | // Binding and stepping should produce one row. |
| 96 | s.BindInt(0, 3); |
| 97 | EXPECT_TRUE(s.Step()); |
| 98 | EXPECT_TRUE(s.Succeeded()); |
| 99 | EXPECT_EQ(12, s.ColumnInt(0)); |
| 100 | EXPECT_FALSE(s.Step()); |
| 101 | EXPECT_TRUE(s.Succeeded()); |
| 102 | } |
[email protected] | faa604e | 2009-09-25 22:38:59 | [diff] [blame] | 103 | |
| 104 | TEST_F(SQLStatementTest, BasicErrorCallback) { |
| 105 | ASSERT_TRUE(db().Execute("CREATE TABLE foo (a INTEGER PRIMARY KEY, b)")); |
| 106 | EXPECT_EQ(SQLITE_OK, sqlite_error()); |
| 107 | // Insert in the foo table the primary key. It is an error to insert |
| 108 | // something other than an number. This error causes the error callback |
| 109 | // handler to be called with SQLITE_MISMATCH as error code. |
| 110 | sql::Statement s(db().GetUniqueStatement("INSERT INTO foo (a) VALUES (?)")); |
| 111 | EXPECT_TRUE(s.is_valid()); |
| 112 | s.BindCString(0, "bad bad"); |
| 113 | EXPECT_FALSE(s.Run()); |
| 114 | EXPECT_EQ(SQLITE_MISMATCH, sqlite_error()); |
[email protected] | 49dc4f2 | 2012-10-17 17:41:16 | [diff] [blame] | 115 | ResetError(); |
[email protected] | faa604e | 2009-09-25 22:38:59 | [diff] [blame] | 116 | } |
[email protected] | 389e0a4 | 2012-04-25 21:36:41 | [diff] [blame] | 117 | |
| 118 | TEST_F(SQLStatementTest, Reset) { |
| 119 | ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); |
| 120 | ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (3, 12)")); |
| 121 | ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (4, 13)")); |
| 122 | |
| 123 | sql::Statement s(db().GetUniqueStatement( |
| 124 | "SELECT b FROM foo WHERE a = ? ")); |
| 125 | s.BindInt(0, 3); |
| 126 | ASSERT_TRUE(s.Step()); |
| 127 | EXPECT_EQ(12, s.ColumnInt(0)); |
| 128 | ASSERT_FALSE(s.Step()); |
| 129 | |
| 130 | s.Reset(false); |
| 131 | // Verify that we can get all rows again. |
| 132 | ASSERT_TRUE(s.Step()); |
| 133 | EXPECT_EQ(12, s.ColumnInt(0)); |
| 134 | EXPECT_FALSE(s.Step()); |
| 135 | |
| 136 | s.Reset(true); |
| 137 | ASSERT_FALSE(s.Step()); |
| 138 | } |