1 /******************************************************************************
2  * $Id: ogr_ingres.h 842d122d2f23aaebb28362e083b52d6bc7dbcde2 2019-08-11 17:42:34 +0200 Even Rouault $
3  *
4  * Project:  OpenGIS Simple Features Reference Implementation
5  * Purpose:  Declarations for Ingres OGR Driver Classes.
6  * Author:   Frank Warmerdam, warmerdam@pobox.com
7  *
8  ******************************************************************************
9  * Copyright (c) 2008, 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_INGRES_H_INCLUDED
31 #define OGR_INGRES_H_INCLUDED
32 
33 #include <iiapi.h>
34 #include "ogrsf_frmts.h"
35 
36 class OGRIngresDataSource;
37 
38 /************************************************************************/
39 /*                          OGRIngresStatement                          */
40 /************************************************************************/
41 
42 class OGRIngresStatement
43 {
44 public:
45     II_PTR            hConn = nullptr;
46     II_PTR            hStmt = nullptr;
47     II_PTR            hTransaction = nullptr;
48 
49     IIAPI_GETDESCRPARM  getDescrParm;
50     IIAPI_GETCOLPARM    getColParm;
51     IIAPI_DATAVALUE     *pasDataBuffer = nullptr;
52     IIAPI_GETQINFOPARM  queryInfo;
53 
54     GByte             *pabyWrkBuffer = nullptr;
55     char              **papszFields = nullptr;
56 
57     int               bDebug = TRUE;
58 
59     int               bHaveParm = FALSE;
60     IIAPI_DT_ID       eParmType;
61     int               nParmLen = 0;
62     GByte            *pabyParmData = nullptr;
63 
64     explicit OGRIngresStatement( II_PTR hConn );
65     ~OGRIngresStatement();
66 
67     void addInputParameter( IIAPI_DT_ID eDType, int nLength, GByte *pabyData );
68 
69     int ExecuteSQL( const char * );
70 
71     char **GetRow();
72     void   DumpRow( FILE * );
73     static void         ReportError( IIAPI_GENPARM *, const char * = NULL );
74 
75     int    IsColumnLong(int iCol);
76     void   ClearDynamicColumns();
77     void   Close();
78     int    SendParms();
79 };
80 
81 /************************************************************************/
82 /*                            OGRIngresLayer                             */
83 /************************************************************************/
84 
85 class OGRIngresLayer CPL_NON_FINAL: public OGRLayer
86 {
87   protected:
88     OGRFeatureDefn     *poFeatureDefn;
89 
90     // Layer spatial reference system, and srid.
91     OGRSpatialReference *poSRS;
92     int                 nSRSId;
93 
94     int                 iNextShapeId;
95 
96     OGRIngresDataSource    *poDS;
97 
98     CPLString           osQueryStatement;
99 
100     int                 nResultOffset;
101 
102     CPLString           osGeomColumn;
103     CPLString           osIngresGeomType;
104 
105     CPLString           osFIDColumn;
106 
107     OGRIngresStatement *poResultSet; /* stmt */
108 
109     int                 FetchSRSId(OGRFeatureDefn *poDefn);
110     OGRGeometry        *TranslateGeometry( const char * );
111 
112   public:
113                         OGRIngresLayer();
114     virtual             ~OGRIngresLayer();
115 
116     virtual void        ResetReading() override;
117 
118     virtual OGRFeature *GetNextFeature() override;
119 
120     virtual OGRFeature *GetFeature( GIntBig nFeatureId ) override;
121 
GetLayerDefn()122     OGRFeatureDefn *    GetLayerDefn() override { return poFeatureDefn; }
123 
124     virtual OGRSpatialReference *GetSpatialRef() override;
125 
126     virtual int         TestCapability( const char * ) override;
127 
128     virtual const char *GetFIDColumn() override;
129     virtual const char *GetGeometryColumn() override;
130 
131     /* custom methods */
132     virtual OGRFeature *RecordToFeature( char **papszRow );
133     virtual OGRFeature *GetNextRawFeature();
134 };
135 
136 /************************************************************************/
137 /*                          OGRIngresTableLayer                          */
138 /************************************************************************/
139 
140 class OGRIngresTableLayer final: public OGRIngresLayer
141 {
142     int                 bUpdateAccess;
143 
144     OGRFeatureDefn     *ReadTableDefinition(const char *);
145 
146     void                BuildWhere();
147     char               *BuildFields();
148     void                BuildFullQueryStatement();
149 
150     CPLString           osQuery;
151     CPLString           osWHERE;
152 
153     int                 bLaunderColumnNames;
154     int                 bPreservePrecision;
155 
156     OGRErr              PrepareOldStyleGeometry( OGRGeometry*, CPLString& );
157     OGRErr              PrepareNewStyleGeometry( OGRGeometry*, CPLString& );
158 
159   public:
160                         OGRIngresTableLayer( OGRIngresDataSource *,
161                                          const char * pszName,
162                                          int bUpdate, int nSRSId = -2 );
163                         virtual ~OGRIngresTableLayer();
164 
165     OGRErr              Initialize(const char* pszTableName);
166 
167 //    virtual OGRFeature *GetFeature( GIntBig nFeatureId );
168     virtual void        ResetReading() override;
169 //    virtual GIntBig     GetFeatureCount( int );
170 
171     void                SetSpatialFilter( OGRGeometry * ) override;
SetSpatialFilter(int iGeomField,OGRGeometry * poGeom)172     virtual void        SetSpatialFilter( int iGeomField, OGRGeometry *poGeom ) override
173                 { OGRLayer::SetSpatialFilter(iGeomField, poGeom); }
174 
175     virtual OGRErr      SetAttributeFilter( const char * ) override;
176 
177     virtual OGRErr      ICreateFeature( OGRFeature *poFeature ) override;
178     virtual OGRErr      DeleteFeature( GIntBig nFID ) override;
179     virtual OGRErr      ISetFeature( OGRFeature *poFeature ) override;
180 
181     virtual OGRErr      CreateField( OGRFieldDefn *poField,
182                                      int bApproxOK = TRUE ) override;
183 
SetLaunderFlag(int bFlag)184     void                SetLaunderFlag( int bFlag )
185                                 { bLaunderColumnNames = bFlag; }
SetPrecisionFlag(int bFlag)186     void                SetPrecisionFlag( int bFlag )
187                                 { bPreservePrecision = bFlag; }
188 
189     virtual int         TestCapability( const char * ) override;
190 };
191 
192 /************************************************************************/
193 /*                         OGRIngresResultLayer                          */
194 /************************************************************************/
195 
196 class OGRIngresResultLayer final: public OGRIngresLayer
197 {
198     void                BuildFullQueryStatement();
199 
200     char                *pszRawStatement;
201 
202     int                 nFeatureCount;
203 
204   public:
205                         OGRIngresResultLayer( OGRIngresDataSource *,
206                                               const char * pszRawStatement,
207                                               OGRIngresStatement *hStmt );
208     virtual             ~OGRIngresResultLayer();
209 
210     OGRFeatureDefn     *ReadResultDefinition();
211 
212     virtual void        ResetReading() override;
213     virtual GIntBig     GetFeatureCount( int ) override;
214 };
215 
216 /************************************************************************/
217 /*                          OGRIngresDataSource                          */
218 /************************************************************************/
219 
220 class OGRIngresDataSource final: public OGRDataSource
221 {
222     OGRIngresLayer    **papoLayers = nullptr;
223     int                 nLayers = 0;
224 
225     char               *pszName = nullptr;
226 
227     int                 bDSUpdate = FALSE;
228 
229     II_PTR              hConn = nullptr;
230 
231     int                 DeleteLayer( int iLayer ) override;
232 
233     // We maintain a list of known SRID to reduce the number of trips to
234     // the database to get SRSes.
235     int                 nKnownSRID = 0;
236     int                *panSRID = nullptr;
237     OGRSpatialReference **papoSRS = nullptr;
238 
239     OGRIngresLayer     *poActiveLayer = nullptr; /* this layer has active transaction */
240 
241     int                 bNewIngres = FALSE; /* TRUE if new spatial library */
242 
243   public:
244                         OGRIngresDataSource();
245                         virtual ~OGRIngresDataSource();
246 
GetConn()247     II_PTR              GetConn() { return hConn; }
248 
249     int                 FetchSRSId( OGRSpatialReference * poSRS );
250 
251     OGRSpatialReference *FetchSRS( int nSRSId );
252 
253     static OGRErr              InitializeMetadataTables();
254 
255     int                 Open( const char *pszFullName,
256                               char **papszOptions, int bUpdate );
257     int                 OpenTable( const char *, int bUpdate );
258 
GetName()259     const char          *GetName() override { return pszName; }
GetLayerCount()260     int                 GetLayerCount() override { return nLayers; }
261     OGRLayer            *GetLayer( int ) override;
262 
263     virtual OGRLayer    *ICreateLayer( const char *,
264                                        OGRSpatialReference * = NULL,
265                                       OGRwkbGeometryType = wkbUnknown,
266                                       char ** = NULL ) override;
267 
268     int                 TestCapability( const char * ) override;
269 
270     virtual OGRLayer *  ExecuteSQL( const char *pszSQLCommand,
271                                     OGRGeometry *poSpatialFilter,
272                                     const char *pszDialect ) override;
273     virtual void        ReleaseResultSet( OGRLayer * poLayer ) override;
274 
275     // nonstandard
276 
277     char               *LaunderName( const char * );
278 
279     void                EstablishActiveLayer( OGRIngresLayer * );
280     int                 IsNewIngres();
281 };
282 
283 /************************************************************************/
284 /*                            OGRIngresDriver                            */
285 /************************************************************************/
286 
287 class OGRIngresDriver final: public OGRSFDriver
288 {
289     char         **ParseWrappedName( const char * );
290 
291   public:
292     virtual ~OGRIngresDriver();
293 
294     const char *GetName() override;
295     OGRDataSource *Open( const char *, int ) override;
296     virtual OGRDataSource *CreateDataSource( const char *pszName,
297                                              char ** = NULL ) override;
298     int                 TestCapability( const char * ) override;
299 };
300 
301 #endif /* ndef OGR_PG_H_INCLUDED */
302