Skip to content
Permalink
Browse files

postgres provider:

- fix connection cleanup from threads
- fix disconnection of style functions

fixes #9280 (adapted PR#1051)
  • Loading branch information
jef-n committed Jan 3, 2014
1 parent 72552bc commit 1215d0b016367f6f7ce6e938b326a26227766a97
Showing with 35 additions and 20 deletions.
  1. +7 −1 src/providers/postgres/qgspostgresconn.cpp
  2. +28 −19 src/providers/postgres/qgspostgresprovider.cpp
@@ -23,7 +23,10 @@
#include "qgsfield.h"
#include "qgspgtablemodel.h"

#include <QApplication>
#include <QSettings>
#include <QThread>

#include <climits>

// for htonl
@@ -267,7 +270,10 @@ void QgsPostgresConn::disconnect()
Q_ASSERT( !key.isNull() );
connections.remove( key );

deleteLater();
if ( QThread::currentThread() == QApplication::instance()->thread() )
deleteLater();
else
delete this;
}

/* private */
@@ -3360,6 +3360,7 @@ QGISEXTERN bool saveStyle( const QString& uri, const QString& qmlStyle, const QS
QMessageBox::Yes | QMessageBox::No ) == QMessageBox::No )
{
errCause = QObject::tr( "Operation aborted. No changes were made in the database" );
conn->disconnect();
return false;
}

@@ -3402,14 +3403,14 @@ QGISEXTERN bool saveStyle( const QString& uri, const QString& qmlStyle, const QS
}

res = conn->PQexec( sql );
conn->disconnect();
if ( res.PQresultStatus() != PGRES_COMMAND_OK )
{

bool saved = res.PQresultStatus() == PGRES_COMMAND_OK;
if ( !saved )
errCause = QObject::tr( "Unable to save layer style. It's not possible to insert a new record into the style table. Maybe this is due to table permissions (user=%1). Please contact your database administrator." ).arg( dsUri.username() );
return false;
}

return true;
conn->disconnect();

return saved;
}


@@ -3439,7 +3440,9 @@ QGISEXTERN QString loadStyle( const QString& uri, QString& errCause )

QgsPostgresResult result = conn->PQexec( selectQmlQuery, false );

return result.PQntuples() == 1 ? result.PQgetvalue( 0, 0 ) : "";
QString style = result.PQntuples() == 1 ? result.PQgetvalue( 0, 0 ) : "";
conn->disconnect();
return style;
}

QGISEXTERN int listStyles( const QString &uri, QStringList &ids, QStringList &names,
@@ -3470,6 +3473,7 @@ QGISEXTERN int listStyles( const QString &uri, QStringList &ids, QStringList &na
{
QgsMessageLog::logMessage( QObject::tr( "Error executing query: %1" ).arg( selectRelatedQuery ) );
errCause = QObject::tr( "Error executing the select query for related styles. The query was logged" );
conn->disconnect();
return -1;
}

@@ -3495,15 +3499,19 @@ QGISEXTERN int listStyles( const QString &uri, QStringList &ids, QStringList &na
{
QgsMessageLog::logMessage( QObject::tr( "Error executing query: %1" ).arg( selectOthersQuery ) );
errCause = QObject::tr( "Error executing the select query for unrelated styles. The query was logged" );
conn->disconnect();
return -1;
}

for ( int i = 0; i < result.PQntuples(); i++ )
{
ids.append( result.PQgetvalue( i, 0 ) );
names.append( result.PQgetvalue( i, 1 ) );
descriptions.append( result.PQgetvalue( i, 2 ) );
}

conn->disconnect();

return numberOfRelatedStyles;
}

@@ -3515,25 +3523,26 @@ QGISEXTERN QString getStyleById( const QString& uri, QString styleId, QString& e
if ( !conn )
{
errCause = QObject::tr( "Connection to database failed using username: %1" ).arg( dsUri.username() );
return QObject::tr( "" );
return "";
}

QString style;
QString selectQmlQuery = QString( "SELECT styleQml FROM layer_styles WHERE id=%1" ).arg( QgsPostgresConn::quotedValue( styleId ) );
QgsPostgresResult result = conn->PQexec( selectQmlQuery );
if ( result.PQresultStatus() != PGRES_TUPLES_OK )
if ( result.PQresultStatus() == PGRES_TUPLES_OK )
{
QgsMessageLog::logMessage( QObject::tr( "Error executing query: %1" ).arg( selectQmlQuery ) );
errCause = QObject::tr( "Error executing the select query. The query was logged" );
return "";
}

if ( result.PQntuples() == 1 )
{
return result.PQgetvalue( 0, 0 );
if ( result.PQntuples() == 1 )
style = result.PQgetvalue( 0, 0 );
else
errCause = QObject::tr( "Consistency error in table '%1'. Style id should be unique" ).arg( "layer_styles" );
}
else
{
errCause = QObject::tr( "Consistency error in table '%1'. Style id should be unique" ).arg( "layer_styles" );
return "";
QgsMessageLog::logMessage( QObject::tr( "Error executing query: %1" ).arg( selectQmlQuery ) );
errCause = QObject::tr( "Error executing the select query. The query was logged" );
}

conn->disconnect();

return style;
}

0 comments on commit 1215d0b

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