From 027892022ea769bd690dd88deb602f55ad7848cd Mon Sep 17 00:00:00 2001 From: Alexey Karandashev Date: Thu, 8 Jun 2023 17:15:01 +0200 Subject: [PATCH] remove QgsPoolPostgresConn and move connection pointer ownership to QgsSQLConnPool --- src/core/providers/qgssqlconnpool.h | 15 +++++++++++ src/providers/postgres/qgspostgresconn.cpp | 10 ------- src/providers/postgres/qgspostgresconn.h | 12 --------- .../qgspostgresproviderconnection.cpp | 26 ++++++++----------- .../postgres/qgspostgresproviderconnection.h | 4 +-- 5 files changed, 28 insertions(+), 39 deletions(-) diff --git a/src/core/providers/qgssqlconnpool.h b/src/core/providers/qgssqlconnpool.h index 00b3baa99054..200abc4a721c 100644 --- a/src/core/providers/qgssqlconnpool.h +++ b/src/core/providers/qgssqlconnpool.h @@ -97,6 +97,21 @@ class QgsSqlConnectionPool : public QgsConnectionPool 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 getConnectionFromInstance( const QString &connInfo ) + { + return std::shared_ptr( instance()->acquireConnection( connInfo ), + []( T * connection ) + { + if ( connection ) + instance()->releaseConnection( connection ); + } ); + } + QString connectionToName( T *connection ) override { return connection->connInfo(); diff --git a/src/providers/postgres/qgspostgresconn.cpp b/src/providers/postgres/qgspostgresconn.cpp index fcee88e0e3c6..0f1a5c556125 100644 --- a/src/providers/postgres/qgspostgresconn.cpp +++ b/src/providers/postgres/qgspostgresconn.cpp @@ -48,16 +48,6 @@ #include #endif -QgsPoolPostgresConn::QgsPoolPostgresConn( const QString &connInfo ) - : mPgConn( QgsPostgresConnPool::instance()->acquireConnection( connInfo ) ) -{ -} - -QgsPoolPostgresConn::~QgsPoolPostgresConn() -{ - if ( mPgConn ) - QgsPostgresConnPool::instance()->releaseConnection( mPgConn ); -} QMap QgsPostgresConn::sConnectionsRO; diff --git a/src/providers/postgres/qgspostgresconn.h b/src/providers/postgres/qgspostgresconn.h index c74786384efe..f23d50bd761d 100644 --- a/src/providers/postgres/qgspostgresconn.h +++ b/src/providers/postgres/qgspostgresconn.h @@ -153,18 +153,6 @@ struct QgsPostgresLayerProperty #endif }; -//! Wraps acquireConnection() and releaseConnection() from a QgsPostgresConnPool. -// This can be used for creating std::shared_ptr. -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 QGS_QUERY_LOG_ORIGIN_PG_CON QString(QString( __FILE__ ).mid( sPostgresConQueryLogFilePrefixLength ) + ':' + QString::number( __LINE__ ) + " (" + __FUNCTION__ + ")") diff --git a/src/providers/postgres/qgspostgresproviderconnection.cpp b/src/providers/postgres/qgspostgresproviderconnection.cpp index d6e9516723b0..738f64f40053 100644 --- a/src/providers/postgres/qgspostgresproviderconnection.cpp +++ b/src/providers/postgres/qgspostgresproviderconnection.cpp @@ -255,17 +255,13 @@ QgsAbstractDatabaseProviderConnection::QueryResult QgsPostgresProviderConnection return execSqlPrivate( sql, true, feedback ); } -QList QgsPostgresProviderConnection::executeSqlPrivate( const QString &sql, bool resolveTypes, QgsFeedback *feedback, std::shared_ptr pgconn ) const +QList QgsPostgresProviderConnection::executeSqlPrivate( const QString &sql, bool resolveTypes, QgsFeedback *feedback, std::shared_ptr pgconn ) const { return execSqlPrivate( sql, resolveTypes, feedback, pgconn ).rows(); } -QgsAbstractDatabaseProviderConnection::QueryResult QgsPostgresProviderConnection::execSqlPrivate( const QString &sql, bool resolveTypes, QgsFeedback *feedback, std::shared_ptr pgconn ) const +QgsAbstractDatabaseProviderConnection::QueryResult QgsPostgresProviderConnection::execSqlPrivate( const QString &sql, bool resolveTypes, QgsFeedback *feedback, std::shared_ptr pgconn ) const { - if ( ! pgconn ) - { - pgconn = std::make_shared( QgsDataSourceUri( uri() ).connectionInfo( false ) ); - } std::shared_ptr iterator = std::make_shared( resolveTypes ); QueryResult results( iterator ); @@ -276,9 +272,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() ) ); } @@ -297,12 +293,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 res = std::make_unique( conn->LoggedPQexec( "QgsPostgresProviderConnection", sql ) ); + std::unique_ptr res = std::make_unique( pgconn->LoggedPQexec( "QgsPostgresProviderConnection", sql ) ); std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); results.setQueryExecutionTime( std::chrono::duration_cast( end - begin ).count() ); @@ -312,20 +308,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 ); } } diff --git a/src/providers/postgres/qgspostgresproviderconnection.h b/src/providers/postgres/qgspostgresproviderconnection.h index 0eefea2a7f3f..97ad0f67065c 100644 --- a/src/providers/postgres/qgspostgresproviderconnection.h +++ b/src/providers/postgres/qgspostgresproviderconnection.h @@ -86,8 +86,8 @@ class QgsPostgresProviderConnection : public QgsAbstractDatabaseProviderConnecti private: - QList 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 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;