/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
 *
22 by edam
updated email and web addresses
4
 * Copyright (C) 2009 Tim Marston <tim@ed.am>
1 by edam
- initial commit
5
 *
2 by edam
- further initial development
6
 * This file is part of sqlite3cc (hereafter referred to as "this program").
22 by edam
updated email and web addresses
7
 * See http://ed.am/dev/sqlite3cc for more information.
8
 *
9
 * This program is free software: you can redistribute it and/or modify it under
10
 * the terms of the GNU Lesser General Public License as published by the Free
11
 * Software Foundation, either version 3 of the License, or (at your option) any
12
 * later version.
13
 *
14
 * This program is distributed in the hope that it will be useful, but WITHOUT
15
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16
 * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
17
 * details.
1 by edam
- initial commit
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
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
35
class connection;
36
37
38
namespace detail
39
{
40
41
	/**
42
	 * A basic (default, deferred) transaction.
43
	 */
44
	class basic_transaction
45
		:
46
		private boost::noncopyable
47
	{
48
	//__________________________________________________________________________
49
	//                                                             instantiation
50
	public:
51
52
		/**
22 by edam
updated email and web addresses
53
		 * Constructor that provides a connection upon which to act.
54
		 *
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
55
		 * @param connection a connection
56
		 */
57
		explicit basic_transaction(
58
			connection &connection );
59
60
	//__________________________________________________________________________
61
	//                                                          public interface
62
	public:
63
64
		/**
65
		 * Begin the transaction
66
		 */
67
		virtual void begin();
68
69
		/**
70
		 * Commit the transaction
71
		 */
72
		virtual void commit();
73
74
		/**
75
		 * Rollback the transaction
76
		 */
77
		virtual void rollback();
78
79
	//__________________________________________________________________________
80
	//                                                            implementation
81
	protected:
82
83
		/** reset any in-progress statements */
84
		void reset_active_queries();
85
86
		/** the connection on which to act */
87
		connection &_connection;
88
89
	};
90
91
} // namespace detail
92
93
94
////////////////////////////////////////////////////////////////////////////////
1 by edam
- initial commit
95
96
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
97
/**
22 by edam
updated email and web addresses
98
 * A deferred transaction.
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
99
 */
21 by edam
typo
100
typedef detail::basic_transaction deferred_transaction;
1 by edam
- initial commit
101
102
103
////////////////////////////////////////////////////////////////////////////////
104
105
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
106
/**
22 by edam
updated email and web addresses
107
 * An immediate transaction.
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
108
 */
109
class immediate_transaction
110
	:
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
111
	public detail::basic_transaction
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
112
{
113
//______________________________________________________________________________
114
//                                                                 instantiation
115
public:
116
117
	/**
22 by edam
updated email and web addresses
118
	 * Constructor that provides a connection upon which to act.
119
	 *
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
120
	 * @param connection a connection
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
121
	 */
122
	explicit immediate_transaction(
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
123
		connection &connection );
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
124
125
//______________________________________________________________________________
126
//                                                              public interface
127
public:
128
129
	/**
22 by edam
updated email and web addresses
130
	 * Begin the transaction.
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
131
	 */
132
	virtual void begin();
133
134
};
135
136
137
////////////////////////////////////////////////////////////////////////////////
138
139
140
/**
22 by edam
updated email and web addresses
141
 * An exclusive transaction.
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
142
 */
1 by edam
- initial commit
143
class exclusive_transaction
144
	:
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
145
	public detail::basic_transaction
3 by edam
- rewrote transaction classes and added transaction_guard
146
{
147
//______________________________________________________________________________
148
//                                                                 instantiation
149
public:
150
151
	/**
22 by edam
updated email and web addresses
152
	 * Constructor that provides a connection upon which to act.
153
	 *
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
154
	 * @param connection a connection
3 by edam
- rewrote transaction classes and added transaction_guard
155
	 */
156
	explicit exclusive_transaction(
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
157
		connection &connection );
3 by edam
- rewrote transaction classes and added transaction_guard
158
159
//______________________________________________________________________________
160
//                                                              public interface
161
public:
162
163
	/**
164
	 * Begin the transaction
165
	 */
166
	virtual void begin();
167
168
};
169
170
171
////////////////////////////////////////////////////////////////////////////////
172
173
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
174
/**
175
 * A recursive transaction, allowing transactions to be nested.
176
 */
