Skip to content

Commit

Permalink
Merge pull request #44419 from mhugent/fix_geometry_postgres_expressi…
Browse files Browse the repository at this point in the history
…on_compiler

Fix geometry handling in postgres expression compiler
  • Loading branch information
mhugent committed Aug 1, 2021
2 parents 4ff9d9a + 6ebeb43 commit 0283cb6
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/providers/postgres/qgspostgresexpressioncompiler.cpp
Expand Up @@ -44,6 +44,14 @@ QString QgsPostgresExpressionCompiler::quotedValue( const QVariant &value, bool
case QVariant::Double:
return value.toString();

case QVariant::UserType:
if ( value.canConvert<QgsGeometry>() )
{
const QgsGeometry geom = value.value<QgsGeometry>();
return QString( "ST_GeomFromText('%1',%2)" ).arg( geom.asWkt() ).arg( mRequestedSrid.isEmpty() ? mDetectedSrid : mRequestedSrid );
}
break;

default:
break;
}
Expand Down
1 change: 1 addition & 0 deletions tests/src/providers/CMakeLists.txt
Expand Up @@ -34,6 +34,7 @@ add_qgis_test(testqgswmscapabilities.cpp MODULE provider LINKEDLIBRARIES provide
add_qgis_test(testqgswmsccapabilities.cpp MODULE provider LINKEDLIBRARIES provider_wms_a qgis_core)
add_qgis_test(testqgswmsprovider.cpp MODULE provider LINKEDLIBRARIES provider_wms_a qgis_core)

add_qgis_test(testqgspostgresexpressioncompiler.cpp MODULE provider LINKEDLIBRARIES provider_postgres_a qgis_core)
add_qgis_test(testqgspostgresprovider.cpp MODULE provider LINKEDLIBRARIES provider_postgres_a qgis_core)
add_qgis_test(testqgspostgresconn.cpp MODULE provider LINKEDLIBRARIES provider_postgres_a qgis_core LABELS "POSTGRES")

Expand Down
62 changes: 62 additions & 0 deletions tests/src/providers/testqgspostgresexpressioncompiler.cpp
@@ -0,0 +1,62 @@
/***************************************************************************
testqgspostgresexpressioncompiler.cpp
---------------------
begin : 23.07.2021
copyright : (C) 2021 by Marco Hugentobler
email : marco at sourcepole dot 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 "qgstest.h"
#include "qgspostgresexpressioncompiler.h"
#include "qgspostgresfeatureiterator.h"
#include "qgspostgresprovider.h"

//The only purpose of this class is to set geomColumn and srid
class QgsTestPostgresExpressionCompiler: public QgsPostgresExpressionCompiler
{
public:
QgsTestPostgresExpressionCompiler( QgsPostgresFeatureSource *source, const QString &srid, const QString &geometryColumn ): QgsPostgresExpressionCompiler( source )
{
mDetectedSrid = srid;
mGeometryColumn = geometryColumn;
}
};

class TestQgsPostgresExpressionCompiler: public QObject
{
Q_OBJECT

public:

TestQgsPostgresExpressionCompiler() = default;

private slots:
void testGeometryFromWkt();
};

void TestQgsPostgresExpressionCompiler::testGeometryFromWkt()
{
QgsPostgresProvider p( QStringLiteral( "" ), QgsDataProvider::ProviderOptions() );
QgsPostgresFeatureSource featureSource( &p );
QgsTestPostgresExpressionCompiler compiler( &featureSource, QStringLiteral( "4326" ), QStringLiteral( "geom" ) );
QgsExpression exp( QStringLiteral( "intersects($geometry,geom_from_wkt('Polygon((0 0, 1 0, 1 1, 0 1, 0 0))'))" ) );
QgsExpressionContext expContext;
exp.prepare( &expContext );
QgsSqlExpressionCompiler::Result r = compiler.compile( &exp );
QCOMPARE( r, QgsSqlExpressionCompiler::Complete );
QString sql = compiler.result();
QCOMPARE( sql, QStringLiteral( "ST_Intersects(\"geom\",ST_GeomFromText('Polygon ((0 0, 1 0, 1 1, 0 1, 0 0))',4326))" ) );
}

QGSTEST_MAIN( TestQgsPostgresExpressionCompiler )

#include "testqgspostgresexpressioncompiler.moc"

0 comments on commit 0283cb6

Please sign in to comment.