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