28
28
from ..connector import DBConnector
29
29
from ..plugin import ConnectionError , DbError , Table
30
30
31
+ from qgis .core import QgsCredentials
32
+
31
33
import os
32
34
import psycopg2
33
35
import psycopg2 .extensions
@@ -45,23 +47,50 @@ def __init__(self, uri):
45
47
46
48
self .host = uri .host () or os .environ .get ('PGHOST' )
47
49
self .port = uri .port () or os .environ .get ('PGPORT' )
48
- self . user = uri . username () or os . environ . get ( 'PGUSER' ) or os . environ . get ( 'USER' )
49
- self . dbname = uri .database () or os .environ .get ('PGDATABASE ' ) or self . user
50
- self . passwd = uri .password () or os .environ .get ('PGPASSWORD' )
50
+
51
+ username = uri .username () or os .environ .get ('PGUSER ' ) or os . environ . get ( 'USER' )
52
+ password = uri .password () or os .environ .get ('PGPASSWORD' )
51
53
52
54
try :
53
55
self .connection = psycopg2 .connect ( self ._connectionInfo ().encode ('utf-8' ) )
54
- self .connection .set_isolation_level (psycopg2 .extensions .ISOLATION_LEVEL_AUTOCOMMIT )
55
56
except self .connection_error_types (), e :
56
- raise ConnectionError (e )
57
+ err = str (e )
58
+ uri = self .uri ()
59
+ conninfo = uri .connectionInfo ()
60
+
61
+ for i in range (3 ):
62
+ (ok , username , password ) = QgsCredentials .instance ().get (conninfo , username , password , err )
63
+ if not ok :
64
+ raise ConnectionError (e )
65
+
66
+ if username :
67
+ uri .setUsername ( username )
68
+
69
+ if password :
70
+ uri .setPassword ( password )
71
+
72
+ try :
73
+ self .connection = psycopg2 .connect ( uri .connectionInfo ().encode ('utf-8' ) )
74
+ QgsCredentials .instance ().put (conninfo , username , password )
75
+ except self .connection_error_types (), e :
76
+ if i == 2 :
77
+ raise ConnectionError (e )
78
+
79
+ err = str (e )
80
+
81
+ self .connection .set_isolation_level (psycopg2 .extensions .ISOLATION_LEVEL_AUTOCOMMIT )
82
+
83
+ c = self ._execute (None , u"SELECT current_user" )
84
+ self .user = self ._fetchone (c )
85
+ self ._close_cursor (c )
57
86
58
87
self ._checkSpatial ()
59
88
self ._checkRaster ()
60
89
self ._checkGeometryColumnsTable ()
61
90
self ._checkRasterColumnsTable ()
62
91
63
92
def _connectionInfo (self ):
64
- return unicode (self ._uri .connectionInfo ())
93
+ return unicode (self .uri () .connectionInfo ())
65
94
66
95
def _checkSpatial (self ):
67
96
""" check whether postgis_version is present in catalog """
@@ -158,7 +187,7 @@ def fieldTypes(self):
158
187
159
188
def getDatabasePrivileges (self ):
160
189
""" db privileges: (can create schemas, can create temp. tables) """
161
- sql = u"SELECT has_database_privilege(%(d)s , 'CREATE'), has_database_privilege(%(d)s , 'TEMP')" % { 'd' : self . quoteString ( self . dbname ) }
190
+ sql = u"SELECT has_database_privilege(current_database() , 'CREATE'), has_database_privilege(current_database() , 'TEMP')"
162
191
c = self ._execute (None , sql )
163
192
res = self ._fetchone (c )
164
193
self ._close_cursor (c )
0 commit comments