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: 2024-11-21 22:18:42 Functions: 17 17 100.0 %

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

Generated by: LCOV version 1.14