Skip to content
Permalink
Browse files

Merge pull request #34171 from elpaso/bugfix-gh34132-db-manager-pg-co…

…nnection-bug

DB manager PG connection proper API usage
  • Loading branch information
elpaso committed Jan 31, 2020
2 parents 0d55352 + 949efbd commit 17448b1aac1867e606f09b3b03d2e29ed29226c9
@@ -67,7 +67,7 @@ def __init__(self, uri, connection):
# QgsAbstractDatabaseProviderConnection instance
self.core_connection = md.findConnection(connection.connectionName())
if self.core_connection is None:
self.core_connection = md.createConnection(connection.connectionName(), uri.database())
self.core_connection = md.createConnection(uri.uri(), {})
self.has_raster = False
self.mapSridToName = {}
# To be removed when migration to new API is completed
@@ -243,7 +243,7 @@ class DbItemObject(QObject):
deleted = pyqtSignal()

def __init__(self, parent=None):
QObject.__init__(self, parent)
super().__init__(parent)

def database(self):
return None
@@ -264,7 +264,7 @@ def registerActions(self, mainWindow):
class Database(DbItemObject):

def __init__(self, dbplugin, uri):
DbItemObject.__init__(self, dbplugin)
super().__init__(dbplugin)
self.connector = self.connectorsFactory(uri)

def connectorsFactory(self, uri):
@@ -181,11 +181,13 @@ def close(self):

class PostGisDBConnector(DBConnector):

def __init__(self, uri):
def __init__(self, uri, connection):
"""Creates a new PostgreSQL connector
:param uri: data source URI
:type uri: QgsDataSourceUri
:param connection: the plugin parent instance
:type connection: PostGisDBPlugin
"""
DBConnector.__init__(self, uri)

@@ -203,8 +205,11 @@ def __init__(self, uri):
#self.passwd = uri.password()
self.host = uri.host()

md = QgsProviderRegistry.instance().providerMetadata('postgres')
self.core_connection = md.createConnection(uri.database())
md = QgsProviderRegistry.instance().providerMetadata(connection.providerName())
# QgsAbstractDatabaseProviderConnection instance
self.core_connection = md.findConnection(connection.connectionName())
if self.core_connection is None:
self.core_connection = md.createConnection(uri.uri(), {})

c = self._execute(None, u"SELECT current_user,current_database()")
self.user, self.dbname = self._fetchone(c)
@@ -26,6 +26,7 @@
from qgis.testing import start_app, unittest
from qgis.core import QgsDataSourceUri
from qgis.utils import iface
from qgis.PyQt.QtCore import QObject

start_app()

@@ -51,7 +52,12 @@ def _getDatabase(self, connector):
# See https://github.com/qgis/QGIS/issues/24525
# and https://github.com/qgis/QGIS/issues/19005
def test_dbnameLessURI(self):
c = PostGisDBConnector(QgsDataSourceUri())

obj = QObject() # needs to be kept alive
obj.connectionName = lambda: 'fake'
obj.providerName = lambda: 'postgres'

c = PostGisDBConnector(QgsDataSourceUri(), obj)
self.assertIsInstance(c, PostGisDBConnector)
uri = c.uri()

@@ -112,7 +112,7 @@ def __init__(self, connection, uri):
Database.__init__(self, connection, uri)

def connectorsFactory(self, uri):
return PostGisDBConnector(uri)
return PostGisDBConnector(uri, self.connection())

def dataTablesFactory(self, row, db, schema=None):
return PGTable(row, db, schema)
@@ -96,6 +96,8 @@ def check_rasterTableURI(expected_dbname):
self.assertGreaterEqual(raster_tables_count, 1)

obj = QObject() # needs to be kept alive
obj.connectionName = lambda: 'fake'
obj.providerName = lambda: 'postgres'

# Test for empty URI
# See https://github.com/qgis/QGIS/issues/24525
@@ -134,6 +136,8 @@ def check_rasterTableURI(expected_dbname):
def test_unicodeInQuery(self):
os.environ['PGDATABASE'] = self.testdb
obj = QObject() # needs to be kept alive
obj.connectionName = lambda: 'fake'
obj.providerName = lambda: 'postgres'
database = PGDatabase(obj, QgsDataSourceUri())
self.assertIsInstance(database, PGDatabase)
# SQL as string literal

0 comments on commit 17448b1

Please sign in to comment.
You can’t perform that action at this time.