/sqlite3cc

To get this branch, use:
bzr branch http://bzr.ed.am/sqlite3cc
1 by edam
- initial commit
1
/*
2 by edam
- further initial development
2
 * transaction.h
1 by edam
- initial commit
3
 *
4
 * Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
5
 *
2 by edam
- further initial development
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.
1 by edam
- initial commit
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
 *
19
 * You should have received a copy of the GNU Lesser General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
2 by edam
- further initial development
23
#ifndef SQLITE3CC_TRANSACTION_H_
24
#define SQLITE3CC_TRANSACTION_H_
1 by edam
- initial commit
25
26
27
#include <boost/utility.hpp>
11 by edam
- update TODO
28
#include <string>
1 by edam
- initial commit
29
30
31
namespace sqlite
32
{
33
34
35
class database;
36
37
3 by edam
- rewrote transaction classes and added transaction_guard
38
class basic_transaction
1 by edam
- initial commit
39
	:
40
	private boost::noncopyable
41
{
42
//______________________________________________________________________________
43
//                                                                 instantiation
44
public:
45
46
	/**
47
	 * Constructor that provides a database upon which to act
48
	 * @param database a database
49
	 */
3 by edam
- rewrote transaction classes and added transaction_guard
50
	explicit basic_transaction(
1 by edam
- initial commit
51
		database &database );
52
53
//______________________________________________________________________________
54
//                                                              public interface
3 by edam
- rewrote transaction classes and added transaction_guard
55
public:
56
57
	/**
58
	 * Begin the transaction
59
	 */
60
	virtual void begin();
1 by edam
- initial commit
61
62
	/**
63
	 * Commit the transaction
64
	 */
65
	virtual void commit();
66
67
	/**
68
	 * Rollback the transaction
69
	 */
3 by edam
- rewrote transaction classes and added transaction_guard
70
	virtual void rollback();
1 by edam
- initial commit
71
72
//______________________________________________________________________________
73
//                                                                implementation
74
protected:
75
3 by edam
- rewrote transaction classes and added transaction_guard
76
	/* the database on which to act */
1 by edam
- initial commit
77
	database &_database;
78
79
};
80
81
82
////////////////////////////////////////////////////////////////////////////////
83
84
85
class exclusive_transaction
86
	:
3 by edam
- rewrote transaction classes and added transaction_guard
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
	{
11 by edam
- update TODO
186
		if( !_released ) {
187
			try {
188
				_transaction.rollback();
189
			}
190
			catch( ... ) {
191
			}
192
		}
3 by edam
- rewrote transaction classes and added transaction_guard
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;
1 by edam
- initial commit
219
220
};
221
222
2 by edam
- further initial development
223
} // namespace sqlite
224
225
226
#endif /* SQLITE3CC_TRANSACTION_H_ */