QGIS API Documentation 3.99.0-Master (09f76ad7019)
Loading...
Searching...
No Matches
qgssqliteexpressioncompiler.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgssqliteexpressioncompiler.cpp
3 -----------------------------------
4 begin : November 2015
5 copyright : (C) 2015 Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17
19
20#include "qgsexpression.h"
24#include "qgssqliteutils.h"
25
26#include <QString>
27
28using namespace Qt::StringLiterals;
29
30QgsSQLiteExpressionCompiler::QgsSQLiteExpressionCompiler( const QgsFields &fields, bool ignoreStaticNodes )
31 : QgsSqlExpressionCompiler( fields, QgsSqlExpressionCompiler::LikeIsCaseInsensitive | QgsSqlExpressionCompiler::IntegerDivisionResultsInInteger, ignoreStaticNodes )
32{
33}
34
35QgsSqlExpressionCompiler::Result QgsSQLiteExpressionCompiler::compileNode( const QgsExpressionNode *node, QString &result )
36{
37 const QgsSqlExpressionCompiler::Result staticRes = replaceNodeByStaticCachedValueIfPossible( node, result );
38 if ( staticRes != Fail )
39 return staticRes;
40
41 switch ( node->nodeType() )
42 {
44 {
45 const QgsExpressionNodeBinaryOperator *op = static_cast<const QgsExpressionNodeBinaryOperator *>( node );
46 switch ( op->op() )
47 {
50 return Fail; //not supported by SQLite
51
54 {
55 QString opL, opR;
56
57 if ( compileNode( op->opLeft(), opL ) != Complete ||
58 compileNode( op->opRight(), opR ) != Complete )
59 return Fail;
60
61 result = u"lower(%1) %2 lower(%3) ESCAPE '\\'"_s
62 .arg( opL )
63 .arg( op->op() == QgsExpressionNodeBinaryOperator::boILike ? u"LIKE"_s : u"NOT LIKE"_s )
64 .arg( opR );
65
66 return Complete;
67 }
68
69 default:
70 //fallback to default handling
71 return QgsSqlExpressionCompiler::compileNode( node, result );
72 }
73 }
74
76 {
77 const QgsExpressionNodeFunction *n = static_cast<const QgsExpressionNodeFunction *>( node );
79
80 if ( fd->name() == "make_datetime"_L1 || fd->name() == "make_date"_L1 || fd->name() == "make_time"_L1 )
81 {
82 const auto constList = n->args()->list();
83 for ( const QgsExpressionNode *ln : constList )
84 {
85 if ( ln->nodeType() != QgsExpressionNode::ntLiteral )
86 return Fail;
87 }
88 }
89
90 return QgsSqlExpressionCompiler::compileNode( node, result );
91 }
92
93 default:
94 break;
95 }
96
97 return QgsSqlExpressionCompiler::compileNode( node, result );
98}
99
100QString QgsSQLiteExpressionCompiler::quotedIdentifier( const QString &identifier )
101{
102 return QgsSqliteUtils::quotedIdentifier( identifier );
103}
104
105QString QgsSQLiteExpressionCompiler::quotedValue( const QVariant &value, bool &ok )
106{
107 ok = true;
108 return QgsSqliteUtils::quotedValue( value );
109}
110
111QString QgsSQLiteExpressionCompiler::sqlFunctionFromFunctionName( const QString &fnName ) const
112{
113 static const QMap<QString, QString> FN_NAMES
114 {
115 { "abs", "abs" },
116 { "char", "char" },
117 { "coalesce", "coalesce" },
118 { "lower", "lower" },
119 { "round", "round" },
120 { "trim", "trim" },
121 { "upper", "upper" },
122 { "make_datetime", "" },
123 { "make_date", "" },
124 { "make_time", "" },
125 };
126
127 return FN_NAMES.value( fnName, QString() );
128}
129
130QStringList QgsSQLiteExpressionCompiler::sqlArgumentsFromFunctionName( const QString &fnName, const QStringList &fnArgs ) const
131{
132 QStringList args( fnArgs );
133 if ( fnName == "make_datetime"_L1 )
134 {
135 args = QStringList( u"'%1-%2-%3T%4:%5:%6Z'"_s.arg( args[0].rightJustified( 4, '0' ) )
136 .arg( args[1].rightJustified( 2, '0' ) )
137 .arg( args[2].rightJustified( 2, '0' ) )
138 .arg( args[3].rightJustified( 2, '0' ) )
139 .arg( args[4].rightJustified( 2, '0' ) )
140 .arg( args[5].rightJustified( 2, '0' ) ) );
141 }
142 else if ( fnName == "make_date"_L1 )
143 {
144 args = QStringList( u"'%1-%2-%3'"_s.arg( args[0].rightJustified( 4, '0' ) )
145 .arg( args[1].rightJustified( 2, '0' ) )
146 .arg( args[2].rightJustified( 2, '0' ) ) );
147 }
148 else if ( fnName == "make_time"_L1 )
149 {
150 args = QStringList( u"'%1:%2:%3'"_s.arg( args[0].rightJustified( 2, '0' ) )
151 .arg( args[1].rightJustified( 2, '0' ) )
152 .arg( args[2].rightJustified( 2, '0' ) ) );
153 }
154 return args;
155}
156
157QString QgsSQLiteExpressionCompiler::castToReal( const QString &value ) const
158{
159 return u"CAST((%1) AS REAL)"_s.arg( value );
160}
161
162QString QgsSQLiteExpressionCompiler::castToInt( const QString &value ) const
163{
164 return u"CAST((%1) AS INTEGER)"_s.arg( value );
165}
166
167QString QgsSQLiteExpressionCompiler::castToText( const QString &value ) const
168{
169 return u"CAST((%1) AS TEXT)"_s.arg( value );
170}
171
An abstract base class for defining QgsExpression functions.
QString name() const
The name of the function.
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.
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.
QList< QgsExpressionNode * > list()
Gets a list of all the nodes.
Abstract base class for all nodes that can appear in an expression.
virtual QgsExpressionNode::NodeType nodeType() const =0
Gets the type of this node.
static const QList< QgsExpressionFunction * > & Functions()
Container of fields for a vector layer.
Definition qgsfields.h:46
Generic expression compiler for translation to provider specific SQL WHERE clauses.
virtual Result compileNode(const QgsExpressionNode *node, QString &str)
Compiles an expression node and returns the result of the compilation.
Result
Possible results from expression compilation.
static QString quotedIdentifier(const QString &identifier)
Returns a properly quoted version of identifier.
static QString quotedValue(const QVariant &value)
Returns a properly quoted and escaped version of value for use in SQL strings.