Skip to content
Permalink
Browse files

[Oracle] Fixes #39504 : Fix default value evaluation when adding feat…

…ures
  • Loading branch information
troopa81 authored and nyalldawson committed Oct 21, 2020
1 parent f21bd11 commit 29ff58e28b4b5bc4ec28b713b362e091d0a1e1ed
Showing with 45 additions and 3 deletions.
  1. +2 −2 src/providers/oracle/qgsoracleprovider.cpp
  2. +43 −1 tests/src/python/test_provider_oracle.py
@@ -1320,7 +1320,7 @@ bool QgsOracleProvider::addFeatures( QgsFeatureList &flist, QgsFeatureSink::Flag
delim = ',';
kdelim = ',';
fieldId << idx;
defaultValues << defaultValue( idx ).toString();
defaultValues << defaultValueClause( idx );
}

if ( !getfid.prepare( QStringLiteral( "SELECT %1 FROM %2 WHERE ROWID=?" ).arg( keys ).arg( mQuery ) ) )
@@ -1353,7 +1353,7 @@ bool QgsOracleProvider::addFeatures( QgsFeatureList &flist, QgsFeatureSink::Flag

insert += delim + quotedIdentifier( fld.name() );

QString defVal = defaultValue( idx ).toString();
QString defVal = defaultValueClause( idx );

values += delim + '?';
defaultValues.append( defVal );
@@ -23,7 +23,8 @@
QgsTransactionGroup,
QgsFeature,
QgsGeometry,
QgsWkbTypes
QgsWkbTypes,
QgsDataProvider
)

from qgis.PyQt.QtCore import QDate, QTime, QDateTime, QVariant
@@ -777,6 +778,47 @@ def getGeneratedColumnsData(self):
return (QgsVectorLayer(self.dbconn + ' sslmode=disable table="QGIS"."GENERATED_COLUMNS"', 'test', 'oracle'),
"""'test:'||TO_CHAR("pk")""")

def testEvaluateDefaultValues(self):
"""
Test default values evaluation on with or without option EvaluateDefaultValues
see https://github.com/qgis/QGIS/issues/39504
"""

self.execSQLCommand('DROP TABLE "QGIS"."TEST_EVAL_EXPR"', ignore_errors=True)
self.execSQLCommand("""CREATE TABLE "QGIS"."TEST_EVAL_EXPR" (pk INTEGER, "name" VARCHAR2(100) DEFAULT 'qgis')""")

vl = QgsVectorLayer(
self.dbconn + ' sslmode=disable table="QGIS"."TEST_EVAL_EXPR" sql=',
'test', 'oracle')

self.assertTrue(vl.isValid())

# feature with default evaluation clause (not yet evaluated)
feat1 = QgsFeature(vl.fields())
feat1.setAttributes([1, "'qgis'"])

feat2 = QgsFeature(vl.fields())
feat2.setAttributes([2, 'test'])

self.assertTrue(vl.dataProvider().addFeatures([feat1, feat2]))

attributes = [feat.attributes() for feat in vl.getFeatures()]
self.assertEqual(attributes, [[1, 'qgis'], [2, 'test']])

vl.dataProvider().setProviderProperty(QgsDataProvider.EvaluateDefaultValues, True)

# feature with already evaluated default value
feat1 = QgsFeature(vl.fields())
feat1.setAttributes([3, 'qgis'])

feat2 = QgsFeature(vl.fields())
feat2.setAttributes([4, 'test'])

self.assertTrue(vl.dataProvider().addFeatures([feat1, feat2]))

attributes = [feat.attributes() for feat in vl.getFeatures()]
self.assertEqual(attributes, [[1, 'qgis'], [2, 'test'], [3, 'qgis'], [4, 'test']])


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

0 comments on commit 29ff58e

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