/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:
25
25
 
26
26
 
27
27
#include <boost/utility.hpp>
 
28
#include <string>
28
29
 
29
30
 
30
31
namespace sqlite
31
32
{
32
33
 
33
34
 
34
 
class database;
35
 
 
36
 
 
37
 
class transaction
38
 
        :
39
 
        private boost::noncopyable
40
 
{
41
 
//______________________________________________________________________________
42
 
//                                                                 instantiation
43
 
public:
44
 
 
45
 
        /**
46
 
         * Constructor that provides a database upon which to act
47
 
         * @param database a database
48
 
         */
49
 
        explicit transaction(
50
 
                database &database );
51
 
 
52
 
        virtual ~transaction() throw( );
53
 
 
54
 
protected:
55
 
 
56
 
        /**
57
 
         * Constructor that provides a way for deriving classes to override the SQL
58
 
         * executed in beginning and rolling-back a transaction during construction
59
 
         * and destruction.
60
 
         * @param database a database
61
 
         * @param begin_sql the SQL statement used to begin the transaction
62
 
         * @param rollback_sql the SQL statement used to rollback the transaction,
63
 
         *              or an empty string if the default is to be used.
64
 
         */
65
 
        transaction(
66
 
                database &database,
67
 
                const std::string &begin_sql,
68
 
                const std::string &rollback_sql = "" );
69
 
 
70
 
//______________________________________________________________________________
71
 
//                                                              public interface
 
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
 
109
        :
 
110
        public detail::basic_transaction
 
111
{
 
112
//______________________________________________________________________________
 
113
//                                                                 instantiation
 
114
public:
 
115
 
 
116
        /**
 
117
         * Constructor that provides a connection upon which to act
 
118
         * @param connection a connection
 
119
         */
 
120
        explicit immediate_transaction(
 
121
                connection &connection );
 
122
 
 
123
//______________________________________________________________________________
 
124
//                                                              public interface
 
125
public:
 
126
 
 
127
        /**
 
128
         * Begin the transaction
 
129
         */
 
130
        virtual void begin();
 
131
 
 
132
};
 
133
 
 
134
 
 
135
////////////////////////////////////////////////////////////////////////////////
 
136
 
 
137
 
 
138
/**
 
139
 * An exclusive transaction
 
140
 */
 
141
class exclusive_transaction
 
142
        :
 
143
        public detail::basic_transaction
 
144
{
 
145
//______________________________________________________________________________
 
146
//                                                                 instantiation
 
147
public:
 
148
 
 
149
        /**
 
150
         * Constructor that provides a connection upon which to act
 
151
         * @param connection a connection
 
152
         */
 
153
        explicit exclusive_transaction(
 
154
                connection &connection );
 
155
 
 
156
//______________________________________________________________________________
 
157
//                                                              public interface
 
158
public:
 
159
 
 
160
        /**
 
161
         * Begin the transaction
 
162
         */
 
163
        virtual void begin();
 
164
 
 
165
};
 
166
 
 
167
 
 
168
////////////////////////////////////////////////////////////////////////////////
 
169
 
 
170
 
 
171
/**
 
172
 * A recursive transaction, allowing transactions to be nested.
 
173
 */
 
174
class recursive_transaction
 
175
        :
 
176
        public detail::basic_transaction
 
177
{
 
178
//______________________________________________________________________________
 
179
//                                                                 instantiation
 
180
public:
 
181
 
 
182
        /**
 
183
         * Constructor that provides a connection upon which to act
 
184
         * @param connection a connection
 
185
         */
 
186
        explicit recursive_transaction(
 
187
                connection &connection );
 
188
 
 
189
//______________________________________________________________________________
 
190
//                                                              public interface
 
191
public:
 
192
 
 
193
        /**
 
194
         * Begin the transaction
 
195
         */
 
196
        virtual void begin();
72
197
 
73
198
        /**
74
199
         * Commit the transaction
78
203
        /**
79
204
         * Rollback the transaction
80
205
         */
81
 
        void rollback();
 
206
        virtual void rollback();
82
207
 
83
208
//______________________________________________________________________________
84
209
//                                                                implementation
85
210
protected:
86
211
 
87
 
        /** the database */
88
 
        database &_database;
89
 
 
90
 
        /** the SQL used to rollback the transaction, or empty to use default */
91
 
        std::string _rollback_sql;
 
212
        /* this transaction's savepoint name */
 
213
        std::string _sp_name;
92
214
 
93
215
};
94
216
 
96
218
////////////////////////////////////////////////////////////////////////////////
97
219
 
98
220
 
99
 
class exclusive_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 >
 
226
class transaction_guard
100
227
        :
101
 
        public transaction
 
228
        private boost::noncopyable
102
229
{
103
230
//______________________________________________________________________________
104
231
//                                                                 instantiation
105
232
public:
106
233
 
107
234
        /**
108
 
         * Constructor that provides a database upon which to act
109
 
         * @param database a database
110
 
         */
111
 
        exclusive_transaction(
112
 
                database &database );
 
235
         * Constructor that provides a connection upon which to act
 
236
         * @param connection a connection
 
237
         */
 
238
        explicit transaction_guard(
 
239
                connection &connection )
 
240
                :
 
241
                _transaction( connection ),
 
242
                _released( false )
 
243
        {
 
244
                _transaction.begin();
 
245
        }
 
246
 
 
247
        ~transaction_guard()
 
248
        {
 
249
                if( !_released )
 
250
                        _transaction.rollback();
 
251
        }
 
252
 
 
253
//______________________________________________________________________________
 
254
//                                                              public interface
 
255
public:
 
256
 
 
257
        /**
 
258
         * Commit the transaction
 
259
         */
 
260
        void commit()
 
261
        {
 
262
                if( !_released ) {
 
263
                        _transaction.commit();
 
264
                        _released = true;
 
265
                }
 
266
        }
 
267
 
 
268
        /**
 
269
         * Rollback the transaction early
 
270
         */
 
271
        void rollback()
 
272
        {
 
273
                if( !_released ) {
 
274
                        _transaction.rollback();
 
275
                        _released = true;
 
276
                }
 
277
        }
 
278
 
 
279
//______________________________________________________________________________
 
280
//                                                                implementation
 
281
protected:
 
282
 
 
283
        /** the transaction */
 
284
        T _transaction;
 
285
 
 
286
        /** have we released the transaction yet? */
 
287
        bool _released;
113
288
 
114
289
};
115
290