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

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  OpenGIS Simple Features Reference Implementation
       4             :  * Purpose:  Classes related to generic implementation of ExecuteSQL().
       5             :  * Author:   Frank Warmerdam, warmerdam@pobox.com
       6             :  *
       7             :  ******************************************************************************
       8             :  * Copyright (c) 2002, Frank Warmerdam
       9             :  * Copyright (c) 2010-2013, Even Rouault <even dot rouault at spatialys.com>
      10             :  *
      11             :  * SPDX-License-Identifier: MIT
      12             :  ****************************************************************************/
      13             : 
      14             : #ifndef OGR_GENSQL_H_INCLUDED
      15             : #define OGR_GENSQL_H_INCLUDED
      16             : 
      17             : #include "ogrsf_frmts.h"
      18             : #include "ogr_swq.h"
      19             : #include "cpl_hash_set.h"
      20             : #include "cpl_string.h"
      21             : 
      22             : #include <vector>
      23             : 
      24             : /*! @cond Doxygen_Suppress */
      25             : 
      26             : #define GEOM_FIELD_INDEX_TO_ALL_FIELD_INDEX(poFDefn, iGeom)                    \
      27             :     ((poFDefn)->GetFieldCount() + SPECIAL_FIELD_COUNT + (iGeom))
      28             : 
      29             : #define IS_GEOM_FIELD_INDEX(poFDefn, idx)                                      \
      30             :     (((idx) >= (poFDefn)->GetFieldCount() + SPECIAL_FIELD_COUNT) &&            \
      31             :      ((idx) < (poFDefn)->GetFieldCount() + SPECIAL_FIELD_COUNT +               \
      32             :                   (poFDefn)->GetGeomFieldCount()))
      33             : 
      34             : #define ALL_FIELD_INDEX_TO_GEOM_FIELD_INDEX(poFDefn, idx)                      \
      35             :     ((idx) - ((poFDefn)->GetFieldCount() + SPECIAL_FIELD_COUNT))
      36             : 
      37             : /************************************************************************/
      38             : /*                        OGRGenSQLResultsLayer                         */
      39             : /************************************************************************/
      40             : 
      41             : class swq_select;
      42             : 
      43             : class OGRGenSQLResultsLayer final : public OGRLayer
      44             : {
      45             :   private:
      46             :     GDALDataset *m_poSrcDS = nullptr;
      47             :     OGRLayer *m_poSrcLayer = nullptr;
      48             :     std::unique_ptr<swq_select> m_pSelectInfo{};
      49             : 
      50             :     std::string m_osInitialWHERE{};
      51             :     bool m_bForwardWhereToSourceLayer = true;
      52             :     bool m_bEOF = false;
      53             : 
      54             :     // Array of source layers (owned by m_poSrcDS or m_apoExtraDS)
      55             :     std::vector<OGRLayer *> m_apoTableLayers{};
      56             : 
      57             :     // Array of extra datasets when referencing a table/layer by a dataset name
      58             :     std::vector<std::unique_ptr<GDALDataset, GDALDatasetUniquePtrReleaser>>
      59             :         m_apoExtraDS{};
      60             : 
      61             :     OGRFeatureDefn *m_poDefn = nullptr;
      62             : 
      63             :     std::vector<int> m_anGeomFieldToSrcGeomField{};
      64             : 
      65             :     std::vector<GIntBig> m_anFIDIndex{};
      66             :     bool m_bOrderByValid = false;
      67             : 
      68             :     GIntBig m_nNextIndexFID = 0;
      69             :     std::unique_ptr<OGRFeature> m_poSummaryFeature{};
      70             : 
      71             :     int m_iFIDFieldIndex = 0;
      72             : 
      73             :     GIntBig m_nIteratedFeatures = -1;
      74             :     std::vector<std::string> m_aosDistinctList{};
      75             : 
      76             :     bool PrepareSummary();
      77             : 
      78             :     std::unique_ptr<OGRFeature> TranslateFeature(std::unique_ptr<OGRFeature>);
      79             :     void CreateOrderByIndex();
      80             :     void ReadIndexFields(OGRFeature *poSrcFeat, int nOrderItems,
      81             :                          OGRField *pasIndexFields);
      82             :     void SortIndexSection(const OGRField *pasIndexFields, GIntBig *panMerged,
      83             :                           size_t nStart, size_t nEntries);
      84             :     void FreeIndexFields(OGRField *pasIndexFields, size_t l_nIndexSize);
      85             :     int Compare(const OGRField *pasFirst, const OGRField *pasSecond);
      86             : 
      87             :     void ClearFilters();
      88             :     void ApplyFiltersToSource();
      89             : 
      90             :     void FindAndSetIgnoredFields();
      91             :     void ExploreExprForIgnoredFields(swq_expr_node *expr, CPLHashSet *hSet);
      92             :     void AddFieldDefnToSet(int iTable, int iColumn, CPLHashSet *hSet);
      93             : 
      94             :     int ContainGeomSpecialField(swq_expr_node *expr);
      95             : 
      96             :     void InvalidateOrderByIndex();
      97             : 
      98             :     int MustEvaluateSpatialFilterOnGenSQL();
      99             : 
     100             :     CPL_DISALLOW_COPY_ASSIGN(OGRGenSQLResultsLayer)
     101             : 
     102             :   public:
     103             :     OGRGenSQLResultsLayer(GDALDataset *poSrcDS,
     104             :                           std::unique_ptr<swq_select> &&pSelectInfo,
     105             :                           const OGRGeometry *poSpatFilter, const char *pszWHERE,
     106             :                           const char *pszDialect);
     107             :     virtual ~OGRGenSQLResultsLayer();
     108             : 
     109             :     virtual OGRGeometry *GetSpatialFilter() override;
     110             : 
     111             :     virtual void ResetReading() override;
     112             :     virtual OGRFeature *GetNextFeature() override;
     113             :     virtual OGRErr SetNextByIndex(GIntBig nIndex) override;
     114             :     virtual OGRFeature *GetFeature(GIntBig nFID) override;
     115             : 
     116             :     virtual OGRFeatureDefn *GetLayerDefn() override;
     117             : 
     118             :     virtual GIntBig GetFeatureCount(int bForce = TRUE) override;
     119             : 
     120           3 :     virtual OGRErr GetExtent(OGREnvelope *psExtent, int bForce = TRUE) override
     121             :     {
     122           3 :         return GetExtent(0, psExtent, bForce);
     123             :     }
     124             : 
     125             :     virtual OGRErr GetExtent(int iGeomField, OGREnvelope *psExtent,
     126             :                              int bForce = TRUE) override;
     127             : 
     128             :     virtual int TestCapability(const char *) override;
     129             : 
     130          31 :     virtual void SetSpatialFilter(OGRGeometry *poGeom) override
     131             :     {
     132          31 :         SetSpatialFilter(0, poGeom);
     133          31 :     }
     134             : 
     135             :     virtual void SetSpatialFilter(int iGeomField, OGRGeometry *) override;
     136             :     virtual OGRErr SetAttributeFilter(const char *) override;
     137             : 
     138             :     bool GetArrowStream(struct ArrowArrayStream *out_stream,
     139             :                         CSLConstList papszOptions = nullptr) override;
     140             : 
     141             :     int GetArrowSchema(struct ArrowArrayStream *stream,
     142             :                        struct ArrowSchema *out_schema) override;
     143             : 
     144             :   protected:
     145             :     friend struct OGRGenSQLResultsLayerArrowStreamPrivateData;
     146             : 
     147             :     int GetArrowSchemaForwarded(struct ArrowArrayStream *stream,
     148             :                                 struct ArrowSchema *out_schema) const;
     149             : 
     150             :     int GetNextArrowArray(struct ArrowArrayStream *stream,
     151             :                           struct ArrowArray *out_array) override;
     152             : 
     153             :     int GetNextArrowArrayForwarded(struct ArrowArrayStream *stream,
     154             :                                    struct ArrowArray *out_array);
     155             : };
     156             : 
     157             : /*! @endcond */
     158             : 
     159             : #endif /* ndef OGR_GENSQL_H_INCLUDED */

Generated by: LCOV version 1.14