Skip to content

Commit 924ce81

Browse files
committedOct 6, 2017
Fixes #17234 save/load styles from Postgres when a service file is used
1 parent d1a9d3f commit 924ce81

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed
 

‎src/providers/postgres/qgspostgresconn.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1855,3 +1855,21 @@ bool QgsPostgresConn::cancel()
18551855

18561856
return res == 0;
18571857
}
1858+
1859+
QString QgsPostgresConn::currentDatabase()
1860+
{
1861+
QString database;
1862+
QString sql = "SELECT current_database()";
1863+
QgsPostgresResult res( PQexec( sql ) );
1864+
1865+
if ( res.PQresultStatus() == PGRES_TUPLES_OK )
1866+
{
1867+
database = res.PQgetvalue( 0, 0 );
1868+
}
1869+
else
1870+
{
1871+
QgsMessageLog::logMessage( tr( "SQL:%1\nresult:%2\nerror:%3\n" ).arg( sql ).arg( res.PQresultStatus() ).arg( res.PQresultErrorMessage() ), tr( "PostGIS" ) );
1872+
}
1873+
1874+
return database;
1875+
}

‎src/providers/postgres/qgspostgresconn.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,13 @@ class QgsPostgresConn : public QObject
310310

311311
QString connInfo() const { return mConnInfo; }
312312

313+
/**
314+
* Returns the underlying database.
315+
*
316+
* \since QGIS 3.0
317+
*/
318+
QString currentDatabase();
319+
313320
static const int GEOM_TYPE_SELECT_LIMIT;
314321

315322
static QString displayStringForWkbType( QgsWkbTypes::Type wkbType );

‎src/providers/postgres/qgspostgresprovider.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4559,6 +4559,11 @@ QGISEXTERN bool saveStyle( const QString &uri, const QString &qmlStyle, const QS
45594559
}
45604560
}
45614561

4562+
if ( dsUri.database().isEmpty() ) // typically when a service file is used
4563+
{
4564+
dsUri.setDatabase( conn->currentDatabase() );
4565+
}
4566+
45624567
QString uiFileColumn;
45634568
QString uiFileValue;
45644569
if ( !uiFileContent.isEmpty() )
@@ -4679,6 +4684,11 @@ QGISEXTERN QString loadStyle( const QString &uri, QString &errCause )
46794684
return QLatin1String( "" );
46804685
}
46814686

4687+
if ( dsUri.database().isEmpty() ) // typically when a service file is used
4688+
{
4689+
dsUri.setDatabase( conn->currentDatabase() );
4690+
}
4691+
46824692
if ( !tableExists( *conn, QStringLiteral( "layer_styles" ) ) )
46834693
{
46844694
return QLatin1String( "" );
@@ -4717,6 +4727,11 @@ QGISEXTERN int listStyles( const QString &uri, QStringList &ids, QStringList &na
47174727
return -1;
47184728
}
47194729

4730+
if ( dsUri.database().isEmpty() ) // typically when a service file is used
4731+
{
4732+
dsUri.setDatabase( conn->currentDatabase() );
4733+
}
4734+
47204735
QString selectRelatedQuery = QString( "SELECT id,styleName,description"
47214736
" FROM layer_styles"
47224737
" WHERE f_table_catalog=%1"

0 commit comments

Comments
 (0)