1 /***************************************************************************
2 qgsoracletransaction.cpp
3 -------------------
4 begin : August 2019
5 copyright : (C) 2019 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18 #include "qgsoracletransaction.h"
19 ///@cond PRIVATE
20
21 #include "qgslogger.h"
22 #include "qgis.h"
23 #include "qgsoracleconn.h"
24
QgsOracleTransaction(const QString & connString)25 QgsOracleTransaction::QgsOracleTransaction( const QString &connString )
26 : QgsTransaction( connString )
27
28 {
29
30 }
31
~QgsOracleTransaction()32 QgsOracleTransaction::~QgsOracleTransaction()
33 {
34 if ( mConn )
35 mConn->unref();
36 }
37
beginTransaction(QString &,int)38 bool QgsOracleTransaction::beginTransaction( QString &, int /* statementTimeout */ )
39 {
40 mConn = QgsOracleConn::connectDb( mConnString, true /*transaction*/ );
41
42 return true;
43 }
44
commitTransaction(QString & error)45 bool QgsOracleTransaction::commitTransaction( QString &error )
46 {
47 if ( executeSql( QStringLiteral( "COMMIT" ), error ) )
48 {
49 mConn->disconnect();
50 mConn = nullptr;
51 return true;
52 }
53 return false;
54 }
55
rollbackTransaction(QString & error)56 bool QgsOracleTransaction::rollbackTransaction( QString &error )
57 {
58 if ( executeSql( QStringLiteral( "ROLLBACK" ), error ) )
59 {
60 mConn->disconnect();
61 mConn = nullptr;
62 return true;
63 }
64 return false;
65 }
66
executeSql(const QString & sql,QString & errorMsg,bool isDirty,const QString & name)67 bool QgsOracleTransaction::executeSql( const QString &sql, QString &errorMsg, bool isDirty, const QString &name )
68 {
69 QString err;
70 if ( isDirty )
71 {
72 createSavepoint( err );
73 }
74
75 QgsDebugMsg( QStringLiteral( "Transaction sql: %1" ).arg( sql ) );
76 const bool res = mConn->exec( sql, true, &errorMsg );
77 if ( !res )
78 {
79 if ( isDirty )
80 {
81 rollbackToSavepoint( savePoints().last(), err );
82 }
83
84 return false;
85 }
86
87 if ( isDirty )
88 {
89 dirtyLastSavePoint();
90 emit dirtied( sql, name );
91 }
92
93 return true;
94 }
95
96 ///@endcond
97