Skip to content
Permalink
Browse files

Prioritise provider default literals over reused values when

creating a new feature
  • Loading branch information
nyalldawson authored and m-kuhn committed Nov 16, 2016
1 parent 631bd48 commit 249c8dca20716ef0b20d02dba80b2a185725c4f1
Showing with 16 additions and 23 deletions.
  1. +12 −19 src/core/qgsvectorlayerutils.cpp
  2. +4 −4 tests/src/python/test_provider_spatialite.py
@@ -27,18 +27,6 @@ bool QgsVectorLayerUtils::valueExists( const QgsVectorLayer* layer, int fieldInd
if ( fieldIndex < 0 || fieldIndex >= fields.count() )
return false;

// check - if value is a provider side defaultValueClause then we exclude it from the check
if ( fields.fieldOrigin( fieldIndex ) == QgsFields::OriginProvider )
{
int providerIdx = fields.fieldOriginIndex( fieldIndex );
QString providerDefaultClause = layer->dataProvider()->defaultValueClause( providerIdx );
if ( !providerDefaultClause.isEmpty() && value.toString() == providerDefaultClause )
{
// exempt from check
return false;
}
}

QString fieldName = fields.at( fieldIndex ).name();

// build up an optimised feature request
@@ -272,19 +260,24 @@ QgsFeature QgsVectorLayerUtils::createFeature( QgsVectorLayer* layer, const QgsG
}
}

// 3. passed attribute value
// 3. provider side default literal
// note - deliberately not using else if!
if ( !v.isValid() && attributes.contains( idx ) )
if ( !v.isValid() && fields.fieldOrigin( idx ) == QgsFields::OriginProvider )
{
v = attributes.value( idx );
int providerIndex = fields.fieldOriginIndex( idx );
v = layer->dataProvider()->defaultValue( providerIndex );
if ( v.isValid() )
{
//trust that the provider default has been sensibly set not to violate any constraints
checkUnique = false;
}
}

// 4. provider side default literal
// 4. passed attribute value
// note - deliberately not using else if!
if ( !v.isValid() && fields.fieldOrigin( idx ) == QgsFields::OriginProvider )
if ( !v.isValid() && attributes.contains( idx ) )
{
int providerIndex = fields.fieldOriginIndex( idx );
v = layer->dataProvider()->defaultValue( providerIndex );
v = attributes.value( idx );
}

// last of all... check that unique constraints are respected
@@ -474,14 +474,14 @@ def testVectorLayerUtilsCreateFeatureWithProviderDefaultLiteral(self):
f = QgsVectorLayerUtils.createFeature(vl)
self.assertEqual(f.attributes(), [None, "qgis 'is good", 5, 5.7, None])

# check that provider passed attribute values take precedence over default literals
# check that provider default literals take precedence over passed attribute values
f = QgsVectorLayerUtils.createFeature(vl, attributes={1: 'qgis is great', 0: 3})
self.assertEqual(f.attributes(), [3, "qgis is great", 5, 5.7, None])
self.assertEqual(f.attributes(), [3, "qgis 'is good", 5, 5.7, None])

# test take vector layer default value expression overrides postgres provider default clause
# test that vector layer default value expression overrides provider default literal
vl.setDefaultValueExpression(3, "4*3")
f = QgsVectorLayerUtils.createFeature(vl, attributes={1: 'qgis is great', 0: 3})
self.assertEqual(f.attributes(), [3, "qgis is great", 5, 12, None])
self.assertEqual(f.attributes(), [3, "qgis 'is good", 5, 12, None])

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

0 comments on commit 249c8dc

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