/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: 2014-01-03 12:17:03 UTC
  • Revision ID: tim@ed.am-20140103121703-afwyh7wifde5kidm
added description of return parameter to header

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
 
        :
51
 
        private boost::noncopyable
52
56
{
53
57
//______________________________________________________________________________
54
58
//                                                                 instantiation
55
59
protected:
56
60
 
57
61
        /**
58
 
         * 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
59
63
         * statement.
60
 
         * @param database a reference to a database
 
64
         *
 
65
         * @param connection a reference to a connection
61
66
         * @param sql an SQL statement in UTF-8
62
67
         */
63
68
        explicit basic_statement(
64
 
                database &database,
 
69
                connection &connection,
65
70
                const std::string &sql );
66
71
 
67
72
        /**
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
 
73
         * Constructor that provides a connection upon which to act.
 
74
         *
 
75
         * @param connection a reference to a connection
71
76
         */
72
77
        explicit basic_statement(
73
 
                database &database );
 
78
                connection &connection );
74
79
 
75
80
        virtual ~basic_statement();
76
81
 
80
85
 
81
86
        /**
82
87
         * Prepare an SQL statement.
 
88
         *
83
89
         * @param sql an SQL statement in UTF-8
84
90
         * @returns an sqlite error code
85
91
         * @see sqlite3_prepare_v2()
88
94
                const std::string &sql );
89
95
 
90
96
        /**
91
 
         * 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
92
98
         * the values bound to the statement.
 
99
         *
93
100
         * @returns an sqlite error code
94
101
         * @see sqlite3_reset()
95
102
         */
96
 
        int reset();
 
103
        virtual int reset();
97
104
 
98
105
        /**
99
106
         * Clears the values bound to a statement to NULL.
 
107
         *
100
108
         * @returns an sqlite error code
101
109
         * @see sqlite3_clear_bindings()
102
110
         */
103
111
        int clear_bindings();
104
112
 
105
113
        /**
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
 
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
108
116
         * internally stores the data anyway, so always binding as text just means
109
117
         * we do the conversion instead of sqlite and is no less efficient.
 
118
         *
110
119
         * @param index the index of the parameter to bind to
111
120
         * @param value the value to bind
112
121
         * @returns an sqlite error code
124
133
 
125
134
        /**
126
135
         * 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
 
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
129
138
         * data.
 
139
         *
130
140
         * @param index the index of the parameter to bind to
131
141
         * @param value the invariant string value
 
142
         * @param value_length the length of the string including zero-terminator
132
143
         * @returns an sqlite error code
133
144
         * @see sqlite3_bind_text()
134
145
         */
139
150
 
140
151
        /**
141
152
         * 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
 
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
144
155
         * data.
 
156
         *
145
157
         * @param index the index of the parameter to bind to
146
158
         * @param value the invariant string value
147
159
         * @returns an sqlite error code
153
165
 
154
166
        /**
155
167
         * 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
 
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
158
170
         * data.
 
171
         *
159
172
         * @param index the index of the parameter to bind to
160
173
         * @param value the invariant string value
161
174
         * @returns an sqlite error code
167
180
 
168
181
        /**
169
182
         * Bind a NULL value to the SQL statement via it's index.
 
183
         *
170
184
         * @param index the index of the parameter to bind to
171
185
         * @returns an sqlite error code
172
186
         * @see sqlite3_bind_null()
175
189
                unsigned int index );
176
190
 
177
191
        /**
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
 
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
180
194
         * sqlite internally stores the data anyway, so always binding as text just
181
195
         * means we do the conversion instead of sqlite and is no less efficient.
 
196
         *
182
197
         * @param name the named parameter to bind to
183
198
         * @param value the value to bind
184
199
         * @returns an sqlite error code
194
209
 
195
210
        /**
196
211
         * 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.
 
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
         *
199
215
         * @param name the named parameter to bind to
200
216
         * @param value the invariant string value
 
217
         * @param value_length the length of the string including zero-terminator
201
218
         * @returns an sqlite error code
202
219
         * @see sqlite3_bind_text()
203
220
         */
208
225
 
209
226
        /**
210
227
         * 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
 
228
         * string value will not change for the duration of the statement.  This
212
229
         * prevents a copy of the string being taken.
 
230
         *
213
231
         * @param name the named parameter to bind to
214
232
         * @param value the invariant string value
215
233
         * @returns an sqlite error code
221
239
 
222
240
        /**
223
241
         * 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
 
242
         * string value will not change for the duration of the statement.  This
225
243
         * prevents a copy of the string being taken.
 
244
         *
226
245
         * @param name the named parameter to bind to
227
246
         * @param value the invariant string value
228
247
         * @returns an sqlite error code
234
253
 
235
254
        /**
236
255
         * Bind a NULL value to the SQL statement via a named parameter.
 
256
         *
237
257
         * @param name the named parameter to bind to
238
258
         * @returns an sqlite error code
239
259
         * @see sqlite3_bind_null()
243
263
 
244
264
        /**
245
265
         * 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.
 
266
         * ascending order.  In addition, the null and set_index() auto-binding
 
267
         * manipulators can be used.
 
268
         *
248
269
         * @param value a value to bind
249
270
         */
250
271
        template< class T >
251
272
        basic_statement &operator <<(
252
273
                const T &value )
253
274
        {
254
 
                int error_code = bind( _bind_index, value );
255
 
                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 );
256
277
                _bind_index++;
257
278
                return *this;
258
279
        }
265
286
 
266
287
        /**
267
288
         * Finalise an SQL statement.
 
289
         *
268
290
         * @returns an sqlite error code
269
291
         * @see sqlite3_finalize()
270
292
         */
271
293
        int finalize();
272
294
 
273
295
        /**
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
 
296
         * Get the index number of a named parameter.
 
297
         *
284
298
         * @param parameter name
285
299
         * @return index of named parameter
286
300
         */
287
301
        int bind_parameter_index(
288
302
                const std::string &name );
289
303
 
290
 
        /** the database upon which to act */
291
 
        database &_database;
 
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;
292
314
 
293
315
        /** the statement handle */
294
316
        sqlite3_stmt *_handle;
295
317
 
296
 
private:
297
 
 
298
318
        /** index used when auto-binding */
299
319
        unsigned int _bind_index;
300
320
 
301
321
};
302
322
 
303
323
 
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 );
 
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
314
334
 
315
335
 
316
336
} // namespace sqlite