/sqlite3cc

To get this branch, use:
bzr branch http://bzr.ed.am/sqlite3cc

« back to all changes in this revision

Viewing changes to include/sqlitepp/transaction.hpp

  • Committer: edam
  • Date: 2009-11-25 20:32:34 UTC
  • Revision ID: edam@waxworlds.org-20091125203234-24i7mzd60c7o46py
- initial commit
- basic support for: database, statements, commands, transactions, exceptions
- initial test app
- initial liscence and readme

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * transaction.h
 
2
 * transaction.hpp
3
3
 *
4
4
 * Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
5
5
 *
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.
 
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
8
 *
9
9
 * This program is free software: you can redistribute it and/or modify
10
10
 * it under the terms of the GNU Lesser General Public License as published
20
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
21
 */
22
22
 
23
 
#ifndef SQLITE3CC_TRANSACTION_H_
24
 
#define SQLITE3CC_TRANSACTION_H_
 
23
#ifndef TRANSACTION_HPP_
 
24
#define TRANSACTION_HPP_
25
25
 
26
26
 
27
27
#include <boost/utility.hpp>
28
 
#include <string>
29
28
 
30
29
 
31
30
namespace sqlite
32
31
{
33
32
 
34
33
 
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
 
                 * @param connection a connection
55
 
                 */
56
 
                explicit basic_transaction(
57
 
                        connection &connection );
58
 
 
59
 
        //__________________________________________________________________________
60
 
        //                                                          public interface
61
 
        public:
62
 
 
63
 
                /**
64
 
                 * Begin the transaction
65
 
                 */
66
 
                virtual void begin();
67
 
 
68
 
                /**
69
 
                 * Commit the transaction
70
 
                 */
71
 
                virtual void commit();
72
 
 
73
 
                /**
74
 
                 * Rollback the transaction
75
 
                 */
76
 
                virtual void rollback();
77
 
 
78
 
        //__________________________________________________________________________
79
 
        //                                                            implementation
80
 
        protected:
81
 
 
82
 
                /** reset any in-progress statements */
83
 
                void reset_active_queries();
84
 
 
85
 
                /** the connection on which to act */
86
 
                connection &_connection;
87
 
 
88
 
        };
89
 
 
90
 
} // namespace detail
91
 
 
92
 
 
93
 
////////////////////////////////////////////////////////////////////////////////
94
 
 
95
 
 
96
 
/**
97
 
 * A deferred transaction (the default)
98
 
 */
99
 
typedef detail::basic_transaction deferred_tranaction;
100
 
 
101
 
 
102
 
////////////////////////////////////////////////////////////////////////////////
103
 
 
104
 
 
105
 
/**
106
 
 * An immediate transaction
107
 
 */
108
 
class immediate_transaction
109
 
        :
110
 
        public detail::basic_transaction
111
 
{
112
 
//______________________________________________________________________________
113
 
//                                                                 instantiation
114
 
public:
115
 
 
116
 
        /**
117
 
         * Constructor that provides a connection upon which to act
118
 
         * @param connection a connection
119
 
         */
120
 
        explicit immediate_transaction(
121
 
                connection &connection );
122
 
 
123
 
//______________________________________________________________________________
124
 
//                                                              public interface
125
 
public:
126
 
 
127
 
        /**
128
 
         * Begin the transaction
129
 
         */
130
 
        virtual void begin();
131
 
 
132
 
};
133
 
 
134
 
 
135
 
////////////////////////////////////////////////////////////////////////////////
136
 
 
137
 
 
138
 
/**
139
 
 * An exclusive transaction
140
 
 */
141
 
class exclusive_transaction
142
 
        :
143
 
        public detail::basic_transaction
144
 
{
145
 
//______________________________________________________________________________
146
 
//                                                                 instantiation
147
 
public:
148
 
 
149
 
        /**
150
 
         * Constructor that provides a connection upon which to act
151
 
         * @param connection a connection
152
 
         */
153
 
        explicit exclusive_transaction(
154
 
                connection &connection );
155
 
 
156
 
//______________________________________________________________________________
157
 
//                                                              public interface
158
 
public:
159
 
 
160
 
        /**
161
 
         * Begin the transaction
162
 
         */
163
 
        virtual void begin();
164
 
 
165
 
};
166
 
 
167
 
 
168
 
