Skip to content

Commit

Permalink
OGR provider: fix uniqueStringsMatching() with GDAL >= 3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault authored and nyalldawson committed Nov 20, 2020
1 parent df4a0fb commit 46067e5
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/core/providers/ogr/qgsogrprovider.cpp
Expand Up @@ -4047,10 +4047,32 @@ QStringList QgsOgrProvider::uniqueStringsMatching( int index, const QString &sub
return results; //not a provider field return results; //not a provider field
} }


// uniqueStringsMatching() is supposed to be case insensitive, so use the
// ILIKE operator when it is available.
// Prior to GDAL 3.1, with OGR SQL, LIKE behaved like ILIKE
bool supportsILIKE = false;
{
QByteArray sql = "SELECT 1 FROM ";
sql += quotedIdentifier( mOgrLayer->name() );
sql += " WHERE 'a' ILIKE 'A' LIMIT 1";
QgsOgrLayerUniquePtr l = mOgrLayer->ExecuteSQL( sql );
if ( l )
{
gdal::ogr_feature_unique_ptr f;
f.reset( l->GetNextFeature() );
supportsILIKE = f != nullptr;
}
}

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


sql += " WHERE " + quotedIdentifier( textEncoding()->fromUnicode( fld.name() ) ) + " LIKE '%" + textEncoding()->fromUnicode( substring ) + "%'"; sql += " WHERE " + quotedIdentifier( textEncoding()->fromUnicode( fld.name() ) );
if ( supportsILIKE )
sql += " ILIKE '%";
else
sql += " LIKE '%";
sql += textEncoding()->fromUnicode( substring ) + "%'";


if ( !mSubsetString.isEmpty() ) if ( !mSubsetString.isEmpty() )
{ {
Expand Down

0 comments on commit 46067e5

Please sign in to comment.