Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[browser] Fix removal of views from postgres databases (fixes #29812)
For views one needs to use DROP VIEW sql command
  • Loading branch information
wonder-sk committed May 30, 2019
1 parent b31f516 commit 8441785
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/providers/postgres/qgspostgresdataitems.cpp
Expand Up @@ -340,7 +340,9 @@ QList<QAction *> QgsPGLayerItem::actions( QWidget *parent )

void QgsPGLayerItem::deleteLayer()
{
if ( QMessageBox::question( nullptr, QObject::tr( "Delete Table" ),
QString typeName = mLayerProperty.isView ? tr( "View" ) : tr( "Table" );

if ( QMessageBox::question( nullptr, tr( "Delete %1" ).arg( typeName ),
QObject::tr( "Are you sure you want to delete %1.%2?" ).arg( mLayerProperty.schemaName, mLayerProperty.tableName ),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
return;
Expand All @@ -349,11 +351,11 @@ void QgsPGLayerItem::deleteLayer()
bool res = ::deleteLayer( mUri, errCause );
if ( !res )
{
QMessageBox::warning( nullptr, tr( "Delete Table" ), errCause );
QMessageBox::warning( nullptr, tr( "Delete %1" ).arg( typeName ), errCause );
}
else
{
QMessageBox::information( nullptr, tr( "Delete Table" ), tr( "Table deleted successfully." ) );
QMessageBox::information( nullptr, tr( "Delete %1" ).arg( typeName ), tr( "%1 deleted successfully." ).arg( typeName ) );
if ( mParent )
mParent->refresh();
}
Expand Down
22 changes: 22 additions & 0 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -4642,6 +4642,28 @@ QGISEXTERN bool deleteLayer( const QString &uri, QString &errCause )
return false;
}

// handle deletion of views
QString sqlViewCheck = QStringLiteral( "SELECT relkind FROM pg_class WHERE oid=regclass(%1)::oid" )
.arg( QgsPostgresConn::quotedValue( schemaTableName ) );
QgsPostgresResult resViewCheck( conn->PQexec( sqlViewCheck ) );
QString type = resViewCheck.PQgetvalue( 0, 0 );
if ( type == QLatin1String( "v" ) || type == QLatin1String( "m" ) )
{
QString sql = QString( "DROP VIEW %1" ).arg( schemaTableName );
QgsPostgresResult result( conn->PQexec( sql ) );
if ( result.PQresultStatus() != PGRES_COMMAND_OK )
{
errCause = QObject::tr( "Unable to delete view %1: \n%2" )
.arg( schemaTableName,
result.PQresultErrorMessage() );
conn->unref();
return false;
}
conn->unref();
return true;
}


// check the geometry column count
QString sql = QString( "SELECT count(*) "
"FROM geometry_columns, pg_class, pg_namespace "
Expand Down

0 comments on commit 8441785

Please sign in to comment.