PG provider connection API: convert to bools

Fixes #33130
This commit is contained in:
Alessandro Pasotti 2019-11-28 18:58:07 +01:00
parent 6ca436d1fd
commit 8d9565099a
2 changed files with 14 additions and 1 deletions

View File

@ -294,7 +294,12 @@ QList<QVariantList> QgsPostgresProviderConnection::executeSqlPrivate( const QStr
{
const QVariant::Type vType { typeMap.value( colIdx, QVariant::Type::String ) };
QVariant val { res.PQgetvalue( rowIdx, colIdx ) };
if ( val.canConvert( static_cast<int>( vType ) ) )
// Special case for bools: 'f' and 't'
if ( vType == QVariant::Bool )
{
val = val.toString() == 't';
}
else if ( val.canConvert( static_cast<int>( vType ) ) )
{
val.convert( static_cast<int>( vType ) );
}

View File

@ -177,6 +177,14 @@ class TestPyQgsProviderConnectionPostgres(unittest.TestCase, TestPyQgsProviderCo
self.assertFalse('Raster2' in table_names)
self.assertTrue('Raster1' in table_names)
def test_true_false(self):
"""Test returned values from BOOL queries"""
md = QgsProviderRegistry.instance().providerMetadata(self.providerKey)
conn = md.createConnection(self.uri, {})
self.assertEqual(conn.executeSql('SELECT FALSE'), [[False]])
self.assertEqual(conn.executeSql('SELECT TRUE'), [[True]])
if __name__ == '__main__':
unittest.main()