Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Quote field name identifiers in ORDER BY and MAX/MIN queries
Fixes #21100
  • Loading branch information
elpaso committed Feb 2, 2019
1 parent 9a8c0d2 commit b514266
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/providers/ogr/qgsogrprovider.cpp
Expand Up @@ -3604,7 +3604,7 @@ QSet<QVariant> QgsOgrProvider::uniqueValues( int index, int limit ) const
sql += " WHERE " + textEncoding()->fromUnicode( mSubsetString );
}

sql += " ORDER BY " + textEncoding()->fromUnicode( fld.name() ) + " ASC"; // quoting of fieldname produces a syntax error
sql += " ORDER BY " + quotedIdentifier( textEncoding()->fromUnicode( fld.name() ) ) + " ASC";

QgsDebugMsg( QStringLiteral( "SQL: %1" ).arg( textEncoding()->toUnicode( sql ) ) );
QgsOgrLayerUniquePtr l = mOgrLayer->ExecuteSQL( sql );
Expand Down Expand Up @@ -3649,7 +3649,7 @@ QStringList QgsOgrProvider::uniqueStringsMatching( int index, const QString &sub
sql += " AND (" + textEncoding()->fromUnicode( mSubsetString ) + ')';
}

sql += " ORDER BY " + textEncoding()->fromUnicode( fld.name() ) + " ASC"; // quoting of fieldname produces a syntax error
sql += " ORDER BY " + quotedIdentifier( textEncoding()->fromUnicode( fld.name() ) ) + " ASC";

QgsDebugMsg( QStringLiteral( "SQL: %1" ).arg( textEncoding()->toUnicode( sql ) ) );
QgsOgrLayerUniquePtr l = mOgrLayer->ExecuteSQL( sql );
Expand Down Expand Up @@ -3681,7 +3681,7 @@ QVariant QgsOgrProvider::minimumValue( int index ) const
QgsField fld = mAttributeFields.at( index );

// Don't quote column name (see https://trac.osgeo.org/gdal/ticket/5799#comment:9)
QByteArray sql = "SELECT MIN(" + textEncoding()->fromUnicode( fld.name() );
QByteArray sql = "SELECT MIN(" + quotedIdentifier( textEncoding()->fromUnicode( fld.name() ) );
sql += ") FROM " + quotedIdentifier( mOgrLayer->name() );

if ( !mSubsetString.isEmpty() )
Expand Down Expand Up @@ -3715,7 +3715,7 @@ QVariant QgsOgrProvider::maximumValue( int index ) const
QgsField fld = mAttributeFields.at( index );

// Don't quote column name (see https://trac.osgeo.org/gdal/ticket/5799#comment:9)
QByteArray sql = "SELECT MAX(" + textEncoding()->fromUnicode( fld.name() );
QByteArray sql = "SELECT MAX(" + quotedIdentifier( textEncoding()->fromUnicode( fld.name() ) );
sql += ") FROM " + quotedIdentifier( mOgrLayer->name() );

if ( !mSubsetString.isEmpty() )
Expand Down
10 changes: 10 additions & 0 deletions tests/src/python/test_provider_ogr_gpkg.py
Expand Up @@ -1293,6 +1293,16 @@ def testJson(self):
self.assertNotEqual(vl.fields().indexFromName('json_content2'), -1)
self.assertEqual(vl.fields().indexFromName('json_content3'), -1)

def test_quote_identifier(self):
"""Regression #21100"""

tmpfile = os.path.join(self.basetestpath, 'bug21100-wierd_field_names.gpkg')
shutil.copy(os.path.join(unitTestDataPath(''), 'bug21100-wierd_field_names.gpkg'), tmpfile)
vl = QgsVectorLayer('{}|layerid=0'.format(tmpfile), 'foo', 'ogr')
self.assertTrue(vl.isValid())
for i in range(1, len(vl.fields())):
self.assertEqual(vl.uniqueValues(i), {'a', 'b', 'c'})


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

0 comments on commit b514266

Please sign in to comment.