kexi

transaction.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2003 Jaroslaw Staniek <js@iidea.pl>
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 <kexidb/transaction.h>
21#include <kexidb/connection.h>
22
23#include <kdebug.h>
24
25#include <assert.h>
26
27//remove debug
28#undef KexiDBDbg
29#define KexiDBDbg if (0) kdDebug()
30
31using namespace KexiDB;
32
33//helper for debugging
34KEXI_DB_EXPORT int Transaction::globalcount = 0;
35KEXI_DB_EXPORT int Transaction::globalCount() { return Transaction::globalcount; }
36KEXI_DB_EXPORT int TransactionData::globalcount = 0;
37KEXI_DB_EXPORT int TransactionData::globalCount() { return TransactionData::globalcount; }
38
39TransactionData::TransactionData(Connection *conn)
40 : m_conn(conn)
41 , m_active(true)
42 , refcount(1)
43{
44 assert(conn);
45 Transaction::globalcount++; //because refcount(1) init.
46 TransactionData::globalcount++;
47 KexiDBDbg << "-- TransactionData::globalcount == " << TransactionData::globalcount << endl;
48}
49
50TransactionData::~TransactionData()
51{
52 TransactionData::globalcount--;
53 KexiDBDbg << "-- TransactionData::globalcount == " << TransactionData::globalcount << endl;
54}
55
56//---------------------------------------------------
57
59
61 : TQObject(0,"kexidb_transaction")
62 , m_data(0)
63{
64}
65
67 : TQObject(0,"kexidb_transaction")
68 , m_data(trans.m_data)
69{
70 if (m_data) {
71 m_data->refcount++;
72 Transaction::globalcount++;
73 }
74}
75
76Transaction::~Transaction()
77{
78 if (m_data) {
79 m_data->refcount--;
80 Transaction::globalcount--;
81 KexiDBDbg << "~Transaction(): m_data->refcount==" << m_data->refcount << endl;
82 if (m_data->refcount==0)
83 delete m_data;
84 }
85 else {
86 KexiDBDbg << "~Transaction(): null" << endl;
87 }
88 KexiDBDbg << "-- Transaction::globalcount == " << Transaction::globalcount << endl;
89}
90
91Transaction& Transaction::operator=(const Transaction& trans)
92{
93 if (m_data) {
94 m_data->refcount--;
95 Transaction::globalcount--;
96 KexiDBDbg << "Transaction::operator=: m_data->refcount==" << m_data->refcount << endl;
97 if (m_data->refcount==0)
98 delete m_data;
99 }
100 m_data = trans.m_data;
101 if (m_data) {
102 m_data->refcount++;
103 Transaction::globalcount++;
104 }
105 return *this;
106}
107
108bool Transaction::operator==(const Transaction& trans) const
109{
110 return m_data==trans.m_data;
111}
112
113Connection* Transaction::connection() const
114{
115 return m_data ? m_data->m_conn : 0;
116}
117
119{
120 return m_data && m_data->m_active;
121}
122
124{
125 return m_data==0;
126}
127
128//---------------------------------------------------
129
131 : m_trans( conn.beginTransaction() )
132 , m_doNothing(false)
133{
134}
135
137 : m_trans(trans)
138 , m_doNothing(false)
139{
140}
141
143 : m_doNothing(false)
144{
145}
146
148{
149 if (!m_doNothing && m_trans.active() && m_trans.connection())
150 m_trans.connection()->rollbackTransaction(m_trans);
151}
152
154{
155 if (m_trans.active() && m_trans.connection()) {
156 return m_trans.connection()->commitTransaction(m_trans);
157 }
158 return false;
159}
160
162{
163 m_doNothing = true;
164}
165
Provides database connection, allowing queries and data modification.
Definition connection.h:62
bool commitTransaction(Transaction trans=Transaction::null, bool ignore_inactive=false)
bool rollbackTransaction(Transaction trans=Transaction::null, bool ignore_inactive=false)
This class encapsulates transaction handle.
Definition transaction.h:60
static const Transaction null
Definition transaction.h:85
High-level database connectivity library with database backend drivers.