/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 06:28:53 UTC
  • Revision ID: edam@waxworlds.org-20100729062853-4i2fec52m86mh724
- made basic_statement::step() protected, for use by query and command only
- moved basic_statement::operator<<() to command and query instead; one needs to accept sqlite::exec, the other doesn't
- added tests for query::operator<<()
- added code to invlaidate in-progress queries during any transaction rollbacks (currently segfaults in basic_statement::finalize())
- added new sqlite_error constructor that obtains a full error message
- implemented database::database_mutex_guard class
- swapped command's step mutex in favour of the database mutex

Show diffs side-by-side

added added

removed removed

25
25
 
26
26
 
27
27
#include <boost/utility.hpp>
 
28
#include <string>
28
29
 
29
30
 
30
31
namespace sqlite
34
35
class database;
35
36
 
36
37
 
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
 
        explicit 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
72
 
 
73
 
        /**
74
 
         * Commit the transaction
75
 
         */
76
 
        virtual void commit();
77
 
 
78
 
        /**
79
 
         * Rollback the transaction
80
 
         */
81
 
        void rollback();
82
 
 
83
 
//______________________________________________________________________________
84
 
//                                                                implementation
85
 
protected:
 
38
/**
 
39
 * A basic (default, deferred) transaction.
 
40
 */
 
41
class basic_transaction
 
42
        :
 
43
        private boost::noncopyable
 
44
{
 
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
 
93
 */
 
94
class immediate_transaction
 
95
        :
 
96
        public basic_transaction
 
97
{
 
98
//______________________________________________________________________________
 
99
//                                                                 instantiation
 
100
public:
 
101
 
 
102
        /**
 
103
         * Constructor that provides a database upon which to act
 
104
         * @param database a database
 
105
         */
 
106
        explicit immediate_transaction(
 
107
                database &database );
 
108
 
 
109
//______________________________________________________________________________
 
110
//                                                              public interface
 
111
public:
 
112
 
 
113
        /**
 
114
         * Begin the transaction
 
115
         */
 
116
        virtual void begin();
 
117
 
 
118
};
 
119
 
 
120
 
 
121
////////////////////////////////////////////////////////////////////////////////
 
122
 
 
123
 
 
124
/**
 
125
 * An exclusive transaction
 
126
 */
 
127
class exclusive_transaction
 
128
        :
 
129
        public basic_transaction
 
130
{
 
131
//______________________________________________________________________________
 
132
//                                                                 instantiation
 
133
public:
 
134
 
 
135
        /**
 
136
         * Constructor that provides a database upon which to act
 
137
         * @param database a database
 
138
         */
 
139
        explicit exclusive_transaction(
 
140
                database &database );
 
141
 
 
142
//______________________________________________________________________________
 
143
//                                                              public interface
 
144
public:
 
145
 
 
146
        /**
 
147
         * Begin the transaction
 
148
         */
 
149
        virtual void begin();
 
150
 
 
151
};
 
152
 
 
153
 
 
154
////////////////////////////////////////////////////////////////////////////////
 
155
 
 
156
 
 
157
/**
 
158
 * A recursive transaction, allowing transactions to be nested.
 
159
 */
 
160
class recursive_transaction
 
161
        :
 
162
        public basic_transaction
 
163
{
 
164
//______________________________________________________________________________
 
165
//                                                                 instantiation
 
166
public:
 
167
 
 
168
        /**
 
169
         * Constructor that provides a database upon which to act
 
170
         * @param database a database
 
171
         */
 
172
        explicit recursive_transaction(
 
173
                database &database );
 
174
 
 
175
//______________________________________________________________________________
 
176
//                                                              public interface
 
177
public:
 
178
 
 
179
        /**
 
180
         * Begin the transaction
 
181
         */
 
182
        virtual void begin();
 
183
 
 
184
        /**
 
185
         * Commit the transaction
 
186
         */
 
187
        virtual void commit();
 
188
 
 
189
        /**
 
190
         * Rollback the transaction
 
191
         */
 
192
        virtual void rollback();
 
193
 
 
194
//______________________________________________________________________________
 
195
//                                                                implementation
 
196
protected:
 
197
 
 
198
        /* this transaction's savepoint name */
 
199
        std::string _sp_name;
 
200
 
 
201
};
 
202
 
 
203
 
 
204
////////////////////////////////////////////////////////////////////////////////
 
205
 
 
206
 
 
207
/**
 
208
 * A scope guard, or sentinel for use with one of the transaction classes.
 
209
 */
 
210
template< class T = basic_transaction >
 
211
class transaction_guard
 
212
        :
 
213
        private boost::noncopyable
 
214
{
 
215
//______________________________________________________________________________
 
216
//                                                                 instantiation
 
217
public:
 
218
 
 
219
        /**
 
220
         * Constructor that provides a database upon which to act
 
221
         * @param database a database
 
222
         */
 
223
        explicit transaction_guard(
 
224
                database &database )
 
225
                :
 
226
                _transaction( database ),
 
227
                _database( database ),
 
228
                _released( false )
 
229
        {
 
230
                _transaction.begin();
 
231
        }
 
232
 
 
233
        ~transaction_guard()
 
234
        {
 
235
                if( !_released ) {
 
236
                        try {
 
237
                                _transaction.rollback();
 
238
                        }
 
239
                        catch( ... ) {
 
240
                        }
 
241
                }
 
242
        }
 
243
 
 
244
//______________________________________________________________________________
 
245
//                                                              public interface
 
246
public:
 
247
 
 
248
        /**
 
249
         * Commit the transaction
 
250
         */
 
251
        void commit()
 
252
        {
 
253
                if( !_released ) {
 
254
                        _transaction.commit();
 
255
                        _released = true;
 
256
                }
 
257
        }
 
258
 
 
259
//______________________________________________________________________________
 
260
//                                                                implementation
 
261
protected:
 
262
 
 
263
        /** the transaction */
 
264
        T _transaction;
86
265
 
87
266
        /** the database */
88
267
        database &_database;
89
268
 
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 );
 
269
        /** have we released the transaction yet? */
 
270
        bool _released;
113
271
 
114
272
};
115
273