blob: 0028b9de5b08704b1a6d42862fc087749835f3d9 [file] [log] [blame]
[email protected]389e0a42012-04-25 21:36:411// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]e5ffd0e42009-09-11 21:30:562// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]faa604e2009-09-25 22:38:595#include <string>
6
[email protected]e5ffd0e42009-09-11 21:30:567#include "base/file_util.h"
[email protected]e0785902011-05-19 23:34:178#include "base/scoped_temp_dir.h"
[email protected]f0a54b22011-07-19 18:40:219#include "sql/connection.h"
10#include "sql/statement.h"
[email protected]e5ffd0e42009-09-11 21:30:5611#include "testing/gtest/include/gtest/gtest.h"
[email protected]e33cba42010-08-18 23:37:0312#include "third_party/sqlite/sqlite3.h"
[email protected]e5ffd0e42009-09-11 21:30:5613
[email protected]faa604e2009-09-25 22:38:5914class StatementErrorHandler : public sql::ErrorDelegate {
15 public:
16 StatementErrorHandler() : error_(SQLITE_OK) {}
17
18 virtual int OnError(int error, sql::Connection* connection,
19 sql::Statement* stmt) {
20 error_ = error;
[email protected]765b44502009-10-02 05:01:4221 const char* sql_txt = stmt ? stmt->GetSQLStatement() : NULL;
[email protected]faa604e2009-09-25 22:38:5922 sql_text_ = sql_txt ? sql_txt : "no statement available";
23 return error;
24 }
25
26 int error() const { return error_; }
27
28 void reset_error() {
29 sql_text_.clear();
30 error_ = SQLITE_OK;
31 }
32
33 const char* sql_statement() const { return sql_text_.c_str(); }
34
35 private:
36 int error_;
37 std::string sql_text_;
38};
39
[email protected]e5ffd0e42009-09-11 21:30:5640class SQLStatementTest : public testing::Test {
41 public:
[email protected]faa604e2009-09-25 22:38:5942 SQLStatementTest() : error_handler_(new StatementErrorHandler) {}
[email protected]e5ffd0e42009-09-11 21:30:5643
44 void SetUp() {
[email protected]3a305db2011-04-12 13:40:5345 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
46 ASSERT_TRUE(db_.Open(temp_dir_.path().AppendASCII("SQLStatementTest.db")));
47
[email protected]faa604e2009-09-25 22:38:5948 // The |error_handler_| will be called if any sqlite statement operation
49 // returns an error code.
50 db_.set_error_delegate(error_handler_);
[email protected]e5ffd0e42009-09-11 21:30:5651 }
52
53 void TearDown() {
[email protected]faa604e2009-09-25 22:38:5954 // If any error happened the original sql statement can be found in
55 // error_handler_->sql_statement().
56 EXPECT_EQ(SQLITE_OK, error_handler_->error());
[email protected]e5ffd0e42009-09-11 21:30:5657 db_.Close();
[email protected]e5ffd0e42009-09-11 21:30:5658 }
59
60 sql::Connection& db() { return db_; }
61
[email protected]faa604e2009-09-25 22:38:5962 int sqlite_error() const { return error_handler_->error(); }
63 void reset_error() const { error_handler_->reset_error(); }
64
[email protected]e5ffd0e42009-09-11 21:30:5665 private:
[email protected]3a305db2011-04-12 13:40:5366 ScopedTempDir temp_dir_;
[email protected]e5ffd0e42009-09-11 21:30:5667 sql::Connection db_;
[email protected]faa604e2009-09-25 22:38:5968 scoped_refptr<StatementErrorHandler> error_handler_;
[email protected]e5ffd0e42009-09-11 21:30:5669};
70
71TEST_F(SQLStatementTest, Assign) {
72 sql::Statement s;
[email protected]e5ffd0e42009-09-11 21:30:5673 EXPECT_FALSE(s.is_valid());
74
75 s.Assign(db().GetUniqueStatement("CREATE TABLE foo (a, b)"));
[email protected]e5ffd0e42009-09-11 21:30:5676 EXPECT_TRUE(s.is_valid());
77}
78
79TEST_F(SQLStatementTest, Run) {
80 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
81 ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (3, 12)"));
82
83 sql::Statement s(db().GetUniqueStatement("SELECT b FROM foo WHERE a=?"));
84 EXPECT_FALSE(s.Succeeded());
85
86 // Stepping it won't work since we haven't bound the value.
87 EXPECT_FALSE(s.Step());
88
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.
[email protected]389e0a42012-04-25 21:36:4191 s.Reset(true);
[email protected]e5ffd0e42009-09-11 21:30:5692 s.BindInt(0, 3);
93 EXPECT_FALSE(s.Run());
94 EXPECT_EQ(SQLITE_ROW, db().GetErrorCode());
95 EXPECT_TRUE(s.Succeeded());
96
97 // Resetting it should put it back to the previous state (not runnable).
[email protected]389e0a42012-04-25 21:36:4198 s.Reset(true);
[email protected]e5ffd0e42009-09-11 21:30:5699 EXPECT_FALSE(s.Succeeded());
100
101 // Binding and stepping should produce one row.
102 s.BindInt(0, 3);
103 EXPECT_TRUE(s.Step());
104 EXPECT_TRUE(s.Succeeded());
105 EXPECT_EQ(12, s.ColumnInt(0));
106 EXPECT_FALSE(s.Step());
107 EXPECT_TRUE(s.Succeeded());
108}
[email protected]faa604e2009-09-25 22:38:59109
110TEST_F(SQLStatementTest, BasicErrorCallback) {
111 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a INTEGER PRIMARY KEY, b)"));
112 EXPECT_EQ(SQLITE_OK, sqlite_error());
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
115 // handler to be called with SQLITE_MISMATCH as error code.
116 sql::Statement s(db().GetUniqueStatement("INSERT INTO foo (a) VALUES (?)"));
117 EXPECT_TRUE(s.is_valid());
118 s.BindCString(0, "bad bad");
119 EXPECT_FALSE(s.Run());
120 EXPECT_EQ(SQLITE_MISMATCH, sqlite_error());
121 reset_error();
122}
[email protected]389e0a42012-04-25 21:36:41123
124TEST_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}