QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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
34QgsSqlExpressionCompiler::Result QgsSQLiteExpressionCompiler::compileNode( const QgsExpressionNode *node, QString &result )
35{
36 const QgsSqlExpressionCompiler::Result staticRes = replaceNodeByStaticCachedValueIfPossible( node, result );
37 if ( staticRes != Fail )
38 return staticRes;
39
40 switch ( node->nodeType() )
41 {
43 {
44 const QgsExpressionNodeBinaryOperator *op = static_cast<const QgsExpressionNodeBinaryOperator *>( node );
45 switch ( op->op() )
46 {
49 return Fail; //not supported by SQLite
50
53 {
54 QString opL, opR;
55
56 if ( compileNode( op->opLeft(), opL ) != Complete || compileNode( op->opRight(), opR ) != Complete )
57 return Fail;
58
59 result = u"lower(%1) %2 lower(%3) ESCAPE '\\'"_s.arg( opL ).arg( op->op() == QgsExpressionNodeBinaryOperator::boILike ? u"LIKE"_s : u"NOT LIKE"_s ).arg( opR );
60
61 return Complete;
62 }
63
64 default:
65 //fallback to default handling
66 return QgsSqlExpressionCompiler::compileNode( node, result );
67 }
68 }
69
71 {
72 const QgsExpressionNodeFunction *n = static_cast<const QgsExpressionNodeFunction *>( node );
74
75 if ( fd->name() == "make_datetime"_L1 || fd->name() == "make_date"_L1 || fd->name() == "make_time"_L1 )
76 {
77 const auto constList = n->args()->list();
78 for ( const QgsExpressionNode *ln : constList )
79 {
80 if ( ln->nodeType() != QgsExpressionNode::ntLiteral )
81 return Fail;
82 }
83 }
84
85 return QgsSqlExpressionCompiler::compileNode( node, result );
86 }
87
88 default:
89 break;
90 }
91
92 return QgsSqlExpressionCompiler::compileNode( node, result );
93}
94
95QString QgsSQLiteExpressionCompiler::quotedIdentifier( const QString &identifier )
96{
97 return QgsSqliteUtils::quotedIdentifier( identifier );
98}
99
100QString QgsSQLiteExpressionCompiler::quotedValue( const QVariant &value, bool &ok )
101{
102 ok = true;
103 return QgsSqliteUtils::quotedValue( value );
104}
105
106QString QgsSQLiteExpressionCompiler::sqlFunctionFromFunctionName( const QString &fnName ) const
107{
108 static const QMap<QString, QString> FN_NAMES {
109 { "abs", "abs" },
110 { "char", "char" },
111 { "coalesce", "coalesce" },
112 { "lower", "lower" },
113 { "round", "round" },
114 { "trim", "trim" },
115 { "upper", "upper" },
116 { "make_datetime", "" },
117 { "make_date", "" },
118 { "make_time", "" },
119 };
120
121 return FN_NAMES.value( fnName, QString() );
122}
123
124QStringList QgsSQLiteExpressionCompiler::sqlArgumentsFromFunctionName( const QString &fnName, const QStringList &fnArgs ) const
125{
126 QStringList args( fnArgs );
127 if ( fnName == "make_datetime"_L1 )
128 {
129 args = QStringList( u"'%1-%2-%3T%4:%5:%6Z'"_s.arg( args[0].rightJustified( 4, '0' ) )
130 .arg( args[1].rightJustified( 2, '0' ) )
131 .arg( args[2].rightJustified( 2, '0' ) )
132 .arg( args[3].rightJustified( 2, '0' ) )
133 .arg( args[4].rightJustified( 2, '0' ) )
134 .arg( args[5].rightJustified( 2, '0' ) ) );
135 }
136 else if ( fnName == "make_date"_L1 )
137 {
138 args = QStringList( u"'%1-%2-%3'"_s.arg( args[0].rightJustified( 4, '0' ) ).arg( args[1].rightJustified( 2, '0' ) ).arg( args[2].rightJustified( 2, '0' ) ) );
139 }
140 else if ( fnName == "make_time"_L1 )
141 {
142 args = QStringList( u"'%1:%2:%3'"_s.arg( args[0].rightJustified( 2, '0' ) ).arg( args[1].rightJustified( 2, '0' ) ).arg( args[2].rightJustified( 2, '0' ) ) );
143 }
144 return args;
145}
146
147QString QgsSQLiteExpressionCompiler::castToReal( const QString &value ) const
148{
149 return u"CAST((%1) AS REAL)"_s.arg( value );
150}
151
152QString QgsSQLiteExpressionCompiler::castToInt( const QString &value ) const
153{
154 return u"CAST((%1) AS INTEGER)"_s.arg( value );
155}
156
157QString QgsSQLiteExpressionCompiler::castToText( const QString &value ) const
158{
159 return u"CAST((%1) AS TEXT)"_s.arg( value );
160}
161
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.