2626
2727__revision__ = '$Format:%H$'
2828
29- from qgis .core import (QgsApplication )
29+ from qgis .core import (QgsApplication ,
30+ QgsProcessingParameterString )
3031from processing .algs .qgis .QgisAlgorithm import QgisAlgorithm
3132from processing .core .GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
32- from processing .core .parameters import ParameterString
3333from processing .tools import postgis
3434
3535
@@ -49,13 +49,15 @@ def group(self):
4949
5050 def __init__ (self ):
5151 super ().__init__ ()
52- self .addParameter (ParameterString (
52+
53+ db_param = QgsProcessingParameterString (
5354 self .DATABASE ,
54- self .tr ('Database' ),
55- metadata = {
56- 'widget_wrapper' : {
57- 'class' : 'processing.gui.wrappers_postgis.ConnectionWidgetWrapper' }}))
58- self .addParameter (ParameterString (self .SQL , self .tr ('SQL query' ), '' , True ))
55+ self .tr ('Database (connection name)' ))
56+ db_param .setMetadata ({
57+ 'widget_wrapper' : {
58+ 'class' : 'processing.gui.wrappers_postgis.ConnectionWidgetWrapper' }})
59+ self .addParameter (db_param )
60+ self .addParameter (QgsProcessingParameterString (self .SQL , self .tr ('SQL query' )))
5961
6062 def name (self ):
6163 return 'postgisexecutesql'
@@ -64,11 +66,13 @@ def displayName(self):
6466 return self .tr ('PostGIS execute SQL' )
6567
6668 def processAlgorithm (self , parameters , context , feedback ):
67- connection = self .getParameterValue (self .DATABASE )
68- self .db = postgis .GeoDB .from_name (connection )
69- sql = self .getParameterValue (self .SQL ).replace ('\n ' , ' ' )
69+ connection = self .parameterAsString (parameters , self .DATABASE , context )
70+ db = postgis .GeoDB .from_name (connection )
71+
72+ sql = self .parameterAsString (parameters , self .SQL , context ).replace ('\n ' , ' ' )
7073 try :
71- self . db ._exec_sql_and_commit (str (sql ))
74+ db ._exec_sql_and_commit (str (sql ))
7275 except postgis .DbError as e :
7376 raise GeoAlgorithmExecutionException (
7477 self .tr ('Error executing SQL:\n {0}' ).format (str (e )))
78+ return {}
0 commit comments