/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

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
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
 
53
55
 * purposes.  The basic_statement class its self has protected instantiation.
54
56
 */
55
57
class basic_statement
56
 
        :
57
 
        private boost::noncopyable
58
58
{
59
59
//______________________________________________________________________________
60
60
//                                                                 instantiation
113
113
        int clear_bindings();
114
114
 
115
115
        /**
116
 
         * Bind a value to the SQL statement via it's index.  This template will
117
 
         * take a variety of data types and bind them as text.  This is how sqlite
118
 
         * internally stores the data anyway, so always binding as text just means
119
 
         * we do the conversion instead of sqlite and is no less efficient.
 
116
         * Bind a value to the SQL statement via it's index.
120
117
         *
121
118
         * @param index the index of the parameter to bind to
122
119
         * @param value the value to bind
126
123
        template< class T >
127
124
        int bind(
128
125
                unsigned int index,
129
 
                T value )
 
126
                const T &value )
130
127
        {
 
128
                // bind as text (applying the type affinity of the underlying column)
131
129
                std::string string_value = boost::lexical_cast< std::string >( value );
132
130
                return sqlite3_bind_text( _handle, index, string_value.c_str(),
133
131
                        string_value.length(), SQLITE_TRANSIENT );
134
132
        }
135
133
 
136
134
        /**
137
 
         * Bind a string value to the SQL statement via it's index where the value
138
 
         * of that string will not change for the duration of the statement.  This
139
 
         * is more optimal because sqlite will not have to take it's own copy of the
140
 
         * data.
141
 
         *
142
 
         * @param index the index of the parameter to bind to
143
 
         * @param value the invariant string value
144
 
         * @param value_length the length of the string including zero-terminator
145
 
         * @returns an sqlite error code
146
 
         * @see sqlite3_bind_text()
147
 
         */
148
 
        int bind_static(
149
 
                unsigned int index,
150
 
                const char *value,
151
 
                unsigned int value_length );
152
 
 
153
 
        /**
154
 
         * Bind a string value to the SQL statement via it's index where the value
155
 
         * of that string will not change for the duration of the statement.  This
156
 
         * is more optimal because sqlite will not have to take it's own copy of the
157
 
         * data.
158
 
         *
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 char *value );
167
 
 
168
 
        /**
169
 
         * Bind a string value to the SQL statement via it's index where the value
170
 
         * of that string will not change for the duration of the statement.  This
171
 
         * is more optimal because sqlite will not have to take it's own copy of the
172
 
         * data.
173
 
         *
174
 
         * @param index the index of the parameter to bind to
175
 
         * @param value the invariant string value
176
 
         * @returns an sqlite error code
177
 
         * @see sqlite3_bind_text()
178
 
         */
179
 
        int bind_static(
180
 
                unsigned int index,
181
 
                const std::string &value );
182
 
 
183
 
        /**
184
 
         * Bind a NULL value to the SQL statement via it's index.
185
 
         *
186
 
         * @param index the index of the parameter to bind to
187
 
         * @returns an sqlite error code
188
 
         * @see sqlite3_bind_null()
189
 
         */
190
 
        int bind_null(
191
 
                unsigned int index );
192
 
 
193
 
        /**
194
 
         * Bind a value to the SQL statement via a named parameter.  This template
195
 
         * will take a variety of data types and bind them as text.  This is how
196
 
         * sqlite internally stores the data anyway, so always binding as text just
197
 
         * 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.
198
136
         *
199
137
         * @param name the named parameter to bind to
200
138
         * @param value the value to bind
204
142
        template< class T >
205
143
        int bind(
206
144
                const std::string &name,
207
 
                T value )
 
145
                const T &value )
208
146
        {
209
147
                return bind( bind_parameter_index( name ), value );
210
148
        }
211
149
 
212
150
        /**
213
 
         * Bind a string value to the SQL statement via a named parameter where the
214
 
         * string value will not change for the duration of the statement.  This
215
 
         * prevents sqlite from taking its own copy of the string.
216
 
         *
217
 
         * @param name the named parameter to bind to
218
 
         * @param value the invariant string value
219
 
         * @param value_length the length of the string including zero-terminator
220
 
         * @returns an sqlite error code
221
 
         * @see sqlite3_bind_text()
222
 
         */
223
 
        int bind_static(
224
 
                const std::string &name,
225
 
                const char *value,
226
 
                unsigned int value_length );
227
 
 
228
 
        /**
229
 
         * Bind a string value to the SQL statement via a named parameter where the
230
 
         * string value will not change for the duration of the statement.  This
231
 
         * prevents a copy of the string being taken.
232
 
         *
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 char *value );
241
 
 
242
 
        /**
243
 
         * Bind a string value to the SQL statement via a named parameter where the
244
 
         * string value will not change for the duration of the statement.  This
245
 
         * prevents a copy of the string being taken.
246
 
         *
247
 
         * @param name the named parameter to bind to
248
 
         * @param value the invariant string value
249
 
         * @returns an sqlite error code
250
 
         * @see sqlite3_bind_text()
251
 
         */
252
 
        int bind_static(
253
 
                const std::string &name,
254
 
                const std::string &value );
255
 
 
256
 
        /**
257
 
         * Bind a NULL value to the SQL statement via a named parameter.
258
 
         *
259
 
         * @param name the named parameter to bind to
260
 
         * @returns an sqlite error code
261
 
         * @see sqlite3_bind_null()
262
 
         */
263
 
        int bind_null(
264
 
                const std::string &name );
265
 
 
266
 
        /**
267
151
         * Stream operator is used to bind values to parameters automatically, in
268
152
         * ascending order.  In addition, the null and set_index() auto-binding
269
153
         * manipulators can be used.
280
164
                return *this;
281
165
        }
282
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
 
283
330
//______________________________________________________________________________
284
331
//                                                                implementation
285
332
protected:
330
377
template< >
331
378
basic_statement &basic_statement::operator << < detail::set_index_t >(
332
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 );
333
394
 
334
395
 
335
396
} // namespace detail