Skip to content

Commit

Permalink
Merge pull request #32865 from elpaso/followup-32487
Browse files Browse the repository at this point in the history
Fix wrong check validity when fixing broken deps
  • Loading branch information
elpaso committed Nov 14, 2019
2 parents a059c36 + ca74d47 commit 0018190
Showing 1 changed file with 65 additions and 68 deletions.
133 changes: 65 additions & 68 deletions src/app/qgisapp.cpp
Expand Up @@ -1990,91 +1990,88 @@ void QgisApp::checkVectorLayerDependencies( QgsVectorLayer *vl )
const auto constDependencies { findBrokenWidgetDependencies( vl ) };
for ( const QgsVectorLayerRef &dependency : constDependencies )
{
if ( ! vl || ! vl->isValid() )
// try to aggressively resolve the broken dependencies
bool loaded = false;
const QString providerName { vl->dataProvider()->name() };
QgsProviderMetadata *providerMetadata { QgsProviderRegistry::instance()->providerMetadata( providerName ) };
if ( providerMetadata )
{
// try to aggressively resolve the broken dependencies
bool loaded = false;
const QString providerName { vl->dataProvider()->name() };
QgsProviderMetadata *providerMetadata { QgsProviderRegistry::instance()->providerMetadata( providerName ) };
if ( providerMetadata )
// Retrieve the DB connection (if any)
std::unique_ptr< QgsAbstractDatabaseProviderConnection > conn { static_cast<QgsAbstractDatabaseProviderConnection *>( providerMetadata->createConnection( vl->dataProvider()->uri().uri(), {} ) ) };
if ( conn )
{
// Retrieve the DB connection (if any)
std::unique_ptr< QgsAbstractDatabaseProviderConnection > conn { static_cast<QgsAbstractDatabaseProviderConnection *>( providerMetadata->createConnection( vl->dataProvider()->uri().uri(), {} ) ) };
if ( conn )
{
QString tableSchema;
QString tableName;
const QVariantMap sourceParts { providerMetadata->decodeUri( dependency.source ) };
QString tableSchema;
QString tableName;
const QVariantMap sourceParts { providerMetadata->decodeUri( dependency.source ) };

// This part should really be abstracted out to the connection classes or to the providers directly.
// Different providers decode the uri differently, for example we don't get the table name out of OGR
// but the layerName/layerId instead, so let's try different approaches
// This part should really be abstracted out to the connection classes or to the providers directly.
// Different providers decode the uri differently, for example we don't get the table name out of OGR
// but the layerName/layerId instead, so let's try different approaches

// This works for GPKG
tableName = sourceParts.value( QStringLiteral( "layerName" ) ).toString();
// This works for GPKG
tableName = sourceParts.value( QStringLiteral( "layerName" ) ).toString();

// This works for PG and spatialite
if ( tableName.isEmpty() )
{
tableName = sourceParts.value( QStringLiteral( "table" ) ).toString();
tableSchema = sourceParts.value( QStringLiteral( "schema" ) ).toString();
}
// This works for PG and spatialite
if ( tableName.isEmpty() )
{
tableName = sourceParts.value( QStringLiteral( "table" ) ).toString();
tableSchema = sourceParts.value( QStringLiteral( "schema" ) ).toString();
}

// Helper to find layers in connections
auto layerFinder = [ &conn, &dependency, &providerName ]( const QString & tableSchema, const QString & tableName ) -> bool
// Helper to find layers in connections
auto layerFinder = [ &conn, &dependency, &providerName ]( const QString & tableSchema, const QString & tableName ) -> bool
{
// First try the current schema (or no schema if it's not supported from the provider)
try
{
// First try the current schema (or no schema if it's not supported from the provider)
try
const QString layerUri { conn->tableUri( tableSchema, tableName )};
// Load it!
std::unique_ptr< QgsVectorLayer > newVl = qgis::make_unique< QgsVectorLayer >( layerUri, dependency.name, providerName );
if ( newVl->isValid() )
{
const QString layerUri { conn->tableUri( tableSchema, tableName )};
// Load it!
std::unique_ptr< QgsVectorLayer > newVl = qgis::make_unique< QgsVectorLayer >( layerUri, dependency.name, providerName );
if ( newVl->isValid() )
{
QgsProject::instance()->addMapLayer( newVl.release() );
return true;
}
QgsProject::instance()->addMapLayer( newVl.release() );
return true;
}
catch ( QgsProviderConnectionException & )
{
// Do nothing!
}
return false;
};
}
catch ( QgsProviderConnectionException & )
{
// Do nothing!
}
return false;
};

loaded = layerFinder( tableSchema, tableName );
loaded = layerFinder( tableSchema, tableName );

// Try different schemas
if ( ! loaded && conn->capabilities().testFlag( QgsAbstractDatabaseProviderConnection::Capability::Schemas ) && ! tableSchema.isEmpty() )
// Try different schemas
if ( ! loaded && conn->capabilities().testFlag( QgsAbstractDatabaseProviderConnection::Capability::Schemas ) && ! tableSchema.isEmpty() )
{
const QStringList schemas { conn->schemas() };
for ( const QString &schemaName : schemas )
{
const QStringList schemas { conn->schemas() };
for ( const QString &schemaName : schemas )
if ( schemaName != tableSchema )
{
if ( schemaName != tableSchema )
{
loaded = layerFinder( schemaName, tableName );
}
if ( loaded )
{
break;
}
loaded = layerFinder( schemaName, tableName );
}
if ( loaded )
{
break;
}
}
}
}
if ( ! loaded )
{
const QString msg { tr( "layer '%1' requires layer '%2' to be loaded but '%2' could not be found, please load it manually if possible." )
.arg( vl->name() )
.arg( dependency.name ) };
messageBar()->pushWarning( tr( "Missing layer form dependency" ), msg );
}
else
{
messageBar()->pushSuccess( tr( "Missing layer form dependency" ), tr( "Layer dependency '%2' required by '%1' was automatically loaded." )
.arg( vl->name() )
.arg( dependency.name ) );
}
}
if ( ! loaded )
{
const QString msg { tr( "layer '%1' requires layer '%2' to be loaded but '%2' could not be found, please load it manually if possible." )
.arg( vl->name() )
.arg( dependency.name ) };
messageBar()->pushWarning( tr( "Missing layer form dependency" ), msg );
}
else
{
messageBar()->pushSuccess( tr( "Missing layer form dependency" ), tr( "Layer dependency '%2' required by '%1' was automatically loaded." )
.arg( vl->name() )
.arg( dependency.name ) );
}
}
}
Expand Down

0 comments on commit 0018190

Please sign in to comment.