Skip to content
Permalink
Browse files

Create QgsPoolPostgresConn for RAII usage.

Wraps acquireConnection() and releaseConnection() from a QgsPostgresConnPool.

This can be used for creating std::shared_ptr<PoolPostgresConn>.

(cherry picked from commit 9c5f35c)
  • Loading branch information
tomtor authored and nyalldawson committed Nov 20, 2020
1 parent 19e2758 commit 68a5a5cd4a6b7142ffefdb1fe1bc9b31d4bbca6f
@@ -29,6 +29,7 @@
#include "qgssettings.h"
#include "qgsjsonutils.h"
#include "qgspostgresstringutils.h"
#include "qgspostgresconnpool.h"

#include <QApplication>
#include <QStringList>
@@ -142,6 +143,17 @@ Oid QgsPostgresResult::PQoidValue()
return ::PQoidValue( mRes );
}

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;
@@ -188,6 +188,17 @@ class QgsPostgresResult

};

//! 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; }
};

class QgsPostgresConn : public QObject
{
@@ -198,7 +198,7 @@ QList<QVariantList> QgsPostgresProviderConnection::executeSql( const QString &sq
return executeSqlPrivate( sql, true, feedback );
}

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

@@ -208,9 +208,10 @@ QList<QVariantList> QgsPostgresProviderConnection::executeSqlPrivate( const QStr
return results;
}

const QgsDataSourceUri dsUri { uri() };
QgsPostgresConn *conn = pgconn ? pgconn : QgsPostgresConnPool::instance()->acquireConnection( dsUri.connectionInfo( false ) );
if ( !conn )
if ( ! pgconn )
pgconn = std::shared_ptr<QgsPoolPostgresConn>( new QgsPoolPostgresConn( QgsDataSourceUri( uri() ).connectionInfo( false ) ) );
QgsPostgresConn *conn = pgconn->get();
if ( ! conn )
{
throw QgsProviderConnectionException( QObject::tr( "Connection failed: %1" ).arg( uri() ) );
}
@@ -269,7 +270,7 @@ QList<QVariantList> QgsPostgresProviderConnection::executeSqlPrivate( const QStr
break;
}
const Oid oid { res.PQftype( rowIdx ) };
QList<QVariantList> typeRes { executeSqlPrivate( QStringLiteral( "SELECT typname FROM pg_type WHERE oid = %1" ).arg( oid ), false, nullptr, conn ) };
QList<QVariantList> typeRes { executeSqlPrivate( QStringLiteral( "SELECT typname FROM pg_type WHERE oid = %1" ).arg( oid ), false, nullptr, pgconn ) };
// Set the default to string
QVariant::Type vType { QVariant::Type::String };
if ( typeRes.size() > 0 && typeRes.first().size() > 0 )
@@ -357,8 +358,6 @@ QList<QVariantList> QgsPostgresProviderConnection::executeSqlPrivate( const QStr
results.push_back( row );
}
}
if ( ! pgconn )
QgsPostgresConnPool::instance()->releaseConnection( conn );
if ( ! errCause.isEmpty() )
{
throw QgsProviderConnectionException( errCause );
@@ -61,7 +61,7 @@ class QgsPostgresProviderConnection : public QgsAbstractDatabaseProviderConnecti

private:

QList<QVariantList> executeSqlPrivate( const QString &sql, bool resolveTypes = true, QgsFeedback *feedback = nullptr, class QgsPostgresConn *pgconn = nullptr ) const;
QList<QVariantList> executeSqlPrivate( const QString &sql, bool resolveTypes = true, QgsFeedback *feedback = nullptr, std::shared_ptr< class QgsPoolPostgresConn > 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;

0 comments on commit 68a5a5c

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