diff --git a/src/providers/postgres/CMakeLists.txt b/src/providers/postgres/CMakeLists.txt index 6237b6976e57..b824f30dbd90 100644 --- a/src/providers/postgres/CMakeLists.txt +++ b/src/providers/postgres/CMakeLists.txt @@ -7,6 +7,7 @@ set(PG_SRCS qgspostgresdataitems.cpp qgspostgresfeatureiterator.cpp qgspostgresprojectstorage.cpp + qgspostgresresult.cpp qgspostgrestransaction.cpp qgspgtablemodel.cpp qgscolumntypethread.cpp @@ -95,6 +96,7 @@ set(PGRASTER_SRCS raster/qgspostgresrasterutils.cpp qgspostgresconn.cpp qgspostgresprovidermetadatautils.cpp + qgspostgresresult.cpp ) # static library diff --git a/src/providers/postgres/qgspostgresconn.cpp b/src/providers/postgres/qgspostgresconn.cpp index 7ffe51bd3a4b..7ff031a96946 100644 --- a/src/providers/postgres/qgspostgresconn.cpp +++ b/src/providers/postgres/qgspostgresconn.cpp @@ -16,6 +16,7 @@ ***************************************************************************/ #include "qgspostgresconn.h" +#include "qgspostgresresult.h" #include "qgslogger.h" #include "qgsdatasourceuri.h" #include "qgsmessagelog.h" @@ -48,101 +49,6 @@ const int PG_DEFAULT_TIMEOUT = 30; -QgsPostgresResult::~QgsPostgresResult() -{ - if ( mRes ) - ::PQclear( mRes ); - mRes = nullptr; -} - -QgsPostgresResult &QgsPostgresResult::operator=( PGresult *result ) -{ - if ( mRes ) - ::PQclear( mRes ); - mRes = result; - return *this; -} - -QgsPostgresResult &QgsPostgresResult::operator=( const QgsPostgresResult &src ) -{ - if ( mRes ) - ::PQclear( mRes ); - mRes = src.result(); - return *this; -} - -ExecStatusType QgsPostgresResult::PQresultStatus() -{ - return mRes ? ::PQresultStatus( mRes ) : PGRES_FATAL_ERROR; -} - -QString QgsPostgresResult::PQresultErrorMessage() -{ - return mRes ? QString::fromUtf8( ::PQresultErrorMessage( mRes ) ) : QObject::tr( "no result buffer" ); -} - -int QgsPostgresResult::PQntuples() -{ - Q_ASSERT( mRes ); - return ::PQntuples( mRes ); -} - -QString QgsPostgresResult::PQgetvalue( int row, int col ) -{ - Q_ASSERT( mRes ); - return PQgetisnull( row, col ) - ? QString() - : QString::fromUtf8( ::PQgetvalue( mRes, row, col ) ); -} - -bool QgsPostgresResult::PQgetisnull( int row, int col ) -{ - Q_ASSERT( mRes ); - return ::PQgetisnull( mRes, row, col ); -} - -int QgsPostgresResult::PQnfields() -{ - Q_ASSERT( mRes ); - return ::PQnfields( mRes ); -} - -QString QgsPostgresResult::PQfname( int col ) -{ - Q_ASSERT( mRes ); - return QString::fromUtf8( ::PQfname( mRes, col ) ); -} - -Oid QgsPostgresResult::PQftable( int col ) -{ - Q_ASSERT( mRes ); - return ::PQftable( mRes, col ); -} - -int QgsPostgresResult::PQftablecol( int col ) -{ - Q_ASSERT( mRes ); - return ::PQftablecol( mRes, col ); -} - -Oid QgsPostgresResult::PQftype( int col ) -{ - Q_ASSERT( mRes ); - return ::PQftype( mRes, col ); -} - -int QgsPostgresResult::PQfmod( int col ) -{ - Q_ASSERT( mRes ); - return ::PQfmod( mRes, col ); -} - -Oid QgsPostgresResult::PQoidValue() -{ - Q_ASSERT( mRes ); - return ::PQoidValue( mRes ); -} - QgsPoolPostgresConn::QgsPoolPostgresConn( const QString &connInfo ) : mPgConn( QgsPostgresConnPool::instance()->acquireConnection( connInfo ) ) { diff --git a/src/providers/postgres/qgspostgresconn.h b/src/providers/postgres/qgspostgresconn.h index 38e313d98418..e69e715ce09d 100644 --- a/src/providers/postgres/qgspostgresconn.h +++ b/src/providers/postgres/qgspostgresconn.h @@ -29,6 +29,7 @@ #include "qgswkbtypes.h" #include "qgsconfig.h" #include "qgsvectordataprovider.h" +#include "qgspostgresresult.h" extern "C" { @@ -151,54 +152,6 @@ struct QgsPostgresLayerProperty #endif }; -class QgsPostgresResult -{ - public: - explicit QgsPostgresResult( PGresult *result = nullptr ) : mRes( result ) {} - ~QgsPostgresResult(); - - QgsPostgresResult &operator=( PGresult *result ); - QgsPostgresResult &operator=( const QgsPostgresResult &src ); - - QgsPostgresResult( const QgsPostgresResult &rh ) = delete; - - ExecStatusType PQresultStatus(); - QString PQresultErrorMessage(); - - int PQntuples(); - QString PQgetvalue( int row, int col ); - bool PQgetisnull( int row, int col ); - - int PQnfields(); - QString PQfname( int col ); - Oid PQftable( int col ); - Oid PQftype( int col ); - int PQfmod( int col ); - int PQftablecol( int col ); - Oid PQoidValue(); - - PGresult *result() const { return mRes; } - - private: - PGresult *mRes = nullptr; - -}; - -struct PGException -{ - explicit PGException( QgsPostgresResult &r ) - : mWhat( r.PQresultErrorMessage() ) - {} - - QString errorMessage() const - { - return mWhat; - } - - private: - QString mWhat; -}; - //! Wraps acquireConnection() and releaseConnection() from a QgsPostgresConnPool. // This can be used for creating std::shared_ptr. class QgsPoolPostgresConn diff --git a/src/providers/postgres/qgspostgresresult.cpp b/src/providers/postgres/qgspostgresresult.cpp new file mode 100644 index 000000000000..39fdb03cebe5 --- /dev/null +++ b/src/providers/postgres/qgspostgresresult.cpp @@ -0,0 +1,118 @@ +/*************************************************************************** + qgspostgresconn.cpp - connection class to PostgreSQL/PostGIS + ------------------- + begin : 2011/01/28 + copyright : (C) 2011 by Juergen E. Fischer + email : jef at norbit dot de + ***************************************************************************/ + +/*************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +#include "qgspostgresresult.h" + +#include + +QgsPostgresResult::~QgsPostgresResult() +{ + if ( mRes ) + ::PQclear( mRes ); + mRes = nullptr; +} + +QgsPostgresResult &QgsPostgresResult::operator=( PGresult *result ) +{ + if ( mRes ) + ::PQclear( mRes ); + mRes = result; + return *this; +} + +QgsPostgresResult &QgsPostgresResult::operator=( const QgsPostgresResult &src ) +{ + if ( this == &src ) + return *this; + + if ( mRes ) + ::PQclear( mRes ); + mRes = src.result(); + return *this; +} + +ExecStatusType QgsPostgresResult::PQresultStatus() +{ + return mRes ? ::PQresultStatus( mRes ) : PGRES_FATAL_ERROR; +} + +QString QgsPostgresResult::PQresultErrorMessage() +{ + return mRes ? QString::fromUtf8( ::PQresultErrorMessage( mRes ) ) : QObject::tr( "no result buffer" ); +} + +int QgsPostgresResult::PQntuples() +{ + Q_ASSERT( mRes ); + return ::PQntuples( mRes ); +} + +QString QgsPostgresResult::PQgetvalue( int row, int col ) +{ + Q_ASSERT( mRes ); + return PQgetisnull( row, col ) + ? QString() + : QString::fromUtf8( ::PQgetvalue( mRes, row, col ) ); +} + +bool QgsPostgresResult::PQgetisnull( int row, int col ) +{ + Q_ASSERT( mRes ); + return ::PQgetisnull( mRes, row, col ); +} + +int QgsPostgresResult::PQnfields() +{ + Q_ASSERT( mRes ); + return ::PQnfields( mRes ); +} + +QString QgsPostgresResult::PQfname( int col ) +{ + Q_ASSERT( mRes ); + return QString::fromUtf8( ::PQfname( mRes, col ) ); +} + +Oid QgsPostgresResult::PQftable( int col ) +{ + Q_ASSERT( mRes ); + return ::PQftable( mRes, col ); +} + +int QgsPostgresResult::PQftablecol( int col ) +{ + Q_ASSERT( mRes ); + return ::PQftablecol( mRes, col ); +} + +Oid QgsPostgresResult::PQftype( int col ) +{ + Q_ASSERT( mRes ); + return ::PQftype( mRes, col ); +} + +int QgsPostgresResult::PQfmod( int col ) +{ + Q_ASSERT( mRes ); + return ::PQfmod( mRes, col ); +} + +Oid QgsPostgresResult::PQoidValue() +{ + Q_ASSERT( mRes ); + return ::PQoidValue( mRes ); +} diff --git a/src/providers/postgres/qgspostgresresult.h b/src/providers/postgres/qgspostgresresult.h new file mode 100644 index 000000000000..925e5544897e --- /dev/null +++ b/src/providers/postgres/qgspostgresresult.h @@ -0,0 +1,74 @@ +/*************************************************************************** + qgspostgresconn.h - connection class to PostgreSQL/PostGIS + ------------------- + begin : 2011/01/28 + copyright : (C) 2011 by Juergen E. Fischer + email : jef at norbit dot de + ***************************************************************************/ + +/*************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +#ifndef QGSPOSTGRESRESULT_H +#define QGSPOSTGRESRESULT_H + + +#include + +extern "C" +{ +#include +} + +class QgsPostgresResult +{ + public: + explicit QgsPostgresResult( PGresult *result = nullptr ) : mRes( result ) {} + ~QgsPostgresResult(); + + QgsPostgresResult &operator=( PGresult *result ); + QgsPostgresResult &operator=( const QgsPostgresResult &src ); + + QgsPostgresResult( const QgsPostgresResult &rh ) = delete; + + ExecStatusType PQresultStatus(); + QString PQresultErrorMessage(); + + int PQntuples(); + QString PQgetvalue( int row, int col ); + bool PQgetisnull( int row, int col ); + + int PQnfields(); + QString PQfname( int col ); + Oid PQftable( int col ); + Oid PQftype( int col ); + int PQfmod( int col ); + int PQftablecol( int col ); + Oid PQoidValue(); + + PGresult *result() const { return mRes; } + + private: + PGresult *mRes = nullptr; + +}; + +struct PGException +{ + explicit PGException( QgsPostgresResult &r ) : mWhat( r.PQresultErrorMessage() ) + { + } + QString errorMessage() const + { + return mWhat; + } + private: + QString mWhat; +}; +#endif diff --git a/src/providers/postgres/raster/qgspostgresrasterprovider.cpp b/src/providers/postgres/raster/qgspostgresrasterprovider.cpp index 3180629cc20b..ed177d0a22f7 100644 --- a/src/providers/postgres/raster/qgspostgresrasterprovider.cpp +++ b/src/providers/postgres/raster/qgspostgresrasterprovider.cpp @@ -16,6 +16,7 @@ #include #include "qgspostgresrasterprovider.h" +#include "qgspostgresresult.h" #include "qgspostgresprovidermetadatautils.h" #include "qgsmessagelog.h" #include "qgsrectangle.h"