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