blob: 2033ee367e9057a62e91c023186913f88d10f75c [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]526b4662013-06-14 04:09:127#include "base/bind.h"
thestig22dfc4012014-09-05 08:29:448#include "base/files/file_util.h"
[email protected]ea1a3f62012-11-16 20:34:239#include "base/files/scoped_temp_dir.h"
Victor Costancfbfa602018-08-01 23:24:4610#include "sql/database.h"
[email protected]f0a54b22011-07-19 18:40:2111#include "sql/statement.h"
[email protected]98cf3002013-07-12 01:38:5612#include "sql/test/error_callback_support.h"
shess976814402016-06-21 06:56:2513#include "sql/test/scoped_error_expecter.h"
Scott Graham47ed2c32017-09-15 02:17:0714#include "sql/test/sql_test_base.h"
[email protected]e5ffd0e42009-09-11 21:30:5615#include "testing/gtest/include/gtest/gtest.h"
[email protected]e33cba42010-08-18 23:37:0316#include "third_party/sqlite/sqlite3.h"
[email protected]e5ffd0e42009-09-11 21:30:5617
[email protected]49dc4f22012-10-17 17:41:1618namespace {
19
erg102ceb412015-06-20 01:38:1320using SQLStatementTest = sql::SQLTestBase;
[email protected]e5ffd0e42009-09-11 21:30:5621
[email protected]49dc4f22012-10-17 17:41:1622} // namespace
23
[email protected]e5ffd0e42009-09-11 21:30:5624TEST_F(SQLStatementTest, Assign) {
25 sql::Statement s;
[email protected]e5ffd0e42009-09-11 21:30:5626 EXPECT_FALSE(s.is_valid());
27
28 s.Assign(db().GetUniqueStatement("CREATE TABLE foo (a, b)"));
[email protected]e5ffd0e42009-09-11 21:30:5629 EXPECT_TRUE(s.is_valid());
30}
31
32TEST_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]389e0a42012-04-25 21:36:4144 s.Reset(true);
[email protected]e5ffd0e42009-09-11 21:30:5645 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]389e0a42012-04-25 21:36:4151 s.Reset(true);
[email protected]e5ffd0e42009-09-11 21:30:5652 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]faa604e2009-09-25 22:38:5962
[email protected]98cf3002013-07-12 01:38:5663// Error callback called for error running a statement.
64TEST_F(SQLStatementTest, ErrorCallback) {
[email protected]faa604e2009-09-25 22:38:5965 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a INTEGER PRIMARY KEY, b)"));
[email protected]98cf3002013-07-12 01:38:5666
67 int error = SQLITE_OK;
68 sql::ScopedErrorCallback sec(
tzikd16d2192018-03-07 08:58:3669 &db(), base::BindRepeating(&sql::CaptureErrorCallback, &error));
[email protected]98cf3002013-07-12 01:38:5670
[email protected]faa604e2009-09-25 22:38:5971 // 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]98cf3002013-07-12 01:38:5678 EXPECT_EQ(SQLITE_MISMATCH, error);
79}
80
shess976814402016-06-21 06:56:2581// Error expecter works for error running a statement.
[email protected]98cf3002013-07-12 01:38:5682TEST_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
shess976814402016-06-21 06:56:2588 {
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]faa604e2009-09-25 22:38:5995}
[email protected]389e0a42012-04-25 21:36:4196
97TEST_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}