/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>
45 by Tim Marston
fixed some missing/incorrect includes
30
#include <string>
1 by edam
- initial commit
31
32
33
namespace sqlite
34
{
35
36
37
class query
38
	:
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
39
	public detail::basic_statement
1 by edam
- initial commit
40
{
2 by edam
- further initial development
41
//______________________________________________________________________________
42
//                                                                 instantiation
43
public:
44
45
	/**
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
46
	 * Constructor that provides a connection upon which to act and the SQL
2 by edam
- further initial development
47
	 * query to execute.
22 by edam
updated email and web addresses
48
	 *
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
49
	 * @param connection a reference to a connection
2 by edam
- further initial development
50
	 * @param sql an SQL statement in UTF-8
51
	 */
52
	explicit query(
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
53
		connection &connection,
2 by edam
- further initial development
54
		const std::string &sql );
55
56
	/**
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
57
	 * Constructor that provides a connection upon which to act.
22 by edam
updated email and web addresses
58
	 *
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
59
	 * @param connection a reference to a connection
2 by edam
- further initial development
60
	 */
61
	explicit query(
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
62
		connection &connection );
2 by edam
- further initial development
63
64
//______________________________________________________________________________
65
//                                                              public interface
66
public:
67
68
	/**
69
	 * Prepare an SQL statement.
22 by edam
updated email and web addresses
70
	 *
2 by edam
- further initial development
71
	 * @param sql an SQL statement in UTF-8
72
	 * @returns an sqlite error code
73
	 * @see sqlite3_prepare_v2()
74
	 */
75
	virtual int prepare(
76
		const std::string &sql );
77
78
	/**
22 by edam
updated email and web addresses
79
	 * 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?)
80
	 * the values bound to the statement.
22 by edam
updated email and web addresses
81
	 *
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
82
	 * @returns an sqlite error code
83
	 * @see sqlite3_reset()
84
	 */
85
	virtual int reset();
86
87
	/**
13 by edam
- made basic_statement::step() protected, for use by query and command only
88
	 * Perform a step() and return row object that can be used to retrieve the
2 by edam
- further initial development
89
	 * results.
22 by edam
updated email and web addresses
90
	 *
2 by edam
- further initial development
91
	 * @return a row object
92
	 */
93
	row step();
94
95
	/**
22 by edam
updated email and web addresses
96
	 * Get the number of columns in the results.
97
	 *
9 by edam
- added NEWS
98
	 * @see sqlite3_column_count()
2 by edam
- further initial development
99
	 */
100
	unsigned int column_count();
1 by edam
- initial commit
101
9 by edam
- added NEWS
102
	/**
22 by edam
updated email and web addresses
103
	 * Get the name of a column in the results.
104
	 *
9 by edam
- added NEWS
105
	 * @param index column index
106
	 * @see sqlite3_column_name()
107
	 */
108
	const std::string column_name(
109
		unsigned int index );
110
111
	/**
22 by edam
updated email and web addresses
112
	 * Gets the number of results.  Be aware that this merely step()s over the
113
	 * 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?)
114
	 * method is intended to be used for convenience, where you only need know
22 by edam
updated email and web addresses
115
	 * 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?)
116
	 * SQL like this: "SELECT COUNT(*) ...", but this only causes SQLite to
22 by edam
updated email and web addresses
117
	 * 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?)
118
	 * method reset()s the query.
31 by Tim Marston
added description of return parameter to header
119
	 *
120
	 * @returns the number of results
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
121
	 */
122
	unsigned long long num_results();
123
124
	/**
22 by edam
updated email and web addresses
125
	 * Query iterator which can be used to obtain rows.
9 by edam
- added NEWS
126
	 */
127
	class iterator
128
		:
129
		public boost::iterator_facade< iterator, row,
130
			boost::single_pass_traversal_tag, row >
131
	{
132
	public:
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
133
		explicit iterator( query &query, bool step );
9 by edam
- added NEWS
134
135
	private:
136
		friend class boost::iterator_core_access;
137
138
		row dereference() const;
139
		void increment();
140
		bool equal( iterator const &other ) const;
141
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
142
		/** the current row */
143
		row _row;
9 by edam
- added NEWS
144
145
		/** the query */
146
		query &_query;
147
	};
148
149
	/**
22 by edam
updated email and web addresses
150
	 * Get an iterator to the initial row.  Note that creating an iterator
9 by edam
- added NEWS
151
	 * causes step() to be called, so it should only be called to begin
152
	 * iterating over the rows and not for comparison.
22 by edam
updated email and web addresses
153
	 *
9 by edam
- added NEWS
154
	 * @return a query iterator
155
	 */
156
	iterator begin();
157
158
	/**
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
159
	 * Get an iterator to after the last row (i.e., an invalid row).
22 by edam
updated email and web addresses
160
	 *
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
161
	 * @return an invalid query iterator
9 by edam
- added NEWS
162
	 */
163
	iterator end();
164
165
//______________________________________________________________________________
166
//                                                                implementation
167
private:
168
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
169
	/** next row number */
170
	unsigned long long _next_row_number;
9 by edam
- added NEWS
171
1 by edam
- initial commit
172
};
173
174
2 by edam
- further initial development
175
} // namespace sqlite
176
177
178
#endif /* SQLITE3CC_QUERY_H_ */