Skip to content
Permalink
Browse files

[virtual] disable FilterRect when no unique id is present. Should fix #…

  • Loading branch information
Hugo Mercier
Hugo Mercier committed Nov 22, 2016
1 parent 8dbe6ea commit 289423617f035108ecaa11a812546681b7a773af
Showing with 32 additions and 28 deletions.
  1. +31 −27 src/providers/virtual/qgsvirtuallayerfeatureiterator.cpp
  2. +1 −1 tests/src/python/test_provider_virtual.py
@@ -43,38 +43,42 @@ QgsVirtualLayerFeatureIterator::QgsVirtualLayerFeatureIterator( QgsVirtualLayerF
wheres << subset;
}

if ( mDefinition.hasDefinedGeometry() && !request.filterRect().isNull() )
if ( !mDefinition.uid().isNull() )
{
bool do_exact = request.flags() & QgsFeatureRequest::ExactIntersect;
QgsRectangle rect( request.filterRect() );
QString mbr = QStringLiteral( "%1,%2,%3,%4" ).arg( rect.xMinimum() ).arg( rect.yMinimum() ).arg( rect.xMaximum() ).arg( rect.yMaximum() );
wheres << quotedColumn( mDefinition.geometryField() ) + " is not null";
wheres << QStringLiteral( "%1Intersects(%2,BuildMbr(%3))" )
.arg( do_exact ? "" : "Mbr",
quotedColumn( mDefinition.geometryField() ),
mbr );
}
else if ( !mDefinition.uid().isNull() && request.filterType() == QgsFeatureRequest::FilterFid )
{
wheres << QStringLiteral( "%1=%2" )
.arg( quotedColumn( mDefinition.uid() ) )
.arg( request.filterFid() );
}
else if ( !mDefinition.uid().isNull() && request.filterType() == QgsFeatureRequest::FilterFids )
{
QString values = quotedColumn( mDefinition.uid() ) + " IN (";
bool first = true;
Q_FOREACH ( QgsFeatureId v, request.filterFids() )
// filters are only available when a column with unique id exists
if ( mDefinition.hasDefinedGeometry() && !request.filterRect().isNull() )
{
bool do_exact = request.flags() & QgsFeatureRequest::ExactIntersect;
QgsRectangle rect( request.filterRect() );
QString mbr = QStringLiteral( "%1,%2,%3,%4" ).arg( rect.xMinimum() ).arg( rect.yMinimum() ).arg( rect.xMaximum() ).arg( rect.yMaximum() );
wheres << quotedColumn( mDefinition.geometryField() ) + " is not null";
wheres << QStringLiteral( "%1Intersects(%2,BuildMbr(%3))" )
.arg( do_exact ? "" : "Mbr",
quotedColumn( mDefinition.geometryField() ),
mbr );
}
else if ( request.filterType() == QgsFeatureRequest::FilterFid )
{
wheres << QStringLiteral( "%1=%2" )
.arg( quotedColumn( mDefinition.uid() ) )
.arg( request.filterFid() );
}
else if ( request.filterType() == QgsFeatureRequest::FilterFids )
{
if ( !first )
QString values = quotedColumn( mDefinition.uid() ) + " IN (";
bool first = true;
Q_FOREACH ( QgsFeatureId v, request.filterFids() )
{
values += QLatin1String( "," );
if ( !first )
{
values += QLatin1String( "," );
}
first = false;
values += QString::number( v );
}
first = false;
values += QString::number( v );
values += QLatin1String( ")" );
wheres << values;
}
values += QLatin1String( ")" );
wheres << values;
}

mFields = mSource->provider()->fields();
@@ -446,7 +446,7 @@ def test_sql2(self):
QgsMapLayerRegistry.instance().addMapLayer(l2)

query = toPercent("SELECT * FROM france_parts")
l4 = QgsVectorLayer("?query=%s" % query, "tt", "virtual")
l4 = QgsVectorLayer("?query=%s&uid=ObjectId" % query, "tt", "virtual")
self.assertEqual(l4.isValid(), True)

self.assertEqual(l4.dataProvider().wkbType(), 3)

0 comments on commit 2894236

Please sign in to comment.
You can’t perform that action at this time.