Skip to content

Commit

Permalink
[OGR] Add workaround for OGRSQL not recognizing the column name retur…
Browse files Browse the repository at this point in the history
…ned by OGR_L_GetFIDColumn
  • Loading branch information
manisandro committed Sep 21, 2017
1 parent 6e5324b commit 4ce2cf1
Show file tree
Hide file tree
Showing 54 changed files with 54 additions and 12 deletions.
47 changes: 36 additions & 11 deletions src/providers/ogr/qgsogrprovider.cpp
Expand Up @@ -3537,26 +3537,51 @@ OGRLayerH QgsOgrProviderUtils::setSubsetString( OGRLayerH layer, GDALDatasetH ds
layerName = encoding->fromUnicode( modifiedLayerName );
}
}
QByteArray sql;
OGRLayerH subsetLayer = 0;
if ( subsetString.startsWith( QLatin1String( "SELECT " ), Qt::CaseInsensitive ) )
sql = encoding->fromUnicode( subsetString );
{
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, mGDALDriverName )
+ " 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, mGDALDriverName );
sql += " WHERE " + encoding->fromUnicode( subsetString );
}

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

// Check if first column is orig_ogc_fid
if ( origFidAddAttempted && subsetLayer )
{
Expand Down
19 changes: 18 additions & 1 deletion tests/src/python/test_provider_ogr.py
Expand Up @@ -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
Expand Down Expand Up @@ -238,6 +238,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 added tests/testdata/test_gdb.gdb/a00000001.gdbindexes
Binary file not shown.
Binary file added tests/testdata/test_gdb.gdb/a00000001.gdbtable
Binary file not shown.
Binary file added tests/testdata/test_gdb.gdb/a00000001.gdbtablx
Binary file not shown.
Binary file added tests/testdata/test_gdb.gdb/a00000002.gdbtable
Binary file not shown.
Binary file added tests/testdata/test_gdb.gdb/a00000002.gdbtablx
Binary file not shown.
Binary file added tests/testdata/test_gdb.gdb/a00000003.gdbindexes
Binary file not shown.
Binary file added tests/testdata/test_gdb.gdb/a00000003.gdbtable
Binary file not shown.
Binary file added tests/testdata/test_gdb.gdb/a00000003.gdbtablx
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added tests/testdata/test_gdb.gdb/a00000004.gdbindexes
Binary file not shown.
Binary file added tests/testdata/test_gdb.gdb/a00000004.gdbtable
Binary file not shown.
Binary file added tests/testdata/test_gdb.gdb/a00000004.gdbtablx
Binary file not shown.
Binary file added tests/testdata/test_gdb.gdb/a00000004.spx
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added tests/testdata/test_gdb.gdb/a00000005.gdbindexes
Binary file not shown.
Binary file added tests/testdata/test_gdb.gdb/a00000005.gdbtable
Binary file not shown.
Binary file added tests/testdata/test_gdb.gdb/a00000005.gdbtablx
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added tests/testdata/test_gdb.gdb/a00000006.gdbindexes
Binary file not shown.
Binary file added tests/testdata/test_gdb.gdb/a00000006.gdbtable
Binary file not shown.
Binary file added tests/testdata/test_gdb.gdb/a00000006.gdbtablx
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 added tests/testdata/test_gdb.gdb/a00000007.gdbindexes
Binary file not shown.
Binary file added tests/testdata/test_gdb.gdb/a00000007.gdbtable
Binary file not shown.
Binary file added tests/testdata/test_gdb.gdb/a00000007.gdbtablx
Binary file not shown.
Binary file added tests/testdata/test_gdb.gdb/a00000009.gdbindexes
Binary file not shown.
Binary file added tests/testdata/test_gdb.gdb/a00000009.gdbtable
Binary file not shown.
Binary file added tests/testdata/test_gdb.gdb/a00000009.gdbtablx
Binary file not shown.
Binary file added tests/testdata/test_gdb.gdb/a00000009.spx
Binary file not shown.
Binary file added tests/testdata/test_gdb.gdb/a0000000a.gdbindexes
Binary file not shown.
Binary file added tests/testdata/test_gdb.gdb/a0000000a.gdbtable
Binary file not shown.
Binary file added tests/testdata/test_gdb.gdb/a0000000a.gdbtablx
Binary file not shown.
Binary file added tests/testdata/test_gdb.gdb/a0000000a.spx
Binary file not shown.
Binary file added tests/testdata/test_gdb.gdb/a0000000b.gdbindexes
Binary file not shown.
Binary file added tests/testdata/test_gdb.gdb/a0000000b.gdbtable
Binary file not shown.
Binary file added tests/testdata/test_gdb.gdb/a0000000b.gdbtablx
Binary file not shown.
Binary file added tests/testdata/test_gdb.gdb/a0000000b.spx
Binary file not shown.
Binary file added tests/testdata/test_gdb.gdb/gdb
Binary file not shown.
Binary file added tests/testdata/test_gdb.gdb/timestamps
Binary file not shown.

0 comments on commit 4ce2cf1

Please sign in to comment.