blob: 1565b3e48a97bdd936ae3fd52aee324d4a8e906d [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"
[email protected]f0a54b22011-07-19 18:40:2110#include "sql/connection.h"
erg102ceb412015-06-20 01:38:1311#include "sql/correct_sql_test_base.h"
[email protected]f0a54b22011-07-19 18:40:2112#include "sql/statement.h"
[email protected]98cf3002013-07-12 01:38:5613#include "sql/test/error_callback_support.h"
14#include "sql/test/scoped_error_ignorer.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(
69 &db(), base::Bind(&sql::CaptureErrorCallback, &error));
70
[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
81// Error ignorer works for error running a statement.
82TEST_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
88 sql::ScopedErrorIgnorer ignore_errors;
89 ignore_errors.IgnoreError(SQLITE_MISMATCH);
90 s.BindCString(0, "bad bad");
91 ASSERT_FALSE(s.Run());
92 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors());
[email protected]faa604e2009-09-25 22:38:5993}
[email protected]389e0a42012-04-25 21:36:4194
95TEST_F(SQLStatementTest, Reset) {
96 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
97 ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (3, 12)"));
98 ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (4, 13)"));
99
100 sql::Statement s(db().GetUniqueStatement(
101 "SELECT b FROM foo WHERE a = ? "));
102 s.BindInt(0, 3);
103 ASSERT_TRUE(s.Step());
104 EXPECT_EQ(12, s.ColumnInt(0));
105 ASSERT_FALSE(s.Step());
106
107 s.Reset(false);
108 // Verify that we can get all rows again.
109 ASSERT_TRUE(s.Step());
110 EXPECT_EQ(12, s.ColumnInt(0));
111 EXPECT_FALSE(s.Step());
112
113 s.Reset(true);
114 ASSERT_FALSE(s.Step());
115}