Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport queued_ltr_backports] OGR CSV: fix slow update #51700

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions src/core/providers/ogr/qgsogrprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2168,11 +2168,9 @@ bool QgsOgrProvider::changeAttributeValues( const QgsChangedAttributesMap &attr_
// But this is undesirable in general, so don't do this when we know that
// we don't need to.
bool mayNeedResetReadingAfterGetFeature = true;
if ( mGDALDriverName == QLatin1String( "ESRI Shapefile" ) )
{
mayNeedResetReadingAfterGetFeature = false;
}
else if ( mGDALDriverName == QLatin1String( "GPKG" ) )
if ( mGDALDriverName == QLatin1String( "ESRI Shapefile" ) ||
mGDALDriverName == QLatin1String( "GPKG" ) ||
mGDALDriverName == QLatin1String( "CSV" ) )
{
mayNeedResetReadingAfterGetFeature = false;
}
Expand Down
35 changes: 35 additions & 0 deletions tests/src/python/test_provider_ogr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2964,6 +2964,41 @@ def testUnknownButNoGeometry(self):
self.assertEqual(vl.featureCount(), 2)
self.assertEqual(len([x for x in vl.getFeatures()]), 2)

def testCsvInterleavedUpdate(self):
"""Checks whether a full update with interleaved fids works reliably, related to GH #51668"""

temp_dir = QTemporaryDir()
temp_path = temp_dir.path()
csv_path = os.path.join(temp_path, 'test.csv')
with open(csv_path, 'w+') as f:
f.write('fid\tname\n')
f.write('1\t"feat 1"\n')
f.write('2\t"feat 2"\n')
f.write('3\t"feat 3"\n')

vl = QgsVectorLayer(csv_path, 'csv')
self.assertTrue(vl.isValid())
self.assertEqual(vl.featureCount(), 3)
f = next(vl.getFeatures())
self.assertEqual(f.attributes(), ['1', "feat 1"])

self.assertTrue(vl.startEditing())
self.assertTrue(vl.addAttribute(QgsField('newf', QVariant.String)))
self.assertTrue(vl.changeAttributeValues(3, {2: 'fid 3'}))
self.assertTrue(vl.changeAttributeValues(1, {2: 'fid 1'}))
self.assertTrue(vl.changeAttributeValues(2, {2: 'fid 2'}))
self.assertTrue(vl.commitChanges())

vl = QgsVectorLayer(csv_path, 'csv')
self.assertTrue(vl.isValid())
self.assertEqual(vl.featureCount(), 3)
features = {f.id(): f.attributes() for f in vl.getFeatures()}
self.assertEqual(features, {
1: ['1', 'feat 1', 'fid 1'],
2: ['2', 'feat 2', 'fid 2'],
3: ['3', 'feat 3', 'fid 3']
})


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