Skip to content

Commit

Permalink
Postgreprovider hide raster overviews (#59884)
Browse files Browse the repository at this point in the history
* add button and store setting

* use new setting

* removeRasterOverviews in functions

* settings

* parameter

* update function

* flip logic to explicitly ask for raster overviews

* only run if there is at least one result

* add explicit check for existence of raster_overviews view

* change to QStringLiteral

Co-authored-by: Stefanos Natsis <[email protected]>

* make const

Co-authored-by: Stefanos Natsis <[email protected]>

* default value false

* make variable name consistent with existing one

---------

Co-authored-by: Stefanos Natsis <[email protected]>
  • Loading branch information
JanCaha and uclaros authored Jan 17, 2025
1 parent c0df09b commit a716032
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 12 deletions.
2 changes: 2 additions & 0 deletions src/providers/postgres/qgspgnewconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ QgsPgNewConnection::QgsPgNewConnection( QWidget *parent, const QString &connName
cb_geometryColumnsOnly->setChecked( settings.value( key + "/geometryColumnsOnly", true ).toBool() );
cb_dontResolveType->setChecked( settings.value( key + "/dontResolveType", false ).toBool() );
cb_allowGeometrylessTables->setChecked( settings.value( key + "/allowGeometrylessTables", false ).toBool() );
cb_allowRasterOverviewTables->setChecked( settings.value( key + "/allowRasterOverviewTables", false ).toBool() );
// Ensure that cb_publicSchemaOnly is set correctly
cb_geometryColumnsOnly_clicked();

Expand Down Expand Up @@ -177,6 +178,7 @@ void QgsPgNewConnection::accept()
configuration.insert( "projectsInDatabase", cb_projectsInDatabase->isChecked() );
configuration.insert( "metadataInDatabase", cb_metadataInDatabase->isChecked() );
configuration.insert( "session_role", txtSessionRole->text() );
configuration.insert( "allowRasterOverviewTables", cb_allowRasterOverviewTables->isChecked() );

QgsProviderMetadata *providerMetadata = QgsProviderRegistry::instance()->providerMetadata( QStringLiteral( "postgres" ) );
std::unique_ptr<QgsPostgresProviderConnection> providerConnection( qgis::down_cast<QgsPostgresProviderConnection *>( providerMetadata->createConnection( txtName->text() ) ) );
Expand Down
77 changes: 71 additions & 6 deletions src/providers/postgres/qgspostgresconn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ void QgsPostgresConn::addColumnInfo( QgsPostgresLayerProperty &layerProperty, co
}
}

bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchPublicOnly, bool allowGeometrylessTables, const QString &schema, const QString &name )
bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchPublicOnly, bool allowGeometrylessTables, bool allowRasterOverviewTables, const QString &schema, const QString &name )
{
QMutexLocker locker( &mLock );
int nColumns = 0;
Expand Down Expand Up @@ -1054,6 +1054,63 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP
}
}

