/sqlite3cc

To get this branch, use:
bzr branch http://bzr.ed.am/sqlite3cc
1 by edam
- initial commit
1
/*
2 by edam
- further initial development
2
 * query.cc
1 by edam
- initial commit
3
 *
4
 * Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
5
 *
2 by edam
- further initial development
6
 * This file is part of sqlite3cc (hereafter referred to as "this program").
7
 * See http://www.waxworlds.org/edam/software/sqlite3cc for more information.
1 by edam
- initial commit
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Lesser General Public License as published
11
 * by the Free Software Foundation, either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Lesser General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
2 by edam
- further initial development
23
#include <sqlite3cc/query.h>
24
#include <sqlite3cc/row.h>
13 by edam
- made basic_statement::step() protected, for use by query and command only
25
#include <sqlite3cc/database.h>
2 by edam
- further initial development
26
#include <assert.h>
27
28
29
sqlite::query::query(
30
	database &database,
31
	const std::string &sql )
32
	:
9 by edam
- added NEWS
33
	basic_statement( database, sql ),
34
	_next_row( 0 )
2 by edam
- further initial development
35
{
36
	assert( sqlite3_column_count( _handle ) > 0 );
37
}
38
39
40
sqlite::query::query(
41
	database &database )
42
	:
9 by edam
- added NEWS
43
	basic_statement( database ),
44
	_next_row( 0 )
2 by edam
- further initial development
45
{
46
}
47
48
49
int sqlite::query::prepare(
50
	const std::string &sql )
51
{
9 by edam
- added NEWS
52
	_next_row = 0;
13 by edam
- made basic_statement::step() protected, for use by query and command only
53
	int code = basic_statement::prepare( sql );
54
	assert( code != SQLITE_OK || sqlite3_column_count( _handle ) > 0 );
55
	return code;
2 by edam
- further initial development
56
}
57
58
59
sqlite::row sqlite::query::step()
60
{
13 by edam
- made basic_statement::step() protected, for use by query and command only
61
	database::database_mutex_guard lock( _database );
62
63
	int code = basic_statement::step();
64
	if( code == SQLITE_DONE ) return row();
65
	if( code == SQLITE_ROW ) return row( _handle, _next_row++ );
66
67
	throw sqlite_error( _database, code );
2 by edam
- further initial development
68
}
69
70
71
unsigned int sqlite::query::column_count()
72
{
73
	return sqlite3_column_count( _handle );
74
}
9 by edam
- added NEWS
75
76
77
const std::string sqlite::query::column_name(
78
	unsigned int index )
79
{
80
	assert( index <
81
		static_cast< unsigned int >( sqlite3_column_count( _handle ) ) );
82
	return sqlite3_column_name( _handle, index );
83
}
84
85
86
sqlite::query::iterator::iterator(
87
	sqlite::query &query,
88
	bool valid )
89
	:
90
	_query( query )
91
{
92
	if( valid )
93
		increment();
94
	else
95
		_valid = false;
96
}
97
98
99
sqlite::row sqlite::query::iterator::dereference() const
100
{
101
	return sqlite::row( _query._handle, _query._next_row++ );
102
}
103
104
105
void sqlite::query::iterator::increment()
106
{
107
	_valid = _query.step();
108
}
109
110
111
bool sqlite::query::iterator::equal(
112
	sqlite::query::iterator const &other )
113
	const
114
{
115
	return _valid == other._valid;
116
}
117
118
119
sqlite::query::iterator sqlite::query::begin()
120
{
121
	return sqlite::query::iterator( *this, true );
122
}
123
124
125
sqlite::query::iterator sqlite::query::end()
126
{
127
	return sqlite::query::iterator( *this, false );
128
}
13 by edam
- made basic_statement::step() protected, for use by query and command only
129