/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: 2014-01-03 12:45:31 UTC
  • Revision ID: tim@ed.am-20140103124531-ehrcs7qw7rf6b1iu
updated bzrignore

Show diffs side-by-side

added added

removed removed

1
1
/*
2
 
 * transaction.hpp
3
 
 *
4
 
 * Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
5
 
 *
6
 
 * This file is part of sqlitepp (hereafter referred to as "this program").
7
 
 * See http://www.waxworlds.org/edam/software/sqlitepp 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.
 
2
 * transaction.h
 
3
 *
 
4
 * Copyright (C) 2009 Tim Marston <tim@ed.am>
 
5
 *
 
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.
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/>.
21
21
 */
22
22
 
23
 
#ifndef TRANSACTION_HPP_
24
 
#define TRANSACTION_HPP_
 
23
#ifndef SQLITE3CC_TRANSACTION_H_
 
24
#define SQLITE3CC_TRANSACTION_H_
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 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
 
110
        :
 
111
        public detail::basic_transaction
 
112
{
 
113
//______________________________________________________________________________
 
114
//                                                                 instantiation
 
115
public:
 
116
 
 
117
        /**
 
118
         * Constructor that provides a connection upon which to act.
 
119
         *
 
120
         * @param connection a connection
 
121
         */
 
122
        explicit immediate_transaction(
 
123
                connection &connection );
 
124
 
 
125
//______________________________________________________________________________
 
126
//                                                              public interface
 
127
public:
 
128
 
 
129
        /**
 
130
         * Begin the transaction.
 
131
         */
 
132
        virtual void begin();
 
133
 
 
134
};
 
135
 
 
136
 
 
137
////////////////////////////////////////////////////////////////////////////////
 
138
 
 
139
 
 
140
/**
 
141
 * An exclusive transaction.
 
142
 */
 
143
class exclusive_transaction
 
144
        :
 
145
        public detail::basic_transaction
 
146
{
 
147
//______________________________________________________________________________
 
148
//                                                                 instantiation
 
149
public:
 
150
 
 
151
        /**
 
152
         * Constructor that provides a connection upon which to act.
 
153
         *
 
154
         * @param connection a connection
 
155
         */
 
156
        explicit exclusive_transaction(
 
157
                connection &connection );
 
158
 
 
159
//______________________________________________________________________________
 
160
//                                                              public interface
 
161
public:
 
162
 
 
163
        /**
 
164
         * Begin the transaction
 
165
         */
 
166
        virtual void begin();
 
167
 
 
168
};
 
169
 
 
170
 
 
171
////////////////////////////////////////////////////////////////////////////////
 
172
 
 
173
 
 
174
/**
 
175
 * A recursive transaction, allowing transactions to be nested.
 
176
 */
 
177
class recursive_transaction
 
178
        :
 
179
        public detail::basic_transaction
 
180
{
 
181
//______________________________________________________________________________
 
182
//                                                                 instantiation
 
183
public:
 
184
 
 
185
        /**
 
186
         * Constructor that provides a connection upon which to act.
 
187
         *
 
188
         * @param connection a connection
 
189
         */
 
190
        explicit recursive_transaction(
 
191
                connection &connection );
 
192
 
 
193
//______________________________________________________________________________
 
194
//                                                              public interface
 
195
public:
 
196
 
 
197
        /**
 
198
         * Begin the transaction.
 
199
         */
 
200
        virtual void begin();
 
201
 
 
202
        /**
 
203
         * Commit the transaction.
 
204
         */
 
205
        virtual void commit();
 
206
 
 
207
        /**
 
208
         * Rollback the transaction.
 
209
         */
 
210
        virtual void rollback();
 
211
 
 
212
//______________________________________________________________________________
 
213
//                                                                implementation
 
214
protected:
 
215
 
 
216
        /* this transaction's savepoint name */
 
217
        std::string _sp_name;
 
218
 
 
219
};
 
220
 
 
221
 
 
222
////////////////////////////////////////////////////////////////////////////////
 
223
 
 
224
 
 
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 >
 
230
class transaction_guard
38
231
        :
39
232
        private boost::noncopyable
40
233
{
43
236
public:
44
237
 
45
238
        /**
46
 
         * Constructor that provides a database upon which to act
47
 
         * @param database a database
48
 
         */
49
 
        transaction(
50
 
                database &database );
51
 
 
52
 
        virtual ~transaction() throw( );
53
 
 
54
 
protected:
55
 
 
56
 
        /**
57
 
         * Constructor that provides a way for deriving classes to override the SQL
58
 
         * executed in beginning and rolling-back a transaction during construction
59
 
         * and destruction.
60
 
         * @param database a database
61
 
         * @param begin_sql the SQL statement used to begin the transaction
62
 
         * @param rollback_sql the SQL statement used to rollback the transaction,
63
 
         *              or an empty string if the default is to be used.
64
 
         */
65
 
        transaction(
66
 
                database &database,
67
 
                const std::string &begin_sql,
68
 
                const std::string &rollback_sql = "" );
 
239
         * Constructor that provides a connection upon which to act.
 
240
         *
 
241
         * @param connection a connection
 
242
         */
 
243
        explicit transaction_guard(
 
244
                connection &connection )
 
245
                :
 
246
                _transaction( connection ),
 
247
                _released( true )
 
248
        {
 
249
                _transaction.begin();
 
250
                _released = false;
 
251
        }
 
252
 
 
253
        ~transaction_guard()
 
254
        {
 
255
                if( !_released )
 
256
                        _transaction.rollback();
 
257
        }
69
258
 
70
259
//______________________________________________________________________________
71
260
//                                                              public interface
72
 
 
73
 
        /**
74
 
         * Commit the transaction
75
 
         */
76
 
        virtual void commit();
77
 
 
78
 
        /**
79
 
         * Rollback the transaction
80
 
         */
81
 
        void rollback();
 
261
public:
 
262
 
 
263
        /**
 
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.
 
277
         */
 
278
        void commit()
 
279
        {
 
280
                if( !_released ) {
 
281
                        _transaction.commit();
 
282
                        _released = true;
 
283
                }
 
284
        }
 
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
        }
82
297
 
83
298
//______________________________________________________________________________
84
299
//                                                                implementation
85
300
protected:
86
301
 
87
 
        /** the database */
88
 
        database &_database;
89
 
 
90
 
        /** the SQL used to rollback the transaction, or empty to use default */
91
 
        std::string _rollback_sql;
92
 
 
93
 
};
94
 
 
95
 
 
96
 
////////////////////////////////////////////////////////////////////////////////
97
 
 
98
 
 
99
 
class exclusive_transaction
100
 
        :
101
 
        public transaction
102
 
{
103
 
//______________________________________________________________________________
104
 
//                                                                 instantiation
105
 
public:
106
 
 
107
 
        /**
108
 
         * Constructor that provides a database upon which to act
109
 
         * @param database a database
110
 
         */
111
 
        exclusive_transaction(
112
 
                database &database );
113
 
 
114
 
};
115
 
 
116
 
 
117
 
}
118
 
 
119
 
 
120
 
#endif /* TRANSACTION_HPP_ */
 
302
        /** the transaction */
 
303
        T _transaction;
 
304
 
 
305
        /** have we released the transaction yet? */
 
306
        bool _released;
 
307
 
 
308
};
 
309
 
 
310
 
 
311
} // namespace sqlite
 
312
 
 
313
 
 
314
#endif /* SQLITE3CC_TRANSACTION_H_ */