Skip to content
Permalink
Browse files

PG: add encodeUri implementation + fixes and tests

  • Loading branch information
elpaso committed Mar 11, 2020
1 parent c4a017b commit ff120814a644e514e0951c66152562b9d93379c0
@@ -5342,24 +5342,84 @@ QGISEXTERN QgsProviderMetadata *providerMetadataFactory()
QVariantMap QgsPostgresProviderMetadata::decodeUri( const QString &uri )
{
const QgsDataSourceUri dsUri { uri };
return
{
{ QStringLiteral( "dbname" ), dsUri.database() },
{ QStringLiteral( "host" ), dsUri.host() },
{ QStringLiteral( "port" ), dsUri.port() },
{ QStringLiteral( "service" ), dsUri.service() },
{ QStringLiteral( "username" ), dsUri.username() },
{ QStringLiteral( "password" ), dsUri.password() },
{ QStringLiteral( "authcfg" ), dsUri.authConfigId() },
{ QStringLiteral( "type" ), dsUri.wkbType() },
{ QStringLiteral( "selectatid" ), dsUri.selectAtIdDisabled() },
{ QStringLiteral( "table" ), dsUri.table() },
{ QStringLiteral( "schema" ), dsUri.schema() },
{ QStringLiteral( "key" ), dsUri.keyColumn() },
{ QStringLiteral( "srid" ), dsUri.srid() },
{ QStringLiteral( "estimatedmetadata" ), dsUri.useEstimatedMetadata() },
{ QStringLiteral( "sslmode" ), dsUri.sslMode() },
{ QStringLiteral( "sql" ), dsUri.sql() },
{ QStringLiteral( "geometrycolumn" ), dsUri.geometryColumn() },
};
QVariantMap uriParts;

if ( ! dsUri.database().isEmpty() )
uriParts[ QStringLiteral( "dbname" ) ] = dsUri.database();
if ( ! dsUri.host().isEmpty() )
uriParts[ QStringLiteral( "host" ) ] = dsUri.host();
if ( ! dsUri.port().isEmpty() )
uriParts[ QStringLiteral( "port" ) ] = dsUri.port();
if ( ! dsUri.service().isEmpty() )
uriParts[ QStringLiteral( "service" ) ] = dsUri.service();
if ( ! dsUri.username().isEmpty() )
uriParts[ QStringLiteral( "username" ) ] = dsUri.username();
if ( ! dsUri.authConfigId().isEmpty() )
uriParts[ QStringLiteral( "authcfg" ) ] = dsUri.authConfigId();
if ( dsUri.wkbType() != QgsWkbTypes::Type::Unknown )
uriParts[ QStringLiteral( "type" ) ] = dsUri.wkbType();

uriParts[ QStringLiteral( "selectatid" ) ] = dsUri.selectAtIdDisabled();

if ( ! dsUri.table().isEmpty() )
uriParts[ QStringLiteral( "table" ) ] = dsUri.table();
if ( ! dsUri.schema().isEmpty() )
uriParts[ QStringLiteral( "schema" ) ] = dsUri.schema();
if ( ! dsUri.keyColumn().isEmpty() )
uriParts[ QStringLiteral( "key" ) ] = dsUri.keyColumn();
if ( ! dsUri.srid().isEmpty() )
uriParts[ QStringLiteral( "srid" ) ] = dsUri.srid();

uriParts[ QStringLiteral( "estimatedmetadata" ) ] = dsUri.useEstimatedMetadata();
uriParts[ QStringLiteral( "sslmode" ) ] = dsUri.sslMode();

if ( ! dsUri.sql().isEmpty() )
uriParts[ QStringLiteral( "sql" ) ] = dsUri.sql();
if ( ! dsUri.geometryColumn().isEmpty() )
uriParts[ QStringLiteral( "geometrycolumn" ) ] = dsUri.geometryColumn();

return uriParts;
}


