/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 06:28:53 UTC
  • Revision ID: edam@waxworlds.org-20100729062853-4i2fec52m86mh724
- made basic_statement::step() protected, for use by query and command only
- moved basic_statement::operator<<() to command and query instead; one needs to accept sqlite::exec, the other doesn't
- added tests for query::operator<<()
- added code to invlaidate in-progress queries during any transaction rollbacks (currently segfaults in basic_statement::finalize())
- added new sqlite_error constructor that obtains a full error message
- implemented database::database_mutex_guard class
- swapped command's step mutex in favour of the database mutex

Show diffs side-by-side

added added

removed removed

32
32
{
33
33
 
34
34
 
35
 
class connection;
36
 
 
37
 
 
38
 
namespace detail
 
35
class database;
 
36
 
 
37
 
 
38
/**
 
39
 * A basic (default, deferred) transaction.
 
40
 */
 
41
class basic_transaction
 
42
        :
 
43
        private boost::noncopyable
39
44
{
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
 
////////////////////////////////////////////////////////////////////////////////
94
 
 
95
 
 
96
 
/**
97
 
 * A deferred transaction
98
 
 */
99
 
typedef detail::basic_transaction deferred_transaction;
100
 
 
101
 
 
102
 
////////////////////////////////////////////////////////////////////////////////
103
 
 
104
 
 
105
 
/**
106
 
 * An immediate transaction
 
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
};
 
86
 
 
87
 
 
88
////////////////////////////////////////////////////////////////////////////////
 
89
 
 
90
 
 
91
/**
 
92
 * An exclusive transaction
107
93
 */
108
94
class immediate_transaction
109
95
        :
110
 
        public detail::basic_transaction
 
96
        public basic_transaction
111
97
{
112
98
//______________________________________________________________________________
113
99
//                                                                 instantiation
114
100
public:
115
101
 
116
102
        /**
117
 
         * Constructor that provides a connection upon which to act
118
 
         * @param connection a connection
 
103
         * Constructor that provides a database upon which to act
 
104
         * @param database a database
119
105
         */
120
106
        explicit immediate_transaction(
121
 
                connection &connection );
 
107
                database &database );
122
108
 
123
109
//______________________________________________________________________________
124
110
//                                                              public interface
140
126
 */
141
127
class exclusive_transaction
142
128
        :
143
 
        public detail::basic_transaction
 
129
        public basic_transaction
144
130
{
145
131
//______________________________________________________________________________
146
132
//                                                                 instantiation
147
133
public:
148
134
 
149
135
        /**
150
 
         * Constructor that provides a connection upon which to act
151
 
         * @param connection a connection
 
136
         * Constructor that provides a database upon which to act
 
137
         * @param database a database
152
138
         */
153
139
        explicit exclusive_transaction(
154
 
                connection &connection );
 
140
                database &database );
155
141
 
156
142
//______________________________________________________________________________
157
143
//                                                              public interface
173
159
 */
174
160
class recursive_transaction
175
161
        :
176
 
        public detail::basic_transaction
 
162
        public basic_transaction
177
163
{
178
164
//______________________________________________________________________________
179
165
//                                                                 instantiation
180
166
public:
181
167
 
182
168
        /**
183
 
         * Constructor that provides a connection upon which to act
184
 
         * @param connection a connection
 
169
         * Constructor that provides a database upon which to act
 
170
         * @param database a database
185
171
         */
186
172
        explicit recursive_transaction(
187
 
                connection &connection );
 
173
                database &database );
188
174
 
189
175
//______________________________________________________________________________
190
176
//                                                              public interface
219
205
 
220
206
 
221
207
/**
222
 
 * A scope guard (sentinel) for use with one of the transaction
223
 
 * classes to provide RAII-style transactions. It defaults to using
224
 
 * deferred transactions.
 
208
 * A scope guard, or sentinel for use with one of the transaction classes.
225
209
 */
226
 
template< class T = deferred_transaction >
 
210
template< class T = basic_transaction >
227
211
class transaction_guard
228
212
        :
229
213
        private boost::noncopyable
233
217
public:
234
218
 
235
219
        /**
236
 
         * Constructor that provides a connection upon which to act
237
 
         * @param connection a connection
 
220
         * Constructor that provides a database upon which to act
 
221
         * @param database a database
238
222
         */
239
223
        explicit transaction_guard(
240
 
                connection &connection )
 
224
                database &database )
241
225
                :
242
 
                _transaction( connection ),
 
226
                _transaction( database ),
 
227
                _database( database ),
243
228
                _released( false )
244
229
        {
245
230
                _transaction.begin();
247
232
 
248
233
        ~transaction_guard()
249
234
        {
250
 
                if( !_released )
251
 
                        _transaction.rollback();
 
235
                if( !_released ) {
 
236
                        try {
 
237
                                _transaction.rollback();
 
238
                        }
 
239
                        catch( ... ) {
 
240
                        }
 
241
                }
252
242
        }
253
243
 
254
244
//______________________________________________________________________________
266
256
                }
267
257
        }
268
258
 
269
 
        /**
270
 
         * Rollback the transaction early
271
 
         */
272
 
        void rollback()
273
 
        {
274
 
                if( !_released ) {
275
 
                        _transaction.rollback();
276
 
                        _released = true;
277
 
                }
278
 
        }
279
 
 
280
259
//______________________________________________________________________________
281
260
//                                                                implementation
282
261
protected:
284
263
        /** the transaction */
285
264
        T _transaction;
286
265
 
 
266
        /** the database */
 
267
        database &_database;
 
268
 
287
269
        /** have we released the transaction yet? */
288
270
        bool _released;
289
271