Line data Source code
1 : /******************************************************************************
2 : * $Id$
3 : *
4 : * Project: CARTO Translator
5 : * Purpose: Definition of classes for OGR Carto driver.
6 : * Author: Even Rouault, even dot rouault at spatialys.com
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 2013, Even Rouault <even dot rouault at spatialys.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_CARTO_H_INCLUDED
31 : #define OGR_CARTO_H_INCLUDED
32 :
33 : #include "ogrsf_frmts.h"
34 :
35 : #include "cpl_http.h"
36 : #include "cpl_json_header.h"
37 :
38 : #include <vector>
39 :
40 : json_object *OGRCARTOGetSingleRow(json_object *poObj);
41 : CPLString OGRCARTOEscapeIdentifier(const char *pszStr);
42 : CPLString OGRCARTOEscapeLiteral(const char *pszStr);
43 : CPLString OGRCARTOEscapeLiteralCopy(const char *pszStr);
44 :
45 : /************************************************************************/
46 : /* OGRCartoGeomFieldDefn */
47 : /************************************************************************/
48 :
49 : class OGRCartoGeomFieldDefn final : public OGRGeomFieldDefn
50 : {
51 : public:
52 : int nSRID;
53 :
54 13 : OGRCartoGeomFieldDefn(const char *pszNameIn, OGRwkbGeometryType eType)
55 13 : : OGRGeomFieldDefn(pszNameIn, eType), nSRID(0)
56 : {
57 13 : }
58 : };
59 :
60 : /************************************************************************/
61 : /* OGRCARTOLayer */
62 : /************************************************************************/
63 : class OGRCARTODataSource;
64 :
65 : class OGRCARTOLayer CPL_NON_FINAL : public OGRLayer
66 : {
67 : protected:
68 : OGRCARTODataSource *poDS;
69 :
70 : OGRFeatureDefn *poFeatureDefn;
71 : CPLString osBaseSQL;
72 : CPLString osFIDColName;
73 :
74 : bool bEOF;
75 : int nFetchedObjects;
76 : int iNextInFetchedObjects;
77 : GIntBig m_nNextFID;
78 : GIntBig m_nNextOffset;
79 : json_object *poCachedObj;
80 :
81 : virtual OGRFeature *GetNextRawFeature();
82 : OGRFeature *BuildFeature(json_object *poRowObj);
83 :
84 : void EstablishLayerDefn(const char *pszLayerName, json_object *poObjIn);
85 : OGRSpatialReference *GetSRS(const char *pszGeomCol, int *pnSRID);
86 : virtual CPLString GetSRS_SQL(const char *pszGeomCol) = 0;
87 :
88 : public:
89 : explicit OGRCARTOLayer(OGRCARTODataSource *poDS);
90 : virtual ~OGRCARTOLayer();
91 :
92 : virtual void ResetReading() override;
93 : virtual OGRFeature *GetNextFeature() override;
94 :
95 : virtual OGRFeatureDefn *GetLayerDefn() override;
96 : virtual OGRFeatureDefn *GetLayerDefnInternal(json_object *poObjIn) = 0;
97 : virtual json_object *FetchNewFeatures();
98 :
99 0 : virtual const char *GetFIDColumn() override
100 : {
101 0 : return osFIDColName.c_str();
102 : }
103 :
104 : virtual int TestCapability(const char *) override;
105 :
106 : GDALDataset *GetDataset() override;
107 :
108 132 : static int GetFeaturesToFetch()
109 : {
110 132 : return atoi(CPLGetConfigOption(
111 132 : "CARTO_PAGE_SIZE", CPLGetConfigOption("CARTODB_PAGE_SIZE", "500")));
112 : }
113 : };
114 :
115 : typedef enum
116 : {
117 : INSERT_UNINIT,
118 : INSERT_SINGLE_FEATURE,
119 : INSERT_MULTIPLE_FEATURE
120 : } InsertState;
121 :
122 : /************************************************************************/
123 : /* OGRCARTOTableLayer */
124 : /************************************************************************/
125 :
126 : class OGRCARTOTableLayer final : public OGRCARTOLayer
127 : {
128 : CPLString osName;
129 : CPLString osQuery;
130 : CPLString osWHERE;
131 : CPLString osSELECTWithoutWHERE;
132 :
133 : bool bLaunderColumnNames;
134 :
135 : bool bInDeferredInsert;
136 : bool bCopyMode;
137 : InsertState eDeferredInsertState;
138 : CPLString osDeferredBuffer;
139 : CPLString osCopySQL;
140 : GIntBig m_nNextFIDWrite;
141 :
142 : bool bDeferredCreation;
143 : bool bCartodbfy;
144 : int nMaxChunkSize;
145 :
146 : bool bDropOnCreation;
147 :
148 : void BuildWhere();
149 : std::vector<bool> m_abFieldSetForInsert;
150 :
151 : virtual CPLString GetSRS_SQL(const char *pszGeomCol) override;
152 :
153 : public:
154 : OGRCARTOTableLayer(OGRCARTODataSource *poDS, const char *pszName);
155 : virtual ~OGRCARTOTableLayer();
156 :
157 8 : virtual const char *GetName() override
158 : {
159 8 : return osName.c_str();
160 : }
161 :
162 : virtual OGRFeatureDefn *GetLayerDefnInternal(json_object *poObjIn) override;
163 : virtual json_object *FetchNewFeatures() override;
164 :
165 : virtual GIntBig GetFeatureCount(int bForce = TRUE) override;
166 : virtual OGRFeature *GetFeature(GIntBig nFeatureId) override;
167 :
168 : virtual int TestCapability(const char *) override;
169 :
170 : virtual OGRErr CreateGeomField(const OGRGeomFieldDefn *poGeomFieldIn,
171 : int bApproxOK = TRUE) override;
172 :
173 : virtual OGRErr CreateField(const OGRFieldDefn *poField,
174 : int bApproxOK = TRUE) override;
175 :
176 : virtual OGRErr DeleteField(int iField) override;
177 :
178 : virtual OGRFeature *GetNextRawFeature() override;
179 :
180 : virtual OGRErr ICreateFeature(OGRFeature *poFeature) override;
181 : virtual OGRErr ISetFeature(OGRFeature *poFeature) override;
182 : virtual OGRErr DeleteFeature(GIntBig nFID) override;
183 :
184 1 : virtual void SetSpatialFilter(OGRGeometry *poGeom) override
185 : {
186 1 : SetSpatialFilter(0, poGeom);
187 1 : }
188 :
189 : virtual void SetSpatialFilter(int iGeomField, OGRGeometry *poGeom) override;
190 : virtual OGRErr SetAttributeFilter(const char *) override;
191 :
192 0 : virtual OGRErr GetExtent(OGREnvelope *psExtent, int bForce) override
193 : {
194 0 : return GetExtent(0, psExtent, bForce);
195 : }
196 :
197 : virtual OGRErr GetExtent(int iGeomField, OGREnvelope *psExtent,
198 : int bForce) override;
199 :
200 4 : void SetLaunderFlag(bool bFlag)
201 : {
202 4 : bLaunderColumnNames = bFlag;
203 4 : }
204 :
205 : void SetDeferredCreation(OGRwkbGeometryType eGType,
206 : OGRSpatialReference *poSRS, bool bGeomNullable,
207 : bool bCartodbfy);
208 : OGRErr RunDeferredCreationIfNecessary();
209 :
210 4 : bool GetDeferredCreation() const
211 : {
212 4 : return bDeferredCreation;
213 : }
214 :
215 4 : void CancelDeferredCreation()
216 : {
217 4 : bDeferredCreation = false;
218 4 : bCartodbfy = false;
219 4 : }
220 :
221 : OGRErr FlushDeferredBuffer(bool bReset = true);
222 : void RunDeferredCartofy();
223 :
224 : OGRErr FlushDeferredInsert(bool bReset = true);
225 : OGRErr FlushDeferredCopy(bool bReset = true);
226 : OGRErr ICreateFeatureInsert(OGRFeature *poFeature,
227 : bool bHasUserFieldMatchingFID,
228 : bool bHasJustGotNextFID);
229 : OGRErr ICreateFeatureCopy(OGRFeature *poFeature,
230 : bool bHasUserFieldMatchingFID,
231 : bool bHasJustGotNextFID);
232 : char *OGRCARTOGetHexGeometry(OGRGeometry *poGeom, int i);
233 :
234 4 : void SetDropOnCreation(bool bFlag)
235 : {
236 4 : bDropOnCreation = bFlag;
237 4 : }
238 :
239 4 : bool GetDropOnCreation() const
240 : {
241 4 : return bDropOnCreation;
242 : }
243 : };
244 :
245 : /************************************************************************/
246 : /* OGRCARTOResultLayer */
247 : /************************************************************************/
248 :
249 : class OGRCARTOResultLayer final : public OGRCARTOLayer
250 : {
251 : OGRFeature *poFirstFeature;
252 :
253 : virtual CPLString GetSRS_SQL(const char *pszGeomCol) override;
254 :
255 : public:
256 : OGRCARTOResultLayer(OGRCARTODataSource *poDS, const char *pszRawStatement);
257 : virtual ~OGRCARTOResultLayer();
258 :
259 : virtual OGRFeatureDefn *GetLayerDefnInternal(json_object *poObjIn) override;
260 : virtual OGRFeature *GetNextRawFeature() override;
261 :
262 : bool IsOK();
263 : };
264 :
265 : /************************************************************************/
266 : /* OGRCARTODataSource */
267 : /************************************************************************/
268 :
269 : class OGRCARTODataSource final : public OGRDataSource
270 : {
271 : char *pszName;
272 : char *pszAccount;
273 :
274 : OGRCARTOTableLayer **papoLayers;
275 : int nLayers;
276 :
277 : bool bReadWrite;
278 : bool bBatchInsert;
279 : bool bCopyMode;
280 :
281 : bool bUseHTTPS;
282 :
283 : CPLString osAPIKey;
284 :
285 : bool bMustCleanPersistent;
286 :
287 : CPLString osCurrentSchema;
288 :
289 : int bHasOGRMetadataFunction;
290 :
291 : int nPostGISMajor;
292 : int nPostGISMinor;
293 :
294 : public:
295 : OGRCARTODataSource();
296 : virtual ~OGRCARTODataSource();
297 :
298 : int Open(const char *pszFilename, char **papszOpenOptions, int bUpdate);
299 :
300 0 : virtual const char *GetName() override
301 : {
302 0 : return pszName;
303 : }
304 :
305 5 : virtual int GetLayerCount() override
306 : {
307 5 : return nLayers;
308 : }
309 :
310 : virtual OGRLayer *GetLayer(int) override;
311 : virtual OGRLayer *GetLayerByName(const char *) override;
312 :
313 : virtual int TestCapability(const char *) override;
314 :
315 : OGRLayer *ICreateLayer(const char *pszName,
316 : const OGRGeomFieldDefn *poGeomFieldDefn,
317 : CSLConstList papszOptions) override;
318 : virtual OGRErr DeleteLayer(int) override;
319 :
320 : virtual OGRLayer *ExecuteSQL(const char *pszSQLCommand,
321 : OGRGeometry *poSpatialFilter,
322 : const char *pszDialect) override;
323 : virtual void ReleaseResultSet(OGRLayer *poLayer) override;
324 :
325 : const char *GetAPIURL() const;
326 :
327 34 : bool IsReadWrite() const
328 : {
329 34 : return bReadWrite;
330 : }
331 :
332 22 : bool DoBatchInsert() const
333 : {
334 22 : return bBatchInsert;
335 : }
336 :
337 22 : bool DoCopyMode() const
338 : {
339 22 : return bCopyMode;
340 : }
341 :
342 : char **AddHTTPOptions();
343 : json_object *RunSQL(const char *pszUnescapedSQL);
344 : json_object *RunCopyFrom(const char *pszSQL, const char *pszCopyFile);
345 :
346 16 : const CPLString &GetCurrentSchema()
347 : {
348 16 : return osCurrentSchema;
349 : }
350 :
351 : static int FetchSRSId(const OGRSpatialReference *poSRS);
352 :
353 45 : int IsAuthenticatedConnection()
354 : {
355 45 : return !osAPIKey.empty();
356 : }
357 :
358 9 : int HasOGRMetadataFunction()
359 : {
360 9 : return bHasOGRMetadataFunction;
361 : }
362 :
363 3 : void SetOGRMetadataFunction(int bFlag)
364 : {
365 3 : bHasOGRMetadataFunction = bFlag;
366 3 : }
367 :
368 : OGRLayer *ExecuteSQLInternal(const char *pszSQLCommand,
369 : OGRGeometry *poSpatialFilter = nullptr,
370 : const char *pszDialect = nullptr,
371 : bool bRunDeferredActions = false);
372 :
373 7 : int GetPostGISMajor() const
374 : {
375 7 : return nPostGISMajor;
376 : }
377 :
378 7 : int GetPostGISMinor() const
379 : {
380 7 : return nPostGISMinor;
381 : }
382 : };
383 :
384 : #endif /* ndef OGR_CARTO_H_INCLUDED */
|