/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.h
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
#ifndef SQLITE3CC_QUERY_H_
24
#define SQLITE3CC_QUERY_H_
25
26
9 by edam
- added NEWS
27
#include <boost/iterator/iterator_facade.hpp>
2 by edam
- further initial development
28
#include <sqlite3cc/basic_statement.h>
9 by edam
- added NEWS
29
#include <sqlite3cc/row.h>
1 by edam
- initial commit
30
31
32
namespace sqlite
33
{
34
35
36
class query
37
	:
2 by edam
- further initial development
38
	public basic_statement
1 by edam
- initial commit
39
{
2 by edam
- further initial development
40
//______________________________________________________________________________
41
//                                                                 instantiation
42
public:
43
44
	/**
45
	 * Constructor that provides a database upon which to act and the SQL
46
	 * query to execute.
47
	 * @param database a reference to a database
48
	 * @param sql an SQL statement in UTF-8
49
	 */
50
	explicit query(
51
		database &database,
52
		const std::string &sql );
53
54
	/**
55
	 * Constructor that provides a database upon which to act.
56
	 * @param database a reference to a database
57
	 * @param sql an SQL statement in UTF-8
58
	 */
59
	explicit query(
60
		database &database );
61
62
//______________________________________________________________________________
63
//                                                              public interface
64
public:
65
66
	/**
67
	 * Prepare an SQL statement.
68
	 * @param sql an SQL statement in UTF-8
69
	 * @returns an sqlite error code
70
	 * @see sqlite3_prepare_v2()
71
	 */
72
	virtual int prepare(
73
		const std::string &sql );
74
75
	/**
76
	 * Peform a step() and return row object that can be used to retrieve the
77
	 * results.
78
	 * @return a row object
79
	 */
80
	row step();
81
82
	/**
9 by edam
- added NEWS
83
	 * Get the number of columns in the results
84
	 * @see sqlite3_column_count()
2 by edam
- further initial development
85
	 */
86
	unsigned int column_count();
1 by edam
- initial commit
87
9 by edam
- added NEWS
88
	/**
89
	 * Get the name of a column in the results
90
	 * @param index column index
91
	 * @see sqlite3_column_name()
92
	 */
93
	const std::string column_name(
94
		unsigned int index );
95
96
	/**
97
	 * Query iterator which can be used to obtain rows
98
	 */
99
	class iterator
100
		:
101
		public boost::iterator_facade< iterator, row,
102
			boost::single_pass_traversal_tag, row >
103
	{
104
	public:
105
		iterator();
106
		explicit iterator( query &query, bool valid );
107
108
	private:
109
		friend class boost::iterator_core_access;
110
111
		row dereference() const;
112
		void increment();
113
		bool equal( iterator const &other ) const;
114
115
		/** is this iterator still pointing to a valid row? */
116
		bool _valid;
117
118
		/** the query */
119
		query &_query;
120
	};
121
122
	/**
123
	 * Return an iterator to the initial row. Note that creating an iterator
124
	 * causes step() to be called, so it should only be called to begin
125
	 * iterating over the rows and not for comparison.
126
	 * @return a query iterator
127
	 */
128
	iterator begin();
129
130
	/**
131
	 * Return an iterator to after the last row (i.e., an invalid row).
132
	 */
133
	iterator end();
134
135
//______________________________________________________________________________
136
//                                                                implementation
137
private:
138
139
	/** next row index */
140
	unsigned long long _next_row;
141
1 by edam
- initial commit
142
};
143
144
2 by edam
- further initial development
145
} // namespace sqlite
146
147
148
#endif /* SQLITE3CC_QUERY_H_ */