KDb

KDbTransaction.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2003-2017 Jarosław Staniek <staniek@kde.org>
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this program; see the file COPYING. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include "KDbTransaction.h"
21#include "KDbConnection.h"
22#include "KDbTransactionData.h"
23#include "KDbTransactionGuard.h"
24#include "kdb_debug.h"
25
26#ifdef KDB_TRANSACTIONS_DEBUG
27//helper for debugging
28int KDbTransaction_globalcount = 0;
29
30KDB_EXPORT int KDbTransaction::globalCount()
31{
32 return KDbTransaction_globalcount;
33}
34
35int KDbTransactionData_globalcount = 0;
36
37KDB_EXPORT int KDbTransactionData::globalCount()
38{
39 return KDbTransactionData_globalcount;
40}
41#endif
42
43class Q_DECL_HIDDEN KDbTransactionData::Private
44{
45public:
46 Private(KDbConnection *c) : connection(c)
47 {
48 Q_ASSERT(connection);
49 }
50 KDbConnection *connection;
51 bool active = true;
52 int refcount = 1;
53};
54
55KDbTransactionData::KDbTransactionData(KDbConnection *connection)
56 : d(new Private(connection))
57{
58#ifdef KDB_TRANSACTIONS_DEBUG
59 KDbTransaction_globalcount++; // because of refcount is initialized to 1
60 KDbTransactionData_globalcount++;
61 transactionsDebug() << "-- globalcount ==" << KDbTransactionData_globalcount;
62#endif
63}
64
65KDbTransactionData::~KDbTransactionData()
66{
67#ifdef KDB_TRANSACTIONS_DEBUG
68 KDbTransactionData_globalcount--;
69 transactionsDebug() << "-- globalcount ==" << KDbTransactionData_globalcount;
70#endif
71 delete d;
72}
73
75{
76 d->refcount++;
77}
78
80{
81 d->refcount--;
82}
83
85{
86 return d->refcount;
87}
88
90{
91 return d->active;
92}
93
95{
96 d->active = set;
97}
98
100{
101 return d->connection;
102}
103
105{
106 return d->connection;
107}
108
109//---------------------------------------------------
110
112 : m_data(nullptr)
113{
114}
115
117 : m_data(trans.m_data)
118{
119 if (m_data) {
120 m_data->ref();
121#ifdef KDB_TRANSACTIONS_DEBUG
122 KDbTransaction_globalcount++;
123#endif
124 }
125}
126
127KDbTransaction::~KDbTransaction()
128{
129 if (m_data) {
130 m_data->deref();
131#ifdef KDB_TRANSACTIONS_DEBUG
132 KDbTransaction_globalcount--;
133#endif
134 transactionsDebug() << "m_data->refcount==" << m_data->refcount();
135 if (m_data->refcount() == 0)
136 delete m_data;
137 } else {
138 transactionsDebug() << "null";
139 }
140#ifdef KDB_TRANSACTIONS_DEBUG
141 transactionsDebug() << "-- globalcount == " << KDbTransaction_globalcount;
142#endif
143}
144
145KDbTransaction& KDbTransaction::operator=(const KDbTransaction & trans)
146{
147 if (this != &trans) {
148 if (m_data) {
149 m_data->deref();
150#ifdef KDB_TRANSACTIONS_DEBUG
151 KDbTransaction_globalcount--;
152#endif
153 transactionsDebug() << "m_data->refcount==" << m_data->refcount();
154 if (m_data->refcount() == 0)
155 delete m_data;
156 }
157 m_data = trans.m_data;
158 if (m_data) {
159 m_data->ref();
160#ifdef KDB_TRANSACTIONS_DEBUG
161 KDbTransaction_globalcount++;
162#endif
163 }
164 }
165 return *this;
166}
167
169{
170 return m_data == other.m_data;
171}
172
174{
175 return m_data ? m_data->connection() : nullptr;
176}
177
179{
180 return const_cast<KDbTransaction*>(this)->connection();
181}
182
184{
185 return m_data && m_data->isActive();
186}
187
189{
190 return m_data == nullptr;
191}
192
193//---------------------------------------------------
194
195class Q_DECL_HIDDEN KDbTransactionGuard::Private
196{
197public:
198 Private() {}
199 KDbTransaction transaction;
200 bool doNothing = false;
201};
202
205{
206 if (connection) {
207 d->transaction = connection->beginTransaction();
208 }
209}
210
213{
214 d->transaction = transaction;
215}
216
218 : d(new Private)
219{
220}
221
223{
224 if (!d->doNothing && d->transaction.isActive()) {
225 const bool result = rollback();
226#ifdef KDB_TRANSACTIONS_DEBUG
227 transactionsDebug() << "~KDbTransactionGuard is rolling back transaction:" << result;
228#else
229 Q_UNUSED(result)
230#endif
231 }
232 delete d;
233}
234
236{
237 d->transaction = transaction;
238}
239
241{
242 if (d->transaction.connection()) {
243 return d->transaction.connection()->commitTransaction(d->transaction, options);
244 }
245 return false;
246}
247
249{
250 if (d->transaction.connection()) {
251 return d->transaction.connection()->rollbackTransaction(d->transaction, options);
252 }
253 return false;
254}
255
257{
258 d->doNothing = true;
259}
260
262{
263 return d->transaction;
264}
Provides database connection, allowing queries and data modification.
bool rollbackTransaction(KDbTransaction trans=KDbTransaction(), KDbTransaction::CommitOptions options=KDbTransaction::CommitOptions())
Rolls back specified transaction for this connection.
KDbTransaction beginTransaction()
Starts a new database transaction.
bool commitTransaction(KDbTransaction transaction=KDbTransaction(), KDbTransaction::CommitOptions options=KDbTransaction::CommitOptions())
Commits specified transaction for this connection.
Internal prototype for storing transaction handle for KDbTransaction object.
void deref()
Decrements the value of reference counter for this data.
KDbConnection * connection()
void setActive(bool set)
Sets "active" flag of this data.
void ref()
Increments the value of reference counter for this data.
KDbTransactionGuard class is a convenience class that simplifies handling transactions.
KDbTransactionGuard()
Creates a new guard without assigning transaction.
const KDbTransaction transaction() const
Returns transaction that is controlled by this guard.
bool commit(KDbTransaction::CommitOptions options=KDbTransaction::CommitOptions())
Commits the guarded transaction.
void doNothing()
Deativates the transaction guard.
~KDbTransactionGuard()
Roll backs not committed transaction unless doNothing() was called before.
bool rollback(KDbTransaction::CommitOptions options=KDbTransaction::CommitOptions())
Rolls back the guarded transaction.
void setTransaction(const KDbTransaction &transaction)
Assigns transaction to this guard.
This class encapsulates a single database transaction.
bool operator==(const KDbTransaction &other) const
Returns true if this transaction is equal to other; otherwise returns false.
KDbTransaction()
Constructs a null transaction.
KDbConnection * connection()
Returns database connection for which the transaction belongs.
bool isActive() const
Returns true if transaction is active (i.e.
bool isNull() const
Returns true if this transaction is null.
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:20:59 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.