/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 14:27:08 UTC
  • Revision ID: edam@waxworlds.org-20120123142708-w12277ptwtlspa6f
updated email and web addresses

Show diffs side-by-side

added added

removed removed

1
1
/*
2
2
 * basic_statement.h
3
3
 *
4
 
 * Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
 
4
 * Copyright (C) 2009 Tim Marston <tim@ed.am>
5
5
 *
6
6
 * This file is part of sqlite3cc (hereafter referred to as "this program").
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.
 
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/>.
34
34
{
35
35
 
36
36
 
37
 
class database;
 
37
class connection;
38
38
class row;
39
 
struct _null_t;
40
 
struct _exec_t;
41
 
struct _set_index_t;
 
39
namespace detail {
 
40
        struct null_t;
 
41
        struct exec_t;
 
42
        struct set_index_t;
 
43
}
 
44
 
 
45
 
 
46
namespace detail
 
47
{
42
48
 
43
49
 
44
50
/**
45
 
 * 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
46
52
 * both the command and the query classes, which should be used for those
47
 
 * purposes. The basic_statement class its self has protected instantiation.
 
53
 * purposes.  The basic_statement class its self has protected instantiation.
48
54
 */
49
55
class basic_statement
50
56
        :
55
61
protected:
56
62
 
57
63
        /**
58
 
         * Constructor that provides a database upon which to act and the SQL
 
64
         * Constructor that provides a connection upon which to act and the SQL
59
65
         * statement.
60
 
         * @param database a reference to a database
 
66
         *
 
67
         * @param connection a reference to a connection
61
68
         * @param sql an SQL statement in UTF-8
62
69
         */
63
70
        explicit basic_statement(
64
 
                database &database,
 
71
                connection &connection,
65
72
                const std::string &sql );
66
73
 
67
74
        /**
68
 
         * Constructor that provides a database upon which to act.
69
 
         * @param database a reference to a database
70
 
         * @param sql an SQL statement in UTF-8
 
75
         * Constructor that provides a connection upon which to act.
 
76
         *
 
77
         * @param connection a reference to a connection
71
78
         */
72
79
        explicit basic_statement(
73
 
                database &database );
 
80
                connection &connection );
74
81
 
75
 
        virtual ~basic_statement() throw( );
 
82
        virtual ~basic_statement();
76
83
 
77
84
//______________________________________________________________________________
78
85
//                                                              public interface
80
87
 
81
88
        /**
82
89
         * Prepare an SQL statement.
 
90
         *
83
91
         * @param sql an SQL statement in UTF-8
84
92
         * @returns an sqlite error code
85
93
         * @see sqlite3_prepare_v2()
88
96
                const std::string &sql );
89
97
 
90
98
        /**
91
 
         * Reset the statement, ready to re-execute it. This does not clear any of
 
99
         * Reset the statement, ready to re-execute it.  This does not clear any of
92
100
         * the values bound to the statement.
 
101
         *
93
102
         * @returns an sqlite error code
94
103
         * @see sqlite3_reset()
95
104
         */
96
 
        int reset();
 
105
        virtual int reset();
97
106
 
98
107
        /**
99
108
         * Clears the values bound to a statement to NULL.
 
109
         *
100
110
         * @returns an sqlite error code
101
111
         * @see sqlite3_clear_bindings()
102
112
         */
103
113
        int clear_bindings();
104
114
 
105
115
        /**
106
 
         * Bind a value to the SQL statement via it's index. This template will take
107
 
         * a variety of data types and bind them as text. This is how sqlite
 
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
108
118
         * internally stores the data anyway, so always binding as text just means
109
119
         * we do the conversion instead of sqlite and is no less efficient.
 
120
         *
110
121
         * @param index the index of the parameter to bind to
111
122
         * @param value the value to bind
112
123
         * @returns an sqlite error code
124
135
 
125
136
        /**
126
137
         * Bind a string value to the SQL statement via it's index where the value
127
 
         * of that string will not change for the duration of the statement. This is
128
 
         * more optimal because sqlite will not have to make it's own copy of the
 
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
129
140
         * data.
 
141
         *
130
142
         * @param index the index of the parameter to bind to
131
143
         * @param value the invariant string value
 
144
         * @param value_length the length of the string including zero-terminator
132
145
         * @returns an sqlite error code
133
146
         * @see sqlite3_bind_text()
134
147
         */
139
152
 
140
153
        /**
141
154
         * Bind a string value to the SQL statement via it's index where the value
142
 
         * of that string will not change for the duration of the statement. This is
143
 
         * more optimal  because sqlite will not have to make it's own copy of the
 
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
144
157
         * data.
 
158
         *
145
159
         * @param index the index of the parameter to bind to
146
160
         * @param value the invariant string value
147
161
         * @returns an sqlite error code
153
167
 
154
168
        /**
155
169
         * Bind a string value to the SQL statement via it's index where the value
156
 
         * of that string will not change for the duration of the statement. This is
157
 
         * more optimal because sqlite will not have to make it's own copy of the
 
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
158
172
         * data.
 
173
         *
159
174
         * @param index the index of the parameter to bind to
160
175
         * @param value the invariant string value
161
176
         * @returns an sqlite error code
167
182
 
168
183
        /**
169
184
         * Bind a NULL value to the SQL statement via it's index.
 
185
         *
170
186
         * @param index the index of the parameter to bind to
171
187
         * @returns an sqlite error code
172
188
         * @see sqlite3_bind_null()
175
191
                unsigned int index );
176
192
 
177
193
        /**
178
 
         * Bind a value to the SQL statement via a named parameter. This template
179
 
         * will take a variety of data types and bind them as text. This is how
 
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
180
196
         * sqlite internally stores the data anyway, so always binding as text just
181
197
         * means we do the conversion instead of sqlite and is no less efficient.
 
198
         *
182
199
         * @param name the named parameter to bind to
183
200
         * @param value the value to bind
184
201
         * @returns an sqlite error code
194
211
 
195
212
        /**
196
213
         * Bind a string value to the SQL statement via a named parameter where the
197
 
         * string value will not change for the duration of the statement. This
198
 
         * prevents a copy of the string being taken.
 
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
         *
199
217
         * @param name the named parameter to bind to
200
218
         * @param value the invariant string value
 
219
         * @param value_length the length of the string including zero-terminator
201
220
         * @returns an sqlite error code
202
221
         * @see sqlite3_bind_text()
203
222
         */
