forked from OSGeo/gdal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoci_wrapper.h
465 lines (393 loc) · 13.9 KB
/
oci_wrapper.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/******************************************************************************
*
* Name: oci_wrapper.h
* Project: Oracle Spatial GeoRaster Driver
* Purpose: Limited wrapper for OCI (Oracle Call Interfaces)
* Author: Ivan Lucena [ivan.lucena at oracle.com]
*
******************************************************************************
* Copyright (c) 2008, Ivan Lucena
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#ifndef OCI_WRAPPER_H_INCLUDED
#define OCI_WRAPPER_H_INCLUDED
// GDAL supporting types
#include "gdal.h"
#include "gdal_priv.h"
#include "cpl_string.h"
// Oracle Class Interface
#include <oci.h>
/***************************************************************************/
/* Data type conversion table record type */
/***************************************************************************/
struct OW_CellDepth
{
const char *pszValue;
GDALDataType eDataType;
};
/***************************************************************************/
/* OCI Error check */
/***************************************************************************/
bool CheckError(sword nStatus, OCIError *hError);
/***************************************************************************/
/* Auxiliary functions */
/***************************************************************************/
GDALDataType OWGetDataType(const char *pszCellDepth);
const char *OWSetDataType(const GDALDataType eType);
int OWParseServerVersion(const char *pszText);
int OWParseEPSG(const char *pszText);
bool OWIsNumeric(const char *pszText);
CPLString OWParseSDO_GEOR_INIT(const char *pszInsert, int nField);
char *OWRemoveQuotes(const char *pszText);
void OWUpperIfNoQuotes(char *pszText);
/***************************************************************************/
/* Arbitrary limits */
/***************************************************************************/
#define OWCODE 64
#define OWNAME 512
#define OWTEXT 1024
/***************************************************************************/
/* TYPES */
/***************************************************************************/
#define TYPE_OWNER "MDSYS"
#define SDO_GEOMETRY TYPE_OWNER ".SDO_GEOMETRY"
#define SDO_GEORASTER TYPE_OWNER ".SDO_GEORASTER"
#define SDO_PC TYPE_OWNER ".SDO_PC"
#define SDO_NUMBER_ARRAY TYPE_OWNER ".SDO_NUMBER_ARRAY"
#define SDO_ORDINATE_ARRAY TYPE_OWNER ".SDO_ORDINATE_ARRAY"
#define SDO_ELEM_INFO_ARRAY TYPE_OWNER ".SDO_ELEM_INFO_ARRAY"
#define OW_XMLNS "xmlns=\"http://xmlns.oracle.com/spatial/georaster\""
/***************************************************************************/
/* USER DEFINED (actually Oracle's) types */
/***************************************************************************/
typedef OCIRef SDO_GEORASTER_ref;
typedef OCIRef SDO_GEOMETRY_ref;
typedef OCIRef SDO_POINT_TYPE_ref;
typedef OCIArray sdo_elem_info_array;
typedef OCIArray sdo_ordinate_array;
typedef OCIArray SDO_NUMBER_ARRAY_TYPE;
/***************************************************************************/
/* Point type */
/***************************************************************************/
struct sdo_point_type
{
OCINumber x;
OCINumber y;
OCINumber z;
};
typedef struct sdo_point_type sdo_point_type;
struct sdo_point_type_ind
{
OCIInd _atomic;
OCIInd x;
OCIInd y;
OCIInd z;
};
typedef struct sdo_point_type_ind sdo_point_type_ind;
/***************************************************************************/
/* Geometry type */
/***************************************************************************/
struct sdo_geometry
{
OCINumber sdo_gtype;
OCINumber sdo_srid;
sdo_point_type sdo_point;
OCIArray *sdo_elem_info;
OCIArray *sdo_ordinates;
};
typedef struct sdo_geometry SDO_GEOMETRY_TYPE;
struct sdo_geometry_ind
{
OCIInd _atomic;
OCIInd sdo_gtype;
OCIInd sdo_srid;
struct sdo_point_type_ind sdo_point;
OCIInd sdo_elem_info;
OCIInd sdo_ordinates;
};
typedef struct SDO_GEOMETRY_ind SDO_GEOMETRY_ind;
/***************************************************************************/
/* GeoRaster type */
/***************************************************************************/
struct sdo_georaster
{
OCINumber rastertype;
SDO_GEOMETRY_TYPE spatialextent;
OCIString *rasterdatatable;
OCINumber rasterid;
void *metadata;
};
typedef struct sdo_georaster SDO_GEORASTER_TYPE;
struct sdo_georaster_ind
{
OCIInd _atomic;
OCIInd rastertype;
sdo_geometry_ind spatialextent;
OCIInd rasterdatatable;
OCIInd rasterid;
OCIInd metadata;
};
typedef struct sdo_georaster_ind SDO_GEORASTER_ind;
/***************************************************************************/
/* Point Cloud type */
/***************************************************************************/
struct sdo_mbr
{
OCIArray *lower_left;
OCIArray *upper_right;
};
typedef struct sdo_mbr SDO_MBR_TYPE;
struct sdo_mbr_ind
{
OCIInd _atomic;
OCIInd lower_left;
OCIInd upper_right;
};
typedef struct sdo_mbr_ind SDO_MBR_ind;
struct sdo_orgscl_type
{
SDO_MBR_TYPE extent;
OCIArray *scale;
OCIArray *ord_cmp_type;
};
typedef struct sdo_orgscl_type SDO_ORGSCL_TYPE;
struct sdo_orgscl_type_ind
{
OCIInd _atomic;
SDO_MBR_ind extent;
OCIInd scale;
OCIInd ord_cmp_type;
};
typedef struct sdo_orgscl_type_ind SDO_ORGSCL_TYPE_ind;
struct sdo_pc
{
OCIString *base_table;
OCIString *base_column;
OCINumber pc_id;
OCIString *blk_table;
OCIString *ptn_params;
SDO_GEOMETRY_TYPE pc_geometry;
OCINumber pc_tol;
OCINumber pc_tot_dimensions;
SDO_ORGSCL_TYPE pc_domain;
OCIString *pc_val_attr_tables;
void *pc_other_attrs;
};
typedef struct sdo_pc SDO_PC_TYPE;
struct sdo_pc_ind
{
OCIInd _atomic;
OCIInd base_table;
OCIInd base_column;
OCIInd pc_id;
OCIInd blk_table;
OCIInd ptn_params;
sdo_geometry_ind pc_geometry;
OCIInd pc_tol;
OCIInd pc_tot_dimensions;
OCIInd pc_domain;
OCIInd pc_val_attr_tables;
OCIInd pc_other_attrs;
};
typedef struct sdo_pc_ind SDO_PC_ind;
/***************************************************************************/
/* Oracle class wrappers */
/***************************************************************************/
class OWConnection;
class OWStatement;
// ---------------------------------------------------------------------------
// OWConnection
// ---------------------------------------------------------------------------
class OWConnection
{
friend class OWStatement;
public:
OWConnection(const char *pszUserIn, const char *pszPasswordIn,
const char *pszServerIn);
explicit OWConnection(OCIExtProcContext *poWithContext);
virtual ~OWConnection();
private:
OCIEnv *hEnv = nullptr;
OCIError *hError = nullptr;
OCISvcCtx *hSvcCtx = nullptr;
OCIServer *hServer = nullptr;
OCISession *hSession = nullptr;
OCIDescribe *hDescribe = nullptr;
int nVersion = 0;
sb4 nCharSize = 1;
bool bSuceeeded = false;
bool bExtProc = false;
char *pszUser = nullptr;
// session is only used when user is not provided.
char *pszSessionUser = nullptr;
char *pszPassword = nullptr;
char *pszServer = nullptr;
char *pszExtProcUser = nullptr;
char *pszExtProcSchema = nullptr;
OCIType *hNumArrayTDO = nullptr;
OCIType *hGeometryTDO = nullptr;
OCIType *hGeoRasterTDO = nullptr;
OCIType *hPCTDO = nullptr;
OCIType *hElemArrayTDO = nullptr;
OCIType *hOrdnArrayTDO = nullptr;
void QueryVersion();
public:
OWStatement *CreateStatement(const char *pszStatement);
OCIParam *GetDescription(char *pszTable);
bool GetNextField(OCIParam *phTable, int nIndex, char *pszName, int *pnType,
int *pnSize, int *pnPrecision, signed short *pnScale);
void CreateType(sdo_geometry **pphData);
void DestroyType(sdo_geometry **pphData);
void CreateType(OCIArray **phData, OCIType *type);
void DestroyType(OCIArray **phData);
void DestroyType(OCIType *phType);
OCIType *DescribeType(const char *pszTypeName);
bool Succeeded() const
{
return bSuceeeded;
}
const char *GetUser() const
{
return pszUser;
}
const char *GetSessionUser() const
{
return (pszSessionUser == nullptr) ? pszUser : pszSessionUser;
}
const char *GetPassword() const
{
return pszPassword;
}
const char *GetServer() const
{
return pszServer;
}
int GetVersion() const
{
return nVersion;
}
sb4 GetCharSize() const
{
return nCharSize;
}
OCIType *GetGeometryType()
{
return hGeometryTDO;
}
OCIType *GetGeoRasterType()
{
return hGeoRasterTDO;
}
OCIType *GetElemInfoType()
{
return hElemArrayTDO;
}
OCIType *GetOrdinateType()
{
return hOrdnArrayTDO;
}
bool Commit(); // OCITransCommit()
bool StartTransaction(); // //OCITransStart()
bool EndTransaction()
{
return Commit();
}
bool IsExtProc() const
{
return bExtProc;
}
const char *GetExtProcUser() const
{
return pszExtProcUser;
}
const char *GetExtProcSchema() const
{
return pszExtProcSchema;
}
};
/***************************************************************************/
/* OWStatement */
/***************************************************************************/
class OWStatement
{
public:
OWStatement(OWConnection *poConnect, const char *pszStatement);
virtual ~OWStatement();
private:
OWConnection *poConnection = nullptr;
OCIStmt *hStmt = nullptr;
OCIError *hError = nullptr;
int nNextCol = 0;
int nNextBnd = 0;
ub4 nStmtMode = OCI_DEFAULT;
public:
bool Execute(int nRows = 1);
bool Fetch(int nRows = 1);
unsigned int nFetchCount = 0;
int GetInteger(OCINumber *ppoData);
double GetDouble(OCINumber *ppoData);
char *GetString(OCIString *ppoData);
void Bind(int *pnData);
void Bind(long *pnData);
void Bind(long long *pnData);
void Bind(double *pnData);
void Bind(char *pData, long nData);
void Bind(sdo_geometry **pphData);
void Bind(OCILobLocator **pphLocator);
void Bind(OCIArray **pphData, OCIType *type);
void Bind(char *pszData, int nSize = OWNAME);
void Define(int *pnData);
void Define(long *pnData);
void Define(long long *pnData);
void Define(double *pnData);
void Define(char *pszData, int nSize = OWNAME);
void Define(OCILobLocator **pphLocator);
void Define(OCIArray **pphData);
void Define(sdo_georaster **pphData);
void Define(sdo_geometry **pphData);
void Define(sdo_pc **pphData);
void Define(OCILobLocator **pphLocator, long nIterations);
void BindName(const char *pszName, int *pnData);
void BindName(const char *pszName, long *pnData);
void BindName(const char *pszName, long long *pnData);
void BindName(const char *pszName, double *pnData);
void BindName(const char *pszName, char *pszData, int nSize = OWNAME);
void BindName(const char *pszName, OCILobLocator **pphLocator);
void BindArray(void *pData, long nSize = 1);
static void Free(OCILobLocator **ppphLocator, int nCount);
unsigned long ReadBlob(OCILobLocator *phLocator, void *pBuffer,
unsigned long nSize);
unsigned long ReadBlob(OCILobLocator *phLocator, void *pBuffer,
unsigned long nOffset, unsigned long nSize);
void FreeLob(OCILobLocator *phLocator);
char *ReadCLob(OCILobLocator *phLocator);
void WriteCLob(OCILobLocator **pphLocator, char *pszData);
bool WriteBlob(OCILobLocator *phLocator, void *pBuffer,
unsigned long nSize);
unsigned long WriteBlob(OCILobLocator *phLocator, void *pBuffer,
unsigned long nOffset, unsigned long nSize);
bool TrimLob(OCILobLocator *phLocator, unsigned long newLen);
int GetElement(OCIArray **ppoData, int nIndex, int *pnResult);
double GetElement(OCIArray **ppoData, int nIndex, double *pdfResult);
void AddElement(OCIArray *ppoData, int nValue);
void AddElement(OCIArray *ppoData, double dfValue);
unsigned long GetBlobLength(OCILobLocator *phLocator);
};
#endif /* ifndef _ORCL_WRAP_H_INCLUDED */