1 /******************************************************************************
2  * $Id: ogr_sde.h 28375 2015-01-30 12:06:11Z rouault $
3  *
4  * Project:  OpenGIS Simple Features Reference Implementation
5  * Purpose:  Private definitions for OGR SDE driver.
6  * Author:   Frank Warmerdam, warmerdam@pobox.com
7  * Copyright (c) 2008, Shawn Gervais <project10@project10.net>
8  * Copyright (c) 2008, Howard Butler <hobu.inc@gmail.com>
9  *
10  ******************************************************************************
11  * Copyright (c) 2005, Frank Warmerdam <warmerdam@pobox.com>
12  *
13  * Permission is hereby granted, free of charge, to any person obtaining a
14  * copy of this software and associated documentation files (the "Software"),
15  * to deal in the Software without restriction, including without limitation
16  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
17  * and/or sell copies of the Software, and to permit persons to whom the
18  * Software is furnished to do so, subject to the following conditions:
19  *
20  * The above copyright notice and this permission notice shall be included
21  * in all copies or substantial portions of the Software.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29  * DEALINGS IN THE SOFTWARE.
30  ****************************************************************************/
31 
32 #ifndef _OGR_SDE_H_INCLUDED
33 #define _OGR_SDE_H_INCLUDED
34 
35 #include "ogrsf_frmts.h"
36 
37 #include <sdetype.h> /* ESRI SDE Client Includes */
38 #include <sdeerno.h>
39 #include <vector>
40 #include "cpl_string.h"
41 
42 #define OGR_SDE_LAYER_CO_GRID1 1000
43 #define OGR_SDE_LAYER_CO_GRID2 0
44 #define OGR_SDE_LAYER_CO_GRID3 0
45 #define OGR_SDE_LAYER_CO_INIT_FEATS 50
46 #define OGR_SDE_LAYER_CO_AVG_PTS 5
47 
48 /************************************************************************/
49 /*                            OGRSDELayer                                */
50 /************************************************************************/
51 
52 
53 class OGRSDEDataSource;
54 
55 class OGRSDELayer : public OGRLayer
56 {
57   protected:
58     OGRFeatureDefn     *poFeatureDefn;
59 
60     // Layer spatial reference system, and srid.
61     OGRSpatialReference *poSRS;
62 
63     char               *pszOwnerName;
64     char               *pszDbTableName;
65 
66     int                 bUpdateAccess;
67     int                 bVersioned;
68     int                 bPreservePrecision;
69 
70     CPLString           osAttributeFilter;
71 
72     int                 bQueryInstalled;
73     int                 bQueryActive;
74 
75     SE_STREAM           hStream;
76 
77     int                 bHaveLayerInfo;
78     SE_LAYERINFO        hLayerInfo;
79     SE_COORDREF         hCoordRef;
80 
81     OGRSDEDataSource    *poDS;
82 
83     int                 iFIDColumn;
84     LONG                nFIDColumnType;
85 
86     int                 nNextFID;
87     int                 iNextFIDToWrite;
88 
89     int                 iShapeColumn;
90 
91     int                 bUseNSTRING;
92 
93 
94     char              **papszAllColumns;
95     std::vector<int>    anFieldMap;     // SDE index of OGR field.
96     std::vector<int>    anFieldTypeMap; // SDE type
97 
98     int                 InstallQuery( int );
99     OGRFeature         *TranslateSDERecord();
100     OGRGeometry        *TranslateSDEGeometry( SE_SHAPE );
101     OGRErr              TranslateOGRRecord( OGRFeature *, int );
102     OGRErr              TranslateOGRGeometry( OGRGeometry *, SE_SHAPE *,
103                                               SE_COORDREF );
104     int                 NeedLayerInfo();
105 
106     // This process can be fairly expensive depending on the configuration
107     // of the layer in SDE. Enable this feature with OGR_SDE_GETLAYERTYPE.
108     OGRwkbGeometryType  DiscoverLayerType();
109 
110   public:
111 
112                         OGRSDELayer( OGRSDEDataSource *,
113                                      int );
114     virtual             ~OGRSDELayer();
115 
116     CPLString           osFIDColumnName;
117     CPLString           osShapeColumnName;
118 
119     int                 Initialize( const char *, const char *, const char * );
120 
121     virtual void        ResetReading();
122     OGRErr              ResetStream();
123 
124     virtual OGRFeature *GetNextFeature();
125     virtual OGRFeature *GetFeature( GIntBig nFeatureId );
126     virtual OGRErr      GetExtent( OGREnvelope *psExtent, int bForce );
127     virtual GIntBig     GetFeatureCount( int bForce );
128 
129     virtual OGRErr      SetAttributeFilter( const char *pszQuery );
130 
131     virtual OGRErr      CreateField( OGRFieldDefn *poFieldIn,
132                                      int bApproxOK );
133 
134     virtual OGRErr      ISetFeature( OGRFeature *poFeature );
135     virtual OGRErr      ICreateFeature( OGRFeature *poFeature );
136     virtual OGRErr      DeleteFeature( GIntBig nFID );
137 
GetLayerDefn()138     OGRFeatureDefn *    GetLayerDefn() { return poFeatureDefn; }
139 
140     virtual OGRSpatialReference *GetSpatialRef();
141 
142     virtual int         TestCapability( const char * );
143 
144     // The following methods are not base class overrides
145     //void                SetOptions( char ** );
146 
SetFIDColType(LONG nType)147     void                SetFIDColType( LONG nType )
148                                 { nFIDColumnType = nType; }
SetPrecisionFlag(int bFlag)149     void                SetPrecisionFlag( int bFlag )
150                                 { bPreservePrecision = bFlag; }
SetUseNSTRING(int bFlag)151     void                SetUseNSTRING( int bFlag )
152                                 { bUseNSTRING = bFlag; }
153 };
154 
155 /************************************************************************/
156 /*                           OGRSDEDataSource                            */
157 /************************************************************************/
158 class OGRSDEDataSource : public OGRDataSource
159 {
160     OGRSDELayer        **papoLayers;
161     int                 nLayers;
162 
163     char               *pszName;
164 
165     int                 bDSUpdate;
166     int                 bDSUseVersionEdits;
167     int                 bDSVersionLocked;
168 
169     SE_CONNECTION       hConnection;
170     LONG                nState;
171     LONG                nNextState;
172     SE_VERSIONINFO      hVersion;
173 
174   public:
175                         OGRSDEDataSource();
176                         ~OGRSDEDataSource();
177 
178     int                 Open( const char *, int );
179     int                 OpenTable( const char *pszTableName,
180                                    const char *pszFIDColumn,
181                                    const char *pszShapeColumn,
182                                    LONG nFIDColumnType );
183 
GetName()184     const char          *GetName() { return pszName; }
GetLayerCount()185     int                 GetLayerCount() { return nLayers; }
186     OGRLayer            *GetLayer( int );
187 
188     virtual OGRLayer    *ICreateLayer( const char *,
189                                       OGRSpatialReference * = NULL,
190                                       OGRwkbGeometryType = wkbUnknown,
191                                       char ** = NULL );
192 
193     virtual OGRErr      DeleteLayer( int );
194 
195     int                 TestCapability( const char * );
196 
GetConnection()197     SE_CONNECTION       GetConnection() { return hConnection; }
GetState()198     LONG                GetState() {return nState; }
GetNextState()199     LONG                GetNextState() {return nNextState; }
200 
201     void                IssueSDEError( int, const char * );
202 
203     OGRErr              ConvertOSRtoSDESpatRef( OGRSpatialReference *,
204                                                 SE_COORDREF * );
IsOpenForUpdate()205     int                 IsOpenForUpdate() { return bDSUpdate; }
UseVersionEdits()206     int                 UseVersionEdits() { return bDSUseVersionEdits; }
207 
208   protected:
209     void                EnumerateSpatialTables();
210     void                OpenSpatialTable( const char* pszTableName );
211     void                CreateLayerFromRegInfo(SE_REGINFO& reginfo);
212     void                CleanupLayerCreation(const char* pszLayerName);
213     int                 SetVersionState( const char* pszVersionName );
214     int                 CreateVersion( const char* pszParentVersion, const char* pszChildVersion);
215 };
216 
217 /************************************************************************/
218 /*                             OGRSDEDriver                              */
219 /************************************************************************/
220 
221 class OGRSDEDriver : public OGRSFDriver
222 {
223   public:
224                 ~OGRSDEDriver();
225 
226     const char *GetName();
227     OGRDataSource *Open( const char *, int );
228 
229     int                 TestCapability( const char * );
230     virtual OGRDataSource *CreateDataSource( const char *pszName,
231                                              char ** = NULL);
232 };
233 
234 
235 #endif /* ndef _OGR_PG_H_INCLUDED */
236 
237 
238