Skip to content
Permalink
Browse files

Merge pull request #6997 from pblottiere/executesql_bugfix

[executesql] Use parameter's name instead of description to save sql query
  • Loading branch information
pblottiere committed May 18, 2018
2 parents b4ec9a3 + 599799d commit e182280102be5f3b8dea6be7d60b9078c0fbf6dc
Showing with 26 additions and 12 deletions.
  1. +26 −12 python/plugins/processing/algs/qgis/ui/ExecuteSQLWidget.py
@@ -28,20 +28,12 @@
import os

from qgis.PyQt import uic
from qgis.PyQt.QtWidgets import QTreeWidgetItem
from qgis.PyQt.QtCore import Qt

from qgis.core import (QgsApplication,
QgsExpressionContextScope,
from qgis.core import (QgsExpressionContextScope,
QgsProcessingParameterString,
QgsProcessingParameterNumber,
QgsExpression,
QgsProcessingModelChildParameterSource,
QgsProcessingParameterFile,
QgsProcessingParameterField,
QgsProcessingOutputString,
QgsProcessingParameterExpression,
QgsProcessingOutputFile)
QgsProcessingModelChildParameterSource)

from qgis.gui import QgsFieldExpressionWidget

@@ -82,7 +74,7 @@ def __init__(self, dialog):

def insert(self):
if self.mExpressionWidget.currentText():
exp = '[% {} %]'.format(self.mExpressionWidget.currentText())
exp = '[%{}%]'.format(self.mExpressionWidget.currentText())
self.mText.insertPlainText(exp)

def setValue(self, value):
@@ -95,6 +87,17 @@ def setValue(self, value):
and v.source() == QgsProcessingModelChildParameterSource.ExpressionText:
text = v.expressionText()

# replace parameter's name by expression (diverging after model save)
names = QgsExpression.referencedVariables(text)

strings = self.dialog.getAvailableValuesOfType(
[QgsProcessingParameterString, QgsProcessingParameterNumber], [])
model_params = [(self.dialog.resolveValueDescription(s), s) for s in strings]

for k, v in model_params:
if v.parameterName() in names:
text = text.replace('[% @{} %]'.format(v.parameterName()), '[% @{} %]'.format(k))

self.mText.setPlainText(text)

def value(self):
@@ -113,8 +116,19 @@ def _expressionValues(self, text):
model_params = [(self.dialog.resolveValueDescription(s), s) for s in strings]

variables = QgsExpression.referencedVariables(text)

# replace description by parameter's name (diverging after model save)
descriptions = QgsExpression.referencedVariables(text)

for k, v in model_params:
if k in descriptions:
text = text.replace('[% @{} %]'.format(k), '[% @{} %]'.format(v.parameterName()))

src = QgsProcessingModelChildParameterSource.fromExpressionText(text)

# add parameters currently used by the expression
expression_values = []
expression_values.append(QgsProcessingModelChildParameterSource.fromExpressionText(text))
expression_values.append(src)

for k, v in model_params:
if k in variables:

0 comments on commit e182280

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