////////////////////////////////////////////////////////////////////////////////
169
 
 
170
 
 
171
 
/**
172
 
 * A recursive transaction, allowing transactions to be nested.
173
 
 */
174
 
class recursive_transaction
175
 
        :
176
 
        public detail::basic_transaction
177
 
{
178
 
//______________________________________________________________________________
179
 
//                                                                 instantiation
180
 
public:
181
 
 
182
 
        /**
183
 
         * Constructor that provides a connection upon which to act
184
 
         * @param connection a connection
185
 
         */
186
 
        explicit recursive_transaction(
187
 
                connection &connection );
188
 
 
189
 
//______________________________________________________________________________
190
 
//                                                              public interface
191
 
public:
192
 
 
193
 
        /**
194
 
         * Begin the transaction
195
 
         */
196
 
        virtual void begin();
 
34
class database;
 
35
 
 
36
 
 
37
class transaction
 
38
        :
 
39
        private boost::noncopyable
 
40
{
 
41
//______________________________________________________________________________
 
42
//                                                                 instantiation
 
43
public:
 
44
 
 
45
        /**
 
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 = "" );
 
69
 
 
70
//______________________________________________________________________________
 
71
//                                                              public interface
197
72
 
198
73
        /**
199
74
         * Commit the transaction
203
78
        /**
204
79
         * Rollback the transaction
205
80
         */
206
 
        virtual void rollback();
 
81
        void rollback();
207
82
 
208
83
//______________________________________________________________________________
209
84
//                                                                implementation
210
85
protected:
211
86
 
212
 
        /* this transaction's savepoint name */
213
 
        std::string _sp_name;
 
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;
214
92
 
215
93
};
216
94
 
218
96
////////////////////////////////////////////////////////////////////////////////
219
97
 
220
98
 
221
 
/**
222
 
 * A scope guard (sentinel) for use with one of the transaction classes to
223
 
 * provide RIAA-style transactions.
224
 
 */
225
 
template< class T = deferred_tranaction >
226
 
class transaction_guard
 
99
class exclusive_transaction
227
100
        :
228
 
        private boost::noncopyable
 
101
        public transaction
229
102
{
230
103
//______________________________________________________________________________
231
104
//                                                                 instantiation
232
105
public:
233
106
 
234
107
        /**
235
 
         * Constructor that provides a connection upon which to act
236
 
         * @param connection a connection
237
 
         */
238
 
        explicit transaction_guard(
239
 
                connection &connection )
240
 
                :
241
 
                _transaction( connection ),
242
 
                _released( false )
243
 
        {
244
 
                _transaction.begin();
245
 
        }
246
 
 
247
 
        ~transaction_guard()
248
 
        {
249
 
                if( !_released )
250
 
                        _transaction.rollback();
251
 
        }
252
 
 
253
 
//______________________________________________________________________________
254
 
//                                                              public interface
255
 
public:
256
 
 
257
 
        /**
258
 
         * Commit the transaction
259
 
         */
260
 
        void commit()
261
 
        {
262
 
                if( !_released ) {
263
 
                        _transaction.commit();
264
 
                        _released = true;
265
 
                }
266
 
        }
267
 
 
268
 
        /**
269
 
         * Rollback the transaction early
270
 
         */
271
 
        void rollback()
272
 
        {
273
 
                if( !_released ) {
274
 
                        _transaction.rollback();
275
 
                        _released = true;
276
 
                }
277
 
        }
278
 
 
279
 
//______________________________________________________________________________
280
 
//                                                                implementation
281
 
protected:
282
 
 
283
 
        /** the transaction */
284
 
        T _transaction;
285
 
 
286
 
        /** have we released the transaction yet? */
287
 
        bool _released;
 
108
         * Constructor that provides a database upon which to act
 
109
         * @param database a database
 
110
         */
 
111
        exclusive_transaction(
 
112
                database &database );
288
113
 
289
114
};
290
115
 
291
116
 
292
 
} // namespace sqlite
293
 
 
294
 
 
295
 
#endif /* SQLITE3CC_TRANSACTION_H_ */
 
117
}
 
118
 
 
119
 
 
120
#endif /* TRANSACTION_HPP_ */