1*11be35a1SLionel Sambuc // Copyright 2011 Google Inc.
2*11be35a1SLionel Sambuc // All rights reserved.
3*11be35a1SLionel Sambuc //
4*11be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
5*11be35a1SLionel Sambuc // modification, are permitted provided that the following conditions are
6*11be35a1SLionel Sambuc // met:
7*11be35a1SLionel Sambuc //
8*11be35a1SLionel Sambuc // * Redistributions of source code must retain the above copyright
9*11be35a1SLionel Sambuc // notice, this list of conditions and the following disclaimer.
10*11be35a1SLionel Sambuc // * Redistributions in binary form must reproduce the above copyright
11*11be35a1SLionel Sambuc // notice, this list of conditions and the following disclaimer in the
12*11be35a1SLionel Sambuc // documentation and/or other materials provided with the distribution.
13*11be35a1SLionel Sambuc // * Neither the name of Google Inc. nor the names of its contributors
14*11be35a1SLionel Sambuc // may be used to endorse or promote products derived from this software
15*11be35a1SLionel Sambuc // without specific prior written permission.
16*11be35a1SLionel Sambuc //
17*11be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*11be35a1SLionel Sambuc // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*11be35a1SLionel Sambuc // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*11be35a1SLionel Sambuc // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*11be35a1SLionel Sambuc // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*11be35a1SLionel Sambuc // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*11be35a1SLionel Sambuc // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*11be35a1SLionel Sambuc // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*11be35a1SLionel Sambuc // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*11be35a1SLionel Sambuc // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*11be35a1SLionel Sambuc // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc
29*11be35a1SLionel Sambuc #include "utils/sqlite/statement.ipp"
30*11be35a1SLionel Sambuc
31*11be35a1SLionel Sambuc extern "C" {
32*11be35a1SLionel Sambuc #include <stdint.h>
33*11be35a1SLionel Sambuc }
34*11be35a1SLionel Sambuc
35*11be35a1SLionel Sambuc #include <cstring>
36*11be35a1SLionel Sambuc #include <iostream>
37*11be35a1SLionel Sambuc
38*11be35a1SLionel Sambuc #include <atf-c++.hpp>
39*11be35a1SLionel Sambuc
40*11be35a1SLionel Sambuc #include "utils/sqlite/database.hpp"
41*11be35a1SLionel Sambuc #include "utils/sqlite/test_utils.hpp"
42*11be35a1SLionel Sambuc
43*11be35a1SLionel Sambuc namespace sqlite = utils::sqlite;
44*11be35a1SLionel Sambuc
45*11be35a1SLionel Sambuc
46*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(step__ok);
ATF_TEST_CASE_BODY(step__ok)47*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(step__ok)
48*11be35a1SLionel Sambuc {
49*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
50*11be35a1SLionel Sambuc sqlite::statement stmt = db.create_statement(
51*11be35a1SLionel Sambuc "CREATE TABLE foo (a INTEGER PRIMARY KEY)");
52*11be35a1SLionel Sambuc ATF_REQUIRE_THROW(sqlite::error, db.exec("SELECT * FROM foo"));
53*11be35a1SLionel Sambuc ATF_REQUIRE(!stmt.step());
54*11be35a1SLionel Sambuc db.exec("SELECT * FROM foo");
55*11be35a1SLionel Sambuc }
56*11be35a1SLionel Sambuc
57*11be35a1SLionel Sambuc
58*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(step__many);
ATF_TEST_CASE_BODY(step__many)59*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(step__many)
60*11be35a1SLionel Sambuc {
61*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
62*11be35a1SLionel Sambuc create_test_table(raw(db));
63*11be35a1SLionel Sambuc sqlite::statement stmt = db.create_statement(
64*11be35a1SLionel Sambuc "SELECT prime FROM test ORDER BY prime");
65*11be35a1SLionel Sambuc for (int i = 0; i < 5; i++)
66*11be35a1SLionel Sambuc ATF_REQUIRE(stmt.step());
67*11be35a1SLionel Sambuc ATF_REQUIRE(!stmt.step());
68*11be35a1SLionel Sambuc }
69*11be35a1SLionel Sambuc
70*11be35a1SLionel Sambuc
71*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(step__fail);
ATF_TEST_CASE_BODY(step__fail)72*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(step__fail)
73*11be35a1SLionel Sambuc {
74*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
75*11be35a1SLionel Sambuc sqlite::statement stmt = db.create_statement(
76*11be35a1SLionel Sambuc "CREATE TABLE foo (a INTEGER PRIMARY KEY)");
77*11be35a1SLionel Sambuc ATF_REQUIRE(!stmt.step());
78*11be35a1SLionel Sambuc REQUIRE_API_ERROR("sqlite3_step", stmt.step());
79*11be35a1SLionel Sambuc }
80*11be35a1SLionel Sambuc
81*11be35a1SLionel Sambuc
82*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(step_without_results__ok);
ATF_TEST_CASE_BODY(step_without_results__ok)83*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(step_without_results__ok)
84*11be35a1SLionel Sambuc {
85*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
86*11be35a1SLionel Sambuc sqlite::statement stmt = db.create_statement(
87*11be35a1SLionel Sambuc "CREATE TABLE foo (a INTEGER PRIMARY KEY)");
88*11be35a1SLionel Sambuc ATF_REQUIRE_THROW(sqlite::error, db.exec("SELECT * FROM foo"));
89*11be35a1SLionel Sambuc stmt.step_without_results();
90*11be35a1SLionel Sambuc db.exec("SELECT * FROM foo");
91*11be35a1SLionel Sambuc }
92*11be35a1SLionel Sambuc
93*11be35a1SLionel Sambuc
94*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(step_without_results__fail);
ATF_TEST_CASE_BODY(step_without_results__fail)95*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(step_without_results__fail)
96*11be35a1SLionel Sambuc {
97*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
98*11be35a1SLionel Sambuc db.exec("CREATE TABLE foo (a INTEGER PRIMARY KEY)");
99*11be35a1SLionel Sambuc db.exec("INSERT INTO foo VALUES (3)");
100*11be35a1SLionel Sambuc sqlite::statement stmt = db.create_statement(
101*11be35a1SLionel Sambuc "INSERT INTO foo VALUES (3)");
102*11be35a1SLionel Sambuc REQUIRE_API_ERROR("sqlite3_step", stmt.step_without_results());
103*11be35a1SLionel Sambuc }
104*11be35a1SLionel Sambuc
105*11be35a1SLionel Sambuc
106*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(column_count);
ATF_TEST_CASE_BODY(column_count)107*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(column_count)
108*11be35a1SLionel Sambuc {
109*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
110*11be35a1SLionel Sambuc db.exec("CREATE TABLE foo (a INTEGER PRIMARY KEY, b INTEGER, c TEXT);"
111*11be35a1SLionel Sambuc "INSERT INTO foo VALUES (5, 3, 'foo');");
112*11be35a1SLionel Sambuc sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
113*11be35a1SLionel Sambuc ATF_REQUIRE(stmt.step());
114*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(3, stmt.column_count());
115*11be35a1SLionel Sambuc ATF_REQUIRE(!stmt.step());
116*11be35a1SLionel Sambuc }
117*11be35a1SLionel Sambuc
118*11be35a1SLionel Sambuc
119*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(column_name__ok);
ATF_TEST_CASE_BODY(column_name__ok)120*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(column_name__ok)
121*11be35a1SLionel Sambuc {
122*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
123*11be35a1SLionel Sambuc db.exec("CREATE TABLE foo (first INTEGER PRIMARY KEY, second TEXT);"
124*11be35a1SLionel Sambuc "INSERT INTO foo VALUES (5, 'foo');");
125*11be35a1SLionel Sambuc sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
126*11be35a1SLionel Sambuc ATF_REQUIRE(stmt.step());
127*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("first", stmt.column_name(0));
128*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("second", stmt.column_name(1));
129*11be35a1SLionel Sambuc ATF_REQUIRE(!stmt.step());
130*11be35a1SLionel Sambuc }
131*11be35a1SLionel Sambuc
132*11be35a1SLionel Sambuc
133*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(column_name__fail);
ATF_TEST_CASE_BODY(column_name__fail)134*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(column_name__fail)
135*11be35a1SLionel Sambuc {
136*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
137*11be35a1SLionel Sambuc db.exec("CREATE TABLE foo (first INTEGER PRIMARY KEY);"
138*11be35a1SLionel Sambuc "INSERT INTO foo VALUES (5);");
139*11be35a1SLionel Sambuc sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
140*11be35a1SLionel Sambuc ATF_REQUIRE(stmt.step());
141*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("first", stmt.column_name(0));
142*11be35a1SLionel Sambuc REQUIRE_API_ERROR("sqlite3_column_name", stmt.column_name(1));
143*11be35a1SLionel Sambuc ATF_REQUIRE(!stmt.step());
144*11be35a1SLionel Sambuc }
145*11be35a1SLionel Sambuc
146*11be35a1SLionel Sambuc
147*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(column_type__ok);
ATF_TEST_CASE_BODY(column_type__ok)148*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(column_type__ok)
149*11be35a1SLionel Sambuc {
150*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
151*11be35a1SLionel Sambuc db.exec("CREATE TABLE foo (a_blob BLOB,"
152*11be35a1SLionel Sambuc " a_float FLOAT,"
153*11be35a1SLionel Sambuc " an_integer INTEGER,"
154*11be35a1SLionel Sambuc " a_null BLOB,"
155*11be35a1SLionel Sambuc " a_text TEXT);"
156*11be35a1SLionel Sambuc "INSERT INTO foo VALUES (x'0102', 0.3, 5, NULL, 'foo bar');"
157*11be35a1SLionel Sambuc "INSERT INTO foo VALUES (NULL, NULL, NULL, NULL, NULL);");
158*11be35a1SLionel Sambuc sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
159*11be35a1SLionel Sambuc ATF_REQUIRE(stmt.step());
160*11be35a1SLionel Sambuc ATF_REQUIRE(sqlite::type_blob == stmt.column_type(0));
161*11be35a1SLionel Sambuc ATF_REQUIRE(sqlite::type_float == stmt.column_type(1));
162*11be35a1SLionel Sambuc ATF_REQUIRE(sqlite::type_integer == stmt.column_type(2));
163*11be35a1SLionel Sambuc ATF_REQUIRE(sqlite::type_null == stmt.column_type(3));
164*11be35a1SLionel Sambuc ATF_REQUIRE(sqlite::type_text == stmt.column_type(4));
165*11be35a1SLionel Sambuc ATF_REQUIRE(stmt.step());
166*11be35a1SLionel Sambuc for (int i = 0; i < stmt.column_count(); i++)
167*11be35a1SLionel Sambuc ATF_REQUIRE(sqlite::type_null == stmt.column_type(i));
168*11be35a1SLionel Sambuc ATF_REQUIRE(!stmt.step());
169*11be35a1SLionel Sambuc }
170*11be35a1SLionel Sambuc
171*11be35a1SLionel Sambuc
172*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(column_type__out_of_range);
ATF_TEST_CASE_BODY(column_type__out_of_range)173*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(column_type__out_of_range)
174*11be35a1SLionel Sambuc {
175*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
176*11be35a1SLionel Sambuc db.exec("CREATE TABLE foo (a INTEGER PRIMARY KEY);"
177*11be35a1SLionel Sambuc "INSERT INTO foo VALUES (1);");
178*11be35a1SLionel Sambuc sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
179*11be35a1SLionel Sambuc ATF_REQUIRE(stmt.step());
180*11be35a1SLionel Sambuc ATF_REQUIRE(sqlite::type_integer == stmt.column_type(0));
181*11be35a1SLionel Sambuc ATF_REQUIRE(sqlite::type_null == stmt.column_type(1));
182*11be35a1SLionel Sambuc ATF_REQUIRE(sqlite::type_null == stmt.column_type(512));
183*11be35a1SLionel Sambuc ATF_REQUIRE(!stmt.step());
184*11be35a1SLionel Sambuc }
185*11be35a1SLionel Sambuc
186*11be35a1SLionel Sambuc
187*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(column_id__ok);
ATF_TEST_CASE_BODY(column_id__ok)188*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(column_id__ok)
189*11be35a1SLionel Sambuc {
190*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
191*11be35a1SLionel Sambuc db.exec("CREATE TABLE foo (bar INTEGER PRIMARY KEY, "
192*11be35a1SLionel Sambuc " baz INTEGER);"
193*11be35a1SLionel Sambuc "INSERT INTO foo VALUES (1, 2);");
194*11be35a1SLionel Sambuc sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
195*11be35a1SLionel Sambuc ATF_REQUIRE(stmt.step());
196*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(0, stmt.column_id("bar"));
197*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(1, stmt.column_id("baz"));
198*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(0, stmt.column_id("bar"));
199*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(1, stmt.column_id("baz"));
200*11be35a1SLionel Sambuc ATF_REQUIRE(!stmt.step());
201*11be35a1SLionel Sambuc }
202*11be35a1SLionel Sambuc
203*11be35a1SLionel Sambuc
204*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(column_id__missing);
ATF_TEST_CASE_BODY(column_id__missing)205*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(column_id__missing)
206*11be35a1SLionel Sambuc {
207*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
208*11be35a1SLionel Sambuc db.exec("CREATE TABLE foo (bar INTEGER PRIMARY KEY, "
209*11be35a1SLionel Sambuc " baz INTEGER);"
210*11be35a1SLionel Sambuc "INSERT INTO foo VALUES (1, 2);");
211*11be35a1SLionel Sambuc sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
212*11be35a1SLionel Sambuc ATF_REQUIRE(stmt.step());
213*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(0, stmt.column_id("bar"));
214*11be35a1SLionel Sambuc try {
215*11be35a1SLionel Sambuc stmt.column_id("bazo");
216*11be35a1SLionel Sambuc fail("invalid_column_error not raised");
217*11be35a1SLionel Sambuc } catch (const sqlite::invalid_column_error& e) {
218*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("bazo", e.column_name());
219*11be35a1SLionel Sambuc }
220*11be35a1SLionel Sambuc ATF_REQUIRE(!stmt.step());
221*11be35a1SLionel Sambuc }
222*11be35a1SLionel Sambuc
223*11be35a1SLionel Sambuc
224*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(column_blob);
ATF_TEST_CASE_BODY(column_blob)225*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(column_blob)
226*11be35a1SLionel Sambuc {
227*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
228*11be35a1SLionel Sambuc db.exec("CREATE TABLE foo (a INTEGER, b BLOB, c INTEGER);"
229*11be35a1SLionel Sambuc "INSERT INTO foo VALUES (NULL, x'cafe', NULL);");
230*11be35a1SLionel Sambuc sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
231*11be35a1SLionel Sambuc ATF_REQUIRE(stmt.step());
232*11be35a1SLionel Sambuc const sqlite::blob blob = stmt.column_blob(1);
233*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(0xca, static_cast< const uint8_t* >(blob.memory)[0]);
234*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(0xfe, static_cast< const uint8_t* >(blob.memory)[1]);
235*11be35a1SLionel Sambuc ATF_REQUIRE(!stmt.step());
236*11be35a1SLionel Sambuc }
237*11be35a1SLionel Sambuc
238*11be35a1SLionel Sambuc
239*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(column_double);
ATF_TEST_CASE_BODY(column_double)240*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(column_double)
241*11be35a1SLionel Sambuc {
242*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
243*11be35a1SLionel Sambuc db.exec("CREATE TABLE foo (a INTEGER, b DOUBLE, c INTEGER);"
244*11be35a1SLionel Sambuc "INSERT INTO foo VALUES (NULL, 0.5, NULL);");
245*11be35a1SLionel Sambuc sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
246*11be35a1SLionel Sambuc ATF_REQUIRE(stmt.step());
247*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(0.5, stmt.column_double(1));
248*11be35a1SLionel Sambuc ATF_REQUIRE(!stmt.step());
249*11be35a1SLionel Sambuc }
250*11be35a1SLionel Sambuc
251*11be35a1SLionel Sambuc
252*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(column_int__ok);
ATF_TEST_CASE_BODY(column_int__ok)253*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(column_int__ok)
254*11be35a1SLionel Sambuc {
255*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
256*11be35a1SLionel Sambuc db.exec("CREATE TABLE foo (a TEXT, b INTEGER, c TEXT);"
257*11be35a1SLionel Sambuc "INSERT INTO foo VALUES (NULL, 987, NULL);");
258*11be35a1SLionel Sambuc sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
259*11be35a1SLionel Sambuc ATF_REQUIRE(stmt.step());
260*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(987, stmt.column_int(1));
261*11be35a1SLionel Sambuc ATF_REQUIRE(!stmt.step());
262*11be35a1SLionel Sambuc }
263*11be35a1SLionel Sambuc
264*11be35a1SLionel Sambuc
265*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(column_int__overflow);
ATF_TEST_CASE_BODY(column_int__overflow)266*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(column_int__overflow)
267*11be35a1SLionel Sambuc {
268*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
269*11be35a1SLionel Sambuc db.exec("CREATE TABLE foo (a TEXT, b INTEGER, c TEXT);"
270*11be35a1SLionel Sambuc "INSERT INTO foo VALUES (NULL, 4294967419, NULL);");
271*11be35a1SLionel Sambuc sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
272*11be35a1SLionel Sambuc ATF_REQUIRE(stmt.step());
273*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(123, stmt.column_int(1));
274*11be35a1SLionel Sambuc ATF_REQUIRE(!stmt.step());
275*11be35a1SLionel Sambuc }
276*11be35a1SLionel Sambuc
277*11be35a1SLionel Sambuc
278*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(column_int64);
ATF_TEST_CASE_BODY(column_int64)279*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(column_int64)
280*11be35a1SLionel Sambuc {
281*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
282*11be35a1SLionel Sambuc db.exec("CREATE TABLE foo (a TEXT, b INTEGER, c TEXT);"
283*11be35a1SLionel Sambuc "INSERT INTO foo VALUES (NULL, 4294967419, NULL);");
284*11be35a1SLionel Sambuc sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
285*11be35a1SLionel Sambuc ATF_REQUIRE(stmt.step());
286*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(4294967419LL, stmt.column_int64(1));
287*11be35a1SLionel Sambuc ATF_REQUIRE(!stmt.step());
288*11be35a1SLionel Sambuc }
289*11be35a1SLionel Sambuc
290*11be35a1SLionel Sambuc
291*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(column_text);
ATF_TEST_CASE_BODY(column_text)292*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(column_text)
293*11be35a1SLionel Sambuc {
294*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
295*11be35a1SLionel Sambuc db.exec("CREATE TABLE foo (a INTEGER, b TEXT, c INTEGER);"
296*11be35a1SLionel Sambuc "INSERT INTO foo VALUES (NULL, 'foo bar', NULL);");
297*11be35a1SLionel Sambuc sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
298*11be35a1SLionel Sambuc ATF_REQUIRE(stmt.step());
299*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("foo bar", stmt.column_text(1));
300*11be35a1SLionel Sambuc ATF_REQUIRE(!stmt.step());
301*11be35a1SLionel Sambuc }
302*11be35a1SLionel Sambuc
303*11be35a1SLionel Sambuc
304*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(column_bytes__blob);
ATF_TEST_CASE_BODY(column_bytes__blob)305*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(column_bytes__blob)
306*11be35a1SLionel Sambuc {
307*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
308*11be35a1SLionel Sambuc db.exec("CREATE TABLE foo (a BLOB);"
309*11be35a1SLionel Sambuc "INSERT INTO foo VALUES (x'12345678');");
310*11be35a1SLionel Sambuc sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
311*11be35a1SLionel Sambuc ATF_REQUIRE(stmt.step());
312*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(4, stmt.column_bytes(0));
313*11be35a1SLionel Sambuc ATF_REQUIRE(!stmt.step());
314*11be35a1SLionel Sambuc }
315*11be35a1SLionel Sambuc
316*11be35a1SLionel Sambuc
317*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(column_bytes__text);
ATF_TEST_CASE_BODY(column_bytes__text)318*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(column_bytes__text)
319*11be35a1SLionel Sambuc {
320*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
321*11be35a1SLionel Sambuc db.exec("CREATE TABLE foo (a TEXT);"
322*11be35a1SLionel Sambuc "INSERT INTO foo VALUES ('foo bar');");
323*11be35a1SLionel Sambuc sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
324*11be35a1SLionel Sambuc ATF_REQUIRE(stmt.step());
325*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(7, stmt.column_bytes(0));
326*11be35a1SLionel Sambuc ATF_REQUIRE(!stmt.step());
327*11be35a1SLionel Sambuc }
328*11be35a1SLionel Sambuc
329*11be35a1SLionel Sambuc
330*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(safe_column_blob__ok);
ATF_TEST_CASE_BODY(safe_column_blob__ok)331*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(safe_column_blob__ok)
332*11be35a1SLionel Sambuc {
333*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
334*11be35a1SLionel Sambuc db.exec("CREATE TABLE foo (a INTEGER, b BLOB, c INTEGER);"
335*11be35a1SLionel Sambuc "INSERT INTO foo VALUES (NULL, x'cafe', NULL);");
336*11be35a1SLionel Sambuc sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
337*11be35a1SLionel Sambuc ATF_REQUIRE(stmt.step());
338*11be35a1SLionel Sambuc const sqlite::blob blob = stmt.safe_column_blob("b");
339*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(0xca, static_cast< const uint8_t* >(blob.memory)[0]);
340*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(0xfe, static_cast< const uint8_t* >(blob.memory)[1]);
341*11be35a1SLionel Sambuc ATF_REQUIRE(!stmt.step());
342*11be35a1SLionel Sambuc }
343*11be35a1SLionel Sambuc
344*11be35a1SLionel Sambuc
345*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(safe_column_blob__fail);
ATF_TEST_CASE_BODY(safe_column_blob__fail)346*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(safe_column_blob__fail)
347*11be35a1SLionel Sambuc {
348*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
349*11be35a1SLionel Sambuc db.exec("CREATE TABLE foo (a INTEGER);"
350*11be35a1SLionel Sambuc "INSERT INTO foo VALUES (123);");
351*11be35a1SLionel Sambuc sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
352*11be35a1SLionel Sambuc ATF_REQUIRE(stmt.step());
353*11be35a1SLionel Sambuc ATF_REQUIRE_THROW(sqlite::invalid_column_error,
354*11be35a1SLionel Sambuc stmt.safe_column_blob("b"));
355*11be35a1SLionel Sambuc ATF_REQUIRE_THROW_RE(sqlite::error, "not a blob",
356*11be35a1SLionel Sambuc stmt.safe_column_blob("a"));
357*11be35a1SLionel Sambuc ATF_REQUIRE(!stmt.step());
358*11be35a1SLionel Sambuc }
359*11be35a1SLionel Sambuc
360*11be35a1SLionel Sambuc
361*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(safe_column_double__ok);
ATF_TEST_CASE_BODY(safe_column_double__ok)362*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(safe_column_double__ok)
363*11be35a1SLionel Sambuc {
364*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
365*11be35a1SLionel Sambuc db.exec("CREATE TABLE foo (a INTEGER, b DOUBLE, c INTEGER);"
366*11be35a1SLionel Sambuc "INSERT INTO foo VALUES (NULL, 0.5, NULL);");
367*11be35a1SLionel Sambuc sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
368*11be35a1SLionel Sambuc ATF_REQUIRE(stmt.step());
369*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(0.5, stmt.safe_column_double("b"));
370*11be35a1SLionel Sambuc ATF_REQUIRE(!stmt.step());
371*11be35a1SLionel Sambuc }
372*11be35a1SLionel Sambuc
373*11be35a1SLionel Sambuc
374*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(safe_column_double__fail);
ATF_TEST_CASE_BODY(safe_column_double__fail)375*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(safe_column_double__fail)
376*11be35a1SLionel Sambuc {
377*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
378*11be35a1SLionel Sambuc db.exec("CREATE TABLE foo (a INTEGER);"
379*11be35a1SLionel Sambuc "INSERT INTO foo VALUES (NULL);");
380*11be35a1SLionel Sambuc sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
381*11be35a1SLionel Sambuc ATF_REQUIRE(stmt.step());
382*11be35a1SLionel Sambuc ATF_REQUIRE_THROW(sqlite::invalid_column_error,
383*11be35a1SLionel Sambuc stmt.safe_column_double("b"));
384*11be35a1SLionel Sambuc ATF_REQUIRE_THROW_RE(sqlite::error, "not a float",
385*11be35a1SLionel Sambuc stmt.safe_column_double("a"));
386*11be35a1SLionel Sambuc ATF_REQUIRE(!stmt.step());
387*11be35a1SLionel Sambuc }
388*11be35a1SLionel Sambuc
389*11be35a1SLionel Sambuc
390*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(safe_column_int__ok);
ATF_TEST_CASE_BODY(safe_column_int__ok)391*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(safe_column_int__ok)
392*11be35a1SLionel Sambuc {
393*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
394*11be35a1SLionel Sambuc db.exec("CREATE TABLE foo (a TEXT, b INTEGER, c TEXT);"
395*11be35a1SLionel Sambuc "INSERT INTO foo VALUES (NULL, 987, NULL);");
396*11be35a1SLionel Sambuc sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
397*11be35a1SLionel Sambuc ATF_REQUIRE(stmt.step());
398*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(987, stmt.safe_column_int("b"));
399*11be35a1SLionel Sambuc ATF_REQUIRE(!stmt.step());
400*11be35a1SLionel Sambuc }
401*11be35a1SLionel Sambuc
402*11be35a1SLionel Sambuc
403*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(safe_column_int__fail);
ATF_TEST_CASE_BODY(safe_column_int__fail)404*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(safe_column_int__fail)
405*11be35a1SLionel Sambuc {
406*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
407*11be35a1SLionel Sambuc db.exec("CREATE TABLE foo (a TEXT);"
408*11be35a1SLionel Sambuc "INSERT INTO foo VALUES ('def');");
409*11be35a1SLionel Sambuc sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
410*11be35a1SLionel Sambuc ATF_REQUIRE(stmt.step());
411*11be35a1SLionel Sambuc ATF_REQUIRE_THROW(sqlite::invalid_column_error,
412*11be35a1SLionel Sambuc stmt.safe_column_int("b"));
413*11be35a1SLionel Sambuc ATF_REQUIRE_THROW_RE(sqlite::error, "not an integer",
414*11be35a1SLionel Sambuc stmt.safe_column_int("a"));
415*11be35a1SLionel Sambuc ATF_REQUIRE(!stmt.step());
416*11be35a1SLionel Sambuc }
417*11be35a1SLionel Sambuc
418*11be35a1SLionel Sambuc
419*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(safe_column_int64__ok);
ATF_TEST_CASE_BODY(safe_column_int64__ok)420*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(safe_column_int64__ok)
421*11be35a1SLionel Sambuc {
422*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
423*11be35a1SLionel Sambuc db.exec("CREATE TABLE foo (a TEXT, b INTEGER, c TEXT);"
424*11be35a1SLionel Sambuc "INSERT INTO foo VALUES (NULL, 4294967419, NULL);");
425*11be35a1SLionel Sambuc sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
426*11be35a1SLionel Sambuc ATF_REQUIRE(stmt.step());
427*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(4294967419LL, stmt.safe_column_int64("b"));
428*11be35a1SLionel Sambuc ATF_REQUIRE(!stmt.step());
429*11be35a1SLionel Sambuc }
430*11be35a1SLionel Sambuc
431*11be35a1SLionel Sambuc
432*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(safe_column_int64__fail);
ATF_TEST_CASE_BODY(safe_column_int64__fail)433*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(safe_column_int64__fail)
434*11be35a1SLionel Sambuc {
435*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
436*11be35a1SLionel Sambuc db.exec("CREATE TABLE foo (a TEXT);"
437*11be35a1SLionel Sambuc "INSERT INTO foo VALUES ('abc');");
438*11be35a1SLionel Sambuc sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
439*11be35a1SLionel Sambuc ATF_REQUIRE(stmt.step());
440*11be35a1SLionel Sambuc ATF_REQUIRE_THROW(sqlite::invalid_column_error,
441*11be35a1SLionel Sambuc stmt.safe_column_int64("b"));
442*11be35a1SLionel Sambuc ATF_REQUIRE_THROW_RE(sqlite::error, "not an integer",
443*11be35a1SLionel Sambuc stmt.safe_column_int64("a"));
444*11be35a1SLionel Sambuc ATF_REQUIRE(!stmt.step());
445*11be35a1SLionel Sambuc }
446*11be35a1SLionel Sambuc
447*11be35a1SLionel Sambuc
448*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(safe_column_text__ok);
ATF_TEST_CASE_BODY(safe_column_text__ok)449*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(safe_column_text__ok)
450*11be35a1SLionel Sambuc {
451*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
452*11be35a1SLionel Sambuc db.exec("CREATE TABLE foo (a INTEGER, b TEXT, c INTEGER);"
453*11be35a1SLionel Sambuc "INSERT INTO foo VALUES (NULL, 'foo bar', NULL);");
454*11be35a1SLionel Sambuc sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
455*11be35a1SLionel Sambuc ATF_REQUIRE(stmt.step());
456*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("foo bar", stmt.safe_column_text("b"));
457*11be35a1SLionel Sambuc ATF_REQUIRE(!stmt.step());
458*11be35a1SLionel Sambuc }
459*11be35a1SLionel Sambuc
460*11be35a1SLionel Sambuc
461*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(safe_column_text__fail);
ATF_TEST_CASE_BODY(safe_column_text__fail)462*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(safe_column_text__fail)
463*11be35a1SLionel Sambuc {
464*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
465*11be35a1SLionel Sambuc db.exec("CREATE TABLE foo (a INTEGER);"
466*11be35a1SLionel Sambuc "INSERT INTO foo VALUES (NULL);");
467*11be35a1SLionel Sambuc sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
468*11be35a1SLionel Sambuc ATF_REQUIRE(stmt.step());
469*11be35a1SLionel Sambuc ATF_REQUIRE_THROW(sqlite::invalid_column_error,
470*11be35a1SLionel Sambuc stmt.safe_column_text("b"));
471*11be35a1SLionel Sambuc ATF_REQUIRE_THROW_RE(sqlite::error, "not a string",
472*11be35a1SLionel Sambuc stmt.safe_column_text("a"));
473*11be35a1SLionel Sambuc ATF_REQUIRE(!stmt.step());
474*11be35a1SLionel Sambuc }
475*11be35a1SLionel Sambuc
476*11be35a1SLionel Sambuc
477*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(safe_column_bytes__ok__blob);
ATF_TEST_CASE_BODY(safe_column_bytes__ok__blob)478*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(safe_column_bytes__ok__blob)
479*11be35a1SLionel Sambuc {
480*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
481*11be35a1SLionel Sambuc db.exec("CREATE TABLE foo (a BLOB);"
482*11be35a1SLionel Sambuc "INSERT INTO foo VALUES (x'12345678');");
483*11be35a1SLionel Sambuc sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
484*11be35a1SLionel Sambuc ATF_REQUIRE(stmt.step());
485*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(4, stmt.safe_column_bytes("a"));
486*11be35a1SLionel Sambuc ATF_REQUIRE(!stmt.step());
487*11be35a1SLionel Sambuc }
488*11be35a1SLionel Sambuc
489*11be35a1SLionel Sambuc
490*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(safe_column_bytes__ok__text);
ATF_TEST_CASE_BODY(safe_column_bytes__ok__text)491*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(safe_column_bytes__ok__text)
492*11be35a1SLionel Sambuc {
493*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
494*11be35a1SLionel Sambuc db.exec("CREATE TABLE foo (a TEXT);"
495*11be35a1SLionel Sambuc "INSERT INTO foo VALUES ('foo bar');");
496*11be35a1SLionel Sambuc sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
497*11be35a1SLionel Sambuc ATF_REQUIRE(stmt.step());
498*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(7, stmt.safe_column_bytes("a"));
499*11be35a1SLionel Sambuc ATF_REQUIRE(!stmt.step());
500*11be35a1SLionel Sambuc }
501*11be35a1SLionel Sambuc
502*11be35a1SLionel Sambuc
503*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(safe_column_bytes__fail);
ATF_TEST_CASE_BODY(safe_column_bytes__fail)504*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(safe_column_bytes__fail)
505*11be35a1SLionel Sambuc {
506*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
507*11be35a1SLionel Sambuc db.exec("CREATE TABLE foo (a TEXT);"
508*11be35a1SLionel Sambuc "INSERT INTO foo VALUES (NULL);");
509*11be35a1SLionel Sambuc sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
510*11be35a1SLionel Sambuc ATF_REQUIRE(stmt.step());
511*11be35a1SLionel Sambuc ATF_REQUIRE_THROW(sqlite::invalid_column_error,
512*11be35a1SLionel Sambuc stmt.safe_column_bytes("b"));
513*11be35a1SLionel Sambuc ATF_REQUIRE_THROW_RE(sqlite::error, "not a blob or a string",
514*11be35a1SLionel Sambuc stmt.safe_column_bytes("a"));
515*11be35a1SLionel Sambuc ATF_REQUIRE(!stmt.step());
516*11be35a1SLionel Sambuc }
517*11be35a1SLionel Sambuc
518*11be35a1SLionel Sambuc
519*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(reset);
ATF_TEST_CASE_BODY(reset)520*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(reset)
521*11be35a1SLionel Sambuc {
522*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
523*11be35a1SLionel Sambuc db.exec("CREATE TABLE foo (a TEXT);"
524*11be35a1SLionel Sambuc "INSERT INTO foo VALUES ('foo bar');");
525*11be35a1SLionel Sambuc sqlite::statement stmt = db.create_statement("SELECT * FROM foo");
526*11be35a1SLionel Sambuc ATF_REQUIRE(stmt.step());
527*11be35a1SLionel Sambuc ATF_REQUIRE(!stmt.step());
528*11be35a1SLionel Sambuc stmt.reset();
529*11be35a1SLionel Sambuc ATF_REQUIRE(stmt.step());
530*11be35a1SLionel Sambuc ATF_REQUIRE(!stmt.step());
531*11be35a1SLionel Sambuc }
532*11be35a1SLionel Sambuc
533*11be35a1SLionel Sambuc
534*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(bind__blob);
ATF_TEST_CASE_BODY(bind__blob)535*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(bind__blob)
536*11be35a1SLionel Sambuc {
537*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
538*11be35a1SLionel Sambuc sqlite::statement stmt = db.create_statement("SELECT 3, ?");
539*11be35a1SLionel Sambuc
540*11be35a1SLionel Sambuc const unsigned char blob[] = {0xca, 0xfe};
541*11be35a1SLionel Sambuc stmt.bind(1, sqlite::blob(static_cast< const void* >(blob), 2));
542*11be35a1SLionel Sambuc ATF_REQUIRE(stmt.step());
543*11be35a1SLionel Sambuc ATF_REQUIRE(sqlite::type_integer == stmt.column_type(0));
544*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(3, stmt.column_int(0));
545*11be35a1SLionel Sambuc ATF_REQUIRE(sqlite::type_blob == stmt.column_type(1));
546*11be35a1SLionel Sambuc const unsigned char* ret_blob =
547*11be35a1SLionel Sambuc static_cast< const unsigned char* >(stmt.column_blob(1).memory);
548*11be35a1SLionel Sambuc ATF_REQUIRE(std::memcmp(blob, ret_blob, 2) == 0);
549*11be35a1SLionel Sambuc ATF_REQUIRE(!stmt.step());
550*11be35a1SLionel Sambuc }
551*11be35a1SLionel Sambuc
552*11be35a1SLionel Sambuc
553*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(bind__double);
ATF_TEST_CASE_BODY(bind__double)554*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(bind__double)
555*11be35a1SLionel Sambuc {
556*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
557*11be35a1SLionel Sambuc sqlite::statement stmt = db.create_statement("SELECT 3, ?");
558*11be35a1SLionel Sambuc
559*11be35a1SLionel Sambuc stmt.bind(1, 0.5);
560*11be35a1SLionel Sambuc ATF_REQUIRE(stmt.step());
561*11be35a1SLionel Sambuc ATF_REQUIRE(sqlite::type_integer == stmt.column_type(0));
562*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(3, stmt.column_int(0));
563*11be35a1SLionel Sambuc ATF_REQUIRE(sqlite::type_float == stmt.column_type(1));
564*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(0.5, stmt.column_double(1));
565*11be35a1SLionel Sambuc ATF_REQUIRE(!stmt.step());
566*11be35a1SLionel Sambuc }
567*11be35a1SLionel Sambuc
568*11be35a1SLionel Sambuc
569*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(bind__int);
ATF_TEST_CASE_BODY(bind__int)570*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(bind__int)
571*11be35a1SLionel Sambuc {
572*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
573*11be35a1SLionel Sambuc sqlite::statement stmt = db.create_statement("SELECT 3, ?");
574*11be35a1SLionel Sambuc
575*11be35a1SLionel Sambuc stmt.bind(1, 123);
576*11be35a1SLionel Sambuc ATF_REQUIRE(stmt.step());
577*11be35a1SLionel Sambuc ATF_REQUIRE(sqlite::type_integer == stmt.column_type(0));
578*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(3, stmt.column_int(0));
579*11be35a1SLionel Sambuc ATF_REQUIRE(sqlite::type_integer == stmt.column_type(1));
580*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(123, stmt.column_int(1));
581*11be35a1SLionel Sambuc ATF_REQUIRE(!stmt.step());
582*11be35a1SLionel Sambuc }
583*11be35a1SLionel Sambuc
584*11be35a1SLionel Sambuc
585*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(bind__int64);
ATF_TEST_CASE_BODY(bind__int64)586*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(bind__int64)
587*11be35a1SLionel Sambuc {
588*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
589*11be35a1SLionel Sambuc sqlite::statement stmt = db.create_statement("SELECT 3, ?");
590*11be35a1SLionel Sambuc
591*11be35a1SLionel Sambuc stmt.bind(1, static_cast< int64_t >(4294967419LL));
592*11be35a1SLionel Sambuc ATF_REQUIRE(stmt.step());
593*11be35a1SLionel Sambuc ATF_REQUIRE(sqlite::type_integer == stmt.column_type(0));
594*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(3, stmt.column_int(0));
595*11be35a1SLionel Sambuc ATF_REQUIRE(sqlite::type_integer == stmt.column_type(1));
596*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(4294967419LL, stmt.column_int64(1));
597*11be35a1SLionel Sambuc ATF_REQUIRE(!stmt.step());
598*11be35a1SLionel Sambuc }
599*11be35a1SLionel Sambuc
600*11be35a1SLionel Sambuc
601*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(bind__null);
ATF_TEST_CASE_BODY(bind__null)602*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(bind__null)
603*11be35a1SLionel Sambuc {
604*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
605*11be35a1SLionel Sambuc sqlite::statement stmt = db.create_statement("SELECT 3, ?");
606*11be35a1SLionel Sambuc
607*11be35a1SLionel Sambuc stmt.bind(1, sqlite::null());
608*11be35a1SLionel Sambuc ATF_REQUIRE(stmt.step());
609*11be35a1SLionel Sambuc ATF_REQUIRE(sqlite::type_integer == stmt.column_type(0));
610*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(3, stmt.column_int(0));
611*11be35a1SLionel Sambuc ATF_REQUIRE(sqlite::type_null == stmt.column_type(1));
612*11be35a1SLionel Sambuc ATF_REQUIRE(!stmt.step());
613*11be35a1SLionel Sambuc }
614*11be35a1SLionel Sambuc
615*11be35a1SLionel Sambuc
616*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(bind__text);
ATF_TEST_CASE_BODY(bind__text)617*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(bind__text)
618*11be35a1SLionel Sambuc {
619*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
620*11be35a1SLionel Sambuc sqlite::statement stmt = db.create_statement("SELECT 3, ?");
621*11be35a1SLionel Sambuc
622*11be35a1SLionel Sambuc const std::string str = "Hello";
623*11be35a1SLionel Sambuc stmt.bind(1, str);
624*11be35a1SLionel Sambuc ATF_REQUIRE(stmt.step());
625*11be35a1SLionel Sambuc ATF_REQUIRE(sqlite::type_integer == stmt.column_type(0));
626*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(3, stmt.column_int(0));
627*11be35a1SLionel Sambuc ATF_REQUIRE(sqlite::type_text == stmt.column_type(1));
628*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(str, stmt.column_text(1));
629*11be35a1SLionel Sambuc ATF_REQUIRE(!stmt.step());
630*11be35a1SLionel Sambuc }
631*11be35a1SLionel Sambuc
632*11be35a1SLionel Sambuc
633*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(bind__text__transient);
ATF_TEST_CASE_BODY(bind__text__transient)634*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(bind__text__transient)
635*11be35a1SLionel Sambuc {
636*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
637*11be35a1SLionel Sambuc sqlite::statement stmt = db.create_statement("SELECT 3, :foo");
638*11be35a1SLionel Sambuc
639*11be35a1SLionel Sambuc {
640*11be35a1SLionel Sambuc const std::string str = "Hello";
641*11be35a1SLionel Sambuc stmt.bind(":foo", str);
642*11be35a1SLionel Sambuc }
643*11be35a1SLionel Sambuc
644*11be35a1SLionel Sambuc ATF_REQUIRE(stmt.step());
645*11be35a1SLionel Sambuc ATF_REQUIRE(sqlite::type_integer == stmt.column_type(0));
646*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(3, stmt.column_int(0));
647*11be35a1SLionel Sambuc ATF_REQUIRE(sqlite::type_text == stmt.column_type(1));
648*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(std::string("Hello"), stmt.column_text(1));
649*11be35a1SLionel Sambuc ATF_REQUIRE(!stmt.step());
650*11be35a1SLionel Sambuc }
651*11be35a1SLionel Sambuc
652*11be35a1SLionel Sambuc
653*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(bind__by_name);
ATF_TEST_CASE_BODY(bind__by_name)654*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(bind__by_name)
655*11be35a1SLionel Sambuc {
656*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
657*11be35a1SLionel Sambuc sqlite::statement stmt = db.create_statement("SELECT 3, :foo");
658*11be35a1SLionel Sambuc
659*11be35a1SLionel Sambuc const std::string str = "Hello";
660*11be35a1SLionel Sambuc stmt.bind(":foo", str);
661*11be35a1SLionel Sambuc ATF_REQUIRE(stmt.step());
662*11be35a1SLionel Sambuc ATF_REQUIRE(sqlite::type_integer == stmt.column_type(0));
663*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(3, stmt.column_int(0));
664*11be35a1SLionel Sambuc ATF_REQUIRE(sqlite::type_text == stmt.column_type(1));
665*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(str, stmt.column_text(1));
666*11be35a1SLionel Sambuc ATF_REQUIRE(!stmt.step());
667*11be35a1SLionel Sambuc }
668*11be35a1SLionel Sambuc
669*11be35a1SLionel Sambuc
670*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(bind_parameter_count);
ATF_TEST_CASE_BODY(bind_parameter_count)671*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(bind_parameter_count)
672*11be35a1SLionel Sambuc {
673*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
674*11be35a1SLionel Sambuc sqlite::statement stmt = db.create_statement("SELECT 3, ?, ?");
675*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(2, stmt.bind_parameter_count());
676*11be35a1SLionel Sambuc }
677*11be35a1SLionel Sambuc
678*11be35a1SLionel Sambuc
679*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(bind_parameter_index);
ATF_TEST_CASE_BODY(bind_parameter_index)680*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(bind_parameter_index)
681*11be35a1SLionel Sambuc {
682*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
683*11be35a1SLionel Sambuc sqlite::statement stmt = db.create_statement("SELECT 3, :foo, ?, :bar");
684*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(1, stmt.bind_parameter_index(":foo"));
685*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(3, stmt.bind_parameter_index(":bar"));
686*11be35a1SLionel Sambuc }
687*11be35a1SLionel Sambuc
688*11be35a1SLionel Sambuc
689*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(bind_parameter_name);
ATF_TEST_CASE_BODY(bind_parameter_name)690*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(bind_parameter_name)
691*11be35a1SLionel Sambuc {
692*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
693*11be35a1SLionel Sambuc sqlite::statement stmt = db.create_statement("SELECT 3, :foo, ?, :bar");
694*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(":foo", stmt.bind_parameter_name(1));
695*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(":bar", stmt.bind_parameter_name(3));
696*11be35a1SLionel Sambuc }
697*11be35a1SLionel Sambuc
698*11be35a1SLionel Sambuc
699*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(clear_bindings);
ATF_TEST_CASE_BODY(clear_bindings)700*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(clear_bindings)
701*11be35a1SLionel Sambuc {
702*11be35a1SLionel Sambuc sqlite::database db = sqlite::database::in_memory();
703*11be35a1SLionel Sambuc sqlite::statement stmt = db.create_statement("SELECT 3, ?");
704*11be35a1SLionel Sambuc
705*11be35a1SLionel Sambuc stmt.bind(1, 5);
706*11be35a1SLionel Sambuc ATF_REQUIRE(stmt.step());
707*11be35a1SLionel Sambuc ATF_REQUIRE(sqlite::type_integer == stmt.column_type(0));
708*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(3, stmt.column_int(0));
709*11be35a1SLionel Sambuc ATF_REQUIRE(sqlite::type_integer == stmt.column_type(1));
710*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(5, stmt.column_int(1));
711*11be35a1SLionel Sambuc stmt.clear_bindings();
712*11be35a1SLionel Sambuc stmt.reset();
713*11be35a1SLionel Sambuc
714*11be35a1SLionel Sambuc ATF_REQUIRE(stmt.step());
715*11be35a1SLionel Sambuc ATF_REQUIRE(sqlite::type_integer == stmt.column_type(0));
716*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(3, stmt.column_int(0));
717*11be35a1SLionel Sambuc ATF_REQUIRE(sqlite::type_null == stmt.column_type(1));
718*11be35a1SLionel Sambuc
719*11be35a1SLionel Sambuc ATF_REQUIRE(!stmt.step());
720*11be35a1SLionel Sambuc }
721*11be35a1SLionel Sambuc
722*11be35a1SLionel Sambuc
ATF_INIT_TEST_CASES(tcs)723*11be35a1SLionel Sambuc ATF_INIT_TEST_CASES(tcs)
724*11be35a1SLionel Sambuc {
725*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, step__ok);
726*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, step__many);
727*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, step__fail);
728*11be35a1SLionel Sambuc
729*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, step_without_results__ok);
730*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, step_without_results__fail);
731*11be35a1SLionel Sambuc
732*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, column_count);
733*11be35a1SLionel Sambuc
734*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, column_name__ok);
735*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, column_name__fail);
736*11be35a1SLionel Sambuc
737*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, column_type__ok);
738*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, column_type__out_of_range);
739*11be35a1SLionel Sambuc
740*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, column_id__ok);
741*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, column_id__missing);
742*11be35a1SLionel Sambuc
743*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, column_blob);
744*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, column_double);
745*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, column_int__ok);
746*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, column_int__overflow);
747*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, column_int64);
748*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, column_text);
749*11be35a1SLionel Sambuc
750*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, column_bytes__blob);
751*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, column_bytes__text);
752*11be35a1SLionel Sambuc
753*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, safe_column_blob__ok);
754*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, safe_column_blob__fail);
755*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, safe_column_double__ok);
756*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, safe_column_double__fail);
757*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, safe_column_int__ok);
758*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, safe_column_int__fail);
759*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, safe_column_int64__ok);
760*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, safe_column_int64__fail);
761*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, safe_column_text__ok);
762*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, safe_column_text__fail);
763*11be35a1SLionel Sambuc
764*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, safe_column_bytes__ok__blob);
765*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, safe_column_bytes__ok__text);
766*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, safe_column_bytes__fail);
767*11be35a1SLionel Sambuc
768*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, reset);
769*11be35a1SLionel Sambuc
770*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, bind__blob);
771*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, bind__double);
772*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, bind__int64);
773*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, bind__int);
774*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, bind__null);
775*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, bind__text);
776*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, bind__text__transient);
777*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, bind__by_name);
778*11be35a1SLionel Sambuc
779*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, bind_parameter_count);
780*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, bind_parameter_index);
781*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, bind_parameter_name);
782*11be35a1SLionel Sambuc
783*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, clear_bindings);
784*11be35a1SLionel Sambuc }
785