Skip to content
Permalink
Browse files

Expose PG unrestricted empty tables as apatial items

also fix test
  • Loading branch information
elpaso authored and nyalldawson committed Sep 24, 2020
1 parent 03e4289 commit ef6a7e5d9ff324e9ba69cb6ecbe710ded73beb23
@@ -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" );
}
@@ -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;
}
@@ -334,6 +334,7 @@ def test_fields_no_pk(self):

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

0 comments on commit ef6a7e5

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