/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:34:45 UTC
  • Revision ID: edam@waxworlds.org-20120123143445-s2v4v90nycmfm6bv
fixed up tests

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