/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: 2012-01-23 13:51:26 UTC
  • Revision ID: edam@waxworlds.org-20120123135126-7gohm0mv9qwismla
typo

Show diffs side-by-side

added added

removed removed

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