Skip to content

Commit d4635d0

Browse files
authoredOct 16, 2017
Merge pull request #5317 from pblottiere/bugfix_style
[bugfix] Fixes #17234 save/load styles from Postgres when a service file is used
2 parents afb2a6a + 562e9b4 commit d4635d0

File tree

4 files changed

+69
-7
lines changed

4 files changed

+69
-7
lines changed
 

‎src/providers/postgres/qgspostgresconn.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,7 +1010,7 @@ QString QgsPostgresConn::quotedValue( const QVariant &value )
10101010
}
10111011
}
10121012

1013-
PGresult *QgsPostgresConn::PQexec( const QString &query, bool logError )
1013+
PGresult *QgsPostgresConn::PQexec( const QString &query, bool logError ) const
10141014
{
10151015
if ( PQstatus() != CONNECTION_OK )
10161016
{
@@ -1192,13 +1192,13 @@ void QgsPostgresConn::PQfinish()
11921192
mConn = nullptr;
11931193
}
11941194

1195-
int QgsPostgresConn::PQstatus()
1195+
int QgsPostgresConn::PQstatus() const
11961196
{
11971197
Q_ASSERT( mConn );
11981198
return ::PQstatus( mConn );
11991199
}
12001200

1201-
QString QgsPostgresConn::PQerrorMessage()
1201+
QString QgsPostgresConn::PQerrorMessage() const
12021202
{
12031203
Q_ASSERT( mConn );
12041204
return QString::fromUtf8( ::PQerrorMessage( mConn ) );
@@ -1855,3 +1855,21 @@ bool QgsPostgresConn::cancel()
18551855

18561856
return res == 0;
18571857
}
1858+
1859+
QString QgsPostgresConn::currentDatabase() const
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: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,11 @@ class QgsPostgresConn : public QObject
244244
//
245245

246246
// run a query and check for errors
247-
PGresult *PQexec( const QString &query, bool logError = true );
247+
PGresult *PQexec( const QString &query, bool logError = true ) const;
248248
void PQfinish();
249-
QString PQerrorMessage();
249+
QString PQerrorMessage() const;
250250
int PQsendQuery( const QString &query );
251-
int PQstatus();
251+
int PQstatus() const;
252252
PGresult *PQgetResult();
253253
PGresult *PQprepare( const QString &stmtName, const QString &query, int nParams, const Oid *paramTypes );
254254
PGresult *PQexecPrepared( const QString &stmtName, const QStringList &params );
@@ -315,6 +315,13 @@ class QgsPostgresConn : public QObject
315315

316316
QString connInfo() const { return mConnInfo; }
317317

318+
/**
319+
* Returns the underlying database.
320+
*
321+
* \since QGIS 3.0
322+
*/
323+
QString currentDatabase() const;
324+
318325
static const int GEOM_TYPE_SELECT_LIMIT;
319326

320327
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
@@ -4560,6 +4560,11 @@ QGISEXTERN bool saveStyle( const QString &uri, const QString &qmlStyle, const QS
45604560
}
45614561
}
45624562

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

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

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

‎tests/src/python/test_provider_postgres.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
QgsTransactionGroup,
3333
QgsReadWriteContext,
3434
QgsRectangle,
35-
QgsDefaultValue
35+
QgsDefaultValue,
36+
QgsDataSourceUri
3637
)
3738
from qgis.gui import QgsGui
3839
from qgis.PyQt.QtCore import QDate, QTime, QDateTime, QVariant, QDir, QObject
@@ -847,6 +848,27 @@ def receive(self, msg):
847848

848849
self.assertTrue(ok)
849850

851+
def testStyleDatabaseWithService(self):
852+
853+
myconn = 'service=\'qgis_test\''
854+
if 'QGIS_PGTEST_DB' in os.environ:
855+
myconn = os.environ['QGIS_PGTEST_DB']
856+
myvl = QgsVectorLayer(myconn + ' sslmode=disable key=\'pk\' srid=4326 type=POINT table="qgis_test"."someData" (geom) sql=', 'test', 'postgres')
857+
858+
styles = myvl.listStylesInDatabase()
859+
ids = styles[1]
860+
self.assertEqual(len(ids), 0)
861+
862+
myvl.saveStyleToDatabase('mystyle', '', False, '')
863+
styles = myvl.listStylesInDatabase()
864+
ids = styles[1]
865+
self.assertEqual(len(ids), 1)
866+
867+
myvl.deleteStyleFromDatabase(ids[0])
868+
styles = myvl.listStylesInDatabase()
869+
ids = styles[1]
870+
self.assertEqual(len(ids), 0)
871+
850872

851873
class TestPyQgsPostgresProviderCompoundKey(unittest.TestCase, ProviderTestCase):
852874

0 commit comments

Comments
 (0)
Please sign in to comment.