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