Line data Source code
1 : /******************************************************************************
2 : * $Id$
3 : *
4 : * Project: OpenGIS Simple Features Reference Implementation
5 : * Purpose: Private definitions for OGR/ODBC driver.
6 : * Author: Frank Warmerdam, warmerdam@pobox.com
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 2003, Frank Warmerdam <warmerdam@pobox.com>
10 : *
11 : * Permission is hereby granted, free of charge, to any person obtaining a
12 : * copy of this software and associated documentation files (the "Software"),
13 : * to deal in the Software without restriction, including without limitation
14 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 : * and/or sell copies of the Software, and to permit persons to whom the
16 : * Software is furnished to do so, subject to the following conditions:
17 : *
18 : * The above copyright notice and this permission notice shall be included
19 : * in all copies or substantial portions of the Software.
20 : *
21 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 : * DEALINGS IN THE SOFTWARE.
28 : ****************************************************************************/
29 :
30 : #ifndef OGR_ODBC_H_INCLUDED
31 : #define OGR_ODBC_H_INCLUDED
32 :
33 : #include "ogrsf_frmts.h"
34 : #include "cpl_odbc.h"
35 : #include "cpl_error.h"
36 :
37 : #include <map>
38 : #include <unordered_set>
39 :
40 : /************************************************************************/
41 : /* OGRODBCLayer */
42 : /************************************************************************/
43 :
44 : class OGRODBCDataSource;
45 :
46 : class OGRODBCLayer CPL_NON_FINAL : public OGRLayer
47 : {
48 : protected:
49 : OGRFeatureDefn *poFeatureDefn;
50 :
51 : int m_nStatementFlags = 0;
52 : CPLODBCStatement *poStmt;
53 :
54 : // Layer spatial reference system, and srid.
55 : OGRSpatialReference *poSRS;
56 : int nSRSId;
57 :
58 : GIntBig iNextShapeId;
59 :
60 : OGRODBCDataSource *poDS;
61 :
62 : int bGeomColumnWKB;
63 : char *pszGeomColumn;
64 : char *pszFIDColumn;
65 :
66 : int *panFieldOrdinals;
67 :
68 : bool m_bEOF = false;
69 :
70 : CPLErr BuildFeatureDefn(const char *pszLayerName, CPLODBCStatement *poStmt);
71 :
72 0 : virtual CPLODBCStatement *GetStatement()
73 : {
74 0 : return poStmt;
75 : }
76 :
77 : public:
78 : OGRODBCLayer();
79 : virtual ~OGRODBCLayer();
80 :
81 : virtual void ResetReading() override;
82 : virtual OGRFeature *GetNextRawFeature();
83 : virtual OGRFeature *GetNextFeature() override;
84 :
85 : virtual OGRFeature *GetFeature(GIntBig nFeatureId) override;
86 :
87 0 : OGRFeatureDefn *GetLayerDefn() override
88 : {
89 0 : return poFeatureDefn;
90 : }
91 :
92 : virtual OGRSpatialReference *GetSpatialRef() override;
93 :
94 : virtual int TestCapability(const char *) override;
95 : };
96 :
97 : /************************************************************************/
98 : /* OGRODBCTableLayer */
99 : /************************************************************************/
100 :
101 : class OGRODBCTableLayer final : public OGRODBCLayer
102 : {
103 : char *pszQuery;
104 :
105 : int bHaveSpatialExtents;
106 :
107 : void ClearStatement();
108 : OGRErr ResetStatement();
109 :
110 : virtual CPLODBCStatement *GetStatement() override;
111 :
112 : char *pszTableName;
113 : char *pszSchemaName;
114 :
115 : public:
116 : explicit OGRODBCTableLayer(OGRODBCDataSource *, int);
117 : virtual ~OGRODBCTableLayer();
118 :
119 : CPLErr Initialize(const char *pszTableName, const char *pszGeomCol);
120 :
121 : virtual void ResetReading() override;
122 : virtual GIntBig GetFeatureCount(int) override;
123 :
124 : virtual OGRErr SetAttributeFilter(const char *) override;
125 : #ifdef notdef
126 : virtual OGRErr ISetFeature(OGRFeature *poFeature);
127 : virtual OGRErr ICreateFeature(OGRFeature *poFeature);
128 :
129 : virtual OGRErr CreateField(OGRFieldDefn *poField, int bApproxOK = TRUE);
130 : #endif
131 : virtual OGRFeature *GetFeature(GIntBig nFeatureId) override;
132 :
133 : virtual OGRSpatialReference *GetSpatialRef() override;
134 :
135 : virtual int TestCapability(const char *) override;
136 :
137 : #ifdef notdef
138 : // follow methods are not base class overrides
139 : void SetLaunderFlag(int bFlag)
140 : {
141 : bLaunderColumnNames = bFlag;
142 : }
143 :
144 : void SetPrecisionFlag(int bFlag)
145 : {
146 : bPreservePrecision = bFlag;
147 : }
148 : #endif
149 : };
150 :
151 : /************************************************************************/
152 : /* OGRODBCSelectLayer */
153 : /************************************************************************/
154 :
155 : class OGRODBCSelectLayer final : public OGRODBCLayer
156 : {
157 : char *pszBaseStatement;
158 :
159 : void ClearStatement();
160 : OGRErr ResetStatement();
161 :
162 : virtual CPLODBCStatement *GetStatement() override;
163 :
164 : public:
165 : OGRODBCSelectLayer(OGRODBCDataSource *, CPLODBCStatement *);
166 : virtual ~OGRODBCSelectLayer();
167 :
168 : virtual void ResetReading() override;
169 : virtual GIntBig GetFeatureCount(int) override;
170 :
171 : virtual OGRFeature *GetFeature(GIntBig nFeatureId) override;
172 :
173 : virtual OGRErr GetExtent(OGREnvelope *psExtent, int bForce = TRUE) override;
174 :
175 0 : virtual OGRErr GetExtent(int iGeomField, OGREnvelope *psExtent,
176 : int bForce) override
177 : {
178 0 : return OGRLayer::GetExtent(iGeomField, psExtent, bForce);
179 : }
180 :
181 : virtual int TestCapability(const char *) override;
182 : };
183 :
184 : /************************************************************************/
185 : /* OGRODBCDataSource */
186 : /************************************************************************/
187 :
188 : class OGRODBCDataSource final : public OGRDataSource
189 : {
190 : OGRODBCLayer **papoLayers;
191 : int nLayers;
192 :
193 : char *pszName;
194 :
195 : CPLODBCSession oSession;
196 :
197 : #if 0
198 : // NOTE: nothing uses the SRS cache currently. Hence disabled.
199 :
200 : // We maintain a list of known SRID to reduce the number of trips to
201 : // the database to get SRSes.
202 : std::map<int,
203 : std::unique_ptr<OGRSpatialReference, OGRSpatialReferenceReleaser>>
204 : m_oSRSCache{};
205 : #endif
206 :
207 : // set of all lowercase table names. Note that this is only used when
208 : // opening MDB datasources, not generic ODBC ones.
209 : std::unordered_set<std::string> m_aosAllLCTableNames;
210 :
211 : int m_nStatementFlags = 0;
212 :
213 : int OpenMDB(GDALOpenInfo *poOpenInfo);
214 : static bool IsPrivateLayerName(const CPLString &osName);
215 :
216 : public:
217 : OGRODBCDataSource();
218 : virtual ~OGRODBCDataSource();
219 :
220 : int Open(GDALOpenInfo *poOpenInfo);
221 : int OpenTable(const char *pszTableName, const char *pszGeomCol);
222 :
223 0 : const char *GetName() override
224 : {
225 0 : return pszName;
226 : }
227 :
228 0 : int GetLayerCount() override
229 : {
230 0 : return nLayers;
231 : }
232 :
233 : OGRLayer *GetLayer(int) override;
234 : OGRLayer *GetLayerByName(const char *) override;
235 : bool IsLayerPrivate(int) const override;
236 :
237 : int TestCapability(const char *) override;
238 :
239 : virtual OGRLayer *ExecuteSQL(const char *pszSQLCommand,
240 : OGRGeometry *poSpatialFilter,
241 : const char *pszDialect) override;
242 : virtual void ReleaseResultSet(OGRLayer *poLayer) override;
243 :
244 : // Internal use
245 0 : CPLODBCSession *GetSession()
246 : {
247 0 : return &oSession;
248 : }
249 : };
250 :
251 : #endif /* ndef OGR_ODBC_H_INCLUDED */
|