Skip to content
Permalink
Browse files

Port last postgis alg to connections API

  • Loading branch information
nyalldawson committed Mar 16, 2020
1 parent cb883b0 commit 184bb0ebb2b6553bfed1c28800b781e0ff6efb86
Showing with 16 additions and 9 deletions.
  1. +16 −9 python/plugins/processing/algs/qgis/PostGISExecuteAndLoadSQL.py
@@ -23,20 +23,18 @@
__date__ = 'May 2018'
__copyright__ = '(C) 2018, Anita Graser'

from qgis.core import (Qgis,
QgsProcessingException,
from qgis.core import (QgsProcessingException,
QgsProcessingParameterString,
QgsApplication,
QgsVectorLayer,
QgsProject,
QgsDataSourceUri,
QgsProcessing,
QgsProcessingException,
QgsProcessingOutputVectorLayer,
QgsProcessingContext,
QgsProcessingParameterProviderConnection,
QgsProcessingFeedback)
QgsProviderRegistry,
QgsProviderConnectionException
)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.tools import postgis


class PostGISExecuteAndLoadSQL(QgisAlgorithm):
@@ -92,11 +90,20 @@ def tags(self):
return self.tr('postgis,table,database').split(',')

def processAlgorithm(self, parameters, context, feedback):
connection = self.parameterAsConnectionName(parameters, self.DATABASE, context)
connection_name = self.parameterAsConnectionName(parameters, self.DATABASE, context)
id_field = self.parameterAsString(parameters, self.ID_FIELD, context)
geom_field = self.parameterAsString(
parameters, self.GEOMETRY_FIELD, context)
uri = postgis.uri_from_name(connection)

# resolve connection details to uri
try:
md = QgsProviderRegistry.instance().providerMetadata('postgres')
conn = md.createConnection(connection_name)
except QgsProviderConnectionException:
raise QgsProcessingException(self.tr('Could not retrieve connection details for {}').format(connection_name))

uri = QgsDataSourceUri(conn.uri())

sql = self.parameterAsString(parameters, self.SQL, context)
sql = sql.replace('\n', ' ')
uri.setDataSource("", "(" + sql.rstrip(';') + ")", geom_field, "", id_field)

0 comments on commit 184bb0e

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