Skip to content

Commit

Permalink
postgres provider:
Browse files Browse the repository at this point in the history
- 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 1215d0b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
8 changes: 7 additions & 1 deletion src/providers/postgres/qgspostgresconn.cpp
Expand Up @@ -23,7 +23,10 @@
#include "qgsfield.h"
#include "qgspgtablemodel.h"

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

#include <climits>

// for htonl
Expand Down Expand Up @@ -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 */
Expand Down
47 changes: 28 additions & 19 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}


Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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.