/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: Tim Marston
  • Date: 2015-02-26 08:59:36 UTC
  • Revision ID: tim@ed.am-20150226085936-xe43in8bfz6f6nxb
fixed some missing/incorrect includes

Show diffs side-by-side

added added

removed removed

1
1
/*
2
2
 * transaction.h
3
3
 *
4
 
 * Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
 
4
 * Copyright (C) 2009 Tim Marston <tim@ed.am>
5
5
 *
6
6
 * This file is part of sqlite3cc (hereafter referred to as "this program").
7
 
 * See http://www.waxworlds.org/edam/software/sqlite3cc for more information.
8
 
 *
9
 
 * This program is free software: you can redistribute it and/or modify
10
 
 * it under the terms of the GNU Lesser General Public License as published
11
 
 * by the Free Software Foundation, either version 3 of the License, or
12
 
 * (at your option) any later version.
13
 
 *
14
 
 * This program is distributed in the hope that it will be useful,
15
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 
 * GNU Lesser General Public License for more details.
 
7
 * See http://ed.am/dev/sqlite3cc for more information.
 
8
 *
 
9
 * This program is free software: you can redistribute it and/or modify it under
 
10
 * the terms of the GNU Lesser General Public License as published by the Free
 
11
 * Software Foundation, either version 3 of the License, or (at your option) any
 
12
 * later version.
 
13
 *
 
14
 * This program is distributed in the hope that it will be useful, but WITHOUT
 
15
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
16
 * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
 
17
 * details.
18
18
 *
19
19
 * You should have received a copy of the GNU Lesser General Public License
20
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
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
                 *
 
55
                 * @param connection a connection
 
56
                 */
 
57
                explicit basic_transaction(
 
58
                        connection &connection );
 
59
 
 
60
        //__________________________________________________________________________
 
61
        //                                                          public interface
 
62
        public:
 
63
 
 
64
                /**
 
65
                 * Begin the transaction
 
66
                 */
 
67
                virtual void begin();
 
68
 
 
69
                /**
 
70
                 * Commit the transaction
 
71
                 */
 
72
                virtual void commit();
 
73
 
 
74
                /**
 
75
                 * Rollback the transaction
 
76
                 */
 
77
                virtual void rollback();
 
78
 
 
79
        //__________________________________________________________________________
 
80
        //                                                            implementation
 
81
        protected:
 
82
 
 
83
                /** reset any in-progress statements */
 
84
                void reset_active_queries();
 
85
 
 
86
                /** the connection on which to act */
 
87
                connection &_connection;
 
88
 
 
89
        };
 
90
 
 
91
} // namespace detail
 
92
 
 
93
 
 
94
////////////////////////////////////////////////////////////////////////////////
 
95
 
 
96
 
 
97
/**
 
98
 * A deferred transaction.
 
99
 */
 
100
typedef detail::basic_transaction deferred_transaction;
 
101
 
 
102
 
 
103
////////////////////////////////////////////////////////////////////////////////
 
104
 
 
105
 
 
106
/**
 
107
 * An immediate transaction.
 
108
 */
 
109
class immediate_transaction
39
110
        :
40
 
        private boost::noncopyable
 
111
        public detail::basic_transaction
41
112
{
42
113
//______________________________________________________________________________
43
114
//                                                                 instantiation
44
115
public:
45
116
 
46
117
        /**
47
 
         * Constructor that provides a database upon which to act
48
 
         * @param database a database
 
118
         * Constructor that provides a connection upon which to act.
 
119
         *
 
120
         * @param connection a connection
49
121
         */
50
 
        explicit basic_transaction(
51
 
                database &database );
 
122
        explicit immediate_transaction(
 
123
                connection &connection );
52
124
 
53
125
//______________________________________________________________________________
54
126
//                                                              public interface
55
127
public:
56
128
 
57
129
        /**
58
 
         * Begin the transaction
 
130
         * Begin the transaction.
59
131
         */
60
132
        virtual void begin();
61
133
 
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
134
};
80
135
 
81
136
 
82
137
////////////////////////////////////////////////////////////////////////////////
83
138
 
84
139
 
 
140
/**
 
141
 * An exclusive transaction.
 
142
 */
85
143
class exclusive_transaction
86
144
        :
87
 
        public basic_transaction
 
145
        public detail::basic_transaction
