QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
QgsSqlExpressionCompiler Class Reference

Generic expression compiler for translation to provider specific SQL WHERE clauses. More...

#include <qgssqlexpressioncompiler.h>

Public Types

enum  Flag {
  CaseInsensitiveStringMatch = 1 , LikeIsCaseInsensitive = 1 << 1 , NoNullInBooleanLogic = 1 << 2 , NoUnaryMinus = 1 << 3 ,
  IntegerDivisionResultsInInteger = 1 << 4
}
 Enumeration of flags for how provider handles SQL clauses. More...
 
typedef QFlags< FlagFlags
 
enum  Result { None , Complete , Partial , Fail }
 Possible results from expression compilation. More...
 

Public Member Functions

 QgsSqlExpressionCompiler (const QgsFields &fields, QgsSqlExpressionCompiler::Flags flags=Flags(), bool ignoreStaticNodes=false)
 Constructor for expression compiler.
 
virtual ~QgsSqlExpressionCompiler ()=default
 
virtual Result compile (const QgsExpression *exp)
 Compiles an expression and returns the result of the compilation.
 
bool opIsStringComparison (QgsExpressionNodeBinaryOperator::BinaryOperator op)
 Returns true if op is one of.
 
virtual QString result ()
 Returns the compiled expression string for use by the provider.
 

Protected Member Functions

virtual QString castToInt (const QString &value) const
 Casts a value to a integer result.
 
virtual QString castToReal (const QString &value) const
 Casts a value to a real result.
 
virtual QString castToText (const QString &value) const
 Casts a value to a text result.
 
virtual Result compileNode (const QgsExpressionNode *node, QString &str)
 Compiles an expression node and returns the result of the compilation.
 
virtual QString quotedIdentifier (const QString &identifier)
 Returns a quoted column identifier, in the format expected by the provider.
 
virtual QString quotedValue (const QVariant &value, bool &ok)
 Returns a quoted attribute value, in the format expected by the provider.
 
virtual Result replaceNodeByStaticCachedValueIfPossible (const QgsExpressionNode *node, QString &str)
 Tries to replace a node by its static cached value where possible.
 
virtual QStringList sqlArgumentsFromFunctionName (const QString &fnName, const QStringList &fnArgs) const
 Returns the Arguments for SQL function for the expression function.
 
virtual QString sqlFunctionFromFunctionName (const QString &fnName) const
 Returns the SQL function for the expression function.
 

Protected Attributes

QgsFields mFields
 
QString mResult
 

Detailed Description

Generic expression compiler for translation to provider specific SQL WHERE clauses.

This class is designed to be overridden by providers to take advantage of expression compilation, so that feature requests can take advantage of the provider's native filtering support.

Note
Not part of stable API, may change in future versions of QGIS
Not available in Python bindings

Definition at line 39 of file qgssqlexpressioncompiler.h.

Member Typedef Documentation

◆ Flags

Definition at line 63 of file qgssqlexpressioncompiler.h.

Member Enumeration Documentation

◆ Flag

Enumeration of flags for how provider handles SQL clauses.

Enumerator
CaseInsensitiveStringMatch 

Provider performs case-insensitive string matching for all strings.

LikeIsCaseInsensitive 

Provider treats LIKE as case-insensitive.

NoNullInBooleanLogic 

Provider does not support using NULL with boolean logic, e.g., "(...) OR NULL".

NoUnaryMinus 

Provider does not unary minus, e.g., " -( 100 * 2 ) = ...".

IntegerDivisionResultsInInteger 

Dividing int by int results in int on provider. Subclass must implement the castToReal() function to allow compilation of division.

Definition at line 55 of file qgssqlexpressioncompiler.h.

◆ Result

Possible results from expression compilation.

Enumerator
None 

No 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-checked using QGIS' expression engine.

Fail 

Provider cannot handle expression.

Definition at line 44 of file qgssqlexpressioncompiler.h.

Constructor & Destructor Documentation

◆ QgsSqlExpressionCompiler()

QgsSqlExpressionCompiler::QgsSqlExpressionCompiler ( const QgsFields fields,
QgsSqlExpressionCompiler::Flags  flags = Flags(),
bool  ignoreStaticNodes = false 
)
explicit

Constructor for expression compiler.

Parameters
fieldsfields from provider
flagsflags which control how expression is compiled
ignoreStaticNodesIf an expression has been partially precalculated due to static nodes in the expression, setting this argument to false will prevent these precalculated values from being utilized during compilation of the expression. This flag significantly limits the effectiveness of compilation and should be used for debugging purposes only. (Since QGIS 3.18)

Definition at line 22 of file qgssqlexpressioncompiler.cpp.

◆ ~QgsSqlExpressionCompiler()

virtual QgsSqlExpressionCompiler::~QgsSqlExpressionCompiler ( )
virtualdefault

Member Function Documentation

◆ castToInt()

QString QgsSqlExpressionCompiler::castToInt ( const QString &  value) const
protectedvirtual