3 by edam
- rewrote transaction classes and added transaction_guard
177
class recursive_transaction
178
	:
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
179
	public detail::basic_transaction
3 by edam
- rewrote transaction classes and added transaction_guard
180
{
181
//______________________________________________________________________________
182
//                                                                 instantiation
183
public:
184
185
	/**
22 by edam
updated email and web addresses
186
	 * Constructor that provides a connection upon which to act.
187
	 *
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
188
	 * @param connection a connection
3 by edam
- rewrote transaction classes and added transaction_guard
189
	 */
190
	explicit recursive_transaction(
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
191
		connection &connection );
3 by edam
- rewrote transaction classes and added transaction_guard
192
193
//______________________________________________________________________________
194
//                                                              public interface
195
public:
196
197
	/**
22 by edam
updated email and web addresses
198
	 * Begin the transaction.
3 by edam
- rewrote transaction classes and added transaction_guard
199
	 */
200
	virtual void begin();
201
202
	/**
22 by edam
updated email and web addresses
203
	 * Commit the transaction.
3 by edam
- rewrote transaction classes and added transaction_guard
204
	 */
205
	virtual void commit();
206
207
	/**
22 by edam
updated email and web addresses
208
	 * Rollback the transaction.
3 by edam
- rewrote transaction classes and added transaction_guard
209
	 */
210
	virtual void rollback();
211
212
//______________________________________________________________________________
213
//                                                                implementation
214
protected:
215
216
	/* this transaction's savepoint name */
217
	std::string _sp_name;
218
219
};
220
221
222
////////////////////////////////////////////////////////////////////////////////
223
224
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
225
/**
22 by edam
updated email and web addresses
226
 * A scope guard (sentinel) for use with one of the transaction classes to
227
 * provide RAII-style transactions.  It defaults to using deferred transactions.
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
228
 */
21 by edam
typo
229
template< class T = deferred_transaction >
3 by edam
- rewrote transaction classes and added transaction_guard
230
class transaction_guard
231
	:
232
	private boost::noncopyable
233
{
234
//______________________________________________________________________________
235
//                                                                 instantiation
236
public:
237
238
	/**
22 by edam
updated email and web addresses
239
	 * Constructor that provides a connection upon which to act.
240
	 *
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
241
	 * @param connection a connection
3 by edam
- rewrote transaction classes and added transaction_guard
242
	 */
243
	explicit transaction_guard(
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
244
		connection &connection )
3 by edam
- rewrote transaction classes and added transaction_guard
245
		:
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
246
		_transaction( connection ),
3 by edam
- rewrote transaction classes and added transaction_guard
247
		_released( false )
248
	{
249
		_transaction.begin();
250
	}
251
252
	~transaction_guard()
253
	{
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
254
		if( !_released )
255
			_transaction.rollback();
3 by edam
- rewrote transaction classes and added transaction_guard
256
	}
257
258
//______________________________________________________________________________
259
//                                                              public interface
260
public:
261
262
	/**
22 by edam
updated email and web addresses
263
	 * Commit the transaction.
3 by edam
- rewrote transaction classes and added transaction_guard
264
	 */
265
	void commit()
266
	{
267
		if( !_released ) {
268
			_transaction.commit();
269
			_released = true;
270
		}
271
	}
272
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
273
	/**
22 by edam
updated email and web addresses
274
	 * Rollback the transaction early.
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
275
	 */
276
	void rollback()
277
	{
278
		if( !_released ) {
279
			_transaction.rollback();
280
			_released = true;
281
		}
282
	}
283
3 by edam
- rewrote transaction classes and added transaction_guard
284
//______________________________________________________________________________
285
//                                                                implementation
286
protected:
287
13 by edam
- made basic_statement::step() protected, for use by query and command only
288
	/** the transaction */
3 by edam
- rewrote transaction classes and added transaction_guard
289
	T _transaction;
290
13 by edam
- made basic_statement::step() protected, for use by query and command only
291
	/** have we released the transaction yet? */
3 by edam
- rewrote transaction classes and added transaction_guard
292
	bool _released;
1 by edam
- initial commit
293
294
};
295
296
2 by edam
- further initial development
297
} // namespace sqlite
298
299
300
#endif /* SQLITE3CC_TRANSACTION_H_ */