Skip to content
Permalink
Browse files

Merge pull request #5695 from pblottiere/bugfix_virtuallayer_218

[backport][bugfix] Fixes #16798 FilterFid feature requests with virtual layers
  • Loading branch information
pblottiere committed Nov 27, 2017
2 parents 193ead7 + 4693f1a commit 485cd80a385896e30049380767d062254bebd696
@@ -37,6 +37,7 @@ QgsVirtualLayerFeatureIterator::QgsVirtualLayerFeatureIterator( QgsVirtualLayerF
QString tableName = mSource->provider()->mTableName;

QStringList wheres;
QString offset;
QString subset = mSource->provider()->mSubset;
if ( !subset.isNull() )
{
@@ -76,6 +77,17 @@ QgsVirtualLayerFeatureIterator::QgsVirtualLayerFeatureIterator( QgsVirtualLayerF
values += ")";
wheres << values;
}
else if ( mDefinition.uid().isNull() && request.filterType() == QgsFeatureRequest::FilterFid )
{
if ( request.filterFid() >= 0 )
{
offset = QString( " LIMIT 1 OFFSET %1" ).arg( request.filterFid() );
}
else // never return a feature if the id is negative
{
offset = QString( " LIMIT 0" );
}
}

mFields = mSource->provider()->fields();
if ( request.flags() & QgsFeatureRequest::SubsetOfAttributes )
@@ -111,7 +123,15 @@ QgsVirtualLayerFeatureIterator::QgsVirtualLayerFeatureIterator( QgsVirtualLayerF
}
else
{
columns = "0";
columns = QString( "0" );
if ( request.filterType() == QgsFeatureRequest::FilterFid )
{
columns = QString::number( request.filterFid() );
}
else
{
columns = QString( "0" );
}
}
Q_FOREACH ( int i, mAttributes )
{
@@ -134,6 +154,11 @@ QgsVirtualLayerFeatureIterator::QgsVirtualLayerFeatureIterator( QgsVirtualLayerF
mSqlQuery += " WHERE " + wheres.join( " AND " );
}

if ( !offset.isEmpty() )
{
mSqlQuery += offset;
}

mQuery.reset( new Sqlite::Query( mSqlite, mSqlQuery ) );

mFid = 0;
@@ -191,7 +216,8 @@ bool QgsVirtualLayerFeatureIterator::fetchFeature( QgsFeature& feature )

feature.setFields( mFields, /* init */ true );

if ( mDefinition.uid().isNull() )
if ( mDefinition.uid().isNull() &&
mRequest.filterType() != QgsFeatureRequest::FilterFid )
{
// no id column => autoincrement
feature.setFeatureId( mFid++ );
@@ -86,6 +86,46 @@ def tearDown(self):
"""Run after each test."""
pass

def test_filterfid_crossjoin(self):
l0 = QgsVectorLayer(os.path.join(self.testDataDir, "france_parts.shp"), "france_parts", "ogr")
self.assertTrue(l0.isValid())
QgsMapLayerRegistry.instance().addMapLayer(l0)

l1 = QgsVectorLayer(os.path.join(self.testDataDir, "points.shp"), "points", "ogr")
self.assertTrue(l1.isValid())
QgsMapLayerRegistry.instance().addMapLayer(l1)

# cross join
query = QUrl.toPercentEncoding("SELECT * FROM france_parts,points")
vl = QgsVectorLayer("?query=%s" % query, "tt", "virtual")

self.assertEqual(vl.featureCount(), l0.featureCount() * l1.featureCount())

# test with FilterFid requests
f = next(vl.getFeatures(QgsFeatureRequest().setFilterFid(0)))
idx = f.fields().fieldNameIndex('Class')
self.assertEqual(f.id(), 0)
self.assertEqual(f.attributes()[idx], 'Jet')

f = next(vl.getFeatures(QgsFeatureRequest().setFilterFid(5)))
self.assertEqual(f.id(), 5)
self.assertEqual(f.attributes()[idx], 'Biplane')

# test with FilterFid requests
fit = vl.getFeatures(QgsFeatureRequest().setFilterFids([0, 3, 5]))

f = next(fit)
self.assertEqual(f.id(), 0)
self.assertEqual(f.attributes()[idx], 'Jet')

f = next(fit)
self.assertEqual(f.id(), 3)
self.assertEqual(f.attributes()[idx], 'Jet')

f = next(fit)
self.assertEqual(f.id(), 5)
self.assertEqual(f.attributes()[idx], 'Biplane')

def test_CsvNoGeometry(self):
l1 = QgsVectorLayer(QUrl.fromLocalFile(os.path.join(self.testDataDir, "delimitedtext/test.csv")).toString() + "?type=csv&geomType=none&subsetIndex=no&watchFile=no", "test", "delimitedtext", False)
self.assertEqual(l1.isValid(), True)

0 comments on commit 485cd80

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