Skip to content

Commit bb9c75b

Browse files
committed
[OGR provider] Make addAttributes() return the requested field type, precision and width so as to make QgsVectorLayerEditBuffer::commitChanges() API
Fixes #15614
1 parent 7d2098c commit bb9c75b

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

src/providers/ogr/qgsogrprovider.cpp

+17-6
Original file line numberDiff line numberDiff line change
@@ -1348,10 +1348,12 @@ bool QgsOgrProvider::addAttributes( const QList<QgsField> &attributes )
13481348

13491349
bool returnvalue = true;
13501350

1351-
QMap< QString, QVariant::Type > mapFieldTypesToPatch;
1351+
QMap< QString, QgsField > mapFieldNameToOriginalField;
13521352

13531353
for ( QList<QgsField>::const_iterator iter = attributes.begin(); iter != attributes.end(); ++iter )
13541354
{
1355+
mapFieldNameToOriginalField[ iter->name()] = *iter;
1356+
13551357
OGRFieldType type;
13561358

13571359
switch ( iter->type() )
@@ -1367,7 +1369,6 @@ bool QgsOgrProvider::addAttributes( const QList<QgsField> &attributes )
13671369
type = OFTInteger64;
13681370
else
13691371
{
1370-
mapFieldTypesToPatch[ iter->name()] = iter->type();
13711372
type = OFTReal;
13721373
}
13731374
break;
@@ -1410,13 +1411,23 @@ bool QgsOgrProvider::addAttributes( const QList<QgsField> &attributes )
14101411
}
14111412
loadFields();
14121413

1413-
// Patch field type in case of Integer64->Real mapping so that QVariant::LongLong
1414-
// is still returned to the caller
1415-
for ( QMap< QString, QVariant::Type >::const_iterator it = mapFieldTypesToPatch.begin(); it != mapFieldTypesToPatch.end(); ++it )
1414+
// The check in QgsVectorLayerEditBuffer::commitChanges() is questionable with
1415+
// real-world drivers that might only be able to satisfy request only partially.
1416+
// So to avoid erroring out, patch field type, width and precision to match
1417+
// what was requested.
1418+
// For example in case of Integer64->Real mapping so that QVariant::LongLong is
1419+
// still returned to the caller
1420+
// Or if a field width was specified but not strictly enforced by the driver (#15614)
1421+
for ( QMap< QString, QgsField >::const_iterator it = mapFieldNameToOriginalField.begin();
1422+
it != mapFieldNameToOriginalField.end(); ++it )
14161423
{
14171424
int idx = mAttributeFields.lookupField( it.key() );
14181425
if ( idx >= 0 )
1419-
mAttributeFields[ idx ].setType( *it );
1426+
{
1427+
mAttributeFields[ idx ].setType( it->type() );
1428+
mAttributeFields[ idx ].setLength( it->length() );
1429+
mAttributeFields[ idx ].setPrecision( it->precision() );
1430+
}
14201431
}
14211432

14221433
return returnvalue;

0 commit comments

Comments
 (0)