Skip to content

Commit

Permalink
QGIS expressions are not sensitive to the case of field names, so
Browse files Browse the repository at this point in the history
mimic this same insensitivity when compiling expressions for providers

Avoids the expression compilation failing whenever a referenced
field in an expression does not exactly match the layer's field
name case.

(cherry picked from commit 52528c5)
  • Loading branch information
nyalldawson committed Mar 19, 2021
1 parent f892b3c commit 69e70d7
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/core/qgssqlexpressioncompiler.cpp
Expand Up @@ -325,11 +325,14 @@ QgsSqlExpressionCompiler::Result QgsSqlExpressionCompiler::compileNode( const Qg
{
const QgsExpressionNodeColumnRef *n = static_cast<const QgsExpressionNodeColumnRef *>( node );

if ( mFields.indexFromName( n->name() ) == -1 )
// QGIS expressions don't care about case sensitive field naming, so we match case insensitively here to the
// layer's fields and then retrieve the actual case of the field name for use in the compilation
const int fieldIndex = mFields.lookupField( n->name() );
if ( fieldIndex == -1 )
// Not a provider field
return Fail;

result = quotedIdentifier( n->name() );
result = quotedIdentifier( mFields.at( fieldIndex ).name() );

return Complete;
}
Expand Down
2 changes: 2 additions & 0 deletions tests/src/python/featuresourcetestbase.py
Expand Up @@ -168,6 +168,8 @@ def runGetFeatureTests(self, source):
self.assert_query(source, '"name" NOT ILIKE \'QGIS\'', [1, 2, 3, 4])
self.assert_query(source, '"name" NOT ILIKE \'pEAR\'', [1, 2, 4])
self.assert_query(source, 'name = \'Apple\'', [2])
# field names themselves are NOT case sensitive -- QGIS expressions don't care about this
self.assert_query(source, '\"NaMe\" = \'Apple\'', [2])
self.assert_query(source, 'name <> \'Apple\'', [1, 3, 4])
self.assert_query(source, 'name = \'apple\'', [])
self.assert_query(source, '"name" <> \'apple\'', [1, 2, 3, 4])
Expand Down
1 change: 1 addition & 0 deletions tests/src/python/test_provider_mssql.py
Expand Up @@ -130,6 +130,7 @@ def partiallyCompiledFilters(self):
filters = set([
'name ILIKE \'QGIS\'',
'name = \'Apple\'',
'\"NaMe\" = \'Apple\'',
'name = \'apple\'',
'name LIKE \'Apple\'',
'name LIKE \'aPple\'',
Expand Down
1 change: 1 addition & 0 deletions tests/src/python/test_provider_shapefile.py
Expand Up @@ -227,6 +227,7 @@ def uncompiledFilters(self):
def partiallyCompiledFilters(self):
return set(['name = \'Apple\'',
'name = \'apple\'',
'\"NaMe\" = \'Apple\'',
'name LIKE \'Apple\'',
'name LIKE \'aPple\'',
'name LIKE \'Ap_le\'',
Expand Down

0 comments on commit 69e70d7

Please sign in to comment.