Skip to content

Commit

Permalink
save as: add a numeric postfix to reserved field ogc_fid when saving …
Browse files Browse the repository at this point in the history
…to SQLite (fixes #8245)
  • Loading branch information
jef-n committed Jul 11, 2013
1 parent 56210eb commit ac21c62
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions src/core/qgsvectorfilewriter.cpp
Expand Up @@ -354,8 +354,35 @@ QgsVectorFileWriter::QgsVectorFileWriter(
return;
}

QString name( attrField.name() );

if ( driverName == "SQLite" && name.compare( "ogc_fid", Qt::CaseInsensitive ) == 0 )
{
int i;
for ( i = 0; i < 10; i++ )
{
name = QString( "ogc_fid%1" ).arg( i );

int j;
for ( j = 0; j < fields.size() && name.compare( fields[j].name(), Qt::CaseInsensitive ) != 0; j++ )
;

if ( j == fields.size() )
break;
}

if ( i == 10 )
{
mErrorMessage = QObject::tr( "no available replacement for internal fieldname ogc_fid found" ).arg( attrField.name() );
mError = ErrAttributeCreationFailed;
return;
}

QgsMessageLog::logMessage( QObject::tr( "Reserved attribute name ogc_fid replaced with %1" ).arg( name ), QObject::tr( "OGR" ) );
}

// create field definition
OGRFieldDefnH fld = OGR_Fld_Create( mCodec->fromUnicode( attrField.name() ), ogrType );
OGRFieldDefnH fld = OGR_Fld_Create( mCodec->fromUnicode( name ), ogrType );
if ( ogrWidth > 0 )
{
OGR_Fld_SetWidth( fld, ogrWidth );
Expand Down Expand Up @@ -383,7 +410,7 @@ QgsVectorFileWriter::QgsVectorFileWriter(
}
OGR_Fld_Destroy( fld );

int ogrIdx = OGR_FD_GetFieldIndex( defn, mCodec->fromUnicode( attrField.name() ) );
int ogrIdx = OGR_FD_GetFieldIndex( defn, mCodec->fromUnicode( name ) );
if ( ogrIdx < 0 )
{
#if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM < 1700
Expand Down

0 comments on commit ac21c62

Please sign in to comment.