Skip to content

Commit

Permalink
[Postgres] Fixes #42778 : don't quote integer array elements to match…
Browse files Browse the repository at this point in the history
… array_out()
  • Loading branch information
troopa81 authored and nyalldawson committed Jun 21, 2021
1 parent e3f9f7b commit 7b3667e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/providers/postgres/qgspostgresconn.cpp
Expand Up @@ -1200,7 +1200,7 @@ static QString quotedList( const QVariantList &list )
}

QString inner = i->toString();
if ( inner.startsWith( '{' ) )
if ( inner.startsWith( '{' ) || i->type() == QVariant::Int || i->type() == QVariant::LongLong )
{
ret.append( inner );
}
Expand Down
2 changes: 1 addition & 1 deletion tests/src/providers/testqgspostgresconn.cpp
Expand Up @@ -82,7 +82,7 @@ class TestQgsPostgresConn: public QObject
QVariantList list;
list << 1 << -5;
const QString actual = QgsPostgresConn::quotedValue( list );
QCOMPARE( actual, QString( "E'{\"1\",\"-5\"}'" ) );
QCOMPARE( actual, QString( "E'{1,-5}'" ) );
}

void quotedValue2DimArray()
Expand Down
26 changes: 26 additions & 0 deletions tests/src/python/test_provider_postgres.py
Expand Up @@ -3106,6 +3106,32 @@ def _get_layer(sql):
self.assertEqual(l.fields().count(), 3)
self.assertEqual([f.name() for f in l.fields()], ['__rid__', 'id', 'id (2)'])

def testPkeyIntArray(self):
"""
Test issue #42778 when pkey is an int array
"""
md = QgsProviderRegistry.instance().providerMetadata("postgres")
conn = md.createConnection(self.dbconn, {})
conn.executeSql('DROP TABLE IF EXISTS public.test_pkey_intarray')
conn.executeSql('CREATE TABLE public.test_pkey_intarray (id _int8 PRIMARY KEY, name VARCHAR(64))')
conn.executeSql("""INSERT INTO public.test_pkey_intarray (id, name) VALUES('{0,0,19111815}', 'test')""")

uri = QgsDataSourceUri(self.dbconn +
' sslmode=disable key=\'id\' table="public"."test_pkey_intarray" sql=')
vl = QgsVectorLayer(uri.uri(), 'test', 'postgres')
self.assertTrue(vl.isValid())

feat = next(vl.getFeatures())
self.assertTrue(feat.isValid())
self.assertEqual(feat["name"], "test")

fid = feat.id()
self.assertTrue(fid > 0)

feat = vl.getFeature(fid)
self.assertTrue(feat.isValid())
self.assertEqual(feat["name"], "test")


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

0 comments on commit 7b3667e

Please sign in to comment.