/
qgspostgresexpressioncompiler.cpp
207 lines (193 loc) · 6.64 KB
/
qgspostgresexpressioncompiler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/***************************************************************************
qgspostgresexpressioncompiler.cpp
----------------------------------------------------
date : 22.4.2015
copyright : (C) 2015 by Matthias Kuhn
email : matthias (at) opengis.ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgspostgresexpressioncompiler.h"
#include "qgssqlexpressioncompiler.h"
#include "qgsexpressionnodeimpl.h"
QgsPostgresExpressionCompiler::QgsPostgresExpressionCompiler( QgsPostgresFeatureSource *source )
: QgsSqlExpressionCompiler( source->mFields, QgsSqlExpressionCompiler::IntegerDivisionResultsInInteger )
, mGeometryColumn( source->mGeometryColumn )
, mSpatialColType( source->mSpatialColType )
, mDetectedGeomType( source->mDetectedGeomType )
, mRequestedGeomType( source->mRequestedGeomType )
, mRequestedSrid( source->mRequestedSrid )
, mDetectedSrid( source->mDetectedSrid )
{
}
QString QgsPostgresExpressionCompiler::quotedIdentifier( const QString &identifier )
{
return QgsPostgresConn::quotedIdentifier( identifier );
}
QString QgsPostgresExpressionCompiler::quotedValue( const QVariant &value, bool &ok )
{
ok = true;
return QgsPostgresConn::quotedValue( value );
}
static const QMap<QString, QString> FUNCTION_NAMES_SQL_FUNCTIONS_MAP
{
{ "sqrt", "sqrt" },
{ "radians", "radians" },
{ "degrees", "degrees" },
{ "abs", "abs" },
{ "cos", "cos" },
{ "sin", "sin" },
{ "tan", "tan" },
{ "acos", "acos" },
{ "asin", "asin" },
{ "atan", "atan" },
{ "atan2", "atan2" },
{ "exp", "exp" },
{ "ln", "ln" },
{ "log", "log" },
{ "log10", "log" },
{ "round", "round" },
{ "floor", "floor" },
{ "ceil", "ceil" },
{ "pi", "pi" },
// geometry functions
//{ "azimuth", "ST_Azimuth" },
{ "x", "ST_X" },
{ "y", "ST_Y" },
//{ "z", "ST_Z" },
//{ "m", "ST_M" },
{ "x_min", "ST_XMin" },
{ "y_min", "ST_YMin" },
{ "x_max", "ST_XMax" },
{ "y_max", "ST_YMax" },
{ "area", "ST_Area" },
{ "perimeter", "ST_Perimeter" },
{ "relate", "ST_Relate" },
{ "disjoint", "ST_Disjoint" },
{ "intersects", "ST_Intersects" },
//{ "touches", "ST_Touches" },
{ "crosses", "ST_Crosses" },
{ "contains", "ST_Contains" },
{ "overlaps", "ST_Overlaps" },
{ "within", "ST_Within" },
{ "translate", "ST_Translate" },
{ "buffer", "ST_Buffer" },
{ "centroid", "ST_Centroid" },
{ "point_on_surface", "ST_PointOnSurface" },
#if 0
{ "reverse", "ST_Reverse" },
{ "is_closed", "ST_IsClosed" },
{ "convex_hull", "ST_ConvexHull" },
{ "difference", "ST_Difference" },
#endif
{ "distance", "ST_Distance" },
#if 0
{ "intersection", "ST_Intersection" },
{ "sym_difference", "ST_SymDifference" },
{ "combine", "ST_Union" },
{ "union", "ST_Union" },
#endif
{ "geom_from_wkt", "ST_GeomFromText" },
{ "geom_from_gml", "ST_GeomFromGML" },
{ "char", "chr" },
{ "coalesce", "coalesce" },
{ "lower", "lower" },
{ "trim", "trim" },
{ "upper", "upper" },
};
QString QgsPostgresExpressionCompiler::sqlFunctionFromFunctionName( const QString &fnName ) const
{
return FUNCTION_NAMES_SQL_FUNCTIONS_MAP.value( fnName, QString() );
}
QStringList QgsPostgresExpressionCompiler::sqlArgumentsFromFunctionName( const QString &fnName, const QStringList &fnArgs ) const
{
QStringList args( fnArgs );
if ( fnName == QLatin1String( "geom_from_wkt" ) )
{
args << ( mRequestedSrid.isEmpty() ? mDetectedSrid : mRequestedSrid );
}
else if ( fnName == QLatin1String( "geom_from_gml" ) )
{
args << ( mRequestedSrid.isEmpty() ? mDetectedSrid : mRequestedSrid );
}
else if ( fnName == QLatin1String( "x" ) || fnName == QLatin1String( "y" ) )
{
args = QStringList( QStringLiteral( "ST_Centroid(%1)" ).arg( args[0] ) );
}
else if ( fnName == QLatin1String( "buffer" ) && args.length() == 2 )
{
args << QStringLiteral( "8" );
}
// x and y functions have to be adapted
return args;
}
QString QgsPostgresExpressionCompiler::castToReal( const QString &value ) const
{
return QStringLiteral( "((%1)::real)" ).arg( value );
}
QString QgsPostgresExpressionCompiler::castToInt( const QString &value ) const
{
return QStringLiteral( "((%1)::int)" ).arg( value );
}
QString QgsPostgresExpressionCompiler::castToText( const QString &value ) const
{
return QStringLiteral( "((%1)::text)" ).arg( value );
}
QgsSqlExpressionCompiler::Result QgsPostgresExpressionCompiler::compileNode( const QgsExpressionNode *node, QString &result )
{
switch ( node->nodeType() )
{
case QgsExpressionNode::ntFunction:
{
const QgsExpressionNodeFunction *n = static_cast<const QgsExpressionNodeFunction *>( node );
QgsExpressionFunction *fd = QgsExpression::Functions()[n->fnIndex()];
if ( fd->name() == QLatin1String( "$geometry" ) )
{
result = quotedIdentifier( mGeometryColumn );
return Complete;
}
#if 0
/*
* These methods are tricky
* QGIS expression versions of these return ellipsoidal measurements
* based on the project settings, and also convert the result to the
* units specified in project properties.
*/
else if ( fd->name() == "$area" )
{
result = QStringLiteral( "ST_Area(%1)" ).arg( quotedIdentifier( mGeometryColumn ) );
return Complete;
}
else if ( fd->name() == "$length" )
{
result = QStringLiteral( "ST_Length(%1)" ).arg( quotedIdentifier( mGeometryColumn ) );
return Complete;
}
else if ( fd->name() == "$perimeter" )
{
result = QStringLiteral( "ST_Perimeter(%1)" ).arg( quotedIdentifier( mGeometryColumn ) );
return Complete;
}
else if ( fd->name() == "$x" )
{
result = QStringLiteral( "ST_X(%1)" ).arg( quotedIdentifier( mGeometryColumn ) );
return Complete;
}
else if ( fd->name() == "$y" )
{
result = QStringLiteral( "ST_Y(%1)" ).arg( quotedIdentifier( mGeometryColumn ) );
return Complete;
}
#endif
FALLTHROUGH;
}
default:
break;
}
return QgsSqlExpressionCompiler::compileNode( node, result );
}