Skip to content

Commit

Permalink
Merge pull request #34171 from elpaso/bugfix-gh34132-db-manager-pg-co…
Browse files Browse the repository at this point in the history
…nnection-bug

DB manager PG connection proper API usage
  • Loading branch information
elpaso committed Jan 31, 2020
2 parents 0d55352 + 949efbd commit 17448b1
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 8 deletions.
2 changes: 1 addition & 1 deletion python/plugins/db_manager/db_plugins/gpkg/connector.py
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/db_manager/db_plugins/plugin.py
Expand Up @@ -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
Expand All @@ -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):
Expand Down
11 changes: 8 additions & 3 deletions python/plugins/db_manager/db_plugins/postgis/connector.py
Expand Up @@ -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)

Expand All @@ -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)
Expand Down
Expand Up @@ -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()

Expand All @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion python/plugins/db_manager/db_plugins/postgis/plugin.py
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions python/plugins/db_manager/db_plugins/postgis/plugin_test.py
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 17448b1

Please sign in to comment.