/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:47:08 UTC
  • Revision ID: edam@waxworlds.org-20120123134708-ol4tilkotsm3han0
updated build system

Show diffs side-by-side

added added

removed removed

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