File tree Expand file tree Collapse file tree 6 files changed +66
-0
lines changed Expand file tree Collapse file tree 6 files changed +66
-0
lines changed Original file line number Diff line number Diff line change @@ -38,6 +38,18 @@ QString QgsSqlExpressionCompiler::result()
38
38
return mResult ;
39
39
}
40
40
41
+ bool QgsSqlExpressionCompiler::opIsStringComparison ( QgsExpressionNodeBinaryOperator::BinaryOperator op )
42
+ {
43
+ if ( op == QgsExpressionNodeBinaryOperator::BinaryOperator::boILike ||
44
+ op == QgsExpressionNodeBinaryOperator::BinaryOperator::boLike ||
45
+ op == QgsExpressionNodeBinaryOperator::BinaryOperator::boNotILike ||
46
+ op == QgsExpressionNodeBinaryOperator::BinaryOperator::boNotLike ||
47
+ op == QgsExpressionNodeBinaryOperator::BinaryOperator::boRegexp )
48
+ return true ;
49
+ else
50
+ return false ;
51
+ }
52
+
41
53
QString QgsSqlExpressionCompiler::quotedIdentifier ( const QString &identifier )
42
54
{
43
55
QString quoted = identifier;
@@ -253,6 +265,9 @@ QgsSqlExpressionCompiler::Result QgsSqlExpressionCompiler::compileNode( const Qg
253
265
QString left;
254
266
Result lr ( compileNode ( n->opLeft (), left ) );
255
267
268
+ if ( opIsStringComparison ( n ->op () ) )
269
+ left = castToText ( left );
270
+
256
271
QString right;
257
272
Result rr ( compileNode ( n->opRight (), right ) );
258
273
@@ -409,6 +424,11 @@ QString QgsSqlExpressionCompiler::castToReal( const QString &value ) const
409
424
return QString ();
410
425
}
411
426
427
+ QString QgsSqlExpressionCompiler::castToText ( const QString &value ) const
428
+ {
429
+ return value;
430
+ }
431
+
412
432
QString QgsSqlExpressionCompiler::castToInt ( const QString &value ) const
413
433
{
414
434
Q_UNUSED ( value );
Original file line number Diff line number Diff line change 20
20
21
21
#include " qgis_core.h"
22
22
#include " qgsfields.h"
23
+ #include " qgsexpressionnodeimpl.h"
23
24
24
25
class QgsExpression ;
25
26
class QgsExpressionNode ;
@@ -80,6 +81,22 @@ class CORE_EXPORT QgsSqlExpressionCompiler
80
81
*/
81
82
virtual QString result ();
82
83
84
+ /* *
85
+ * Returns true if \a op is one of
86
+ *
87
+ * - LIKE
88
+ * - ILIKE
89
+ * - NOT LIKE
90
+ * - NOT ILIKE
91
+ * - ~ (regexp)
92
+ *
93
+ * In such cases the left operator will be cast to string to behave equal to
94
+ * QGIS own expression engine.
95
+ *
96
+ * \since QGIS 3.2
97
+ */
98
+ bool opIsStringComparison ( QgsExpressionNodeBinaryOperator::BinaryOperator op );
99
+
83
100
protected:
84
101
85
102
/* *
@@ -132,6 +149,23 @@ class CORE_EXPORT QgsSqlExpressionCompiler
132
149
*/
133
150
virtual QString castToReal ( const QString &value ) const ;
134
151
152
+ /* *
153
+ * Casts a value to a text result. Subclasses that support casting to text may implement this function
154
+ * to get equal behavior to the QGIS expression engine when string comparison operators are applied
155
+ * on non-string data.
156
+ *
157
+ * Example:
158
+ *
159
+ * 579 LIKE '5%'
160
+ *
161
+ * which on a postgres database needs to be
162
+ *
163
+ * 579::text LIKE '5%'
164
+ *
165
+ * \since QGIS 3.2
166
+ */
167
+ virtual QString castToText ( const QString &value ) const ;
168
+
135
169
/* *
136
170
* Casts a value to a integer result. Subclasses must reimplement this to cast a numeric value to a integer
137
171
* type value so that integer division results in a integer value result instead of real.
Original file line number Diff line number Diff line change @@ -111,4 +111,9 @@ QString QgsSQLiteExpressionCompiler::castToInt( const QString &value ) const
111
111
return QStringLiteral ( " CAST((%1) AS INTEGER)" ).arg ( value );
112
112
}
113
113
114
+ QString QgsSQLiteExpressionCompiler::castToText ( const QString &value ) const
115
+ {
116
+ return QStringLiteral ( " CAST((%1) AS TEXT)" ).arg ( value );
117
+ }
118
+
114
119
// /@endcond
Original file line number Diff line number Diff line change @@ -52,6 +52,7 @@ class CORE_EXPORT QgsSQLiteExpressionCompiler : public QgsSqlExpressionCompiler
52
52
QString sqlFunctionFromFunctionName ( const QString &fnName ) const override ;
53
53
QString castToReal ( const QString &value ) const override ;
54
54
QString castToInt ( const QString &value ) const override ;
55
+ QString castToText ( const QString &value ) const override ;
55
56
56
57
};
57
58
Original file line number Diff line number Diff line change @@ -144,6 +144,11 @@ QString QgsPostgresExpressionCompiler::castToInt( const QString &value ) const
144
144
return QStringLiteral ( " ((%1)::int)" ).arg ( value );
145
145
}
146
146
147
+ QString QgsPostgresExpressionCompiler::castToText ( const QString &value ) const
148
+ {
149
+ return QStringLiteral ( " ((%1)::text)" ).arg ( value );
150
+ }
151
+
147
152
QgsSqlExpressionCompiler::Result QgsPostgresExpressionCompiler::compileNode ( const QgsExpressionNode *node, QString &result )
148
153
{
149
154
switch ( node->nodeType () )
Original file line number Diff line number Diff line change @@ -36,6 +36,7 @@ class QgsPostgresExpressionCompiler : public QgsSqlExpressionCompiler
36
36
QStringList sqlArgumentsFromFunctionName ( const QString &fnName, const QStringList &fnArgs ) const override ;
37
37
QString castToReal ( const QString &value ) const override ;
38
38
QString castToInt ( const QString &value ) const override ;
39
+ QString castToText ( const QString &value ) const override ;
39
40
40
41
QString mGeometryColumn ;
41
42
QgsPostgresGeometryColumnType mSpatialColType ;
You can’t perform that action at this time.
0 commit comments