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