// remove raster overivews if allowRasterOverviewTables is FALSE
if ( !allowRasterOverviewTables )
{
QString sqlRasterOverviewExist = QStringLiteral( "SELECT table_schema, table_name"
" FROM information_schema.views"
" WHERE table_schema = 'public' AND table_name = 'raster_overviews'" );

QgsPostgresResult resultRasterOverviewsExist;
resultRasterOverviewsExist = LoggedPQexec( "QgsPostgresConn", sqlRasterOverviewExist );

if ( resultRasterOverviewsExist.result() && resultRasterOverviewsExist.PQntuples() > 0 )
{
const QString sqlRasterOverviews = QStringLiteral( "SELECT o_table_schema, o_table_name FROM public.raster_overviews" );

QgsPostgresResult resultRasterOverviews;
resultRasterOverviews = LoggedPQexec( "QgsPostgresConn", sqlRasterOverviews );

if ( resultRasterOverviews.result() && resultRasterOverviews.PQntuples() > 0 )
{
QVector<QgsPostgresRasterOverviewLayerProperty> overviews;
for ( int idx = 0; idx < resultRasterOverviews.PQntuples(); idx++ )
{
QgsPostgresRasterOverviewLayerProperty rasterOverviewProperty;
rasterOverviewProperty.schemaName = resultRasterOverviews.PQgetvalue( idx, 0 );
rasterOverviewProperty.tableName = resultRasterOverviews.PQgetvalue( idx, 1 );
overviews.append( rasterOverviewProperty );
}

QVector<QgsPostgresLayerProperty> layersToKeep;
for ( int i = 0; i < mLayersSupported.count(); i++ )
{
QgsPostgresLayerProperty property = mLayersSupported.at( i );

if ( !property.isRaster )
{
layersToKeep.append( property );
}
else
{
bool keepRasterTable = true;
for ( const QgsPostgresRasterOverviewLayerProperty &overview : std::as_const( overviews ) )
{
if ( property.schemaName == overview.schemaName && property.tableName == overview.tableName )
{
keepRasterTable = false;
}
}

if ( keepRasterTable )
layersToKeep.append( property );
}
}
mLayersSupported = layersToKeep;
}
}
}

if ( nColumns == 0 && schema.isEmpty() )
{
QgsMessageLog::logMessage( tr( "Database connection was successful, but the accessible tables could not be determined." ), tr( "PostGIS" ) );
Expand All @@ -1062,14 +1119,14 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP
return true;
}

bool QgsPostgresConn::supportedLayersPrivate( QVector<QgsPostgresLayerProperty> &layers, bool searchGeometryColumnsOnly, bool searchPublicOnly, bool allowGeometrylessTables, const QString &schema, const QString &table )
bool QgsPostgresConn::supportedLayersPrivate( QVector<QgsPostgresLayerProperty> &layers, bool searchGeometryColumnsOnly, bool searchPublicOnly, bool allowGeometrylessTables, bool allowRasterOverviewTables, const QString &schema, const QString &table )
{
QMutexLocker locker( &mLock );

mLayersSupported.clear();

// Get the list of supported tables
if ( !getTableInfo( searchGeometryColumnsOnly, searchPublicOnly, allowGeometrylessTables, schema, table ) )
if ( !getTableInfo( searchGeometryColumnsOnly, searchPublicOnly, allowGeometrylessTables, allowRasterOverviewTables, schema, table ) )
{
QgsMessageLog::logMessage( tr( "Unable to get list of spatially enabled tables from the database" ), tr( "PostGIS" ) );
return false;
Expand Down Expand Up @@ -1458,15 +1515,15 @@ Qgis::PostgresRelKind QgsPostgresConn::relKindFromValue( const QString &value )
return Qgis::PostgresRelKind::Unknown;
}

bool QgsPostgresConn::supportedLayers( QVector<QgsPostgresLayerProperty> &layers, bool searchGeometryColumnsOnly, bool searchPublicOnly, bool allowGeometrylessTables, const QString &schema )
bool QgsPostgresConn::supportedLayers( QVector<QgsPostgresLayerProperty> &layers, bool searchGeometryColumnsOnly, bool searchPublicOnly, bool allowGeometrylessTables, bool allowRasterOverviewTables, const QString &schema )
{
return supportedLayersPrivate( layers, searchGeometryColumnsOnly, searchPublicOnly, allowGeometrylessTables, schema );
return supportedLayersPrivate( layers, searchGeometryColumnsOnly, searchPublicOnly, allowGeometrylessTables, allowRasterOverviewTables, schema );
}

bool QgsPostgresConn::supportedLayer( QgsPostgresLayerProperty &layerProperty, const QString &schema, const QString &table )
{
QVector<QgsPostgresLayerProperty> layers;
if ( !supportedLayersPrivate( layers, false, false, true /* allowGeometrylessTables */, schema, table ) || layers.empty() )
if ( !supportedLayersPrivate( layers, false, false, true /* allowGeometrylessTables */, false, schema, table ) || layers.empty() )
{
return false;
}
Expand Down Expand Up @@ -2737,6 +2794,12 @@ bool QgsPostgresConn::allowProjectsInDatabase( const QString &connName )
return settings.value( "/PostgreSQL/connections/" + connName + "/projectsInDatabase", false ).toBool();
}

bool QgsPostgresConn::allowRasterOverviewTables( const QString &connName )
{
QgsSettings settings;
return settings.value( "/PostgreSQL/connections/" + connName + "/allowRasterOverviewTables", true ).toBool();
}

void QgsPostgresConn::deleteConnection( const QString &connName )
{
QgsSettings settings;
Expand All @@ -2761,6 +2824,7 @@ void QgsPostgresConn::deleteConnection( const QString &connName )
settings.remove( key + "/metadataInDatabase" );
settings.remove( key + "/dontResolveType" );
settings.remove( key + "/session_role" );
settings.remove( key + "/allowRasterOverviewTables" );
settings.remove( key );
}

Expand Down Expand Up @@ -2788,6 +2852,7 @@ void QgsPostgresConn::duplicateConnection( const QString &src, const QString &ds
settings.setValue( newKey + QStringLiteral( "/saveUsername" ), settings.value( key + QStringLiteral( "/saveUsername" ) ).toString() );
settings.setValue( newKey + QStringLiteral( "/savePassword" ), settings.value( key + QStringLiteral( "/savePassword" ) ).toString() );
settings.setValue( newKey + QStringLiteral( "/authcfg" ), settings.value( key + QStringLiteral( "/authcfg" ) ).toString() );
settings.setValue( newKey + QStringLiteral( "/allowRasterOverviewTables" ), settings.value( key + QStringLiteral( "/allowRasterOverviewTables" ) ).toString() );

settings.sync();
}
Expand Down
16 changes: 13 additions & 3 deletions src/providers/postgres/qgspostgresconn.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ struct QgsPostgresSchemaProperty
QString owner;
};