88
146
{
89
147
//______________________________________________________________________________
90
148
//                                                                 instantiation
91
149
public:
92
150
 
93
151
        /**
94
 
         * Constructor that provides a database upon which to act
95
 
         * @param database a database
 
152
         * Constructor that provides a connection upon which to act.
 
153
         *
 
154
         * @param connection a connection
96
155
         */
97
156
        explicit exclusive_transaction(
98
 
                database &database );
 
157
                connection &connection );
99
158
 
100
159
//______________________________________________________________________________
101
160
//                                                              public interface
112
171
////////////////////////////////////////////////////////////////////////////////
113
172
 
114
173
 
 
174
/**
 
175
 * A recursive transaction, allowing transactions to be nested.
 
176
 */
115
177
class recursive_transaction
116
178
        :
117
 
        public basic_transaction
 
179
        public detail::basic_transaction
118
180
{
119
181
//______________________________________________________________________________
120
182
//                                                                 instantiation
121
183
public:
122
184
 
123
185
        /**
124
 
         * Constructor that provides a database upon which to act
125
 
         * @param database a database
 
186
         * Constructor that provides a connection upon which to act.
 
187
         *
 
188
         * @param connection a connection
126
189
         */
127
190
        explicit recursive_transaction(
128
 
                database &database );
 
191
                connection &connection );
129
192
 
130
193
//______________________________________________________________________________
131
194
//                                                              public interface
132
195
public:
133
196
 
134
197
        /**
135
 
         * Begin the transaction
 
198
         * Begin the transaction.
136
199
         */
137
200
        virtual void begin();
138
201
 
139
202
        /**
140
 
         * Commit the transaction
 
203
         * Commit the transaction.
141
204
         */
142
205
        virtual void commit();
143
206
 
144
207
        /**
145
 
         * Rollback the transaction
 
208
         * Rollback the transaction.
146
209
         */
147
210
        virtual void rollback();
148
211
 
159
222
////////////////////////////////////////////////////////////////////////////////
160
223
 
161
224
 
162
 
template< class T = basic_transaction >
 
225
/**
 
226
 * A scope guard (sentinel) for use with one of the transaction classes to
 
227
 * provide RAII-style transactions.  It defaults to using deferred transactions.
 
228
 */
 
229
template< class T = deferred_transaction >
163
230
class transaction_guard
164
231
        :
165
232
        private boost::noncopyable
169
236
public:
170
237
 
171
238
        /**
172
 
         * Constructor that provides a database upon which to act
173
 
         * @param database a database
 
239
         * Constructor that provides a connection upon which to act.
 
240
         *
 
241
         * @param connection a connection
174
242
         */
175
243
        explicit transaction_guard(
176
 
                database &database )
 
244
                connection &connection )
177
245
                :
178
 
                _transaction( database ),
179
 
                _released( false )
 
246
                _transaction( connection ),
 
247
                _released( true )
180
248
        {
181
249
                _transaction.begin();
 
250
                _released = false;
182
251
        }
183
252
 
184
253
        ~transaction_guard()
185
254
        {
186
 
                if( !_released ) {
187
 
                        try {
188
 
                                _transaction.rollback();
189
 
                        }
190
 
                        catch( ... ) {
191
 
                        }
192
 
                }
 
255
                if( !_released )
 
256
                        _transaction.rollback();
193
257
        }
194
258
 
195
259
//______________________________________________________________________________
197
261
public:
198
262
 
199
263
        /**
200
 
         * Commit the transaction
 
264
         * Begin the transaction. Note that this is done automatically in the
 
265
         * constructor.
 
266
         */
 
267
        void begin()
 
268
        {
 
269
                if( _released ) {
 
270
                        _transaction.begin();
 
271
                        _released = false;
 
272
                }
 
273
        }
 
274
 
 
275
        /**
 
276
         * Commit the transaction.
201
277
         */
202
278
        void commit()
203
279
        {
207
283
                }
208
284
        }
209
285
 
 
286
        /**
 
287
         * Rollback the transaction. Note that this is done automatically in the
 
288
         * destructor if the transaction hasn't otherwise been completed.
 
289
         */
 
290
        void rollback()
 
291
        {
 
292
                if( !_released ) {
 
293
                        _transaction.rollback();
 
294
                        _released = true;
 
295
                }
 
296
        }
 
297
 
210
298
//______________________________________________________________________________
211
299
//                                                                implementation
212
300
protected:
213
301
 
214
 
        /* the transaction */
 
302
        /** the transaction */
215
303
        T _transaction;
216
304
 
217
 
        /* have we released the transaction yet? */
 
305
        /** have we released the transaction yet? */
218
306
        bool _released;
219
307
 
220
308
};