/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: 2010-07-29 09:16:26 UTC
  • Revision ID: edam@waxworlds.org-20100729091626-h8fmg0r74eyfo5ae
- fixed error caused by finialising in-progress queries during rollback that were later finaliased by RAII.

Show diffs side-by-side

added added

removed removed

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