Skip to content

Commit

Permalink
remove QgsPoolPostgresConn and move connection pointer ownership to Q…
Browse files Browse the repository at this point in the history
…gsSQLConnPool
  • Loading branch information
Alexey Karandashev committed Jun 13, 2023
1 parent dbb2065 commit e555804
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 41 deletions.
15 changes: 15 additions & 0 deletions src/core/providers/qgssqlconnpool.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,21 @@ class QgsSqlConnectionPool : public QgsConnectionPool<T *, T_Group>
sInstance = nullptr;
}


/**
* Call instance and acquire a connection, return a shared pointer with a destructor
* that will call the connection pool instance's release of the connection.
*/
static std::shared_ptr<T> getConnectionFromInstance( const QString &connInfo )
{
return std::shared_ptr<T>( instance()->acquireConnection( connInfo ),
[]( T * connection )
{
if ( connection )
instance()->releaseConnection( connection );
} );
}

QString connectionToName( T *connection ) override
{
return connection->connInfo();
Expand Down
12 changes: 0 additions & 12 deletions src/providers/postgres/qgspostgresconn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,6 @@

const int PG_DEFAULT_TIMEOUT = 30;

QgsPoolPostgresConn::QgsPoolPostgresConn( const QString &connInfo )
: mPgConn( QgsPostgresConnPool::instance()->acquireConnection( connInfo ) )
{
}

QgsPoolPostgresConn::~QgsPoolPostgresConn()
{
if ( mPgConn )
QgsPostgresConnPool::instance()->releaseConnection( mPgConn );
}


QMap<QString, QgsPostgresConn *> QgsPostgresConn::sConnectionsRO;
QMap<QString, QgsPostgresConn *> QgsPostgresConn::sConnectionsRW;

Expand Down
12 changes: 0 additions & 12 deletions src/providers/postgres/qgspostgresconn.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,6 @@ struct QgsPostgresLayerProperty
#endif
};

//! Wraps acquireConnection() and releaseConnection() from a QgsPostgresConnPool.
// This can be used for creating std::shared_ptr<QgsPoolPostgresConn>.
class QgsPoolPostgresConn
{
class QgsPostgresConn *mPgConn;
public:
QgsPoolPostgresConn( const QString &connInfo );
~QgsPoolPostgresConn();

class QgsPostgresConn *get() const { return mPgConn; }
};

#include "qgsconfig.h"
constexpr int sPostgresConQueryLogFilePrefixLength = CMAKE_SOURCE_DIR[sizeof( CMAKE_SOURCE_DIR ) - 1] == '/' ? sizeof( CMAKE_SOURCE_DIR ) + 1 : sizeof( CMAKE_SOURCE_DIR );
#define LoggedPQexecNR(_class, query) PQexecNR( query, _class, QString(QString( __FILE__ ).mid( sPostgresConQueryLogFilePrefixLength ) + ':' + QString::number( __LINE__ ) + " (" + __FUNCTION__ + ")") )
Expand Down
26 changes: 11 additions & 15 deletions src/providers/postgres/qgspostgresproviderconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,17 +261,13 @@ QgsAbstractDatabaseProviderConnection::QueryResult QgsPostgresProviderConnection
return execSqlPrivate( sql, true, feedback );
}

QList<QVariantList> QgsPostgresProviderConnection::executeSqlPrivate( const QString &sql, bool resolveTypes, QgsFeedback *feedback, std::shared_ptr<QgsPoolPostgresConn> pgconn ) const
QList<QVariantList> QgsPostgresProviderConnection::executeSqlPrivate( const QString &sql, bool resolveTypes, QgsFeedback *feedback, std::shared_ptr<QgsPostgresConn> pgconn ) const
{
return execSqlPrivate( sql, resolveTypes, feedback, pgconn ).rows();
}

