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