Skip to content

Commit

Permalink
Use relkind everywhere to determine if table is a materialized view
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Apr 6, 2023
1 parent afc48e0 commit 1b1747d
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/providers/postgres/qgspgtablemodel.cpp
Expand Up @@ -187,7 +187,7 @@ void QgsPgTableModel::addTableEntry( const QgsPostgresLayerProperty &layerProper
// Legacy: default value is determined by project option to trust layer's metadata
// TODO: remove this default from QGIS 4 and leave default value to false?
// checkPkUnicity has only effect on views and materialized views, so we can safely disable it
if ( layerProperty.isView || layerProperty.isMaterializedView )
if ( layerProperty.isView || layerProperty.relKind == Qgis::PostgresRelKind::MaterializedView )
{
checkPkUnicityItem->setCheckState( ( QgsProject::instance( )->flags() & Qgis::ProjectFlag::TrustStoredLayerStatistics ) ? Qt::CheckState::Unchecked : Qt::CheckState::Checked );
checkPkUnicityItem->setToolTip( headerData( Columns::DbtmCheckPkUnicity, Qt::Orientation::Horizontal, Qt::ToolTipRole ).toString() );
Expand Down
13 changes: 3 additions & 10 deletions src/providers/postgres/qgspostgresconn.cpp
Expand Up @@ -20,7 +20,6 @@
#include "qgsdatasourceuri.h"
#include "qgsmessagelog.h"
#include "qgscredentials.h"
#include "qgsfields.h"
#include "qgsvectordataprovider.h"
#include "qgswkbtypes.h"
#include "qgssettings.h"
Expand Down Expand Up @@ -714,7 +713,6 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP
int dim = result.PQgetvalue( idx, 5 ).toInt();
QString relkind = result.PQgetvalue( idx, 6 );
bool isView = relkind == QLatin1String( "v" ) || relkind == QLatin1String( "m" );
bool isMaterializedView = relkind == QLatin1String( "m" );
bool isForeignTable = relkind == QLatin1String( "f" );
bool isRaster = type == QLatin1String( "RASTER" );
QString comment = result.PQgetvalue( idx, 7 );
Expand Down Expand Up @@ -769,11 +767,10 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP
layerProperty.types = QList<Qgis::WkbType>() << ( QgsPostgresConn::wkbTypeFromPostgis( type ) );
layerProperty.srids = QList<int>() << srid;
layerProperty.sql.clear();
layerProperty.relKind = relkind;
layerProperty.relKind = relKindFromValue( relkind );
layerProperty.isView = isView;
layerProperty.isForeignTable = isForeignTable;
layerProperty.isRaster = isRaster;
layerProperty.isMaterializedView = isMaterializedView;
layerProperty.tableComment = comment;
layerProperty.nSpCols = nSpCols;
if ( isView || isForeignTable )
Expand Down Expand Up @@ -880,7 +877,6 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP
QString relkind = result.PQgetvalue( i, 3 ); // relation kind
QString coltype = result.PQgetvalue( i, 4 ); // column type
bool isView = relkind == QLatin1String( "v" ) || relkind == QLatin1String( "m" );
bool isMaterializedView = relkind == QLatin1String( "m" );
bool isForeignTable = relkind == QLatin1String( "f" );
QString comment = result.PQgetvalue( i, 5 ); // table comment

Expand All @@ -890,11 +886,10 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP
layerProperty.schemaName = schemaName;
layerProperty.tableName = tableName;
layerProperty.geometryColName = column;
layerProperty.relKind = relkind;
layerProperty.relKind = relKindFromValue( relkind );
layerProperty.isView = isView;
layerProperty.isForeignTable = isForeignTable;
layerProperty.isRaster = coltype == QLatin1String( "raster" );
layerProperty.isMaterializedView = isMaterializedView;
layerProperty.tableComment = comment;
if ( coltype == QLatin1String( "geometry" ) )
{
Expand Down Expand Up @@ -987,7 +982,6 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP
QString comment = result.PQgetvalue( i, 3 ); // table comment
QString attributes = result.PQgetvalue( i, 4 ); // attributes array
bool isView = relkind == QLatin1String( "v" ) || relkind == QLatin1String( "m" );
bool isMaterializedView = relkind == QLatin1String( "m" );
bool isForeignTable = relkind == QLatin1String( "f" );

QgsPostgresLayerProperty layerProperty;
Expand All @@ -998,11 +992,10 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP
layerProperty.geometryColName = QString();
layerProperty.geometryColType = SctNone;
layerProperty.nSpCols = 0;
layerProperty.relKind = relkind;
layerProperty.relKind = relKindFromValue( relkind );
layerProperty.isView = isView;
layerProperty.isForeignTable = isForeignTable;
layerProperty.isRaster = false;
layerProperty.isMaterializedView = isMaterializedView;
layerProperty.tableComment = comment;

//check if we've already added this layer in some form
Expand Down
4 changes: 1 addition & 3 deletions src/providers/postgres/qgspostgresconn.h
Expand Up @@ -82,9 +82,8 @@ struct QgsPostgresLayerProperty
QList<int> srids;
unsigned int nSpCols;
QString sql;
QString relKind;
Qgis::PostgresRelKind relKind = Qgis::PostgresRelKind::Unknown;
bool isView = false;
bool isMaterializedView = false;
bool isForeignTable = false;
bool isRaster = false;
QString tableComment;
Expand Down Expand Up @@ -117,7 +116,6 @@ struct QgsPostgresLayerProperty
property.relKind = relKind;
property.isView = isView;
property.isRaster = isRaster;
property.isMaterializedView = isMaterializedView;
property.tableComment = tableComment;

return property;
Expand Down
4 changes: 2 additions & 2 deletions src/providers/postgres/qgspostgresdataitemguiprovider.cpp
Expand Up @@ -107,7 +107,7 @@ void QgsPostgresDataItemGuiProvider::populateContextMenu( QgsDataItem *item, QMe
maintainMenu->addAction( actionTruncateLayer );
}

if ( layerInfo.isMaterializedView )
if ( layerInfo.relKind == Qgis::PostgresRelKind::MaterializedView )
{
QAction *actionRefreshMaterializedView = new QAction( tr( "Refresh Materialized View…" ), menu );
connect( actionRefreshMaterializedView, &QAction::triggered, this, [layerItem, context] { refreshMaterializedView( layerItem, context ); } );
Expand Down Expand Up @@ -410,7 +410,7 @@ void QgsPostgresDataItemGuiProvider::renameLayer( QgsPGLayerItem *layerItem, Qgs
QString sql;
if ( layerInfo.isView )
{
sql = QStringLiteral( "ALTER %1 VIEW %2 RENAME TO %3" ).arg( layerInfo.relKind == QLatin1String( "m" ) ? QStringLiteral( "MATERIALIZED" ) : QString(),
sql = QStringLiteral( "ALTER %1 VIEW %2 RENAME TO %3" ).arg( layerInfo.relKind == Qgis::PostgresRelKind::MaterializedView ? QStringLiteral( "MATERIALIZED" ) : QString(),
oldName, newName );
}
else
Expand Down
5 changes: 2 additions & 3 deletions src/providers/postgres/qgspostgresdataitems.cpp
Expand Up @@ -18,7 +18,6 @@
#include "qgspostgresconnpool.h"
#include "qgspostgresprojectstorage.h"
#include "qgspostgresprovider.h"
#include "qgscolumntypethread.h"
#include "qgslogger.h"
#include "qgsdatasourceuri.h"
#include "qgsapplication.h"
Expand Down Expand Up @@ -539,11 +538,11 @@ QgsPGLayerItem *QgsPGSchemaItem::createLayer( QgsPostgresLayerProperty layerProp
{
//QgsDebugMsg( "schemaName = " + layerProperty.schemaName + " tableName = " + layerProperty.tableName + " geometryColName = " + layerProperty.geometryColName );
QString tip;
if ( layerProperty.isView && ! layerProperty.isMaterializedView )
if ( layerProperty.isView && layerProperty.relKind != Qgis::PostgresRelKind::MaterializedView )
{
tip = tr( "View" );
}
else if ( layerProperty.isView && layerProperty.isMaterializedView )
else if ( layerProperty.isView && layerProperty.relKind == Qgis::PostgresRelKind::MaterializedView )
{
tip = tr( "Materialized view" );
}
Expand Down
4 changes: 2 additions & 2 deletions src/providers/postgres/qgspostgresproviderconnection.cpp
Expand Up @@ -622,7 +622,7 @@ QList<QgsPostgresProviderConnection::TableProperty> QgsPostgresProviderConnectio
{
prFlags.setFlag( QgsPostgresProviderConnection::TableFlag::View );
}
if ( pr.isMaterializedView )
if ( pr.relKind == Qgis::PostgresRelKind::MaterializedView )
{
prFlags.setFlag( QgsPostgresProviderConnection::TableFlag::MaterializedView );
}
Expand Down Expand Up @@ -667,7 +667,7 @@ QList<QgsPostgresProviderConnection::TableProperty> QgsPostgresProviderConnectio
property.setComment( pr.tableComment );

// Get PKs
if ( pr.isView || pr.isMaterializedView || pr.isForeignTable )
if ( pr.isView || ( pr.relKind == Qgis::PostgresRelKind::MaterializedView ) || pr.isForeignTable )
{
// Set the candidates
property.setPrimaryKeyColumns( pr.pkCols );
Expand Down

0 comments on commit 1b1747d

Please sign in to comment.