Skip to content

Commit

Permalink
Fixes #17234 save/load styles from Postgres when a service file is used
Browse files Browse the repository at this point in the history
  • Loading branch information
pblottiere committed Oct 6, 2017
1 parent d1a9d3f commit 924ce81
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/providers/postgres/qgspostgresconn.cpp
Expand Up @@ -1855,3 +1855,21 @@ bool QgsPostgresConn::cancel()

return res == 0;
}

QString QgsPostgresConn::currentDatabase()
{
QString database;
QString sql = "SELECT current_database()";
QgsPostgresResult res( PQexec( sql ) );

if ( res.PQresultStatus() == PGRES_TUPLES_OK )
{
database = res.PQgetvalue( 0, 0 );
}
else
{
QgsMessageLog::logMessage( tr( "SQL:%1\nresult:%2\nerror:%3\n" ).arg( sql ).arg( res.PQresultStatus() ).arg( res.PQresultErrorMessage() ), tr( "PostGIS" ) );
}

return database;
}
7 changes: 7 additions & 0 deletions src/providers/postgres/qgspostgresconn.h
Expand Up @@ -310,6 +310,13 @@ class QgsPostgresConn : public QObject

QString connInfo() const { return mConnInfo; }

/**
* Returns the underlying database.
*
* \since QGIS 3.0
*/
QString currentDatabase();

static const int GEOM_TYPE_SELECT_LIMIT;

static QString displayStringForWkbType( QgsWkbTypes::Type wkbType );
Expand Down
15 changes: 15 additions & 0 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -4559,6 +4559,11 @@ QGISEXTERN bool saveStyle( const QString &uri, const QString &qmlStyle, const QS
}
}

if ( dsUri.database().isEmpty() ) // typically when a service file is used
{
dsUri.setDatabase( conn->currentDatabase() );
}

QString uiFileColumn;
QString uiFileValue;
if ( !uiFileContent.isEmpty() )
Expand Down Expand Up @@ -4679,6 +4684,11 @@ QGISEXTERN QString loadStyle( const QString &uri, QString &errCause )
return QLatin1String( "" );
}

if ( dsUri.database().isEmpty() ) // typically when a service file is used
{
dsUri.setDatabase( conn->currentDatabase() );
}

if ( !tableExists( *conn, QStringLiteral( "layer_styles" ) ) )
{
return QLatin1String( "" );
Expand Down Expand Up @@ -4717,6 +4727,11 @@ QGISEXTERN int listStyles( const QString &uri, QStringList &ids, QStringList &na
return -1;
}

if ( dsUri.database().isEmpty() ) // typically when a service file is used
{
dsUri.setDatabase( conn->currentDatabase() );
}

QString selectRelatedQuery = QString( "SELECT id,styleName,description"
" FROM layer_styles"
" WHERE f_table_catalog=%1"
Expand Down

0 comments on commit 924ce81

Please sign in to comment.