4
* Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
4
* Copyright (C) 2009 Tim Marston <tim@ed.am>
6
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.
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.
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.
7
* See http://ed.am/dev/sqlite3cc for more information.
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
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
19
19
* You should have received a copy of the GNU Lesser General Public License
20
20
* along with this program. If not, see <http://www.gnu.org/licenses/>.
28
28
#include <boost/utility.hpp>
29
29
#include <boost/lexical_cast.hpp>
30
30
#include <sqlite3cc/exception.h>
45
* The statement class represents an SQL statement. It is the base class for
53
* The statement class represents an SQL statement. It is the base class for
46
54
* both the command and the query classes, which should be used for those
47
* purposes. The basic_statement class its self has protected instantiation.
55
* purposes. The basic_statement class its self has protected instantiation.
49
57
class basic_statement
51
private boost::noncopyable
53
59
//______________________________________________________________________________
58
* Constructor that provides a database upon which to act and the SQL
64
* Constructor that provides a connection upon which to act and the SQL
60
* @param database a reference to a database
67
* @param connection a reference to a connection
61
68
* @param sql an SQL statement in UTF-8
63
70
explicit basic_statement(
71
connection &connection,
65
72
const std::string &sql );
68
* Constructor that provides a database upon which to act.
69
* @param database a reference to a database
70
* @param sql an SQL statement in UTF-8
75
* Constructor that provides a connection upon which to act.
77
* @param connection a reference to a connection
72
79
explicit basic_statement(
80
connection &connection );
75
virtual ~basic_statement() throw( );
82
virtual ~basic_statement();
77
84
//______________________________________________________________________________
78
85
// public interface
115
123
template< class T >
117
125
unsigned int index,
128
// bind as text (applying the type affinity of the underlying column)
120
129
std::string string_value = boost::lexical_cast< std::string >( value );
121
130
return sqlite3_bind_text( _handle, index, string_value.c_str(),
122
131
string_value.length(), SQLITE_TRANSIENT );
126
* Bind a string value to the SQL statement via it's index where the value
127
* of that string will not change for the duration of the statement. This is
128
* more optimal because sqlite will not have to make it's own copy of the
130
* @param index the index of the parameter to bind to
131
* @param value the invariant string value
132
* @returns an sqlite error code
133
* @see sqlite3_bind_text()
138
unsigned int value_length );
141
* Bind a string value to the SQL statement via it's index where the value
142
* of that string will not change for the duration of the statement. This is
143
* more optimal because sqlite will not have to make it's own copy of the
145
* @param index the index of the parameter to bind to
146
* @param value the invariant string value
147
* @returns an sqlite error code
148
* @see sqlite3_bind_text()
155
* Bind a string value to the SQL statement via it's index where the value
156
* of that string will not change for the duration of the statement. This is
157
* more optimal because sqlite will not have to make it's own copy of the
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()
166
const std::string &value );
169
* Bind a NULL value to the SQL statement via it's index.
170
* @param index the index of the parameter to bind to
171
* @returns an sqlite error code
172
* @see sqlite3_bind_null()
175
unsigned int index );
178
* Bind a value to the SQL statement via a named parameter. This template
179
* will take a variety of data types and bind them as text. This is how
180
* sqlite internally stores the data anyway, so always binding as text just
181
* means we do the conversion instead of sqlite and is no less efficient.
135
* Bind a value to the SQL statement via a named parameter.
182
137
* @param name the named parameter to bind to
183
138
* @param value the value to bind
184
139
* @returns an sqlite error code
187
142
template< class T >
189
144
const std::string &name,
192
147
return bind( bind_parameter_index( name ), value );
196
* Bind a string value to the SQL statement via a named parameter where the
197
* string value will not change for the duration of the statement. This
198
* prevents a copy of the string being taken.
199
* @param name the named parameter to bind to
200
* @param value the invariant string value
201
* @returns an sqlite error code
202
* @see sqlite3_bind_text()
205
const std::string &name,
207
unsigned int value_length );
210
* Bind a string value to the SQL statement via a named parameter where the
211
* string value will not change for the duration of the statement. This
212
* prevents a copy of the string being taken.
213
* @param name the named parameter to bind to
214
* @param value the invariant string value
215
* @returns an sqlite error code
216
* @see sqlite3_bind_text()
219
const std::string &name,
223
* Bind a string value to the SQL statement via a named parameter where the
224
* string value will not change for the duration of the statement. This
225
* prevents a copy of the string being taken.
226
* @param name the named parameter to bind to
227
* @param value the invariant string value
228
* @returns an sqlite error code
229
* @see sqlite3_bind_text()
232
const std::string &name,
233
const std::string &value );
236
* Bind a NULL value to the SQL statement via a named parameter.
237
* @param name the named parameter to bind to
238
* @returns an sqlite error code
239
* @see sqlite3_bind_null()
242
const std::string &name );
245
151
* Stream operator is used to bind values to parameters automatically, in
246
* ascending order. In addition, the null, set_index() and execute auto-
247
* binding manipulators can be used.
152
* ascending order. In addition, the null and set_index() auto-binding
153
* manipulators can be used.
248
155
* @param value a value to bind
250
157
template< class T >
251
158
basic_statement &operator <<(
254
int error_code = bind( _bind_index, value );
255
if( error_code != SQLITE_OK ) throw sqlite_error( error_code );
161
int code = bind( _bind_index, value );
162
if( code != SQLITE_OK ) throw sqlite_error( _connection, code );
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
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()
182
unsigned int value_length );
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
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()
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
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()
212
const std::string &value );
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.
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()
226
const std::string &name,
228
unsigned int value_length );
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.
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()
241
const std::string &name,
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.
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()
255
const std::string &name,
256
const std::string &value );
259
* Bind a NULL value to the SQL statement via it's index.
261
* @param index the index of the parameter to bind to
262
* @returns an sqlite error code
263
* @see sqlite3_bind_null()
266
unsigned int index );
269
* Bind a NULL value to the SQL statement via a named parameter.
271
* @param name the named parameter to bind to
272
* @returns an sqlite error code
273
* @see sqlite3_bind_null()
276
const std::string &name );
279
* Bind a string value as a blob to the SQL statement via its index.
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()
290
unsigned int value_length );
293
* Bind a string value as a blob to the SQL statement via its index.
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()
302
const std::string &value );
305
* Bind a string value as a blob to the SQL statement via a named parameter.
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()
314
const std::string &name,
316
unsigned int value_length );
319
* Bind a string value as a blob to the SQL statement via a named parameter.
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()
327
const std::string &name,
328
const std::string &value );
260
330
//______________________________________________________________________________
261
331
// implementation
267
337
* Finalise an SQL statement.
268
339
* @returns an sqlite error code
269
340
* @see sqlite3_finalize()
274
* Step through one execution cycle of the SQL statement. If this is an SQL
275
* statement that doesn't return any rows, only one cycle is required,
276
* otherwise, each cycle will return another row
277
* @return an sqlite error code
278
* @see sqlite3_step()
283
* Get the index number of a named parameter
345
* Get the index number of a named parameter.
284
347
* @param parameter name
285
348
* @return index of named parameter
287
350
int bind_parameter_index(
288
351
const std::string &name );
290
/** the database upon which to act */
356
* @return sqlite error code
357
* @see sqlite3_step()
361
/** the connection upon which to act */
362
connection &_connection;
293
364
/** the statement handle */
294
365
sqlite3_stmt *_handle;
298
367
/** index used when auto-binding */
299
368
unsigned int _bind_index;
304
// template specialisations for statement::operator <<()
306
basic_statement &basic_statement::operator << < _null_t >(
309
basic_statement &basic_statement::operator << < _exec_t >(
312
basic_statement &basic_statement::operator << < _set_index_t >(
313
const _set_index_t &t );
373
// template specialisations for basic_statement::operator <<()
375
basic_statement &basic_statement::operator << < detail::null_t >(
376
const detail::null_t & );
378
basic_statement &basic_statement::operator << < detail::set_index_t >(
379
const detail::set_index_t &t );
381
basic_statement &basic_statement::operator << < detail::blob_t >(
382
const detail::blob_t &t );
385
// template specialisations for basic_statement::bind()
387
int basic_statement::bind< int >(
391
int basic_statement::bind< double >(
393
const double &value );
396
} // namespace detail
316
399
} // namespace sqlite