Skip to content

Commit

Permalink
Refactor PGException and QgsPostgresResult
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexey Karandashev committed Jul 28, 2023
1 parent 78ef09d commit f5fe176
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 143 deletions.
2 changes: 2 additions & 0 deletions src/providers/postgres/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ set(PG_SRCS
qgspostgresdataitems.cpp
qgspostgresfeatureiterator.cpp
qgspostgresprojectstorage.cpp
qgspostgresresult.cpp
qgspostgrestransaction.cpp
qgspgtablemodel.cpp
qgscolumntypethread.cpp
Expand Down Expand Up @@ -95,6 +96,7 @@ set(PGRASTER_SRCS
raster/qgspostgresrasterutils.cpp
qgspostgresconn.cpp
qgspostgresprovidermetadatautils.cpp
qgspostgresresult.cpp
)

# static library
Expand Down
96 changes: 1 addition & 95 deletions src/providers/postgres/qgspostgresconn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
***************************************************************************/

#include "qgspostgresconn.h"
#include "qgspostgresresult.h"
#include "qgslogger.h"
#include "qgsdatasourceuri.h"
#include "qgsmessagelog.h"
Expand Down Expand Up @@ -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 ) )
{
Expand Down
49 changes: 1 addition & 48 deletions src/providers/postgres/qgspostgresconn.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "qgswkbtypes.h"
#include "qgsconfig.h"
#include "qgsvectordataprovider.h"
#include "qgspostgresresult.h"

extern "C"
{
Expand Down Expand Up @@ -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<QgsPoolPostgresConn>.
class QgsPoolPostgresConn
Expand Down
118 changes: 118 additions & 0 deletions src/providers/postgres/qgspostgresresult.cpp
Original file line number Diff line number Diff line change
@@ -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 <QObject>

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 );
}
74 changes: 74 additions & 0 deletions src/providers/postgres/qgspostgresresult.h
Original file line number Diff line number Diff line change
@@ -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 <QString>

extern "C"
{
#include <libpq-fe.h>
}

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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <cstring>
#include "qgspostgresrasterprovider.h"
#include "qgspostgresresult.h"
#include "qgspostgresprovidermetadatautils.h"
#include "qgsmessagelog.h"
#include "qgsrectangle.h"
Expand Down

0 comments on commit f5fe176

Please sign in to comment.