/sqlite3cc

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

« back to all changes in this revision

Viewing changes to include/sqlitepp/statement.hpp

  • Committer: edam
  • Date: 2009-11-25 20:32:34 UTC
  • Revision ID: edam@waxworlds.org-20091125203234-24i7mzd60c7o46py
- initial commit
- basic support for: database, statements, commands, transactions, exceptions
- initial test app
- initial liscence and readme

Show diffs side-by-side

added added

removed removed

1
1
/*
2
 
 * basic_statement.h
3
 
 *
4
 
 * Copyright (C) 2009 Tim Marston <tim@ed.am>
5
 
 *
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.
 
2
 * statement.hpp
 
3
 *
 
4
 * Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
 
5
 *
 
6
 * This file is part of sqlitepp (hereafter referred to as "this program").
 
7
 * See http://www.waxworlds.org/edam/software/sqlitepp 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/>.
21
21
 */
22
22
 
23
 
#ifndef SQLITE3CC_BASIC_STATEMENT_H_
24
 
#define SQLITE3CC_BASIC_STATEMENT_H_
 
23
#ifndef STATEMENT_HPP_
 
24
#define STATEMENT_HPP_
25
25
 
26
26
 
27
27
#include <sqlite3.h>
28
28
#include <boost/utility.hpp>
29
29
#include <boost/lexical_cast.hpp>
30
 
#include <sqlite3cc/exception.h>
31
 
#include <string>
 
30
#include <sqlitepp/exception.hpp>
32
31
 
33
32
 