//! Raster overview table properties structure
struct QgsPostgresRasterOverviewLayerProperty
{
QString schemaName;
QString tableName;
};

//! Layer Property structure
// TODO: Fill to Postgres/PostGIS specifications
Expand Down Expand Up @@ -382,10 +388,11 @@ class QgsPostgresConn : public QObject
* contained in the geometry_columns metatable
* \param searchPublicOnly
* \param allowGeometrylessTables
* \param allowRasterOverviewTables list raster layer overviews
* \param schema restrict layers to layers within specified schema
* \returns true if layers were fetched successfully
*/
bool supportedLayers( QVector<QgsPostgresLayerProperty> &layers, bool searchGeometryColumnsOnly = true, bool searchPublicOnly = true, bool allowGeometrylessTables = false, const QString &schema = QString() );
bool supportedLayers( QVector<QgsPostgresLayerProperty> &layers, bool searchGeometryColumnsOnly = true, bool searchPublicOnly = true, bool allowGeometrylessTables = false, bool allowRasterOverviewTables = false, const QString &schema = QString() );

/**
* Get the information about a supported layer
Expand Down Expand Up @@ -418,11 +425,12 @@ class QgsPostgresConn : public QObject
* contained in the geometry_columns metatable
* \param searchPublicOnly
* \param allowGeometrylessTables
* \param allowRasterOverviewTables list raster layer overviews
* \param schema restrict tables to those within specified schema
* \param name restrict tables to those with specified name
* \returns true if tables were successfully queried
*/
bool getTableInfo( bool searchGeometryColumnsOnly, bool searchPublicOnly, bool allowGeometrylessTables, const QString &schema = QString(), const QString &name = QString() );
bool getTableInfo( bool searchGeometryColumnsOnly, bool searchPublicOnly, bool allowGeometrylessTables, bool allowRasterOverviewTables = false, const QString &schema = QString(), const QString &name = QString() );

