1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
/*
* test-database.cc
*
* Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
*
* This file is part of sqlite3cc (hereafter referred to as "this program").
* See http://www.waxworlds.org/edam/software/sqlite3cc for more information.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <sqlite3cc.h>
#include <boost/filesystem.hpp>
#define DBFILE "test.db"
int main()
{
// delete any existing test database
if( boost::filesystem::exists( DBFILE ) )
boost::filesystem::remove( DBFILE );
// open database
sqlite::database db( DBFILE );
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// test basic statement, via command class
sqlite::command c1( db,
"CREATE TABLE pets ( "
"name TEXT PRIMARY KEY, "
"age INTEGER "
")" );
c1.step();
// test basic_statement bind() calls, via command
sqlite::command c2( db, "INSERT INTO pets VALUES( ?, ? )" );
c2.bind( 1, "billy" ); c2.bind( 2, 12 ); c2.exec();
sqlite::command c3( db, "INSERT INTO pets VALUES( ?6, ?9 )" );
c3.bind( 9, 16 ); c3.bind( 6, "mopti" ); c3.exec();
sqlite::command c6( db, "INSERT INTO pets VALUES( ?, ? )" );
c6.bind_null( 1 ); c6.bind( 2, 123 ); c6.exec();
sqlite::command c7( db, "INSERT INTO pets VALUES( :foo, :bar )" );
c7.bind( ":foo", "foocat" ); c7.bind( ":bar", 22 ); c7.exec();
// test basic_statement binding via stream operator
sqlite::command c4( db, "INSERT INTO pets VALUES( ?, ? )" );
c4 << "tessa" << 16 << sqlite::exec;
sqlite::command c5( db, "INSERT INTO pets VALUES( ?, ? )" );
c5 << sqlite::null << sqlite::null <<
sqlite::set_index( 1 ) << "tamara" << sqlite::exec;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// test basic queries
sqlite::query q1( db, "SELECT age, age + 3, name "
"FROM pets WHERE NOT age IS NULL "
"ORDER BY age ASC" );
assert( q1.column_count() == 3 );
// test rows
unsigned int age;
std::string name;
q1.step().column( 0, age ); assert( age == 12 );
q1.step().column( 2, name ); assert( name == "mopti" );
// test row stream operator
sqlite::row r1 = q1.step();
r1 >> age; assert( age == 16 );
r1 >> age; assert( age == 19 );
r1 >> name; assert( name == "tessa" );
q1.step() >> age >> sqlite::null >> name;
assert( age == 22 && name == "foocat" );
sqlite::row row = q1.step();
row >> age >> sqlite::null >> sqlite::null; assert( age == 123 );
row >> sqlite::set_index( 1 ) >> age; assert( age == 126 );
// test NULL value handling
assert( row.column_type( 2 ) == SQLITE_NULL );
row.column( 2, age ); assert( age == 0 );
row.column( 2, name ); assert( name == "" );
// test end of results
assert( !q1.step() );
return 0;
}
|