/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-01-31 21:12:21 UTC
  • Revision ID: edam@waxworlds.org-20100131211221-5xtrhfrchozhk326
- rewrote transaction classes and added transaction_guard
- removed leftover code (sqlite_busy error)
- added tests for transactions

Show diffs side-by-side

added added

removed removed

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