Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DB manager PG connection proper API usage #34171

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion python/plugins/db_manager/db_plugins/gpkg/connector.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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