-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathconnection_wrapper.py
245 lines (182 loc) · 7.71 KB
/
connection_wrapper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
"""
/**
* Copyright (C) 2017 Oslandia <infos@oslandia.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
"""
# -*- coding: utf-8 -*-
from qgis.core import QgsTransactionGroup, QgsProject, QgsDataSourceUri
from psycopg2 import Error
from .credentials_dialog import CredentialsDialog
import psycopg2
import os
# This object contains database connection wrapped for both
# psycopg2 direct database connection OR QGis transaction group
# connection.
#
# First open database connection: openConnection().
# In second time, use executeSql() and commit().
# In last time delete connection: closeConnection().
#
# In some case transaction group cannot be used. To disable transaction
# group use disableTransactionGroup().
# Direct connection allow the use of cursor() for cursor creation.
class ConnectionWrapper():
# Disable transaction group.
def disableTransactionGroup(self, disabled):
self.qgisTransactionGroupDisabled = disabled
# Create database connection.
# db_connection: database connection string.
def openConnection(self, db_connection):
# Check for connection is already open.
if self.isConnected() and self.db_source == db_connection:
print("Connection already open: reusing it.")
return
self.db_source = db_connection
self.closeConnection()
# Try to retrieve transaction group if enabled.
if self.qgisTransactionGroupDisabled == False:
tgConn = self.createConnectionFromTransactionsGroup(db_connection)
if tgConn != None:
self.storeQGisTransactionGroupConnection(tgConn)
return
# Transaction group not available: try to create direct connection.
if self.psycopg2Connection == None:
dirConn = self.createSingleConnection(db_connection)
if dirConn != None:
self.storePsycopg2Connection(dirConn)
# Check for a valid connection has been wrapped.
def isValid(self):
if self.psycopg2Connection == None and self.qgisTransactionGroupConnection == None:
return False
return True
# Execute Sql on database connection if available.
# Prior to transaction group.
def executeSql(self, sql):
# Use QGis.QgsTransactionGroup connection.
if self.qgisTransactionGroupConnection != None:
error = self.qgisTransactionGroupConnection.executeSql(sql)
# HACK: PostgreSql can return "Status 2 ()" when PGRES_TUPLES_OK is returned.
if error == "Status 2 ()":
return ""
if error != "":
return error
# Use psycopg2 connection.
elif self.psycopg2Connection != None:
cursor = self.psycopg2Connection.cursor()
if cursor == None:
return "Invalid database connection: cannot get cursor."
try:
cursor.execute(sql)
except Exception as ex:
return ex.diag.context
return ""
return "No connection to database. Please connect before."
# Commit Sql on database.
def commit(self):
# Use psycopg2 connection.
if self.psycopg2Connection != None:
self.psycopg2Connection.commit()
# Use QGis.QgsTransactionGroup connection.
elif self.qgisTransactionGroupConnection != None:
# No commit for transaction group.
pass
else:
print("Connection wrapper doesn't store any database connection!")
# Create new cursor. For direct connection only.
def cursor(self):
if self.psycopg2Connection != None:
return self.psycopg2Connection.cursor()
print("Cannot create cursor without direct connection!")
return None
# Close connection.
def closeConnection(self):
if self.psycopg2Connection != None:
del self.psycopg2Connection
self.psycopg2Connection = None
if self.qgisTransactionGroupConnection != None:
del self.qgisTransactionGroupConnection
self.qgisTransactionGroupConnection = None
# Check for open connection available.
def isConnected(self):
if self.psycopg2Connection != None:
return True
if self.qgisTransactionGroupConnection != None:
return True
return False
#
# Internal members.
#
db_source = ""
# psycopg2 database connection.
psycopg2Connection = None
# QGis.QgsTransactionGroup database connection.
qgisTransactionGroupConnection = None
qgisTransactionGroupDisabled = False
def __exit__(self, exc_type, exc_value, traceback):
self.closeConnection()
def storePsycopg2Connection(self, connection):
self.psycopg2Connection = connection
def storeQGisTransactionGroupConnection(self, connection):
self.qgisTransactionGroupConnection = connection
# Create database connection from existing transactions group.
def createConnectionFromTransactionsGroup(self, db_connection):
# Check for QGis project permits to use transactions group.
activated = False
try:
activated = QgsProject.instance().autoTransaction()
except:
print("QGis project doesn't provide QgsProject::autoTransaction() method.")
return None
if not activated:
print("This QGis project has disabled transactions group.")
return None
# Get transactions group.
# Pg plugin allows only 'postgres' provider.
providerKey = "postgres"
# Database string. Removing user.
sourceUri = QgsDataSourceUri(db_connection)
sourceUri.setUsername("")
uriStr = sourceUri.connectionInfo()
try:
print("Getting transactions group for provider ",
providerKey, " and database connection: ", uriStr)
return QgsProject.instance().transactionGroup(providerKey, uriStr)
except:
print("QgsProject::transactionGroup(): unavailable feature.")
return None
return None
# Create database connection with psycopg2.
def createSingleConnection(self, db_connection):
# Create new single connection.
conn = None
try:
conn = psycopg2.connect(db_connection)
except Exception as ex:
# Ask user for credentials.
self.credDlg = CredentialsDialog(None)
self.credDlg.setErrorText(str(ex))
self.credDlg.setDomainText(db_connection)
self.credDlg.setPasswordText("")
self.credDlg.exec_()
# User has canceled: abort.
if self.credDlg.hasUserCanceled():
return None
# User has validated: get credentials & create single connection again.
else:
db_connection = db_connection + "user='" + self.credDlg.getUserText() + \
"' password='" + self.credDlg.getPasswordText() + "'"
return self.createSingleConnection(db_connection)
# Create has been successfull done.
return conn