Skip to content

Commit

Permalink
Fix db login procedure for listener
Browse files Browse the repository at this point in the history
  • Loading branch information
MorriganR authored and nyalldawson committed May 5, 2021
1 parent 89c069f commit b7f990d
Showing 1 changed file with 38 additions and 7 deletions.
45 changes: 38 additions & 7 deletions src/providers/postgres/qgspostgreslistener.cpp
Expand Up @@ -16,7 +16,8 @@
***************************************************************************/

#include "qgspostgreslistener.h"

#include "qgsdatasourceuri.h"
#include "qgscredentials.h"
#include "qgslogger.h"

#ifdef Q_OS_WIN
Expand Down Expand Up @@ -58,7 +59,42 @@ QgsPostgresListener::~QgsPostgresListener()
void QgsPostgresListener::run()
{
PGconn *conn = nullptr;
conn = PQconnectdb( mConnString.toLocal8Bit() );

conn = PQconnectdb( mConnString.toUtf8() );

if ( PQstatus( conn ) != CONNECTION_OK )
{
QgsDataSourceUri uri( mConnString );
QString username = uri.username();
QString password = uri.password();

PQfinish( conn );

QgsCredentials::instance()->lock();

if ( QgsCredentials::instance()->get( mConnString, username, password, PQerrorMessage( conn ) ) )
{
if ( !username.isEmpty() )
uri.setUsername( username );

if ( !password.isEmpty() )
uri.setPassword( password );

QString connectString = uri.connectionInfo( false );
conn = PQconnectdb( connectString.toUtf8() );
if ( PQstatus( conn ) == CONNECTION_OK )
QgsCredentials::instance()->put( mConnString, username, password );
}

QgsCredentials::instance()->unlock();

if ( PQstatus( conn ) != CONNECTION_OK )
{
QgsDebugMsg( QStringLiteral( "LISTENer not started" ) );
return;
}
}


PGresult *res = PQexec( conn, "LISTEN qgis" );
if ( PQresultStatus( res ) != PGRES_COMMAND_OK )
Expand Down Expand Up @@ -94,7 +130,6 @@ void QgsPostgresListener::run()
timeout.tv_sec = 1;
timeout.tv_usec = 0;

QgsDebugMsg( QStringLiteral( "select in the loop" ) );
if ( select( sock + 1, &input_mask, nullptr, nullptr, &timeout ) < 0 )
{
QgsDebugMsg( QStringLiteral( "error in select" ) );
Expand All @@ -110,10 +145,6 @@ void QgsPostgresListener::run()
QgsDebugMsg( "notify " + msg );
PQfreemem( n );
}
else
{
QgsDebugMsg( QStringLiteral( "not a notify" ) );
}

if ( mStop )
{
Expand Down

0 comments on commit b7f990d

Please sign in to comment.