/sqlite3cc

To get this branch, use:
bzr branch http://bzr.ed.am/sqlite3cc

« back to all changes in this revision

Viewing changes to include/sqlite3cc/basic_statement.h

  • Committer: edam
  • Date: 2010-07-29 09:16:26 UTC
  • Revision ID: edam@waxworlds.org-20100729091626-h8fmg0r74eyfo5ae
- fixed error caused by finialising in-progress queries during rollback that were later finaliased by RAII.

Show diffs side-by-side

added added

removed removed

1
1
/*
2
2
 * basic_statement.h
3
3
 *
4
 
 * Copyright (C) 2009 Tim Marston <tim@ed.am>
 
4
 * Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
5
5
 *
6
6
 * This file is part of sqlite3cc (hereafter referred to as "this program").
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.
 
7
 * See http://www.waxworlds.org/edam/software/sqlite3cc for more information.
 
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
18
 *
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>
31
 
#include <string>
32
31
 
33
32
 
34
33
namespace sqlite
35
34
{
36
35
 
37
36
 
38
 
class connection;
 
37
class database;
39
38
class row;
40
39
namespace detail {
41
40
        struct null_t;
42
41
        struct exec_t;
43
42
        struct set_index_t;
44
 
        struct blob_t;
45
43
}
46
44
 
47
45
 
48
 
namespace detail
49
 
{
50
 
 
51
 
 
52
46
/**
53
 
 * The statement class represents an SQL statement.  It is the base class for
 
47
 * The statement class represents an SQL statement. It is the base class for
54
48
 * both the command and the query classes, which should be used for those
55
 
 * purposes.  The basic_statement class its self has protected instantiation.
 
49
 * purposes. The basic_statement class its self has protected instantiation.
56
50
 */
57
51
class basic_statement
 
52
        :
 
53
        private boost::noncopyable
58
54
{
59
55
//______________________________________________________________________________
60
56
//                                                                 instantiation
61
57
protected:
62
58
 
63
59
        /**
64
 
         * Constructor that provides a connection upon which to act and the SQL
 
60
         * Constructor that provides a database upon which to act and the SQL
65
61
         * statement.
66
 
         *
67
 
         * @param connection a reference to a connection
 
62
         * @param database a reference to a database
68
63
         * @param sql an SQL statement in UTF-8
69
64
         */
70
65
        explicit basic_statement(
71
 
                connection &connection,
 
66
                database &database,
72
67
                const std::string &sql );
73
68
 
74
69
        /**
75
 
         * Constructor that provides a connection upon which to act.
76
 
         *
77
 
         * @param connection a reference to a connection
 
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
78
73
         */
79
74
        explicit basic_statement(
80
 
                connection &connection );
 
75
                database &database );
81
76
 
82
77
        virtual ~basic_statement();
83
78
 
87
82
 
88
83
        /**
89
84
         * Prepare an SQL statement.
90
 
         *
91
85
         * @param sql an SQL statement in UTF-8
92
86
         * @returns an sqlite error code
93
87
         * @see sqlite3_prepare_v2()
96
90
                const std::string &sql );
97
91
 
98
92
        /**
99
 
         * Reset the statement, ready to re-execute it.  This does not clear any of
 
93
         * Reset the statement, ready to re-execute it. This does not clear any of
100
94
         * the values bound to the statement.
101
 
         *
102
95
         * @returns an sqlite error code
103
96
         * @see sqlite3_reset()
104
97
         */
105
 
        virtual int reset();
 
98
        int reset();
106
99
 
107
100
        /**
108
101
         * Clears the values bound to a statement to NULL.
109
 
         *
110
102
         * @returns an sqlite error code
111
103
         * @see sqlite3_clear_bindings()
112
104
         */
113
105
        int clear_bindings();
114
106
 
115
107
        /**
116
 
         * Bind a value to the SQL statement via it's index.
117
 
         *
 
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.
118
112
         * @param index the index of the parameter to bind to
119
113
         * @param value the value to bind
120
114
         * @returns an sqlite error code
123
117
        template< class T >
124
118
        int bind(
125
119
                unsigned int index,
126
 
                const T &value )
 
120
                T value )
127
121
        {
128
 
                // bind as text (applying the type affinity of the underlying column)
129
122
                std::string string_value = boost::lexical_cast< std::string >( value );
130
123
                return sqlite3_bind_text( _handle, index, string_value.c_str(),
131
124
                        string_value.length(), SQLITE_TRANSIENT );
132
125
        }
133
126
 
134
127
        /**
135
 
         * Bind a value to the SQL statement via a named parameter.
136
 
         *
 
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.
137
184
         * @param name the named parameter to bind to
138
185
         * @param value the value to bind
139
186
         * @returns an sqlite error code
142
189
        template< class T >
143
190
        int bind(
144
191
                const std::string &name,
145
 
                const T &value )
 
192
                T value )
146
193
        {
147
194
                return bind( bind_parameter_index( name ), value );
148
195
        }
149
196
 
150
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
 
 
246
        /**
151
247
         * Stream operator is used to bind values to parameters automatically, in
152
 
         * ascending order.  In addition, the null and set_index() auto-binding
 
248
         * ascending order. In addition, the null and set_index() auto-binding
153
249
         * manipulators can be used.
154
 
         *
155
250
         * @param value a value to bind
156
251
         */
157
252
        template< class T >
159
254
                const T &value )
160
255
        {
161
256
                int code = bind( _bind_index, value );
162
 
                if( code != SQLITE_OK ) throw sqlite_error( _connection, code );
 
257
                if( code != SQLITE_OK ) throw sqlite_error( _database, code );
163
258
                _bind_index++;
164
259
                return *this;
165
260
        }
166
261
 
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
 
 
330
262
//______________________________________________________________________________
331
263
//                                                                implementation
332
264
protected:
335
267
 
336
268
        /**
337
269
         * Finalise an SQL statement.
338
 
         *
339
270
         * @returns an sqlite error code
340
271
         * @see sqlite3_finalize()
341
272
         */
342
273
        int finalize();
343
274
 
344
275
        /**
345
 
         * Get the index number of a named parameter.
346
 
         *
 
276
         * Get the index number of a named parameter
347
277
         * @param parameter name
348
278
         * @return index of named parameter
349
279
         */
351
281
                const std::string &name );
352
282
 
353
283
        /**
354
 
         * Perform a step.
355
 
         *
 
284
         * Perform a step
356
285
         * @return sqlite error code
357
286
         * @see sqlite3_step()
358
287
         */
359
288
        int step();
360
289
 
361
 
        /** the connection upon which to act */
362
 
        connection &_connection;
 
290
        /** the database upon which to act */
 
291
        database &_database;
363
292
 
364
293
        /** the statement handle */
365
294
        sqlite3_stmt *_handle;
377
306
template< >
378
307
basic_statement &basic_statement::operator << < detail::set_index_t >(
379
308
        const detail::set_index_t &t );
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 );
394
 
 
395
 
 
396
 
} // namespace detail
397
309
 
398
310
 
399
311
} // namespace sqlite