/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

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
35
34
class database;
36
35
 
37
36
 
38
 
class basic_transaction
 
37
class transaction
39
38
        :
40
39
        private boost::noncopyable
41
40
{
47
46
         * Constructor that provides a database upon which to act
48
47
         * @param database a database
49
48
         */
50
 
        explicit basic_transaction(
 
49
        transaction(
51
50
                database &database );
52
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
 
53
70
//______________________________________________________________________________
54
71
//                                                              public interface
55
 
public:
56
 
 
57
 
        /**
58
 
         * Begin the transaction
59
 
         */
60
 
        virtual void begin();
61
72
 
62
73
        /**
63
74
         * Commit the transaction
67
78
        /**
68
79
         * Rollback the transaction
69
80
         */
70
 
        virtual void rollback();
 
81
        void rollback();
71
82
 
72
83
//______________________________________________________________________________
73
84
//                                                                implementation
74
85
protected:
75
86
 
76
 
        /* the database on which to act */
 
87
        /** the database */
77
88
        database &_database;
78
89
 
 
90
        /** the SQL used to rollback the transaction, or empty to use default */
 
91
        std::string _rollback_sql;
 
92
 
79
93
};
80
94
 
81
95
 
84
98
 
85
99
class exclusive_transaction
86
100
        :
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;
219
 
 
220
 
};
221
 
 
222
 
 
223
 
} // namespace sqlite
224
 
 
225
 
 
226
 
#endif /* SQLITE3CC_TRANSACTION_H_ */
 
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 );
 
113
 
 
114
};
 
115
 
 
116
 
 
117
}
 
118
 
 
119
 
 
120
#endif /* TRANSACTION_HPP_ */