Skip to content

Commit 3c9b6e5

Browse files
authored
Merge pull request #9071 from elpaso/bugfix-21100-ogrprovider-missing-quoteidentifier
Quote field name identifiers in ORDER BY and MAX/MIN queries
2 parents 8ac762e + 0201172 commit 3c9b6e5

3 files changed

Lines changed: 14 additions & 4 deletions

File tree

‎src/providers/ogr/qgsogrprovider.cpp‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3604,7 +3604,7 @@ QSet<QVariant> QgsOgrProvider::uniqueValues( int index, int limit ) const
36043604
sql += " WHERE " + textEncoding()->fromUnicode( mSubsetString );
36053605
}
36063606

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

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

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

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

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

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

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

37213721
if ( !mSubsetString.isEmpty() )

‎tests/src/python/test_provider_ogr_gpkg.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,6 +1293,16 @@ def testJson(self):
12931293
self.assertNotEqual(vl.fields().indexFromName('json_content2'), -1)
12941294
self.assertEqual(vl.fields().indexFromName('json_content3'), -1)
12951295

1296+
def test_quote_identifier(self):
1297+
"""Regression #21100"""
1298+
1299+
tmpfile = os.path.join(self.basetestpath, 'bug21100-wierd_field_names.gpkg') # spellok
1300+
shutil.copy(os.path.join(unitTestDataPath(''), 'bug21100-wierd_field_names.gpkg'), tmpfile) # spellok
1301+
vl = QgsVectorLayer('{}|layerid=0'.format(tmpfile), 'foo', 'ogr')
1302+
self.assertTrue(vl.isValid())
1303+
for i in range(1, len(vl.fields())):
1304+
self.assertEqual(vl.uniqueValues(i), {'a', 'b', 'c'})
1305+
12961306

12971307
if __name__ == '__main__':
12981308
unittest.main()
96 KB
Binary file not shown.

0 commit comments

Comments
 (0)