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