/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-27 15:12:55 UTC
  • Revision ID: edam@waxworlds.org-20100727151255-goaqgdz4kj13q7gz
- update TODO
- added some missing includes for <string>
- changed usage of database::exec() to not require return code!
- prevented transaction_guard destructor from throwing an exception

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
class basic_transaction
38
39
        :
39
40
        private boost::noncopyable
40
41
{
46
47
         * Constructor that provides a database upon which to act
47
48
         * @param database a database
48
49
         */
49
 
        explicit transaction(
 
50
        explicit basic_transaction(
50
51
                database &database );
51
52
 
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
53
//______________________________________________________________________________
71
54
//                                                              public interface
 
55
public:
 
56
 
 
57
        /**
 
58
         * Begin the transaction
 
59
         */
 
60
        virtual void begin();
72
61
 
73
62
        /**
74
63
         * Commit the transaction
78
67
        /**
79
68
         * Rollback the transaction
80
69
         */
81
 
        void rollback();
 
70
        virtual void rollback();
82
71
 
83
72
//______________________________________________________________________________
84
73
//                                                                implementation
85
74
protected:
86
75
 
87
 
        /** the database */
 
76
        /* the database on which to act */
88
77
        database &_database;
89
78
 
90
 
        /** the SQL used to rollback the transaction, or empty to use default */
91
 
        std::string _rollback_sql;
92
 
 
93
79
};
94
80
 
95
81
 
98
84
 
99
85
class exclusive_transaction
100
86
        :
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 );
 
87
        public basic_transaction
 
88
{
 
89
//______________________________________________________________________________
 
90
//                                                                 instantiation
 
91
public:
 
92
 
 
93
        /**
 
94
         * Constructor that provides a database upon which to act
 
95
         * @param database a database
 
96
         */
 
97
        explicit exclusive_transaction(
 
98
                database &database );
 
99
 
 
100
//______________________________________________________________________________
 
101
//                                                              public interface
 
102
public:
 
103
 
 
104
        /**
 
105
         * Begin the transaction
 
106
         */
 
107
        virtual void begin();
 
108
 
 
109
};
 
110
 
 
111
 
 
112
////////////////////////////////////////////////////////////////////////////////
 
113
 
 
114
 
 
115
class recursive_transaction
 
116
        :
 
117
        public basic_transaction
 
118
{
 
119
//______________________________________________________________________________
 
120
//                                                                 instantiation
 
121
public:
 
122
 
 
123
        /**
 
124
         * Constructor that provides a database upon which to act
 
125
         * @param database a database
 
126
         */
 
127
        explicit recursive_transaction(
 
128
                database &database );
 
129
 
 
130
//______________________________________________________________________________
 
131
//                                                              public interface
 
132
public:
 
133
 
 
134
        /**
 
135
         * Begin the transaction
 
136
         */
 
137
        virtual void begin();
 
138
 
 
139
        /**
 
140
         * Commit the transaction
 
141
         */
 
142
        virtual void commit();
 
143
 
 
144
        /**
 
145
         * Rollback the transaction
 
146
         */
 
147
        virtual void rollback();
 
148
 
 
149
//______________________________________________________________________________
 
150
//                                                                implementation
 
151
protected:
 
152
 
 
153
        /* this transaction's savepoint name */
 
154
        std::string _sp_name;
 
155
 
 
156
};
 
157
 
 
158
 
 
159
////////////////////////////////////////////////////////////////////////////////
 
160
 
 
161
 
 
162
template< class T = basic_transaction >
 
163
class transaction_guard
 
164
        :
 
165
        private boost::noncopyable
 
166
{
 
167
//______________________________________________________________________________
 
168
//                                                                 instantiation
 
169
public:
 
170
 
 
171
        /**
 
172
         * Constructor that provides a database upon which to act
 
173
         * @param database a database
 
174
         */
 
175
        explicit transaction_guard(
 
176
                database &database )
 
177
                :
 
178
                _transaction( database ),
 
179
                _released( false )
 
180
        {
 
181
                _transaction.begin();
 
182
        }
 
183
 
 
184
        ~transaction_guard()
 
185
        {
 
186
                if( !_released ) {
 
187
                        try {
 
188
                                _transaction.rollback();
 
189
                        }
 
190
                        catch( ... ) {
 
191
                        }
 
192
                }
 
193
        }
 
194
 
 
195
//______________________________________________________________________________
 
196
//                                                              public interface
 
197
public:
 
198
 
 
199
        /**
 
200
         * Commit the transaction
 
201
         */
 
202
        void commit()
 
203
        {
 
204
                if( !_released ) {
 
205
                        _transaction.commit();
 
206
                        _released = true;
 
207
                }
 
208
        }
 
209
 
 
210
//______________________________________________________________________________
 
211
//                                                                implementation
 
212
protected:
 
213
 
 
214
        /* the transaction */
 
215
        T _transaction;
 
216
 
 
217
        /* have we released the transaction yet? */
 
218
        bool _released;
113
219
 
114
220
};
115
221