/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 20:56:19 UTC
  • Revision ID: edam@waxworlds.org-20100729205619-a9yo4uzu647nvnsj
- renamed database to connection to better identify what it is (would database_connection be better though?)
- moved basic_statement and basic_transaction to sqlite::detail
- made sqlite::threadsafe() return the threading mode int, not a bool
- renamed row::index() to row_number() so it isn't confused with column index
- added typedef for deferred_transaction
- added early rollback method to transaction_guard
- allowed transaction_guard::~transaction_guard() to throw exceptions, since when it needs to, we're really screwed anyway
- bugfix: query::reset() didn't reset the internal row counter
- added query::num_results()
- added docs/design-notes
- reimplemented query::iterator so that increment() (which performs a step() on the query) now caches the returned row to be returned during dereference() (previously it stashed details and returned new row!?)
- bugfix: resetting active queries during rollbacks would hang!

Show diffs side-by-side

added added

removed removed

1
1
/*
2
 
 * statement.hpp
 
2
 * basic_statement.h
3
3
 *
4
4
 * Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
5
5
 *
6
 
 * This file is part of sqlitepp (hereafter referred to as "this program").
7
 
 * See http://www.waxworlds.org/edam/software/sqlitepp for more information.
 
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
8
 *
9
9
 * This program is free software: you can redistribute it and/or modify
10
10
 * it under the terms of the GNU Lesser General Public License as published
20
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
21
 */
22
22
 
23
 
#ifndef STATEMENT_HPP_
24
 
#define STATEMENT_HPP_
 
23
#ifndef SQLITE3CC_BASIC_STATEMENT_H_
 
24
#define SQLITE3CC_BASIC_STATEMENT_H_
25
25
 
26
26
 
27
27
#include <sqlite3.h>
28
28
#include <boost/utility.hpp>
29
29
#include <boost/lexical_cast.hpp>
30
 
#include <sqlitepp/exception.hpp>
 
30
#include <sqlite3cc/exception.h>
31
31
 
32
32
 
