/sqlite3cc

To get this branch, use:
bzr branch http://bzr.ed.am/sqlite3cc
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 * 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( db, "INSERT INTO pets VALUES( ?, ? )" ) <<
		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 );
	name = q1.step().column< std::string >( 2 ); 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 );
	age = row.column< unsigned int >( 2 ); assert( age == 0 );
	row.column( 2, name ); assert( name == "" );

	// test end of results
	assert( !q1.step() );

	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	// test query itterator
	sqlite::query q2( db, "SELECT * FROM pets ORDER BY age DESC" );
	for( sqlite::query::iterator i = q2.begin(); i != q2.end(); i++ ) {
		if( i->index() == 3 )
			assert( i->column< std::string >( 0 ) == "tessa" &&
				i->column< unsigned int >( 1 ) == 16 );
//		for( unsigned int a = 0; a < q2.column_count(); a++ )
//			std::cout << q2.column_name( a ) << "[" <<
//			i->column< std::string >( a ) << "] ";
//		std::cout << "\n";
	}
	assert( q2.column_count() == 2 );
	assert( q2.column_name( 0 ) == "name" );

	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	// test transactions
	{	sqlite::transaction_guard< > t1( db ); }
	{	sqlite::transaction_guard< sqlite::exclusive_transaction > t1( db ); }
	{	sqlite::transaction_guard< sqlite::recursive_transaction > t1( db );
		sqlite::transaction_guard< sqlite::recursive_transaction > t2( db ); }

	// test transaction guard rollback
	{
		sqlite::transaction_guard< > t1( db );
		db.exec( "UPDATE pets SET age = 99" );
	}
	sqlite::query( db, "SELECT age FROM pets ORDER BY age DESC LIMIT 1" )
		.step() >> age; assert( age == 123 );

	// text recursive transactions
	{
		sqlite::transaction_guard< sqlite::recursive_transaction > t1( db );
		db.exec( "UPDATE pets SET age = 66" );

		{
			sqlite::transaction_guard< sqlite::recursive_transaction > t2( db );
			db.exec( "UPDATE pets SET age = 1" );
		}

		sqlite::query( db, "SELECT age FROM pets ORDER BY age DESC LIMIT 1" )
			.step() >> age; assert( age == 66 );
	}
	sqlite::query( db, "SELECT age FROM pets ORDER BY age DESC LIMIT 1" )
		.step() >> age; assert( age == 123 );

	// ok
	return 0;
}