/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: 2012-01-23 13:46:27 UTC
  • Revision ID: edam@waxworlds.org-20120123134627-i6hi9aftfvwgp8vw
updated autotols stuff

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
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
 
50
48
 
51
49
 
52
50
/**
53
 
 * The statement class represents an SQL statement.  It is the base class for
 
51
 * The statement class represents an SQL statement. It is the base class for
54
52
 * both the command and the query classes, which should be used for those
55
 
 * purposes.  The basic_statement class its self has protected instantiation.
 
53
 * purposes. The basic_statement class its self has protected instantiation.
56
54
 */
57
55
class basic_statement
 
56
        :
 
57
        private boost::noncopyable
58
58
{
59
59
//______________________________________________________________________________
60
60
//                                                                 instantiation
63
63
        /**
64
64
         * Constructor that provides a connection upon which to act and the SQL
65
65
         * statement.
66
 
         *
67
66
         * @param connection a reference to a connection
68
67
         * @param sql an SQL statement in UTF-8
69
68
         */
73
72
 
74
73
        /**
75
74
         * Constructor that provides a connection upon which to act.
76
 
         *
77
75
         * @param connection a reference to a connection
78
76
         */
79
77
        explicit basic_statement(
87
85
 
88
86
        /**
89
87
         * Prepare an SQL statement.
90
 
         *
91
88
         * @param sql an SQL statement in UTF-8
92
89
         * @returns an sqlite error code
93
90
         * @see sqlite3_prepare_v2()
96
93
                const std::string &sql );
97
94
 
98
95
        /**
99
 
         * Reset the statement, ready to re-execute it.  This does not clear any of
 
96
         * Reset the statement, ready to re-execute it. This does not clear any of
100
97
         * the values bound to the statement.
101
 
         *
102
98
         * @returns an sqlite error code
103
99
         * @see sqlite3_reset()
104
100
         */
106
102
 
107
103
        /**
108
104
         * Clears the values bound to a statement to NULL.
109
 
         *
110
105
         * @returns an sqlite error code
111
106
         * @see sqlite3_clear_bindings()
112
107
         */
113
108
        int clear_bindings();
114
109
 
115
110
        /**
116
 
         * Bind a value to the SQL statement via it's index.
117
 
         *
 
111
         * Bind a value to the SQL statement via it's index. This template will take
 
112
         * a variety of data types and bind them as text. This is how sqlite
 
113
         * internally stores the data anyway, so always binding as text just means
 
114
         * we do the conversion instead of sqlite and is no less efficient.
118
115
         * @param index the index of the parameter to bind to
119
116
         * @param value the value to bind
120
117
         * @returns an sqlite error code
123
120
        template< class T >
124
121
        int bind(
125
122
                unsigned int index,
126
 
                const T &value )
 
123
                T value )
127
124
        {
128
 
                // bind as text (applying the type affinity of the underlying column)
129
125
                std::string string_value = boost::lexical_cast< std::string >( value );
130
126
                return sqlite3_bind_text( _handle, index, string_value.c_str(),
131
127
                        string_value.length(), SQLITE_TRANSIENT );
132
128
        }
133
129
 
134
130
        /**
135
 
         * Bind a value to the SQL statement via a named parameter.
136
 
         *
 
131
         * Bind a string value to the SQL statement via it's index where the value
 
132
         * of that string will not change for the duration of the statement. This is
 
133
         * more optimal because sqlite will not have to take it's own copy of the
 
134
         * data.
 
135
         * @param index the index of the parameter to bind to
 
136
         * @param value the invariant string value
 
137
         * @param value_length the length of the string including zero-terminator
 
138
         * @returns an sqlite error code
 
139
         * @see sqlite3_bind_text()
 
140
         */
 
141
        int bind_static(
 
142
                unsigned int index,
 
143
                const char *value,
 
144
                unsigned int value_length );
 
145
 
 
146
        /**
 
147
         * Bind a string value to the SQL statement via it's index where the value
 
148
         * of that string will not change for the duration of the statement. This is
 
149
         * more optimal  because sqlite will not have to take it's own copy of the
 
150
         * data.
 
151
         * @param index the index of the parameter to bind to
 
152
         * @param value the invariant string value
 
153
         * @returns an sqlite error code
 
154
         * @see sqlite3_bind_text()
 
155
         */
 
156
        int bind_static(
 
157
                unsigned int index,
 
158
                const char *value );
 
159
 
 
160
        /**
 
161
         * Bind a string value to the SQL statement via it's index where the value
 
162
         * of that string will not change for the duration of the statement. This is
 
163
         * more optimal because sqlite will not have to take it's own copy of the
 
164
         * data.
 
165
         * @param index the index of the parameter to bind to
 
166
         * @param value the invariant string value
 
167
         * @returns an sqlite error code
 
168
         * @see sqlite3_bind_text()
 
169
         */
 
170
        int bind_static(
 
171
                unsigned int index,
 
172
                const std::string &value );
 
173
 
 
174
        /**
 
175
         * Bind a NULL value to the SQL statement via it's index.
 
176
         * @param index the index of the parameter to bind to
 
177
         * @returns an sqlite error code
 
178
         * @see sqlite3_bind_null()
 
179
         */
 
180
        int bind_null(
 
181
                unsigned int index );
 
182
 
 
183
        /**
 
184
         * Bind a value to the SQL statement via a named parameter. This template
 
185
         * will take a variety of data types and bind them as text. This is how
 
186
         * sqlite internally stores the data anyway, so always binding as text just
 
187
         * means we do the conversion instead of sqlite and is no less efficient.
137
188
         * @param name the named parameter to bind to
138
189
         * @param value the value to bind
139
190
         * @returns an sqlite error code
142
193
        template< class T >
143
194
        int bind(
144
195
                const std::string &name,
145
 
                const T &value )
 
196
                T value )
146
197
        {
147
198
                return bind( bind_parameter_index( name ), value );
148
199
        }
149
200
 
150
201
        /**
 
202
         * Bind a string value to the SQL statement via a named parameter where the
 
203
         * string value will not change for the duration of the statement. This
 
204
         * prevents sqlite from taking its own copy of the string.
 
205
         * @param name the named parameter to bind to
 
206
         * @param value the invariant string value
 
207
         * @param value_length the length of the string including zero-terminator
 
208
         * @returns an sqlite error code
 
209
         * @see sqlite3_bind_text()
 
210
         */
 
211
        int bind_static(
 
212
                const std::string &name,
 
213
                const char *value,
 
214
                unsigned int value_length );
 
215
 
 
216
        /**
 
217
         * Bind a string value to the SQL statement via a named parameter where the
 
218
         * string value will not change for the duration of the statement. This
 
219
         * prevents a copy of the string being taken.
 
220
         * @param name the named parameter to bind to
 
221
         * @param value the invariant string value
 
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
 
 
229
        /**
 
230
         * Bind a string value to the SQL statement via a named parameter where the
 
231
         * string value will not change for the duration of the statement. This
 
232
         * prevents a copy of the string being taken.
 
233
         * @param name the named parameter to bind to
 
234
         * @param value the invariant string value
 
235
         * @returns an sqlite error code
 
236
         * @see sqlite3_bind_text()
 
237
         */
 
238
        int bind_static(
 
239
                const std::string &name,
 
240
                const std::string &value );
 
241
 
 
242
        /**
 
243
         * Bind a NULL value to the SQL statement via a named parameter.
 
244
         * @param name the named parameter to bind to
 
245
         * @returns an sqlite error code
 
246
         * @see sqlite3_bind_null()
 
247
         */
 
248
        int bind_null(
 
249
                const std::string &name );
 
250
 
 
251
        /**
151
252
         * Stream operator is used to bind values to parameters automatically, in
152
 
         * ascending order.  In addition, the null and set_index() auto-binding
 
253
         * ascending order. In addition, the null and set_index() auto-binding
153
254
         * manipulators can be used.
154
 
         *
155
255
         * @param value a value to bind
156
256
         */
157
257
        template< class T >
164
264
                return *this;
165
265
        }
166
266
 
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
267
//______________________________________________________________________________
331
268
//                                                                implementation
332
269
protected:
335
272
 
336
273
        /**
337
274
         * Finalise an SQL statement.
338
 
         *
339
275
         * @returns an sqlite error code
340
276
         * @see sqlite3_finalize()
341
277
         */
342
278
        int finalize();
343
279
 
344
280
        /**
345
 
         * Get the index number of a named parameter.
346
 
         *
 
281
         * Get the index number of a named parameter
347
282
         * @param parameter name
348
283
         * @return index of named parameter
349
284
         */
351
286
                const std::string &name );
352
287
 
353
288
        /**
354
 
         * Perform a step.
355
 
         *
 
289
         * Perform a step
356
290
         * @return sqlite error code
357
291
         * @see sqlite3_step()
358
292
         */
377
311
template< >
378
312
basic_statement &basic_statement::operator << < detail::set_index_t >(
379
313
        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
314
 
395
315
 
396
316
} // namespace detail