Casts a value to a integer result.

Subclasses must reimplement this to cast a numeric value to a integer type value so that integer division results in a integer value result instead of real.

Definition at line 540 of file qgssqlexpressioncompiler.cpp.

◆ castToReal()

QString QgsSqlExpressionCompiler::castToReal ( const QString &  value) const
protectedvirtual

Casts a value to a real result.

Subclasses which indicate the IntegerDivisionResultsInInteger flag must reimplement this to cast a numeric value to a real type value so that division results in a real value result instead of integer.

Definition at line 529 of file qgssqlexpressioncompiler.cpp.

◆ castToText()

QString QgsSqlExpressionCompiler::castToText ( const QString &  value) const
protectedvirtual

Casts a value to a text result.

Subclasses that support casting to text may implement this function to get equal behavior to the QGIS expression engine when string comparison operators are applied on non-string data.

Example

579 LIKE '5%'

which on a postgres database needs to be

579::text LIKE '5%'
Since
QGIS 3.2

Definition at line 535 of file qgssqlexpressioncompiler.cpp.

◆ compile()

QgsSqlExpressionCompiler::Result QgsSqlExpressionCompiler::compile ( const QgsExpression exp)
virtual

Compiles an expression and returns the result of the compilation.

Definition at line 29 of file qgssqlexpressioncompiler.cpp.

◆ compileNode()

QgsSqlExpressionCompiler::Result QgsSqlExpressionCompiler::compileNode ( const QgsExpressionNode node,
QString &  str 
)
protectedvirtual

Compiles an expression node and returns the result of the compilation.

Parameters
nodeexpression node to compile
strstring representing compiled node should be stored in this parameter
Returns
result of node compilation

Definition at line 90 of file qgssqlexpressioncompiler.cpp.

◆ opIsStringComparison()

bool QgsSqlExpressionCompiler::opIsStringComparison ( QgsExpressionNodeBinaryOperator::BinaryOperator  op)

Returns true if op is one of.

  • LIKE
  • ILIKE
  • NOT LIKE
  • NOT ILIKE
  • ~ (regexp)

In such cases the left operator will be cast to string to behave equal to QGIS own expression engine.

Since
QGIS 3.2

Definition at line 42 of file qgssqlexpressioncompiler.cpp.

◆ quotedIdentifier()

QString QgsSqlExpressionCompiler::quotedIdentifier ( const QString &  identifier)
protectedvirtual

Returns a quoted column identifier, in the format expected by the provider.

Derived classes should override this if special handling of column identifiers is required.

See also
quotedValue()

Definition at line 54 of file qgssqlexpressioncompiler.cpp.

◆ quotedValue()

QString QgsSqlExpressionCompiler::quotedValue ( const QVariant &  value,
bool &  ok 
)
protectedvirtual

Returns a quoted attribute value, in the format expected by the provider.

Derived classes should override this if special handling of attribute values is required.

Parameters
valuevalue to quote
okwill be set to true if value can be compiled
See also
quotedIdentifier()

Definition at line 62 of file qgssqlexpressioncompiler.cpp.

◆ replaceNodeByStaticCachedValueIfPossible()

QgsSqlExpressionCompiler::Result QgsSqlExpressionCompiler::replaceNodeByStaticCachedValueIfPossible ( const QgsExpressionNode node,
QString &  str 
)
protectedvirtual

Tries to replace a node by its static cached value where possible.

Since
QGIS 3.18

Definition at line 546 of file qgssqlexpressioncompiler.cpp.

◆ result()

QString QgsSqlExpressionCompiler::result ( )
virtual

Returns the compiled expression string for use by the provider.

Definition at line 37 of file qgssqlexpressioncompiler.cpp.

◆ sqlArgumentsFromFunctionName()

QStringList QgsSqlExpressionCompiler::sqlArgumentsFromFunctionName ( const QString &  fnName,
const QStringList &  fnArgs 
) const
protectedvirtual

Returns the Arguments for SQL function for the expression function.

Derived classes should override this to help compile functions

Parameters
fnNameexpression function name
fnArgsarguments from expression
Returns
the arguments updated for SQL Function

Definition at line 523 of file qgssqlexpressioncompiler.cpp.

◆ sqlFunctionFromFunctionName()

QString QgsSqlExpressionCompiler::sqlFunctionFromFunctionName ( const QString &  fnName) const
protectedvirtual

Returns the SQL function for the expression function.

Derived classes should override this to help compile functions

Parameters
fnNameexpression function name
Returns
the SQL function name

Definition at line 517 of file qgssqlexpressioncompiler.cpp.

Member Data Documentation

◆ mFields

QgsFields QgsSqlExpressionCompiler::mFields
protected

Definition at line 184 of file qgssqlexpressioncompiler.h.

◆ mResult

QString QgsSqlExpressionCompiler::mResult
protected

Definition at line 183 of file qgssqlexpressioncompiler.h.


The documentation for this class was generated from the following files: