LCOV - code coverage report
Current view: top level - ogr/ogrsf_frmts/pgdump - ogr_pgdump.h (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 49 49 100.0 %
Date: 2025-01-18 12:42:00 Functions: 17 17 100.0 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  OpenGIS Simple Features Reference Implementation
       4             :  * Purpose:  Private definitions for OGR/PostgreSQL dump driver.
       5             :  * Author:   Even Rouault, <even dot rouault at spatialys.com>
       6             :  *
       7             :  ******************************************************************************
       8             :  * Copyright (c) 2010-2013, Even Rouault <even dot rouault at spatialys.com>
       9             :  *
      10             :  * SPDX-License-Identifier: MIT
      11             :  ****************************************************************************/
      12             : 
      13             : #ifndef OGR_PGDUMP_H_INCLUDED
      14             : #define OGR_PGDUMP_H_INCLUDED
      15             : 
      16             : #include "ogrsf_frmts.h"
      17             : #include "cpl_string.h"
      18             : 
      19             : #include <vector>
      20             : 
      21             : // Cf https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
      22             : constexpr int OGR_PG_NAMEDATALEN = 64;
      23             : 
      24             : CPLString OGRPGDumpEscapeColumnName(const char *pszColumnName);
      25             : CPLString OGRPGDumpEscapeString(const char *pszStrValue, int nMaxLength = -1,
      26             :                                 const char *pszFieldName = "");
      27             : char CPL_DLL *OGRPGCommonGByteArrayToBYTEA(const GByte *pabyData, size_t nLen);
      28             : CPLString CPL_DLL OGRPGCommonLayerGetType(const OGRFieldDefn &oField,
      29             :                                           bool bPreservePrecision,
      30             :                                           bool bApproxOK);
      31             : bool CPL_DLL OGRPGCommonLayerSetType(OGRFieldDefn &oField, const char *pszType,
      32             :                                      const char *pszFormatType, int nWidth);
      33             : void CPL_DLL OGRPGCommonLayerNormalizeDefault(OGRFieldDefn *poFieldDefn,
      34             :                                               const char *pszDefault);
      35             : CPLString CPL_DLL OGRPGCommonLayerGetPGDefault(OGRFieldDefn *poFieldDefn);
      36             : 
      37             : void CPL_DLL OGRPGCommonAppendCopyFID(CPLString &osCommand,
      38             :                                       OGRFeature *poFeature);
      39             : 
      40             : typedef CPLString (*OGRPGCommonEscapeStringCbk)(void *userdata,
      41             :                                                 const char *pszValue,
      42             :                                                 int nWidth,
      43             :                                                 const char *pszLayerName,
      44             :                                                 const char *pszFieldRef);
      45             : void CPL_DLL OGRPGCommonAppendCopyRegularFields(
      46             :     CPLString &osCommand, OGRFeature *poFeature, const char *pszFIDColumn,
      47             :     const std::vector<bool> &abFieldsToInclude,
      48             :     OGRPGCommonEscapeStringCbk pfnEscapeString, void *userdata);
      49             : 
      50             : void CPL_DLL OGRPGCommonAppendFieldValue(
      51             :     CPLString &osCommand, OGRFeature *poFeature, int i,
      52             :     OGRPGCommonEscapeStringCbk pfnEscapeString, void *userdata);
      53             : 
      54             : char CPL_DLL *OGRPGCommonLaunderName(const char *pszSrcName,
      55             :                                      const char *pszDebugPrefix,
      56             :                                      bool bUTF8ToASCII);
      57             : 
      58             : std::string CPL_DLL
      59             : OGRPGCommonGenerateShortEnoughIdentifier(const char *pszIdentifier);
      60             : 
      61             : std::string CPL_DLL OGRPGCommonGenerateSpatialIndexName(
      62             :     const char *pszTableName, const char *pszGeomFieldName, int nGeomFieldIdx);
      63             : 
      64             : /************************************************************************/
      65             : /*                        OGRPGDumpGeomFieldDefn                        */
      66             : /************************************************************************/
      67             : 
      68             : class OGRPGDumpGeomFieldDefn final : public OGRGeomFieldDefn
      69             : {
      70             :     OGRPGDumpGeomFieldDefn(const OGRPGDumpGeomFieldDefn &) = delete;
      71             :     OGRPGDumpGeomFieldDefn &operator=(const OGRPGDumpGeomFieldDefn &) = delete;
      72             : 
      73             :   public:
      74         105 :     explicit OGRPGDumpGeomFieldDefn(OGRGeomFieldDefn *poGeomField)
      75         105 :         : OGRGeomFieldDefn(poGeomField), m_nSRSId(-1), m_nGeometryTypeFlags(0)
      76             :     {
      77         105 :     }
      78             : 
      79             :     int m_nSRSId;
      80             :     int m_nGeometryTypeFlags;
      81             : };
      82             : 
      83             : /************************************************************************/
      84             : /*                          OGRPGDumpLayer                              */
      85             : /************************************************************************/
      86             : 
      87             : class OGRPGDumpDataSource;
      88             : 
      89             : class OGRPGDumpLayer final : public OGRLayer
      90             : {
      91             :     OGRPGDumpLayer(const OGRPGDumpLayer &) = delete;
      92             :     OGRPGDumpLayer &operator=(const OGRPGDumpLayer &) = delete;
      93             : 
      94             :     static constexpr int USE_COPY_UNSET = -1;
      95             : 
      96             :     char *m_pszSchemaName = nullptr;
      97             :     char *m_pszSqlTableName = nullptr;
      98             :     CPLString m_osForcedDescription{};
      99             :     char *m_pszFIDColumn = nullptr;
     100             :     OGRFeatureDefn *m_poFeatureDefn = nullptr;
     101             :     OGRPGDumpDataSource *m_poDS = nullptr;
     102             :     bool m_bLaunderColumnNames = true;
     103             :     bool m_bUTF8ToASCII = false;
     104             :     bool m_bPreservePrecision = true;
     105             :     int m_bUseCopy = USE_COPY_UNSET;
     106             :     bool m_bWriteAsHex = false;
     107             :     bool m_bCopyActive = false;
     108             :     bool m_bFIDColumnInCopyFields = false;
     109             :     int m_bCreateTable = false;
     110             :     int m_nUnknownSRSId = -1;
     111             :     int m_nForcedSRSId = -1;
     112             :     int m_nForcedGeometryTypeFlags = -2;
     113             :     bool m_bCreateSpatialIndexFlag = false;
     114             :     CPLString m_osSpatialIndexType{};
     115             :     int m_nPostGISMajor = 0;
     116             :     int m_nPostGISMinor = 0;
     117             : 
     118             :     GIntBig m_iNextShapeId = 0;
     119             :     int m_iFIDAsRegularColumnIndex = -1;
     120             :     bool m_bAutoFIDOnCreateViaCopy = true;
     121             :     bool m_bCopyStatementWithFID = true;
     122             :     bool m_bNeedToUpdateSequence = false;
     123             :     bool m_bGeomColumnPositionImmediate = true;
     124             :     std::vector<std::string> m_aosDeferredGeomFieldCreationCommands{};
     125             :     std::vector<std::string> m_aosDeferrentNonGeomFieldCreationCommands{};
     126             :     std::vector<std::string> m_aosSpatialIndexCreationCommands{};
     127             : 
     128             :     CPLStringList m_apszOverrideColumnTypes{};
     129             : 
     130             :     CPLString m_osFirstGeometryFieldName{};
     131             : 
     132             :     OGRErr StartCopy(int bSetFID);
     133             :     CPLString BuildCopyFields(int bSetFID);
     134             : 
     135             :     void UpdateSequenceIfNeeded();
     136             : 
     137             :     void LogDeferredFieldCreationIfNeeded();
     138             : 
     139             :   public:
     140             :     OGRPGDumpLayer(OGRPGDumpDataSource *poDS, const char *pszSchemaName,
     141             :                    const char *pszLayerName, const char *pszFIDColumn,
     142             :                    int bWriteAsHexIn, int bCreateTable);
     143             :     virtual ~OGRPGDumpLayer();
     144             : 
     145         574 :     virtual OGRFeatureDefn *GetLayerDefn() override
     146             :     {
     147         574 :         return m_poFeatureDefn;
     148             :     }
     149             : 
     150           7 :     virtual const char *GetFIDColumn() override
     151             :     {
     152           7 :         return m_pszFIDColumn ? m_pszFIDColumn : "";
     153             :     }
     154             : 
     155          16 :     virtual void ResetReading() override
     156             :     {
     157          16 :     }
     158             : 
     159             :     virtual int TestCapability(const char *) override;
     160             : 
     161             :     virtual OGRErr ICreateFeature(OGRFeature *poFeature) override;
     162             :     virtual OGRErr CreateFeatureViaInsert(OGRFeature *poFeature);
     163             :     virtual OGRErr CreateFeatureViaCopy(OGRFeature *poFeature);
     164             : 
     165             :     virtual OGRErr CreateField(const OGRFieldDefn *poField,
     166             :                                int bApproxOK = TRUE) override;
     167             :     virtual OGRErr CreateGeomField(const OGRGeomFieldDefn *poGeomField,
     168             :                                    int bApproxOK = TRUE) override;
     169             : 
     170             :     virtual OGRFeature *GetNextFeature() override;
     171             : 
     172             :     virtual CPLErr SetMetadata(char **papszMD,
     173             :                                const char *pszDomain = "") override;
     174             :     virtual CPLErr SetMetadataItem(const char *pszName, const char *pszValue,
     175             :                                    const char *pszDomain = "") override;
     176             : 
     177             :     GDALDataset *GetDataset() override;
     178             : 
     179             :     // follow methods are not base class overrides
     180         115 :     void SetLaunderFlag(bool bFlag)
     181             :     {
     182         115 :         m_bLaunderColumnNames = bFlag;
     183         115 :     }
     184             : 
     185         115 :     void SetUTF8ToASCIIFlag(bool bFlag)
     186             :     {
     187         115 :         m_bUTF8ToASCII = bFlag;
     188         115 :     }
     189             : 
     190         115 :     void SetPrecisionFlag(bool bFlag)
     191             :     {
     192         115 :         m_bPreservePrecision = bFlag;
     193         115 :     }
     194             : 
     195             :     void SetOverrideColumnTypes(const char *pszOverrideColumnTypes);
     196             : 
     197         115 :     void SetUnknownSRSId(int nUnknownSRSIdIn)
     198             :     {
     199         115 :         m_nUnknownSRSId = nUnknownSRSIdIn;
     200         115 :     }
     201             : 
     202         115 :     void SetForcedSRSId(int nForcedSRSIdIn)
     203             :     {
     204         115 :         m_nForcedSRSId = nForcedSRSIdIn;
     205         115 :     }
     206             : 
     207         115 :     void SetForcedGeometryTypeFlags(int GeometryTypeFlagsIn)
     208             :     {
     209         115 :         m_nForcedGeometryTypeFlags = GeometryTypeFlagsIn;
     210         115 :     }
     211             : 
     212         115 :     void SetCreateSpatialIndex(bool bFlag, const char *pszSpatialIndexType)
     213             :     {
     214         115 :         m_bCreateSpatialIndexFlag = bFlag;
     215         115 :         m_osSpatialIndexType = pszSpatialIndexType;
     216         115 :     }
     217             : 
     218         115 :     void SetPostGISVersion(int nPostGISMajorIn, int nPostGISMinorIn)
     219             :     {
     220         115 :         m_nPostGISMajor = nPostGISMajorIn;
     221         115 :         m_nPostGISMinor = nPostGISMinorIn;
     222         115 :     }
     223             : 
     224           6 :     void SetGeometryFieldName(const char *pszGeomFieldName)
     225             :     {
     226           6 :         m_osFirstGeometryFieldName = pszGeomFieldName;
     227           6 :     }
     228             : 
     229             :     void SetForcedDescription(const char *pszDescriptionIn);
     230             : 
     231         115 :     void SetGeomColumnPositionImmediate(bool bGeomColumnPositionImmediate)
     232             :     {
     233         115 :         m_bGeomColumnPositionImmediate = bGeomColumnPositionImmediate;
     234         115 :     }
     235             : 
     236           6 :     void SetDeferredGeomFieldCreationCommands(
     237             :         const std::vector<std::string> &aosDeferredGeomFieldCreationCommands)
     238             :     {
     239             :         m_aosDeferredGeomFieldCreationCommands =
     240           6 :             aosDeferredGeomFieldCreationCommands;
     241           6 :     }
     242             : 
     243         115 :     void SetSpatialIndexCreationCommands(
     244             :         const std::vector<std::string> &aosSpatialIndexCreationCommands)
     245             :     {
     246         115 :         m_aosSpatialIndexCreationCommands = aosSpatialIndexCreationCommands;
     247         115 :     }
     248             : 
     249             :     OGRErr EndCopy();
     250             : };
     251             : 
     252             : /************************************************************************/
     253             : /*                       OGRPGDumpDataSource                            */
     254             : /************************************************************************/
     255             : class OGRPGDumpDataSource final : public GDALDataset
     256             : {
     257             :     OGRPGDumpDataSource(const OGRPGDumpDataSource &) = delete;
     258             :     OGRPGDumpDataSource &operator=(const OGRPGDumpDataSource &) = delete;
     259             : 
     260             :     std::vector<std::unique_ptr<OGRPGDumpLayer>> m_apoLayers{};
     261             :     VSILFILE *m_fp = nullptr;
     262             :     bool m_bInTransaction = false;
     263             :     OGRPGDumpLayer *m_poLayerInCopyMode = nullptr;
     264             :     const char *m_pszEOL = "\n";
     265             : 
     266             :   public:
     267             :     OGRPGDumpDataSource(const char *pszName, char **papszOptions);
     268             :     virtual ~OGRPGDumpDataSource();
     269             : 
     270             :     bool Log(const char *pszStr, bool bAddSemiColumn = true);
     271             : 
     272          21 :     virtual int GetLayerCount() override
     273             :     {
     274          21 :         return static_cast<int>(m_apoLayers.size());
     275             :     }
     276             : 
     277             :     virtual OGRLayer *GetLayer(int) override;
     278             : 
     279             :     virtual OGRLayer *ICreateLayer(const char *pszName,
     280             :                                    const OGRGeomFieldDefn *poGeomFieldDefn,
     281             :                                    CSLConstList papszOptions) override;
     282             : 
     283             :     virtual int TestCapability(const char *) override;
     284             : 
     285             :     void LogStartTransaction();
     286             :     void LogCommit();
     287             : 
     288             :     void StartCopy(OGRPGDumpLayer *poPGLayer);
     289             :     OGRErr EndCopy();
     290             : };
     291             : 
     292             : #endif /* ndef OGR_PGDUMP_H_INCLUDED */

Generated by: LCOV version 1.14