Skip to content

Commit 29a137d

Browse files
authoredOct 24, 2017
Merge pull request #5381 from pblottiere/bugfix_style_218
[bugfix] Fixes #17234 save/load styles from Postgres when a service file is used
2 parents 2e87d32 + 6c6d837 commit 29a137d

File tree

5 files changed

+64
-8
lines changed

5 files changed

+64
-8
lines changed
 

‎src/core/qgsvectorlayer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4367,14 +4367,14 @@ void QgsVectorLayer::saveStyleToDatabase( const QString& name, const QString& de
43674367

43684368
QDomDocument qmlDocument, sldDocument;
43694369
this->exportNamedStyle( qmlDocument, msgError );
4370-
if ( !msgError.isNull() )
4370+
if ( !msgError.isEmpty() )
43714371
{
43724372
return;
43734373
}
43744374
qmlStyle = qmlDocument.toString();
43754375

43764376
this->exportSldStyle( sldDocument, msgError );
4377-
if ( !msgError.isNull() )
4377+
if ( !msgError.isEmpty() )
43784378
{
43794379
return;
43804380
}

‎src/providers/postgres/qgspostgresconn.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,7 @@ QString QgsPostgresConn::quotedValue( const QVariant& value )
956956
}
957957
}
958958

959-
PGresult *QgsPostgresConn::PQexec( const QString& query, bool logError )
959+
PGresult *QgsPostgresConn::PQexec( const QString& query, bool logError ) const
960960
{
961961
if ( PQstatus() != CONNECTION_OK )
962962
{
@@ -1138,13 +1138,13 @@ void QgsPostgresConn::PQfinish()
11381138
mConn = nullptr;
11391139
}
11401140

1141-
int QgsPostgresConn::PQstatus()
1141+
int QgsPostgresConn::PQstatus() const
11421142
{
11431143
Q_ASSERT( mConn );
11441144
return ::PQstatus( mConn );
11451145
}
11461146

1147-
QString QgsPostgresConn::PQerrorMessage()
1147+
QString QgsPostgresConn::PQerrorMessage() const
11481148
{
11491149
Q_ASSERT( mConn );
11501150
return QString::fromUtf8( ::PQerrorMessage( mConn ) );
@@ -1795,3 +1795,21 @@ bool QgsPostgresConn::cancel()
17951795

17961796
return res == 0;
17971797
}
1798+
1799+
QString QgsPostgresConn::currentDatabase() const
1800+
{
1801+
QString database;
1802+
QString sql = "SELECT current_database()";
1803+
QgsPostgresResult res( PQexec( sql ) );
1804+
1805+
if ( res.PQresultStatus() == PGRES_TUPLES_OK )
1806+
{
1807+
database = res.PQgetvalue( 0, 0 );
1808+
}
1809+
else
1810+
{
1811+
QgsMessageLog::logMessage( tr( "SQL:%1\nresult:%2\nerror:%3\n" ).arg( sql ).arg( res.PQresultStatus() ).arg( res.PQresultErrorMessage() ), tr( "PostGIS" ) );
1812+
}
1813+
1814+
return database;
1815+
}

‎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 );
@@ -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 2.18
317+
*/
318+
QString currentDatabase() const;
319+
313320
static const int sGeomTypeSelectLimit;
314321

315322
static QString displayStringForWkbType( QGis::WkbType wkbType );

‎src/providers/postgres/qgspostgresprovider.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4073,6 +4073,11 @@ QGISEXTERN bool saveStyle( const QString& uri, const QString& qmlStyle, const QS
40734073
}
40744074
}
40754075

4076+
if ( dsUri.database().isEmpty() ) // typically when a service file is used
4077+
{
4078+
dsUri.setDatabase( conn->currentDatabase() );
4079+
}
4080+
40764081
QString uiFileColumn;
40774082
QString uiFileValue;
40784083
if ( !uiFileContent.isEmpty() )
@@ -4199,6 +4204,11 @@ QGISEXTERN QString loadStyle( const QString& uri, QString& errCause )
41994204
return "";
42004205
}
42014206

4207+
if ( dsUri.database().isEmpty() ) // typically when a service file is used
4208+
{
4209+
dsUri.setDatabase( conn->currentDatabase() );
4210+
}
4211+
42024212
QString selectQmlQuery = QString( "SELECT styleQML"
42034213
" FROM layer_styles"
42044214
" WHERE f_table_catalog=%1"
@@ -4232,6 +4242,11 @@ QGISEXTERN int listStyles( const QString &uri, QStringList &ids, QStringList &na
42324242
return -1;
42334243
}
42344244

4245+
if ( dsUri.database().isEmpty() ) // typically when a service file is used
4246+
{
4247+
dsUri.setDatabase( conn->currentDatabase() );
4248+
}
4249+
42354250
QString selectRelatedQuery = QString( "SELECT id,styleName,description"
42364251
" FROM layer_styles"
42374252
" WHERE f_table_catalog=%1"

‎tests/src/python/test_provider_postgres.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,22 @@ def testKey(lyr, key, kfnames):
391391
testKey(lyr, '"f1","F2","f3"', ['f1', 'F2', 'f3'])
392392
testKey(lyr, None, ['id'])
393393

394+
def testStyleDatabaseWithService(self):
395+
myconn = 'service=\'qgis_test\''
396+
if 'QGIS_PGTEST_DB' in os.environ:
397+
myconn = os.environ['QGIS_PGTEST_DB']
398+
myvl = QgsVectorLayer(myconn + ' sslmode=disable key=\'pk\' srid=4326 type=POINT table="qgis_test"."someData" (geom) sql=', 'test', 'postgres')
399+
self.assertTrue(myvl.isValid())
400+
401+
styles = myvl.listStylesInDatabase()
402+
ids = styles[1]
403+
self.assertEqual(len(ids), 0)
404+
405+
myvl.saveStyleToDatabase('mystyle', '', False, '', '')
406+
styles = myvl.listStylesInDatabase()
407+
ids = styles[1]
408+
self.assertEqual(len(ids), 1)
409+
394410

395411
if __name__ == '__main__':
396412
unittest.main()

0 commit comments

Comments
 (0)
Please sign in to comment.