/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>
42 by Tim Marston
improved support for blobs: added bind_blob(), added blob_t for binding stream
31
#include <string>
1 by edam
- initial commit
32
33
34
namespace sqlite
35
{
36
37
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
38
class connection;
2 by edam
- further initial development
39
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
40
namespace detail {
41
	struct null_t;
42
	struct exec_t;
43
	struct set_index_t;
42 by Tim Marston
improved support for blobs: added bind_blob(), added blob_t for binding stream
44
	struct blob_t;
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
45
}
2 by edam
- further initial development
46
47
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
48
namespace detail
49
{
50
51
2 by edam
- further initial development
52
/**
22 by edam
updated email and web addresses
53
 * The statement class represents an SQL statement.  It is the base class for
2 by edam
- further initial development
54
 * both the command and the query classes, which should be used for those
22 by edam
updated email and web addresses
55
 * purposes.  The basic_statement class its self has protected instantiation.
2 by edam
- further initial development
56
 */
57
class basic_statement
1 by edam
- initial commit
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
	/**
42 by Tim Marston
improved support for blobs: added bind_blob(), added blob_t for binding stream
116
	 * Bind a value to the SQL statement via it's index.
22 by edam
updated email and web addresses
117
	 *
1 by edam
- initial commit
118
	 * @param index the index of the parameter to bind to
119
	 * @param value the value to bind
120
	 * @returns an sqlite error code
121
	 * @see sqlite3_bind_text()
122
	 */
123
	template< class T >
124
	int bind(
125
		unsigned int index,
42 by Tim Marston
improved support for blobs: added bind_blob(), added blob_t for binding stream
126
		const T &value )
1 by edam
- initial commit
127
	{
42 by Tim Marston
improved support for blobs: added bind_blob(), added blob_t for binding stream
128
		// bind as text (applying the type affinity of the underlying column)
1 by edam
- initial commit
129
		std::string string_value = boost::lexical_cast< std::string >( value );
130
		return sqlite3_bind_text( _handle, index, string_value.c_str(),
131
			string_value.length(), SQLITE_TRANSIENT );
132
	}
133
134
	/**
42 by Tim Marston
improved support for blobs: added bind_blob(), added blob_t for binding stream
135
	 * Bind a value to the SQL statement via a named parameter.
22 by edam
updated email and web addresses
136
	 *
1 by edam
- initial commit
137
	 * @param name the named parameter to bind to
138
	 * @param value the value to bind
139
	 * @returns an sqlite error code
140
	 * @see sqlite3_bind_text()
141
	 */
142
	template< class T >
143
	int bind(
144
		const std::string &name,
42 by Tim Marston
improved support for blobs: added bind_blob(), added blob_t for binding stream
145
		const T &value )
1 by edam
- initial commit
146
	{
2 by edam
- further initial development
147
		return bind( bind_parameter_index( name ), value );
1 by edam
- initial commit
148
	}
149
150
	/**
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
151
	 * Stream operator is used to bind values to parameters automatically, in
22 by edam
updated email and web addresses
152
	 * 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
153
	 * manipulators can be used.
22 by edam
updated email and web addresses
154
	 *
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
155
	 * @param value a value to bind
156
	 */
157
	template< class T >
158
	basic_statement &operator <<(
159
		const T &value )
160
	{
161
		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?)
162
		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
163
		_bind_index++;
164
		return *this;
165
	}
166
42 by Tim Marston
improved support for blobs: added bind_blob(), added blob_t for binding stream
167
	/**
168
	 * Bind a string value to the SQL statement via it's index where the value
169
	 * of that string will not change for the duration of the statement.  This
170
	 * is more optimal because sqlite will not have to take it's own copy of the
171
	 * data.
172
	 *
173
	 * @param index the index of the parameter to bind to
174
	 * @param value the invariant string value
175
	 * @param value_length the length of the string including zero-terminator
176
	 * @returns an sqlite error code
177
	 * @see sqlite3_bind_text()
178
	 */
179
	int bind_static(
180
		unsigned int index,
181
		const char *value,
182
		unsigned int value_length );
183
184
	/**
185
	 * Bind a string value to the SQL statement via it's index where the value
186
	 * of that string will not change for the duration of the statement.  This
187
	 * is more optimal because sqlite will not have to take it's own copy of the
188
	 * data.
189
	 *
190
	 * @param index the index of the parameter to bind to
191
	 * @param value the invariant string value
192
	 * @returns an sqlite error code
193
	 * @see sqlite3_bind_text()
194
	 */
195
	int bind_static(
196
		unsigned int index,
197
		const char *value );
198
199
	/**
200
	 * Bind a string value to the SQL statement via it's index where the value
201
	 * of that string will not change for the duration of the statement.  This
202
	 * is more optimal because sqlite will not have to take it's own copy of the
203
	 * data.
204
	 *
205
	 * @param index the index of the parameter to bind to
206
	 * @param value the invariant string value
207
	 * @returns an sqlite error code
208
	 * @see sqlite3_bind_text()
209
	 */
210
	int bind_static(
211
		unsigned int index,
212
		const std::string &value );
213
214
	/**
215
	 * Bind a string value to the SQL statement via a named parameter where the
216
	 * string value will not change for the duration of the statement.  This
217
	 * prevents sqlite from taking its own copy of the string.
218
	 *
219
	 * @param name the named parameter to bind to
220
	 * @param value the invariant string value
221
	 * @param value_length the length of the string including zero-terminator
222
	 * @returns an sqlite error code
223
	 * @see sqlite3_bind_text()
224
	 */
225
	int bind_static(
226
		const std::string &name,
227
		const char *value,
228
		unsigned int value_length );
229
230
	/**
231
	 * Bind a string value to the SQL statement via a named parameter where the
232
	 * string value will not change for the duration of the statement.  This
233
	 * prevents a copy of the string being taken.
234
	 *
235
	 * @param name the named parameter to bind to
236
	 * @param value the invariant string value
237
	 * @returns an sqlite error code
238
	 * @see sqlite3_bind_text()
239
	 */
240
	int bind_static(
241
		const std::string &name,
242
		const char *value );
243
244
	/**
245
	 * Bind a string value to the SQL statement via a named parameter where the
246
	 * string value will not change for the duration of the statement.  This
247
	 * prevents a copy of the string being taken.
248
	 *
249
	 * @param name the named parameter to bind to
250
	 * @param value the invariant string value
251
	 * @returns an sqlite error code
252
	 * @see sqlite3_bind_text()
253
	 */
254
	int bind_static(
255
		const std::string &name,
256
		const std::string &value );
257
258
	/**
259
	 * Bind a NULL value to the SQL statement via it's index.
260
	 *
261
	 * @param index the index of the parameter to bind to
262
	 * @returns an sqlite error code
263
	 * @see sqlite3_bind_null()
264
	 */
265
	int bind_null(
266
		unsigned int index );
267
268
	/**
269
	 * Bind a NULL value to the SQL statement via a named parameter.
270
	 *
271
	 * @param name the named parameter to bind to
272
	 * @returns an sqlite error code
273
	 * @see sqlite3_bind_null()
274
	 */
275
	int bind_null(
276
		const std::string &name );
277
278
	/**
279
	 * Bind a string value as a blob to the SQL statement via its index.
280
	 *
281
	 * @param name the named parameter to bind to
282
	 * @param value the blob data value
283
	 * @param value_length the length of the blob data
284
	 * @returns an sqlite error code
285
	 * @see sqlite3_bind_blob()
286
	 */
287
	int bind_blob(
288
		unsigned int index,
289
		const char *value,
290
		unsigned int value_length );
291
292
	/**
293
	 * Bind a string value as a blob to the SQL statement via its index.
294
	 *
295
	 * @param name the named parameter to bind to
296
	 * @param value the blob data value
297
	 * @returns an sqlite error code
298
	 * @see sqlite3_bind_blob()
299
	 */
300
	int bind_blob(
301
		unsigned int index,
302
		const std::string &value );
303
304
	/**
305
	 * Bind a string value as a blob to the SQL statement via a named parameter.
306
	 *
307
	 * @param name the named parameter to bind to
308
	 * @param value the blob data value
309
	 * @param value_length the length of the blob data
310
	 * @returns an sqlite error code
311
	 * @see sqlite3_bind_blob()
312
	 */
313
	int bind_blob(
314
		const std::string &name,
315
		const char *value,
316
		unsigned int value_length );
317
318
	/**
319
	 * Bind a string value as a blob to the SQL statement via a named parameter.
320
	 *
321
	 * @param name the named parameter to bind to
322
	 * @param value the blob data value
323
	 * @returns an sqlite error code
324
	 * @see sqlite3_bind_blob()
325
	 */
326
	int bind_blob(
327
		const std::string &name,
328
		const std::string &value );
329
1 by edam
- initial commit
330
//______________________________________________________________________________
331
//                                                                implementation
332
protected:
333
2 by edam
- further initial development
334
	friend class row;
335
1 by edam
- initial commit
336
	/**
337
	 * Finalise an SQL statement.
22 by edam
updated email and web addresses
338
	 *
1 by edam
- initial commit
339
	 * @returns an sqlite error code
340
	 * @see sqlite3_finalize()
341
	 */
342
	int finalize();
343
2 by edam
- further initial development
344
	/**
22 by edam
updated email and web addresses
345
	 * Get the index number of a named parameter.
346
	 *
2 by edam
- further initial development
347
	 * @param parameter name
348
	 * @return index of named parameter
349
	 */
350
	int bind_parameter_index(
351
		const std::string &name );
1 by edam
- initial commit
352
13 by edam
- made basic_statement::step() protected, for use by query and command only
353
	/**
22 by edam
updated email and web addresses
354
	 * Perform a step.
355
	 *
13 by edam
- made basic_statement::step() protected, for use by query and command only
356
	 * @return sqlite error code
357
	 * @see sqlite3_step()
358
	 */
359
	int step();
360
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
361
	/** the connection upon which to act */
362
	connection &_connection;
1 by edam
- initial commit
363
364
	/** the statement handle */
365
	sqlite3_stmt *_handle;
366
367
	/** index used when auto-binding */
368
	unsigned int _bind_index;
369
370
};
371
372
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
373
// template specialisations for basic_statement::operator <<()
374
template< >
375
basic_statement &basic_statement::operator << < detail::null_t >(
376
	const detail::null_t & );
377
template< >
378
basic_statement &basic_statement::operator << < detail::set_index_t >(
379
	const detail::set_index_t &t );
42 by Tim Marston
improved support for blobs: added bind_blob(), added blob_t for binding stream
380
template< >
381
basic_statement &basic_statement::operator << < detail::blob_t >(
382
	const detail::blob_t &t );
383
384
385
// template specialisations for basic_statement::bind()
386
template< >
387
int basic_statement::bind< int >(
388
	unsigned int index,
389
	const int &value );
390
template< >
391
int basic_statement::bind< double >(
392
	unsigned int index,
393
	const double &value );
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
394
395
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
396
} // namespace detail
397
398
2 by edam
- further initial development
399
} // namespace sqlite
400
401
402
#endif /* SQLITE3CC_BASIC_STATEMENT_H_ */