-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor PGException and QgsPostgresResult
- Loading branch information
Alexey Karandashev
committed
Jul 28, 2023
1 parent
78ef09d
commit f5fe176
Showing
6 changed files
with
197 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters