/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
12 by edam
- moved null_t, exec_t and set_index_t to detail namespace so only their extern instantiations are in the main namespace
38
/**
39
 * A basic (default, deferred) transaction.
40
 */
3 by edam
- rewrote transaction classes and added transaction_guard
41
class basic_transaction
1 by edam
- initial commit
42
	:
43
	private boost::noncopyable
44
{
45
//______________________________________________________________________________
46
//                                                                 instantiation
47
public:
48
49
	/**
50
	 * Constructor that provides a database upon which to act
51
	 * @param database a database
52
	 */
3 by edam
- rewrote transaction classes and added transaction_guard
53
	explicit basic_transaction(
1 by edam
- initial commit
54
		database &database );
55
56
//______________________________________________________________________________
57
//                                                              public interface
3 by edam
- rewrote transaction classes and added transaction_guard
58
public:
59
60
	/**
61
	 * Begin the transaction
62
	 */
63
	virtual void begin();
1 by edam
- initial commit
64
65
	/**
66
	 * Commit the transaction
67
	 */
68
	virtual void commit();
69
70
	/**
71
	 * Rollback the transaction
72
	 */
3 by edam
- rewrote transaction classes and added transaction_guard
73
	virtual void rollback();
1 by edam
- initial commit
74
75
//______________________________________________________________________________
76
//                                                                implementation
77
protected:
78
3 by edam
- rewrote transaction classes and added transaction_guard
79
	/* the database on which to act */
1 by edam
- initial commit
80
	database &_database;
81
82
};
83
84
85
////////////////////////////////////////////////////////////////////////////////
86
87
12 by edam
- moved null_t, exec_t and set_index_t to detail namespace so only their extern instantiations are in the main namespace
88
/**
89
 * An exclusive transaction
90
 */
91
class immediate_transaction
92
	:
93
	public basic_transaction
94
{
95
//______________________________________________________________________________
96
//                                                                 instantiation
97
public:
98
99
	/**
100
	 * Constructor that provides a database upon which to act
101
	 * @param database a database
102
	 */
103
	explicit immediate_transaction(
104
		database &database );
105
106
//______________________________________________________________________________
107
//                                                              public interface
108
public:
109
110
	/**
111
	 * Begin the transaction
112
	 */
113
	virtual void begin();
114
115
};
116
117
118
////////////////////////////////////////////////////////////////////////////////
119
120
121
/**
122
 * An exclusive transaction
123
 */
1 by edam
- initial commit
124
class exclusive_transaction
125
	:
3 by edam
- rewrote transaction classes and added transaction_guard
126
	public basic_transaction
127
{
128
//______________________________________________________________________________
129
//                                                                 instantiation
130
public:
131
132
	/**
133
	 * Constructor that provides a database upon which to act
134
	 * @param database a database
135
	 */
136
	explicit exclusive_transaction(
137
		database &database );
138
139
//______________________________________________________________________________
140
//                                                              public interface
141
public:
142
143
	/**
144
	 * Begin the transaction
145
	 */
146
	virtual void begin();
147
148
};
149
150
151
////////////////////////////////////////////////////////////////////////////////
152
153
12 by edam
- moved null_t, exec_t and set_index_t to detail namespace so only their extern instantiations are in the main namespace
154
/**
155
 * A recursive transaction, allowing transactions to be nested.
156
 */
3 by edam
- rewrote transaction classes and added transaction_guard
157
class recursive_transaction
158
	:
159
	public basic_transaction
160
{
161
//______________________________________________________________________________
162
//                                                                 instantiation
163
public:
164
165
	/**
166
	 * Constructor that provides a database upon which to act
167
	 * @param database a database
168
	 */
169
	explicit recursive_transaction(
170
		database &database );
171
172
//______________________________________________________________________________
173
//                                                              public interface
174
public:
175
176
	/**
177
	 * Begin the transaction
178
	 */
179
	virtual void begin();
180
181
	/**
182
	 * Commit the transaction
183
	 */
184
	virtual void commit();
185
186
	/**
187
	 * Rollback the transaction
188
	 */
189
	virtual void rollback();
190
191
//______________________________________________________________________________
192
//                                                                implementation
193
protected:
194
195
	/* this transaction's savepoint name */
196
	std::string _sp_name;
197
198
};
199
200
201
////////////////////////////////////////////////////////////////////////////////
202
203
12 by edam
- moved null_t, exec_t and set_index_t to detail namespace so only their extern instantiations are in the main namespace
204
/**
205
 * A scope guard, or sentinel for use with one of the transaction classes.
206
 */
3 by edam
- rewrote transaction classes and added transaction_guard
207
template< class T = basic_transaction >
208
class transaction_guard
209
	:
210
	private boost::noncopyable
211
{
212
//______________________________________________________________________________
213
//                                                                 instantiation
214
public:
215
216
	/**
217
	 * Constructor that provides a database upon which to act
218
	 * @param database a database
219
	 */
220
	explicit transaction_guard(
221
		database &database )
222
		:
223
		_transaction( database ),
224
		_released( false )
225
	{
226
		_transaction.begin();
227
	}
228
229
	~transaction_guard()
230
	{
11 by edam
- update TODO
231
		if( !_released ) {
232
			try {
233
				_transaction.rollback();
234
			}
235
			catch( ... ) {
236
			}
237
		}
3 by edam
- rewrote transaction classes and added transaction_guard
238
	}
239
240
//______________________________________________________________________________
241
//                                                              public interface
242
public:
243
244
	/**
245
	 * Commit the transaction
246
	 */
247
	void commit()
248
	{
249
		if( !_released ) {
250
			_transaction.commit();
251
			_released = true;
252
		}
253
	}
254
255
//______________________________________________________________________________
256
//                                                                implementation
257
protected:
258
259
	/* the transaction */
260
	T _transaction;
261
262
	/* have we released the transaction yet? */
263
	bool _released;
1 by edam
- initial commit
264
265
};
266
267
2 by edam
- further initial development
268
} // namespace sqlite
269
270
271
#endif /* SQLITE3CC_TRANSACTION_H_ */