/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>
28
29
30
namespace sqlite
31
{
32
33
34
class database;
35
36
3 by edam
- rewrote transaction classes and added transaction_guard
37
class basic_transaction
1 by edam
- initial commit
38
	:
39
	private boost::noncopyable
40
{
41
//______________________________________________________________________________
42
//                                                                 instantiation
43
public:
44
45
	/**
46
	 * Constructor that provides a database upon which to act
47
	 * @param database a database
48
	 */
3 by edam
- rewrote transaction classes and added transaction_guard
49
	explicit basic_transaction(
1 by edam
- initial commit
50
		database &database );
51
52
//______________________________________________________________________________
53
//                                                              public interface
3 by edam
- rewrote transaction classes and added transaction_guard
54
public:
55
56
	/**
57
	 * Begin the transaction
58
	 */
59
	virtual void begin();
1 by edam
- initial commit
60
61
	/**
62
	 * Commit the transaction
63
	 */
64
	virtual void commit();
65
66
	/**
67
	 * Rollback the transaction
68
	 */
3 by edam
- rewrote transaction classes and added transaction_guard
69
	virtual void rollback();
1 by edam
- initial commit
70
71
//______________________________________________________________________________
72
//                                                                implementation
73
protected:
74
3 by edam
- rewrote transaction classes and added transaction_guard
75
	/* the database on which to act */
1 by edam
- initial commit
76
	database &_database;
77
78
};
79
80
81
////////////////////////////////////////////////////////////////////////////////
82
83
84
class exclusive_transaction
85
	:
3 by edam
- rewrote transaction classes and added transaction_guard
86
	public basic_transaction
87
{
88
//______________________________________________________________________________
89
//                                                                 instantiation
90
public:
91
92
	/**
93
	 * Constructor that provides a database upon which to act
94
	 * @param database a database
95
	 */
96
	explicit exclusive_transaction(
97
		database &database );
98
99
//______________________________________________________________________________
100
//                                                              public interface
101
public:
102
103
	/**
104
	 * Begin the transaction
105
	 */
106
	virtual void begin();
107
108
};
109
110
111
////////////////////////////////////////////////////////////////////////////////
112
113
114
class recursive_transaction
115
	:
116
	public basic_transaction
117
{
118
//______________________________________________________________________________
119
//                                                                 instantiation
120
public:
121
122
	/**
123
	 * Constructor that provides a database upon which to act
124
	 * @param database a database
125
	 */
126
	explicit recursive_transaction(
127
		database &database );
128
129
//______________________________________________________________________________
130
//                                                              public interface
131
public:
132
133
	/**
134
	 * Begin the transaction
135
	 */
136
	virtual void begin();
137
138
	/**
139
	 * Commit the transaction
140
	 */
141
	virtual void commit();
142
143
	/**
144
	 * Rollback the transaction
145
	 */
146
	virtual void rollback();
147
148
//______________________________________________________________________________
149
//                                                                implementation
150
protected:
151
152
	/* this transaction's savepoint name */
153
	std::string _sp_name;
154
155
};
156
157
158
////////////////////////////////////////////////////////////////////////////////
159
160
161
template< class T = basic_transaction >
162
class transaction_guard
163
	:
164
	private boost::noncopyable
165
{
166
//______________________________________________________________________________
167
//                                                                 instantiation
168
public:
169
170
	/**
171
	 * Constructor that provides a database upon which to act
172
	 * @param database a database
173
	 */
174
	explicit transaction_guard(
175
		database &database )
176
		:
177
		_transaction( database ),
178
		_released( false )
179
	{
180
		_transaction.begin();
181
	}
182
183
	~transaction_guard()
184
	{
185
		if( !_released )
186
			_transaction.rollback();
187
	}
188
189
//______________________________________________________________________________
190
//                                                              public interface
191
public:
192
193
	/**
194
	 * Commit the transaction
195
	 */
196
	void commit()
197
	{
198
		if( !_released ) {
199
			_transaction.commit();
200
			_released = true;
201
		}
202
	}
203
204
//______________________________________________________________________________
205
//                                                                implementation
206
protected:
207
208
	/* the transaction */
209
	T _transaction;
210
211
	/* have we released the transaction yet? */
212
	bool _released;
1 by edam
- initial commit
213
214
};
215
216
2 by edam
- further initial development
217
} // namespace sqlite
218
219
220
#endif /* SQLITE3CC_TRANSACTION_H_ */