/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 17:12:43 UTC
  • Revision ID: edam@waxworlds.org-20120123171243-bgqedav41y2x8rlw
added command and query factory methods to connection

Show diffs side-by-side

added added

removed removed

1
1
/*
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.
 
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.
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 STATEMENT_HPP_
24
 
#define STATEMENT_HPP_
 
23
#ifndef SQLITE3CC_BASIC_STATEMENT_H_
 
24
#define SQLITE3CC_BASIC_STATEMENT_H_
25
25
 
26
26
 
27
27
#include <sqlite3.h>
28
28
#include <boost/utility.hpp>
29
29
#include <boost/lexical_cast.hpp>
30
 
#include <sqlitepp/exception.hpp>
 
30
#include <sqlite3cc/exception.h>
31
31
 
32
32
 
33
33
namespace sqlite
34
34
{
35
35
 
36
36
 
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
 
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
75
56
{
76
57
//______________________________________________________________________________
77
58
//                                                                 instantiation
78
59
protected:
79
60
 
80
61
        /**
81
 
         * Constructor that provides a database upon which to act and the SQL
 
62
         * Constructor that provides a connection upon which to act and the SQL
82
63
         * statement.
83
 
         * @param database a reference to the database
 
64
         *
 
65
         * @param connection a reference to a connection
84
66
         * @param sql an SQL statement in UTF-8
85
67
         */
86
 
        statement(
87
 
                database &database,
 
68
        explicit basic_statement(
 
69
                connection &connection,
88
70
                const std::string &sql );
89
71
 
90
 
        virtual ~statement() throw( );
 
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();
91
81
 
92
82
//______________________________________________________________________________
93
83
//                                                              public interface
95
85
 
96
86
        /**
97
87
         * Prepare an SQL statement.
 
88
         *
98
89
         * @param sql an SQL statement in UTF-8
99
90
         * @returns an sqlite error code
100
91
         * @see sqlite3_prepare_v2()
101
92
         */
102
 
        int prepare(
 
93
        virtual int prepare(
103
94
                const std::string &sql );
104
95
 
105
96
        /**
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
 
97
         * Reset the statement, ready to re-execute it.  This does not clear any of
116
98
         * the values bound to the statement.
 
99
         *
117
100
         * @returns an sqlite error code
118
101
         * @see sqlite3_reset()
119
102
         */
120
 
        int reset();
 
103
        virtual int reset();
121
104
 
122
105
        /**
123
106
         * Clears the values bound to a statement to NULL.
 
107
         *
124
108
         * @returns an sqlite error code
125
109
         * @see sqlite3_clear_bindings()
126
110
         */
127
111
        int clear_bindings();
128
112
 
129
113
        /**
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
 
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
132
116
         * internally stores the data anyway, so always binding as text just means
133
117
         * we do the conversion instead of sqlite and is no less efficient.
 
118
         *
134
119
         * @param index the index of the parameter to bind to
135
120
         * @param value the value to bind
136
121
         * @returns an sqlite error code
148
133
 
149
134
        /**
150
135
         * 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
 
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
153
138
         * data.
 
139
         *
154
140
         * @param index the index of the parameter to bind to
155
141
         * @param value the invariant string value
 
142
         * @param value_length the length of the string including zero-terminator
156
143
         * @returns an sqlite error code
157
144
         * @see sqlite3_bind_text()
158
145
         */
163
150
 
164
151
        /**
165
152
         * 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
 
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
168
155
         * data.
 
156
         *
169
157
         * @param index the index of the parameter to bind to
170
158
         * @param value the invariant string value
171
159
         * @returns an sqlite error code
177
165
 
178
166
        /**
179
167
         * 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
 
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
182
170
         * data.
 
171
         *
183
172
         * @param index the index of the parameter to bind to
184
173
         * @param value the invariant string value
185
174
         * @returns an sqlite error code
191
180
 
192
181
        /**
193
182
         * Bind a NULL value to the SQL statement via it's index.
 
183
         *
194
184
         * @param index the index of the parameter to bind to
195
185
         * @returns an sqlite error code
196
186
         * @see sqlite3_bind_null()
199
189
                unsigned int index );
200
190
 
201
191
        /**
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
 
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
204
194
         * sqlite internally stores the data anyway, so always binding as text just
205
195
         * means we do the conversion instead of sqlite and is no less efficient.
 
196
         *
206
197
         * @param name the named parameter to bind to
207
198
         * @param value the value to bind
208
199
         * @returns an sqlite error code
213
204
                const std::string &name,
214
205
                T value )
215
206
        {
216
 
                unsigned int index =
217
 
                        sqlite3_bind_parameter_index( _handle, name.c_str() );
218
 
                return bind( index, value );
 
207
                return bind( bind_parameter_index( name ), value );
219
208
        }
220
209
 
221
210
        /**
222
211
         * 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.
 
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
         *
225
215
         * @param name the named parameter to bind to
226
216
         * @param value the invariant string value
 
217
         * @param value_length the length of the string including zero-terminator
227
218
         * @returns an sqlite error code
228
219
         * @see sqlite3_bind_text()
229
220
         */
234
225
 
235
226
        /**
236
227
         * 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
 
228
         * string value will not change for the duration of the statement.  This
238
229
         * prevents a copy of the string being taken.
 
230
         *
239
231
         * @param name the named parameter to bind to
240
232
         * @param value the invariant string value
241
233
         * @returns an sqlite error code
247
239
 
248
240
        /**
249
241
         * 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
 
242
         * string value will not change for the duration of the statement.  This
251
243
         * prevents a copy of the string being taken.
 
244
         *
252
245
         * @param name the named parameter to bind to
253
246
         * @param value the invariant string value
254
247
         * @returns an sqlite error code
260
253
 
261
254
        /**
262
255
         * Bind a NULL value to the SQL statement via a named parameter.
 
256
         *
263
257
         * @param name the named parameter to bind to
264
258
         * @returns an sqlite error code
265
259
         * @see sqlite3_bind_null()
269
263
 
270
264
        /**
271
265
         * Stream operator is used to bind values to parameters automatically, in
272
 
         * ascending order. In addition, the null, execute and set_index() auto-
273
 
         * binding manipulators can be used.
 
266
         * ascending order.  In addition, the null and set_index() auto-binding
 
267
         * manipulators can be used.
 
268
         *
274
269
         * @param value a value to bind
275
270
         */
276
271
        template< class T >
277
 
        statement &operator <<( T value )
 
272
        basic_statement &operator <<(
 
273
                const T &value )
278
274
        {
279
 
                int error_code = bind( _bind_index, value );
280
 
                if( error_code != SQLITE_OK ) throw sqlite_error( error_code );
 
275
                int code = bind( _bind_index, value );
 
276
                if( code != SQLITE_OK ) throw sqlite_error( _connection, code );
281
277
                _bind_index++;
282
278
                return *this;
283
279
        }
286
282
//                                                                implementation
287
283
protected:
288
284
 
 
285
        friend class row;
 
286
 
289
287
        /**
290
288
         * Finalise an SQL statement.
 
289
         *
291
290
         * @returns an sqlite error code
292
291
         * @see sqlite3_finalize()
293
292
         */
294
293
        int finalize();
295
294
 
296
 
private:
297
 
 
298
 
        /** the database upon which to act */
299
 
        database &_database;
 
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;
300
314
 
301
315
        /** the statement handle */
302
316
        sqlite3_stmt *_handle;
307
321
};
308
322
 
309
323
 
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_ */
 
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_ */