/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-02-08 22:08:57 UTC
  • Revision ID: edam@waxworlds.org-20100208220857-5up6fi4y6jh5bz4k
- removed "OK" from test-main when test is successful
- aranged for test/test-main to buld properly and be called from "make check"
- added include/Makefile.am to arrange for installation of library headers
- added project website to configure.ac
- added library interface configuration variable to configure.ac

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