Skip to content

Commit

Permalink
Fixes qgis#31626 don't crash on oracle getFeature(id) with an invalid id
Browse files Browse the repository at this point in the history
  • Loading branch information
troopa81 committed Feb 27, 2020
1 parent 7e531f1 commit e15738f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/providers/oracle/qgsoracleprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,11 +410,9 @@ QString QgsOracleUtils::whereClause( QgsFeatureId featureId, const QgsFields &fi
case PktRowId:
case PktFidMap:
{
QVariant pkValsVariant = sharedData->lookupKey( featureId );
if ( !pkValsVariant.isNull() )
QVariantList pkVals = sharedData->lookupKey( featureId );
if ( !pkVals.isEmpty() )
{
QList<QVariant> pkVals = pkValsVariant.toList();

if ( primaryKeyType == PktFidMap )
{
Q_ASSERT( pkVals.size() == primaryKeyAttrs.size() );
Expand Down
44 changes: 44 additions & 0 deletions tests/src/python/test_provider_oracle.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,50 @@ def testTransactionTuple(self):
# underlying data has not been modified
self.assertFalse(vl.isModified())

def testIdentityCommit(self):
# create a vector layer based on oracle
vl = QgsVectorLayer(
self.dbconn + ' sslmode=disable key=\'pk\' srid=4326 type=POINT table="QGIS"."POINT_DATA_IDENTITY" (GEOM) sql=',
'test', 'oracle')
self.assertTrue(vl.isValid())

features = [f for f in vl.getFeatures()]

# add a new feature
newf = QgsFeature(features[0].fields())
success, featureAdded = vl.dataProvider().addFeatures([newf])
self.assertTrue(success)

# clean up
features = [f for f in vl.getFeatures()]
vl.dataProvider().deleteFeatures([features[-1].id()])

def testGetFeatureFidInvalid(self):
"""
Get feature with an invalid fid
https://github.com/qgis/QGIS/issues/31626
"""
self.execSQLCommand('DROP TABLE "QGIS"."TABLE_QGIS_ISSUE_FLOAT_KEY"', ignore_errors=True)
self.execSQLCommand("""CREATE TABLE "QGIS"."TABLE_QGIS_ISSUE_FLOAT_KEY" (CODE NUMBER PRIMARY KEY, DESCRIPTION VARCHAR2(25))""")
self.execSQLCommand("""INSERT INTO "QGIS"."TABLE_QGIS_ISSUE_FLOAT_KEY" VALUES(1000,'Desc for 1st record')""")
self.execSQLCommand("""INSERT INTO "QGIS"."TABLE_QGIS_ISSUE_FLOAT_KEY" VALUES(2000,'Desc for 2nd record')""")
self.execSQLCommand("""CREATE OR REPLACE VIEW "QGIS"."VIEW_QGIS_ISSUE_FLOAT_KEY" AS SELECT * FROM "QGIS"."TABLE_QGIS_ISSUE_FLOAT_KEY" """)

vl = QgsVectorLayer(
self.dbconn + ' sslmode=disable key=\'CODE\' table="QGIS"."VIEW_QGIS_ISSUE_FLOAT_KEY" sql=',
'test', 'oracle')

# feature are not loaded yet, mapping between CODE and fid is not built so feature
# is invalid
feature = vl.getFeature(2)
self.assertTrue(not feature.isValid())

# load all features
for f in vl.getFeatures():
pass

feature = vl.getFeature(2)
self.assertTrue(feature.isValid())

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

0 comments on commit e15738f

Please sign in to comment.