@@ -197,18 +197,6 @@ class QgsPostgresProvider : public QgsVectorDataProvider
197
197
198
198
QgsAttributeList allAttributesList ();
199
199
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
-
212
200
/* *Returns the default value for field specified by @c fieldId */
213
201
QVariant getDefaultValue ( int fieldId );
214
202
@@ -401,21 +389,6 @@ class QgsPostgresProvider : public QgsVectorDataProvider
401
389
* Geometry type
402
390
*/
403
391
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
- }
419
392
420
393
/* *
421
394
* Spatial reference id of the layer
@@ -457,8 +430,6 @@ class QgsPostgresProvider : public QgsVectorDataProvider
457
430
bool deduceEndian ();
458
431
bool getGeometryDetails ();
459
432
460
- PGresult* executeDbCommand ( PGconn* connection, const QString& sql );
461
-
462
433
// Produces a QMessageBox with the given title and text. Doesn't
463
434
// return until the user has dismissed the dialog box.
464
435
static void showMessageBox ( const QString& title, const QString& text );
@@ -556,27 +527,6 @@ class QgsPostgresProvider : public QgsVectorDataProvider
556
527
int SRCFromViewColumn ( const QString& ns, const QString& relname, const QString& attname_table,
557
528
const QString& attname_view, const QString& viewDefinition, SRC& result ) const ;
558
529
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
-
580
530
int enabledCapabilities;
581
531
582
532
/* *Returns the maximum value of the primary key attribute
@@ -597,29 +547,129 @@ class QgsPostgresProvider : public QgsVectorDataProvider
597
547
*/
598
548
void customEvent ( QEvent *e );
599
549
600
- PGconn *connectDb ( const QString &conninfo, bool readonly = true );
601
- void disconnectDb ();
602
-
603
- bool useWkbHex;
604
-
605
550
void appendGeomString ( QgsGeometry *geom, QString &geomParam ) const ;
606
551
QByteArray paramValue ( QString fieldvalue, const QString &defaultValue ) const ;
607
552
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; }
610
581
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 );
613
584
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 ¶ms );
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
615
642
{
616
- Conn ( PGconn *connection ) : ref( 1 ), conn( connection ) {}
643
+ public:
644
+ Result ( PGresult *theRes = 0 ) : res( theRes ) {}
645
+ ~Result () { if ( res ) PQclear ( res ); }
617
646
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;
620
653
};
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
+
623
673
static int providerIds;
624
674
};
625
675
0 commit comments