Skip to content

Commit

Permalink
Connectios API fix Postgres char conversion in execSQL
Browse files Browse the repository at this point in the history
Fixes #34806
  • Loading branch information
elpaso committed Mar 2, 2020
1 parent dfa5cb6 commit aac0c92
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/providers/postgres/qgspostgresproviderconnection.cpp
Expand Up @@ -228,7 +228,6 @@ QList<QVariantList> QgsPostgresProviderConnection::executeSqlPrivate( const QStr
if ( typeRes.size() > 0 && typeRes.first().size() > 0 )
{
static const QStringList intTypes = { QStringLiteral( "oid" ),
QStringLiteral( "char" ),
QStringLiteral( "int2" ),
QStringLiteral( "int4" ),
QStringLiteral( "int8" )
Expand Down Expand Up @@ -263,6 +262,10 @@ QList<QVariantList> QgsPostgresProviderConnection::executeSqlPrivate( const QStr
{
vType = QVariant::Bool;
}
else if ( typName == QStringLiteral( "char" ) )
{
vType = QVariant::Char;
}
else
{
QgsDebugMsg( QStringLiteral( "Unhandled PostgreSQL type %1" ).arg( typName ) );
Expand Down
9 changes: 9 additions & 0 deletions tests/src/python/test_qgsproviderconnection_postgres.py
Expand Up @@ -38,6 +38,7 @@ class TestPyQgsProviderConnectionPostgres(unittest.TestCase, TestPyQgsProviderCo
@classmethod
def setUpClass(cls):
"""Run before all tests"""

TestPyQgsProviderConnectionBase.setUpClass()
cls.postgres_conn = "service='qgis_test'"
if 'QGIS_PGTEST_DB' in os.environ:
Expand Down Expand Up @@ -219,6 +220,7 @@ def test_nulls(self):
self.assertEqual(conn.executeSql('SELECT NULL::bool'), [[None]])
self.assertEqual(conn.executeSql('SELECT NULL::text'), [[None]])
self.assertEqual(conn.executeSql('SELECT NULL::bytea'), [[None]])
self.assertEqual(conn.executeSql('SELECT NULL::char'), [[None]])

def test_pk_cols_order(self):
"""Test that PKs are returned in consistent order: see GH #34167"""
Expand All @@ -228,6 +230,13 @@ def test_pk_cols_order(self):
self.assertEqual(conn.table('qgis_test', 'bikes_view').primaryKeyColumns(), ['pk', 'name'])
self.assertEqual(conn.table('qgis_test', 'some_poly_data_view').primaryKeyColumns(), ['pk', 'geom'])

def test_char_type_conversion(self):
"""Test char types: see GH #34806"""

md = QgsProviderRegistry.instance().providerMetadata(self.providerKey)
conn = md.createConnection(self.uri, {})
self.assertEqual(conn.executeSql("SELECT relname, relkind FROM pg_class c, pg_namespace n WHERE n.oid = c.relnamespace AND relname = 'bikes_view' AND c.relkind IN ('t', 'v', 'm')"), [['bikes_view', 'v']])


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

0 comments on commit aac0c92

Please sign in to comment.