QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 
21 QgsSQLiteExpressionCompiler::QgsSQLiteExpressionCompiler( const QgsFields& fields )
22  : QgsSqlExpressionCompiler( fields, QgsSqlExpressionCompiler::LikeIsCaseInsensitive )
23 {
24 }
25 
26 QgsSqlExpressionCompiler::Result QgsSQLiteExpressionCompiler::compileNode( const QgsExpression::Node* node, QString& result )
27 {
28  switch ( node->nodeType() )
29  {
31  {
32  switch ( static_cast<const QgsExpression::NodeBinaryOperator*>( node )->op() )
33  {
36  return Fail; //not supported by SQLite
37 
38  default:
39  //fallback to default handling
40  return QgsSqlExpressionCompiler::compileNode( node, result );
41  }
42  }
43 
44  default:
45  break;
46  }
47 
48  return QgsSqlExpressionCompiler::compileNode( node, result );
49 }
50 
51 QString QgsSQLiteExpressionCompiler::quotedIdentifier( const QString& identifier )
52 {
53  QString id( identifier );
54  id.replace( '\"', "\"\"" );
55  return id.prepend( '\"' ).append( '\"' );
56 }
57 
58 QString QgsSQLiteExpressionCompiler::quotedValue( const QVariant& value, bool& ok )
59 {
60  ok = true;
61 
62  if ( value.isNull() )
63  return "NULL";
64 
65  switch ( value.type() )
66  {
67  case QVariant::Int:
68  case QVariant::LongLong:
69  case QVariant::Double:
70  return value.toString();
71 
72  case QVariant::Bool:
73  //SQLite has no boolean literals
74  return value.toBool() ? "1" : "0";
75 
76  default:
77  case QVariant::String:
78  QString v = value.toString();
79  // https://www.sqlite.org/lang_expr.html :
80  // """A string constant is formed by enclosing the string in single quotes (').
81  // A single quote within the string can be encoded by putting two single quotes
82  // in a row - as in Pascal. C-style escapes using the backslash character are not supported because they are not standard SQL. """
83  return v.replace( '\'', "''" ).prepend( '\'' ).append( '\'' );
84  }
85 }
86 
virtual NodeType nodeType() const =0
Abstract virtual that returns the type of this node.
QString & append(QChar ch)
QString & prepend(QChar ch)
Container of fields for a vector layer.
Definition: qgsfield.h:252
bool isNull() const
virtual QString result()
Returns the compiled expression string for use by the provider.
Generic expression compiler for translation to provider specific SQL WHERE clauses.
QString & replace(int position, int n, QChar after)
Result
Possible results from expression compilation.
bool toBool() const
virtual Result compileNode(const QgsExpression::Node *node, QString &str)
Compiles an expression node and returns the result of the compilation.
Type type() const
QString toString() const