Skip to content

Commit

Permalink
[OGR] Ensure subset string is set when reopening dataset
Browse files Browse the repository at this point in the history
QgsOgrProvider::reloadData calls close() and open(), which in turn called setSubsetString with mSubsetString.
Since setSubsetString does nothing if the passed sql string is equal to mSubsetString, this resulted in the
substring not being set on re-open. This commit clears mSubsetString before calling setSubsetString, and
blocks signals when calling setSubsetString to avoid an endless recursion of emit dataChanged -> reload.
Fixes #17122.
  • Loading branch information
manisandro committed Sep 12, 2017
1 parent b035704 commit 54653e4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/providers/ogr/qgsogrprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3574,7 +3574,13 @@ void QgsOgrProvider::open( OpenMode mode )
// check that the initial encoding setting is fit for this layer
setEncoding( encoding() );

mValid = setSubsetString( mSubsetString );
// Ensure subset is set (setSubsetString does nothing if the passed sql subset string is equal to mSubsetString, which is the case when reloading the dataset)
QString origSubsetString = mSubsetString;
mSubsetString = "";
// Block signals to avoid endless recusion reloadData -> emit dataChanged -> reloadData
blockSignals( true );
mValid = setSubsetString( origSubsetString );
blockSignals( false );
if ( mValid )
{
if ( mode == OpenModeInitial )
Expand Down
10 changes: 9 additions & 1 deletion tests/src/python/test_provider_ogr_sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ def testFidSupport(self):
self.assertEqual(got, [(12, 123)])

def testSubsetStringFids(self):
""" tests that feature ids are stable even if a subset string is set """
"""
- tests that feature ids are stable even if a subset string is set
- tests that the subset string is correctly set on the ogr layer event when reloading the data source (issue #17122)
"""

tmpfile = os.path.join(self.basetestpath, 'subsetStringFids.sqlite')
ds = ogr.GetDriverByName('SQLite').CreateDataSource(tmpfile)
Expand Down Expand Up @@ -172,6 +175,7 @@ def testSubsetStringFids(self):

vl = QgsVectorLayer(tmpfile + "|subset=type=2", 'test', 'ogr')
self.assertTrue(vl.isValid())
self.assertTrue(vl.fields().at(0).name() == "orig_ogc_fid")

req = QgsFeatureRequest()
req.setFilterExpression("value=16")
Expand All @@ -180,6 +184,10 @@ def testSubsetStringFids(self):
self.assertTrue(it.nextFeature(f))
self.assertTrue(f.id() == 5)

# Check that subset string is correctly set on reload
vl.reload()
self.assertTrue(vl.fields().at(0).name() == "orig_ogc_fid")


if __name__ == '__main__':
unittest.main()

0 comments on commit 54653e4

Please sign in to comment.