qint64 getBinaryInt( QgsPostgresResult &queryResult, int row, int col );

Expand Down Expand Up @@ -471,6 +479,7 @@ class QgsPostgresConn : public QObject
static bool allowProjectsInDatabase( const QString &connName );
static void deleteConnection( const QString &connName );
static bool allowMetadataInDatabase( const QString &connName );
static bool allowRasterOverviewTables( const QString &connName );

/**
* Duplicates \a src connection settings to a new \a dst connection.
Expand Down Expand Up @@ -543,11 +552,12 @@ class QgsPostgresConn : public QObject
* contained in the geometry_columns metatable
* \param searchPublicOnly
* \param allowGeometrylessTables
* \param allowRasterOverviewTables list raster layer overviews
* \param schema restrict layers to layers within specified schema
* \param table restrict tables to those with specified table
* \returns true if layers were fetched successfully
*/
bool supportedLayersPrivate( QVector<QgsPostgresLayerProperty> &layers, bool searchGeometryColumnsOnly = true, bool searchPublicOnly = true, bool allowGeometrylessTables = false, const QString &schema = QString(), const QString &table = QString() );
bool supportedLayersPrivate( QVector<QgsPostgresLayerProperty> &layers, bool searchGeometryColumnsOnly = true, bool searchPublicOnly = true, bool allowGeometrylessTables = false, bool allowRasterOverviewTables = false, const QString &schema = QString(), const QString &table = QString() );

//! List of the supported layers
QVector<QgsPostgresLayerProperty> mLayersSupported;
Expand Down
2 changes: 1 addition & 1 deletion src/providers/postgres/qgspostgresdataitems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ QVector<QgsDataItem *> QgsPGSchemaItem::createChildren()
}

QVector<QgsPostgresLayerProperty> layerProperties;
const bool ok = conn->supportedLayers( layerProperties, QgsPostgresConn::geometryColumnsOnly( mConnectionName ), QgsPostgresConn::publicSchemaOnly( mConnectionName ), QgsPostgresConn::allowGeometrylessTables( mConnectionName ), mName );
const bool ok = conn->supportedLayers( layerProperties, QgsPostgresConn::geometryColumnsOnly( mConnectionName ), QgsPostgresConn::publicSchemaOnly( mConnectionName ), QgsPostgresConn::allowGeometrylessTables( mConnectionName ), QgsPostgresConn::allowRasterOverviewTables( mConnectionName ), mName );

if ( !ok )
{
Expand Down
3 changes: 2 additions & 1 deletion src/providers/postgres/qgspostgresproviderconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const QStringList QgsPostgresProviderConnection::CONFIGURATION_PARAMETERS = {
QStringLiteral( "projectsInDatabase" ),
QStringLiteral( "metadataInDatabase" ),
QStringLiteral( "session_role" ),
QStringLiteral( "allowRasterOverviewTables" ),
};

const QString QgsPostgresProviderConnection::SETTINGS_BASE_KEY = QStringLiteral( "/PostgreSQL/connections/" );
Expand Down Expand Up @@ -239,7 +240,7 @@ QList<QgsAbstractDatabaseProviderConnection::TableProperty> QgsPostgresProviderC
}
else
{
ok = conn->supportedLayers( properties, false, schema == QStringLiteral( "public" ), aspatial, schema );
ok = conn->supportedLayers( properties, false, schema == QStringLiteral( "public" ), aspatial, false, schema );
}

if ( !ok )
Expand Down
9 changes: 8 additions & 1 deletion src/ui/qgspgnewconnectionbase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>448</width>
<height>510</height>
<height>573</height>
</rect>
</property>
<property name="sizePolicy">
Expand Down Expand Up @@ -236,6 +236,13 @@
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="cb_allowRasterOverviewTables">
<property name="text">
<string>Also list raster overview tables</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
Expand Down

0 comments on commit a716032

Please sign in to comment.