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

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

Generated by: LCOV version 1.14