Skip to content

Commit d0774e6

Browse files
committedOct 31, 2016
[OGR provider] Make addAttributes() return the requested field type, precision and width so as to make QgsVectorLayerEditBuffer::commitChanges() API
Fixes #15614
1 parent 1e6b5eb commit d0774e6

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed
 

‎src/providers/ogr/qgsogrprovider.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,10 +1342,12 @@ bool QgsOgrProvider::addAttributes( const QList<QgsField> &attributes )
13421342

13431343
bool returnvalue = true;
13441344

1345-
QMap< QString, QVariant::Type > mapFieldTypesToPatch;
1345+
QMap< QString, QgsField > mapFieldNameToOriginalField;
13461346

13471347
for ( QList<QgsField>::const_iterator iter = attributes.begin(); iter != attributes.end(); ++iter )
13481348
{
1349+
mapFieldNameToOriginalField[ iter->name()] = *iter;
1350+
13491351
OGRFieldType type;
13501352

13511353
switch ( iter->type() )
@@ -1361,7 +1363,6 @@ bool QgsOgrProvider::addAttributes( const QList<QgsField> &attributes )
13611363
type = OFTInteger64;
13621364
else
13631365
{
1364-
mapFieldTypesToPatch[ iter->name()] = iter->type();
13651366
type = OFTReal;
13661367
}
13671368
break;
@@ -1404,13 +1405,23 @@ bool QgsOgrProvider::addAttributes( const QList<QgsField> &attributes )
14041405
}
14051406
loadFields();
14061407

1407-
// Patch field type in case of Integer64->Real mapping so that QVariant::LongLong
1408-
// is still returned to the caller
1409-
for ( QMap< QString, QVariant::Type >::const_iterator it = mapFieldTypesToPatch.begin(); it != mapFieldTypesToPatch.end(); ++it )
1408+
// The check in QgsVectorLayerEditBuffer::commitChanges() is questionable with
1409+
// real-world drivers that might only be able to satisfy request only partially.
1410+
// So to avoid erroring out, patch field type, width and precision to match
1411+
// what was requested.
1412+
// For example in case of Integer64->Real mapping so that QVariant::LongLong is
1413+
// still returned to the caller
1414+
// Or if a field width was specified but not strictly enforced by the driver (#15614)
1415+
for ( QMap< QString, QgsField >::const_iterator it = mapFieldNameToOriginalField.begin();
1416+
it != mapFieldNameToOriginalField.end(); ++it )
14101417
{
14111418
int idx = mAttributeFields.fieldNameIndex( it.key() );
14121419
if ( idx >= 0 )
1413-
mAttributeFields[ idx ].setType( *it );
1420+
{
1421+
mAttributeFields[ idx ].setType( it->type() );
1422+
mAttributeFields[ idx ].setLength( it->length() );
1423+
mAttributeFields[ idx ].setPrecision( it->precision() );
1424+
}
14141425
}
14151426

14161427
return returnvalue;

0 commit comments

Comments
 (0)
Please sign in to comment.