QgsAbstractDatabaseProviderConnection::QueryResult QgsPostgresProviderConnection::execSqlPrivate( const QString &sql, bool resolveTypes, QgsFeedback *feedback, std::shared_ptr<QgsPoolPostgresConn> pgconn ) const
QgsAbstractDatabaseProviderConnection::QueryResult QgsPostgresProviderConnection::execSqlPrivate( const QString &sql, bool resolveTypes, QgsFeedback *feedback, std::shared_ptr<QgsPostgresConn> pgconn ) const
{
if ( ! pgconn )
{
pgconn = std::make_shared<QgsPoolPostgresConn>( QgsDataSourceUri( uri() ).connectionInfo( false ) );
}

std::shared_ptr<QgsAbstractDatabaseProviderConnection::QueryResult::QueryResultIterator> iterator = std::make_shared<QgsPostgresProviderResultIterator>( resolveTypes );
QueryResult results( iterator );
Expand All @@ -282,9 +278,9 @@ QgsAbstractDatabaseProviderConnection::QueryResult QgsPostgresProviderConnection
return results;
}

QgsPostgresConn *conn = pgconn->get();
pgconn = QgsPostgresConnPool::getConnectionFromInstance( QgsDataSourceUri( uri() ).connectionInfo( false ) );

if ( ! conn )
if ( ! pgconn )
{
throw QgsProviderConnectionException( QObject::tr( "Connection failed: %1" ).arg( uri() ) );
}
Expand All @@ -303,12 +299,12 @@ QgsAbstractDatabaseProviderConnection::QueryResult QgsPostgresProviderConnection
qtConnection = QObject::connect( feedback, &QgsFeedback::canceled, [ &pgconn ]
{
if ( pgconn )
pgconn->get()->PQCancel();
pgconn->PQCancel();
} );
}

std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
std::unique_ptr<QgsPostgresResult> res = std::make_unique<QgsPostgresResult>( conn->LoggedPQexec( "QgsPostgresProviderConnection", sql ) );
std::unique_ptr<QgsPostgresResult> res = std::make_unique<QgsPostgresResult>( pgconn->LoggedPQexec( "QgsPostgresProviderConnection", sql ) );
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
results.setQueryExecutionTime( std::chrono::duration_cast<std::chrono::milliseconds>( end - begin ).count() );

Expand All @@ -318,20 +314,20 @@ QgsAbstractDatabaseProviderConnection::QueryResult QgsPostgresProviderConnection
}

QString errCause;
if ( conn->PQstatus() != CONNECTION_OK || ! res->result() )
if ( pgconn->PQstatus() != CONNECTION_OK || ! res->result() )
{
errCause = QObject::tr( "Connection error: %1 returned %2 [%3]" )
.arg( sql ).arg( conn->PQstatus() )
.arg( conn->PQerrorMessage() );
.arg( sql ).arg( pgconn->PQstatus() )
.arg( pgconn->PQerrorMessage() );
}
else
{
const QString err { conn->PQerrorMessage() };
const QString err { pgconn->PQerrorMessage() };
if ( ! err.isEmpty() )
{
errCause = QObject::tr( "SQL error: %1 returned %2 [%3]" )
.arg( sql )
.arg( conn->PQstatus() )
.arg( pgconn->PQstatus() )
.arg( err );
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/providers/postgres/qgspostgresproviderconnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ class QgsPostgresProviderConnection : public QgsAbstractDatabaseProviderConnecti

private:

QList<QVariantList> executeSqlPrivate( const QString &sql, bool resolveTypes = true, QgsFeedback *feedback = nullptr, std::shared_ptr< class QgsPoolPostgresConn > pgconn = nullptr ) const;
QgsAbstractDatabaseProviderConnection::QueryResult execSqlPrivate( const QString &sql, bool resolveTypes = true, QgsFeedback *feedback = nullptr, std::shared_ptr< class QgsPoolPostgresConn > pgconn = nullptr ) const;
QList<QVariantList> executeSqlPrivate( const QString &sql, bool resolveTypes = true, QgsFeedback *feedback = nullptr, std::shared_ptr< class QgsPostgresConn > pgconn = nullptr ) const;
QgsAbstractDatabaseProviderConnection::QueryResult execSqlPrivate( const QString &sql, bool resolveTypes = true, QgsFeedback *feedback = nullptr, std::shared_ptr< class QgsPostgresConn > pgconn = nullptr ) const;
void setDefaultCapabilities();
void dropTablePrivate( const QString &schema, const QString &name ) const;
void renameTablePrivate( const QString &schema, const QString &name, const QString &newName ) const;
Expand Down

0 comments on commit e555804

Please sign in to comment.