1 : /******************************************************************************
2 : * $Id: ogr_sqlite.h 19800 2010-06-04 21:38:03Z 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 : /* When used with Spatialite amalgation, there might be no sqlite3 headers */
37 : /* in other places than /include/spatialite/ subdir */
38 : #ifdef HAVE_SPATIALITE
39 : #include <spatialite/sqlite3.h>
40 : #else
41 : #include "sqlite3.h"
42 : #endif
43 :
44 : /************************************************************************/
45 : /* Format used to store geometry data in the database. */
46 : /************************************************************************/
47 :
48 : enum OGRSQLiteGeomFormat
49 : {
50 : OSGF_None = 0,
51 : OSGF_WKT = 1,
52 : OSGF_WKB = 2,
53 : OSGF_FGF = 3,
54 : OSGF_SpatiaLite = 4
55 : };
56 :
57 : /************************************************************************/
58 : /* OGRSQLiteLayer */
59 : /************************************************************************/
60 :
61 : class OGRSQLiteDataSource;
62 :
63 : class OGRSQLiteLayer : public OGRLayer
64 : {
65 : private:
66 : static OGRErr createFromSpatialiteInternal(const GByte *pabyData,
67 : OGRGeometry **ppoReturn,
68 : int nBytes,
69 : OGRwkbByteOrder eByteOrder,
70 : int* pnBytesConsumed);
71 :
72 : static int ComputeSpatiaLiteGeometrySize(const OGRGeometry *poGeometry);
73 : static int ExportSpatiaLiteGeometryInternal(const OGRGeometry *poGeometry,
74 : OGRwkbByteOrder eByteOrder,
75 : GByte* pabyData);
76 :
77 : protected:
78 : OGRFeatureDefn *poFeatureDefn;
79 :
80 : // Layer spatial reference system, and srid.
81 : OGRSpatialReference *poSRS;
82 : int nSRSId;
83 :
84 : int iNextShapeId;
85 :
86 : sqlite3_stmt *hStmt;
87 :
88 : OGRSQLiteDataSource *poDS;
89 :
90 : int bTriedAsSpatiaLite;
91 : CPLString osGeomColumn;
92 : OGRSQLiteGeomFormat eGeomFormat;
93 :
94 : char *pszFIDColumn;
95 :
96 : int *panFieldOrdinals;
97 : int bHasSpatialIndex;
98 :
99 : CPLErr BuildFeatureDefn( const char *pszLayerName,
100 : sqlite3_stmt *hStmt );
101 :
102 : void ClearStatement();
103 : virtual OGRErr ResetStatement() = 0;
104 :
105 : static OGRErr ImportSpatiaLiteGeometry( const GByte *, int,
106 : OGRGeometry ** );
107 : static OGRErr ExportSpatiaLiteGeometry( const OGRGeometry *,
108 : GInt32, OGRwkbByteOrder,
109 : GByte **, int * );
110 :
111 : public:
112 : OGRSQLiteLayer();
113 : virtual ~OGRSQLiteLayer();
114 :
115 : virtual void ResetReading();
116 : virtual OGRFeature *GetNextRawFeature();
117 : virtual OGRFeature *GetNextFeature();
118 :
119 : virtual OGRFeature *GetFeature( long nFeatureId );
120 :
121 1492 : OGRFeatureDefn * GetLayerDefn() { return poFeatureDefn; }
122 :
123 : virtual OGRSpatialReference *GetSpatialRef();
124 :
125 : virtual const char *GetFIDColumn();
126 : virtual const char *GetGeometryColumn();
127 :
128 : virtual int TestCapability( const char * );
129 :
130 : virtual OGRErr StartTransaction();
131 : virtual OGRErr CommitTransaction();
132 : virtual OGRErr RollbackTransaction();
133 : };
134 :
135 : /************************************************************************/
136 : /* OGRSQLiteTableLayer */
137 : /************************************************************************/
138 :
139 : class OGRSQLiteTableLayer : public OGRSQLiteLayer
140 : {
141 : int bUpdateAccess;
142 : int bLaunderColumnNames;
143 :
144 : CPLString osWHERE;
145 : CPLString osQuery;
146 :
147 : void BuildWhere(void);
148 :
149 : OGRErr ResetStatement();
150 :
151 : public:
152 : OGRSQLiteTableLayer( OGRSQLiteDataSource * );
153 : ~OGRSQLiteTableLayer();
154 :
155 : CPLErr Initialize( const char *pszTableName,
156 : const char *pszGeomCol,
157 : OGRwkbGeometryType eGeomType,
158 : const char *pszGeomFormat,
159 : OGRSpatialReference *poSRS,
160 : int nSRSId = -1,
161 : int bHasSpatialIndex = FALSE);
162 :
163 : virtual int GetFeatureCount( int );
164 :
165 : virtual void SetSpatialFilter( OGRGeometry * );
166 : virtual OGRErr SetAttributeFilter( const char * );
167 : virtual OGRErr SetFeature( OGRFeature *poFeature );
168 : virtual OGRErr CreateFeature( OGRFeature *poFeature );
169 :
170 : virtual OGRErr CreateField( OGRFieldDefn *poField,
171 : int bApproxOK = TRUE );
172 : virtual OGRFeature *GetFeature( long nFeatureId );
173 :
174 : virtual OGRSpatialReference *GetSpatialRef();
175 :
176 : virtual int TestCapability( const char * );
177 :
178 : // follow methods are not base class overrides
179 14 : void SetLaunderFlag( int bFlag )
180 14 : { bLaunderColumnNames = bFlag; }
181 : };
182 :
183 : /************************************************************************/
184 : /* OGRSQLiteSelectLayer */
185 : /************************************************************************/
186 :
187 : class OGRSQLiteSelectLayer : public OGRSQLiteLayer
188 : {
189 : CPLString osSQL;
190 :
191 : OGRErr ResetStatement();
192 :
193 : public:
194 : OGRSQLiteSelectLayer( OGRSQLiteDataSource *,
195 : CPLString osSQL,
196 : sqlite3_stmt * );
197 : ~OGRSQLiteSelectLayer();
198 : };
199 :
200 : /************************************************************************/
201 : /* OGRSQLiteSingleFeatureLayer */
202 : /************************************************************************/
203 :
204 : class OGRSQLiteSingleFeatureLayer : public OGRLayer
205 : {
206 : private:
207 : int nVal;
208 : OGRFeatureDefn *poFeatureDefn;
209 : int iNextShapeId;
210 :
211 : public:
212 : OGRSQLiteSingleFeatureLayer( const char* pszLayerName,
213 : int nVal );
214 : ~OGRSQLiteSingleFeatureLayer();
215 :
216 : virtual void ResetReading();
217 : virtual OGRFeature *GetNextFeature();
218 : virtual OGRFeatureDefn *GetLayerDefn();
219 : virtual int TestCapability( const char * );
220 : };
221 :
222 : /************************************************************************/
223 : /* OGRSQLiteDataSource */
224 : /************************************************************************/
225 :
226 : class OGRSQLiteDataSource : public OGRDataSource
227 : {
228 : OGRSQLiteLayer **papoLayers;
229 : int nLayers;
230 :
231 : char *pszName;
232 :
233 : sqlite3 *hDB;
234 :
235 : int nSoftTransactionLevel;
236 :
237 : // We maintain a list of known SRID to reduce the number of trips to
238 : // the database to get SRSes.
239 : int nKnownSRID;
240 : int *panSRID;
241 : OGRSpatialReference **papoSRS;
242 :
243 : int bHaveGeometryColumns;
244 : int bIsSpatiaLite;
245 :
246 : virtual void DeleteLayer( const char *pszLayer );
247 :
248 : static OGRwkbGeometryType SpatiaLiteToOGRGeomType( const char * );
249 : static const char *OGRToSpatiaLiteGeomType( OGRwkbGeometryType );
250 :
251 : public:
252 : OGRSQLiteDataSource();
253 : ~OGRSQLiteDataSource();
254 :
255 : int Open( const char * );
256 : int OpenTable( const char *pszTableName,
257 : const char *pszGeomCol = NULL,
258 : OGRwkbGeometryType eGeomType = wkbUnknown,
259 : const char *pszGeomFormat = NULL,
260 : OGRSpatialReference *poSRS = NULL,
261 : int nSRID = -1,
262 : int bHasSpatialIndex = FALSE);
263 :
264 107 : const char *GetName() { return pszName; }
265 1047 : int GetLayerCount() { return nLayers; }
266 : OGRLayer *GetLayer( int );
267 :
268 : virtual OGRLayer *CreateLayer( const char *pszLayerName,
269 : OGRSpatialReference *poSRS,
270 : OGRwkbGeometryType eType,
271 : char **papszOptions );
272 :
273 : int TestCapability( const char * );
274 :
275 : virtual OGRLayer * ExecuteSQL( const char *pszSQLCommand,
276 : OGRGeometry *poSpatialFilter,
277 : const char *pszDialect );
278 : virtual void ReleaseResultSet( OGRLayer * poLayer );
279 :
280 : OGRErr SoftStartTransaction();
281 : OGRErr SoftCommit();
282 : OGRErr SoftRollback();
283 :
284 : OGRErr FlushSoftTransaction();
285 :
286 1322 : sqlite3 *GetDB() { return hDB; }
287 :
288 : char *LaunderName( const char * );
289 : int FetchSRSId( OGRSpatialReference * poSRS );
290 : OGRSpatialReference*FetchSRS( int nSRID );
291 : };
292 :
293 : /************************************************************************/
294 : /* OGRSQLiteDriver */
295 : /************************************************************************/
296 :
297 : class OGRSQLiteDriver : public OGRSFDriver
298 80 : {
299 : private:
300 : static int InitWithEPSG(sqlite3* hDB, int bSpatialite);
301 :
302 : public:
303 : ~OGRSQLiteDriver();
304 :
305 : const char *GetName();
306 : OGRDataSource *Open( const char *, int );
307 :
308 : virtual OGRDataSource *CreateDataSource( const char *pszName,
309 : char ** = NULL );
310 :
311 : int TestCapability( const char * );
312 : };
313 :
314 :
315 : #endif /* ndef _OGR_SQLITE_H_INCLUDED */
316 :
317 :
|