/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
 *
22 by edam
updated email and web addresses
4
 * Copyright (C) 2009 Tim Marston <tim@ed.am>
1 by edam
- initial commit
5
 *
2 by edam
- further initial development
6
 * This file is part of sqlite3cc (hereafter referred to as "this program").
22 by edam
updated email and web addresses
7
 * See http://ed.am/dev/sqlite3cc for more information.
8
 *
9
 * This program is free software: you can redistribute it and/or modify it under
10
 * the terms of the GNU Lesser General Public License as published by the Free
11
 * Software Foundation, either version 3 of the License, or (at your option) any
12
 * later version.
13
 *
14
 * This program is distributed in the hope that it will be useful, but WITHOUT
15
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16
 * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
17
 * details.
1 by edam
- initial commit
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
	:
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
38
	public detail::basic_statement
1 by edam
- initial commit
39
{
2 by edam
- further initial development
40
//______________________________________________________________________________
41
//                                                                 instantiation
42
public:
43
44
	/**
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
45
	 * Constructor that provides a connection upon which to act and the SQL
2 by edam
- further initial development
46
	 * query to execute.
22 by edam
updated email and web addresses
47
	 *
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
48
	 * @param connection a reference to a connection
2 by edam
- further initial development
49
	 * @param sql an SQL statement in UTF-8
50
	 */
51
	explicit query(
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
52
		connection &connection,
2 by edam
- further initial development
53
		const std::string &sql );
54
55
	/**
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
56
	 * Constructor that provides a connection upon which to act.
22 by edam
updated email and web addresses
57
	 *
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
58
	 * @param connection a reference to a connection
2 by edam
- further initial development
59
	 */
60
	explicit query(
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
61
		connection &connection );
2 by edam
- further initial development
62
63
//______________________________________________________________________________
64
//                                                              public interface
65
public:
66
67
	/**
68
	 * Prepare an SQL statement.
22 by edam
updated email and web addresses
69
	 *
2 by edam
- further initial development
70
	 * @param sql an SQL statement in UTF-8
71
	 * @returns an sqlite error code
72
	 * @see sqlite3_prepare_v2()
73
	 */
74
	virtual int prepare(
75
		const std::string &sql );
76
77
	/**
22 by edam
updated email and web addresses
78
	 * Reset the statement, ready to re-execute it.  This does not clear any of
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
79
	 * the values bound to the statement.
22 by edam
updated email and web addresses
80
	 *
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
81
	 * @returns an sqlite error code
82
	 * @see sqlite3_reset()
83
	 */
84
	virtual int reset();
85
86
	/**
13 by edam
- made basic_statement::step() protected, for use by query and command only
87
	 * Perform a step() and return row object that can be used to retrieve the
2 by edam
- further initial development
88
	 * results.
22 by edam
updated email and web addresses
89
	 *
2 by edam
- further initial development
90
	 * @return a row object
91
	 */
92
	row step();
93
94
	/**
22 by edam
updated email and web addresses
95
	 * Get the number of columns in the results.
96
	 *
9 by edam
- added NEWS
97
	 * @see sqlite3_column_count()
2 by edam
- further initial development
98
	 */
99
	unsigned int column_count();
1 by edam
- initial commit
100
9 by edam
- added NEWS
101
	/**
22 by edam
updated email and web addresses
102
	 * Get the name of a column in the results.
103
	 *
9 by edam
- added NEWS
104
	 * @param index column index
105
	 * @see sqlite3_column_name()
106
	 */
107
	const std::string column_name(
108
		unsigned int index );
109
110
	/**
22 by edam
updated email and web addresses
111
	 * Gets the number of results.  Be aware that this merely step()s over the
112
	 * results and counts them, which is something you could do yourself.  This
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
113
	 * method is intended to be used for convenience, where you only need know
22 by edam
updated email and web addresses
114
	 * the number of results and not what they are.  You could also execute some
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
115
	 * SQL like this: "SELECT COUNT(*) ...", but this only causes SQLite to
22 by edam
updated email and web addresses
116
	 * internally do the same counting as this method does.  Also note that this
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
117
	 * method reset()s the query.
31 by Tim Marston
added description of return parameter to header
118
	 *
119
	 * @returns the number of results
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
120
	 */
121
	unsigned long long num_results();
122
123
	/**
22 by edam
updated email and web addresses
124
	 * Query iterator which can be used to obtain rows.
9 by edam
- added NEWS
125
	 */
126
	class iterator
127
		:
128
		public boost::iterator_facade< iterator, row,
129
			boost::single_pass_traversal_tag, row >
130
	{
131
	public:
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
132
		explicit iterator( query &query, bool step );
9 by edam
- added NEWS
133
134
	private:
135
		friend class boost::iterator_core_access;
136
137
		row dereference() const;
138
		void increment();
139
		bool equal( iterator const &other ) const;
140
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
141
		/** the current row */
142
		row _row;
9 by edam
- added NEWS
143
144
		/** the query */
145
		query &_query;
146
	};
147
148
	/**
22 by edam
updated email and web addresses
149
	 * Get an iterator to the initial row.  Note that creating an iterator
9 by edam
- added NEWS
150
	 * causes step() to be called, so it should only be called to begin
151
	 * iterating over the rows and not for comparison.
22 by edam
updated email and web addresses
152
	 *
9 by edam
- added NEWS
153
	 * @return a query iterator
154
	 */
155
	iterator begin();
156
157
	/**
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
158
	 * Get an iterator to after the last row (i.e., an invalid row).
22 by edam
updated email and web addresses
159
	 *
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
160
	 * @return an invalid query iterator
9 by edam
- added NEWS
161
	 */
162
	iterator end();
163
164
//______________________________________________________________________________
165
//                                                                implementation
166
private:
167
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
168
	/** next row number */
169
	unsigned long long _next_row_number;
9 by edam
- added NEWS
170
1 by edam
- initial commit
171
};
172
173
2 by edam
- further initial development
174
} // namespace sqlite
175
176
177
#endif /* SQLITE3CC_QUERY_H_ */