Skip to content

Commit

Permalink
[OGR provider] Make addAttributes() return the requested field type, …
Browse files Browse the repository at this point in the history
…precision and width so as to make QgsVectorLayerEditBuffer::commitChanges() API

Fixes #15614
  • Loading branch information
rouault committed Oct 31, 2016
1 parent 7d2098c commit bb9c75b
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/providers/ogr/qgsogrprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1348,10 +1348,12 @@ bool QgsOgrProvider::addAttributes( const QList<QgsField> &attributes )

bool returnvalue = true;

QMap< QString, QVariant::Type > mapFieldTypesToPatch;
QMap< QString, QgsField > mapFieldNameToOriginalField;

for ( QList<QgsField>::const_iterator iter = attributes.begin(); iter != attributes.end(); ++iter )
{
mapFieldNameToOriginalField[ iter->name()] = *iter;

OGRFieldType type;

switch ( iter->type() )
Expand All @@ -1367,7 +1369,6 @@ bool QgsOgrProvider::addAttributes( const QList<QgsField> &attributes )
type = OFTInteger64;
else
{
mapFieldTypesToPatch[ iter->name()] = iter->type();
type = OFTReal;
}
break;
Expand Down Expand Up @@ -1410,13 +1411,23 @@ bool QgsOgrProvider::addAttributes( const QList<QgsField> &attributes )
}
loadFields();

// Patch field type in case of Integer64->Real mapping so that QVariant::LongLong
// is still returned to the caller
for ( QMap< QString, QVariant::Type >::const_iterator it = mapFieldTypesToPatch.begin(); it != mapFieldTypesToPatch.end(); ++it )
// The check in QgsVectorLayerEditBuffer::commitChanges() is questionable with
// real-world drivers that might only be able to satisfy request only partially.
// So to avoid erroring out, patch field type, width and precision to match
// what was requested.
// For example in case of Integer64->Real mapping so that QVariant::LongLong is
// still returned to the caller
// Or if a field width was specified but not strictly enforced by the driver (#15614)
for ( QMap< QString, QgsField >::const_iterator it = mapFieldNameToOriginalField.begin();

This comment has been minimized.

Copy link
@m-kuhn

m-kuhn Oct 31, 2016

Member

Shouldn't this use constBegin()?

This comment has been minimized.

Copy link
@rouault

rouault Oct 31, 2016

Author Contributor

Fixed. Thanks

it != mapFieldNameToOriginalField.end(); ++it )
{
int idx = mAttributeFields.lookupField( it.key() );
if ( idx >= 0 )
mAttributeFields[ idx ].setType( *it );
{
mAttributeFields[ idx ].setType( it->type() );
mAttributeFields[ idx ].setLength( it->length() );
mAttributeFields[ idx ].setPrecision( it->precision() );
}
}

return returnvalue;
Expand Down

0 comments on commit bb9c75b

Please sign in to comment.