|
27 | 27 |
|
28 | 28 | import re
|
29 | 29 | import os
|
| 30 | +import psycopg2 |
30 | 31 |
|
31 | 32 | from qgis.core import QgsDataSourceURI, QgsCredentials
|
32 | 33 |
|
@@ -54,14 +55,28 @@ def ogrConnectionString(self, uri):
|
54 | 55 | # key='gid' estimatedmetadata=true srid=4326 type=MULTIPOLYGON
|
55 | 56 | # table="t4" (geom) sql=
|
56 | 57 | dsUri = QgsDataSourceURI(layer.dataProvider().dataSourceUri())
|
57 |
| - connInfo = dsUri.connectionInfo() |
58 |
| - (success, user, passwd ) = QgsCredentials.instance().get(connInfo, None, None) |
59 |
| - if success: |
60 |
| - QgsCredentials.instance().put(connInfo, user, passwd) |
61 |
| - ogrstr = ("PG:dbname='%s' host='%s' port='%s' user='%s' password='%s'" |
62 |
| - % (dsUri.database(), dsUri.host(), dsUri.port(), user, passwd)) |
| 58 | + conninfo = dsUri.connectionInfo() |
| 59 | + |
| 60 | + conn = None |
| 61 | + while not conn: |
| 62 | + try: |
| 63 | + conn = psycopg2.connect(dsUri.connectionInfo()) |
| 64 | + except psycopg2.OperationalError, e: |
| 65 | + (ok, user, passwd ) = QgsCredentials.instance().get(conninfo, dsUri.username(), dsUri.password()) |
| 66 | + if not ok: |
| 67 | + break |
| 68 | + |
| 69 | + dsUri.setUsername( user ) |
| 70 | + dsUri.setPassword( passwd ) |
| 71 | + |
| 72 | + if not conn: |
| 73 | + raise RuntimeError('Could not connect to PostgreSQL database - check connection info') |
| 74 | + |
| 75 | + QgsCredentials.instance().put(conninfo, user, passwd) |
| 76 | + ogrstr = "PG:%s" % dsUri.connectionInfo() |
63 | 77 | else:
|
64 | 78 | ogrstr = unicode(layer.source()).split("|")[0]
|
| 79 | + |
65 | 80 | return '"' + ogrstr + '"'
|
66 | 81 |
|
67 | 82 | def ogrLayerName(self, uri):
|
|
0 commit comments