QString QgsPostgresProviderMetadata::encodeUri( const QVariantMap &parts )
{
QgsDataSourceUri dsUri;
if ( parts.contains( QStringLiteral( "dbname" ) ) )
dsUri.setDatabase( parts.value( QStringLiteral( "dbname" ) ).toString() );
if ( parts.contains( QStringLiteral( "port" ) ) )
dsUri.setParam( QStringLiteral( "port" ), parts.value( QStringLiteral( "port" ) ).toString() );
if ( parts.contains( QStringLiteral( "host" ) ) )
dsUri.setParam( QStringLiteral( "host" ), parts.value( QStringLiteral( "host" ) ).toString() );
if ( parts.contains( QStringLiteral( "service" ) ) )
dsUri.setParam( QStringLiteral( "service" ), parts.value( QStringLiteral( "service" ) ).toString() );
if ( parts.contains( QStringLiteral( "username" ) ) )
dsUri.setUsername( parts.value( QStringLiteral( "username" ) ).toString() );
if ( parts.contains( QStringLiteral( "password" ) ) )
dsUri.setPassword( parts.value( QStringLiteral( "password" ) ).toString() );
if ( parts.contains( QStringLiteral( "authcfg" ) ) )
dsUri.setAuthConfigId( parts.value( QStringLiteral( "authcfg" ) ).toString() );
if ( parts.contains( QStringLiteral( "type" ) ) )
dsUri.setParam( QStringLiteral( "type" ), QgsWkbTypes::displayString( static_cast<QgsWkbTypes::Type>( parts.value( QStringLiteral( "type" ) ).toInt() ) ) );
if ( parts.contains( QStringLiteral( "selectatid" ) ) )
dsUri.setParam( QStringLiteral( "selectatid" ), parts.value( QStringLiteral( "selectatid" ) ).toString() );
if ( parts.contains( QStringLiteral( "table" ) ) )
dsUri.setTable( parts.value( QStringLiteral( "table" ) ).toString() );
if ( parts.contains( QStringLiteral( "schema" ) ) )
dsUri.setSchema( parts.value( QStringLiteral( "schema" ) ).toString() );
if ( parts.contains( QStringLiteral( "key" ) ) )
dsUri.setParam( QStringLiteral( "key" ), parts.value( QStringLiteral( "key" ) ).toString() );
if ( parts.contains( QStringLiteral( "srid" ) ) )
dsUri.setSrid( parts.value( QStringLiteral( "srid" ) ).toString() );
if ( parts.contains( QStringLiteral( "estimatedmetadata" ) ) )
dsUri.setParam( QStringLiteral( "estimatedmetadata" ), parts.value( QStringLiteral( "estimatedmetadata" ) ).toString() );
if ( parts.contains( QStringLiteral( "sslmode" ) ) )
dsUri.setParam( QStringLiteral( "sslmode" ), QgsDataSourceUri::encodeSslMode( static_cast<QgsDataSourceUri::SslMode>( parts.value( QStringLiteral( "sslmode" ) ).toInt( ) ) ) );
if ( parts.contains( QStringLiteral( "sql" ) ) )
dsUri.setSql( parts.value( QStringLiteral( "sql" ) ).toString() );
if ( parts.contains( QStringLiteral( "checkPrimaryKeyUnicity" ) ) )
dsUri.setParam( QStringLiteral( "checkPrimaryKeyUnicity" ), parts.value( QStringLiteral( "checkPrimaryKeyUnicity" ) ).toString() );
if ( parts.contains( QStringLiteral( "geometrycolumn" ) ) )
dsUri.setGeometryColumn( parts.value( QStringLiteral( "geometrycolumn" ) ).toString() );
return dsUri.uri();
}
@@ -583,6 +583,7 @@ class QgsPostgresProviderMetadata final: public QgsProviderMetadata
void initProvider() override;
void cleanupProvider() override;
QVariantMap decodeUri( const QString &uri ) override;
QString encodeUri( const QVariantMap &parts ) override;
};

// clazy:excludeall=qstring-allocations
@@ -43,7 +43,8 @@
QgsCoordinateReferenceSystem,
QgsProject,
QgsWkbTypes,
QgsGeometry
QgsGeometry,
QgsProviderRegistry
)
from qgis.gui import QgsGui, QgsAttributeForm
from qgis.PyQt.QtCore import QDate, QTime, QDateTime, QVariant, QDir, QObject, QByteArray
@@ -1502,6 +1503,64 @@ def testDefaultValuesAndClauses(self):
self.assertEqual(feature.attribute(4), 123)
self.assertEqual(feature.attribute(5), 'My default')

def testEncodeDecodeUri(self):
"""Test PG encode/decode URI"""

md = QgsProviderRegistry.instance().providerMetadata('postgres')
self.assertEqual(md.decodeUri('dbname=\'qgis_tests\' host=localhost port=5432 user=\'myuser\' sslmode=disable estimatedmetadata=true srid=3067 table="public"."basic_map_tiled" (rast)'),
{'dbname': 'qgis_tests',
'estimatedmetadata': True,
'geometrycolumn': 'rast',
'host': 'localhost',
'port': '5432',
'schema': 'public',
'selectatid': False,
'srid': '3067',
'sslmode': 1,
'table': 'basic_map_tiled',
'username': 'myuser'})

self.assertEqual(md.decodeUri('dbname=\'qgis_tests\' host=localhost port=5432 user=\'myuser\' sslmode=disable key=\'id\' estimatedmetadata=true srid=3763 type=MultiPolygon checkPrimaryKeyUnicity=\'1\' table="public"."copas1" (geom)'),
{'dbname': 'qgis_tests',
'estimatedmetadata': True,
'geometrycolumn': 'geom',
'host': 'localhost',
'key': 'id',
'port': '5432',
'schema': 'public',
'selectatid': False,
'srid': '3763',
'sslmode': 1,
'table': 'copas1',
'type': 6,
'username': 'myuser'})

self.assertEqual(md.encodeUri({'dbname': 'qgis_tests',
'estimatedmetadata': True,
'geometrycolumn': 'geom',
'host': 'localhost',
'key': 'id',
'port': '5432',
'schema': 'public',
'selectatid': False,
'srid': '3763',
'sslmode': 1,
'table': 'copas1',
'type': 6,
'username': 'myuser'}), "dbname='qgis_tests' user='myuser' srid=3763 estimatedmetadata='true' host='localhost' key='id' port='5432' selectatid='false' sslmode='disable' type='MultiPolygon' table=\"public\".\"copas1\" (geom)")

self.assertEqual(md.encodeUri({'dbname': 'qgis_tests',
'estimatedmetadata': True,
'geometrycolumn': 'rast',
'host': 'localhost',
'port': '5432',
'schema': 'public',
'selectatid': False,
'srid': '3067',
'sslmode': 1,
'table': 'basic_map_tiled',
'username': 'myuser'}), "dbname='qgis_tests' user='myuser' srid=3067 estimatedmetadata='true' host='localhost' port='5432' selectatid='false' sslmode='disable' table=\"public\".\"basic_map_tiled\" (rast)")


class TestPyQgsPostgresProviderCompoundKey(unittest.TestCase, ProviderTestCase):

0 comments on commit ff12081

Please sign in to comment.
You can’t perform that action at this time.