Skip to content

Commit ac21c62

Browse files
committed
save as: add a numeric postfix to reserved field ogc_fid when saving to SQLite (fixes #8245)
1 parent 56210eb commit ac21c62

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

src/core/qgsvectorfilewriter.cpp

+29-2
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,35 @@ QgsVectorFileWriter::QgsVectorFileWriter(
354354
return;
355355
}
356356

357+
QString name( attrField.name() );
358+
359+
if ( driverName == "SQLite" && name.compare( "ogc_fid", Qt::CaseInsensitive ) == 0 )
360+
{
361+
int i;
362+
for ( i = 0; i < 10; i++ )
363+
{
364+
name = QString( "ogc_fid%1" ).arg( i );
365+
366+
int j;
367+
for ( j = 0; j < fields.size() && name.compare( fields[j].name(), Qt::CaseInsensitive ) != 0; j++ )
368+
;
369+
370+
if ( j == fields.size() )
371+
break;
372+
}
373+
374+
if ( i == 10 )
375+
{
376+
mErrorMessage = QObject::tr( "no available replacement for internal fieldname ogc_fid found" ).arg( attrField.name() );
377+
mError = ErrAttributeCreationFailed;
378+
return;
379+
}
380+
381+
QgsMessageLog::logMessage( QObject::tr( "Reserved attribute name ogc_fid replaced with %1" ).arg( name ), QObject::tr( "OGR" ) );
382+
}
383+
357384
// create field definition
358-
OGRFieldDefnH fld = OGR_Fld_Create( mCodec->fromUnicode( attrField.name() ), ogrType );
385+
OGRFieldDefnH fld = OGR_Fld_Create( mCodec->fromUnicode( name ), ogrType );
359386
if ( ogrWidth > 0 )
360387
{
361388
OGR_Fld_SetWidth( fld, ogrWidth );
@@ -383,7 +410,7 @@ QgsVectorFileWriter::QgsVectorFileWriter(
383410
}
384411
OGR_Fld_Destroy( fld );
385412

386-
int ogrIdx = OGR_FD_GetFieldIndex( defn, mCodec->fromUnicode( attrField.name() ) );
413+
int ogrIdx = OGR_FD_GetFieldIndex( defn, mCodec->fromUnicode( name ) );
387414
if ( ogrIdx < 0 )
388415
{
389416
#if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM < 1700

0 commit comments

Comments
 (0)