QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 
20 #include "qgsexpressionnodeimpl.h"
21 #include "qgssqliteutils.h"
22 
23 QgsSQLiteExpressionCompiler::QgsSQLiteExpressionCompiler( const QgsFields &fields )
24  : QgsSqlExpressionCompiler( fields, QgsSqlExpressionCompiler::LikeIsCaseInsensitive | QgsSqlExpressionCompiler::IntegerDivisionResultsInInteger )
25 {
26 }
27 
28 QgsSqlExpressionCompiler::Result QgsSQLiteExpressionCompiler::compileNode( const QgsExpressionNode *node, QString &result )
29 {
30  switch ( node->nodeType() )
31  {
33  {
34  switch ( static_cast<const QgsExpressionNodeBinaryOperator *>( node )->op() )
35  {
38  return Fail; //not supported by SQLite
39 
40  default:
41  //fallback to default handling
42  return QgsSqlExpressionCompiler::compileNode( node, result );
43  }
44  }
45 
46  default:
47  break;
48  }
49 
50  return QgsSqlExpressionCompiler::compileNode( node, result );
51 }
52 
53 QString QgsSQLiteExpressionCompiler::quotedIdentifier( const QString &identifier )
54 {
55  return QgsSqliteUtils::quotedIdentifier( identifier );
56 }
57 
58 QString QgsSQLiteExpressionCompiler::quotedValue( const QVariant &value, bool &ok )
59 {
60  ok = true;
61  return QgsSqliteUtils::quotedValue( value );
62 }
63 
64 QString QgsSQLiteExpressionCompiler::sqlFunctionFromFunctionName( const QString &fnName ) const
65 {
66  static const QMap<QString, QString> FN_NAMES
67  {
68  { "abs", "abs" },
69  { "char", "char" },
70  { "coalesce", "coalesce" },
71  { "lower", "lower" },
72  { "round", "round" },
73  { "trim", "trim" },
74  { "upper", "upper" },
75  };
76 
77  return FN_NAMES.value( fnName, QString() );
78 }
79 
80 QString QgsSQLiteExpressionCompiler::castToReal( const QString &value ) const
81 {
82  return QStringLiteral( "CAST((%1) AS REAL)" ).arg( value );
83 }
84 
85 QString QgsSQLiteExpressionCompiler::castToInt( const QString &value ) const
86 {
87  return QStringLiteral( "CAST((%1) AS INTEGER)" ).arg( value );
88 }
89 
90 QString QgsSQLiteExpressionCompiler::castToText( const QString &value ) const
91 {
92  return QStringLiteral( "CAST((%1) AS TEXT)" ).arg( value );
93 }
94 
static QString quotedValue(const QVariant &value)
Returns a properly quoted and escaped version of value for use in SQL strings.
static QString quotedIdentifier(const QString &identifier)
Returns a properly quoted version of identifier.
Container of fields for a vector layer.
Definition: qgsfields.h:42
Provider cannot handle expression.
Abstract base class for all nodes that can appear in an expression.
Generic expression compiler for translation to provider specific SQL WHERE clauses.
Result
Possible results from expression compilation.
virtual Result compileNode(const QgsExpressionNode *node, QString &str)
Compiles an expression node and returns the result of the compilation.
virtual QString result()
Returns the compiled expression string for use by the provider.
virtual QgsExpressionNode::NodeType nodeType() const =0
Gets the type of this node.