17 #include "qgsexpressionnodeimpl.h" 18 #include "qgsexpressionfunction.h" 19 #include "qgsexpression.h" 30 if ( exp->rootNode() )
43 QString quoted = identifier;
44 quoted.replace(
'"', QLatin1String(
"\"\"" ) );
45 quoted = quoted.prepend(
'\"' ).append(
'\"' );
54 return QStringLiteral(
"NULL" );
56 switch ( value.type() )
59 case QVariant::LongLong:
60 case QVariant::Double:
61 return value.toString();
64 return value.toBool() ? QStringLiteral(
"TRUE" ) : QStringLiteral(
"FALSE" );
67 case QVariant::String:
68 QString v = value.toString();
69 v.replace(
'\'', QLatin1String(
"''" ) );
70 if ( v.contains(
'\\' ) )
71 return v.replace(
'\\', QLatin1String(
"\\\\" ) ).prepend(
"E'" ).append(
'\'' );
73 return v.prepend(
'\'' ).append(
'\'' );
79 switch ( node->nodeType() )
81 case QgsExpressionNode::ntUnaryOperator:
83 const QgsExpressionNodeUnaryOperator *n =
static_cast<const QgsExpressionNodeUnaryOperator *
>( node );
86 case QgsExpressionNodeUnaryOperator::uoNot:
91 result =
"( NOT " + right +
')';
98 case QgsExpressionNodeUnaryOperator::uoMinus:
106 result =
"( - (" + right +
"))";
117 case QgsExpressionNodeBinaryOperator::ntBinaryOperator:
119 const QgsExpressionNodeBinaryOperator *n =
static_cast<const QgsExpressionNodeBinaryOperator *
>( node );
122 bool partialCompilation =
false;
123 bool failOnPartialNode =
false;
126 case QgsExpressionNodeBinaryOperator::boEQ:
127 if ( mFlags.testFlag(
CaseInsensitiveStringMatch ) && n->opLeft()->nodeType() == QgsExpressionNode::ntColumnRef && n->opRight()->nodeType() == QgsExpressionNode::ntColumnRef )
131 partialCompilation =
true;
134 op = QStringLiteral(
"=" );
137 case QgsExpressionNodeBinaryOperator::boGE:
138 op = QStringLiteral(
">=" );
141 case QgsExpressionNodeBinaryOperator::boGT:
142 op = QStringLiteral(
">" );
145 case QgsExpressionNodeBinaryOperator::boLE:
146 op = QStringLiteral(
"<=" );
149 case QgsExpressionNodeBinaryOperator::boLT:
150 op = QStringLiteral(
"<" );
153 case QgsExpressionNodeBinaryOperator::boIs:
154 op = QStringLiteral(
"IS" );
157 case QgsExpressionNodeBinaryOperator::boIsNot:
158 op = QStringLiteral(
"IS NOT" );
162 case QgsExpressionNodeBinaryOperator::boLike:
163 op = QStringLiteral(
"LIKE" );
167 case QgsExpressionNodeBinaryOperator::boILike:
169 op = QStringLiteral(
"LIKE" );
171 op = QStringLiteral(
"ILIKE" );
174 case QgsExpressionNodeBinaryOperator::boNotLike:
175 op = QStringLiteral(
"NOT LIKE" );
180 case QgsExpressionNodeBinaryOperator::boNotILike:
183 op = QStringLiteral(
"NOT LIKE" );
185 op = QStringLiteral(
"NOT ILIKE" );
188 case QgsExpressionNodeBinaryOperator::boOr:
191 if ( nodeIsNullLiteral( n->opLeft() ) || nodeIsNullLiteral( n->opRight() ) )
195 op = QStringLiteral(
"OR" );
198 case QgsExpressionNodeBinaryOperator::boAnd:
201 if ( nodeIsNullLiteral( n->opLeft() ) || nodeIsNullLiteral( n->opRight() ) )
205 op = QStringLiteral(
"AND" );
208 case QgsExpressionNodeBinaryOperator::boNE:
210 op = QStringLiteral(
"<>" );
213 case QgsExpressionNodeBinaryOperator::boMul:
214 op = QStringLiteral(
"*" );
217 case QgsExpressionNodeBinaryOperator::boPlus:
218 op = QStringLiteral(
"+" );
221 case QgsExpressionNodeBinaryOperator::boMinus:
222 op = QStringLiteral(
"-" );
225 case QgsExpressionNodeBinaryOperator::boDiv:
226 op = QStringLiteral(
"/" );
229 case QgsExpressionNodeBinaryOperator::boMod:
230 op = QStringLiteral(
"%" );
233 case QgsExpressionNodeBinaryOperator::boConcat:
234 op = QStringLiteral(
"||" );
237 case QgsExpressionNodeBinaryOperator::boIntDiv:
238 op = QStringLiteral(
"/" );
241 case QgsExpressionNodeBinaryOperator::boPow:
242 op = QStringLiteral(
"^" );
245 case QgsExpressionNodeBinaryOperator::boRegexp:
246 op = QStringLiteral(
"~" );
265 if ( right.isEmpty() )
272 result =
'(' + left +
' ' + op +
' ' + right +
')';
273 if ( n->op() == QgsExpressionNodeBinaryOperator::boIntDiv )
276 if ( result.isEmpty() )
291 case QgsExpressionNode::ntLiteral:
293 const QgsExpressionNodeLiteral *n =
static_cast<const QgsExpressionNodeLiteral *
>( node );
309 case QgsExpressionNode::ntColumnRef:
311 const QgsExpressionNodeColumnRef *n =
static_cast<const QgsExpressionNodeColumnRef *
>( node );
322 case QgsExpressionNode::ntInOperator:
324 const QgsExpressionNodeInOperator *n =
static_cast<const QgsExpressionNodeInOperator *
>( node );
328 Q_FOREACH (
const QgsExpressionNode *ln, n->list()->list() )
347 result = QStringLiteral(
"%1 %2IN (%3)" ).arg( nd, n->isNotIn() ? QStringLiteral(
"NOT " ) : QString(), list.join(
',' ) );
351 case QgsExpressionNode::ntFunction:
353 const QgsExpressionNodeFunction *n =
static_cast<const QgsExpressionNodeFunction *
>( node );
354 QgsExpressionFunction *fd = QgsExpression::Functions()[n->fnIndex()];
365 Q_FOREACH (
const QgsExpressionNode *ln, n->args()->list() )
383 result = QStringLiteral(
"%1(%2)" ).arg( nd, args.join(
',' ) );
387 case QgsExpressionNode::ntCondition:
403 return QStringList( fnArgs );
418 bool QgsSqlExpressionCompiler::nodeIsNullLiteral(
const QgsExpressionNode *node )
const 420 if ( node->nodeType() != QgsExpressionNode::ntLiteral )
423 const QgsExpressionNodeLiteral *nLit =
static_cast<const QgsExpressionNodeLiteral *
>( node );
424 return nLit->value().isNull();
Provider treats LIKE as case-insensitive.
virtual Result compile(const QgsExpression *exp)
Compiles an expression and returns the result of the compilation.
Container of fields for a vector layer.
Provider cannot handle expression.
Provider does not support using NULL with boolean logic, e.g., "(...) OR NULL".
QgsSqlExpressionCompiler(const QgsFields &fields, QgsSqlExpressionCompiler::Flags flags=Flags())
Constructor for expression compiler.
Provider does not unary minus, e.g., " -( 100 * 2 ) = ...".
int indexFromName(const QString &fieldName) const
Get the field index from the field name.
virtual QString quotedValue(const QVariant &value, bool &ok)
Returns a quoted attribute value, in the format expected by the provider.
virtual QStringList sqlArgumentsFromFunctionName(const QString &fnName, const QStringList &fnArgs) const
Return the Arguments for SQL function for the expression function.
virtual QString sqlFunctionFromFunctionName(const QString &fnName) const
Return the SQL function for the expression function.
virtual QString castToInt(const QString &value) const
Casts a value to a integer result.
Result
Possible results from expression compilation.
Expression was partially compiled, but provider will return extra records and results must be double-...
Expression was successfully compiled and can be completely delegated to provider. ...
virtual QString castToReal(const QString &value) const
Casts a value to a real result.
Provider performs case-insensitive string matching for all strings.
virtual QString quotedIdentifier(const QString &identifier)
Returns a quoted column identifier, in the format expected by the provider.
virtual Result compileNode(const QgsExpressionNode *node, QString &str)
Compiles an expression node and returns the result of the compilation.
virtual QString result()
Returns the compiled expression string for use by the provider.
Dividing int by int results in int on provider. Subclass must implement the castToReal() function to ...