mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-15 00:04:00 -04:00
Merge pull request #9071 from elpaso/bugfix-21100-ogrprovider-missing-quoteidentifier
Quote field name identifiers in ORDER BY and MAX/MIN queries
This commit is contained in:
commit
3c9b6e5e4f
@ -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 );
|
||||
@ -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 );
|
||||
@ -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() )
|
||||
@ -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() )
|
||||
|
@ -1293,6 +1293,16 @@ class TestPyQgsOGRProviderGpkg(unittest.TestCase):
|
||||
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()
|
||||
|
BIN
tests/testdata/bug21100-wierd_field_names.gpkg
vendored
Normal file
BIN
tests/testdata/bug21100-wierd_field_names.gpkg
vendored
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user