/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-29 09:16:26 UTC
  • Revision ID: edam@waxworlds.org-20100729091626-h8fmg0r74eyfo5ae
- fixed error caused by finialising in-progress queries during rollback that were later finaliased by RAII.

Show diffs side-by-side

added added

removed removed

1
1
/*
2
2
 * transaction.h
3
3
 *
4
 
 * Copyright (C) 2009 Tim Marston <tim@ed.am>
 
4
 * Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
5
5
 *
6
6
 * This file is part of sqlite3cc (hereafter referred to as "this program").
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.
 
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.
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 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
 
                 *
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.
 
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
        /** close any in-progress statements */
 
80
        void invalidate_queries();
 
81
 
 
82
        /** the database on which to act */
 
83
        database &_database;
 
84
 
 
85
};
 
86
 
 
87
 
 
88
////////////////////////////////////////////////////////////////////////////////
 
89
 
 
90
 
 
91
/**
 
92
 * An exclusive transaction
108
93
 */
109
94
class immediate_transaction
110
95
        :
111
 
        public detail::basic_transaction
 
96
        public basic_transaction
112
97
{
113
98
//______________________________________________________________________________
114
99
//                                                                 instantiation
115
100
public:
116
101
 
117
102
        /**
118
 
         * Constructor that provides a connection upon which to act.
119
 
         *
120
 
         * @param connection a connection
 
103
         * Constructor that provides a database upon which to act
 
104
         * @param database a database
121
105
         */
122
106
        explicit immediate_transaction(
123
 
                connection &connection );
 
107
                database &database );
124
108
 
125
109
//______________________________________________________________________________
126
110
//                                                              public interface
127
111
public:
128
112
 
129
113
        /**
130
 
         * Begin the transaction.
 
114
         * Begin the transaction
131
115
         */
132
116
        virtual void begin();
133
117
 
138
122
 
139
123
 
140
124
/**
141
 
 * An exclusive transaction.
 
125
 * An exclusive transaction
142
126
 */
143
127
class exclusive_transaction
144
128
        :
145
 
        public detail::basic_transaction
 
129
        public basic_transaction
146
130
{
147
131
//______________________________________________________________________________
148
132
//                                                                 instantiation
149
133
public:
150
134
 
151
135
        /**
152
 
         * Constructor that provides a connection upon which to act.
153
 
         *
154
 
         * @param connection a connection
 
136
         * Constructor that provides a database upon which to act
 
137
         * @param database a database
155
138
         */
156
139
        explicit exclusive_transaction(
157
 
                connection &connection );
 
140
                database &database );
158
141
 
159
142
//______________________________________________________________________________
160
143
//                                                              public interface
176
159
 */
177
160
class recursive_transaction
178
161
        :
179
 
        public detail::basic_transaction
 
162
        public basic_transaction
180
163
{
181
164
//______________________________________________________________________________
182
165
//                                                                 instantiation
183
166
public:
184
167
 
185
168
        /**
186
 
         * Constructor that provides a connection upon which to act.
187
 
         *
188
 
         * @param connection a connection
 
169
         * Constructor that provides a database upon which to act
 
170
         * @param database a database
189
171
         */
190
172
        explicit recursive_transaction(
191
 
                connection &connection );
 
173
                database &database );
192
174
 
193
175
//______________________________________________________________________________
194
176
//                                                              public interface
195
177
public:
196
178
 
197
179
        /**
198
 
         * Begin the transaction.
 
180
         * Begin the transaction
199
181
         */
200
182
        virtual void begin();
201
183
 
202
184
        /**
203
 
         * Commit the transaction.
 
185
         * Commit the transaction
204
186
         */
205
187
        virtual void commit();
206
188
 
207
189
        /**
208
 
         * Rollback the transaction.
 
190
         * Rollback the transaction
209
191
         */
210
192
        virtual void rollback();
211
193
 
223
205
 
224
206
 
225
207
/**
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.
 
208
 * A scope guard, or sentinel for use with one of the transaction classes.
228
209
 */
229
 
template< class T = deferred_transaction >
 
210
template< class T = basic_transaction >
230
211
class transaction_guard
231
212
        :
232
213
        private boost::noncopyable
236
217
public:
237
218
 
238
219
        /**
239
 
         * Constructor that provides a connection upon which to act.
240
 
         *
241
 
         * @param connection a connection
 
220
         * Constructor that provides a database upon which to act
 
221
         * @param database a database
242
222
         */
243
223
        explicit transaction_guard(
244
 
                connection &connection )
 
224
                database &database )
245
225
                :
246
 
                _transaction( connection ),
247
 
                _released( true )
 
226
                _transaction( database ),
 
227
                _released( false )
248
228
        {
249
229
                _transaction.begin();
250
 
                _released = false;
251
230
        }
252
231
 
253
232
        ~transaction_guard()
254
233
        {
255
 
                if( !_released )
256
 
                        _transaction.rollback();
 
234
                if( !_released ) {
 
235
                        try {
 
236
                                _transaction.rollback();
 
237
                        }
 
238
                        catch( ... ) {
 
239
                        }
 
240
                }
257
241
        }
258
242
 
259
243
//______________________________________________________________________________
261
245
public:
262
246
 
263
247
        /**
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.
 
248
         * Commit the transaction
277
249
         */
278
250
        void commit()
279
251
        {
283
255
                }
284
256
        }
285
257
 
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
 
 
298
258
//______________________________________________________________________________
299
259
//                                                                implementation
300
260
protected: