/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/transaction.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

32
32
{
33
33
 
34
34
 
35
 
class database;
 
35
class connection;
 
36
 
 
37
 
 
38
namespace detail
 
39
{
 
40
 
 
41
        /**
 
42
         * A basic (default, deferred) transaction.
 
43
         */
 
44
        class basic_transaction
 
45
                :
 
46
                private boost::noncopyable
 
47
        {
 
48
        //__________________________________________________________________________
 
49
        //                                                             instantiation
 
50
        public:
 
51
 
 
52
                /**
 
53
                 * Constructor that provides a connection upon which to act
 
54
                 * @param connection a connection
 
55
                 */
 
56
                explicit basic_transaction(
 
57
                        connection &connection );
 
58
 
 
59
        //__________________________________________________________________________
 
60
        //                                                          public interface
 
61
        public:
 
62
 
 
63
                /**
 
64
                 * Begin the transaction
 
65
                 */
 
66
                virtual void begin();
 
67
 
 
68
                /**
 
69
                 * Commit the transaction
 
70
                 */
 
71
                virtual void commit();
 
72
 
 
73
                /**
 
74
                 * Rollback the transaction
 
75
                 */
 
76
                virtual void rollback();
 
77
 
 
78
        //__________________________________________________________________________
 
79
        //                                                            implementation
 
80
        protected:
 
81
 
 
82
                /** reset any in-progress statements */
 
83
                void reset_active_queries();
 
84
 
 
85
                /** the connection on which to act */
 
86
                connection &_connection;
 
87
 
 
88
        };
 
89
 
 
90
} // namespace detail
 
91
 
 
92
 
 
93
////////////////////////////////////////////////////////////////////////////////
36
94
 
37
95
 
38
96
/**
39
 
 * A basic (default, deferred) transaction.
 
97
 * A deferred transaction (the default)
40
98
 */
41
 
class basic_transaction
42
 
        :
43
 
        private boost::noncopyable
44
 
{
45
 
//______________________________________________________________________________
46
 
//                                                                 instantiation
47
 
public:
48
 
 
49
 
        /**
50
 
         * Constructor that provides a database upon which to act
51
 
         * @param database a database
52
 
         */
53
 
        explicit basic_transaction(
54
 
                database &database );
55
 
 
56
 
//______________________________________________________________________________
57
 
//                                                              public interface
58
 
public:
59
 
 
60
 
        /**
61
 
         * Begin the transaction
62
 
         */
63
 
        virtual void begin();
64
 
 
65
 
        /**
66
 
         * Commit the transaction
67
 
         */
68
 
        virtual void commit();
69
 
 
70
 
        /**
71
 
         * Rollback the transaction
72
 
         */
73
 
        virtual void rollback();
74
 
 
75
 
//______________________________________________________________________________
76
 
//                                                                implementation
77
 
protected:
78
 
 
79
 
        /** close any in-progress statements */
80
 
        void invalidate_queries();
81
 
 
82
 
        /** the database on which to act */
83
 
        database &_database;
84
 
 
85
 
};
 
99
typedef detail::basic_transaction deferred_tranaction;
86
100
 
87
101
 
88
102
////////////////////////////////////////////////////////////////////////////////
93
107
 */
94
108
class immediate_transaction
95
109
        :
96
 
        public basic_transaction
 
110
        public detail::basic_transaction
97
111
{
98
112
//______________________________________________________________________________
99
113
//                                                                 instantiation
100
114
public:
101
115
 
102
116
        /**
103
 
         * Constructor that provides a database upon which to act
104
 
         * @param database a database
 
117
         * Constructor that provides a connection upon which to act
 
118
         * @param connection a connection
105
119
         */
106
120
        explicit immediate_transaction(
107
 
                database &database );
 
121
                connection &connection );
108
122
 
109
123
//______________________________________________________________________________
110
124
//                                                              public interface
126
140
 */
127
141
class exclusive_transaction
128
142
        :
129
 
        public basic_transaction
 
143
        public detail::basic_transaction
130
144
{
131
145
//______________________________________________________________________________
132
146
//                                                                 instantiation
133
147
public:
134
148
 
135
149
        /**
136
 
         * Constructor that provides a database upon which to act
137
 
         * @param database a database
 
150
         * Constructor that provides a connection upon which to act
 
151
         * @param connection a connection
138
152
         */
139
153
        explicit exclusive_transaction(
140
 
                database &database );
 
154
                connection &connection );
141
155
 
142
156
//______________________________________________________________________________
143
157
//                                                              public interface
159
173
 */
160
174
class recursive_transaction
161
175
        :
162
 
        public basic_transaction
 
176
        public detail::basic_transaction
163
177
{
164
178
//______________________________________________________________________________
165
179
//                                                                 instantiation
166
180
public:
167
181
 
168
182
        /**
169
 
         * Constructor that provides a database upon which to act
170
 
         * @param database a database
 
183
         * Constructor that provides a connection upon which to act
 
184
         * @param connection a connection
171
185
         */
172
186
        explicit recursive_transaction(
173
 
                database &database );
 
187
                connection &connection );
174
188
 
175
189
//______________________________________________________________________________
176
190
//                                                              public interface
205
219
 
206
220
 
207
221
/**
208
 
 * A scope guard, or sentinel for use with one of the transaction classes.
 
222
 * A scope guard (sentinel) for use with one of the transaction classes to
 
223
 * provide RIAA-style transactions.
209
224
 */
210
 
template< class T = basic_transaction >
 
225
template< class T = deferred_tranaction >
211
226
class transaction_guard
212
227
        :
213
228
        private boost::noncopyable
217
232
public:
218
233
 
219
234
        /**
220
 
         * Constructor that provides a database upon which to act
221
 
         * @param database a database
 
235
         * Constructor that provides a connection upon which to act
 
236
         * @param connection a connection
222
237
         */
223
238
        explicit transaction_guard(
224
 
                database &database )
 
239
                connection &connection )
225
240
                :
226
 
                _transaction( database ),
 
241
                _transaction( connection ),
227
242
                _released( false )
228
243
        {
229
244
                _transaction.begin();
231
246
 
232
247
        ~transaction_guard()
233
248
        {
234
 
                if( !_released ) {
235
 
                        try {
236
 
                                _transaction.rollback();
237
 
                        }
238
 
                        catch( ... ) {
239
 
                        }
240
 
                }
 
249
                if( !_released )
 
250
                        _transaction.rollback();
241
251
        }
242
252
 
243
253
//______________________________________________________________________________
245
255
public:
246
256
 
247
257
        /**
248
 
         * Commit the transaction
 
258
         * Commit the transaction early
249
259
         */
250
260
        void commit()
251
261
        {
255
265
                }
256
266
        }
257
267
 
 
268
        /**
 
269
         * Rollback the transaction early
 
270
         */
 
271
        void rollback()
 
272
        {
 
273
                if( !_released ) {
 
274
                        _transaction.rollback();
 
275
                        _released = true;
 
276
                }
 
277
        }
 
278
 
258
279
//______________________________________________________________________________
259
280
//                                                                implementation
260
281
protected: