Skip to content
Permalink
Browse files

add button to refresh a materialized view in browser [needs-docs]

  • Loading branch information
Gustry committed Feb 20, 2018
1 parent b719441 commit 8e38788d64f1e158f8cfdcd1c5cc87327da61d37
@@ -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();
@@ -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 );

@@ -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 ) );
@@ -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" ) )
{
@@ -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 ) );
@@ -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
@@ -79,7 +79,8 @@ struct QgsPostgresLayerProperty
unsigned int nSpCols;
QString sql;
QString relKind;
bool isView;
bool isView = false;
bool isMaterializedView = false;
QString tableComment;


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

return property;
@@ -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;
}

@@ -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()
@@ -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;
@@ -131,6 +131,7 @@ class QgsPGLayerItem : public QgsLayerItem
void deleteLayer();
void renameLayer();
void truncateTable();
void refreshMaterializedView();
#endif

private:

0 comments on commit 8e38788

Please sign in to comment.
You can’t perform that action at this time.