Skip to content
Permalink
Browse files

[OGR] Add workaround for OGRSQL not recognizing the column name retur…

…ned by OGR_L_GetFIDColumn
  • Loading branch information
manisandro committed Sep 20, 2017
1 parent 36e6bd1 commit e09946a9c5cbb18a40fe282da5e4166dd1924a98
Showing with 56 additions and 13 deletions.
  1. +37 −12 src/providers/ogr/qgsogrprovider.cpp
  2. +19 −1 tests/src/python/test_provider_ogr.py
  3. BIN tests/testdata/test_gdb.gdb/a00000001.TablesByName.atx
  4. BIN tests/testdata/test_gdb.gdb/a00000001.gdbindexes
  5. BIN tests/testdata/test_gdb.gdb/a00000001.gdbtable
  6. BIN tests/testdata/test_gdb.gdb/a00000001.gdbtablx
  7. BIN tests/testdata/test_gdb.gdb/a00000002.gdbtable
  8. BIN tests/testdata/test_gdb.gdb/a00000002.gdbtablx
  9. BIN tests/testdata/test_gdb.gdb/a00000003.gdbindexes
  10. BIN tests/testdata/test_gdb.gdb/a00000003.gdbtable
  11. BIN tests/testdata/test_gdb.gdb/a00000003.gdbtablx
  12. BIN tests/testdata/test_gdb.gdb/a00000004.CatItemsByPhysicalName.atx
  13. BIN tests/testdata/test_gdb.gdb/a00000004.CatItemsByType.atx
  14. BIN tests/testdata/test_gdb.gdb/a00000004.FDO_UUID.atx
  15. BIN tests/testdata/test_gdb.gdb/a00000004.gdbindexes
  16. BIN tests/testdata/test_gdb.gdb/a00000004.gdbtable
  17. BIN tests/testdata/test_gdb.gdb/a00000004.gdbtablx
  18. BIN tests/testdata/test_gdb.gdb/a00000004.spx
  19. BIN tests/testdata/test_gdb.gdb/a00000005.CatItemTypesByName.atx
  20. BIN tests/testdata/test_gdb.gdb/a00000005.CatItemTypesByParentTypeID.atx
  21. BIN tests/testdata/test_gdb.gdb/a00000005.CatItemTypesByUUID.atx
  22. BIN tests/testdata/test_gdb.gdb/a00000005.gdbindexes
  23. BIN tests/testdata/test_gdb.gdb/a00000005.gdbtable
  24. BIN tests/testdata/test_gdb.gdb/a00000005.gdbtablx
  25. BIN tests/testdata/test_gdb.gdb/a00000006.CatRelsByDestinationID.atx
  26. BIN tests/testdata/test_gdb.gdb/a00000006.CatRelsByOriginID.atx
  27. BIN tests/testdata/test_gdb.gdb/a00000006.CatRelsByType.atx
  28. BIN tests/testdata/test_gdb.gdb/a00000006.FDO_UUID.atx
  29. BIN tests/testdata/test_gdb.gdb/a00000006.gdbindexes
  30. BIN tests/testdata/test_gdb.gdb/a00000006.gdbtable
  31. BIN tests/testdata/test_gdb.gdb/a00000006.gdbtablx
  32. BIN tests/testdata/test_gdb.gdb/a00000007.CatRelTypesByBackwardLabel.atx
  33. BIN tests/testdata/test_gdb.gdb/a00000007.CatRelTypesByDestItemTypeID.atx
  34. BIN tests/testdata/test_gdb.gdb/a00000007.CatRelTypesByForwardLabel.atx
  35. BIN tests/testdata/test_gdb.gdb/a00000007.CatRelTypesByName.atx
  36. BIN tests/testdata/test_gdb.gdb/a00000007.CatRelTypesByOriginItemTypeID.atx
  37. BIN tests/testdata/test_gdb.gdb/a00000007.CatRelTypesByUUID.atx
  38. BIN tests/testdata/test_gdb.gdb/a00000007.gdbindexes
  39. BIN tests/testdata/test_gdb.gdb/a00000007.gdbtable
  40. BIN tests/testdata/test_gdb.gdb/a00000007.gdbtablx
  41. BIN tests/testdata/test_gdb.gdb/a00000009.gdbindexes
  42. BIN tests/testdata/test_gdb.gdb/a00000009.gdbtable
  43. BIN tests/testdata/test_gdb.gdb/a00000009.gdbtablx
  44. BIN tests/testdata/test_gdb.gdb/a00000009.spx
  45. BIN tests/testdata/test_gdb.gdb/a0000000a.gdbindexes
  46. BIN tests/testdata/test_gdb.gdb/a0000000a.gdbtable
  47. BIN tests/testdata/test_gdb.gdb/a0000000a.gdbtablx
  48. BIN tests/testdata/test_gdb.gdb/a0000000a.spx
  49. BIN tests/testdata/test_gdb.gdb/a0000000b.gdbindexes
  50. BIN tests/testdata/test_gdb.gdb/a0000000b.gdbtable
  51. BIN tests/testdata/test_gdb.gdb/a0000000b.gdbtablx
  52. BIN tests/testdata/test_gdb.gdb/a0000000b.spx
  53. BIN tests/testdata/test_gdb.gdb/gdb
  54. BIN tests/testdata/test_gdb.gdb/timestamps
