LCOV - code coverage report
Current view: top level - ogr/ogrsf_frmts/pg - ogr_pg.h (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 78 78 100.0 %
Date: 2025-02-20 10:14:44 Functions: 28 28 100.0 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  OpenGIS Simple Features Reference Implementation
       4             :  * Purpose:  Private definitions for OGR/PostgreSQL driver.
       5             :  * Author:   Frank Warmerdam, warmerdam@pobox.com
       6             :  *
       7             :  ******************************************************************************
       8             :  * Copyright (c) 2000, Frank Warmerdam
       9             :  * Copyright (c) 2008-2013, Even Rouault <even dot rouault at spatialys.com>
      10             :  *
      11             :  * SPDX-License-Identifier: MIT
      12             :  ****************************************************************************/
      13             : 
      14             : #ifndef OGR_PG_H_INCLUDED
      15             : #define OGR_PG_H_INCLUDED
      16             : 
      17             : #include "ogrsf_frmts.h"
      18             : #include "libpq-fe.h"
      19             : #include "cpl_string.h"
      20             : 
      21             : #include "ogrpgutility.h"
      22             : #include "ogr_pgdump.h"
      23             : 
      24             : #include <map>
      25             : #include <optional>
      26             : #include <vector>
      27             : 
      28             : /* These are the OIDs for some builtin types, as returned by PQftype(). */
      29             : /* They were copied from pg_type.h in src/include/catalog/pg_type.h */
      30             : 
      31             : #define BOOLOID 16
      32             : #define BYTEAOID 17
      33             : #define CHAROID 18
      34             : #define NAMEOID 19
      35             : #define INT8OID 20
      36             : #define INT2OID 21
      37             : #define INT2VECTOROID 22
      38             : #define INT4OID 23
      39             : #define REGPROCOID 24
      40             : #define TEXTOID 25
      41             : #define OIDOID 26
      42             : #define TIDOID 27
      43             : #define XIDOID 28
      44             : #define CIDOID 29
      45             : #define OIDVECTOROID 30
      46             : #define JSONOID 114
      47             : #define FLOAT4OID 700
      48             : #define FLOAT8OID 701
      49             : #define BOOLARRAYOID 1000
      50             : #define INT2ARRAYOID 1005
      51             : #define INT4ARRAYOID 1007
      52             : #define TEXTARRAYOID 1009
      53             : #define BPCHARARRAYOID 1014
      54             : #define VARCHARARRAYOID 1015
      55             : #define INT8ARRAYOID 1016
      56             : #define FLOAT4ARRAYOID 1021
      57             : #define FLOAT8ARRAYOID 1022
      58             : #define BPCHAROID 1042
      59             : #define VARCHAROID 1043
      60             : #define DATEOID 1082
      61             : #define TIMEOID 1083
      62             : #define TIMESTAMPOID 1114
      63             : #define TIMESTAMPTZOID 1184
      64             : #define NUMERICOID 1700
      65             : #define NUMERICARRAYOID 1231
      66             : #define UUIDOID 2950
      67             : #define JSONBOID 3802
      68             : 
      69             : CPLString OGRPGEscapeString(void *hPGConn, const char *pszStrValue,
      70             :                             int nMaxLength = -1, const char *pszTableName = "",
      71             :                             const char *pszFieldName = "");
      72             : CPLString OGRPGEscapeColumnName(const char *pszColumnName);
      73             : 
      74             : #define UNDETERMINED_SRID                                                      \
      75             :     -2 /* Special value when we haven't yet looked for SRID */
      76             : 
      77             : class OGRPGDataSource;
      78             : class OGRPGLayer;
      79             : 
      80             : typedef enum
      81             : {
      82             :     GEOM_TYPE_UNKNOWN = 0,
      83             :     GEOM_TYPE_GEOMETRY = 1,
      84             :     GEOM_TYPE_GEOGRAPHY = 2,
      85             :     GEOM_TYPE_WKB = 3
      86             : } PostgisType;
      87             : 
      88             : typedef struct
      89             : {
      90             :     char *pszName;
      91             :     char *pszGeomType;
      92             :     int GeometryTypeFlags;
      93             :     int nSRID;
      94             :     PostgisType ePostgisType;
      95             :     int bNullable;
      96             : } PGGeomColumnDesc;
      97             : 
      98             : /************************************************************************/
      99             : /*                         OGRPGGeomFieldDefn                           */
     100             : /************************************************************************/
     101             : 
     102             : class OGRPGGeomFieldDefn final : public OGRGeomFieldDefn
     103             : {
     104             :     OGRPGGeomFieldDefn(const OGRPGGeomFieldDefn &) = delete;
     105             :     OGRPGGeomFieldDefn &operator=(const OGRPGGeomFieldDefn &) = delete;
     106             : 
     107             :   protected:
     108             :     OGRPGLayer *poLayer;
     109             : 
     110             :   public:
     111         562 :     OGRPGGeomFieldDefn(OGRPGLayer *poLayerIn, const char *pszFieldName)
     112         562 :         : OGRGeomFieldDefn(pszFieldName, wkbUnknown), poLayer(poLayerIn),
     113             :           nSRSId(UNDETERMINED_SRID), GeometryTypeFlags(0),
     114         562 :           ePostgisType(GEOM_TYPE_UNKNOWN)
     115             :     {
     116         562 :     }
     117             : 
     118             :     virtual const OGRSpatialReference *GetSpatialRef() const override;
     119             : 
     120         562 :     void UnsetLayer()
     121             :     {
     122         562 :         poLayer = nullptr;
     123         562 :     }
     124             : 
     125             :     mutable int nSRSId;
     126             :     mutable int GeometryTypeFlags;
     127             :     mutable PostgisType ePostgisType;
     128             : };
     129             : 
     130             : /************************************************************************/
     131             : /*                          OGRPGFeatureDefn                            */
     132             : /************************************************************************/
     133             : 
     134             : class OGRPGFeatureDefn CPL_NON_FINAL : public OGRFeatureDefn
     135             : {
     136             :   public:
     137         947 :     explicit OGRPGFeatureDefn(const char *pszName = nullptr)
     138         947 :         : OGRFeatureDefn(pszName)
     139             :     {
     140         947 :         SetGeomType(wkbNone);
     141         947 :     }
     142             : 
     143         947 :     virtual void UnsetLayer()
     144             :     {
     145         947 :         const int nGeomFieldCount = GetGeomFieldCount();
     146        1509 :         for (int i = 0; i < nGeomFieldCount; i++)
     147         562 :             cpl::down_cast<OGRPGGeomFieldDefn *>(apoGeomFieldDefn[i].get())
     148         562 :                 ->UnsetLayer();
     149         947 :     }
     150             : 
     151       22548 :     OGRPGGeomFieldDefn *GetGeomFieldDefn(int i) override
     152             :     {
     153       22548 :         return cpl::down_cast<OGRPGGeomFieldDefn *>(
     154       22548 :             OGRFeatureDefn::GetGeomFieldDefn(i));
     155             :     }
     156             : 
     157        1531 :     const OGRPGGeomFieldDefn *GetGeomFieldDefn(int i) const override
     158             :     {
     159        1531 :         return cpl::down_cast<const OGRPGGeomFieldDefn *>(
     160        1531 :             OGRFeatureDefn::GetGeomFieldDefn(i));
     161             :     }
     162             : };
     163             : 
     164             : /************************************************************************/
     165             : /*                            OGRPGLayer                                */
     166             : /************************************************************************/
     167             : 
     168             : class OGRPGLayer CPL_NON_FINAL : public OGRLayer
     169             : {
     170             :     OGRPGLayer(const OGRPGLayer &) = delete;
     171             :     OGRPGLayer &operator=(const OGRPGLayer &) = delete;
     172             : 
     173             :   protected:
     174             :     OGRPGFeatureDefn *poFeatureDefn = nullptr;
     175             : 
     176             :     int nCursorPage = 0;
     177             :     GIntBig iNextShapeId = 0;
     178             : 
     179             :     static char *GeometryToBYTEA(const OGRGeometry *, int nPostGISMajor,
     180             :                                  int nPostGISMinor);
     181             :     static GByte *BYTEAToGByteArray(const char *pszBytea, int *pnLength);
     182             :     static OGRGeometry *BYTEAToGeometry(const char *);
     183             :     Oid GeometryToOID(OGRGeometry *);
     184             :     OGRGeometry *OIDToGeometry(Oid);
     185             : 
     186             :     OGRPGDataSource *poDS = nullptr;
     187             : 
     188             :     char *pszQueryStatement = nullptr;
     189             : 
     190             :     char *pszCursorName = nullptr;
     191             :     PGresult *hCursorResult = nullptr;
     192             :     int bInvalidated = false;
     193             : 
     194             :     int nResultOffset = 0;
     195             : 
     196             :     int bWkbAsOid = false;
     197             : 
     198             :     char *pszFIDColumn = nullptr;
     199             : 
     200             :     int bCanUseBinaryCursor = true;
     201             :     int *m_panMapFieldNameToIndex = nullptr;
     202             :     int *m_panMapFieldNameToGeomIndex = nullptr;
     203             : 
     204             :     int ParsePGDate(const char *, OGRField *);
     205             : 
     206             :     void SetInitialQueryCursor();
     207             :     void CloseCursor();
     208             : 
     209             :     virtual CPLString GetFromClauseForGetExtent() = 0;
     210             :     OGRErr RunGetExtentRequest(OGREnvelope &sExtent, int bForce,
     211             :                                const std::string &osCommand, int bErrorAsDebug);
     212             :     OGRErr RunGetExtent3DRequest(OGREnvelope3D &sExtent3D,
     213             :                                  const std::string &osCommand,
     214             :                                  int bErrorAsDebug);
     215             :     static void CreateMapFromFieldNameToIndex(PGresult *hResult,
     216             :                                               OGRFeatureDefn *poFeatureDefn,
     217             :                                               int *&panMapFieldNameToIndex,
     218             :                                               int *&panMapFieldNameToGeomIndex);
     219             : 
     220             :     int ReadResultDefinition(PGresult *hInitialResultIn);
     221             : 
     222             :     OGRFeature *RecordToFeature(PGresult *hResult,
     223             :                                 const int *panMapFieldNameToIndex,
     224             :                                 const int *panMapFieldNameToGeomIndex,
     225             :                                 int iRecord);
     226             :     OGRFeature *GetNextRawFeature();
     227             : 
     228             :   public:
     229             :     OGRPGLayer();
     230             :     virtual ~OGRPGLayer();
     231             : 
     232             :     virtual void ResetReading() override;
     233             : 
     234       11973 :     virtual OGRPGFeatureDefn *GetLayerDefn() override
     235             :     {
     236       11973 :         return poFeatureDefn;
     237             :     }
     238             : 
     239             :     OGRErr IGetExtent(int iGeomField, OGREnvelope *psExtent,
     240             :                       bool bForce) override;
     241             : 
     242             :     OGRErr IGetExtent3D(int iGeomField, OGREnvelope3D *psExtent3D,
     243             :                         bool bForce) override;
     244             : 
     245             :     virtual OGRErr StartTransaction() override;
     246             :     virtual OGRErr CommitTransaction() override;
     247             :     virtual OGRErr RollbackTransaction() override;
     248             : 
     249             :     void InvalidateCursor();
     250             : 
     251             :     virtual const char *GetFIDColumn() override;
     252             : 
     253             :     virtual OGRErr SetNextByIndex(GIntBig nIndex) override;
     254             : 
     255          33 :     OGRPGDataSource *GetDS()
     256             :     {
     257          33 :         return poDS;
     258             :     }
     259             : 
     260             :     GDALDataset *GetDataset() override;
     261             : 
     262             :     virtual void ResolveSRID(const OGRPGGeomFieldDefn *poGFldDefn) = 0;
     263             : };
     264             : 
     265             : /************************************************************************/
     266             : /*                           OGRPGTableLayer                            */
     267             : /************************************************************************/
     268             : 
     269             : class OGRPGTableLayer final : public OGRPGLayer
     270             : {
     271             :     OGRPGTableLayer(const OGRPGTableLayer &) = delete;
     272             :     OGRPGTableLayer &operator=(const OGRPGTableLayer &) = delete;
     273             : 
     274             :     static constexpr int USE_COPY_UNSET = -10;
     275             : 
     276             :     int bUpdateAccess = false;
     277             : 
     278             :     void BuildWhere();
     279             :     CPLString BuildFields();
     280             :     void BuildFullQueryStatement();
     281             : 
     282             :     char *pszTableName = nullptr;
     283             :     char *pszSchemaName = nullptr;
     284             :     char *m_pszTableDescription = nullptr;
     285             :     CPLString osForcedDescription{};
     286             :     bool m_bMetadataLoaded = false;
     287             :     bool m_bMetadataModified = false;
     288             :     char *pszSqlTableName = nullptr;
     289             :     int bTableDefinitionValid = -1;
     290             : 
     291             :     CPLString osPrimaryKey{};
     292             : 
     293             :     int bGeometryInformationSet = false;
     294             : 
     295             :     /* Name of the parent table with the geometry definition if it is a derived
     296             :      * table or NULL */
     297             :     char *pszSqlGeomParentTableName = nullptr;
     298             : 
     299             :     char *pszGeomColForced = nullptr;
     300             : 
     301             :     CPLString osQuery{};
     302             :     CPLString osWHERE{};
     303             : 
     304             :     int bLaunderColumnNames = true;
     305             :     bool m_bUTF8ToASCII = false;
     306             :     int bPreservePrecision = true;
     307             :     int bUseCopy = USE_COPY_UNSET;
     308             :     int bCopyActive = false;
     309             :     bool bFIDColumnInCopyFields = false;
     310             :     int bFirstInsertion = true;
     311             : 
     312             :     OGRErr CreateFeatureViaCopy(OGRFeature *poFeature);
     313             :     OGRErr CreateFeatureViaInsert(OGRFeature *poFeature);
     314             :     CPLString BuildCopyFields();
     315             : 
     316             :     int bHasWarnedIncompatibleGeom = false;
     317             :     void CheckGeomTypeCompatibility(int iGeomField, OGRGeometry *poGeom);
     318             : 
     319             :     int bRetrieveFID = true;
     320             :     int bSkipConflicts = false;
     321             :     int bHasWarnedAlreadySetFID = false;
     322             : 
     323             :     char **papszOverrideColumnTypes = nullptr;
     324             :     int nForcedSRSId = UNDETERMINED_SRID;
     325             :     int nForcedGeometryTypeFlags = -1;
     326             :     bool bCreateSpatialIndexFlag = true;
     327             :     CPLString osSpatialIndexType = "GIST";
     328             :     int bInResetReading = false;
     329             : 
     330             :     int bAutoFIDOnCreateViaCopy = false;
     331             :     int bUseCopyByDefault = false;
     332             :     bool bNeedToUpdateSequence = false;
     333             : 
     334             :     int bDeferredCreation = false;
     335             :     CPLString osCreateTable{};
     336             :     std::vector<std::string> m_aosDeferredCommentOnColumns{};
     337             : 
     338             :     int iFIDAsRegularColumnIndex = -1;
     339             : 
     340             :     CPLString m_osFirstGeometryFieldName{};
     341             : 
     342             :     std::string m_osLCOGeomType{};
     343             : 
     344          17 :     virtual CPLString GetFromClauseForGetExtent() override
     345             :     {
     346          17 :         return pszSqlTableName;
     347             :     }
     348             : 
     349             :     OGRErr RunAddGeometryColumn(const OGRPGGeomFieldDefn *poGeomField);
     350             :     OGRErr RunCreateSpatialIndex(const OGRPGGeomFieldDefn *poGeomField,
     351             :                                  int nIdx);
     352             : 
     353             :     void UpdateSequenceIfNeeded();
     354             : 
     355             :     void LoadMetadata();
     356             :     void SerializeMetadata();
     357             : 
     358             :   public:
     359             :     OGRPGTableLayer(OGRPGDataSource *, CPLString &osCurrentSchema,
     360             :                     const char *pszTableName, const char *pszSchemaName,
     361             :                     const char *pszDescriptionIn, const char *pszGeomColForced,
     362             :                     int bUpdate);
     363             :     virtual ~OGRPGTableLayer();
     364             : 
     365             :     void SetGeometryInformation(PGGeomColumnDesc *pasDesc, int nGeomFieldCount);
     366             : 
     367             :     virtual OGRFeature *GetFeature(GIntBig nFeatureId) override;
     368             :     virtual void ResetReading() override;
     369             :     virtual OGRFeature *GetNextFeature() override;
     370             :     virtual GIntBig GetFeatureCount(int) override;
     371             : 
     372             :     OGRErr ISetSpatialFilter(int iGeomField,
     373             :                              const OGRGeometry *poGeom) override;
     374             : 
     375             :     virtual OGRErr SetAttributeFilter(const char *) override;
     376             : 
     377             :     virtual OGRErr ISetFeature(OGRFeature *poFeature) override;
     378             :     OGRErr IUpdateFeature(OGRFeature *poFeature, int nUpdatedFieldsCount,
     379             :                           const int *panUpdatedFieldsIdx,
     380             :                           int nUpdatedGeomFieldsCount,
     381             :                           const int *panUpdatedGeomFieldsIdx,
     382             :                           bool bUpdateStyleString) override;
     383             :     virtual OGRErr DeleteFeature(GIntBig nFID) override;
     384             :     virtual OGRErr ICreateFeature(OGRFeature *poFeature) override;
     385             : 
     386             :     virtual OGRErr CreateField(const OGRFieldDefn *poField,
     387             :                                int bApproxOK = TRUE) override;
     388             :     virtual OGRErr CreateGeomField(const OGRGeomFieldDefn *poGeomField,
     389             :                                    int bApproxOK = TRUE) override;
     390             :     virtual OGRErr DeleteField(int iField) override;
     391             :     virtual OGRErr AlterFieldDefn(int iField, OGRFieldDefn *poNewFieldDefn,
     392             :                                   int nFlags) override;
     393             :     virtual OGRErr
     394             :     AlterGeomFieldDefn(int iGeomFieldToAlter,
     395             :                        const OGRGeomFieldDefn *poNewGeomFieldDefn,
     396             :                        int nFlagsIn) override;
     397             : 
     398             :     virtual int TestCapability(const char *) override;
     399             : 
     400             :     OGRErr IGetExtent(int iGeomField, OGREnvelope *psExtent,
     401             :                       bool bForce) override;
     402             : 
     403          24 :     const char *GetTableName()
     404             :     {
     405          24 :         return pszTableName;
     406             :     }
     407             : 
     408          24 :     const char *GetSchemaName()
     409             :     {
     410          24 :         return pszSchemaName;
     411             :     }
     412             : 
     413             :     virtual const char *GetFIDColumn() override;
     414             : 
     415             :     virtual char **GetMetadataDomainList() override;
     416             :     virtual char **GetMetadata(const char *pszDomain = "") override;
     417             :     virtual const char *GetMetadataItem(const char *pszName,
     418             :                                         const char *pszDomain = "") override;
     419             :     virtual CPLErr SetMetadata(char **papszMD,
     420             :                                const char *pszDomain = "") override;
     421             :     virtual CPLErr SetMetadataItem(const char *pszName, const char *pszValue,
     422             :                                    const char *pszDomain = "") override;
     423             : 
     424             :     virtual OGRErr Rename(const char *pszNewName) override;
     425             : 
     426             :     OGRGeometryTypeCounter *GetGeometryTypes(int iGeomField, int nFlagsGGT,
     427             :                                              int &nEntryCountOut,
     428             :                                              GDALProgressFunc pfnProgress,
     429             :                                              void *pProgressData) override;
     430             : 
     431             :     int FindFieldIndex(const char *pszFieldName, int bExactMatch) override;
     432             : 
     433             :     // follow methods are not base class overrides
     434         223 :     void SetLaunderFlag(int bFlag)
     435             :     {
     436         223 :         bLaunderColumnNames = bFlag;
     437         223 :     }
     438             : 
     439         223 :     void SetUTF8ToASCIIFlag(bool bFlag)
     440             :     {
     441         223 :         m_bUTF8ToASCII = bFlag;
     442         223 :     }
     443             : 
     444         223 :     void SetPrecisionFlag(int bFlag)
     445             :     {
     446         223 :         bPreservePrecision = bFlag;
     447         223 :     }
     448             : 
     449             :     void SetOverrideColumnTypes(const char *pszOverrideColumnTypes);
     450             : 
     451             :     OGRErr StartCopy();
     452             :     OGRErr EndCopy();
     453             : 
     454             :     int ReadTableDefinition();
     455             : 
     456       72285 :     int HasGeometryInformation()
     457             :     {
     458       72285 :         return bGeometryInformationSet;
     459             :     }
     460             : 
     461             :     void SetTableDefinition(const char *pszFIDColumnName,
     462             :                             const char *pszGFldName, OGRwkbGeometryType eType,
     463             :                             const char *pszGeomType, int nSRSId,
     464             :                             int GeometryTypeFlags);
     465             : 
     466             :     void SetForcedSRSId(int nForcedSRSIdIn)
     467             :     {
     468             :         nForcedSRSId = nForcedSRSIdIn;
     469             :     }
     470             : 
     471         223 :     void SetForcedGeometryTypeFlags(int GeometryTypeFlagsIn)
     472             :     {
     473         223 :         nForcedGeometryTypeFlags = GeometryTypeFlagsIn;
     474         223 :     }
     475             : 
     476         223 :     void SetCreateSpatialIndex(bool bFlag, const char *pszSpatialIndexType)
     477             :     {
     478         223 :         bCreateSpatialIndexFlag = bFlag;
     479         223 :         osSpatialIndexType = pszSpatialIndexType;
     480         223 :     }
     481             : 
     482             :     void SetForcedDescription(const char *pszDescriptionIn);
     483             : 
     484         223 :     void AllowAutoFIDOnCreateViaCopy()
     485             :     {
     486         223 :         bAutoFIDOnCreateViaCopy = TRUE;
     487         223 :     }
     488             : 
     489         217 :     void SetUseCopy()
     490             :     {
     491         217 :         bUseCopy = TRUE;
     492         217 :         bUseCopyByDefault = TRUE;
     493         217 :     }
     494             : 
     495             :     void SetDeferredCreation(int bDeferredCreationIn,
     496             :                              const std::string &osCreateTable);
     497             :     OGRErr RunDeferredCreationIfNecessary();
     498             : 
     499             :     virtual void ResolveSRID(const OGRPGGeomFieldDefn *poGFldDefn) override;
     500             : };
     501             : 
     502             : /************************************************************************/
     503             : /*                           OGRPGResultLayer                           */
     504             : /************************************************************************/
     505             : 
     506             : class OGRPGResultLayer final : public OGRPGLayer
     507             : {
     508             :     OGRPGResultLayer(const OGRPGResultLayer &) = delete;
     509             :     OGRPGResultLayer &operator=(const OGRPGResultLayer &) = delete;
     510             : 
     511             :     void BuildFullQueryStatement();
     512             : 
     513             :     char *pszRawStatement = nullptr;
     514             : 
     515             :     char *pszGeomTableName = nullptr;
     516             :     char *pszGeomTableSchemaName = nullptr;
     517             : 
     518             :     CPLString osWHERE{};
     519             : 
     520           6 :     virtual CPLString GetFromClauseForGetExtent() override
     521             :     {
     522           6 :         CPLString osStr("(");
     523           6 :         osStr += pszRawStatement;
     524           6 :         osStr += ")";
     525           6 :         return osStr;
     526             :     }
     527             : 
     528             :   public:
     529             :     OGRPGResultLayer(OGRPGDataSource *, const char *pszRawStatement,
     530             :                      PGresult *hInitialResult);
     531             :     virtual ~OGRPGResultLayer();
     532             : 
     533             :     virtual void ResetReading() override;
     534             :     virtual GIntBig GetFeatureCount(int) override;
     535             : 
     536             :     OGRErr ISetSpatialFilter(int iGeomField,
     537             :                              const OGRGeometry *poGeom) override;
     538             : 
     539             :     virtual int TestCapability(const char *) override;
     540             : 
     541             :     virtual OGRFeature *GetNextFeature() override;
     542             : 
     543             :     virtual void ResolveSRID(const OGRPGGeomFieldDefn *poGFldDefn) override;
     544             : };
     545             : 
     546             : /************************************************************************/
     547             : /*                           OGRPGDataSource                            */
     548             : /************************************************************************/
     549             : 
     550         428 : class OGRPGDataSource final : public GDALDataset
     551             : {
     552             :     OGRPGDataSource(const OGRPGDataSource &) = delete;
     553             :     OGRPGDataSource &operator=(const OGRPGDataSource &) = delete;
     554             : 
     555             :     typedef struct
     556             :     {
     557             :         int nMajor;
     558             :         int nMinor;
     559             :         int nRelease;
     560             :     } PGver;
     561             : 
     562             :     OGRPGTableLayer **papoLayers = nullptr;
     563             :     int nLayers = 0;
     564             : 
     565             :     bool m_bUTF8ClientEncoding = false;
     566             : 
     567             :     int bDSUpdate = false;
     568             :     int bHavePostGIS = false;
     569             :     int bHaveGeography = false;
     570             : 
     571             :     bool bUserTransactionActive = false;
     572             :     int bSavePointActive = false;
     573             :     int nSoftTransactionLevel = 0;
     574             : 
     575             :     PGconn *hPGConn = nullptr;
     576             : 
     577             :     OGRErr DeleteLayer(int iLayer) override;
     578             : 
     579             :     Oid nGeometryOID = static_cast<Oid>(0);
     580             :     Oid nGeographyOID = static_cast<Oid>(0);
     581             : 
     582             :     // We maintain a list of known SRID to reduce the number of trips to
     583             :     // the database to get SRSes.
     584             :     std::map<int,
     585             :              std::unique_ptr<OGRSpatialReference, OGRSpatialReferenceReleaser>>
     586             :         m_oSRSCache{};
     587             : 
     588             :     OGRPGTableLayer *poLayerInCopyMode = nullptr;
     589             : 
     590             :     static void OGRPGDecodeVersionString(PGver *psVersion, const char *pszVer);
     591             : 
     592             :     CPLString osCurrentSchema{};
     593             :     CPLString GetCurrentSchema();
     594             : 
     595             :     // Actual value will be auto-detected if PostGIS >= 2.0 detected.
     596             :     int nUndefinedSRID = -1;
     597             : 
     598             :     char *pszForcedTables = nullptr;
     599             :     char **papszSchemaList = nullptr;
     600             :     int bHasLoadTables = false;
     601             :     CPLString osActiveSchema{};
     602             :     int bListAllTables = false;
     603             :     bool m_bSkipViews = false;
     604             : 
     605             :     bool m_bOgrSystemTablesMetadataTableExistenceTested = false;
     606             :     bool m_bOgrSystemTablesMetadataTableFound = false;
     607             : 
     608             :     bool m_bCreateMetadataTableIfNeededRun = false;
     609             :     bool m_bCreateMetadataTableIfNeededSuccess = false;
     610             : 
     611             :     bool m_bHasWritePermissionsOnMetadataTableRun = false;
     612             :     bool m_bHasWritePermissionsOnMetadataTableSuccess = false;
     613             : 
     614             :     void LoadTables();
     615             : 
     616             :     CPLString osDebugLastTransactionCommand{};
     617             :     OGRErr DoTransactionCommand(const char *pszCommand);
     618             : 
     619             :     OGRErr FlushSoftTransaction();
     620             : 
     621             :     OGRErr FlushCacheWithRet(bool bAtClosing);
     622             : 
     623             :     std::optional<std::string> FindSchema(const char *pszSchemaNameIn);
     624             : 
     625             :     bool IsSuperUser();
     626             :     bool OGRSystemTablesEventTriggerExists();
     627             : 
     628             :   public:
     629             :     PGver sPostgreSQLVersion = {0, 0, 0};
     630             :     PGver sPostGISVersion = {0, 0, 0};
     631             : 
     632             :     int bUseBinaryCursor = false;
     633             :     int bBinaryTimeFormatIsInt8 = false;
     634             : 
     635             :     bool m_bHasGeometryColumns = false;
     636             :     bool m_bHasSpatialRefSys = false;
     637             : 
     638         745 :     bool HavePostGIS() const
     639             :     {
     640         745 :         return bHavePostGIS;
     641             :     }
     642             : 
     643         130 :     int GetUndefinedSRID() const
     644             :     {
     645         130 :         return nUndefinedSRID;
     646             :     }
     647             : 
     648        3739 :     bool IsUTF8ClientEncoding() const
     649             :     {
     650        3739 :         return m_bUTF8ClientEncoding;
     651             :     }
     652             : 
     653             :   public:
     654             :     OGRPGDataSource();
     655             :     virtual ~OGRPGDataSource();
     656             : 
     657      247992 :     PGconn *GetPGConn()
     658             :     {
     659      247992 :         return hPGConn;
     660             :     }
     661             : 
     662             :     int FetchSRSId(const OGRSpatialReference *poSRS);
     663             :     const OGRSpatialReference *FetchSRS(int nSRSId);
     664             :     static OGRErr InitializeMetadataTables();
     665             : 
     666             :     int Open(const char *, int bUpdate, int bTestOpen, char **papszOpenOptions);
     667             :     OGRPGTableLayer *
     668             :     OpenTable(CPLString &osCurrentSchema, const char *pszTableName,
     669             :               const char *pszSchemaName, const char *pszDescription,
     670             :               const char *pszGeomColForced, int bUpdate, int bTestOpen);
     671             : 
     672             :     int GetLayerCount() override;
     673             :     OGRLayer *GetLayer(int) override;
     674             :     OGRLayer *GetLayerByName(const char *pszName) override;
     675             : 
     676             :     virtual CPLErr FlushCache(bool bAtClosing) override;
     677             : 
     678             :     OGRLayer *ICreateLayer(const char *pszName,
     679             :                            const OGRGeomFieldDefn *poGeomFieldDefn,
     680             :                            CSLConstList papszOptions) override;
     681             : 
     682             :     int TestCapability(const char *) override;
     683             : 
     684             :     virtual OGRErr StartTransaction(int bForce = FALSE) override;
     685             :     virtual OGRErr CommitTransaction() override;
     686             :     virtual OGRErr RollbackTransaction() override;
     687             : 
     688             :     OGRErr SoftStartTransaction();
     689             :     OGRErr SoftCommitTransaction();
     690             :     OGRErr SoftRollbackTransaction();
     691             : 
     692         421 :     Oid GetGeometryOID()
     693             :     {
     694         421 :         return nGeometryOID;
     695             :     }
     696             : 
     697         471 :     Oid GetGeographyOID()
     698             :     {
     699         471 :         return nGeographyOID;
     700             :     }
     701             : 
     702             :     virtual OGRLayer *ExecuteSQL(const char *pszSQLCommand,
     703             :                                  OGRGeometry *poSpatialFilter,
     704             :                                  const char *pszDialect) override;
     705             :     virtual OGRErr AbortSQL() override;
     706             :     virtual void ReleaseResultSet(OGRLayer *poLayer) override;
     707             : 
     708             :     virtual const char *GetMetadataItem(const char *pszKey,
     709             :                                         const char *pszDomain) override;
     710             : 
     711             :     int UseCopy();
     712             :     void StartCopy(OGRPGTableLayer *poPGLayer);
     713             :     OGRErr EndCopy();
     714             : 
     715          16 :     bool IsUserTransactionActive()
     716             :     {
     717          16 :         return bUserTransactionActive;
     718             :     }
     719             : 
     720             :     bool CreateMetadataTableIfNeeded();
     721             :     bool HasOgrSystemTablesMetadataTable();
     722             :     bool HasWritePermissionsOnMetadataTable();
     723             : };
     724             : 
     725             : #endif /* ndef OGR_PG_H_INCLUDED */

Generated by: LCOV version 1.14