Skip to content

Commit 8cf03ce

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 d6860f3 commit 8cf03ce

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

src/providers/ogr/qgsogrprovider.cpp

+33-1
Original file line numberDiff line numberDiff line change
@@ -1153,8 +1153,12 @@ bool QgsOgrProvider::addAttributes( const QList<QgsField> &attributes )
11531153

11541154
bool returnvalue = true;
11551155

1156+
QMap< QString, QgsField > mapFieldNameToOriginalField;
1157+
11561158
for ( QList<QgsField>::const_iterator iter = attributes.begin(); iter != attributes.end(); ++iter )
11571159
{
1160+
mapFieldNameToOriginalField[ iter->name()] = *iter;
1161+
11581162
OGRFieldType type;
11591163

11601164
switch ( iter->type() )
@@ -1164,8 +1168,16 @@ bool QgsOgrProvider::addAttributes( const QList<QgsField> &attributes )
11641168
break;
11651169
#if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 2000000
11661170
case QVariant::LongLong:
1167-
type = OFTInteger64;
1171+
{
1172+
const char* pszDataTypes = GDALGetMetadataItem( ogrDriver, GDAL_DMD_CREATIONFIELDDATATYPES, NULL );
1173+
if ( pszDataTypes && strstr( pszDataTypes, "Integer64" ) )
1174+
type = OFTInteger64;
1175+
else
1176+
{
1177+
type = OFTReal;
1178+
}
11681179
break;
1180+
}
11691181
#endif
11701182
case QVariant::Double:
11711183
type = OFTReal;
@@ -1203,6 +1215,26 @@ bool QgsOgrProvider::addAttributes( const QList<QgsField> &attributes )
12031215
OGR_Fld_Destroy( fielddefn );
12041216
}
12051217
loadFields();
1218+
1219+
// The check in QgsVectorLayerEditBuffer::commitChanges() is questionable with
1220+
// real-world drivers that might only be able to satisfy request only partially.
1221+
// So to avoid erroring out, patch field type, width and precision to match
1222+
// what was requested.
1223+
// For example in case of Integer64->Real mapping so that QVariant::LongLong is
1224+
// still returned to the caller
1225+
// Or if a field width was specified but not strictly enforced by the driver (#15614)
1226+
for ( QMap< QString, QgsField >::const_iterator it = mapFieldNameToOriginalField.begin();
1227+
it != mapFieldNameToOriginalField.end(); ++it )
1228+
{
1229+
int idx = mAttributeFields.fieldNameIndex( it.key() );
1230+
if ( idx >= 0 )
1231+
{
1232+
mAttributeFields[ idx ].setType( it->type() );
1233+
mAttributeFields[ idx ].setLength( it->length() );
1234+
mAttributeFields[ idx ].setPrecision( it->precision() );
1235+
}
1236+
}
1237+
12061238
return returnvalue;
12071239
}
12081240

0 commit comments

Comments
 (0)