QgsSqlExpressionCompiler class

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.

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.
enum Result { None, Complete, Partial, Fail }
Possible results from expression compilation.

Constructors, destructors, conversion operators

QgsSqlExpressionCompiler(const QgsFields& fields, QgsSqlExpressionCompiler::Flags flags = Flags()) explicit
Constructor for expression compiler.

Public functions

auto compile(const QgsExpression* exp) -> Result virtual
Compiles an expression and returns the result of the compilation.
auto opIsStringComparison(QgsExpressionNodeBinaryOperator::BinaryOperator op) -> bool
Returns true if op is one of.
auto result() -> QString virtual
Returns the compiled expression string for use by the provider.

Protected functions

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

Enum documentation

enum QgsSqlExpressionCompiler::Flag

Enumeration of flags for how provider handles SQL clauses.

Enumerators
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.

enum QgsSqlExpressionCompiler::Result

Possible results from expression compilation.

Enumerators
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.

Function documentation

QgsSqlExpressionCompiler::QgsSqlExpressionCompiler(const QgsFields& fields, QgsSqlExpressionCompiler::Flags flags = Flags()) explicit

Constructor for expression compiler.

Parameters
fields fields from provider
flags flags which control how expression is compiled

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.

QString QgsSqlExpressionCompiler::castToInt(const QString& value) const virtual protected

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.

QString QgsSqlExpressionCompiler::castToReal(const QString& value) const virtual protected

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.

QString QgsSqlExpressionCompiler::castToText(const QString& value) const virtual protected

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%'

Result QgsSqlExpressionCompiler::compileNode(const QgsExpressionNode* node, QString& str) virtual protected

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

Parameters
node expression node to compile
str string representing compiled node should be stored in this parameter
Returns result of node compilation

QString QgsSqlExpressionCompiler::quotedIdentifier(const QString& identifier) virtual protected

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.

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

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

Parameters
value value to quote
ok wil be set to true if value can be compiled

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

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

Returns the Arguments for SQL function for the expression function.

Parameters
fnName expression function name
fnArgs arguments from expression
Returns the arguments updated for SQL Function

Derived classes should override this to help compile functions

QString QgsSqlExpressionCompiler::sqlFunctionFromFunctionName(const QString& fnName) const virtual protected

Returns the SQL function for the expression function.

Parameters
fnName expression function name
Returns the SQL function name

Derived classes should override this to help compile functions