/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-27 15:12:55 UTC
  • Revision ID: edam@waxworlds.org-20100727151255-goaqgdz4kj13q7gz
- update TODO
- added some missing includes for <string>
- changed usage of database::exec() to not require return code!
- prevented transaction_guard destructor from throwing an exception

Show diffs side-by-side

added added

removed removed

32
32
{
33
33
 
34
34
 
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
 
////////////////////////////////////////////////////////////////////////////////
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
107
 
 */
108
 
class immediate_transaction
 
35
class database;
 
36
 
 
37
 
 
38
class basic_transaction
109
39
        :
110
 
        public detail::basic_transaction
 
40
        private boost::noncopyable
111
41
{
112
42
//______________________________________________________________________________
113
43
//                                                                 instantiation
114
44
public:
115
45
 
116
46
        /**
117
 
         * Constructor that provides a connection upon which to act
118
 
         * @param connection a connection
 
47
         * Constructor that provides a database upon which to act
 
48
         * @param database a database
119
49
         */
120
 
        explicit immediate_transaction(
121
 
                connection &connection );
 
50
        explicit basic_transaction(
 
51
                database &database );
122
52
 
123
53
//______________________________________________________________________________
124
54
//                                                              public interface
129
59
         */
130
60
        virtual void begin();
131
61
 
 
62
        /**
 
63
         * Commit the transaction
 
64
         */
 
65
        virtual void commit();
 
66
 
 
67
        /**
 
68
         * Rollback the transaction
 
69
         */
 
70
        virtual void rollback();
 
71
 
 
72
//______________________________________________________________________________
 
73
//                                                                implementation
 
74
protected:
 
75
 
 
76
        /* the database on which to act */
 
77
        database &_database;
 
78
 
132
79
};
133
80
 
134
81
 
135
82
////////////////////////////////////////////////////////////////////////////////
136
83
 
137
84
 
138
 
/**
139
 
 * An exclusive transaction
140
 
 */
141
85
class exclusive_transaction
142
86
        :
143
 
        public detail::basic_transaction
 
87
        public basic_transaction
144
88
{
145
89
//______________________________________________________________________________
146
90
//                                                                 instantiation
147
91
public:
148
92
 
149
93
        /**
150
 
         * Constructor that provides a connection upon which to act
151
 
         * @param connection a connection
 
94
         * Constructor that provides a database upon which to act
 
95
         * @param database a database
152
96
         */
153
97
        explicit exclusive_transaction(
154
 
                connection &connection );
 
98
                database &database );
155
99
 
156
100
//______________________________________________________________________________
157
101
//                                                              public interface
168
112
////////////////////////////////////////////////////////////////////////////////
169
113
 
170
114
 
171
 
/**
172
 
 * A recursive transaction, allowing transactions to be nested.
173
 
 */
174
115
class recursive_transaction
175
116
        :
176
 
        public detail::basic_transaction
 
117
        public basic_transaction
177
118
{
178
119
//______________________________________________________________________________
179
120
//                                                                 instantiation
180
121
public:
181
122
 
182
123
        /**
183
 
         * Constructor that provides a connection upon which to act
184
 
         * @param connection a connection
 
124
         * Constructor that provides a database upon which to act
 
125
         * @param database a database
185
126
         */
186
127
        explicit recursive_transaction(
187
 
                connection &connection );
 
128
                database &database );
188
129
 
189
130
//______________________________________________________________________________
190
131
//                                                              public interface
218
159
////////////////////////////////////////////////////////////////////////////////
219
160
 
220
161
 
221
 
/**
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.
225
 
 */
226
 
template< class T = deferred_transaction >
 
162
template< class T = basic_transaction >
227
163
class transaction_guard
228
164
        :
229
165
        private boost::noncopyable
233
169
public:
234
170
 
235
171
        /**
236
 
         * Constructor that provides a connection upon which to act
237
 
         * @param connection a connection
 
172
         * Constructor that provides a database upon which to act
 
173
         * @param database a database
238
174
         */
239
175
        explicit transaction_guard(
240
 
                connection &connection )
 
176
                database &database )
241
177
                :
242
 
                _transaction( connection ),
 
178
                _transaction( database ),
243
179
                _released( false )
244
180
        {
245
181
                _transaction.begin();
247
183
 
248
184
        ~transaction_guard()
249
185
        {
250
 
                if( !_released )
251
 
                        _transaction.rollback();
 
186
                if( !_released ) {
 
187
                        try {
 
188
                                _transaction.rollback();
 
189
                        }
 
190
                        catch( ... ) {
 
191
                        }
 
192
                }
252
193
        }
253
194
 
254
195
//______________________________________________________________________________
266
207
                }
267
208
        }
268
209
 
269
 
        /**
270
 
         * Rollback the transaction early
271
 
         */
272
 
        void rollback()
273
 
        {
274
 
                if( !_released ) {
275
 
                        _transaction.rollback();
276
 
                        _released = true;
277
 
                }
278
 
        }
279
 
 
280
210
//______________________________________________________________________________
281
211
//                                                                implementation
282
212
protected:
283
213
 
284
 
        /** the transaction */
 
214
        /* the transaction */
285
215
        T _transaction;
286
216
 
287
 
        /** have we released the transaction yet? */
 
217
        /* have we released the transaction yet? */
288
218
        bool _released;
289
219
 
290
220
};