Debian Codegraph @

ogr/ogrsf_frmts/oci/ogrocisession.cpp

raw
1 /******************************************************************************
2 *
3 * Project: Oracle Spatial Driver
4 * Purpose: Implementation of OGROCISession, which encapsulates much of the
5 * direct access to OCI.
6 * Author: Frank Warmerdam, [email protected]
7 *
8 ******************************************************************************
9 * Copyright (c) 2002, Frank Warmerdam <[email protected]>
10 *
11 * SPDX-License-Identifier: MIT
12 ****************************************************************************/
13
14 #include "ogr_oci.h"
15 #include "cpl_conv.h"
16
17 /************************************************************************/
18 /* OGRGetOCISession() */
19 /************************************************************************/
20
21 OGROCISession *OGRGetOCISession(const char *pszUserid, const char *pszPassword,
22 const char *pszDatabase)
23
24 {
25 OGROCISession *poSession;
26
27 poSession = new OGROCISession();
28 if (poSession->EstablishSession(pszUserid, pszPassword, pszDatabase))
29 return poSession;
30 else
31 {
32 delete poSession;
33 return nullptr;
34 }
35 }
36
37 /************************************************************************/
38 /* OGROCISession() */
39 /************************************************************************/
40
41 OGROCISession::OGROCISession()
42
43 {
44 hEnv = nullptr;
45 hError = nullptr;
46 hSvcCtx = nullptr;
47 hServer = nullptr;
48 hSession = nullptr;
49 hDescribe = nullptr;
50 hGeometryTDO = nullptr;
51 hOrdinatesTDO = nullptr;
52 hElemInfoTDO = nullptr;
53 pszUserid = nullptr;
54 pszPassword = nullptr;
55 pszDatabase = nullptr;
56 nServerVersion = 10;
57 nServerRelease = 1;
58 nMaxNameLength = 30;
59 }
60
61 /************************************************************************/
62 /* ~OGROCISession() */
63 /************************************************************************/
64
65 OGROCISession::~OGROCISession()
66
67 {
68 if (hDescribe != nullptr)
69 OCIHandleFree(static_cast<dvoid *>(hDescribe), (ub4)OCI_HTYPE_DESCRIBE);
70
71 if (hSvcCtx != nullptr)
72 {
73 OCISessionEnd(hSvcCtx, hError, hSession, (ub4)0);
74
75 if (hSvcCtx && hError)
76 OCIServerDetach(hServer, hError, (ub4)OCI_DEFAULT);
77
78 if (hServer)
79 OCIHandleFree(static_cast<dvoid *>(hServer), (ub4)OCI_HTYPE_SERVER);
80
81 if (hSvcCtx)
82 OCIHandleFree(static_cast<dvoid *>(hSvcCtx), (ub4)OCI_HTYPE_SVCCTX);
83
84 if (hError)
85 OCIHandleFree(static_cast<dvoid *>(hError), (ub4)OCI_HTYPE_ERROR);
86
87 if (hSession)
88 OCIHandleFree(static_cast<dvoid *>(hSession),
89 (ub4)OCI_HTYPE_SESSION);
90
91 if (hEnv)
92 OCIHandleFree(static_cast<dvoid *>(hEnv), (ub4)OCI_HTYPE_ENV);
93 }
94
95 CPLFree(pszUserid);
96 CPLFree(pszPassword);
97 CPLFree(pszDatabase);
98 }
99
100 /************************************************************************/
101 /* EstablishSession() */
102 /************************************************************************/
103
104 int OGROCISession::EstablishSession(const char *pszUseridIn,
105 const char *pszPasswordIn,
106 const char *pszDatabaseIn)
107
108 {
109 /* -------------------------------------------------------------------- */
110 /* Operational Systems's authentication option */
111 /* -------------------------------------------------------------------- */
112
113 // Setting to "/" for backward compatibility
114 const char *pszUser = "/";
115
116 ub4 eCred = OCI_CRED_RDBMS;
117
118 if (EQUAL(pszPasswordIn, "") && EQUAL(pszUseridIn, ""))
119 {
120 // user and password are ignored in this credential type.
121 eCred = OCI_CRED_EXT;
122 }
123 else
124 {
125 pszUser = pszUseridIn;
126 }
127
128 /* -------------------------------------------------------------------- */
129 /* Initialize Environment handler */
130 /* -------------------------------------------------------------------- */
131
132 if (Failed(OCIEnvCreate(static_cast<OCIEnv **>(&hEnv),
133 OCI_THREADED | OCI_OBJECT, nullptr, nullptr,
134 nullptr, nullptr, 0, nullptr)))
135 {
136 return FALSE;
137 }
138
139 if (Failed(OCIHandleAlloc(static_cast<dvoid *>(hEnv),
140 reinterpret_cast<dvoid **>(&hError),
141 OCI_HTYPE_ERROR, 0, nullptr)))
142 {
143 return FALSE;
144 }
145
146 /* -------------------------------------------------------------------- */
147 /* Initialize Server Context */
148 /* -------------------------------------------------------------------- */
149
150 if (Failed(OCIHandleAlloc(static_cast<dvoid *>(hEnv),
151 reinterpret_cast<dvoid **>(&hServer),
152 OCI_HTYPE_SERVER, 0, nullptr)))
153 {
154 return FALSE;
155 }
156
157 if (Failed(OCIHandleAlloc(static_cast<dvoid *>(hEnv),
158 reinterpret_cast<dvoid **>(&hSvcCtx),
159 OCI_HTYPE_SVCCTX, 0, nullptr)))
160 {
161 return FALSE;
162 }
163
164 if (Failed(OCIServerAttach(
165 hServer, hError,
166 reinterpret_cast<text *>(const_cast<char *>(pszDatabaseIn)),
167 static_cast<int>(strlen(pszDatabaseIn)), 0)))
168 {
169 return FALSE;
170 }
171
172 /* -------------------------------------------------------------------- */
173 /* Initialize Service Context */
174 /* -------------------------------------------------------------------- */
175
176 if (Failed(OCIAttrSet(static_cast<dvoid *>(hSvcCtx), OCI_HTYPE_SVCCTX,
177 static_cast<dvoid *>(hServer), (ub4)0,
178 OCI_ATTR_SERVER, static_cast<OCIError *>(hError))))
179 {
180 return FALSE;
181 }
182
183 if (Failed(OCIHandleAlloc(static_cast<dvoid *>(hEnv),
184 reinterpret_cast<dvoid **>(&hSession),
185 (ub4)OCI_HTYPE_SESSION, 0, nullptr)))
186 {
187 return FALSE;
188 }
189
190 if (Failed(
191 OCIAttrSet(static_cast<dvoid *>(hSession), (ub4)OCI_HTYPE_SESSION,
192 static_cast<dvoid *>(const_cast<char *>(pszUser)),
193 (ub4)strlen(pszUser), (ub4)OCI_ATTR_USERNAME, hError)))
194 {
195 return FALSE;
196 }
197
198 if (Failed(OCIAttrSet(
199 static_cast<dvoid *>(hSession), (ub4)OCI_HTYPE_SESSION,
200 static_cast<dvoid *>(const_cast<char *>(pszPasswordIn)),
201 (ub4)strlen(pszPasswordIn), (ub4)OCI_ATTR_PASSWORD, hError)))
202 {
203 return FALSE;
204 }
205
206 /* -------------------------------------------------------------------- */
207 /* Initialize Session */
208 /* -------------------------------------------------------------------- */
209
210 if (Failed(OCISessionBegin(hSvcCtx, hError, hSession, eCred,
211 (ub4)OCI_DEFAULT)))
212 {
213 CPLDebug("OCI", "OCISessionBegin() failed to initialize session");
214 return FALSE;
215 }
216
217 /* -------------------------------------------------------------------- */
218 /* Initialize Service */
219 /* -------------------------------------------------------------------- */
220
221 if (Failed(OCIAttrSet(static_cast<dvoid *>(hSvcCtx), (ub4)OCI_HTYPE_SVCCTX,
222 static_cast<dvoid *>(hSession), (ub4)0,
223 (ub4)OCI_ATTR_SESSION, hError)))
224 {
225 return FALSE;
226 }
227
228 /* -------------------------------------------------------------------- */
229 /* Create a describe handle. */
230 /* -------------------------------------------------------------------- */
231
232 if (Failed(OCIHandleAlloc(hEnv, reinterpret_cast<dvoid **>(&hDescribe),
233 (ub4)OCI_HTYPE_DESCRIBE, 0, nullptr),
234 "OCIHandleAlloc(Describe)"))
235 return FALSE;
236
237 /* -------------------------------------------------------------------- */
238 /* Try to get the MDSYS.SDO_GEOMETRY type object. */
239 /* -------------------------------------------------------------------- */
240 /* If we have no MDSYS.SDO_GEOMETRY then we consider we are
241 working along with the VRT driver and access non spatial tables.
242 See #2202 for more details (Tamas Szekeres)*/
243 if (OCIDescribeAny(
244 hSvcCtx, hError,
245 reinterpret_cast<text *>(const_cast<char *>(SDO_GEOMETRY)),
246 (ub4)strlen(SDO_GEOMETRY), OCI_OTYPE_NAME, (ub1)OCI_DEFAULT,
247 (ub1)OCI_PTYPE_TYPE, hDescribe) != OCI_ERROR)
248 {
249 hGeometryTDO = PinTDO(SDO_GEOMETRY);
250 if (hGeometryTDO == nullptr)
251 return FALSE;
252
253 /* --------------------------------------------------------------------
254 */
255 /* Try to get the MDSYS.SDO_ORDINATE_ARRAY type object. */
256 /* --------------------------------------------------------------------
257 */
258 hOrdinatesTDO = PinTDO("MDSYS.SDO_ORDINATE_ARRAY");
259 if (hOrdinatesTDO == nullptr)
260 return FALSE;
261
262 /* --------------------------------------------------------------------
263 */
264 /* Try to get the MDSYS.SDO_ELEM_INFO_ARRAY type object. */
265 /* --------------------------------------------------------------------
266 */
267 hElemInfoTDO = PinTDO("MDSYS.SDO_ELEM_INFO_ARRAY");
268 if (hElemInfoTDO == nullptr)
269 return FALSE;
270 }
271 /* -------------------------------------------------------------------- */
272 /* Record information about the session. */
273 /* -------------------------------------------------------------------- */
274 pszUserid = CPLStrdup(pszUseridIn);
275 pszPassword = CPLStrdup(pszPasswordIn);
276 pszDatabase = CPLStrdup(pszDatabaseIn);
277
278 /* -------------------------------------------------------------------- */
279 /* Get server version information */
280 /* -------------------------------------------------------------------- */
281
282 char szVersionTxt[256];
283
284 OCIServerVersion(hSvcCtx, hError, reinterpret_cast<text *>(szVersionTxt),
285 (ub4)sizeof(szVersionTxt), (ub1)OCI_HTYPE_SVCCTX);
286
287 char **papszNameValue =
288 CSLTokenizeString2(szVersionTxt, " .", CSLT_STRIPLEADSPACES);
289
290 int count = CSLCount(papszNameValue);
291
292 for (int i = 0; i < count; i++)
293 {
294 if (EQUAL(papszNameValue[i], "Release"))
295 {
296 if (i + 1 < count)
297 {
298 nServerVersion = atoi(papszNameValue[i + 1]);
299 }
300 if (i + 2 < count)
301 {
302 nServerRelease = atoi(papszNameValue[i + 2]);
303 }
304 break;
305 }
306 }
307
308 CPLDebug("OCI", "From '%s' :", szVersionTxt);
309 CPLDebug("OCI", "Version:%d", nServerVersion);
310 CPLDebug("OCI", "Release:%d", nServerRelease);
311
312 /* -------------------------------------------------------------------- */
313 /* Set maximum name length (before 12.2 ? 30 : 128) */
314 /* -------------------------------------------------------------------- */
315
316 if (nServerVersion > 12 || (nServerVersion == 12 && nServerRelease >= 2))
317 {
318 nMaxNameLength = 128;
319 }
320
321 CSLDestroy(papszNameValue);
322
323 /* -------------------------------------------------------------------- */
324 /* Setting up the OGR compatible time formatting rules. */
325 /* -------------------------------------------------------------------- */
326 OGROCIStatement oSetNLSTimeFormat(this);
327 if (oSetNLSTimeFormat.Execute(
328 "ALTER SESSION SET NLS_DATE_FORMAT='YYYY/MM/DD' \
329 NLS_TIME_FORMAT='HH24:MI:SS' NLS_TIME_TZ_FORMAT='HH24:MI:SS.FF TZHTZM' \
330 NLS_TIMESTAMP_FORMAT='YYYY/MM/DD HH24:MI:SS.FF' \
331 NLS_TIMESTAMP_TZ_FORMAT='YYYY/MM/DD HH24:MI:SS.FF TZHTZM' \
332 NLS_NUMERIC_CHARACTERS = '. '") != CE_None)
333 return OGRERR_FAILURE;
334
335 return TRUE;
336 }
337
338 /************************************************************************/
339 /* Failed() */
340 /************************************************************************/
341
342 int OGROCISession::Failed(sword nStatus, const char *pszFunction)
343
344 {
345 if (pszFunction == nullptr)
346 pszFunction = "<unnamed>";
347 if (nStatus == OCI_ERROR)
348 {
349 sb4 nErrCode = 0;
350 char szErrorMsg[10000];
351
352 szErrorMsg[0] = '\0';
353 if (hError != nullptr)
354 {
355 OCIErrorGet(static_cast<dvoid *>(hError), (ub4)1, nullptr,
356 &nErrCode, reinterpret_cast<text *>(szErrorMsg),
357 (ub4)sizeof(szErrorMsg), OCI_HTYPE_ERROR);
358 }
359 szErrorMsg[sizeof(szErrorMsg) - 1] = '\0';
360
361 CPLError(CE_Failure, CPLE_AppDefined, "%s in %s", szErrorMsg,
362 pszFunction);
363 return TRUE;
364 }
365 else if (nStatus == OCI_NEED_DATA)
366 {
367 CPLError(CE_Failure, CPLE_AppDefined, "OCI_NEED_DATA");
368 return TRUE;
369 }
370 else if (nStatus == OCI_INVALID_HANDLE)
371 {
372 CPLError(CE_Failure, CPLE_AppDefined, "OCI_INVALID_HANDLE in %s",
373 pszFunction);
374 return TRUE;
375 }
376 else if (nStatus == OCI_STILL_EXECUTING)
377 {
378 CPLError(CE_Failure, CPLE_AppDefined, "OCI_STILL_EXECUTING in %s",
379 pszFunction);
380 return TRUE;
381 }
382 else if (nStatus == OCI_CONTINUE)
383 {
384 CPLError(CE_Failure, CPLE_AppDefined, "OCI_CONTINUE in %s",
385 pszFunction);
386 return TRUE;
387 }
388 else
389 return FALSE;
390 }
391
392 /************************************************************************/
393 /* GetParamInfo() */
394 /************************************************************************/
395
396 CPLErr OGROCISession::GetParamInfo(OCIParam *hParamDesc,
397 OGRFieldDefn *poOGRDefn, ub2 *pnOCIType,
398 ub4 *pnOCILen)
399
400 {
401 ub2 nOCIType, nOCILen;
402 ub4 nColLen;
403 ub1 bOCINull;
404 char *pszColName;
405 char szTermColName[128];
406
407 /* -------------------------------------------------------------------- */
408 /* Get basic parameter details. */
409 /* -------------------------------------------------------------------- */
410 if (Failed(OCIAttrGet(hParamDesc, OCI_DTYPE_PARAM, &nOCIType, nullptr,
411 OCI_ATTR_DATA_TYPE, hError),
412 "OCIAttrGet(Type)"))
413 return CE_Failure;
414
415 if (Failed(OCIAttrGet(hParamDesc, OCI_DTYPE_PARAM, &nOCILen, nullptr,
416 OCI_ATTR_DATA_SIZE, hError),
417 "OCIAttrGet(Size)"))
418 return CE_Failure;
419
420 if (Failed(OCIAttrGet(hParamDesc, OCI_DTYPE_PARAM, &pszColName, &nColLen,
421 OCI_ATTR_NAME, hError),
422 "OCIAttrGet(Name)"))
423 return CE_Failure;
424
425 if (Failed(OCIAttrGet(hParamDesc, OCI_DTYPE_PARAM, &bOCINull, nullptr,
426 OCI_ATTR_IS_NULL, hError),
427 "OCIAttrGet(Null)"))
428 return CE_Failure;
429
430 if (nColLen >= sizeof(szTermColName))
431 {
432 CPLError(CE_Failure, CPLE_AppDefined,
433 "Column length (%d) longer than column name buffer (%d) in\n"
434 "OGROCISession::GetParamInfo()",
435 nColLen, (int)sizeof(szTermColName));
436 return CE_Failure;
437 }
438
439 strncpy(szTermColName, pszColName, nColLen);
440 szTermColName[nColLen] = '\0';
441
442 poOGRDefn->SetName(szTermColName);
443 poOGRDefn->SetNullable(bOCINull);
444
445 /* -------------------------------------------------------------------- */
446 /* Attempt to classify as an OGRType. */
447 /* -------------------------------------------------------------------- */
448 switch (nOCIType)
449 {
450 case SQLT_CHR:
451 case SQLT_AFC: /* CHAR(), NCHAR() */
452 poOGRDefn->SetType(OFTString);
453 if (nOCILen <= 4000)
454 poOGRDefn->SetWidth(nOCILen);
455 break;
456
457 case SQLT_NUM:
458 {
459 // NOTE: OCI docs say this should be ub1 type, but we have
460 // determined that oracle is actually returning a short so we
461 // use that type and try to compensate for possible problems by
462 // initializing, and dividing by 256 if it is large.
463 unsigned short byPrecision = 0;
464 sb1 nScale;
465
466 if (Failed(OCIAttrGet(hParamDesc, OCI_DTYPE_PARAM, &byPrecision,
467 nullptr, OCI_ATTR_PRECISION, hError),
468 "OCIAttrGet(Precision)"))
469 return CE_Failure;
470 if (Failed(OCIAttrGet(hParamDesc, OCI_DTYPE_PARAM, &nScale, nullptr,
471 OCI_ATTR_SCALE, hError),
472 "OCIAttrGet(Scale)"))
473 return CE_Failure;
474 #ifdef notdef
475 CPLDebug("OCI", "%s: Scale=%d, Precision=%d", szTermColName, nScale,
476 byPrecision);
477 #endif
478 if (byPrecision > 255)
479 byPrecision = byPrecision / 256;
480
481 if (nScale < 0)
482 poOGRDefn->SetType(OFTReal);
483 else if (nScale > 0)
484 {
485 poOGRDefn->SetType(OFTReal);
486 poOGRDefn->SetWidth(byPrecision);
487 poOGRDefn->SetPrecision(nScale);
488 }
489 else if (byPrecision < 38)
490 {
491 poOGRDefn->SetType((byPrecision < 10) ? OFTInteger
492 : OFTInteger64);
493 poOGRDefn->SetWidth(byPrecision);
494 }
495 else
496 {
497 poOGRDefn->SetType(OFTInteger64);
498 }
499 }
500 break;
501
502 case SQLT_DAT:
503 case SQLT_DATE:
504 poOGRDefn->SetType(OFTDate);
505 break;
506 case SQLT_TIMESTAMP:
507 case SQLT_TIME:
508 poOGRDefn->SetType(OFTDateTime);
509 break;
510 case SQLT_TIMESTAMP_TZ:
511 case SQLT_TIMESTAMP_LTZ:
512 case SQLT_TIME_TZ:
513 poOGRDefn->SetType(OFTDateTime);
514 // Indicates that there's timezones. They might not actually be
515 // mixed !
516 poOGRDefn->SetTZFlag(OGR_TZFLAG_MIXED_TZ);
517 break;
518
519 case SQLT_RID:
520 case SQLT_BIN:
521 case SQLT_LBI:
522 case 111: /* REF */
523 case SQLT_CLOB:
524 case SQLT_BLOB:
525 case SQLT_FILE:
526 case 208: /* UROWID */
527 poOGRDefn->SetType(OFTBinary);
528 break;
529
530 default:
531 poOGRDefn->SetType(OFTBinary);
532 break;
533 }
534
535 if (pnOCIType != nullptr)
536 *pnOCIType = nOCIType;
537
538 if (pnOCILen != nullptr)
539 *pnOCILen = nOCILen;
540
541 return CE_None;
542 }
543
544 /************************************************************************/
545 /* CleanName() */
546 /* */
547 /* Modify a name in-place to be a well formed Oracle name. */
548 /************************************************************************/
549
550 void OGROCISession::CleanName(char *pszName)
551
552 {
553 int i;
554
555 if (strlen(pszName) > nMaxNameLength)
556 pszName[nMaxNameLength] = '\0';
557
558 for (i = 0; pszName[i] != '\0'; i++)
559 {
560 pszName[i] = static_cast<char>(
561 CPLToupper(static_cast<unsigned char>(pszName[i])));
562
563 if ((pszName[i] < '0' || pszName[i] > '9') &&
564 (pszName[i] < 'A' || pszName[i] > 'Z') && pszName[i] != '_')
565 pszName[i] = '_';
566 }
567 }
568
569 /************************************************************************/
570 /* PinTDO() */
571 /* */
572 /* Fetch a Type Description Object for the named type. */
573 /************************************************************************/
574
575 OCIType *OGROCISession::PinTDO(const char *pszType)
576
577 {
578 OCIParam *hGeomParam = nullptr;
579 OCIRef *hGeomTypeRef = nullptr;
580 OCIType *hPinnedTDO = nullptr;
581
582 if (Failed(OCIDescribeAny(
583 hSvcCtx, hError,
584 reinterpret_cast<text *>(const_cast<char *>(pszType)),
585 (ub4)strlen(pszType), OCI_OTYPE_NAME, (ub1)1,
586 (ub1)OCI_PTYPE_TYPE, hDescribe),
587 "GetTDO()->OCIDescribeAny()"))
588 return nullptr;
589
590 if (Failed(OCIAttrGet(static_cast<dvoid *>(hDescribe),
591 (ub4)OCI_HTYPE_DESCRIBE,
592 static_cast<dvoid *>(&hGeomParam), nullptr,
593 (ub4)OCI_ATTR_PARAM, hError),
594 "GetTDO()->OCIGetAttr(ATTR_PARAM)"))
595 return nullptr;
596
597 if (Failed(OCIAttrGet(static_cast<dvoid *>(hGeomParam),
598 (ub4)OCI_DTYPE_PARAM,
599 static_cast<dvoid *>(&hGeomTypeRef), nullptr,
600 (ub4)OCI_ATTR_REF_TDO, hError),
601 "GetTDO()->OCIAttrGet(ATTR_REF_TDO)"))
602 return nullptr;
603
604 if (Failed(OCIObjectPin(hEnv, hError, hGeomTypeRef, nullptr, OCI_PIN_ANY,
605 OCI_DURATION_SESSION, OCI_LOCK_NONE,
606 reinterpret_cast<dvoid **>(&hPinnedTDO)),
607 "GetTDO()->OCIObjectPin()"))
608 return nullptr;
609
610 return hPinnedTDO;
611 }