KDb

KDbTransaction.cpp
1 /* This file is part of the KDE project
2  Copyright (C) 2003-2017 JarosÅ‚aw Staniek <[email protected]>
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
28 int KDbTransaction_globalcount = 0;
29 
30 KDB_EXPORT int KDbTransaction::globalCount()
31 {
32  return KDbTransaction_globalcount;
33 }
34 
35 int KDbTransactionData_globalcount = 0;
36 
37 KDB_EXPORT int KDbTransactionData::globalCount()
38 {
39  return KDbTransactionData_globalcount;
40 }
41 #endif
42 
43 class Q_DECL_HIDDEN KDbTransactionData::Private
44 {
45 public:
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 
55 KDbTransactionData::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 
65 KDbTransactionData::~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 
127 KDbTransaction::~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 
145 KDbTransaction& 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 
195 class Q_DECL_HIDDEN KDbTransactionGuard::Private
196 {
197 public:
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 }
KDbConnection * connection()
KDbTransaction beginTransaction()
Starts a new database transaction.
bool isActive() const
Returns true if transaction is active (i.e.
const KDbTransaction transaction() const
Returns transaction that is controlled by this guard.
void ref()
Increments the value of reference counter for this data.
bool operator==(const KDbTransaction &other) const
Returns true if this transaction is equal to other; otherwise returns false.
~KDbTransactionGuard()
Roll backs not committed transaction unless doNothing() was called before.
bool commit(KDbTransaction::CommitOptions options=KDbTransaction::CommitOptions())
Commits the guarded transaction.
void doNothing()
Deativates the transaction guard.
KDbTransactionGuard class is a convenience class that simplifies handling transactions.
KDbConnection * connection()
Returns database connection for which the transaction belongs.
KDbTransaction()
Constructs a null transaction.
void deref()
Decrements the value of reference counter for this data.
bool isActive() const
KDbTransactionGuard()
Creates a new guard without assigning transaction.
Provides database connection, allowing queries and data modification.
Definition: KDbConnection.h:51
bool rollback(KDbTransaction::CommitOptions options=KDbTransaction::CommitOptions())
Rolls back the guarded transaction.
void setActive(bool set)
Sets "active" flag of this data.
Internal prototype for storing transaction handle for KDbTransaction object.
This class encapsulates a single database transaction.
void setTransaction(const KDbTransaction &transaction)
Assigns transaction to this guard.
bool isNull() const
Returns true if this transaction is null.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Jun 25 2022 06:21:34 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.