Skip to content

Commit 8e38788

Browse files
committed
add button to refresh a materialized view in browser [needs-docs]
1 parent b719441 commit 8e38788

File tree

4 files changed

+70
-2
lines changed

4 files changed

+70
-2
lines changed

src/providers/postgres/qgspostgresconn.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,7 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP
503503
int dim = result.PQgetvalue( idx, 5 ).toInt();
504504
QString relkind = result.PQgetvalue( idx, 6 );
505505
bool isView = relkind == QLatin1String( "v" ) || relkind == QLatin1String( "m" );
506+
bool isMaterializedView = relkind == QLatin1String( "m" );
506507
QString comment = result.PQgetvalue( idx, 7 );
507508

508509
int srid = ssrid.isEmpty() ? INT_MIN : ssrid.toInt();
@@ -535,6 +536,7 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP
535536
layerProperty.sql.clear();
536537
layerProperty.relKind = relkind;
537538
layerProperty.isView = isView;
539+
layerProperty.isMaterializedView = isMaterializedView;
538540
layerProperty.tableComment = comment;
539541
addColumnInfo( layerProperty, schemaName, tableName, isView );
540542

@@ -623,6 +625,7 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP
623625
QString relkind = result.PQgetvalue( i, 3 ); // relation kind
624626
QString coltype = result.PQgetvalue( i, 4 ); // column type
625627
bool isView = relkind == QLatin1String( "v" ) || relkind == QLatin1String( "m" );
628+
bool isMaterializedView = relkind == QLatin1String( "m" );
626629
QString comment = result.PQgetvalue( i, 5 ); // table comment
627630

628631
//QgsDebugMsg( QString( "%1.%2.%3: %4" ).arg( schemaName ).arg( tableName ).arg( column ).arg( relkind ) );
@@ -634,6 +637,7 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP
634637
layerProperty.geometryColName = column;
635638
layerProperty.relKind = relkind;
636639
layerProperty.isView = isView;
640+
layerProperty.isMaterializedView = isMaterializedView;
637641
layerProperty.tableComment = comment;
638642
if ( coltype == QLatin1String( "geometry" ) )
639643
{
@@ -710,6 +714,7 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP
710714
QString schema = result.PQgetvalue( i, 1 ); // nspname
711715
QString relkind = result.PQgetvalue( i, 2 ); // relation kind
712716
bool isView = relkind == QLatin1String( "v" ) || relkind == QLatin1String( "m" );
717+
bool isMaterializedView = relkind == QLatin1String( "m" );
713718
QString comment = result.PQgetvalue( i, 3 ); // table comment
714719

715720
//QgsDebugMsg( QString( "%1.%2: %3" ).arg( schema ).arg( table ).arg( relkind ) );
@@ -722,6 +727,7 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP
722727
layerProperty.geometryColType = SctNone;
723728
layerProperty.relKind = relkind;
724729
layerProperty.isView = isView;
730+
layerProperty.isMaterializedView = isMaterializedView;
725731
layerProperty.tableComment = comment;
726732

727733
//check if we've already added this layer in some form

src/providers/postgres/qgspostgresconn.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ struct QgsPostgresLayerProperty
7979
unsigned int nSpCols;
8080
QString sql;
8181
QString relKind;
82-
bool isView;
82+
bool isView = false;
83+
bool isMaterializedView = false;
8384
QString tableComment;
8485

8586

@@ -110,6 +111,7 @@ struct QgsPostgresLayerProperty
110111
property.sql = sql;
111112
property.relKind = relKind;
112113
property.isView = isView;
114+
property.isMaterializedView = isMaterializedView;
113115
property.tableComment = tableComment;
114116

115117
return property;

src/providers/postgres/qgspostgresdataitems.cpp

+60-1
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,13 @@ QList<QAction *> QgsPGLayerItem::actions( QWidget *parent )
324324
lst.append( actionTruncateLayer );
325325
}
326326

327+
if ( mLayerProperty.isMaterializedView )
328+
{
329+
QAction *actionRefreshMaterializedView = new QAction( tr( "Refresh Materialized View" ), parent );
330+
connect( actionRefreshMaterializedView, &QAction::triggered, this, &QgsPGLayerItem::refreshMaterializedView );
331+
lst.append( actionRefreshMaterializedView );
332+
}
333+
327334
return lst;
328335
}
329336