208
227
 
209
228
        /**
210
229
         * Bind a string value to the SQL statement via a named parameter where the
211
 
         * string value will not change for the duration of the statement. This
 
230
         * string value will not change for the duration of the statement.  This
212
231
         * prevents a copy of the string being taken.
 
232
         *
213
233
         * @param name the named parameter to bind to
214
234
         * @param value the invariant string value
215
235
         * @returns an sqlite error code
221
241
 
222
242
        /**
223
243
         * Bind a string value to the SQL statement via a named parameter where the
224
 
         * string value will not change for the duration of the statement. This
 
244
         * string value will not change for the duration of the statement.  This
225
245
         * prevents a copy of the string being taken.
 
246
         *
226
247
         * @param name the named parameter to bind to
227
248
         * @param value the invariant string value
228
249
         * @returns an sqlite error code
234
255
 
235
256
        /**
236
257
         * Bind a NULL value to the SQL statement via a named parameter.
 
258
         *
237
259
         * @param name the named parameter to bind to
238
260
         * @returns an sqlite error code
239
261
         * @see sqlite3_bind_null()
243
265
 
244
266
        /**
245
267
         * Stream operator is used to bind values to parameters automatically, in
246
 
         * ascending order. In addition, the null, set_index() and execute auto-
247
 
         * binding manipulators can be used.
 
268
         * ascending order.  In addition, the null and set_index() auto-binding
 
269
         * manipulators can be used.
 
270
         *
248
271
         * @param value a value to bind
249
272
         */
250
273
        template< class T >
251
274
        basic_statement &operator <<(
252
275
                const T &value )
253
276
        {
254
 
                int error_code = bind( _bind_index, value );
255
 
                if( error_code != SQLITE_OK ) throw sqlite_error( error_code );
 
277
                int code = bind( _bind_index, value );
 
278
                if( code != SQLITE_OK ) throw sqlite_error( _connection, code );
256
279
                _bind_index++;
257
280
                return *this;
258
281
        }
265
288
 
266
289
        /**
267
290
         * Finalise an SQL statement.
 
291
         *
268
292
         * @returns an sqlite error code
269
293
         * @see sqlite3_finalize()
270
294
         */
271
295
        int finalize();
272
296
 
273
297
        /**
274
 
         * Step through one execution cycle of the SQL statement. If this is an SQL
275
 
         * statement that doesn't return any rows, only one cycle is required,
276
 
         * otherwise, each cycle will return another row
277
 
         * @return an sqlite error code
278
 
         * @see sqlite3_step()
279
 
         */
280
 
        int step();
281
 
 
282
 
        /**
283
 
         * Get the index number of a named parameter
 
298
         * Get the index number of a named parameter.
 
299
         *
284
300
         * @param parameter name
285
301
         * @return index of named parameter
286
302
         */
287
303
        int bind_parameter_index(
288
304
                const std::string &name );
289
305
 
290
 
        /** the database upon which to act */
291
 
        database &_database;
 
306
        /**
 
307
         * Perform a step.
 
308
         *
 
309
         * @return sqlite error code
 
310
         * @see sqlite3_step()
 
311
         */
 
312
        int step();
 
313
 
 
314
        /** the connection upon which to act */
 
315
        connection &_connection;
292
316
 
293
317
        /** the statement handle */
294
318
        sqlite3_stmt *_handle;
295
319
 
296
 
private:
297
 
 
298
320
        /** index used when auto-binding */
299
321
        unsigned int _bind_index;
300
322
 
301
323
};
302
324
 
303
325
 
304
 
// template specialisations for statement::operator <<()
305
 
template< >
306
 
basic_statement &basic_statement::operator << < _null_t >(
307
 
        const _null_t & );
308
 
template< >
309
 
basic_statement &basic_statement::operator << < _exec_t >(
310
 
        const _exec_t & );
311
 
template< >
312
 
basic_statement &basic_statement::operator << < _set_index_t >(
313
 
        const _set_index_t &t );
 
326
// template specialisations for basic_statement::operator <<()
 
327
template< >
 
328
basic_statement &basic_statement::operator << < detail::null_t >(
 
329
        const detail::null_t & );
 
330
template< >
 
331
basic_statement &basic_statement::operator << < detail::set_index_t >(
 
332
        const detail::set_index_t &t );
 
333
 
 
334
 
 
335
} // namespace detail
314
336
 
315
337
 
316
338
} // namespace sqlite