/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 16:40:21 UTC
  • Revision ID: tim@ed.am-20140103164021-i6o99h1459xjdq5o
reverted version no. to 0.2dev

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