Skip to content

Commit b9452c2

Browse files
author
jef
committedAug 27, 2008
postgres provider update
- track open cursors and commit transaction whenever no open cursors are left - close cursor after immediately after feature retrieval (releases locks earlier) - wrap postgres calls in Conn class - introduce Result class to handle PQclear mostly transparently (fixes minor leaks) - remove executeDbCommand method (duplicate of PQexecNR) - introduce support for estimate_extents (fixes #582) - introduce support for enum types (fixes #1210) git-svn-id: http://svn.osgeo.org/qgis/trunk@9183 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 683dcea commit b9452c2

File tree

2 files changed

+433
-419
lines changed

2 files changed

+433
-419
lines changed
 

‎src/providers/postgres/qgspostgresprovider.cpp

Lines changed: 318 additions & 354 deletions
Large diffs are not rendered by default.

‎src/providers/postgres/qgspostgresprovider.h

Lines changed: 115 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -197,18 +197,6 @@ class QgsPostgresProvider : public QgsVectorDataProvider
197197

198198
QgsAttributeList allAttributesList();
199199

200-
//! get postgis version string
201-
QString postgisVersion( PGconn * );
202-
203-
//! get status of GEOS capability
204-
bool hasGEOS( PGconn * );
205-
206-
//! get status of GIST capability
207-
bool hasGIST( PGconn * );
208-
209-
//! get status of PROJ4 capability
210-
bool hasPROJ( PGconn * );
211-
212200
/**Returns the default value for field specified by @c fieldId */
213201
QVariant getDefaultValue( int fieldId );
214202

@@ -401,21 +389,6 @@ class QgsPostgresProvider : public QgsVectorDataProvider
401389
* Geometry type
402390
*/
403391
QGis::WKBTYPE geomType;
404-
/**
405-
* Connection pointer
406-
*/
407-
PGconn *connectionRO;
408-
PGconn *connectionRW;
409-
410-
bool connectRW()
411-
{
412-
if ( connectionRW )
413-
return connectionRW;
414-
415-
connectionRW = connectDb( mUri.connInfo(), false );
416-
417-
return connectionRW;
418-
}
419392

420393
/**
421394
* Spatial reference id of the layer
@@ -457,8 +430,6 @@ class QgsPostgresProvider : public QgsVectorDataProvider
457430
bool deduceEndian();
458431
bool getGeometryDetails();
459432

460-
PGresult* executeDbCommand( PGconn* connection, const QString& sql );
461-
462433
// Produces a QMessageBox with the given title and text. Doesn't
463434
// return until the user has dismissed the dialog box.
464435
static void showMessageBox( const QString& title, const QString& text );
@@ -556,27 +527,6 @@ class QgsPostgresProvider : public QgsVectorDataProvider
556527
int SRCFromViewColumn( const QString& ns, const QString& relname, const QString& attname_table,
557528
const QString& attname_view, const QString& viewDefinition, SRC& result ) const;
558529

559-
//! PostGIS version string
560-
QString postgisVersionInfo;
561-
562-
//! Are postgisVersionMajor, postgisVersionMinor, geosAvailable, gistAvailable, projAvailable valid?
563-
bool gotPostgisVersion;
564-
565-
//! PostGIS major version
566-
int postgisVersionMajor;
567-
568-
//! PostGIS minor version
569-
int postgisVersionMinor;
570-
571-
//! GEOS capability
572-
bool geosAvailable;
573-
574-
//! GIST capability
575-
bool gistAvailable;
576-
577-
//! PROJ4 capability
578-
bool projAvailable;
579-
580530
int enabledCapabilities;
581531

582532
/**Returns the maximum value of the primary key attribute
@@ -597,29 +547,129 @@ class QgsPostgresProvider : public QgsVectorDataProvider
597547
*/
598548
void customEvent( QEvent *e );
599549

600-
PGconn *connectDb( const QString &conninfo, bool readonly = true );
601-
void disconnectDb();
602-
603-
bool useWkbHex;
604-
605550
void appendGeomString( QgsGeometry *geom, QString &geomParam ) const;
606551
QByteArray paramValue( QString fieldvalue, const QString &defaultValue ) const;
607552

608-
// run a query and check for errors
609-
static PGresult *PQexec( PGconn *conn, const char *query );
553+
class Conn
554+
{
555+
public:
556+
Conn( PGconn *connection ) :
557+
ref( 1 ),
558+
openCursors( 0 ),
559+
conn( connection ),
560+
gotPostgisVersion( false )
561+
{
562+
}
563+
564+
//! get postgis version string
565+
QString postgisVersion();
566+
567+
//! get status of GEOS capability
568+
bool hasGEOS();
569+
570+
//! get status of GIST capability
571+
bool hasGIST();
572+
573+
//! get status of PROJ4 capability
574+
bool hasPROJ();
575+
576+
//! encode wkb in hex
577+
bool useWkbHex() { return mUseWkbHex; }
578+
579+
//! major PostgreSQL version
580+
int majorVersion() { return postgisVersionMajor; }
610581

611-
// run a query and free result buffer
612-
static bool PQexecNR( PGconn *conn, const char *query );
582+
// run a query and free result buffer
583+
bool PQexecNR( QString query );
613584

614-
struct Conn
585+
// cursor handling
586+
bool openCursor( QString cursorName, QString declare );
587+
bool closeCursor( QString cursorName );
588+
589+
PGconn *pgConnection() { return conn; }
590+
591+
//
592+
// libpq wrapper
593+
//
594+
595+
// run a query and check for errors
596+
PGresult *PQexec( QString query );
597+
void PQfinish();
598+
int PQsendQuery( QString query );
599+
PGresult *PQgetResult();
600+
PGresult *PQprepare( QString stmtName, QString query, int nParams, const Oid *paramTypes );
601+
PGresult *PQexecPrepared( QString stmtName, const QStringList &params );
602+
603+
static Conn *connectDb( const QString &conninfo, bool readonly );
604+
static void disconnectRW( Conn *&conn );
605+
static void disconnectRO( Conn *&conn );
606+
static void disconnect( QMap<QString, Conn *> &connections, Conn *&conn );
607+
608+
private:
609+
int ref;
610+
int openCursors;
611+
PGconn *conn;
612+
613+
//! GEOS capability
614+
bool geosAvailable;
615+
616+
//! PostGIS version string
617+
QString postgisVersionInfo;
618+
619+
//! Are postgisVersionMajor, postgisVersionMinor, geosAvailable, gistAvailable, projAvailable valid?
620+
bool gotPostgisVersion;
621+
622+
//! PostGIS major version
623+
int postgisVersionMajor;
624+
625+
//! PostGIS minor version
626+
int postgisVersionMinor;
627+
628+
//! GIST capability
629+
bool gistAvailable;
630+
631+
//! PROJ4 capability
632+
bool projAvailable;
633+
634+
//! encode wkb in hex
635+
bool mUseWkbHex;
636+
637+
static QMap<QString, Conn *> connectionsRW;
638+
static QMap<QString, Conn *> connectionsRO;
639+
};
640+
641+
class Result
615642
{
616-
Conn( PGconn *connection ) : ref( 1 ), conn( connection ) {}
643+
public:
644+
Result( PGresult *theRes = 0 ) : res( theRes ) {}
645+
~Result() { if ( res ) PQclear( res ); }
617646

618-
int ref;
619-
PGconn *conn;
647+
operator PGresult *() { return res; }
648+
649+
Result &operator=( PGresult *theRes ) { if ( res ) PQclear( res ); res = theRes; return *this; }
650+
651+
private:
652+
PGresult *res;
620653
};
621-
static QMap<QString, Conn *> connectionsRW;
622-
static QMap<QString, Conn *> connectionsRO;
654+
655+
/**
656+
* Connection pointers
657+
*/
658+
Conn *connectionRO;
659+
Conn *connectionRW;
660+
661+
bool connectRW()
662+
{
663+
if ( connectionRW )
664+
return connectionRW;
665+
666+
connectionRW = Conn::connectDb( mUri.connInfo(), false );
667+
668+
return connectionRW;
669+
}
670+
671+
void disconnectDb();
672+
623673
static int providerIds;
624674
};
625675

0 commit comments

Comments
 (0)
Please sign in to comment.