Skip to content

Commit

Permalink
Merge pull request #6366 from Gustry/materialized_view
Browse files Browse the repository at this point in the history
add button to refresh a materialized view in browser [needs-docs]
  • Loading branch information
nyalldawson committed Feb 23, 2018
2 parents 2de99fb + 8e38788 commit 669e754
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/providers/postgres/qgspostgresconn.cpp
Expand Up @@ -503,6 +503,7 @@ 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" );
QString comment = result.PQgetvalue( idx, 7 );

int srid = ssrid.isEmpty() ? INT_MIN : ssrid.toInt();
Expand Down Expand Up @@ -535,6 +536,7 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP
layerProperty.sql.clear();
layerProperty.relKind = relkind;
layerProperty.isView = isView;
layerProperty.isMaterializedView = isMaterializedView;
layerProperty.tableComment = comment;
addColumnInfo( layerProperty, schemaName, tableName, isView );

Expand Down Expand Up @@ -623,6 +625,7 @@ 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" );
QString comment = result.PQgetvalue( i, 5 ); // table comment

//QgsDebugMsg( QString( "%1.%2.%3: %4" ).arg( schemaName ).arg( tableName ).arg( column ).arg( relkind ) );
Expand All @@ -634,6 +637,7 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP
layerProperty.geometryColName = column;
layerProperty.relKind = relkind;
layerProperty.isView = isView;
layerProperty.isMaterializedView = isMaterializedView;
layerProperty.tableComment = comment;
if ( coltype == QLatin1String( "geometry" ) )
{
Expand Down Expand Up @@ -710,6 +714,7 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP
QString schema = result.PQgetvalue( i, 1 ); // nspname
QString relkind = result.PQgetvalue( i, 2 ); // relation kind
bool isView = relkind == QLatin1String( "v" ) || relkind == QLatin1String( "m" );
bool isMaterializedView = relkind == QLatin1String( "m" );
QString comment = result.PQgetvalue( i, 3 ); // table comment

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

//check if we've already added this layer in some form
Expand Down
4 changes: 3 additions & 1 deletion src/providers/postgres/qgspostgresconn.h
Expand Up @@ -79,7 +79,8 @@ struct QgsPostgresLayerProperty
unsigned int nSpCols;
QString sql;
QString relKind;
bool isView;
bool isView = false;
bool isMaterializedView = false;
QString tableComment;


Expand Down Expand Up @@ -110,6 +111,7 @@ struct QgsPostgresLayerProperty
property.sql = sql;
property.relKind = relKind;
property.isView = isView;
property.isMaterializedView = isMaterializedView;
property.tableComment = tableComment;

return property;
Expand Down
61 changes: 60 additions & 1 deletion src/providers/postgres/qgspostgresdataitems.cpp
Expand Up @@ -324,6 +324,13 @@ QList<QAction *> QgsPGLayerItem::actions( QWidget *parent )
lst.append( actionTruncateLayer );
}

if ( mLayerProperty.isMaterializedView )
{
QAction *actionRefreshMaterializedView = new QAction( tr( "Refresh Materialized View" ), parent );
connect( actionRefreshMaterializedView, &QAction::triggered, this, &QgsPGLayerItem::refreshMaterializedView );
lst.append( actionRefreshMaterializedView );
}

return lst;
}

Expand Down Expand Up @@ -440,6 +447,45 @@ void QgsPGLayerItem::truncateTable()
conn->unref();
QMessageBox::information( nullptr, tr( "Truncate Table" ), tr( "Table truncated successfully." ) );
}

void QgsPGLayerItem::refreshMaterializedView()
{
if ( QMessageBox::question( nullptr, QObject::tr( "Refresh Materialized View" ),
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 ),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
return;

QgsDataSourceUri dsUri( mUri );
QgsPostgresConn *conn = QgsPostgresConn::connectDb( dsUri.connectionInfo( false ), false );
if ( !conn )
{
QMessageBox::warning( nullptr, tr( "Refresh View" ), tr( "Unable to refresh the view." ) );
return;
}

QString schemaName = mLayerProperty.schemaName;
QString tableName = mLayerProperty.tableName;
QString schemaTableName;
if ( !schemaName.isEmpty() )
{
schemaTableName = QgsPostgresConn::quotedIdentifier( schemaName ) + '.';
}
QString tableRef = schemaTableName + QgsPostgresConn::quotedIdentifier( tableName );

QString sql = QStringLiteral( "REFRESH MATERIALIZED VIEW CONCURRENTLY %1" ).arg( tableRef );

QgsPostgresResult result( conn->PQexec( sql ) );
if ( result.PQresultStatus() != PGRES_COMMAND_OK )
{
QMessageBox::warning( nullptr, tr( "Refresh View" ), tr( "Unable to refresh view %1\n%2" ).arg( mName,
result.PQresultErrorMessage() ) );
conn->unref();
return;
}

conn->unref();
QMessageBox::information( nullptr, tr( "Refresh View" ), tr( "Materialized view refreshed successfully." ) );
}
#endif

QString QgsPGLayerItem::createUri()
Expand Down Expand Up @@ -659,8 +705,21 @@ void QgsPGSchemaItem::renameSchema()
QgsPGLayerItem *QgsPGSchemaItem::createLayer( QgsPostgresLayerProperty layerProperty )
{
//QgsDebugMsg( "schemaName = " + layerProperty.schemaName + " tableName = " + layerProperty.tableName + " geometryColName = " + layerProperty.geometryColName );
QString tip;
if ( layerProperty.isView && ! layerProperty.isMaterializedView )
{
tip = tr( "View" );
}
else if ( layerProperty.isView && layerProperty.isMaterializedView )
{
tip = tr( "Materialized view" );
}
else
{
tip = tr( "Table" );
}
QgsWkbTypes::Type wkbType = layerProperty.types.at( 0 );
QString tip = tr( "%1 as %2 in %3" ).arg( layerProperty.geometryColName, QgsPostgresConn::displayStringForWkbType( wkbType ) ).arg( layerProperty.srids.at( 0 ) );
tip += tr( "\n%1 as %2 in %3" ).arg( layerProperty.geometryColName, QgsPostgresConn::displayStringForWkbType( wkbType ) ).arg( layerProperty.srids.at( 0 ) );
if ( !layerProperty.tableComment.isEmpty() )
{
tip = layerProperty.tableComment + '\n' + tip;
Expand Down
1 change: 1 addition & 0 deletions src/providers/postgres/qgspostgresdataitems.h
Expand Up @@ -131,6 +131,7 @@ class QgsPGLayerItem : public QgsLayerItem
void deleteLayer();
void renameLayer();
void truncateTable();
void refreshMaterializedView();
#endif

private:
Expand Down

0 comments on commit 669e754

Please sign in to comment.