Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix issue when inserting a feature with only null attributes
  • Loading branch information
troopa81 authored and nyalldawson committed Feb 19, 2021
1 parent 209fad5 commit b1067af
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
3 changes: 0 additions & 3 deletions src/providers/oracle/qgsoracleprovider.cpp
Expand Up @@ -1342,11 +1342,8 @@ bool QgsOracleProvider::addFeatures( QgsFeatureList &flist, QgsFeatureSink::Flag
// e.g. for defaults
for ( int idx = 0; idx < std::min( attributevec.size(), mAttributeFields.size() ); ++idx )
{
QVariant v = attributevec[idx];
if ( mAlwaysGenerated.at( idx ) )
continue;
if ( !v.isValid() )
continue;

if ( fieldId.contains( idx ) )
continue;
Expand Down
43 changes: 43 additions & 0 deletions tests/src/python/test_provider_oracle.py
Expand Up @@ -908,6 +908,49 @@ def testCreateInvalidLayer(self):
exporter = QgsVectorLayerExporter(uri=uri, provider='oracle', fields=fields, geometryType=QgsWkbTypes.NoGeometry, crs=QgsCoordinateReferenceSystem(), overwrite=True)
self.assertEqual(exporter.errorCode(), QgsVectorLayerExporter.ErrCreateDataSource)

def testAddEmptyFeature(self):
"""
Test inserting a feature with only null attributes
"""

def countFeature(table_name):
self.assertTrue(self.conn)
query = QSqlQuery(self.conn)
res = query.exec_('SELECT count(*) FROM "QGIS"."{}"'.format(table_name))
self.assertTrue(query.next())
count = query.value(0)
query.finish()
return count

self.execSQLCommand('DROP TABLE "QGIS"."EMPTYFEATURE_LAYER"', ignore_errors=True)
self.execSQLCommand('CREATE TABLE "QGIS"."EMPTYFEATURE_LAYER" ( "num" INTEGER, GEOM SDO_GEOMETRY)')

vl = QgsVectorLayer(self.dbconn + ' sslmode=disable type=Point table="QGIS"."EMPTYFEATURE_LAYER" (GEOM) sql=', 'test', 'oracle')
self.assertTrue(vl.isValid())

# add feature with no attributes, no geometry
feature = QgsFeature(vl.fields())
self.assertTrue(vl.dataProvider().addFeatures([feature])[0])
self.assertEqual(countFeature('EMPTYFEATURE_LAYER'), 1)

# add feature with no attribute and one geometry
feature = QgsFeature(vl.fields())
feature.setGeometry(QgsGeometry.fromWkt('Point (43.5 1.42)'))
self.assertTrue(vl.dataProvider().addFeatures([feature])[0])
self.assertEqual(countFeature('EMPTYFEATURE_LAYER'), 2)

self.execSQLCommand('DROP TABLE "QGIS"."EMPTYFEATURE_NOGEOM_LAYER"', ignore_errors=True)
self.execSQLCommand('CREATE TABLE "QGIS"."EMPTYFEATURE_NOGEOM_LAYER" ( "num" INTEGER)')

# same tests but with no geometry in table definition
vl = QgsVectorLayer(self.dbconn + ' sslmode=disable table="QGIS"."EMPTYFEATURE_NOGEOM_LAYER" sql=', 'test', 'oracle')
self.assertTrue(vl.isValid())

# add feature with no attributes
feature = QgsFeature(vl.fields())
self.assertTrue(vl.dataProvider().addFeatures([feature])[0])
self.assertEqual(countFeature('EMPTYFEATURE_NOGEOM_LAYER'), 1)


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

0 comments on commit b1067af

Please sign in to comment.