Skip to content

Commit

Permalink
Merge pull request #9071 from elpaso/bugfix-21100-ogrprovider-missing…
Browse files Browse the repository at this point in the history
…-quoteidentifier

Quote field name identifiers in ORDER BY and MAX/MIN queries
  • Loading branch information
elpaso authored Feb 4, 2019
2 parents 8ac762e + 0201172 commit 3c9b6e5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/providers/ogr/qgsogrprovider.cpp
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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') # spellok
shutil.copy(os.path.join(unitTestDataPath(''), 'bug21100-wierd_field_names.gpkg'), tmpfile) # spellok
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()
Binary file added tests/testdata/bug21100-wierd_field_names.gpkg
Binary file not shown.

0 comments on commit 3c9b6e5

Please sign in to comment.