/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-23 12:57:07 UTC
  • Revision ID: edam@waxworlds.org-20100723125707-qu9jk9vvg2uewx7t
- cleaned up test-main
- fixed comment type
- made sqlite::exec() throw instead of returning sqlite error code

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