Skip to content
Permalink
Browse files

Fix unique values on SQLite (OGR) pk

  • Loading branch information
elpaso committed Feb 19, 2019
1 parent 818611c commit 5d7a7ac97388d5fe96df76faa39ec41c370de539
Showing with 25 additions and 2 deletions.
  1. +3 −2 src/providers/ogr/qgsogrprovider.cpp
  2. +22 −0 tests/src/python/test_provider_ogr_sqlite.py
@@ -3621,8 +3621,9 @@ QSet<QVariant> QgsOgrProvider::uniqueValues( int index, int limit ) const

QByteArray sql = "SELECT DISTINCT " + quotedIdentifier( textEncoding()->fromUnicode( fld.name() ) );

// GPKG fid
if ( mGDALDriverName == QLatin1String( "GPKG" ) && mFirstFieldIsFid && index == 0 )
// GPKG/SQLite fid
if ( ( mGDALDriverName == QLatin1String( "GPKG" ) || mGDALDriverName == QLatin1String( "SQLite" ) )
&& mFirstFieldIsFid && index == 0 )
{
sql += ", " + quotedIdentifier( textEncoding()->fromUnicode( fld.name() ) ) + " AS fid2";
}
@@ -433,6 +433,28 @@ def testBlob(self):
f5 = next(dp.getFeatures())
self.assertEqual(f5.attributes(), [1, 'str', 200, QByteArray(bin_4), QByteArray(bin_3)])

def testUniqueValuesOnFidColumn(self):
"""Test regression #21311 OGR provider returns an empty set for sqlite uniqueValues"""

tmpfile = os.path.join(self.basetestpath, 'testSQLiteUniqueValuesOnFidColumn.db')
ds = ogr.GetDriverByName('SQLite').CreateDataSource(tmpfile)
lyr = ds.CreateLayer('test', geom_type=ogr.wkbPolygon)
lyr.CreateField(ogr.FieldDefn('str_field', ogr.OFTString))
f = ogr.Feature(lyr.GetLayerDefn())
f.SetGeometry(ogr.CreateGeometryFromWkt('POLYGON ((0 0,0 1,1 1,1 0,0 0))'))
f.SetField('str_field', 'one')
lyr.CreateFeature(f)
f = ogr.Feature(lyr.GetLayerDefn())
f.SetGeometry(ogr.CreateGeometryFromWkt('POLYGON ((0 0,0 2,2 2,2 0,0 0))'))
f.SetField('str_field', 'two')
lyr.CreateFeature(f)
f = None
ds = None
vl1 = QgsVectorLayer(tmpfile)
self.assertTrue(vl1.isValid())
self.assertEqual(vl1.uniqueValues(0), {1, 2})
self.assertEqual(vl1.uniqueValues(1), {'one', 'two'})


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

0 comments on commit 5d7a7ac

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