Skip to content
Permalink
Browse files

[OGR provider] Support full SELECT subset string

This will be useful for DBManager SQL request layer.
  • Loading branch information
rouault committed Oct 18, 2016
1 parent 2655ced commit 68cb04a549d86bfba85a52fbe8eea6d8f184a33d
Showing with 30 additions and 2 deletions.
  1. +8 −2 src/providers/ogr/qgsogrprovider.cpp
  2. +22 −0 tests/src/python/test_provider_ogr_gpkg.py
@@ -3249,8 +3249,14 @@ OGRLayerH QgsOgrProviderUtils::setSubsetString( OGRLayerH layer, OGRDataSourceH
layerName = encoding->fromUnicode( modifiedLayerName );
}
}
QByteArray sql = "SELECT * FROM " + quotedIdentifier( layerName, ogrDriverName );
sql += " WHERE " + encoding->fromUnicode( subsetString );
QByteArray sql;
if ( subsetString.startsWith( "SELECT ", Qt::CaseInsensitive ) )
sql = encoding->fromUnicode( subsetString );
else
{
sql = "SELECT * FROM " + quotedIdentifier( layerName, ogrDriverName );
sql += " WHERE " + encoding->fromUnicode( subsetString );
}

QgsDebugMsg( QString( "SQL: %1" ).arg( encoding->toUnicode( sql ) ) );
return OGR_DS_ExecuteSQL( ds, sql.constData(), nullptr, nullptr );
@@ -199,5 +199,27 @@ def testGeopackageExtentUpdate(self):
self.assertTrue(QgsGeometry.compare(provider_extent.asPolygon()[0], reference.asPolygon()[0], 0.00001),
provider_extent.asPolygon()[0])

def testSelectSubsetString(self):

tmpfile = os.path.join(self.basetestpath, 'testSelectSubsetString.gpkg')
ds = ogr.GetDriverByName('GPKG').CreateDataSource(tmpfile)
lyr = ds.CreateLayer('test', geom_type=ogr.wkbMultiPolygon)
lyr.CreateField(ogr.FieldDefn('foo', ogr.OFTString))
f = ogr.Feature(lyr.GetLayerDefn())
f['foo'] = 'bar'
lyr.CreateFeature(f)
f = None
f = ogr.Feature(lyr.GetLayerDefn())
f['foo'] = 'baz'
lyr.CreateFeature(f)
f = None
ds = None

vl = QgsVectorLayer('{}|layerid=0'.format(tmpfile), 'test', 'ogr')
vl.setSubsetString("SELECT fid, foo FROM test WHERE foo = 'baz'")
got = [feat for feat in vl.getFeatures()]
self.assertEqual(len(got), 1)


if __name__ == '__main__':
unittest.main()

0 comments on commit 68cb04a

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