1 /***************************************************************************
2 QgsOgrTransaction.cpp - Transaction support for OGR layers
3 -------------------
4 begin : June 13, 2018
5 copyright : (C) 2018 by Even Rouault
6 email : even.rouault @ spatialys.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 "qgsogrtransaction.h"
19 ///@cond PRIVATE
20
21 #include "qgsogrprovider.h"
22 #include "qgslogger.h"
23 #include "qgis.h"
24
QgsOgrTransaction(const QString & connString,QgsOgrDatasetSharedPtr ds)25 QgsOgrTransaction::QgsOgrTransaction( const QString &connString, QgsOgrDatasetSharedPtr ds )
26 : QgsTransaction( connString ), mSharedDS( ds )
27
28 {
29 Q_ASSERT( mSharedDS );
30 }
31
beginTransaction(QString & error,int)32 bool QgsOgrTransaction::beginTransaction( QString &error, int /* statementTimeout */ )
33 {
34 GDALDriverH hDriver = GDALGetDatasetDriver( mSharedDS.get()->mDs->hDS );
35 const QString driverName = GDALGetDriverShortName( hDriver );
36 if ( driverName == QLatin1String( "GPKG" ) || driverName == QLatin1String( "SQLite" ) )
37 {
38 QString fkDeferError;
39 if ( ! executeSql( QStringLiteral( "PRAGMA defer_foreign_keys = ON" ), fkDeferError ) )
40 {
41 QgsDebugMsg( QStringLiteral( "Error setting PRAGMA defer_foreign_keys = ON: %1" ).arg( fkDeferError ) );
42 }
43 }
44 return executeSql( QStringLiteral( "BEGIN" ), error );
45 }
46
commitTransaction(QString & error)47 bool QgsOgrTransaction::commitTransaction( QString &error )
48 {
49 return executeSql( QStringLiteral( "COMMIT" ), error );
50 }
51
rollbackTransaction(QString & error)52 bool QgsOgrTransaction::rollbackTransaction( QString &error )
53 {
54 return executeSql( QStringLiteral( "ROLLBACK" ), error );
55 }
56
executeSql(const QString & sql,QString & errorMsg,bool isDirty,const QString & name)57 bool QgsOgrTransaction::executeSql( const QString &sql, QString &errorMsg, bool isDirty, const QString &name )
58 {
59
60 QString err;
61 if ( isDirty )
62 {
63 createSavepoint( err );
64 }
65
66 QgsDebugMsgLevel( QStringLiteral( "Transaction sql: %1" ).arg( sql ), 2 );
67 if ( !mSharedDS->executeSQLNoReturn( sql ) )
68 {
69 errorMsg = CPLGetLastErrorMsg();
70 QgsDebugMsg( errorMsg );
71
72 if ( isDirty )
73 {
74 rollbackToSavepoint( savePoints().last(), err );
75 }
76
77 return false;
78 }
79
80 if ( isDirty )
81 {
82 dirtyLastSavePoint();
83 emit dirtied( sql, name );
84 }
85
86 QgsDebugMsgLevel( QStringLiteral( "... ok" ), 2 );
87 return true;
88 }
89
90 ///@endcond
91