/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:46:42 UTC
  • Revision ID: edam@waxworlds.org-20100727154642-1uxrjkpxhp7xl6hq
- moved null_t, exec_t and set_index_t to detail namespace so only their extern instantiations are in the main namespace
- added immediate transation

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