/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: Tim Marston
  • Date: 2015-02-26 08:59:36 UTC
  • Revision ID: tim@ed.am-20150226085936-xe43in8bfz6f6nxb
fixed some missing/incorrect includes

Show diffs side-by-side

added added

removed removed

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