/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:12:55 UTC
  • Revision ID: edam@waxworlds.org-20100727151255-goaqgdz4kj13q7gz
- update TODO
- added some missing includes for <string>
- changed usage of database::exec() to not require return code!
- prevented transaction_guard destructor from throwing an exception

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
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
 
35
class database;
 
36
 
 
37
 
 
38
class basic_transaction
110
39
        :
111
 
        public detail::basic_transaction
 
40
        private boost::noncopyable
112
41
{
113
42
//______________________________________________________________________________
114
43
//                                                                 instantiation
115
44
public:
116
45
 
117
46
        /**
118
 
         * Constructor that provides a connection upon which to act.
119
 
         *
120
 
         * @param connection a connection
 
47
         * Constructor that provides a database upon which to act
 
48
         * @param database a database
121
49
         */
122
 
        explicit immediate_transaction(
123
 
                connection &connection );
 
50
        explicit basic_transaction(
 
51
                database &database );
124
52
 
125
53
//______________________________________________________________________________
126
54
//                                                              public interface
127
55
public:
128
56
 
129
57
        /**
130
 
         * Begin the transaction.
 
58
         * Begin the transaction
131
59
         */
132
60
        virtual void begin();
133
61
 
 
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
 
134
79
};
135
80
 
136
81
 
137
82
////////////////////////////////////////////////////////////////////////////////
138
83
 
139
84
 
140
 
/**
141
 
 * An exclusive transaction.
142
 
 */
143
85
class exclusive_transaction
144
86
        :
145
 
        public detail::basic_transaction
 
87
        public basic_transaction
146
88
{
147
89
//______________________________________________________________________________
148
90
//                                                                 instantiation
149
91
public:
150
92
 
151
93
        /**
152
 
         * Constructor that provides a connection upon which to act.
153
 
         *
154
 
         * @param connection a connection
 
94
         * Constructor that provides a database upon which to act
 
95
         * @param database a database
155
96
         */
156
97
        explicit exclusive_transaction(
157
 
                connection &connection );
 
98
                database &database );
158
99
 
159
100
//______________________________________________________________________________
160
101
//                                                              public interface
171
112
////////////////////////////////////////////////////////////////////////////////
172
113
 
173
114
 
174
 
/**
175
 
 * A recursive transaction, allowing transactions to be nested.
176
 
 */
177
115
class recursive_transaction
178
116
        :
179
 
        public detail::basic_transaction
 
117
        public basic_transaction
180
118
{
181
119
//______________________________________________________________________________
182
120
//                                                                 instantiation
183
121
public:
184
122
 
185
123
        /**
186
 
         * Constructor that provides a connection upon which to act.
187
 
         *
188
 
         * @param connection a connection
 
124
         * Constructor that provides a database upon which to act
 
125
         * @param database a database
189
126
         */
190
127
        explicit recursive_transaction(
191
 
                connection &connection );
 
128
                database &database );
192
129
 
193
130
//______________________________________________________________________________
194
131
//                                                              public interface
195
132
public:
196
133
 
197
134
        /**
198
 
         * Begin the transaction.
 
135
         * Begin the transaction
199
136
         */
200
137
        virtual void begin();
201
138
 
202
139
        /**
203
 
         * Commit the transaction.
 
140
         * Commit the transaction
204
141
         */
205
142
        virtual void commit();
206
143
 
207
144
        /**
208
 
         * Rollback the transaction.
 
145
         * Rollback the transaction
209
146
         */
210
147
        virtual void rollback();
211
148
 
222
159
////////////////////////////////////////////////////////////////////////////////
223
160
 
224
161
 
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 >
 
162
template< class T = basic_transaction >
230
163
class transaction_guard
231
164
        :
232
165
        private boost::noncopyable
236
169
public:
237
170
 
238
171
        /**
239
 
         * Constructor that provides a connection upon which to act.
240
 
         *
241
 
         * @param connection a connection
 
172
         * Constructor that provides a database upon which to act
 
173
         * @param database a database
242
174
         */
243
175
        explicit transaction_guard(
244
 
                connection &connection )
 
176
                database &database )
245
177
                :
246
 
                _transaction( connection ),
247
 
                _released( true )
 
178
                _transaction( database ),
 
179
                _released( false )
248
180
        {
249
181
                _transaction.begin();
250
 
                _released = false;
251
182
        }
252
183
 
253
184
        ~transaction_guard()
254
185
        {
255
 
                if( !_released )
256
 
                        _transaction.rollback();
 
186
                if( !_released ) {
 
187
                        try {
 
188
                                _transaction.rollback();
 
189
                        }
 
190
                        catch( ... ) {
 
191
                        }
 
192
                }
257
193
        }
258
194
 
259
195
//______________________________________________________________________________
261
197
public:
262
198
 
263
199
        /**
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.
 
200
         * Commit the transaction
277
201
         */
278
202
        void commit()
279
203
        {
283
207
                }
284
208
        }
285
209
 
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
210
//______________________________________________________________________________
299
211
//                                                                implementation
300
212
protected:
301
213
 
302
 
        /** the transaction */
 
214
        /* the transaction */
303
215
        T _transaction;
304
216
 
305
 
        /** have we released the transaction yet? */
 
217
        /* have we released the transaction yet? */
306
218
        bool _released;
307
219
 
308
220
};