@@ -440,6 +447,45 @@ void QgsPGLayerItem::truncateTable()
440447
conn->unref();
441448
QMessageBox::information( nullptr, tr( "Truncate Table" ), tr( "Table truncated successfully." ) );
442449
}
450+
451+
void QgsPGLayerItem::refreshMaterializedView()
452+
{
453+
if ( QMessageBox::question( nullptr, QObject::tr( "Refresh Materialized View" ),
454+
QObject::tr( "Are you sure you want to refresh the materialized view %1.%2?\n\nThis will update all data within the table." ).arg( mLayerProperty.schemaName, mLayerProperty.tableName ),
455+
QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
456+
return;
457+
458+
QgsDataSourceUri dsUri( mUri );
459+
QgsPostgresConn *conn = QgsPostgresConn::connectDb( dsUri.connectionInfo( false ), false );
460+
if ( !conn )
461+
{
462+
QMessageBox::warning( nullptr, tr( "Refresh View" ), tr( "Unable to refresh the view." ) );
463+
return;
464+
}
465+
466+
QString schemaName = mLayerProperty.schemaName;
467+
QString tableName = mLayerProperty.tableName;
468+
QString schemaTableName;
469+
if ( !schemaName.isEmpty() )
470+
{
471+
schemaTableName = QgsPostgresConn::quotedIdentifier( schemaName ) + '.';
472+
}
473+
QString tableRef = schemaTableName + QgsPostgresConn::quotedIdentifier( tableName );
474+
475+
QString sql = QStringLiteral( "REFRESH MATERIALIZED VIEW CONCURRENTLY %1" ).arg( tableRef );
476+
477+
QgsPostgresResult result( conn->PQexec( sql ) );
478+
if ( result.PQresultStatus() != PGRES_COMMAND_OK )
479+
{
480+
QMessageBox::warning( nullptr, tr( "Refresh View" ), tr( "Unable to refresh view %1\n%2" ).arg( mName,
481+
result.PQresultErrorMessage() ) );
482+
conn->unref();
483+
return;
484+
}
485+
486+
conn->unref();
487+
QMessageBox::information( nullptr, tr( "Refresh View" ), tr( "Materialized view refreshed successfully." ) );
488+
}
443489
#endif
444490

445491
QString QgsPGLayerItem::createUri()
@@ -659,8 +705,21 @@ void QgsPGSchemaItem::renameSchema()
659705
QgsPGLayerItem *QgsPGSchemaItem::createLayer( QgsPostgresLayerProperty layerProperty )
660706
{
661707
//QgsDebugMsg( "schemaName = " + layerProperty.schemaName + " tableName = " + layerProperty.tableName + " geometryColName = " + layerProperty.geometryColName );
708+
QString tip;
709+
if ( layerProperty.isView && ! layerProperty.isMaterializedView )
710+
{
711+
tip = tr( "View" );
712+
}
713+
else if ( layerProperty.isView && layerProperty.isMaterializedView )
714+
{
715+
tip = tr( "Materialized view" );
716+
}
717+
else
718+
{
719+
tip = tr( "Table" );
720+
}
662721
QgsWkbTypes::Type wkbType = layerProperty.types.at( 0 );
663-
QString tip = tr( "%1 as %2 in %3" ).arg( layerProperty.geometryColName, QgsPostgresConn::displayStringForWkbType( wkbType ) ).arg( layerProperty.srids.at( 0 ) );
722+
tip += tr( "\n%1 as %2 in %3" ).arg( layerProperty.geometryColName, QgsPostgresConn::displayStringForWkbType( wkbType ) ).arg( layerProperty.srids.at( 0 ) );
664723
if ( !layerProperty.tableComment.isEmpty() )
665724
{
666725
tip = layerProperty.tableComment + '\n' + tip;

src/providers/postgres/qgspostgresdataitems.h

+1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ class QgsPGLayerItem : public QgsLayerItem
131131
void deleteLayer();
132132
void renameLayer();
133133
void truncateTable();
134+
void refreshMaterializedView();
134135
#endif
135136

136137
private:

0 commit comments

Comments
 (0)