/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

34
34
{
35
35
 
36
36
 
37
 
class database;
 
37
class connection;
38
38
class row;
39
 
struct _null_t;
40
 
struct _exec_t;
41
 
struct _set_index_t;
 
39
namespace detail {
 
40
        struct null_t;
 
41
        struct exec_t;
 
42
        struct set_index_t;
 
43
}
 
44
 
 
45
 
 
46
namespace detail
 
47
{
42
48
 
43
49
 
44
50
/**
55
61
protected:
56
62
 
57
63
        /**
58
 
         * 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
59
65
         * statement.
60
 
         * @param database a reference to a database
 
66
         * @param connection a reference to a connection
61
67
         * @param sql an SQL statement in UTF-8
62
68
         */
63
69
        explicit basic_statement(
64
 
                database &database,
 
70
                connection &connection,
65
71
                const std::string &sql );
66
72
 
67
73
        /**
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
 
74
         * Constructor that provides a connection upon which to act.
 
75
         * @param connection a reference to a connection
71
76
         */
72
77
        explicit basic_statement(
73
 
                database &database );
 
78
                connection &connection );
74
79
 
75
 
        virtual ~basic_statement() throw( );
 
80
        virtual ~basic_statement();
76
81
 
77
82
//______________________________________________________________________________
78
83
//                                                              public interface
93
98
         * @returns an sqlite error code
94
99
         * @see sqlite3_reset()
95
100
         */
96
 
        int reset();
 
101
        virtual int reset();
97
102
 
98
103
        /**
99
104
         * Clears the values bound to a statement to NULL.
125
130
        /**
126
131
         * Bind a string value to the SQL statement via it's index where the value
127
132
         * 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
 
133
         * more optimal because sqlite will not have to take it's own copy of the
129
134
         * data.
130
135
         * @param index the index of the parameter to bind to
131
136
         * @param value the invariant string value
 
137
         * @param value_length the length of the string including zero-terminator
132
138
         * @returns an sqlite error code
133
139
         * @see sqlite3_bind_text()
134
140
         */
140
146
        /**
141
147
         * Bind a string value to the SQL statement via it's index where the value
142
148
         * 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
 
149
         * more optimal  because sqlite will not have to take it's own copy of the
144
150
         * data.
145
151
         * @param index the index of the parameter to bind to
146
152
         * @param value the invariant string value
154
160
        /**
155
161
         * Bind a string value to the SQL statement via it's index where the value
156
162
         * 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
 
163
         * more optimal because sqlite will not have to take it's own copy of the
158
164
         * data.
159
165
         * @param index the index of the parameter to bind to
160
166
         * @param value the invariant string value
195
201
        /**
196
202
         * Bind a string value to the SQL statement via a named parameter where the
197
203
         * string value will not change for the duration of the statement. This
198
 
         * prevents a copy of the string being taken.
 
204
         * prevents sqlite from taking its own copy of the string.
199
205
         * @param name the named parameter to bind to
200
206
         * @param value the invariant string value
 
207
         * @param value_length the length of the string including zero-terminator
201
208
         * @returns an sqlite error code
202
209
         * @see sqlite3_bind_text()
203
210
         */
243
250
 
244
251
        /**
245
252
         * Stream operator is used to bind values to parameters automatically, in
246
 
         * ascending order. In addition, the null, set_index() and execute auto-
247
 
         * binding manipulators can be used.
 
253
         * ascending order. In addition, the null and set_index() auto-binding
 
254
         * manipulators can be used.
248
255
         * @param value a value to bind
249
256
         */
250
257
        template< class T >
251
258
        basic_statement &operator <<(
252
259
                const T &value )
253
260
        {
254
 
                int error_code = bind( _bind_index, value );
255
 
                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 );
256
263
                _bind_index++;
257
264
                return *this;
258
265
        }
271
278
        int finalize();
272
279
 
273
280
        /**
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
281
         * Get the index number of a named parameter
284
282
         * @param parameter name
285
283
         * @return index of named parameter
287
285
        int bind_parameter_index(
288
286
                const std::string &name );
289
287
 
290
 
        /** the database upon which to act */
291
 
        database &_database;
 
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;
292
297
 
293
298
        /** the statement handle */
294
299
        sqlite3_stmt *_handle;
295
300
 
296
 
private:
297
 
 
298
301
        /** index used when auto-binding */
299
302
        unsigned int _bind_index;
300
303
 
301
304
};
302
305
 
303
306
 
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 );
 
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
314
317
 
315
318
 
316
319
} // namespace sqlite