Expose PG unrestricted empty tables as apatial items

also fix test
This commit is contained in:
Alessandro Pasotti 2020-09-23 15:31:12 +02:00 committed by Nyall Dawson
parent 03e42892b0
commit ef6a7e5d9f
3 changed files with 28 additions and 20 deletions

View File

@ -416,10 +416,16 @@ QVector<QgsDataItem *> 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" );
}

View File

@ -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;
}

View File

@ -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',