1 : /******************************************************************************
2 : * $Id: ogr_sqlite.h 23589 2011-12-17 14:21:01Z rouault $
3 : *
4 : * Project: OpenGIS Simple Features Reference Implementation
5 : * Purpose: Private definitions for OGR/SQLite driver.
6 : * Author: Frank Warmerdam, warmerdam@pobox.com
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 2004, 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_SQLITE_H_INCLUDED
31 : #define _OGR_SQLITE_H_INCLUDED
32 :
33 : #include "ogrsf_frmts.h"
34 : #include "cpl_error.h"
35 :
36 : #ifdef HAVE_SPATIALITE
37 : #ifdef SPATIALITE_AMALGAMATION
38 : /*
39 : / using an AMALGAMATED version of SpatiaLite
40 : / a private internal copy of SQLite is included:
41 : / so we are required including the SpatiaLite's
42 : / own header
43 : /
44 : / IMPORTANT NOTICE: using AMALAGATION is only
45 : / useful on Windows (to skip DLL hell related oddities)
46 : */
47 : #include <spatialite/sqlite3.h>
48 : #else
49 : /*
50 : / You MUST NOT use AMALGAMATION on Linux or any
51 : / other "sane" operating system !!!!
52 : */
53 : #include "sqlite3.h"
54 : #endif
55 : #else
56 : #include "sqlite3.h"
57 : #endif
58 :
59 : #if SQLITE_VERSION_NUMBER >= 3006000
60 : #define HAVE_SQLITE_VFS
61 : #endif
62 :
63 : /************************************************************************/
64 : /* Format used to store geometry data in the database. */
65 : /************************************************************************/
66 :
67 : enum OGRSQLiteGeomFormat
68 : {
69 : OSGF_None = 0,
70 : OSGF_WKT = 1,
71 : OSGF_WKB = 2,
72 : OSGF_FGF = 3,
73 : OSGF_SpatiaLite = 4
74 : };
75 :
76 : /************************************************************************/
77 : /* SpatiaLite's own Geometry type IDs. */
78 : /************************************************************************/
79 :
80 : enum OGRSpatialiteGeomType
81 : {
82 : // 2D [XY]
83 : OGRSplitePointXY = 1,
84 : OGRSpliteLineStringXY = 2,
85 : OGRSplitePolygonXY = 3,
86 : OGRSpliteMultiPointXY = 4,
87 : OGRSpliteMultiLineStringXY = 5,
88 : OGRSpliteMultiPolygonXY = 6,
89 : OGRSpliteGeometryCollectionXY = 7,
90 : // 3D [XYZ]
91 : OGRSplitePointXYZ = 1001,
92 : OGRSpliteLineStringXYZ = 1002,
93 : OGRSplitePolygonXYZ = 1003,
94 : OGRSpliteMultiPointXYZ = 1004,
95 : OGRSpliteMultiLineStringXYZ = 1005,
96 : OGRSpliteMultiPolygonXYZ = 1006,
97 : OGRSpliteGeometryCollectionXYZ = 1007,
98 : // 2D with Measure [XYM]
99 : OGRSplitePointXYM = 2001,
100 : OGRSpliteLineStringXYM = 2002,
101 : OGRSplitePolygonXYM = 2003,
102 : OGRSpliteMultiPointXYM = 2004,
103 : OGRSpliteMultiLineStringXYM = 2005,
104 : OGRSpliteMultiPolygonXYM = 2006,
105 : OGRSpliteGeometryCollectionXYM = 2007,
106 : // 3D with Measure [XYZM]
107 : OGRSplitePointXYZM = 3001,
108 : OGRSpliteLineStringXYZM = 3002,
109 : OGRSplitePolygonXYZM = 3003,
110 : OGRSpliteMultiPointXYZM = 3004,
111 : OGRSpliteMultiLineStringXYZM = 3005,
112 : OGRSpliteMultiPolygonXYZM = 3006,
113 : OGRSpliteGeometryCollectionXYZM = 3007,
114 : // COMPRESSED: 2D [XY]
115 : OGRSpliteComprLineStringXY = 1000002,
116 : OGRSpliteComprPolygonXY = 1000003,
117 : OGRSpliteComprMultiPointXY = 1000004,
118 : OGRSpliteComprMultiLineStringXY = 1000005,
119 : OGRSpliteComprMultiPolygonXY = 1000006,
120 : OGRSpliteComprGeometryCollectionXY = 1000007,
121 : // COMPRESSED: 3D [XYZ]
122 : OGRSpliteComprLineStringXYZ = 1001002,
123 : OGRSpliteComprPolygonXYZ = 1001003,
124 : OGRSpliteComprMultiPointXYZ = 1001004,
125 : OGRSpliteComprMultiLineStringXYZ = 1001005,
126 : OGRSpliteComprMultiPolygonXYZ = 1001006,
127 : OGRSpliteComprGeometryCollectionXYZ = 1001007,
128 : // COMPRESSED: 2D with Measure [XYM]
129 : OGRSpliteComprLineStringXYM = 1002002,
130 : OGRSpliteComprPolygonXYM = 1002003,
131 : OGRSpliteComprMultiPointXYM = 1002004,
132 : OGRSpliteComprMultiLineStringXYM = 1002005,
133 : OGRSpliteComprMultiPolygonXYM = 1002006,
134 : OGRSpliteComprGeometryCollectionXYM = 1002007,
135 : // COMPRESSED: 3D with Measure [XYZM]
136 : OGRSpliteComprLineStringXYZM = 1003002,
137 : OGRSpliteComprPolygonXYZM = 1003003,
138 : OGRSpliteComprMultiPointXYZM = 1003004,
139 : OGRSpliteComprMultiLineStringXYZM = 1003005,
140 : OGRSpliteComprMultiPolygonXYZM = 1003006,
141 : OGRSpliteComprGeometryCollectionXYZM = 1003007
142 : };
143 :
144 : /************************************************************************/
145 : /* OGRSQLiteLayer */
146 : /************************************************************************/
147 :
148 : class OGRSQLiteDataSource;
149 :
150 : class OGRSQLiteLayer : public OGRLayer
151 : {
152 : private:
153 : static OGRErr createFromSpatialiteInternal(const GByte *pabyData,
154 : OGRGeometry **ppoReturn,
155 : int nBytes,
156 : OGRwkbByteOrder eByteOrder,
157 : int* pnBytesConsumed,
158 : int nRecLevel);
159 :
160 : static int CanBeCompressedSpatialiteGeometry(const OGRGeometry *poGeometry);
161 :
162 : static int ComputeSpatiaLiteGeometrySize(const OGRGeometry *poGeometry,
163 : int bHasM, int bSpatialite2D,
164 : int bUseComprGeom );
165 :
166 : static int GetSpatialiteGeometryCode(const OGRGeometry *poGeometry,
167 : int bHasM, int bSpatialite2D,
168 : int bUseComprGeom,
169 : int bAcceptMultiGeom);
170 :
171 : static int ExportSpatiaLiteGeometryInternal(const OGRGeometry *poGeometry,
172 : OGRwkbByteOrder eByteOrder,
173 : int bHasM, int bSpatialite2D,
174 : int bUseComprGeom,
175 : GByte* pabyData );
176 :
177 : protected:
178 : OGRFeatureDefn *poFeatureDefn;
179 :
180 : // Layer spatial reference system, and srid.
181 : OGRSpatialReference *poSRS;
182 : int nSRSId;
183 :
184 : int iNextShapeId;
185 :
186 : sqlite3_stmt *hStmt;
187 :
188 : OGRSQLiteDataSource *poDS;
189 :
190 : int bTriedAsSpatiaLite;
191 : CPLString osGeomColumn;
192 : OGRSQLiteGeomFormat eGeomFormat;
193 :
194 : char *pszFIDColumn;
195 :
196 : int *panFieldOrdinals;
197 : int bHasSpatialIndex;
198 : int bHasM;
199 : int bSpatialiteReadOnly;
200 : int bSpatialiteLoaded;
201 : int iSpatialiteVersion;
202 :
203 : int bIsVirtualShape;
204 :
205 : CPLErr BuildFeatureDefn( const char *pszLayerName,
206 : sqlite3_stmt *hStmt );
207 :
208 : void ClearStatement();
209 : virtual OGRErr ResetStatement() = 0;
210 :
211 : static OGRErr ImportSpatiaLiteGeometry( const GByte *, int,
212 : OGRGeometry ** );
213 : static OGRErr ExportSpatiaLiteGeometry( const OGRGeometry *,
214 : GInt32, OGRwkbByteOrder,
215 : int, int, int bUseComprGeom, GByte **, int * );
216 :
217 : int bUseComprGeom;
218 :
219 : public:
220 : OGRSQLiteLayer();
221 : virtual ~OGRSQLiteLayer();
222 :
223 : virtual void ResetReading();
224 : virtual OGRFeature *GetNextRawFeature();
225 : virtual OGRFeature *GetNextFeature();
226 :
227 : virtual OGRFeature *GetFeature( long nFeatureId );
228 :
229 3143 : OGRFeatureDefn * GetLayerDefn() { return poFeatureDefn; }
230 :
231 : virtual OGRSpatialReference *GetSpatialRef();
232 :
233 : virtual const char *GetFIDColumn();
234 : virtual const char *GetGeometryColumn();
235 :
236 : virtual int TestCapability( const char * );
237 :
238 : virtual OGRErr StartTransaction();
239 : virtual OGRErr CommitTransaction();
240 : virtual OGRErr RollbackTransaction();
241 :
242 0 : virtual int IsTableLayer() { return FALSE; }
243 :
244 3 : int HasSpatialIndex() const { return bHasSpatialIndex; }
245 : };
246 :
247 : /************************************************************************/
248 : /* OGRSQLiteTableLayer */
249 : /************************************************************************/
250 :
251 : class OGRSQLiteTableLayer : public OGRSQLiteLayer
252 : {
253 : int bLaunderColumnNames;
254 : int bSpatialite2D;
255 :
256 : CPLString osWHERE;
257 : CPLString osQuery;
258 : int bHasCheckedSpatialIndexTable;
259 :
260 : char *pszEscapedTableName;
261 :
262 : void BuildWhere(void);
263 :
264 : OGRErr ResetStatement();
265 :
266 : OGRErr AddColumnAncientMethod( OGRFieldDefn& oField);
267 :
268 : void InitFieldListForRecrerate(char* & pszNewFieldList,
269 : char* & pszFieldListForSelect,
270 : int nExtraSpace = 0);
271 : OGRErr RecreateTable(const char* pszFieldListForSelect,
272 : const char* pszNewFieldList,
273 : const char* pszGenericErrorMessage);
274 : OGRErr BindValues( OGRFeature *poFeature,
275 : sqlite3_stmt* hStmt,
276 : int bBindNullValues );
277 :
278 : int CheckSpatialIndexTable();
279 :
280 : public:
281 : OGRSQLiteTableLayer( OGRSQLiteDataSource * );
282 : ~OGRSQLiteTableLayer();
283 :
284 : CPLErr Initialize( const char *pszTableName,
285 : const char *pszGeomCol,
286 : OGRwkbGeometryType eGeomType,
287 : const char *pszGeomFormat,
288 : OGRSpatialReference *poSRS,
289 : int nSRSId = -1,
290 : int bHasSpatialIndex = FALSE,
291 : int bHasM = FALSE,
292 : int bSpatialiteReadOnly = FALSE,
293 : int bSpatialiteLoaded = FALSE,
294 : int iSpatialiteVersion = -1,
295 : int bIsVirtualShapeIn = FALSE);
296 :
297 : virtual int GetFeatureCount( int );
298 : virtual OGRErr GetExtent(OGREnvelope *psExtent, int bForce);
299 :
300 : virtual void SetSpatialFilter( OGRGeometry * );
301 : virtual OGRErr SetAttributeFilter( const char * );
302 : virtual OGRErr SetFeature( OGRFeature *poFeature );
303 : virtual OGRErr CreateFeature( OGRFeature *poFeature );
304 :
305 : virtual OGRErr CreateField( OGRFieldDefn *poField,
306 : int bApproxOK = TRUE );
307 : virtual OGRErr DeleteField( int iField );
308 : virtual OGRErr ReorderFields( int* panMap );
309 : virtual OGRErr AlterFieldDefn( int iField, OGRFieldDefn* poNewFieldDefn, int nFlags );
310 :
311 : virtual OGRFeature *GetFeature( long nFeatureId );
312 :
313 : virtual int TestCapability( const char * );
314 :
315 : // follow methods are not base class overrides
316 80 : void SetLaunderFlag( int bFlag )
317 80 : { bLaunderColumnNames = bFlag; }
318 1020 : void SetSpatialite2D( int bFlag )
319 1020 : { bSpatialite2D = bFlag; }
320 32 : void SetUseCompressGeom( int bFlag )
321 32 : { bUseComprGeom = bFlag; }
322 :
323 3 : virtual int IsTableLayer() { return TRUE; }
324 : };
325 :
326 : /************************************************************************/
327 : /* OGRSQLiteViewLayer */
328 : /************************************************************************/
329 :
330 : class OGRSQLiteViewLayer : public OGRSQLiteLayer
331 : {
332 : CPLString osWHERE;
333 : CPLString osQuery;
334 : int bHasCheckedSpatialIndexTable;
335 :
336 : char *pszEscapedTableName;
337 : char *pszEscapedUnderlyingTableName;
338 :
339 : CPLString osUnderlyingTableName;
340 : CPLString osUnderlyingGeometryColumn;
341 :
342 : void BuildWhere(void);
343 :
344 : OGRErr ResetStatement();
345 :
346 : public:
347 : OGRSQLiteViewLayer( OGRSQLiteDataSource * );
348 : ~OGRSQLiteViewLayer();
349 :
350 : CPLErr Initialize( const char *pszViewName,
351 : const char *pszViewGeometry,
352 : const char *pszViewRowid,
353 : const char *pszTableName,
354 : const char *pszGeometryColumn,
355 : int bSpatialiteLoaded);
356 :
357 : virtual int GetFeatureCount( int );
358 :
359 : virtual void SetSpatialFilter( OGRGeometry * );
360 : virtual OGRErr SetAttributeFilter( const char * );
361 :
362 : virtual OGRFeature *GetFeature( long nFeatureId );
363 :
364 : virtual int TestCapability( const char * );
365 : };
366 :
367 : /************************************************************************/
368 : /* OGRSQLiteSelectLayer */
369 : /************************************************************************/
370 :
371 : class OGRSQLiteSelectLayer : public OGRSQLiteLayer
372 : {
373 : CPLString osSQL;
374 :
375 : OGRErr ResetStatement();
376 :
377 : public:
378 : OGRSQLiteSelectLayer( OGRSQLiteDataSource *,
379 : CPLString osSQL,
380 : sqlite3_stmt * );
381 : ~OGRSQLiteSelectLayer();
382 : };
383 :
384 : /************************************************************************/
385 : /* OGRSQLiteSingleFeatureLayer */
386 : /************************************************************************/
387 :
388 : class OGRSQLiteSingleFeatureLayer : public OGRLayer
389 : {
390 : private:
391 : int nVal;
392 : OGRFeatureDefn *poFeatureDefn;
393 : int iNextShapeId;
394 :
395 : public:
396 : OGRSQLiteSingleFeatureLayer( const char* pszLayerName,
397 : int nVal );
398 : ~OGRSQLiteSingleFeatureLayer();
399 :
400 : virtual void ResetReading();
401 : virtual OGRFeature *GetNextFeature();
402 : virtual OGRFeatureDefn *GetLayerDefn();
403 : virtual int TestCapability( const char * );
404 : };
405 :
406 : /************************************************************************/
407 : /* OGRSQLiteDataSource */
408 : /************************************************************************/
409 :
410 : class OGRSQLiteDataSource : public OGRDataSource
411 : {
412 : OGRSQLiteLayer **papoLayers;
413 : int nLayers;
414 :
415 : char *pszName;
416 :
417 : sqlite3 *hDB;
418 : int bUpdate;
419 :
420 : int nSoftTransactionLevel;
421 :
422 : // We maintain a list of known SRID to reduce the number of trips to
423 : // the database to get SRSes.
424 : int nKnownSRID;
425 : int *panSRID;
426 : OGRSpatialReference **papoSRS;
427 :
428 : int bHaveGeometryColumns;
429 : int bIsSpatiaLite;
430 :
431 : virtual void DeleteLayer( const char *pszLayer );
432 :
433 : int DetectSRSWktColumn();
434 :
435 : int OpenOrCreateDB(int flags);
436 : int InitWithEPSG();
437 : int SetSynchronous();
438 : int SetCacheSize();
439 :
440 : int OpenVirtualTable(const char* pszName, const char* pszSQL);
441 :
442 : #ifdef HAVE_SQLITE_VFS
443 : sqlite3_vfs* pMyVFS;
444 : #endif
445 :
446 : public:
447 : OGRSQLiteDataSource();
448 : ~OGRSQLiteDataSource();
449 :
450 : int Open( const char *, int bUpdateIn );
451 : int Create( const char *, char **papszOptions );
452 :
453 : int OpenTable( const char *pszTableName,
454 : const char *pszGeomCol = NULL,
455 : OGRwkbGeometryType eGeomType = wkbUnknown,
456 : const char *pszGeomFormat = NULL,
457 : OGRSpatialReference *poSRS = NULL,
458 : int nSRID = -1,
459 : int bHasSpatialIndex = FALSE,
460 : int bHasM = FALSE,
461 : int bSpatialiteReadOnly = FALSE,
462 : int bSpatialiteLoaded = FALSE,
463 : int iSpatialiteVersion = -1,
464 : int bForce2D = FALSE,
465 : int bIsVirtualShapeIn = FALSE );
466 : int OpenView( const char *pszViewName,
467 : const char *pszViewGeometry,
468 : const char *pszViewRowid,
469 : const char *pszTableName,
470 : const char *pszGeometryColumn,
471 : int bSpatialiteLoaded);
472 :
473 118 : const char *GetName() { return pszName; }
474 1479 : int GetLayerCount() { return nLayers; }
475 : OGRLayer *GetLayer( int );
476 :
477 : virtual OGRLayer *CreateLayer( const char *pszLayerName,
478 : OGRSpatialReference *poSRS,
479 : OGRwkbGeometryType eType,
480 : char **papszOptions );
481 : virtual OGRErr DeleteLayer(int);
482 :
483 : int TestCapability( const char * );
484 :
485 : virtual OGRLayer * ExecuteSQL( const char *pszSQLCommand,
486 : OGRGeometry *poSpatialFilter,
487 : const char *pszDialect );
488 : virtual void ReleaseResultSet( OGRLayer * poLayer );
489 :
490 : OGRErr SoftStartTransaction();
491 : OGRErr SoftCommit();
492 : OGRErr SoftRollback();
493 :
494 : OGRErr FlushSoftTransaction();
495 :
496 2321 : sqlite3 *GetDB() { return hDB; }
497 :
498 : char *LaunderName( const char * );
499 : int FetchSRSId( OGRSpatialReference * poSRS );
500 : OGRSpatialReference*FetchSRS( int nSRID );
501 :
502 355 : int GetUpdate() const { return bUpdate; }
503 :
504 : void SetName(const char* pszNameIn);
505 : };
506 :
507 : /************************************************************************/
508 : /* OGRSQLiteDriver */
509 : /************************************************************************/
510 :
511 : class OGRSQLiteDriver : public OGRSFDriver
512 178 : {
513 : public:
514 : ~OGRSQLiteDriver();
515 :
516 : const char *GetName();
517 : OGRDataSource *Open( const char *, int );
518 :
519 : virtual OGRDataSource *CreateDataSource( const char *pszName,
520 : char ** = NULL );
521 : virtual OGRErr DeleteDataSource( const char *pszName );
522 :
523 : int TestCapability( const char * );
524 : };
525 :
526 : CPLString OGRSQLiteEscape( const char *pszSrcName );
527 : int OGRSQLiteGetSpatialiteVersionNumber();
528 : #ifdef HAVE_SQLITE_VFS
529 : sqlite3_vfs* OGRSQLiteCreateVFS();
530 : #endif
531 :
532 : #endif /* ndef _OGR_SQLITE_H_INCLUDED */
533 :
534 :
|