@@ -3443,26 +3443,51 @@ OGRLayerH QgsOgrProviderUtils::setSubsetString( OGRLayerH layer, OGRDataSourceH
layerName = encoding->fromUnicode( modifiedLayerName );
}
}
QByteArray sql;
if ( subsetString.startsWith( "SELECT ", Qt::CaseInsensitive ) )
sql = encoding->fromUnicode( subsetString );
OGRLayerH subsetLayer = 0;
if ( subsetString.startsWith( QLatin1String( "SELECT " ), Qt::CaseInsensitive ) )
{
QByteArray sql = encoding->fromUnicode( subsetString );

QgsDebugMsg( QString( "SQL: %1" ).arg( encoding->toUnicode( sql ) ) );
subsetLayer = GDALDatasetExecuteSQL( ds, sql.constData(), nullptr, nullptr );
}
else
{
QByteArray sqlPart1 = "SELECT ";
QByteArray sqlPart3 = "* FROM " + quotedIdentifier( layerName, ogrDriverName )
+ " WHERE " + encoding->fromUnicode( subsetString );

origFidAddAttempted = true;

QByteArray fidColumn = OGR_L_GetFIDColumn( layer );
// Fallback to FID if OGR_L_GetFIDColumn returns nothing
if ( fidColumn.isEmpty() )
{
fidColumn = "FID";
}

QByteArray sql = sqlPart1 + fidColumn + " as orig_ogc_fid, " + sqlPart3;
QgsDebugMsg( QString( "SQL: %1" ).arg( encoding->toUnicode( sql ) ) );
subsetLayer = GDALDatasetExecuteSQL( ds, sql.constData(), nullptr, nullptr );

sql = QByteArray( "SELECT " );
if ( !fidColumn.isEmpty() )
// See https://lists.osgeo.org/pipermail/qgis-developer/2017-September/049802.html
// If execute SQL fails because it did not find the fidColumn, retry with hardcoded FID
if ( !subsetLayer )
{
sql += fidColumn + " as orig_ogc_fid, ";
origFidAddAttempted = true;
QByteArray sql = sqlPart1 + "FID as orig_ogc_fid, " + sqlPart3;
QgsDebugMsg( QString( "SQL: %1" ).arg( encoding->toUnicode( sql ) ) );
subsetLayer = GDALDatasetExecuteSQL( ds, sql.constData(), nullptr, nullptr );
}
// If that also fails, just continue without the orig_ogc_fid
if ( !subsetLayer )
{
QByteArray sql = sqlPart1 + sqlPart3;
QgsDebugMsg( QString( "SQL: %1" ).arg( encoding->toUnicode( sql ) ) );
subsetLayer = GDALDatasetExecuteSQL( ds, sql.constData(), nullptr, nullptr );
origFidAddAttempted = false;
}
sql += "* FROM " + quotedIdentifier( layerName, ogrDriverName );
sql += " WHERE " + encoding->fromUnicode( subsetString );
}

QgsDebugMsg( QString( "SQL: %1" ).arg( encoding->toUnicode( sql ) ) );
OGRLayerH subsetLayer = OGR_DS_ExecuteSQL( ds, sql.constData(), nullptr, nullptr );

// Check if first column is orig_ogc_fid
if ( origFidAddAttempted && subsetLayer )
{
@@ -17,7 +17,7 @@
import sys
import tempfile

from qgis.core import QgsVectorLayer, QgsVectorDataProvider, QgsWKBTypes
from qgis.core import QgsVectorLayer, QgsVectorDataProvider, QgsWKBTypes, QgsFeature
from qgis.testing import (
start_app,
unittest
@@ -240,5 +240,23 @@ def testGeometryCollection(self):
os.unlink(datasource)
self.assertFalse(os.path.exists(datasource))

def testGdb(self):
""" Test opening a GDB database layer"""
gdb_path = os.path.join(unitTestDataPath(), 'test_gdb.gdb')
for i in range(3):
l = QgsVectorLayer(gdb_path + '|layerid=' + str(i), 'test', 'ogr')
self.assertTrue(l.isValid())

def testGdbFilter(self):
""" Test opening a GDB database layer with filter"""
gdb_path = os.path.join(unitTestDataPath(), 'test_gdb.gdb')
l = QgsVectorLayer(gdb_path + '|layerid=1|subset="text" = \'shape 2\'', 'test', 'ogr')
self.assertTrue(l.isValid())
it = l.getFeatures()
f = QgsFeature()
while it.nextFeature(f):
self.assertTrue(f.attribute("text") == "shape 2")


if __name__ == '__main__':
unittest.main()
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit e09946a

Please sign in to comment.
You can’t perform that action at this time.