Skip to content

Commit

Permalink
Use parameter's name instead of description to save sql query
Browse files Browse the repository at this point in the history
  • Loading branch information
pblottiere committed May 18, 2018
1 parent 610d99c commit e74e836
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions python/plugins/processing/algs/qgis/ui/ExecuteSQLWidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,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 +95,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 +124,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 e74e836

Please sign in to comment.