GDAL
ogr_gensql.h
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  * 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_GENSQL_H_INCLUDED
32 #define OGR_GENSQL_H_INCLUDED
33 
34 #include "ogrsf_frmts.h"
35 #include "ogr_swq.h"
36 #include "cpl_hash_set.h"
37 #include "cpl_string.h"
38 
39 #include <vector>
40 
43 #define GEOM_FIELD_INDEX_TO_ALL_FIELD_INDEX(poFDefn, iGeom) \
44  ((poFDefn)->GetFieldCount() + SPECIAL_FIELD_COUNT + (iGeom))
45 
46 #define IS_GEOM_FIELD_INDEX(poFDefn, idx) \
47  (((idx) >= (poFDefn)->GetFieldCount() + SPECIAL_FIELD_COUNT) && \
48  ((idx) < (poFDefn)->GetFieldCount() + SPECIAL_FIELD_COUNT + \
49  (poFDefn)->GetGeomFieldCount()))
50 
51 #define ALL_FIELD_INDEX_TO_GEOM_FIELD_INDEX(poFDefn, idx) \
52  ((idx) - ((poFDefn)->GetFieldCount() + SPECIAL_FIELD_COUNT))
53 
54 /************************************************************************/
55 /* OGRGenSQLResultsLayer */
56 /************************************************************************/
57 
58 class swq_select;
59 
60 class OGRGenSQLResultsLayer final : public OGRLayer
61 {
62  private:
63  GDALDataset *m_poSrcDS = nullptr;
64  OGRLayer *m_poSrcLayer = nullptr;
65  std::unique_ptr<swq_select> m_pSelectInfo{};
66 
67  std::string m_osInitialWHERE{};
68  bool m_bForwardWhereToSourceLayer = true;
69  bool m_bEOF = false;
70 
71  // Array of source layers (owned by m_poSrcDS or m_apoExtraDS)
72  std::vector<OGRLayer *> m_apoTableLayers{};
73 
74  // Array of extra datasets when referencing a table/layer by a dataset name
75  std::vector<std::unique_ptr<GDALDataset, GDALDatasetUniquePtrReleaser>>
76  m_apoExtraDS{};
77 
78  OGRFeatureDefn *m_poDefn = nullptr;
79 
80  std::vector<int> m_anGeomFieldToSrcGeomField{};
81 
82  std::vector<GIntBig> m_anFIDIndex{};
83  bool m_bOrderByValid = false;
84 
85  GIntBig m_nNextIndexFID = 0;
86  std::unique_ptr<OGRFeature> m_poSummaryFeature{};
87 
88  int m_iFIDFieldIndex = 0;
89 
90  GIntBig m_nIteratedFeatures = -1;
91  std::vector<std::string> m_aosDistinctList{};
92 
93  bool PrepareSummary();
94 
95  std::unique_ptr<OGRFeature> TranslateFeature(std::unique_ptr<OGRFeature>);
96  void CreateOrderByIndex();
97  void ReadIndexFields(OGRFeature *poSrcFeat, int nOrderItems,
98  OGRField *pasIndexFields);
99  void SortIndexSection(const OGRField *pasIndexFields, GIntBig *panMerged,
100  size_t nStart, size_t nEntries);
101  void FreeIndexFields(OGRField *pasIndexFields, size_t l_nIndexSize);
102  int Compare(const OGRField *pasFirst, const OGRField *pasSecond);
103 
104  void ClearFilters();
105  void ApplyFiltersToSource();
106 
107  void FindAndSetIgnoredFields();
108  void ExploreExprForIgnoredFields(swq_expr_node *expr, CPLHashSet *hSet);
109  void AddFieldDefnToSet(int iTable, int iColumn, CPLHashSet *hSet);
110 
111  int ContainGeomSpecialField(swq_expr_node *expr);
112 
113  void InvalidateOrderByIndex();
114 
115  int MustEvaluateSpatialFilterOnGenSQL();
116 
117  CPL_DISALLOW_COPY_ASSIGN(OGRGenSQLResultsLayer)
118 
119  public:
120  OGRGenSQLResultsLayer(GDALDataset *poSrcDS,
121  std::unique_ptr<swq_select> &&pSelectInfo,
122  const OGRGeometry *poSpatFilter, const char *pszWHERE,
123  const char *pszDialect);
124  virtual ~OGRGenSQLResultsLayer();
125 
126  virtual OGRGeometry *GetSpatialFilter() override;
127 
128  virtual void ResetReading() override;
129  virtual OGRFeature *GetNextFeature() override;
130  virtual OGRErr SetNextByIndex(GIntBig nIndex) override;
131  virtual OGRFeature *GetFeature(GIntBig nFID) override;
132 
133  virtual OGRFeatureDefn *GetLayerDefn() override;
134 
135  virtual GIntBig GetFeatureCount(int bForce = TRUE) override;
136 
137  virtual OGRErr GetExtent(OGREnvelope *psExtent, int bForce = TRUE) override
138  {
139  return GetExtent(0, psExtent, bForce);
140  }
141 
142  virtual OGRErr GetExtent(int iGeomField, OGREnvelope *psExtent,
143  int bForce = TRUE) override;
144 
145  virtual int TestCapability(const char *) override;
146 
147  virtual void SetSpatialFilter(OGRGeometry *poGeom) override
148  {
149  SetSpatialFilter(0, poGeom);
150  }
151 
152  virtual void SetSpatialFilter(int iGeomField, OGRGeometry *) override;
153  virtual OGRErr SetAttributeFilter(const char *) override;
154 };
155 
158 #endif /* ndef OGR_GENSQL_H_INCLUDED */
A set of associated raster bands, usually from one file.
Definition: gdal_priv.h:503
Simple container for a bounding region (rectangle)
Definition: ogr_core.h:61
Definition of a feature class or feature layer.
Definition: ogr_feature.h:517
A simple feature, including geometry and attributes.
Definition: ogr_feature.h:893
Abstract base class for all geometry classes.
Definition: ogr_geometry.h:377
This class represents a layer of simple features, with access methods.
Definition: ogrsf_frmts.h:74
virtual void SetSpatialFilter(OGRGeometry *)
Set a new spatial filter.
Definition: ogrlayer.cpp:1487
virtual GIntBig GetFeatureCount(int bForce=TRUE)
Fetch the feature count in this layer.
Definition: ogrlayer.cpp:173
virtual OGRErr GetExtent(OGREnvelope *psExtent, int bForce=TRUE)
Fetch the extent of this layer.
Definition: ogrlayer.cpp:211
virtual OGRFeature * GetNextFeature()=0
Fetch the next available feature from this layer.
virtual OGRFeatureDefn * GetLayerDefn()=0
Fetch the schema information for this layer.
virtual void ResetReading()=0
Reset feature reading to start on the first feature.
virtual OGRErr SetNextByIndex(GIntBig nIndex)
Move read cursor to the nIndex'th feature in the current resultset.
Definition: ogrlayer.cpp:598
virtual OGRErr SetAttributeFilter(const char *)
Set a new attribute query.
Definition: ogrlayer.cpp:436
virtual OGRFeature * GetFeature(GIntBig nFID)
Fetch a feature by its identifier.
Definition: ogrlayer.cpp:544
virtual OGRGeometry * GetSpatialFilter()
This method returns the current spatial filter for this layer.
Definition: ogrlayer.cpp:1418
virtual int TestCapability(const char *)=0
Test if this layer supported the named capability.
Hash set implementation.
struct _CPLHashSet CPLHashSet
Opaque type for a hash set.
Definition: cpl_hash_set.h:52
#define CPL_DISALLOW_COPY_ASSIGN(ClassName)
Helper to remove the copy and assignment constructors so that the compiler will not generate the defa...
Definition: cpl_port.h:1042
long long GIntBig
Large signed integer type (generally 64-bit integer type).
Definition: cpl_port.h:215
Various convenience functions for working with strings and string lists.
int OGRErr
Type for a OGR error.
Definition: ogr_core.h:387
Classes related to registration of format support, and opening datasets.
OGRFeature field attribute value union.
Definition: ogr_core.h:910