34
33
namespace sqlite
35
34
{
36
35
 
37
36
 
38
 
class connection;
39
 
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
 
{
50
 
 
51
 
 
52
 
/**
53
 
 * The statement class represents an SQL statement.  It is the base class for
54
 
 * both the command and the query classes, which should be used for those
55
 
 * purposes.  The basic_statement class its self has protected instantiation.
56
 
 */
57
 
class basic_statement
 
37
class database;
 
38
 
 
39
 
 
40
struct _null_t { };
 
41
struct _exec_t { };
 
42
struct _set_index_t { unsigned int _index; };
 
43
 
 
44
/**
 
45
 * Auto-binding manipulator, for use with a statment's stream operator. This
 
46
 * specifies a NULL value to bind to a parameter.
 
47
 */
 
48
extern const _null_t null;
 
49
 
 
50
/**
 
51
 * Auto-binding manipulator, for use with a statment's stream operator. This
 
52
 * indicates that the statement should be executed immediately. Unlike a
 
53
 * statment's exec() method, this will throw on error. Also, it will throw if
 
54
 * the execution returns any result rows.
 
55
 */
 
56
extern const _exec_t exec;
 
57
 
 
58
/**
 
59
 * Auto-binding manipulator, for use with a statment's stream operator. This
 
60
 * manipulator sets the index used to automatically assign values to parameters.
 
61
 * @param index the new index for incremental assignment
 
62
 */
 
63
_set_index_t set_index(
 
64
        unsigned int index );
 
65
 
 
66
 
 
67
/**
 
68
 * The statement class represents an SQL statement. It is the bas class for both
 
69
 * the command and the query classes which should be used for those purposes.
 
70
 * The statement class its self has protected instantiation.
 
71
 */
 
72
class statement
 
73
        :
 
74
        private boost::noncopyable
58
75
{
59
76
//______________________________________________________________________________
60
77
//                                                                 instantiation
61
78
protected:
62
79
 
63
80
        /**
64
 
         * Constructor that provides a connection upon which to act and the SQL
 
81
         * Constructor that provides a database upon which to act and the SQL
65
82
         * statement.
66
 
         *
67
 
         * @param connection a reference to a connection
 
83
         * @param database a reference to the database
68
84
         * @param sql an SQL statement in UTF-8
69
85
         */
70
 
        explicit basic_statement(
71
 
                connection &connection,
 
86
        statement(
 
87
                database &database,
72
88
                const std::string &sql );
73
89
 
74
 
        /**
75
 
         * Constructor that provides a connection upon which to act.
76
 
         *
77
 
         * @param connection a reference to a connection
78
 
         */
79
 
        explicit basic_statement(
80
 
                connection &connection );
81
 
 
82
 
        virtual ~basic_statement();
 
90
        virtual ~statement() throw( );
83
91
 
84
92
//______________________________________________________________________________
85
93
//                                                              public interface
87
95
 
88
96
        /**
89
97
         * Prepare an SQL statement.
90
 
         *
91
98
         * @param sql an SQL statement in UTF-8
92
99
         * @returns an sqlite error code
93
100
         * @see sqlite3_prepare_v2()
94
101
         */
95
 
        virtual int prepare(
 
102
        int prepare(
96
103
                const std::string &sql );
97
104
 
98
105
        /**
99
 
         * Reset the statement, ready to re-execute it.  This does not clear any of
 
106
         * Step through one execution cycle of the SQL statement. If this is an SQL
 
107
         * statement that doesn't return any rows, only one cycle is required,
 
108
         * otherwise, each cycle will return another row
 
109
         * @return an sqlite error code
 
110
         * @see sqlite3_step()
 
111
         */
 
112
        int step();
 
113
 
 
114
        /**
 
115
         * Reset the statement, ready to re-execute it. This does not clear any of
100
116
         * the values bound to the statement.
101
 
         *
102
117
         * @returns an sqlite error code
103
118
         * @see sqlite3_reset()
104
119
         */
105
 
        virtual int reset();
 
120
        int reset();
106
121
 
107
122
        /**
108
123
         * Clears the values bound to a statement to NULL.
109
 
         *
110
124
         * @returns an sqlite error code
111
125
         * @see sqlite3_clear_bindings()
112
126
         */
113
127
        int clear_bindings();
114
128
 
115
129
        /**
116
 
         * Bind a value to the SQL statement via it's index.
117
 
         *
 
130
         * Bind a value to the SQL statement via it's index. This template will take
 
131
         * a variety of data types and bind them as text. This is how sqlite
 
132
         * internally stores the data anyway, so always binding as text just means
 
133
         * we do the conversion instead of sqlite and is no less efficient.
118
134
         * @param index the index of the parameter to bind to
119
135
         * @param value the value to bind
120
136
         * @returns an sqlite error code
123
139
        template< class T >
124
140
        int bind(
125
141
                unsigned int index,
126
 
                const T &value )
 
142
                T value )
127
143
        {
128
 
                // bind as text (applying the type affinity of the underlying column)
129
144
                std::string string_value = boost::lexical_cast< std::string >( value );
130
145
                return sqlite3_bind_text( _handle, index, string_value.c_str(),
131
146
                        string_value.length(), SQLITE_TRANSIENT );
132
147
        }
133
148
 
134
149
        /**
135
 
         * Bind a value to the SQL statement via a named parameter.
136
 
         *
 
150
         * Bind a string value to the SQL statement via it's index where the value
 
151
         * of that string will not change for the duration of the statement. This is
 
152
         * more optimal because sqlite will not have to make it's own copy of the
 
153
         * data.
 
154
         * @param index the index of the parameter to bind to
 
155
         * @param value the invariant string value
 
156
         * @returns an sqlite error code
 
157
         * @see sqlite3_bind_text()
 
158
         */
 
159
        int bind_static(
 
160
                unsigned int index,
 
161
                const char *value,
 
162
                unsigned int value_length );
 
163
 
 
164
        /**
 
165
         * Bind a string value to the SQL statement via it's index where the value
 
166
         * of that string will not change for the duration of the statement. This is
 
167
         * more optimal  because sqlite will not have to make it's own copy of the
 
168
         * data.
 
169
         * @param index the index of the parameter to bind to
 
170
         * @param value the invariant string value
 
171
         * @returns an sqlite error code
 
172
         * @see sqlite3_bind_text()
 
173
         */
 
174
        int bind_static(
 
175
                unsigned int index,
 
176
                const char *value );
 
177
 
 
178
        /**
 
179
         * Bind a string value to the SQL statement via it's index where the value
 
180
         * of that string will not change for the duration of the statement. This is
 
181
         * more optimal because sqlite will not have to make it's own copy of the
 
182
         * data.
 
183
         * @param index the index of the parameter to bind to
 
184
         * @param value the invariant string value
 
185
         * @returns an sqlite error code
 
186
         * @see sqlite3_bind_text()
 
187
         */
 
188
        int bind_static(
 
189
                unsigned int index,
 
190
                const std::string &value );
 
191
 
 
192
        /**
 
193
         * Bind a NULL value to the SQL statement via it's index.
 
194
         * @param index the index of the parameter to bind to
 
195
         * @returns an sqlite error code
 
196
         * @see sqlite3_bind_null()
 
197
         */
 
198
        int bind_null(
 
199
                unsigned int index );
 
200
 
 
201
        /**
 
202
         * Bind a value to the SQL statement via a named parameter. This template
 
203
         * will take a variety of data types and bind them as text. This is how
 
204
         * sqlite internally stores the data anyway, so always binding as text just
 
205
         * means we do the conversion instead of sqlite and is no less efficient.
137
206
         * @param name the named parameter to bind to
138
207
         * @param value the value to bind
139
208
         * @returns an sqlite error code
142
211
        template< class T >
143
212
        int bind(
144
213
                const std::string &name,
145
 
                const T &value )
 
214
                T value )
146
215
        {
147
 
                return bind( bind_parameter_index( name ), value );
 
216
                unsigned int index =
 
217
                        sqlite3_bind_parameter_index( _handle, name.c_str() );
 
218
                return bind( index, value );
148
219
        }
149
220
 
150
221
        /**
 
222
         * Bind a string value to the SQL statement via a named parameter where the
 
223
         * string value will not change for the duration of the statement. This
 
224
         * prevents a copy of the string being taken.
 
225
         * @param name the named parameter to bind to
 
226
         * @param value the invariant string value
 
227
         * @returns an sqlite error code
 
228
         * @see sqlite3_bind_text()
 
229
         */
 
230
        int bind_static(
 
231
                const std::string &name,
 
232
                const char *value,
 
233
                unsigned int value_length );
 
234
 
 
235
        /**
 
236
         * Bind a string value to the SQL statement via a named parameter where the
 
237
         * string value will not change for the duration of the statement. This
 
238
         * prevents a copy of the string being taken.
 
239
         * @param name the named parameter to bind to
 
240
         * @param value the invariant string value
 
241
         * @returns an sqlite error code
 
242
         * @see sqlite3_bind_text()
 
243
         */
 
244
        int bind_static(
 
245
                const std::string &name,
 
246
                const char *value );
 
247
 
 
248
        /**
 
249
         * Bind a string value to the SQL statement via a named parameter where the
 
250
         * string value will not change for the duration of the statement. This
 
251
         * prevents a copy of the string being taken.
 
252
         * @param name the named parameter to bind to
 
253
         * @param value the invariant string value
 
254
         * @returns an sqlite error code
 
255
         * @see sqlite3_bind_text()
 
256
         */
 
257
        int bind_static(
 
258
                const std::string &name,
 
259
                const std::string &value );
 
260
 
 
261
        /**
 
262
         * Bind a NULL value to the SQL statement via a named parameter.
 
263
         * @param name the named parameter to bind to
 
264
         * @returns an sqlite error code
 
265
         * @see sqlite3_bind_null()
 
266
         */
 
267
        int bind_null(
 
268
                const std::string &name );
 
269
 
 
270
        /**
151
271
         * 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
 
         *
 
272
         * ascending order. In addition, the null, execute and set_index() auto-
 
273
         * binding manipulators can be used.
155
274
         * @param value a value to bind
156
275
         */
157
276
        template< class T >
158
 
        basic_statement &operator <<(
159
 
                const T &value )
 
277
        statement &operator <<( T value )
160
278
        {
161
 
                int code = bind( _bind_index, value );
162
 
                if( code != SQLITE_OK ) throw sqlite_error( _connection, code );
 
279
                int error_code = bind( _bind_index, value );
 
280
                if( error_code != SQLITE_OK ) throw sqlite_error( error_code );
163
281
                _bind_index++;
164
282
                return *this;
165
283
        }
166
284
 
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
285
//______________________________________________________________________________
331
286
//                                                                implementation
332
287
protected:
333
288
 
334
 
        friend class row;
335
 
 
336
289
        /**
337
290
         * Finalise an SQL statement.
338
 
         *
339
291
         * @returns an sqlite error code
340
292
         * @see sqlite3_finalize()
341
293
         */
342
294
        int finalize();
343
295
 
344
 
        /**
345
 
         * Get the index number of a named parameter.
346
 
         *
347
 
         * @param parameter name
348
 
         * @return index of named parameter
349
 
         */
350
 
        int bind_parameter_index(
351
 
                const std::string &name );
352
 
 
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;
 
296
private:
 
297
 
 
298
        /** the database upon which to act */
 
299
        database &_database;
363
300
 
364
301
        /** the statement handle */
365
302
        sqlite3_stmt *_handle;
370
307
};
371
308
 
372
309
 
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
397
 
 
398
 
 
399
 
} // namespace sqlite
400
 
 
401
 
 
402
 
#endif /* SQLITE3CC_BASIC_STATEMENT_H_ */
 
310
// template specialisations for statement::operator <<()
 
311
template< >
 
312
statement& statement::operator << < _null_t >(
 
313
        _null_t );
 
314
template< >
 
315
statement& statement::operator << < _exec_t >(
 
316
        _exec_t );
 
317
template< >
 
318
statement& statement::operator << < _set_index_t >(
 
319
        _set_index_t t );
 
320
 
 
321
 
 
322
}
 
323
 
 
324
 
 
325
#endif /* STATEMENT_HPP_ */