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