/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 (the default)
98
 
 */
99
 
typedef detail::basic_transaction deferred_tranaction;
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 classes to
223
 
 * provide RIAA-style transactions.
224
 
 */
225
 
template< class T = deferred_tranaction >
 
162
template< class T = basic_transaction >
226
163
class transaction_guard
227
164
        :
228
165
        private boost::noncopyable
232
169
public:
233
170
 
234
171
        /**
235
 
         * Constructor that provides a connection upon which to act
236
 
         * @param connection a connection
 
172
         * Constructor that provides a database upon which to act
 
173
         * @param database a database
237
174
         */
238
175
        explicit transaction_guard(
239
 
                connection &connection )
 
176
                database &database )
240
177
                :
241
 
                _transaction( connection ),
 
178
                _transaction( database ),
242
179
                _released( false )
243
180
        {
244
181
                _transaction.begin();
246
183
 
247
184
        ~transaction_guard()
248
185
        {
249
 
                if( !_released )
250
 
                        _transaction.rollback();
 
186
                if( !_released ) {
 
187
                        try {
 
188
                                _transaction.rollback();
 
189
                        }
 
190
                        catch( ... ) {
 
191
                        }
 
192
                }
251
193
        }
252
194
 
253
195
//______________________________________________________________________________
265
207
                }
266
208
        }
267
209
 
268
 
        /**
269
 
         * Rollback the transaction early
270
 
         */
271
 
        void rollback()
272
 
        {
273
 
                if( !_released ) {
274
 
                        _transaction.rollback();
275
 
                        _released = true;
276
 
                }
277
 
        }
278
 
 
279
210
//______________________________________________________________________________
280
211
//                                                                implementation
281
212
protected:
282
213
 
283
 
        /** the transaction */
 
214
        /* the transaction */
284
215
        T _transaction;
285
216
 
286
 
        /** have we released the transaction yet? */
 
217
        /* have we released the transaction yet? */
287
218
        bool _released;
288
219
 
289
220
};