33
33
namespace sqlite
34
34
{
35
35
 
36
36
 
37
 
class database;
38
 
 
39
 
 
40
 
struct _null_t { };
41
 
struct _exec_t { };
42
 
struct _set_index_t { unsigned int _index; };
43
 
 
44
 
/**
45
 
 * Auto-binding manipulator, for use with a statment's stream operator. This
46
 
 * specifies a NULL value to bind to a parameter.
47
 
 */
48
 
extern const _null_t null;
49
 
 
50
 
/**
51
 
 * Auto-binding manipulator, for use with a statment's stream operator. This
52
 
 * indicates that the statement should be executed immediately. Unlike a
53
 
 * statment's exec() method, this will throw on error. Also, it will throw if
54
 
 * the execution returns any result rows.
55
 
 */
56
 
extern const _exec_t exec;
57
 
 
58
 
/**
59
 
 * Auto-binding manipulator, for use with a statment's stream operator. This
60
 
 * manipulator sets the index used to automatically assign values to parameters.
61
 
 * @param index the new index for incremental assignment
62
 
 */
63
 
_set_index_t set_index(
64
 
        unsigned int index );
65
 
 
66
 
 
67
 
/**
68
 
 * The statement class represents an SQL statement. It is the bas class for both
69
 
 * the command and the query classes which should be used for those purposes.
70
 
 * The statement class its self has protected instantiation.
71
 
 */
72
 
class statement
 
37
class connection;
 
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
{
 
48
 
 
49
 
 
50
/**
 
51
 * The statement class represents an SQL statement. It is the base class for
 
52
 * both the command and the query classes, which should be used for those
 
53
 * purposes. The basic_statement class its self has protected instantiation.
 
54
 */
 
55
class basic_statement
73
56
        :
74
57
        private boost::noncopyable
75
58
{
78
61
protected:
79
62
 
80
63
        /**
81
 
         * 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
82
65
         * statement.
83
 
         * @param database a reference to the database
 
66
         * @param connection a reference to a connection
84
67
         * @param sql an SQL statement in UTF-8
85
68
         */
86
 
        statement(
87
 
                database &database,
 
69
        explicit basic_statement(
 
70
                connection &connection,
88
71
                const std::string &sql );
89
72
 
90
 
        virtual ~statement() throw( );
 
73
        /**
 
74
         * Constructor that provides a connection upon which to act.
 
75
         * @param connection a reference to a connection
 
76
         */
 
77
        explicit basic_statement(
 
78
                connection &connection );
 
79
 
 
80
        virtual ~basic_statement();
91
81
 
92
82
//______________________________________________________________________________
93
83
//                                                              public interface
99
89
         * @returns an sqlite error code
100
90
         * @see sqlite3_prepare_v2()
101
91
         */
102
 
        int prepare(
 
92
        virtual int prepare(
103
93
                const std::string &sql );
104
94
 
105
95
        /**
106
 
         * Step through one execution cycle of the SQL statement. If this is an SQL
107
 
         * statement that doesn't return any rows, only one cycle is required,
108
 
         * otherwise, each cycle will return another row
109
 
         * @return an sqlite error code
110
 
         * @see sqlite3_step()
111
 
         */
112
 
        int step();
113
 
 
114
 
        /**
115
96
         * Reset the statement, ready to re-execute it. This does not clear any of
116
97
         * the values bound to the statement.
117
98
         * @returns an sqlite error code
118
99
         * @see sqlite3_reset()
119
100
         */
120
 
        int reset();
 
101
        virtual int reset();
121
102
 
122
103
        /**
123
104
         * Clears the values bound to a statement to NULL.
149
130
        /**
150
131
         * Bind a string value to the SQL statement via it's index where the value
151
132
         * of that string will not change for the duration of the statement. This is
152
 
         * more optimal because sqlite will not have to make it's own copy of the
 
133
         * more optimal because sqlite will not have to take it's own copy of the
153
134
         * data.
154
135
         * @param index the index of the parameter to bind to
155
136
         * @param value the invariant string value
 
137
         * @param value_length the length of the string including zero-terminator
156
138
         * @returns an sqlite error code
157
139
         * @see sqlite3_bind_text()
158
140
         */
164
146
        /**
165
147
         * Bind a string value to the SQL statement via it's index where the value
166
148
         * of that string will not change for the duration of the statement. This is
167
 
         * more optimal  because sqlite will not have to make it's own copy of the
 
149
         * more optimal  because sqlite will not have to take it's own copy of the
168
150
         * data.
169
151
         * @param index the index of the parameter to bind to
170
152
         * @param value the invariant string value
178
160
        /**
179
161
         * Bind a string value to the SQL statement via it's index where the value
180
162
         * of that string will not change for the duration of the statement. This is
181
 
         * more optimal because sqlite will not have to make it's own copy of the
 
163
         * more optimal because sqlite will not have to take it's own copy of the
182
164
         * data.
183
165
         * @param index the index of the parameter to bind to
184
166
         * @param value the invariant string value
213
195
                const std::string &name,
214
196
                T value )
215
197
        {
216
 
                unsigned int index =
217
 
                        sqlite3_bind_parameter_index( _handle, name.c_str() );
218
 
                return bind( index, value );
 
198
                return bind( bind_parameter_index( name ), value );
219
199
        }
220
200
 
221
201
        /**
222
202
         * Bind a string value to the SQL statement via a named parameter where the
223
203
         * string value will not change for the duration of the statement. This
224
 
         * prevents a copy of the string being taken.
 
204
         * prevents sqlite from taking its own copy of the string.
225
205
         * @param name the named parameter to bind to
226
206
         * @param value the invariant string value
 
207
         * @param value_length the length of the string including zero-terminator
227
208
         * @returns an sqlite error code
228
209
         * @see sqlite3_bind_text()
229
210
         */
269
250
 
270
251
        /**
271
252
         * Stream operator is used to bind values to parameters automatically, in
272
 
         * ascending order. In addition, the null, execute and set_index() auto-
273
 
         * binding manipulators can be used.
 
253
         * ascending order. In addition, the null and set_index() auto-binding
 
254
         * manipulators can be used.
274
255
         * @param value a value to bind
275
256
         */
276
257
        template< class T >
277
 
        statement &operator <<( T value )
 
258
        basic_statement &operator <<(
 
259
                const T &value )
278
260
        {
279
 
                int error_code = bind( _bind_index, value );
280
 
                if( error_code != SQLITE_OK ) throw sqlite_error( error_code );
 
261
                int code = bind( _bind_index, value );
 
262
                if( code != SQLITE_OK ) throw sqlite_error( _connection, code );
281
263
                _bind_index++;
282
264
                return *this;
283
265
        }
286
268
//                                                                implementation
287
269
protected:
288
270
 
 
271
        friend class row;
 
272
 
289
273
        /**
290
274
         * Finalise an SQL statement.
291
275
         * @returns an sqlite error code
293
277
         */
294
278
        int finalize();
295
279
 
296
 
private:
297
 
 
298
 
        /** the database upon which to act */
299
 
        database &_database;
 
280
        /**
 
281
         * Get the index number of a named parameter
 
282
         * @param parameter name
 
283
         * @return index of named parameter
 
284
         */
 
285
        int bind_parameter_index(
 
286
                const std::string &name );
 
287
 
 
288
        /**
 
289
         * Perform a step
 
290
         * @return sqlite error code
 
291
         * @see sqlite3_step()
 
292
         */
 
293
        int step();
 
294
 
 
295
        /** the connection upon which to act */
 
296
        connection &_connection;
300
297
 
301
298
        /** the statement handle */
302
299
        sqlite3_stmt *_handle;
307
304
};
308
305
 
309
306
 
310
 
// template specialisations for statement::operator <<()
311
 
template< >
312
 
statement& statement::operator << < _null_t >(
313
 
        _null_t );
314
 
template< >
315
 
statement& statement::operator << < _exec_t >(
316
 
        _exec_t );
317
 
template< >
318
 
statement& statement::operator << < _set_index_t >(
319
 
        _set_index_t t );
320
 
 
321
 
 
322
 
}
323
 
 
324
 
 
325
 
#endif /* STATEMENT_HPP_ */
 
307
// template specialisations for basic_statement::operator <<()
 
308
template< >
 
309
basic_statement &basic_statement::operator << < detail::null_t >(
 
310
        const detail::null_t & );
 
311
template< >
 
312
basic_statement &basic_statement::operator << < detail::set_index_t >(
 
313
        const detail::set_index_t &t );
 
314
 
 
315
 
 
316
} // namespace detail
 
317
 
 
318
 
 
319
} // namespace sqlite
 
320
 
 
321
 
 
322
#endif /* SQLITE3CC_BASIC_STATEMENT_H_ */