Skip to content

Commit 38a7711

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 8082762 commit 38a7711

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
@@ -1309,10 +1309,12 @@ bool QgsOgrProvider::addAttributes( const QList<QgsField> &attributes )
13091309

13101310
bool returnvalue = true;
13111311

1312-
QMap< QString, QVariant::Type > mapFieldTypesToPatch;
1312+
QMap< QString, QgsField > mapFieldNameToOriginalField;
13131313

13141314
for ( QList<QgsField>::const_iterator iter = attributes.begin(); iter != attributes.end(); ++iter )
13151315
{
1316+
mapFieldNameToOriginalField[ iter->name()] = *iter;
1317+
13161318
OGRFieldType type;
13171319

13181320
switch ( iter->type() )
@@ -1328,7 +1330,6 @@ bool QgsOgrProvider::addAttributes( const QList<QgsField> &attributes )
13281330
type = OFTInteger64;
13291331
else
13301332
{
1331-
mapFieldTypesToPatch[ iter->name()] = iter->type();
13321333
type = OFTReal;
13331334
}
13341335
break;
@@ -1371,13 +1372,23 @@ bool QgsOgrProvider::addAttributes( const QList<QgsField> &attributes )
13711372
}
13721373
loadFields();
13731374

1374-
// Patch field type in case of Integer64->Real mapping so that QVariant::LongLong
1375-
// is still returned to the caller
1376-
for ( QMap< QString, QVariant::Type >::const_iterator it = mapFieldTypesToPatch.begin(); it != mapFieldTypesToPatch.end(); ++it )
1375+
// The check in QgsVectorLayerEditBuffer::commitChanges() is questionable with
1376+
// real-world drivers that might only be able to satisfy request only partially.
1377+
// So to avoid erroring out, patch field type, width and precision to match
1378+
// what was requested.
1379+
// For example in case of Integer64->Real mapping so that QVariant::LongLong is
1380+
// still returned to the caller
1381+
// Or if a field width was specified but not strictly enforced by the driver (#15614)
1382+
for ( QMap< QString, QgsField >::const_iterator it = mapFieldNameToOriginalField.begin();
1383+
it != mapFieldNameToOriginalField.end(); ++it )
13771384
{
13781385
int idx = mAttributeFields.fieldNameIndex( it.key() );
13791386
if ( idx >= 0 )
1380-
mAttributeFields[ idx ].setType( *it );
1387+
{
1388+
mAttributeFields[ idx ].setType( it->type() );
1389+
mAttributeFields[ idx ].setLength( it->length() );
1390+
mAttributeFields[ idx ].setPrecision( it->precision() );
1391+
}
13811392
}
13821393

13831394
return returnvalue;

0 commit comments

Comments
 (0)
Please sign in to comment.