Skip to content
Permalink
Browse files

Fixes #17234 save/load styles from Postgres when a service file is used

  • Loading branch information
pblottiere committed Oct 16, 2017
1 parent 2e87d32 commit ac5d0f805cbfdaa4e592a69a9d5492563f340227
@@ -4367,14 +4367,14 @@ void QgsVectorLayer::saveStyleToDatabase( const QString& name, const QString& de

QDomDocument qmlDocument, sldDocument;
this->exportNamedStyle( qmlDocument, msgError );
if ( !msgError.isNull() )
if ( !msgError.isEmpty() )
{
return;
}
qmlStyle = qmlDocument.toString();

this->exportSldStyle( sldDocument, msgError );
if ( !msgError.isNull() )
if ( !msgError.isEmpty() )
{
return;
}
@@ -956,7 +956,7 @@ QString QgsPostgresConn::quotedValue( const QVariant& value )
}
}

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

int QgsPostgresConn::PQstatus()
int QgsPostgresConn::PQstatus() const
{
Q_ASSERT( mConn );
return ::PQstatus( mConn );
}

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

return res == 0;
}

QString QgsPostgresConn::currentDatabase() const
{
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;
}
@@ -244,11 +244,11 @@ class QgsPostgresConn : public QObject
//

// run a query and check for errors
PGresult *PQexec( const QString& query, bool logError = true );
PGresult *PQexec( const QString& query, bool logError = true ) const;
void PQfinish();
QString PQerrorMessage();
QString PQerrorMessage() const;
int PQsendQuery( const QString& query );
int PQstatus();
int PQstatus() const;
PGresult *PQgetResult();
PGresult *PQprepare( const QString& stmtName, const QString& query, int nParams, const Oid *paramTypes );
PGresult *PQexecPrepared( const QString& stmtName, const QStringList &params );
@@ -310,6 +310,13 @@ class QgsPostgresConn : public QObject

QString connInfo() const { return mConnInfo; }

/**
* Returns the underlying database.
*
* @since QGIS 2.18
*/
QString currentDatabase() const;

static const int sGeomTypeSelectLimit;

static QString displayStringForWkbType( QGis::WkbType wkbType );
@@ -4073,6 +4073,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() )
@@ -4199,6 +4204,11 @@ QGISEXTERN QString loadStyle( const QString& uri, QString& errCause )
return "";
}

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

QString selectQmlQuery = QString( "SELECT styleQML"
" FROM layer_styles"
" WHERE f_table_catalog=%1"
@@ -4232,6 +4242,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"

0 comments on commit ac5d0f8

Please sign in to comment.
You can’t perform that action at this time.