/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
 * basic_statement.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_BASIC_STATEMENT_H_
24
#define SQLITE3CC_BASIC_STATEMENT_H_
1 by edam
- initial commit
25
26
27
#include <sqlite3.h>
28
#include <boost/utility.hpp>
29
#include <boost/lexical_cast.hpp>
2 by edam
- further initial development
30
#include <sqlite3cc/exception.h>
1 by edam
- initial commit
31
32
33
namespace sqlite
34
{
35
36
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
37
class connection;
2 by edam
- further initial development
38
class row;
12 by edam
- moved null_t, exec_t and set_index_t to detail namespace so only their extern instantiations are in the main namespace
39
namespace detail {
40
	struct null_t;
41
	struct exec_t;
42
	struct set_index_t;
43
}
2 by edam
- further initial development
44
45
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
46
namespace detail
47
{
48
49
2 by edam
- further initial development
50
/**
22 by edam
updated email and web addresses
51
 * The statement class represents an SQL statement.  It is the base class for
2 by edam
- further initial development
52
 * both the command and the query classes, which should be used for those
22 by edam
updated email and web addresses
53
 * purposes.  The basic_statement class its self has protected instantiation.
2 by edam
- further initial development
54
 */
55
class basic_statement
1 by edam
- initial commit
56
	:
57
	private boost::noncopyable
58
{
59
//______________________________________________________________________________
60
//                                                                 instantiation
61
protected:
62
63
	/**
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
64
	 * Constructor that provides a connection upon which to act and the SQL
1 by edam
- initial commit
65
	 * statement.
22 by edam
updated email and web addresses
66
	 *
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
67
	 * @param connection a reference to a connection
1 by edam
- initial commit
68
	 * @param sql an SQL statement in UTF-8
69
	 */
2 by edam
- further initial development
70
	explicit basic_statement(
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
71
		connection &connection,
1 by edam
- initial commit
72
		const std::string &sql );
73
2 by edam
- further initial development
74
	/**
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
75
	 * Constructor that provides a connection upon which to act.
22 by edam
updated email and web addresses
76
	 *
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
77
	 * @param connection a reference to a connection
2 by edam
- further initial development
78
	 */
79
	explicit basic_statement(
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
80
		connection &connection );
2 by edam
- further initial development
81
9 by edam
- added NEWS
82
	virtual ~basic_statement();
1 by edam
- initial commit
83
84
//______________________________________________________________________________
85
//                                                              public interface
86
public:
87
88
	/**
89
	 * Prepare an SQL statement.
22 by edam
updated email and web addresses
90
	 *
1 by edam
- initial commit
91
	 * @param sql an SQL statement in UTF-8
92
	 * @returns an sqlite error code
93
	 * @see sqlite3_prepare_v2()
94
	 */
2 by edam
- further initial development
95
	virtual int prepare(
1 by edam
- initial commit
96
		const std::string &sql );
97
98
	/**
22 by edam
updated email and web addresses
99
	 * Reset the statement, ready to re-execute it.  This does not clear any of
1 by edam
- initial commit
100
	 * the values bound to the statement.
22 by edam
updated email and web addresses
101
	 *
1 by edam
- initial commit
102
	 * @returns an sqlite error code
103
	 * @see sqlite3_reset()
104
	 */
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
105
	virtual int reset();
1 by edam
- initial commit
106
107
	/**
108
	 * Clears the values bound to a statement to NULL.
22 by edam
updated email and web addresses
109
	 *
1 by edam
- initial commit
110
	 * @returns an sqlite error code
111
	 * @see sqlite3_clear_bindings()
112
	 */
113
	int clear_bindings();
114
115
	/**
22 by edam
updated email and web addresses
116
	 * Bind a value to the SQL statement via it's index.  This template will
117
	 * take a variety of data types and bind them as text.  This is how sqlite
1 by edam
- initial commit
118
	 * internally stores the data anyway, so always binding as text just means
119
	 * we do the conversion instead of sqlite and is no less efficient.
22 by edam
updated email and web addresses
120
	 *
1 by edam
- initial commit
121
	 * @param index the index of the parameter to bind to
122
	 * @param value the value to bind
123
	 * @returns an sqlite error code
124
	 * @see sqlite3_bind_text()
125
	 */
126
	template< class T >
127
	int bind(
128
		unsigned int index,
129
		T value )
130
	{
131
		std::string string_value = boost::lexical_cast< std::string >( value );
132
		return sqlite3_bind_text( _handle, index, string_value.c_str(),
133
			string_value.length(), SQLITE_TRANSIENT );
134
	}
135
136
	/**
137
	 * Bind a string value to the SQL statement via it's index where the value
22 by edam
updated email and web addresses
138
	 * of that string will not change for the duration of the statement.  This
139
	 * is more optimal because sqlite will not have to take it's own copy of the
1 by edam
- initial commit
140
	 * data.
22 by edam
updated email and web addresses
141
	 *
1 by edam
- initial commit
142
	 * @param index the index of the parameter to bind to
143
	 * @param value the invariant string value
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
144
	 * @param value_length the length of the string including zero-terminator
1 by edam
- initial commit
145
	 * @returns an sqlite error code
146
	 * @see sqlite3_bind_text()
147
	 */
148
	int bind_static(
149
		unsigned int index,
150
		const char *value,
151
		unsigned int value_length );
152
153
	/**
154
	 * Bind a string value to the SQL statement via it's index where the value
22 by edam
updated email and web addresses
155
	 * of that string will not change for the duration of the statement.  This
156
	 * is more optimal because sqlite will not have to take it's own copy of the
1 by edam
- initial commit
157
	 * data.
22 by edam
updated email and web addresses
158
	 *
1 by edam
- initial commit
159
	 * @param index the index of the parameter to bind to
160
	 * @param value the invariant string value
161
	 * @returns an sqlite error code
162
	 * @see sqlite3_bind_text()
163
	 */
164
	int bind_static(
165
		unsigned int index,
166
		const char *value );
167
168
	/**
169
	 * Bind a string value to the SQL statement via it's index where the value
22 by edam
updated email and web addresses
170
	 * of that string will not change for the duration of the statement.  This
171
	 * is more optimal because sqlite will not have to take it's own copy of the
1 by edam
- initial commit
172
	 * data.
22 by edam
updated email and web addresses
173
	 *
1 by edam
- initial commit
174
	 * @param index the index of the parameter to bind to
175
	 * @param value the invariant string value
176
	 * @returns an sqlite error code
177
	 * @see sqlite3_bind_text()
178
	 */
179
	int bind_static(
180
		unsigned int index,
181
		const std::string &value );
182
183
	/**
184
	 * Bind a NULL value to the SQL statement via it's index.
22 by edam
updated email and web addresses
185
	 *
1 by edam
- initial commit
186
	 * @param index the index of the parameter to bind to
187
	 * @returns an sqlite error code
188
	 * @see sqlite3_bind_null()
189
	 */
190
	int bind_null(
191
		unsigned int index );
192
193
	/**
22 by edam
updated email and web addresses
194
	 * Bind a value to the SQL statement via a named parameter.  This template
195
	 * will take a variety of data types and bind them as text.  This is how
1 by edam
- initial commit
196
	 * sqlite internally stores the data anyway, so always binding as text just
197
	 * means we do the conversion instead of sqlite and is no less efficient.
22 by edam
updated email and web addresses
198
	 *
1 by edam
- initial commit
199
	 * @param name the named parameter to bind to
200
	 * @param value the value to bind
201
	 * @returns an sqlite error code
202
	 * @see sqlite3_bind_text()
203
	 */
204
	template< class T >
205
	int bind(
206
		const std::string &name,
207
		T value )
208
	{
2 by edam
- further initial development
209
		return bind( bind_parameter_index( name ), value );
1 by edam
- initial commit
210
	}
211
212
	/**
213
	 * Bind a string value to the SQL statement via a named parameter where the
22 by edam
updated email and web addresses
214
	 * string value will not change for the duration of the statement.  This
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
215
	 * prevents sqlite from taking its own copy of the string.
22 by edam
updated email and web addresses
216
	 *
1 by edam
- initial commit
217
	 * @param name the named parameter to bind to
218
	 * @param value the invariant string value
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
219
	 * @param value_length the length of the string including zero-terminator
1 by edam
- initial commit
220
	 * @returns an sqlite error code
221
	 * @see sqlite3_bind_text()
222
	 */
223
	int bind_static(
224
		const std::string &name,
225
		const char *value,
226
		unsigned int value_length );
227
228
	/**
229
	 * Bind a string value to the SQL statement via a named parameter where the
22 by edam
updated email and web addresses
230
	 * string value will not change for the duration of the statement.  This
1 by edam
- initial commit
231
	 * prevents a copy of the string being taken.
22 by edam
updated email and web addresses
232
	 *
1 by edam
- initial commit
233
	 * @param name the named parameter to bind to
234
	 * @param value the invariant string value
235
	 * @returns an sqlite error code
236
	 * @see sqlite3_bind_text()
237
	 */
238
	int bind_static(
239
		const std::string &name,
240
		const char *value );
241
242
	/**
243
	 * Bind a string value to the SQL statement via a named parameter where the
22 by edam
updated email and web addresses
244
	 * string value will not change for the duration of the statement.  This
1 by edam
- initial commit
245
	 * prevents a copy of the string being taken.
22 by edam
updated email and web addresses
246
	 *
1 by edam
- initial commit
247
	 * @param name the named parameter to bind to
248
	 * @param value the invariant string value
249
	 * @returns an sqlite error code
250
	 * @see sqlite3_bind_text()
251
	 */
252
	int bind_static(
253
		const std::string &name,
254
		const std::string &value );
255
256
	/**
257
	 * Bind a NULL value to the SQL statement via a named parameter.
22 by edam
updated email and web addresses
258
	 *
1 by edam
- initial commit
259
	 * @param name the named parameter to bind to
260
	 * @returns an sqlite error code
261
	 * @see sqlite3_bind_null()
262
	 */
263
	int bind_null(
264
		const std::string &name );
265
14 by edam
- moved basic_statement::operator <<() back to basic_statement and just create another specialisation in command so that it can use sqlite::exec
266
	/**
267
	 * Stream operator is used to bind values to parameters automatically, in
22 by edam
updated email and web addresses
268
	 * ascending order.  In addition, the null and set_index() auto-binding
14 by edam
- moved basic_statement::operator <<() back to basic_statement and just create another specialisation in command so that it can use sqlite::exec
269
	 * manipulators can be used.
22 by edam
updated email and web addresses
270
	 *
14 by edam
- moved basic_statement::operator <<() back to basic_statement and just create another specialisation in command so that it can use sqlite::exec
271
	 * @param value a value to bind
272
	 */
273
	template< class T >
274
	basic_statement &operator <<(
275
		const T &value )
276
	{
277
		int code = bind( _bind_index, value );
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
278
		if( code != SQLITE_OK ) throw sqlite_error( _connection, code );
14 by edam
- moved basic_statement::operator <<() back to basic_statement and just create another specialisation in command so that it can use sqlite::exec
279
		_bind_index++;
280
		return *this;
281
	}
282
1 by edam
- initial commit
283
//______________________________________________________________________________
284
//                                                                implementation
285
protected:
286
2 by edam
- further initial development
287
	friend class row;
288
1 by edam
- initial commit
289
	/**
290
	 * Finalise an SQL statement.
22 by edam
updated email and web addresses
291
	 *
1 by edam
- initial commit
292
	 * @returns an sqlite error code
293
	 * @see sqlite3_finalize()
294
	 */
295
	int finalize();
296
2 by edam
- further initial development
297
	/**
22 by edam
updated email and web addresses
298
	 * Get the index number of a named parameter.
299
	 *
2 by edam
- further initial development
300
	 * @param parameter name
301
	 * @return index of named parameter
302
	 */
303
	int bind_parameter_index(
304
		const std::string &name );
1 by edam
- initial commit
305
13 by edam
- made basic_statement::step() protected, for use by query and command only
306
	/**
22 by edam
updated email and web addresses
307
	 * Perform a step.
308
	 *
13 by edam
- made basic_statement::step() protected, for use by query and command only
309
	 * @return sqlite error code
310
	 * @see sqlite3_step()
311
	 */
312
	int step();
313
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
314
	/** the connection upon which to act */
315
	connection &_connection;
1 by edam
- initial commit
316
317
	/** the statement handle */
318
	sqlite3_stmt *_handle;
319
320
	/** index used when auto-binding */
321
	unsigned int _bind_index;
322
323
};
324
325
14 by edam
- moved basic_statement::operator <<() back to basic_statement and just create another specialisation in command so that it can use sqlite::exec
326
// template specialisations for basic_statement::operator <<()
327
template< >
328
basic_statement &basic_statement::operator << < detail::null_t >(
329
	const detail::null_t & );
330
template< >
331
basic_statement &basic_statement::operator << < detail::set_index_t >(
332
	const detail::set_index_t &t );
333
334
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
335
} // namespace detail
336
337
2 by edam
- further initial development
338
} // namespace sqlite
339
340
341
#endif /* SQLITE3CC_BASIC_STATEMENT_H_ */