/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-23 12:57:07 UTC
  • Revision ID: edam@waxworlds.org-20100723125707-qu9jk9vvg2uewx7t
- cleaned up test-main
- fixed comment type
- made sqlite::exec() throw instead of returning sqlite error code

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
 
namespace detail {
41
 
        struct null_t;
42
 
        struct exec_t;
43
 
        struct set_index_t;
44
 
        struct blob_t;
45
 
}
46
 
 
47
 
 
48
 
namespace detail
49
 
{
 
39
struct _null_t;
 
40
struct _exec_t;
 
41
struct _set_index_t;
50
42
 
51
43
 
52
44
/**
53
 
 * The statement class represents an SQL statement.  It is the base class for
 
45
 * The statement class represents an SQL statement. It is the base class for
54
46
 * both the command and the query classes, which should be used for those
55
 
 * purposes.  The basic_statement class its self has protected instantiation.
 
47
 * purposes. The basic_statement class its self has protected instantiation.
56
48
 */
57
49
class basic_statement
 
50
        :
 
51
        private boost::noncopyable
58
52
{
59
53
//______________________________________________________________________________
60
54
//                                                                 instantiation
61
55
protected:
62
56
 
63
57
        /**
64
 
         * Constructor that provides a connection upon which to act and the SQL
 
58
         * Constructor that provides a database upon which to act and the SQL
65
59
         * statement.
66
 
         *
67
 
         * @param connection a reference to a connection
 
60
         * @param database a reference to a database
68
61
         * @param sql an SQL statement in UTF-8
69
62
         */
70
63
        explicit basic_statement(
71
 
                connection &connection,
 
64
                database &database,
72
65
                const std::string &sql );
73
66
 
74
67
        /**
75
 
         * Constructor that provides a connection upon which to act.
76
 
         *
77
 
         * @param connection a reference to a connection
 
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
78
71
         */
79
72
        explicit basic_statement(
80
 
                connection &connection );
 
73
                database &database );
81
74
 
82
75
        virtual ~basic_statement();
83
76
 
87
80
 
88
81
        /**
89
82
         * Prepare an SQL statement.
90
 
         *
91
83
         * @param sql an SQL statement in UTF-8
92
84
         * @returns an sqlite error code
93
85
         * @see sqlite3_prepare_v2()
96
88
                const std::string &sql );
97
89
 
98
90
        /**
99
 
         * Reset the statement, ready to re-execute it.  This does not clear any of
 
91
         * Reset the statement, ready to re-execute it. This does not clear any of
100
92
         * the values bound to the statement.
101
 
         *
102
93
         * @returns an sqlite error code
103
94
         * @see sqlite3_reset()
104
95
         */
105
 
        virtual int reset();
 
96
        int reset();
106
97
 
107
98
        /**
108
99
         * Clears the values bound to a statement to NULL.
109
 
         *
110
100
         * @returns an sqlite error code
111
101
         * @see sqlite3_clear_bindings()
112
102
         */
113
103
        int clear_bindings();
114
104
 
115
105
        /**
116
 
         * Bind a value to the SQL statement via it's index.
117
 
         *
 
106
         * Bind a value to the SQL statement via it's index. This template will take
 
107
         * a variety of data types and bind them as text. This is how sqlite
 
108
         * internally stores the data anyway, so always binding as text just means
 
109
         * we do the conversion instead of sqlite and is no less efficient.
118
110
         * @param index the index of the parameter to bind to
119
111
         * @param value the value to bind
120
112
         * @returns an sqlite error code
123
115
        template< class T >
124
116
        int bind(
125
117
                unsigned int index,
126
 
                const T &value )
 
118
                T value )
127
119
        {
128
 
                // bind as text (applying the type affinity of the underlying column)
129
120
                std::string string_value = boost::lexical_cast< std::string >( value );
130
121
                return sqlite3_bind_text( _handle, index, string_value.c_str(),
131
122
                        string_value.length(), SQLITE_TRANSIENT );
132
123
        }
133
124
 
134
125
        /**
135
 
         * Bind a value to the SQL statement via a named parameter.
136
 
         *
 
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
 
129
         * data.
 
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()
 
134
         */
 
135
        int bind_static(
 
136
                unsigned int index,
 
137
                const char *value,
 
138
                unsigned int value_length );
 
139
 
 
140
        /**
 
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
 
144
         * data.
 
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()
 
149
         */
 
150
        int bind_static(
 
151
                unsigned int index,
 
152
                const char *value );
 
153
 
 
154
        /**
 
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
 
158
         * data.
 
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 std::string &value );
 
167
 
 
168
        /**
 
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()
 
173
         */
 
174
        int bind_null(
 
175
                unsigned int index );
 
176
 
 
177
        /**
 
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.
137
182
         * @param name the named parameter to bind to
138
183
         * @param value the value to bind
139
184
         * @returns an sqlite error code
142
187
        template< class T >
143
188
        int bind(
144
189
                const std::string &name,
145
 
                const T &value )
 
190
                T value )
146
191
        {
147
192
                return bind( bind_parameter_index( name ), value );
148
193
        }
149
194
 
150
195
        /**
 
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()
 
203
         */
 
204
        int bind_static(
 
205
                const std::string &name,
 
206
                const char *value,
 
207
                unsigned int value_length );
 
208
 
 
209
        /**
 
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()
 
217
         */
 
218
        int bind_static(
 
219
                const std::string &name,
 
220
                const char *value );
 
221
 
 
222
        /**
 
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()
 
230
         */
 
231
        int bind_static(
 
232
                const std::string &name,
 
233
                const std::string &value );
 
234
 
 
235
        /**
 
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()
 
240
         */
 
241
        int bind_null(
 
242
                const std::string &name );
 
243
 
 
244
        /**
151
245
         * Stream operator is used to bind values to parameters automatically, in
152
 
         * ascending order.  In addition, the null and set_index() auto-binding
153
 
         * manipulators can be used.
154
 
         *
 
246
         * ascending order. In addition, the null, set_index() and execute auto-
 
247
         * binding manipulators can be used.
155
248
         * @param value a value to bind
156
249
         */
157
250
        template< class T >
158
251
        basic_statement &operator <<(
159
252
                const T &value )
160
253
        {
161
 
                int code = bind( _bind_index, value );
162
 
                if( code != SQLITE_OK ) throw sqlite_error( _connection, code );
 
254
                int error_code = bind( _bind_index, value );
 
255
                if( error_code != SQLITE_OK ) throw sqlite_error( error_code );
163
256
                _bind_index++;
164
257
                return *this;
165
258
        }
166
259
 
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
260
//______________________________________________________________________________
331
261
//                                                                implementation
332
262
protected:
335
265
 
336
266
        /**
337
267
         * Finalise an SQL statement.
338
 
         *
339
268
         * @returns an sqlite error code
340
269
         * @see sqlite3_finalize()
341
270
         */
342
271
        int finalize();
343
272
 
344
273
        /**
345
 
         * Get the index number of a named parameter.
346
 
         *
 
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()
 
279
         */
 
280
        int step();
 
281
 
 
282
        /**
 
283
         * Get the index number of a named parameter
347
284
         * @param parameter name
348
285
         * @return index of named parameter
349
286
         */
350
287
        int bind_parameter_index(
351
288
                const std::string &name );
352
289
 
353
 
        /**
354
 
         * Perform a step.
355
 
         *
356
 
         * @return sqlite error code
357
 
         * @see sqlite3_step()
358
 
         */
359
 
        int step();
360
 
 
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;
366
295
 
 
296
private:
 
297
 
367
298
        /** index used when auto-binding */
368
299
        unsigned int _bind_index;
369
300
 
370
301
};
371
302
 
372
303
 
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 );
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
 
304
// template specialisations for statement::operator <<()
 
305
template< >
 
306
basic_statement &basic_statement::operator << < _null_t >(
 
307
        const _null_t & );
 
308
template< >
 
309
basic_statement &basic_statement::operator << < _exec_t >(
 
310
        const _exec_t & );
 
311
template< >
 
312
basic_statement &basic_statement::operator << < _set_index_t >(
 
313
        const _set_index_t &t );
397
314
 
398
315
 
399
316
} // namespace sqlite