summaryrefslogtreecommitdiffstats
path: root/kexi/kexidb/transaction.h
blob: bee90b2635dac579724bb6a7e479ef50a27f3be8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* This file is part of the KDE project
   Copyright (C) 2003 Jaroslaw Staniek <js@iidea.pl>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#ifndef KEXIDB_TRANSACTION_H
#define KEXIDB_TRANSACTION_H

#include <tqguardedptr.h>

#include <kexidb/kexidb_export.h>

namespace KexiDB {

class Connection;

/*! Internal prototype for storing transaction handles for Transaction object.
 Only for driver developers: reimplement this class for driver that
 support transaction handles.
*/
class KEXI_DB_EXPORT TransactionData
{
	public:
		TransactionData(Connection *conn);
		~TransactionData();

		//helper for debugging
		static int globalcount;
		//helper for debugging
		static int globalCount();
		
		Connection *m_conn;
		bool m_active : 1;
		uint refcount;
};

//! This class encapsulates transaction handle.
/*! Transaction handle is sql driver-dependent,
	but outside Transaction is visible as universal container 
	for any handler implementation.
	
	Transaction object is value-based, internal data (handle) structure,
	reference-counted.
*/
class KEXI_DB_EXPORT Transaction : public TQObject
{
	public:
		/*! Constructs uninitialised (null) transaction.
		 Only in Conenction code it can be initialised */
		Transaction();
		
		//! Copy ctor.
		Transaction( const Transaction& trans );
		
		virtual ~Transaction();

		Transaction& operator=(const Transaction& trans);

		bool operator==(const Transaction& trans ) const;
		
		Connection* connection() const;
		
		/*! \return true if transaction is avtive (ie. started)
		 Returns false also if transaction is uninitialised (null). */
		bool active() const;
		
		/*! \return true if transaction is uinitialised (null). */
		bool isNull() const;
		
		/*! shortcut that offers uinitialised (null) transaction */
		static const Transaction null;

		//helper for debugging
		static int globalCount();
		static int globalcount;
	protected:
		
		TransactionData *m_data;
		
	friend class Connection;
};

//! Helper class for using inside methods for given connection. 
/*! It can be used in two ways:
	- start new transaction in constructor and rollback on destruction (1st constructor),
	- use already started transaction and rollback on destruction (2nd constructor).
	In any case, if transaction is committed or rolled back outside this TransactionGuard
	object in the meantime, nothing happens on TransactionGuard destruction.
	<code>
	Example usage:
	void myclas::my_method()
	{
		Transaction *transaction = connection->beginTransaction();
		TransactionGuard tg(transaction);
		...some code that operates inside started transaction...
		if (something)
			return //after return from this code block: tg will call
		           //connection->rollbackTransaction() automatically
		if (something_else)
			transaction->commit();
		//for now tg won't do anything because transaction does not exist
	}
	</code>
*/
class KEXI_DB_EXPORT TransactionGuard
{
	public:
		/*! Constructor #1: Starts new transaction constructor for \a connection.
		 Started transaction handle is available via transaction().*/
		TransactionGuard( Connection& conn );
		
		/*! Constructor #2: Uses already started transaction. */
		TransactionGuard( const Transaction& trans );

		/*! Constructor #3: Creates TransactionGuard without transaction assinged. 
		 setTransaction() can be used later to do so. */
		TransactionGuard();

		/*! Rollbacks not committed transaction. */
		~TransactionGuard();

		/*! Assigns transaction \a trans to this guard. 
		 Previously assigned transaction will be unassigned from this guard. */
		void setTransaction( const Transaction& trans ) { m_trans = trans; }

		/*! Comits the guarded transaction. 
		 It is convenient shortcut to connection->commitTransaction(this->transaction()) */
		bool commit();

		/*! Makes guarded transaction not guarded, so nothing will be performed on guard's desctruction. */
		void doNothing();

		/*! Transaction that are controlled by this guard. */
		const Transaction transaction() const { return m_trans; }

	protected:
		Transaction m_trans;
		bool m_doNothing : 1;
};

} //namespace KexiDB

#endif