LCOV - code coverage report
Current view: top level - ogr/ogrsf_frmts/sqlite - ogr_sqlite.h (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 84 97 86.6 %
Date: 2024-04-29 12:21:24 Functions: 37 43 86.0 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  * $Id$
       3             :  *
       4             :  * Project:  OpenGIS Simple Features Reference Implementation
       5             :  * Purpose:  Private definitions for OGR/SQLite driver.
       6             :  * Author:   Frank Warmerdam, warmerdam@pobox.com
       7             :  *
       8             :  ******************************************************************************
       9             :  * Copyright (c) 2004, Frank Warmerdam <warmerdam@pobox.com>
      10             :  * Copyright (c) 2009-2014, Even Rouault <even dot rouault at spatialys.com>
      11             :  *
      12             :  * Permission is hereby granted, free of charge, to any person obtaining a
      13             :  * copy of this software and associated documentation files (the "Software"),
      14             :  * to deal in the Software without restriction, including without limitation
      15             :  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      16             :  * and/or sell copies of the Software, and to permit persons to whom the
      17             :  * Software is furnished to do so, subject to the following conditions:
      18             :  *
      19             :  * The above copyright notice and this permission notice shall be included
      20             :  * in all copies or substantial portions of the Software.
      21             :  *
      22             :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
      23             :  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      24             :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
      25             :  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      26             :  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
      27             :  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
      28             :  * DEALINGS IN THE SOFTWARE.
      29             :  ****************************************************************************/
      30             : 
      31             : #ifndef OGR_SQLITE_H_INCLUDED
      32             : #define OGR_SQLITE_H_INCLUDED
      33             : 
      34             : #include <cstddef>
      35             : #include <map>
      36             : #include <set>
      37             : #include <utility>
      38             : #include <vector>
      39             : 
      40             : #include "cpl_error.h"
      41             : #include "gdal_pam.h"
      42             : #include "ogrsf_frmts.h"
      43             : #include "ogrsf_frmts.h"
      44             : #include "rasterlite2_header.h"
      45             : 
      46             : #ifdef SPATIALITE_AMALGAMATION
      47             : /*
      48             : / using an AMALGAMATED version of SpatiaLite
      49             : / a private internal copy of SQLite is included:
      50             : / so we are required including the SpatiaLite's
      51             : / own header
      52             : /
      53             : / IMPORTANT NOTICE: using AMALAGATION is only
      54             : / useful on Windows (to skip DLL hell related oddities)
      55             : /
      56             : / You MUST NOT use AMALGAMATION on Linux or any
      57             : / other "sane" operating system !!!!
      58             : */
      59             : #include <spatialite/sqlite3.h>
      60             : #else
      61             : #include <sqlite3.h>
      62             : #endif
      63             : 
      64             : #define UNINITIALIZED_SRID -2
      65             : 
      66             : #if defined(DEBUG) || defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) ||     \
      67             :     defined(ALLOW_FORMAT_DUMPS)
      68             : // Enable accepting a SQL dump (starting with a "-- SQL SQLITE" or
      69             : // "-- SQL RASTERLITE" or "--SQL MBTILES" line) as a valid
      70             : // file. This makes fuzzer life easier
      71             : #define ENABLE_SQL_SQLITE_FORMAT
      72             : #endif
      73             : 
      74             : #include "ogrsqlitebase.h"
      75             : 
      76             : /************************************************************************/
      77             : /*      SpatiaLite's own Geometry type IDs.                             */
      78             : /************************************************************************/
      79             : 
      80             : enum OGRSpatialiteGeomType
      81             : {
      82             :     // 2D [XY]
      83             :     OGRSplitePointXY = 1,
      84             :     OGRSpliteLineStringXY = 2,
      85             :     OGRSplitePolygonXY = 3,
      86             :     OGRSpliteMultiPointXY = 4,
      87             :     OGRSpliteMultiLineStringXY = 5,
      88             :     OGRSpliteMultiPolygonXY = 6,
      89             :     OGRSpliteGeometryCollectionXY = 7,
      90             :     // 3D [XYZ]
      91             :     OGRSplitePointXYZ = 1001,
      92             :     OGRSpliteLineStringXYZ = 1002,
      93             :     OGRSplitePolygonXYZ = 1003,
      94             :     OGRSpliteMultiPointXYZ = 1004,
      95             :     OGRSpliteMultiLineStringXYZ = 1005,
      96             :     OGRSpliteMultiPolygonXYZ = 1006,
      97             :     OGRSpliteGeometryCollectionXYZ = 1007,
      98             :     // 2D with Measure [XYM]
      99             :     OGRSplitePointXYM = 2001,
     100             :     OGRSpliteLineStringXYM = 2002,
     101             :     OGRSplitePolygonXYM = 2003,
     102             :     OGRSpliteMultiPointXYM = 2004,
     103             :     OGRSpliteMultiLineStringXYM = 2005,
     104             :     OGRSpliteMultiPolygonXYM = 2006,
     105             :     OGRSpliteGeometryCollectionXYM = 2007,
     106             :     // 3D with Measure [XYZM]
     107             :     OGRSplitePointXYZM = 3001,
     108             :     OGRSpliteLineStringXYZM = 3002,
     109             :     OGRSplitePolygonXYZM = 3003,
     110             :     OGRSpliteMultiPointXYZM = 3004,
     111             :     OGRSpliteMultiLineStringXYZM = 3005,
     112             :     OGRSpliteMultiPolygonXYZM = 3006,
     113             :     OGRSpliteGeometryCollectionXYZM = 3007,
     114             :     // COMPRESSED: 2D [XY]
     115             :     OGRSpliteComprLineStringXY = 1000002,
     116             :     OGRSpliteComprPolygonXY = 1000003,
     117             :     OGRSpliteComprMultiPointXY = 1000004,
     118             :     OGRSpliteComprMultiLineStringXY = 1000005,
     119             :     OGRSpliteComprMultiPolygonXY = 1000006,
     120             :     OGRSpliteComprGeometryCollectionXY = 1000007,
     121             :     // COMPRESSED: 3D [XYZ]
     122             :     OGRSpliteComprLineStringXYZ = 1001002,
     123             :     OGRSpliteComprPolygonXYZ = 1001003,
     124             :     OGRSpliteComprMultiPointXYZ = 1001004,
     125             :     OGRSpliteComprMultiLineStringXYZ = 1001005,
     126             :     OGRSpliteComprMultiPolygonXYZ = 1001006,
     127             :     OGRSpliteComprGeometryCollectionXYZ = 1001007,
     128             :     // COMPRESSED: 2D with Measure [XYM]
     129             :     OGRSpliteComprLineStringXYM = 1002002,
     130             :     OGRSpliteComprPolygonXYM = 1002003,
     131             :     OGRSpliteComprMultiPointXYM = 1002004,
     132             :     OGRSpliteComprMultiLineStringXYM = 1002005,
     133             :     OGRSpliteComprMultiPolygonXYM = 1002006,
     134             :     OGRSpliteComprGeometryCollectionXYM = 1002007,
     135             :     // COMPRESSED: 3D with Measure [XYZM]
     136             :     OGRSpliteComprLineStringXYZM = 1003002,
     137             :     OGRSpliteComprPolygonXYZM = 1003003,
     138             :     OGRSpliteComprMultiPointXYZM = 1003004,
     139             :     OGRSpliteComprMultiLineStringXYZM = 1003005,
     140             :     OGRSpliteComprMultiPolygonXYZM = 1003006,
     141             :     OGRSpliteComprGeometryCollectionXYZM = 1003007
     142             : };
     143             : 
     144             : /************************************************************************/
     145             : /*                            OGRSQLiteLayer                            */
     146             : /************************************************************************/
     147             : 
     148             : class OGRSQLiteDataSource;
     149             : 
     150             : class OGRSQLiteLayer CPL_NON_FINAL : public OGRLayer,
     151             :                                      public IOGRSQLiteGetSpatialWhere
     152             : {
     153             :   private:
     154             :     static OGRErr
     155             :     createFromSpatialiteInternal(const GByte *pabyData, OGRGeometry **ppoReturn,
     156             :                                  int nBytes, OGRwkbByteOrder eByteOrder,
     157             :                                  int *pnBytesConsumed, int nRecLevel);
     158             : 
     159             :     static int CanBeCompressedSpatialiteGeometry(const OGRGeometry *poGeometry);
     160             : 
     161             :     static int ComputeSpatiaLiteGeometrySize(const OGRGeometry *poGeometry,
     162             :                                              bool bSpatialite2D,
     163             :                                              bool bUseComprGeom);
     164             : 
     165             :     static int GetSpatialiteGeometryCode(const OGRGeometry *poGeometry,
     166             :                                          bool bSpatialite2D, bool bUseComprGeom,
     167             :                                          bool bAcceptMultiGeom);
     168             : 
     169             :     static int ExportSpatiaLiteGeometryInternal(const OGRGeometry *poGeometry,
     170             :                                                 OGRwkbByteOrder eByteOrder,
     171             :                                                 bool bSpatialite2D,
     172             :                                                 bool bUseComprGeom,
     173             :                                                 GByte *pabyData);
     174             : 
     175             :   protected:
     176             :     OGRSQLiteFeatureDefn *m_poFeatureDefn = nullptr;
     177             : 
     178             :     GIntBig m_iNextShapeId = 0;
     179             : 
     180             :     sqlite3_stmt *m_hStmt = nullptr;
     181             :     bool m_bDoStep = true;
     182             :     bool m_bEOF = false;
     183             : 
     184             :     OGRSQLiteDataSource *m_poDS = nullptr;
     185             : 
     186             :     char *m_pszFIDColumn = nullptr;
     187             : 
     188             :     int *m_panFieldOrdinals = nullptr;
     189             :     int m_iFIDCol = -1;
     190             :     int m_iOGRNativeDataCol = -1;
     191             :     int m_iOGRNativeMediaTypeCol = -1;
     192             : 
     193             :     bool m_bIsVirtualShape = false;
     194             : 
     195             :     void BuildFeatureDefn(const char *pszLayerName, bool bIsSelect,
     196             :                           sqlite3_stmt *hStmt,
     197             :                           const std::set<CPLString> *paosGeomCols,
     198             :                           const std::set<CPLString> &aosIgnoredCols);
     199             : 
     200             :     void ClearStatement();
     201             :     virtual OGRErr ResetStatement() = 0;
     202             : 
     203             :     bool m_bUseComprGeom = false;
     204             : 
     205             :     char **m_papszCompressedColumns = nullptr;
     206             : 
     207             :     bool m_bAllowMultipleGeomFields = false;
     208             : 
     209             :     static CPLString FormatSpatialFilterFromRTree(
     210             :         OGRGeometry *poFilterGeom, const char *pszRowIDName,
     211             :         const char *pszEscapedTable, const char *pszEscapedGeomCol);
     212             : 
     213             :     static CPLString
     214             :     FormatSpatialFilterFromMBR(OGRGeometry *poFilterGeom,
     215             :                                const char *pszEscapedGeomColName);
     216             : 
     217             :     explicit OGRSQLiteLayer(OGRSQLiteDataSource *poDSIn);
     218             : 
     219             :   public:
     220             :     virtual ~OGRSQLiteLayer();
     221             : 
     222             :     void Finalize();
     223             : 
     224             :     GDALDataset *GetDataset() override;
     225             : 
     226             :     virtual void ResetReading() override;
     227             :     virtual OGRFeature *GetNextRawFeature();
     228             :     virtual OGRFeature *GetNextFeature() override;
     229             : 
     230             :     virtual OGRFeature *GetFeature(GIntBig nFeatureId) override;
     231             : 
     232        2136 :     virtual OGRFeatureDefn *GetLayerDefn() override
     233             :     {
     234        2136 :         return m_poFeatureDefn;
     235             :     }
     236             : 
     237          26 :     virtual OGRSQLiteFeatureDefn *myGetLayerDefn()
     238             :     {
     239          26 :         return m_poFeatureDefn;
     240             :     }
     241             : 
     242             :     virtual const char *GetFIDColumn() override;
     243             : 
     244             :     virtual int TestCapability(const char *) override;
     245             : 
     246             :     virtual OGRErr StartTransaction() override;
     247             :     virtual OGRErr CommitTransaction() override;
     248             :     virtual OGRErr RollbackTransaction() override;
     249             : 
     250           1 :     virtual void InvalidateCachedFeatureCountAndExtent()
     251             :     {
     252           1 :     }
     253             : 
     254          17 :     virtual bool IsTableLayer()
     255             :     {
     256          17 :         return false;
     257             :     }
     258             : 
     259           0 :     virtual bool HasSpatialIndex(CPL_UNUSED int iGeomField)
     260             :     {
     261           0 :         return false;
     262             :     }
     263             : 
     264           0 :     virtual bool HasFastSpatialFilter(CPL_UNUSED int iGeomCol) override
     265             :     {
     266           0 :         return false;
     267             :     }
     268             : 
     269             :     virtual CPLString
     270           0 :     GetSpatialWhere(CPL_UNUSED int iGeomCol,
     271             :                     CPL_UNUSED OGRGeometry *poFilterGeom) override
     272             :     {
     273           0 :         return "";
     274             :     }
     275             : 
     276             :     static OGRErr GetSpatialiteGeometryHeader(const GByte *pabyData, int nBytes,
     277             :                                               int *pnSRID,
     278             :                                               OGRwkbGeometryType *peType,
     279             :                                               bool *pbIsEmpty, double *pdfMinX,
     280             :                                               double *pdfMinY, double *pdfMaxX,
     281             :                                               double *pdfMaxY);
     282             :     static OGRErr ImportSpatiaLiteGeometry(const GByte *, int, OGRGeometry **);
     283             :     static OGRErr ImportSpatiaLiteGeometry(const GByte *, int, OGRGeometry **,
     284             :                                            int *pnSRID);
     285             :     static OGRErr ExportSpatiaLiteGeometry(const OGRGeometry *, GInt32,
     286             :                                            OGRwkbByteOrder, bool,
     287             :                                            bool bUseComprGeom, GByte **, int *);
     288             : };
     289             : 
     290             : /************************************************************************/
     291             : /*                         OGRSQLiteTableLayer                          */
     292             : /************************************************************************/
     293             : 
     294             : class OGRSQLiteTableLayer final : public OGRSQLiteLayer
     295             : {
     296             :     bool m_bIsTable = true;
     297             : 
     298             :     bool m_bLaunderColumnNames = true;
     299             :     bool m_bSpatialite2D = false;
     300             :     bool m_bStrict = false;
     301             : 
     302             :     CPLString m_osWHERE{};
     303             :     CPLString m_osQuery{};
     304             :     bool m_bDeferredSpatialIndexCreation = false;
     305             : 
     306             :     char *m_pszTableName = nullptr;
     307             :     char *m_pszEscapedTableName = nullptr;
     308             : 
     309             :     bool m_bLayerDefnError = false;
     310             : 
     311             :     sqlite3_stmt *m_hInsertStmt = nullptr;
     312             :     CPLString m_osLastInsertStmt{};
     313             : 
     314             :     bool m_bHasCheckedTriggers = false;
     315             :     bool m_bHasTriedDetectingFID64 = false;
     316             : 
     317             :     bool m_bStatisticsNeedsToBeFlushed = false;
     318             :     GIntBig m_nFeatureCount = -1; /* if -1, means not up-to-date */
     319             : 
     320             :     int m_bDeferredCreation = false;
     321             : 
     322             :     char *m_pszCreationGeomFormat = nullptr;
     323             :     int m_iFIDAsRegularColumnIndex = -1;
     324             : 
     325             :     void ClearInsertStmt();
     326             : 
     327             :     void BuildWhere();
     328             : 
     329             :     virtual OGRErr ResetStatement() override;
     330             : 
     331             :     OGRErr RecomputeOrdinals();
     332             : 
     333             :     void AddColumnDef(char *pszNewFieldList, size_t nBufLen,
     334             :                       OGRFieldDefn *poFldDefn);
     335             : 
     336             :     void InitFieldListForRecrerate(char *&pszNewFieldList,
     337             :                                    char *&pszFieldListForSelect,
     338             :                                    size_t &nBufLenOut, int nExtraSpace = 0);
     339             :     OGRErr RecreateTable(const char *pszFieldListForSelect,
     340             :                          const char *pszNewFieldList,
     341             :                          const char *pszGenericErrorMessage,
     342             :                          const char *pszAdditionalDef = nullptr);
     343             :     OGRErr BindValues(OGRFeature *poFeature, sqlite3_stmt *hStmt,
     344             :                       bool bBindUnsetAsNull);
     345             : 
     346             :     bool CheckSpatialIndexTable(int iGeomCol);
     347             : 
     348             :     CPLErr EstablishFeatureDefn(const char *pszGeomCol, bool bMayEmitError);
     349             : 
     350             :     void LoadStatistics();
     351             :     void LoadStatisticsSpatialite4DB();
     352             : 
     353             :     CPLString FieldDefnToSQliteFieldDefn(OGRFieldDefn *poFieldDefn);
     354             : 
     355             :     OGRErr RunAddGeometryColumn(const OGRSQLiteGeomFieldDefn *poGeomField,
     356             :                                 bool bAddColumnsForNonSpatialite);
     357             : 
     358             :   public:
     359             :     explicit OGRSQLiteTableLayer(OGRSQLiteDataSource *);
     360             :     virtual ~OGRSQLiteTableLayer();
     361             : 
     362             :     CPLErr Initialize(const char *pszTableName, bool bIsTable,
     363             :                       bool bIsVirtualShapeIn, bool bDeferredCreation,
     364             :                       bool bMayEmitError);
     365             :     void SetCreationParameters(const char *pszFIDColumnName,
     366             :                                OGRwkbGeometryType eGeomType,
     367             :                                const char *pszGeomFormat,
     368             :                                const char *pszGeometryName,
     369             :                                OGRSpatialReference *poSRS, int nSRSId);
     370             :     virtual const char *GetName() override;
     371             : 
     372             :     virtual GIntBig GetFeatureCount(int) override;
     373             :     virtual OGRErr GetExtent(OGREnvelope *psExtent, int bForce) override;
     374             :     virtual OGRErr GetExtent(int iGeomField, OGREnvelope *psExtent,
     375             :                              int bForce) override;
     376             : 
     377             :     virtual OGRFeatureDefn *GetLayerDefn() override;
     378             : 
     379        5999 :     bool HasLayerDefnError()
     380             :     {
     381        5999 :         GetLayerDefn();
     382        5999 :         return m_bLayerDefnError;
     383             :     }
     384             : 
     385             :     virtual void SetSpatialFilter(OGRGeometry *) override;
     386             :     virtual void SetSpatialFilter(int iGeomField, OGRGeometry *) override;
     387             :     virtual OGRErr SetAttributeFilter(const char *) override;
     388             :     virtual OGRErr ISetFeature(OGRFeature *poFeature) override;
     389             :     virtual OGRErr DeleteFeature(GIntBig nFID) override;
     390             :     virtual OGRErr ICreateFeature(OGRFeature *poFeature) override;
     391             : 
     392             :     virtual OGRErr CreateField(const OGRFieldDefn *poField,
     393             :                                int bApproxOK = TRUE) override;
     394             :     virtual OGRErr CreateGeomField(const OGRGeomFieldDefn *poGeomFieldIn,
     395             :                                    int bApproxOK = TRUE) override;
     396             :     virtual OGRErr DeleteField(int iField) override;
     397             :     virtual OGRErr ReorderFields(int *panMap) override;
     398             :     virtual OGRErr AlterFieldDefn(int iField, OGRFieldDefn *poNewFieldDefn,
     399             :                                   int nFlags) override;
     400             :     OGRErr AddForeignKeysToTable(const char *pszKeys);
     401             : 
     402             :     virtual OGRFeature *GetNextFeature() override;
     403             :     virtual OGRFeature *GetFeature(GIntBig nFeatureId) override;
     404             : 
     405             :     virtual int TestCapability(const char *) override;
     406             : 
     407             :     virtual char **GetMetadata(const char *pszDomain = "") override;
     408             :     virtual const char *GetMetadataItem(const char *pszName,
     409             :                                         const char *pszDomain = "") override;
     410             : 
     411             :     // follow methods are not base class overrides
     412         382 :     void SetLaunderFlag(bool bFlag)
     413             :     {
     414         382 :         m_bLaunderColumnNames = bFlag;
     415         382 :     }
     416             : 
     417          47 :     void SetUseCompressGeom(bool bFlag)
     418             :     {
     419          47 :         m_bUseComprGeom = bFlag;
     420          47 :     }
     421             : 
     422         147 :     void SetDeferredSpatialIndexCreation(bool bFlag)
     423             :     {
     424         147 :         m_bDeferredSpatialIndexCreation = bFlag;
     425         147 :     }
     426             : 
     427             :     void SetCompressedColumns(const char *pszCompressedColumns);
     428             : 
     429         382 :     void SetStrictFlag(bool bFlag)
     430             :     {
     431         382 :         m_bStrict = bFlag;
     432         382 :     }
     433             : 
     434             :     int CreateSpatialIndex(int iGeomCol);
     435             : 
     436             :     void CreateSpatialIndexIfNecessary();
     437             : 
     438             :     void InitFeatureCount();
     439             :     bool DoStatisticsNeedToBeFlushed();
     440             :     void ForceStatisticsToBeFlushed();
     441             :     bool AreStatisticsValid();
     442             :     int SaveStatistics();
     443             : 
     444             :     virtual void InvalidateCachedFeatureCountAndExtent() override;
     445             : 
     446       13378 :     virtual bool IsTableLayer() override
     447             :     {
     448       13378 :         return true;
     449             :     }
     450             : 
     451             :     virtual bool HasSpatialIndex(int iGeomField) override;
     452             :     virtual bool HasFastSpatialFilter(int iGeomCol) override;
     453             :     virtual CPLString GetSpatialWhere(int iGeomCol,
     454             :                                       OGRGeometry *poFilterGeom) override;
     455             : 
     456             :     OGRErr RunDeferredCreationIfNecessary();
     457             : };
     458             : 
     459             : /************************************************************************/
     460             : /*                         OGRSQLiteViewLayer                           */
     461             : /************************************************************************/
     462             : 
     463             : class OGRSQLiteViewLayer final : public OGRSQLiteLayer
     464             : {
     465             :     CPLString m_osWHERE{};
     466             :     CPLString m_osQuery{};
     467             :     bool m_bHasCheckedSpatialIndexTable = false;
     468             : 
     469             :     OGRSQLiteGeomFormat m_eGeomFormat = OSGF_None;
     470             :     CPLString m_osGeomColumn{};
     471             :     bool m_bHasSpatialIndex = false;
     472             : 
     473             :     char *m_pszViewName = nullptr;
     474             :     char *m_pszEscapedTableName = nullptr;
     475             :     char *m_pszEscapedUnderlyingTableName = nullptr;
     476             : 
     477             :     bool m_bLayerDefnError = false;
     478             : 
     479             :     CPLString m_osUnderlyingTableName{};
     480             :     CPLString m_osUnderlyingGeometryColumn{};
     481             : 
     482             :     OGRSQLiteLayer *m_poUnderlyingLayer = nullptr;
     483             : 
     484             :     OGRSQLiteLayer *GetUnderlyingLayer();
     485             : 
     486             :     void BuildWhere();
     487             : 
     488             :     virtual OGRErr ResetStatement() override;
     489             : 
     490             :     CPLErr EstablishFeatureDefn();
     491             : 
     492             :   public:
     493             :     explicit OGRSQLiteViewLayer(OGRSQLiteDataSource *);
     494             :     virtual ~OGRSQLiteViewLayer();
     495             : 
     496          27 :     virtual const char *GetName() override
     497             :     {
     498          27 :         return m_pszViewName;
     499             :     }
     500             : 
     501             :     virtual OGRwkbGeometryType GetGeomType() override;
     502             : 
     503             :     CPLErr Initialize(const char *pszViewName, const char *pszViewGeometry,
     504             :                       const char *pszViewRowid, const char *pszTableName,
     505             :                       const char *pszGeometryColumn);
     506             : 
     507             :     virtual OGRFeatureDefn *GetLayerDefn() override;
     508             : 
     509          19 :     bool HasLayerDefnError()
     510             :     {
     511          19 :         GetLayerDefn();
     512          19 :         return m_bLayerDefnError;
     513             :     }
     514             : 
     515             :     virtual OGRFeature *GetNextFeature() override;
     516             :     virtual GIntBig GetFeatureCount(int) override;
     517             : 
     518             :     virtual void SetSpatialFilter(OGRGeometry *) override;
     519             : 
     520           0 :     virtual void SetSpatialFilter(int iGeomField, OGRGeometry *poGeom) override
     521             :     {
     522           0 :         OGRSQLiteLayer::SetSpatialFilter(iGeomField, poGeom);
     523           0 :     }
     524             : 
     525             :     virtual OGRErr SetAttributeFilter(const char *) override;
     526             : 
     527             :     virtual OGRFeature *GetFeature(GIntBig nFeatureId) override;
     528             : 
     529             :     virtual int TestCapability(const char *) override;
     530             : 
     531           0 :     virtual bool HasSpatialIndex(CPL_UNUSED int iGeomField) override
     532             :     {
     533           0 :         return m_bHasSpatialIndex;
     534             :     }
     535             : 
     536             :     virtual CPLString GetSpatialWhere(int iGeomCol,
     537             :                                       OGRGeometry *poFilterGeom) override;
     538             : };
     539             : 
     540             : /************************************************************************/
     541             : /*                         OGRSQLiteSelectLayer                         */
     542             : /************************************************************************/
     543             : 
     544             : class OGRSQLiteSelectLayer CPL_NON_FINAL : public OGRSQLiteLayer,
     545             :                                            public IOGRSQLiteSelectLayer
     546             : {
     547             :     OGRSQLiteSelectLayerCommonBehaviour *m_poBehavior = nullptr;
     548             :     bool m_bCanReopenBaseDS = false;
     549             : 
     550             :     virtual OGRErr ResetStatement() override;
     551             : 
     552             :     CPL_DISALLOW_COPY_ASSIGN(OGRSQLiteSelectLayer)
     553             : 
     554             :   public:
     555             :     OGRSQLiteSelectLayer(OGRSQLiteDataSource *, const CPLString &osSQL,
     556             :                          sqlite3_stmt *, bool bUseStatementForGetNextFeature,
     557             :                          bool bEmptyLayer, bool bAllowMultipleGeomFields,
     558             :                          bool bCanReopenBaseDS);
     559             :     virtual ~OGRSQLiteSelectLayer();
     560             : 
     561             :     virtual void ResetReading() override;
     562             : 
     563             :     virtual OGRFeature *GetNextFeature() override;
     564             :     virtual GIntBig GetFeatureCount(int) override;
     565             : 
     566         117 :     virtual void SetSpatialFilter(OGRGeometry *poGeom) override
     567             :     {
     568         117 :         SetSpatialFilter(0, poGeom);
     569         117 :     }
     570             : 
     571             :     virtual void SetSpatialFilter(int iGeomField, OGRGeometry *) override;
     572             :     virtual OGRErr SetAttributeFilter(const char *) override;
     573             : 
     574             :     virtual int TestCapability(const char *) override;
     575             : 
     576           2 :     virtual OGRErr GetExtent(OGREnvelope *psExtent, int bForce = TRUE) override
     577             :     {
     578           2 :         return GetExtent(0, psExtent, bForce);
     579             :     }
     580             : 
     581             :     virtual OGRErr GetExtent(int iGeomField, OGREnvelope *psExtent,
     582             :                              int bForce = TRUE) override;
     583             : 
     584        2136 :     virtual OGRFeatureDefn *GetLayerDefn() override
     585             :     {
     586        2136 :         return OGRSQLiteLayer::GetLayerDefn();
     587             :     }
     588             : 
     589         725 :     virtual char *&GetAttrQueryString() override
     590             :     {
     591         725 :         return m_pszAttrQueryString;
     592             :     }
     593             : 
     594         203 :     virtual OGRFeatureQuery *&GetFeatureQuery() override
     595             :     {
     596         203 :         return m_poAttrQuery;
     597             :     }
     598             : 
     599         494 :     virtual OGRGeometry *&GetFilterGeom() override
     600             :     {
     601         494 :         return m_poFilterGeom;
     602             :     }
     603             : 
     604         270 :     virtual int &GetIGeomFieldFilter() override
     605             :     {
     606         270 :         return m_iGeomFieldFilter;
     607             :     }
     608             : 
     609         348 :     virtual OGRSpatialReference *GetSpatialRef() override
     610             :     {
     611         348 :         return OGRSQLiteLayer::GetSpatialRef();
     612             :     }
     613             : 
     614         163 :     virtual int InstallFilter(OGRGeometry *poGeomIn) override
     615             :     {
     616         163 :         return OGRSQLiteLayer::InstallFilter(poGeomIn);
     617             :     }
     618             : 
     619         551 :     virtual int HasReadFeature() override
     620             :     {
     621         551 :         return m_iNextShapeId > 0;
     622             :     }
     623             : 
     624         364 :     virtual void BaseResetReading() override
     625             :     {
     626         364 :         OGRSQLiteLayer::ResetReading();
     627         364 :     }
     628             : 
     629        3085 :     virtual OGRFeature *BaseGetNextFeature() override
     630             :     {
     631        3085 :         return OGRSQLiteLayer::GetNextFeature();
     632             :     }
     633             : 
     634           2 :     virtual OGRErr BaseSetAttributeFilter(const char *pszQuery) override
     635             :     {
     636           2 :         return OGRSQLiteLayer::SetAttributeFilter(pszQuery);
     637             :     }
     638             : 
     639           9 :     virtual GIntBig BaseGetFeatureCount(int bForce) override
     640             :     {
     641           9 :         return OGRSQLiteLayer::GetFeatureCount(bForce);
     642             :     }
     643             : 
     644          50 :     virtual int BaseTestCapability(const char *pszCap) override
     645             :     {
     646          50 :         return OGRSQLiteLayer::TestCapability(pszCap);
     647             :     }
     648             : 
     649           5 :     virtual OGRErr BaseGetExtent(OGREnvelope *psExtent, int bForce) override
     650             :     {
     651           5 :         return OGRSQLiteLayer::GetExtent(psExtent, bForce);
     652             :     }
     653             : 
     654           0 :     virtual OGRErr BaseGetExtent(int iGeomField, OGREnvelope *psExtent,
     655             :                                  int bForce) override
     656             :     {
     657           0 :         return OGRSQLiteLayer::GetExtent(iGeomField, psExtent, bForce);
     658             :     }
     659             : 
     660             :     bool
     661         171 :     ValidateGeometryFieldIndexForSetSpatialFilter(int iGeomField,
     662             :                                                   const OGRGeometry *poGeomIn,
     663             :                                                   bool bIsSelectLayer) override
     664             :     {
     665         171 :         return OGRSQLiteLayer::ValidateGeometryFieldIndexForSetSpatialFilter(
     666         171 :             iGeomField, poGeomIn, bIsSelectLayer);
     667             :     }
     668             : };
     669             : 
     670             : /************************************************************************/
     671             : /*                         OGRSQLiteDataSource                          */
     672             : /************************************************************************/
     673             : 
     674             : class OGR2SQLITEModule;
     675             : 
     676             : class OGRSQLiteDataSource final : public OGRSQLiteBaseDataSource
     677             : {
     678             :     OGRSQLiteLayer **m_papoLayers = nullptr;
     679             :     int m_nLayers = 0;
     680             : 
     681             :     // We maintain a list of known SRID to reduce the number of trips to
     682             :     // the database to get SRSes.
     683             :     std::map<int,
     684             :              std::unique_ptr<OGRSpatialReference, OGRSpatialReferenceReleaser>>
     685             :         m_oSRSCache{};
     686             : 
     687             :     OGRSpatialReference *AddSRIDToCache(
     688             :         int nId,
     689             :         std::unique_ptr<OGRSpatialReference, OGRSpatialReferenceReleaser>
     690             :             &&poSRS);
     691             : 
     692             :     bool m_bHaveGeometryColumns = false;
     693             :     bool m_bIsSpatiaLiteDB = false;
     694             :     bool m_bSpatialite4Layout = false;
     695             : 
     696             :     int m_nUndefinedSRID = -1;
     697             : 
     698             :     virtual void DeleteLayer(const char *pszLayer);
     699             : 
     700             :     const char *GetSRTEXTColName();
     701             : 
     702             :     bool InitWithEPSG();
     703             : 
     704             :     bool OpenVirtualTable(const char *pszName, const char *pszSQL);
     705             : 
     706             :     GIntBig m_nFileTimestamp = 0;
     707             :     bool m_bLastSQLCommandIsUpdateLayerStatistics = false;
     708             : 
     709             :     std::map<CPLString, std::set<CPLString>> m_aoMapTableToSetOfGeomCols{};
     710             : 
     711             :     void SaveStatistics();
     712             : 
     713             :     std::vector<OGRLayer *> m_apoInvisibleLayers{};
     714             : 
     715             : #ifdef HAVE_RASTERLITE2
     716             :     void *m_hRL2Ctxt = nullptr;
     717             :     bool InitRasterLite2();
     718             :     void FinishRasterLite2();
     719             : 
     720             :     CPLString m_osCoverageName{};
     721             :     GIntBig m_nSectionId = -1;
     722             :     rl2CoveragePtr m_pRL2Coverage = nullptr;
     723             :     bool m_bRL2MixedResolutions = false;
     724             : #endif
     725             :     CPLStringList m_aosSubDatasets{};
     726             :     bool m_bGeoTransformValid = false;
     727             :     double m_adfGeoTransform[6];
     728             :     OGRSpatialReference m_oSRS{};
     729             :     bool m_bPromote1BitAs8Bit = false;
     730             :     bool OpenRaster();
     731             :     bool OpenRasterSubDataset(const char *pszConnectionId);
     732             :     OGRSQLiteDataSource *m_poParentDS = nullptr;
     733             :     std::vector<OGRSQLiteDataSource *> m_apoOverviewDS{};
     734             : 
     735             :     OGR2SQLITEModule *m_poSQLiteModule = nullptr;
     736             : 
     737             : #ifdef HAVE_RASTERLITE2
     738             :     void ListOverviews();
     739             :     void CreateRL2OverviewDatasetIfNeeded(double dfXRes, double dfYRes);
     740             : #endif
     741             : 
     742             :     bool OpenOrCreateDB(int flags, bool bRegisterOGR2SQLiteExtensions);
     743             : 
     744             :     CPLErr Close() override;
     745             : 
     746             :     void PostInitSpatialite();
     747             : 
     748             :   public:
     749             :     OGRSQLiteDataSource();
     750             :     virtual ~OGRSQLiteDataSource();
     751             : 
     752             :     bool Open(GDALOpenInfo *poOpenInfo);
     753             :     bool Create(const char *, char **papszOptions);
     754             : 
     755             :     bool OpenTable(const char *pszTableName, bool IsTable, bool bIsVirtualShape,
     756             :                    bool bMayEmitError);
     757             :     bool OpenView(const char *pszViewName, const char *pszViewGeometry,
     758             :                   const char *pszViewRowid, const char *pszTableName,
     759             :                   const char *pszGeometryColumn);
     760             : 
     761       19109 :     virtual int GetLayerCount() override
     762             :     {
     763       19109 :         return m_nLayers;
     764             :     }
     765             : 
     766             :     virtual OGRLayer *GetLayer(int) override;
     767             :     virtual OGRLayer *GetLayerByName(const char *) override;
     768             :     virtual bool IsLayerPrivate(int) const override;
     769             :     OGRLayer *GetLayerByNameNotVisible(const char *);
     770             :     virtual std::pair<OGRLayer *, IOGRSQLiteGetSpatialWhere *>
     771             :     GetLayerWithGetSpatialWhereByName(const char *pszName) override;
     772             : 
     773             :     virtual OGRLayer *ICreateLayer(const char *pszName,
     774             :                                    const OGRGeomFieldDefn *poGeomFieldDefn,
     775             :                                    CSLConstList papszOptions) override;
     776             :     virtual OGRErr DeleteLayer(int) override;
     777             : 
     778             :     virtual int TestCapability(const char *) override;
     779             : 
     780             :     virtual OGRLayer *ExecuteSQL(const char *pszSQLCommand,
     781             :                                  OGRGeometry *poSpatialFilter,
     782             :                                  const char *pszDialect) override;
     783             :     virtual void ReleaseResultSet(OGRLayer *poLayer) override;
     784             : 
     785             :     virtual CPLErr FlushCache(bool bAtClosing) override;
     786             : 
     787             :     virtual OGRErr CommitTransaction() override;
     788             :     virtual OGRErr RollbackTransaction() override;
     789             : 
     790             :     virtual char **GetMetadata(const char *pszDomain = "") override;
     791             : 
     792             :     virtual CPLErr GetGeoTransform(double *padfGeoTransform) override;
     793             :     const OGRSpatialReference *GetSpatialRef() const override;
     794             : 
     795             :     static char *LaunderName(const char *);
     796             :     int FetchSRSId(const OGRSpatialReference *poSRS);
     797             :     OGRSpatialReference *FetchSRS(int nSRID);
     798             : 
     799           1 :     void DisableUpdate()
     800             :     {
     801           1 :         eAccess = GA_ReadOnly;
     802           1 :     }
     803             : 
     804             :     void SetName(const char *pszNameIn);
     805             : 
     806         751 :     const std::set<CPLString> &GetGeomColsForTable(const char *pszTableName)
     807             :     {
     808         751 :         return m_aoMapTableToSetOfGeomCols[pszTableName];
     809             :     }
     810             : 
     811          10 :     GIntBig GetFileTimestamp() const
     812             :     {
     813          10 :         return m_nFileTimestamp;
     814             :     }
     815             : 
     816        3646 :     bool IsSpatialiteDB() const
     817             :     {
     818        3646 :         return m_bIsSpatiaLiteDB;
     819             :     }
     820             : 
     821        3158 :     bool HasSpatialite4Layout() const
     822             :     {
     823        3158 :         return m_bSpatialite4Layout;
     824             :     }
     825             : 
     826         718 :     int GetUndefinedSRID() const
     827             :     {
     828         718 :         return m_nUndefinedSRID;
     829             :     }
     830             : 
     831         382 :     bool HasGeometryColumns() const
     832             :     {
     833         382 :         return m_bHaveGeometryColumns;
     834             :     }
     835             : 
     836             :     bool AddRelationship(std::unique_ptr<GDALRelationship> &&relationship,
     837             :                          std::string &failureReason) override;
     838             :     bool ValidateRelationship(const GDALRelationship *poRelationship,
     839             :                               std::string &failureReason);
     840             : 
     841             :     void ReloadLayers();
     842             : 
     843             : #ifdef HAVE_RASTERLITE2
     844             :     void *GetRL2Context() const
     845             :     {
     846             :         return m_hRL2Ctxt;
     847             :     }
     848             : 
     849             :     rl2CoveragePtr GetRL2CoveragePtr() const
     850             :     {
     851             :         return m_pRL2Coverage;
     852             :     }
     853             : 
     854             :     GIntBig GetSectionId() const
     855             :     {
     856             :         return m_nSectionId;
     857             :     }
     858             : 
     859             :     const double *GetGeoTransform() const
     860             :     {
     861             :         return m_adfGeoTransform;
     862             :     }
     863             : 
     864             :     bool IsRL2MixedResolutions() const
     865             :     {
     866             :         return m_bRL2MixedResolutions;
     867             :     }
     868             : 
     869             :     virtual CPLErr IBuildOverviews(const char *, int, const int *, int,
     870             :                                    const int *, GDALProgressFunc, void *,
     871             :                                    CSLConstList papszOptions) override;
     872             : 
     873             : #endif
     874             :     OGRSQLiteDataSource *GetParentDS() const
     875             :     {
     876             :         return m_poParentDS;
     877             :     }
     878             : 
     879             :     const std::vector<OGRSQLiteDataSource *> &GetOverviews() const
     880             :     {
     881             :         return m_apoOverviewDS;
     882             :     }
     883             : 
     884             :     bool HasPromote1BitAS8Bit() const
     885             :     {
     886             :         return m_bPromote1BitAs8Bit;
     887             :     }
     888             : };
     889             : 
     890             : #ifdef HAVE_RASTERLITE2
     891             : /************************************************************************/
     892             : /*                           RL2RasterBand                              */
     893             : /************************************************************************/
     894             : 
     895             : class RL2RasterBand final : public GDALPamRasterBand
     896             : {
     897             :     bool m_bHasNoData;
     898             :     double m_dfNoDataValue;
     899             :     GDALColorInterp m_eColorInterp;
     900             :     GDALColorTable *m_poCT;
     901             : 
     902             :   public:
     903             :     RL2RasterBand(int nBandIn, int nPixelType, GDALDataType eDT, int nBits,
     904             :                   bool bPromote1BitAs8Bit, int nBlockXSizeIn, int nBlockYSizeIn,
     905             :                   bool bHasNoDataIn, double dfNoDataValueIn);
     906             :     explicit RL2RasterBand(const RL2RasterBand *poOther);
     907             : 
     908             :     virtual ~RL2RasterBand();
     909             : 
     910             :   protected:
     911             :     virtual CPLErr IReadBlock(int, int, void *) override;
     912             : 
     913             :     virtual GDALColorInterp GetColorInterpretation() override
     914             :     {
     915             :         return m_eColorInterp;
     916             :     }
     917             : 
     918             :     virtual double GetNoDataValue(int *pbSuccess = nullptr) override;
     919             :     virtual GDALColorTable *GetColorTable() override;
     920             :     virtual int GetOverviewCount() override;
     921             :     virtual GDALRasterBand *GetOverview(int) override;
     922             : };
     923             : #endif  // HAVE_RASTERLITE2
     924             : 
     925             : CPLString OGRSQLiteFieldDefnToSQliteFieldDefn(OGRFieldDefn *poFieldDefn,
     926             :                                               bool bSQLiteDialectInternalUse,
     927             :                                               bool bStrict);
     928             : 
     929             : void OGRSQLiteRegisterInflateDeflate(sqlite3 *hDB);
     930             : 
     931             : void OGRSQLiteDriverUnload(GDALDriver *);
     932             : 
     933             : #ifdef HAVE_RASTERLITE2
     934             : GDALDataset *OGRSQLiteDriverCreateCopy(const char *, GDALDataset *, int,
     935             :                                        char **, GDALProgressFunc pfnProgress,
     936             :                                        void *pProgressData);
     937             : #endif
     938             : 
     939             : #endif /* ndef OGR_SQLITE_H_INCLUDED */

Generated by: LCOV version 1.14