@@ -3443,33 +3443,59 @@ OGRLayerH QgsOgrProviderUtils::setSubsetString( OGRLayerH layer, OGRDataSourceH
3443
3443
layerName = encoding->fromUnicode ( modifiedLayerName );
3444
3444
}
3445
3445
}
3446
- QByteArray sql;
3447
- if ( subsetString.startsWith ( " SELECT " , Qt::CaseInsensitive ) )
3448
- sql = encoding->fromUnicode ( subsetString );
3446
+ OGRLayerH subsetLayer = 0 ;
3447
+ if ( subsetString.startsWith ( QLatin1String ( " SELECT " ), Qt::CaseInsensitive ) )
3448
+ {
3449
+ QByteArray sql = encoding->fromUnicode ( subsetString );
3450
+
3451
+ QgsDebugMsg ( QString ( " SQL: %1" ).arg ( encoding->toUnicode ( sql ) ) );
3452
+ subsetLayer = GDALDatasetExecuteSQL ( ds, sql.constData (), nullptr , nullptr );
3453
+ }
3449
3454
else
3450
3455
{
3456
+ QByteArray sqlPart1 = " SELECT *" ;
3457
+ QByteArray sqlPart3 = " FROM " + quotedIdentifier ( layerName, ogrDriverName )
3458
+ + " WHERE " + encoding->fromUnicode ( subsetString );
3459
+
3460
+ origFidAddAttempted = true ;
3461
+
3451
3462
QByteArray fidColumn = OGR_L_GetFIDColumn ( layer );
3463
+ // Fallback to FID if OGR_L_GetFIDColumn returns nothing
3464
+ if ( fidColumn.isEmpty () )
3465
+ {
3466
+ fidColumn = " FID" ;
3467
+ }
3468
+
3469
+ QByteArray sql = sqlPart1 + " , " + fidColumn + " as orig_ogc_fid" + sqlPart3;
3470
+ QgsDebugMsg ( QString ( " SQL: %1" ).arg ( encoding->toUnicode ( sql ) ) );
3471
+ subsetLayer = GDALDatasetExecuteSQL ( ds, sql.constData (), nullptr , nullptr );
3452
3472
3453
- sql = QByteArray ( " SELECT " );
3454
- if ( !fidColumn.isEmpty () )
3473
+ // See https://lists.osgeo.org/pipermail/qgis-developer/2017-September/049802.html
3474
+ // If execute SQL fails because it did not find the fidColumn, retry with hardcoded FID
3475
+ if ( !subsetLayer )
3455
3476
{
3456
- sql += fidColumn + " as orig_ogc_fid, " ;
3457
- origFidAddAttempted = true ;
3477
+ QByteArray sql = sqlPart1 + " , " + " FID as orig_ogc_fid" + sqlPart3;
3478
+ QgsDebugMsg ( QString ( " SQL: %1" ).arg ( encoding->toUnicode ( sql ) ) );
3479
+ subsetLayer = GDALDatasetExecuteSQL ( ds, sql.constData (), nullptr , nullptr );
3480
+ }
3481
+ // If that also fails, just continue without the orig_ogc_fid
3482
+ if ( !subsetLayer )
3483
+ {
3484
+ QByteArray sql = sqlPart1 + sqlPart3;
3485
+ QgsDebugMsg ( QString ( " SQL: %1" ).arg ( encoding->toUnicode ( sql ) ) );
3486
+ subsetLayer = GDALDatasetExecuteSQL ( ds, sql.constData (), nullptr , nullptr );
3487
+ origFidAddAttempted = false ;
3458
3488
}
3459
- sql += " * FROM " + quotedIdentifier ( layerName, ogrDriverName );
3460
- sql += " WHERE " + encoding->fromUnicode ( subsetString );
3461
3489
}
3462
3490
3463
- QgsDebugMsg ( QString ( " SQL: %1" ).arg ( encoding->toUnicode ( sql ) ) );
3464
- OGRLayerH subsetLayer = OGR_DS_ExecuteSQL ( ds, sql.constData (), nullptr , nullptr );
3465
-
3466
- // Check if first column is orig_ogc_fid
3491
+ // Check if last column is orig_ogc_fid
3467
3492
if ( origFidAddAttempted && subsetLayer )
3468
3493
{
3469
3494
OGRFeatureDefnH fdef = OGR_L_GetLayerDefn ( subsetLayer );
3470
- if ( OGR_FD_GetFieldCount ( fdef ) > 0 )
3495
+ int fieldCount = OGR_FD_GetFieldCount ( fdef );
3496
+ if ( fieldCount > 0 )
3471
3497
{
3472
- OGRFieldDefnH fldDef = OGR_FD_GetFieldDefn ( fdef, 0 );
3498
+ OGRFieldDefnH fldDef = OGR_FD_GetFieldDefn ( fdef, fieldCount - 1 );
3473
3499
origFidAdded = qstrcmp ( OGR_Fld_GetNameRef ( fldDef ), " orig_ogc_fid" ) == 0 ;
3474
3500
}
3475
3501
}
0 commit comments