QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 
22 QgsSQLiteExpressionCompiler::QgsSQLiteExpressionCompiler( const QgsFields &fields )
23  : QgsSqlExpressionCompiler( fields, QgsSqlExpressionCompiler::LikeIsCaseInsensitive | QgsSqlExpressionCompiler::IntegerDivisionResultsInInteger )
24 {
25 }
26 
27 QgsSqlExpressionCompiler::Result QgsSQLiteExpressionCompiler::compileNode( const QgsExpressionNode *node, QString &result )
28 {
29  switch ( node->nodeType() )
30  {
32  {
33  switch ( static_cast<const QgsExpressionNodeBinaryOperator *>( node )->op() )
34  {
37  return Fail; //not supported by SQLite
38 
39  default:
40  //fallback to default handling
41  return QgsSqlExpressionCompiler::compileNode( node, result );
42  }
43  }
44 
45  default:
46  break;
47  }
48 
49  return QgsSqlExpressionCompiler::compileNode( node, result );
50 }
51 
52 QString QgsSQLiteExpressionCompiler::quotedIdentifier( const QString &identifier )
53 {
54  QString id( identifier );
55  id.replace( '\"', QLatin1String( "\"\"" ) );
56  return id.prepend( '\"' ).append( '\"' );
57 }
58 
59 QString QgsSQLiteExpressionCompiler::quotedValue( const QVariant &value, bool &ok )
60 {
61  ok = true;
62 
63  if ( value.isNull() )
64  return QStringLiteral( "NULL" );
65 
66  switch ( value.type() )
67  {
68  case QVariant::Int:
69  case QVariant::LongLong:
70  case QVariant::Double:
71  return value.toString();
72 
73  case QVariant::Bool:
74  //SQLite has no boolean literals
75  return value.toBool() ? "1" : "0";
76 
77  default:
78  case QVariant::String:
79  QString v = value.toString();
80  // https://www.sqlite.org/lang_expr.html :
81  // """A string constant is formed by enclosing the string in single quotes (').
82  // A single quote within the string can be encoded by putting two single quotes
83  // in a row - as in Pascal. C-style escapes using the backslash character are not supported because they are not standard SQL. """
84  return v.replace( '\'', QLatin1String( "''" ) ).prepend( '\'' ).append( '\'' );
85  }
86 }
87 
88 QString QgsSQLiteExpressionCompiler::sqlFunctionFromFunctionName( const QString &fnName ) const
89 {
90  static const QMap<QString, QString> FN_NAMES
91  {
92  { "abs", "abs" },
93  { "char", "char" },
94  { "coalesce", "coalesce" },
95  { "lower", "lower" },
96  { "round", "round" },
97  { "trim", "trim" },
98  { "upper", "upper" },
99  };
100 
101  return FN_NAMES.value( fnName, QString() );
102 }
103 
104 QString QgsSQLiteExpressionCompiler::castToReal( const QString &value ) const
105 {
106  return QStringLiteral( "CAST((%1) AS REAL)" ).arg( value );
107 }
108 
109 QString QgsSQLiteExpressionCompiler::castToInt( const QString &value ) const
110 {
111  return QStringLiteral( "CAST((%1) AS INTEGER)" ).arg( value );
112 }
113 
114 QString QgsSQLiteExpressionCompiler::castToText( const QString &value ) const
115 {
116  return QStringLiteral( "CAST((%1) AS TEXT)" ).arg( value );
117 }
118 
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.