From ef6a7e5d9ff324e9ba69cb6ecbe710ded73beb23 Mon Sep 17 00:00:00 2001 From: Alessandro Pasotti Date: Wed, 23 Sep 2020 15:31:12 +0200 Subject: [PATCH] Expose PG unrestricted empty tables as apatial items also fix test --- .../postgres/qgspostgresdataitems.cpp | 13 +++++-- .../qgspostgresproviderconnection.cpp | 34 +++++++++---------- .../test_qgsproviderconnection_postgres.py | 1 + 3 files changed, 28 insertions(+), 20 deletions(-) diff --git a/src/providers/postgres/qgspostgresdataitems.cpp b/src/providers/postgres/qgspostgresdataitems.cpp index 746bd605884..538b54ca387 100644 --- a/src/providers/postgres/qgspostgresdataitems.cpp +++ b/src/providers/postgres/qgspostgresdataitems.cpp @@ -416,10 +416,16 @@ QVector QgsPGSchemaItem::createChildren() { if ( dontResolveType ) { - //QgsDebugMsg( QStringLiteral( "skipping column %1.%2 without type constraint" ).arg( layerProperty.schemaName ).arg( layerProperty.tableName ) ); + QgsDebugMsgLevel( QStringLiteral( "skipping column %1.%2 without type constraint" ).arg( layerProperty.schemaName ).arg( layerProperty.tableName ), 2 ); continue; } + // If the table is empty there is no way we can retrieve layer types, let's make a copy and restore it + QgsPostgresLayerProperty propertyCopy { layerProperty }; conn->retrieveLayerTypes( layerProperty, estimatedMetadata ); + if ( layerProperty.size() == 0 ) + { + layerProperty = propertyCopy; + } } for ( int i = 0; i < layerProperty.size(); i++ ) @@ -512,8 +518,9 @@ QgsPGLayerItem *QgsPGSchemaItem::createLayer( QgsPostgresLayerProperty layerProp break; default: if ( !layerProperty.geometryColName.isEmpty() ) - return nullptr; - + { + QgsDebugMsgLevel( QStringLiteral( "Adding layer item %1.%2 without type constraint as geometryless table" ).arg( layerProperty.schemaName ).arg( layerProperty.tableName ), 2 ); + } layerType = QgsLayerItem::TableLayer; tip = tr( "as geometryless table" ); } diff --git a/src/providers/postgres/qgspostgresproviderconnection.cpp b/src/providers/postgres/qgspostgresproviderconnection.cpp index 3d2255fb226..2e6ec459f46 100644 --- a/src/providers/postgres/qgspostgresproviderconnection.cpp +++ b/src/providers/postgres/qgspostgresproviderconnection.cpp @@ -671,32 +671,32 @@ QgsFields QgsPostgresProviderConnection::fields( const QString &schema, const QS // This table might expose multiple geometry columns (different geom type or SRID) // but we are only interested in fields here, so let's pick the first one. TableProperty tableInfo { table( schema, tableName ) }; - if ( tableInfo.geometryColumnTypes().count( ) > 1 ) + try { - try + QgsDataSourceUri tUri { tableUri( schema, tableName ) }; + if ( tableInfo.geometryColumnTypes().count( ) > 1 ) { - QgsDataSourceUri tUri { tableUri( schema, tableName ) }; TableProperty::GeometryColumnType geomCol { tableInfo.geometryColumnTypes().first() }; tUri.setGeometryColumn( tableInfo.geometryColumn() ); tUri.setWkbType( geomCol.wkbType ); tUri.setSrid( QString::number( geomCol.crs.postgisSrid() ) ); - if ( tableInfo.primaryKeyColumns().count() > 0 ) - { - tUri.setKeyColumn( tableInfo.primaryKeyColumns().first() ); - } - tUri.setParam( QStringLiteral( "checkPrimaryKeyUnicity" ), QLatin1String( "0" ) ); - QgsVectorLayer::LayerOptions options { false, true }; - options.skipCrsValidation = true; - QgsVectorLayer vl { tUri.uri(), QStringLiteral( "temp_layer" ), mProviderKey, options }; - if ( vl.isValid() ) - { - return vl.fields(); - } } - catch ( QgsProviderConnectionException & ) + if ( tableInfo.primaryKeyColumns().count() > 0 ) { - // fall-through + tUri.setKeyColumn( tableInfo.primaryKeyColumns().first() ); } + tUri.setParam( QStringLiteral( "checkPrimaryKeyUnicity" ), QLatin1String( "0" ) ); + QgsVectorLayer::LayerOptions options { false, true }; + options.skipCrsValidation = true; + QgsVectorLayer vl { tUri.uri(), QStringLiteral( "temp_layer" ), mProviderKey, options }; + if ( vl.isValid() ) + { + return vl.fields(); + } + } + catch ( QgsProviderConnectionException & ) + { + // fall-through } throw ex; } diff --git a/tests/src/python/test_qgsproviderconnection_postgres.py b/tests/src/python/test_qgsproviderconnection_postgres.py index df6ef0267fa..5f2c92bb16c 100644 --- a/tests/src/python/test_qgsproviderconnection_postgres.py +++ b/tests/src/python/test_qgsproviderconnection_postgres.py @@ -334,6 +334,7 @@ IMPORT FOREIGN SCHEMA qgis_test LIMIT TO ( "someData" ) md = QgsProviderRegistry.instance().providerMetadata('postgres') conn = md.createConnection(self.uri, {}) + self.assertTrue(conn.tableExists('public', 'raster_columns')) fields = conn.fields("public", "raster_columns") self.assertEqual(fields.names(), [ 'r_table_catalog',