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