/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>
 
30
#include <sqlitepp/exception.hpp>
31
31
 
32
32
 
33
33
namespace sqlite
34
34
{
35
35
 
36
36
 
37
 
class connection;
38
 
class row;
39
 
namespace detail {
40
 
        struct null_t;
41
 
        struct exec_t;
42
 
        struct set_index_t;
43
 
}
44
 
 
45
 
 
46
 
namespace detail
47
 
{
48
 
 
49
 
 
50
 
/**
51
 
 * The statement class represents an SQL statement.  It is the base class for
52
 
 * both the command and the query classes, which should be used for those
53
 
 * purposes.  The basic_statement class its self has protected instantiation.
54
 
 */
55
 
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
56
75
{
57
76
//______________________________________________________________________________
58
77
//                                                                 instantiation
59
78
protected:
60
79
 
61
80
        /**
62
 
         * 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
63
82
         * statement.
64
 
         *
65
 
         * @param connection a reference to a connection
 
83
         * @param database a reference to the database
66
84
         * @param sql an SQL statement in UTF-8
67
85
         */
68
 
        explicit basic_statement(
69
 
                connection &connection,
 
86
        statement(
 
87
                database &database,
70
88
                const std::string &sql );
71
89
 
72
 
        /**
73
 
         * Constructor that provides a connection upon which to act.
74
 
         *
75
 
         * @param connection a reference to a connection
76
 
         */
77
 
        explicit basic_statement(
78
 
                connection &connection );
79
 
 
80
 
        virtual ~basic_statement();
 
90
        virtual ~statement() throw( );
81
91
 
82
92
//______________________________________________________________________________
83
93
//                                                              public interface
85
95
 
86
96
        /**
87
97
         * Prepare an SQL statement.
88
 
         *
89
98
         * @param sql an SQL statement in UTF-8
90
99
         * @returns an sqlite error code
91
100
         * @see sqlite3_prepare_v2()
92
101
         */
93
 
        virtual int prepare(
 
102
        int prepare(
94
103
                const std::string &sql );
95
104
 
96
105
        /**
97
 
         * 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
98
116
         * the values bound to the statement.
99
 
         *
100
117
         * @returns an sqlite error code
101
118
         * @see sqlite3_reset()
102
119
         */
103
 
        virtual int reset();
 
120
        int reset();
104
121
 
105
122
        /**
106
123
         * Clears the values bound to a statement to NULL.
107
 
         *
108
124
         * @returns an sqlite error code
109
125
         * @see sqlite3_clear_bindings()
110
126
         */
111
127
        int clear_bindings();
112
128
 
113
129
        /**
114
 
         * Bind a value to the SQL statement via it's index.  This template will
115
 
         * take a variety of data types and bind them as text.  This is how sqlite
 
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
116
132
         * internally stores the data anyway, so always binding as text just means
117
133
         * we do the conversion instead of sqlite and is no less efficient.
118
 
         *
119
134
         * @param index the index of the parameter to bind to
120
135
         * @param value the value to bind
121
136
         * @returns an sqlite error code
133
148
 
134
149
        /**
135
150
         * Bind a string value to the SQL statement via it's index where the value
136
 
         * of that string will not change for the duration of the statement.  This
137
 
         * is more optimal because sqlite will not have to take it's own copy of the
 
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
138
153
         * data.
139
 
         *
140
154
         * @param index the index of the parameter to bind to
141
155
         * @param value the invariant string value
142
 
         * @param value_length the length of the string including zero-terminator
143
156
         * @returns an sqlite error code
144
157
         * @see sqlite3_bind_text()
145
158
         */
150
163
 
151
164
        /**
152
165
         * Bind a string value to the SQL statement via it's index where the value
153
 
         * of that string will not change for the duration of the statement.  This
154
 
         * is more optimal because sqlite will not have to take it's own copy of the
 
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
155
168
         * data.
156
 
         *
157
169
         * @param index the index of the parameter to bind to
158
170
         * @param value the invariant string value
159
171
         * @returns an sqlite error code
165
177
 
166
178
        /**
167
179
         * Bind a string value to the SQL statement via it's index where the value
168
 
         * of that string will not change for the duration of the statement.  This
169
 
         * is more optimal because sqlite will not have to take it's own copy of the
 
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
170
182
         * data.
171
 
         *
172
183
         * @param index the index of the parameter to bind to
173
184
         * @param value the invariant string value
174
185
         * @returns an sqlite error code
180
191
 
181
192
        /**
182
193
         * Bind a NULL value to the SQL statement via it's index.
183
 
         *
184
194
         * @param index the index of the parameter to bind to
185
195
         * @returns an sqlite error code
186
196
         * @see sqlite3_bind_null()
189
199
                unsigned int index );
190
200
 
191
201
        /**
192
 
         * Bind a value to the SQL statement via a named parameter.  This template
193
 
         * will take a variety of data types and bind them as text.  This is how
 
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
194
204
         * sqlite internally stores the data anyway, so always binding as text just
195
205
         * means we do the conversion instead of sqlite and is no less efficient.
196
 
         *
197
206
         * @param name the named parameter to bind to
198
207
         * @param value the value to bind
199
208
         * @returns an sqlite error code
204
213
                const std::string &name,
205
214
                T value )
206
215
        {
207
 
                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 );
208
219
        }
209
220
 
210
221
        /**
211
222
         * Bind a string value to the SQL statement via a named parameter where the
212
 
         * string value will not change for the duration of the statement.  This
213
 
         * prevents sqlite from taking its own copy of the string.
214
 
         *
 
223
         * string value will not change for the duration of the statement. This
 
224
         * prevents a copy of the string being taken.
215
225
         * @param name the named parameter to bind to
216
226
         * @param value the invariant string value
217
 
         * @param value_length the length of the string including zero-terminator
218
227
         * @returns an sqlite error code
219
228
         * @see sqlite3_bind_text()
220
229
         */
225
234
 
226
235
        /**
227
236
         * Bind a string value to the SQL statement via a named parameter where the
228
 
         * string value will not change for the duration of the statement.  This
 
237
         * string value will not change for the duration of the statement. This
229
238
         * prevents a copy of the string being taken.
230
 
         *
231
239
         * @param name the named parameter to bind to
232
240
         * @param value the invariant string value
233
241
         * @returns an sqlite error code
239
247
 
240
248
        /**
241
249
         * Bind a string value to the SQL statement via a named parameter where the
242
 
         * string value will not change for the duration of the statement.  This
 
250
         * string value will not change for the duration of the statement. This
243
251
         * prevents a copy of the string being taken.
244
 
         *
245
252
         * @param name the named parameter to bind to
246
253
         * @param value the invariant string value
247
254
         * @returns an sqlite error code
253
260
 
254
261
        /**
255
262
         * Bind a NULL value to the SQL statement via a named parameter.
256
 
         *
257
263
         * @param name the named parameter to bind to
258
264
         * @returns an sqlite error code
259
265
         * @see sqlite3_bind_null()
263
269
 
264
270
        /**
265
271
         * Stream operator is used to bind values to parameters automatically, in
266
 
         * ascending order.  In addition, the null and set_index() auto-binding
267
 
         * manipulators can be used.
268
 
         *
 
272
         * ascending order. In addition, the null, execute and set_index() auto-
 
273
         * binding manipulators can be used.
269
274
         * @param value a value to bind
270
275
         */
271
276
        template< class T >
272
 
        basic_statement &operator <<(
273
 
                const T &value )
 
277
        statement &operator <<( T value )
274
278
        {
275
 
                int code = bind( _bind_index, value );
276
 
                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 );
277
281
                _bind_index++;
278
282
                return *this;
279
283
        }
282
286
//                                                                implementation
283
287
protected:
284
288
 
285
 
        friend class row;
286
 
 
287
289
        /**
288
290
         * Finalise an SQL statement.
289
 
         *
290
291
         * @returns an sqlite error code
291
292
         * @see sqlite3_finalize()
292
293
         */
293
294
        int finalize();
294
295
 
295
 
        /**
296
 
         * Get the index number of a named parameter.
297
 
         *
298
 
         * @param parameter name
299
 
         * @return index of named parameter
300
 
         */
301
 
        int bind_parameter_index(
302
 
                const std::string &name );
303
 
 
304
 
        /**
305
 
         * Perform a step.
306
 
         *
307
 
         * @return sqlite error code
308
 
         * @see sqlite3_step()
309
 
         */
310
 
        int step();
311
 
 
312
 
        /** the connection upon which to act */
313
 
        connection &_connection;
 
296
private:
 
297
 
 
298
        /** the database upon which to act */
 
299
        database &_database;
314
300
 
315
301
        /** the statement handle */
316
302
        sqlite3_stmt *_handle;
321
307
};
322
308
 
323
309
 
324
 
// template specialisations for basic_statement::operator <<()
325
 
template< >
326
 
basic_statement &basic_statement::operator << < detail::null_t >(
327
 
        const detail::null_t & );
328
 
template< >
329
 
basic_statement &basic_statement::operator << < detail::set_index_t >(
330
 
        const detail::set_index_t &t );
331
 
 
332
 
 
333
 
} // namespace detail
334
 
 
335
 
 
336
 
} // namespace sqlite
337
 
 
338
 
 
339
 
#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_ */