Skip to content

Commit

Permalink
Merge pull request #6997 from pblottiere/executesql_bugfix
Browse files Browse the repository at this point in the history
[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 e182280
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions python/plugins/processing/algs/qgis/ui/ExecuteSQLWidget.py
Expand Up @@ -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

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

0 comments on commit e182280

Please sign in to comment.