26 , mIgnoreStaticNodes( ignoreStaticNodes )
57 QString quoted = identifier;
58 quoted.replace(
'"', QLatin1String(
"\"\"" ) );
59 quoted = quoted.prepend(
'\"' ).append(
'\"' );
68 return QStringLiteral(
"NULL" );
70 switch ( value.userType() )
72 case QMetaType::Type::Int:
73 case QMetaType::Type::LongLong:
74 case QMetaType::Type::Double:
75 return value.toString();
77 case QMetaType::Type::Bool:
78 return value.toBool() ? QStringLiteral(
"TRUE" ) : QStringLiteral(
"FALSE" );
81 case QMetaType::Type::QString:
82 QString v = value.toString();
83 v.replace(
'\'', QLatin1String(
"''" ) );
84 if ( v.contains(
'\\' ) )
85 return v.replace(
'\\', QLatin1String(
"\\\\" ) ).prepend(
"E'" ).append(
'\'' );
87 return v.prepend(
'\'' ).append(
'\'' );
94 if ( staticRes !=
Fail )
108 const int idx =
mFields.indexFromName( col->
name() );
119 return nodeIsNumeric( op->
opLeft() ) && nodeIsNumeric( op->
opRight() );
124 return nodeIsNumeric( op->
operand() );
145 result =
"( NOT " + right +
')';
160 result =
"( - (" + right +
"))";
176 bool partialCompilation =
false;
177 bool failOnPartialNode =
false;
185 partialCompilation =
true;
188 op = QStringLiteral(
"=" );
192 op = QStringLiteral(
">=" );
196 op = QStringLiteral(
">" );
200 op = QStringLiteral(
"<=" );
204 op = QStringLiteral(
"<" );
208 op = QStringLiteral(
"IS" );
212 op = QStringLiteral(
"IS NOT" );
217 op = QStringLiteral(
"LIKE" );
223 op = QStringLiteral(
"LIKE" );
225 op = QStringLiteral(
"ILIKE" );
229 op = QStringLiteral(
"NOT LIKE" );
237 op = QStringLiteral(
"NOT LIKE" );
239 op = QStringLiteral(
"NOT ILIKE" );
245 if ( nodeIsNullLiteral( n->
opLeft() ) || nodeIsNullLiteral( n->
opRight() ) )
249 op = QStringLiteral(
"OR" );
255 if ( nodeIsNullLiteral( n->
opLeft() ) || nodeIsNullLiteral( n->
opRight() ) )
259 op = QStringLiteral(
"AND" );
264 op = QStringLiteral(
"<>" );
268 op = QStringLiteral(
"*" );
274 if ( nodeIsNumeric( nodeOp->
opLeft() ) && nodeIsNumeric( nodeOp->
opRight() ) )
276 op = QStringLiteral(
"+" );
282 op = QStringLiteral(
"-" );
286 op = QStringLiteral(
"/" );
290 op = QStringLiteral(
"%" );
294 op = QStringLiteral(
"||" );
298 op = QStringLiteral(
"/" );
302 op = QStringLiteral(
"^" );
306 op = QStringLiteral(
"~" );
328 if ( right.isEmpty() )
335 result =
'(' + left +
' ' + op +
' ' + right +
')';
387 res.append( n->
negate() ? QStringLiteral(
" NOT BETWEEN %1" ).arg( s ) : QStringLiteral(
" BETWEEN %1" ).arg( s ) );
402 res.append( QStringLiteral(
" AND %1" ).arg( s ) );
404 return betweenResult;
431 const int fieldIndex =
mFields.lookupField( n->
name() );
432 if ( fieldIndex == -1 )
447 const auto constList = n->
list()->
list();
467 result = QStringLiteral(
"%1 %2IN (%3)" ).arg( nd, n->
isNotIn() ? QStringLiteral(
"NOT " ) : QString(), list.join(
',' ) );
485 const auto constList = n->
args()->
list();
504 result = !nd.isEmpty() ? QStringLiteral(
"%1(%2)" ).arg( nd, args.join(
',' ) ) : args.join(
',' );
527 return QStringList( fnArgs );
549 if ( mIgnoreStaticNodes )
571bool QgsSqlExpressionCompiler::nodeIsNullLiteral(
const QgsExpressionNode *node )
const
An abstract base class for defining QgsExpression functions.
QString name() const
The name of the function.
SQL-like BETWEEN and NOT BETWEEN predicates.
bool negate() const
Returns true if the predicate is an exclusion test (NOT BETWEEN).
QgsExpressionNode * lowerBound() const
Returns the lower bound expression node of the range.
QgsExpressionNode * higherBound() const
Returns the higher bound expression node of the range.
QgsExpressionNode * node() const
Returns the expression node.
A binary expression operator, which operates on two values.
QgsExpressionNode * opLeft() const
Returns the node to the left of the operator.
QgsExpressionNode * opRight() const
Returns the node to the right of the operator.
QgsExpressionNodeBinaryOperator::BinaryOperator op() const
Returns the binary operator.
BinaryOperator
list of binary operators
An expression node which takes its value from a feature's field.
QString name() const
The name of the column.
An expression node for expression functions.
int fnIndex() const
Returns the index of the node's function.
QgsExpressionNode::NodeList * args() const
Returns a list of arguments specified for the function.
An expression node for value IN or NOT IN clauses.
QgsExpressionNode * node() const
Returns the expression node.
QgsExpressionNode::NodeList * list() const
Returns the list of nodes to search for matching values within.
bool isNotIn() const
Returns true if this node is a "NOT IN" operator, or false if the node is a normal "IN" operator.
An expression node for literal values.
QVariant value() const
The value of the literal.
A unary node is either negative as in boolean (not) or as in numbers (minus).
QgsExpressionNodeUnaryOperator::UnaryOperator op() const
Returns the unary operator.
QgsExpressionNode * operand() const
Returns the node the operator will operate upon.
QList< QgsExpressionNode * > list()
Gets a list of all the nodes.
Abstract base class for all nodes that can appear in an expression.
bool hasCachedStaticValue() const
Returns true if the node can be replaced by a static cached value.
virtual QgsExpressionNode::NodeType nodeType() const =0
Gets the type of this node.
QVariant cachedStaticValue() const
Returns the node's static cached value.
NodeType
Known node types.
@ ntBetweenOperator
Between operator.
@ ntIndexOperator
Index operator.
Handles parsing and evaluation of expressions (formerly called "search strings").
static const QList< QgsExpressionFunction * > & Functions()
const QgsExpressionNode * rootNode() const
Returns the root node of the expression.
Container of fields for a vector layer.
virtual Result compileNode(const QgsExpressionNode *node, QString &str)
Compiles an expression node and returns the result of the compilation.
virtual Result compile(const QgsExpression *exp)
Compiles an expression and returns the result of the compilation.
Result
Possible results from expression compilation.
@ Fail
Provider cannot handle expression.
@ Complete
Expression was successfully compiled and can be completely delegated to provider.
@ Partial
Expression was partially compiled, but provider will return extra records and results must be double-...
virtual QStringList sqlArgumentsFromFunctionName(const QString &fnName, const QStringList &fnArgs) const
Returns the Arguments for SQL function for the expression function.
virtual Result replaceNodeByStaticCachedValueIfPossible(const QgsExpressionNode *node, QString &str)
Tries to replace a node by its static cached value where possible.
virtual QString result()
Returns the compiled expression string for use by the provider.
virtual QString quotedValue(const QVariant &value, bool &ok)
Returns a quoted attribute value, in the format expected by the provider.
virtual QString castToText(const QString &value) const
Casts a value to a text result.
virtual QString castToInt(const QString &value) const
Casts a value to a integer result.
virtual QString quotedIdentifier(const QString &identifier)
Returns a quoted column identifier, in the format expected by the provider.
virtual QString sqlFunctionFromFunctionName(const QString &fnName) const
Returns the SQL function for the expression function.
QgsSqlExpressionCompiler(const QgsFields &fields, QgsSqlExpressionCompiler::Flags flags=Flags(), bool ignoreStaticNodes=false)
Constructor for expression compiler.
virtual QString castToReal(const QString &value) const
Casts a value to a real result.
bool opIsStringComparison(QgsExpressionNodeBinaryOperator::BinaryOperator op)
Returns true if op is one of.
@ LikeIsCaseInsensitive
Provider treats LIKE as case-insensitive.
@ NoUnaryMinus
Provider does not unary minus, e.g., " -( 100 * 2 ) = ...".
@ CaseInsensitiveStringMatch
Provider performs case-insensitive string matching for all strings.
@ NoNullInBooleanLogic
Provider does not support using NULL with boolean logic, e.g., "(...) OR NULL".
@ IntegerDivisionResultsInInteger
Dividing int by int results in int on provider. Subclass must implement the castToReal() function to ...
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
static bool isNumericType(QMetaType::Type metaType)
Returns true if the specified metaType is a numeric type.