forked from laurenz/oracle_fdw
-
Notifications
You must be signed in to change notification settings - Fork 2
/
oracle_utils.c
3310 lines (2958 loc) · 98.7 KB
/
oracle_utils.c
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-------------------------------------------------------------------------
*
* oracle_utils.c
* routines that use OCI (Oracle's C API)
*
*-------------------------------------------------------------------------
*/
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#if defined _WIN32 || defined _WIN64
/* for getpid */
#include <process.h>
/* Windows doesn't have snprintf */
#define snprintf _snprintf
#endif
/* Oracle header */
#include <oci.h>
#include "oracle_fdw.h"
/* number of bytes to read per LOB chunk */
#define LOB_CHUNK_SIZE 8132
/* emit no error messages when set, used for shutdown */
static volatile int silent = 0;
/* contains Oracle error messages, set by checkerr() */
#define ERRBUFSIZE 500
static __thread char oraMessage[ERRBUFSIZE];
static __thread sb4 err_code;
/* set to "1" as soon as OCIEnvCreate is called */
static volatile int oci_initialized = 0;
/*
* Linked list for temporary Oracle handles and descriptors.
* Stores statement and describe handles as well as timetamp and LOB descriptors.
* Other handles are stored in the handle cache below.
*/
struct handleEntry
{
dvoid *handlep;
ub4 type;
int isDescriptor;
struct handleEntry *next;
};
/*
* Linked list of handles for cached Oracle connections.
*/
static struct envEntry *envlist = NULL;
/*
* NULL value used for "in" callback in RETURNING clauses.
*/
static ora_geometry null_geometry = { NULL, NULL, -1, NULL, -1, NULL };
/* error handle for oracle api. It's not only used by oracle session. */
static OCIError *errhp = (OCIError *)NULL;
/*
* Helper functions
*/
static void getServerVersion(struct srvEntry *srvp, OCIError *errhp);
static void oracleSetSavepoint(oracleSession *session, int nest_level);
static void setOracleEnvironment(char *nls_lang);
static void oracleQueryPlan(oracleSession *session, const char *query, const char *desc_query, int nres, dvoid **res, sb4 *res_size, ub2 *res_type, ub2 *res_len, sb2 *res_ind);
static sword checkerr(sword status, dvoid *handle, ub4 handleType);
static char *copyOraText(const char *string, int size, int quote);
static void closeSession(OCIEnv *envhp, OCIServer *srvhp, OCISession *userhp, int disconnect);
static void disconnectServer(OCIEnv *envhp, OCIServer *srvhp);
static void removeEnvironment(OCIEnv *envhp);
static void allocHandle(dvoid **handlep, ub4 type, int isDescriptor, OCIEnv *envhp, struct connEntry *connp, oraError error, const char *errmsg);
static void freeHandle(dvoid *handlep, struct connEntry *connp);
static ub2 getOraType(oraType arg);
static sb4 bind_out_callback(void *octxp, OCIBind *bindp, ub4 iter, ub4 index, void **bufpp, ub4 **alenp, ub1 *piecep, void **indp, ub2 **rcodep);
static sb4 bind_in_callback(void *ictxp, OCIBind *bindp, ub4 iter, ub4 index, void **bufpp, ub4 *alenp, ub1 *piecep, void **indpp);
static void setNullGeometry(oracleSession *session, ora_geometry *geom);
/*
* oracleGetSession
* Look up an Oracle connection in the cache, create a new one if there is none.
* The result is a palloc'ed data structure containing the connection.
* "curlevel" is the current PostgreSQL transaction level.
*/
oracleSession
*oracleGetSession(
const char *connectstring, oraIsoLevel isolation_level, char *user, char *password,
const char *nls_lang, int have_nchar, const char *tablename, int curlevel)
{
OCIEnv *envhp = NULL;
OCISvcCtx *svchp = NULL;
OCIServer *srvhp = NULL;
OCISession *userhp = NULL;
OCITrans *txnhp = NULL;
oracleSession *session;
struct envEntry *envp;
struct srvEntry *srvp;
struct connEntry *connp;
char pid[30], *nlscopy = NULL;
ub4 is_connected;
int retry = 1, i;
ub4 isolevel = OCI_TRANS_SERIALIZABLE;
/* convert isolation_level to Oracle OCI value */
switch(isolation_level)
{
case ORA_TRANS_SERIALIZABLE:
isolevel = OCI_TRANS_SERIALIZABLE;
break;
case ORA_TRANS_READ_COMMITTED:
isolevel = OCI_TRANS_NEW;
break;
case ORA_TRANS_READ_ONLY:
isolevel = OCI_TRANS_READONLY;
break;
}
/* it's easier to deal with empty strings */
if (!connectstring)
connectstring = "";
if (!user)
user = "";
if (!password)
password = "";
if (!nls_lang)
nls_lang = "";
/*
* Check if PostGIS is installed and initialize GEOMETRYOID if it is.
* We have to initialize that here rather than in _PG_init()
* because we need to look up system catalogs.
*/
initializePostGIS();
/* search environment and server handle in cache */
for (envp = envlist; envp != NULL; envp = envp->next)
{
if (strcmp(envp->nls_lang, nls_lang) == 0)
{
envhp = envp->envhp;
errhp = envp->errhp;
break;
}
}
if (envhp == NULL)
{
ub4 handle_mode;
/*
* Create environment and error handle.
*/
/* create persistent copy of "nls_lang" */
if ((nlscopy = strdup(nls_lang)) == NULL)
oracleError_i(FDW_OUT_OF_MEMORY,
"error connecting to Oracle: failed to allocate %d bytes of memory",
strlen(nls_lang) + 1);
/* set Oracle environment */
setOracleEnvironment(nlscopy);
/* use OCI_NCHAR_LITERAL_REPLACE_ON only if we have to convert national characters */
if (have_nchar)
handle_mode = OCI_OBJECT | OCI_NCHAR_LITERAL_REPLACE_ON;
else
handle_mode = OCI_OBJECT;
#ifdef OCI_THREAD_SAFE
/*
* Enable thread safety feature of OCI libraries,
* it allows Oracle connection can be used in multithread environment.
*/
handle_mode = handle_mode | OCI_THREADED;
#endif
/* create environment handle */
if (checkerr(
OCIEnvCreate((OCIEnv **) &envhp, handle_mode,
(dvoid *) 0, (dvoid * (*)(dvoid *,size_t)) 0,
(dvoid * (*)(dvoid *, dvoid *, size_t)) 0,
(void (*)(dvoid *, dvoid *)) 0, (size_t) 0, (dvoid **) 0),
(dvoid *)envhp, OCI_HTYPE_ENV) != 0)
{
free(nlscopy);
oracleError_d(FDW_UNABLE_TO_ESTABLISH_CONNECTION,
"error connecting to Oracle: OCIEnvCreate failed to create environment handle",
oraMessage);
}
/* we can call OCITerminate now */
oci_initialized = 1;
/*
* Oracle overwrites PostgreSQL's signal handlers, so we have to restore them.
* Oracle's SIGINT handler is ok (it cancels the query), but we must do something
* reasonable for SIGTERM.
*/
oracleSetHandlers();
/* allocate error handle */
if (checkerr(
OCIHandleAlloc((dvoid *) envhp, (dvoid **) &errhp,
(ub4) OCI_HTYPE_ERROR,
(size_t) 0, (dvoid **) 0),
(dvoid *)envhp, OCI_HTYPE_ENV) != OCI_SUCCESS)
{
free(nlscopy);
oracleError_d(FDW_UNABLE_TO_ESTABLISH_CONNECTION,
"error connecting to Oracle: OCIHandleAlloc failed to allocate error handle",
oraMessage);
}
/* add handles to cache */
if ((envp = malloc(sizeof(struct envEntry))) == NULL)
{
oracleError_i(FDW_OUT_OF_MEMORY,
"error connecting to Oracle: failed to allocate %d bytes of memory",
sizeof(struct envEntry));
}
envp->nls_lang = nlscopy;
envp->envhp = envhp;
envp->errhp = errhp;
envp->srvlist = NULL;
envp->next = envlist;
envlist = envp;
}
/* search connect string in cache */
for (srvp = envp->srvlist; srvp != NULL; srvp = srvp->next)
{
if (strcmp(srvp->connectstring, connectstring) == 0)
{
srvhp = srvp->srvhp;
break;
}
}
if (srvp != NULL)
{
/*
* Test if we are still connected.
* If not, clean up the mess.
*/
if (checkerr(
OCIAttrGet((dvoid *)srvhp, (ub4)OCI_HTYPE_SERVER,
(dvoid *)&is_connected, (ub4 *)0, (ub4)OCI_ATTR_SERVER_STATUS, errhp),
(dvoid *)errhp, OCI_HTYPE_ERROR) != OCI_SUCCESS)
{
oracleError_d(FDW_UNABLE_TO_CREATE_REPLY,
"error connecting to Oracle: OCIAttrGet failed to get connection status",
oraMessage);
}
if (is_connected == OCI_SERVER_NOT_CONNECTED)
{
/* clean up */
silent = 1;
while (srvp->connlist != NULL)
{
closeSession(envhp, srvhp, srvp->connlist->userhp, 0);
}
disconnectServer(envhp, srvhp);
silent = 0;
srvp = NULL;
}
}
retry_connect:
if (srvp == NULL)
{
/*
* No cache entry was found, we have to create a new server connection.
*/
/* create new server handle */
if (checkerr(
OCIHandleAlloc((dvoid *) envhp, (dvoid **) &srvhp,
(ub4) OCI_HTYPE_SERVER,
(size_t) 0, (dvoid **) 0),
(dvoid *)envhp, OCI_HTYPE_ENV) != OCI_SUCCESS)
{
oracleError_d(FDW_UNABLE_TO_ESTABLISH_CONNECTION,
"error connecting to Oracle: OCIHandleAlloc failed to allocate server handle",
oraMessage);
}
/* connect to the Oracle server */
if (checkerr(
OCIServerAttach(srvhp, errhp, (text *)connectstring, strlen(connectstring), 0),
(dvoid *)errhp, OCI_HTYPE_ERROR) != OCI_SUCCESS)
{
if (tablename)
oracleError_sd(FDW_UNABLE_TO_ESTABLISH_CONNECTION,
"connection for foreign table \"%s\" cannot be established", tablename,
oraMessage);
else
oracleError_d(FDW_UNABLE_TO_ESTABLISH_CONNECTION,
"cannot connect to foreign Oracle server",
oraMessage);
}
/* add server handle to cache */
if ((srvp = malloc(sizeof(struct srvEntry))) == NULL)
{
oracleError_i(FDW_OUT_OF_MEMORY,
"error connecting to Oracle: failed to allocate %d bytes of memory",
sizeof(struct srvEntry));
}
if ((srvp->connectstring = strdup(connectstring)) == NULL)
{
oracleError_i(FDW_OUT_OF_MEMORY,
"error connecting to Oracle: failed to allocate %d bytes of memory",
strlen(connectstring) + 1);
}
srvp->srvhp = srvhp;
srvp->next = envp->srvlist;
srvp->connlist = NULL;
envp->srvlist = srvp;
}
/* search user session for this server in cache */
for (connp = srvp->connlist; connp != NULL; connp = connp->next)
{
if (strcmp(connp->user, user) == 0)
{
svchp = connp->svchp;
userhp = connp->userhp;
break;
}
}
if (userhp == NULL)
{
/*
* If no cached user session was found, authenticate.
*/
/* allocate service handle */
if (checkerr(
OCIHandleAlloc((dvoid *) envhp, (dvoid **) &svchp,
(ub4) OCI_HTYPE_SVCCTX,
(size_t) 0, (dvoid **) 0),
(dvoid *)envhp, OCI_HTYPE_ENV) != OCI_SUCCESS)
{
free(nlscopy);
oracleError_d(FDW_UNABLE_TO_ESTABLISH_CONNECTION,
"error connecting to Oracle: OCIHandleAlloc failed to allocate service handle",
oraMessage);
}
/* set server handle in service handle */
if (checkerr(
OCIAttrSet(svchp, OCI_HTYPE_SVCCTX, srvhp, 0,
OCI_ATTR_SERVER, errhp),
(dvoid *)errhp, OCI_HTYPE_ERROR) != OCI_SUCCESS)
{
oracleError_d(FDW_UNABLE_TO_ESTABLISH_CONNECTION,
"error connecting to Oracle: OCIAttrSet failed to set server handle in service handle",
oraMessage);
}
/* create transaction handle */
if (checkerr(
OCIHandleAlloc((dvoid *) envhp, (dvoid **) &txnhp,
(ub4) OCI_HTYPE_TRANS,
(size_t) 0, (dvoid **) 0),
(dvoid *)envhp, OCI_HTYPE_ENV) != OCI_SUCCESS)
{
oracleError_d(FDW_UNABLE_TO_ESTABLISH_CONNECTION,
"error connecting to Oracle: OCIHandleAlloc failed to allocate transaction handle",
oraMessage);
}
/* set transaction handle in service handle */
if (checkerr(
OCIAttrSet(svchp, OCI_HTYPE_SVCCTX, txnhp, 0,
OCI_ATTR_TRANS, errhp),
(dvoid *)errhp, OCI_HTYPE_ERROR) != OCI_SUCCESS)
{
oracleError_d(FDW_UNABLE_TO_ESTABLISH_CONNECTION,
"error connecting to Oracle: OCIAttrSet failed to set transaction handle in service handle",
oraMessage);
}
/* create session handle */
if (checkerr(
OCIHandleAlloc((dvoid *) envhp, (dvoid **) &userhp,
(ub4) OCI_HTYPE_SESSION,
(size_t) 0, (dvoid **) 0),
(dvoid *)envhp, OCI_HTYPE_ENV) != OCI_SUCCESS)
{
oracleError_d(FDW_UNABLE_TO_ESTABLISH_CONNECTION,
"error connecting to Oracle: OCIHandleAlloc failed to allocate session handle",
oraMessage);
}
/* set MODULE for the Oracle session */
sprintf(pid, "%lu", (unsigned long)getpid());
pid[29] = '\0';
if (checkerr(
OCIAttrSet(userhp, OCI_HTYPE_SESSION, "postgres", (ub4)8,
OCI_ATTR_MODULE, errhp),
(dvoid *)errhp, OCI_HTYPE_ERROR) != OCI_SUCCESS)
{
oracleError_d(FDW_UNABLE_TO_ESTABLISH_CONNECTION,
"error connecting to Oracle: OCIAttrSet failed to set module in session handle",
oraMessage);
}
/* set ACTION for the Oracle session */
if (checkerr(
OCIAttrSet(userhp, OCI_HTYPE_SESSION, pid, (ub4)strlen(pid),
OCI_ATTR_ACTION, errhp),
(dvoid *)errhp, OCI_HTYPE_ERROR) != OCI_SUCCESS)
{
oracleError_d(FDW_UNABLE_TO_ESTABLISH_CONNECTION,
"error connecting to Oracle: OCIAttrSet failed to set action in session handle",
oraMessage);
}
/* set driver name for the Oracle session */
if (checkerr(
OCIAttrSet(userhp, OCI_HTYPE_SESSION, "oracle_fdw", (ub4)10,
OCI_ATTR_DRIVER_NAME, errhp),
(dvoid *)errhp, OCI_HTYPE_ERROR) != OCI_SUCCESS)
{
oracleError_d(FDW_UNABLE_TO_ESTABLISH_CONNECTION,
"error connecting to Oracle: OCIAttrSet failed to set driver name in session handle",
oraMessage);
}
/* set user name */
if (checkerr(
OCIAttrSet(userhp, OCI_HTYPE_SESSION, user, strlen(user),
OCI_ATTR_USERNAME, errhp),
(dvoid *)errhp, OCI_HTYPE_ERROR) != OCI_SUCCESS)
{
oracleError_d(FDW_UNABLE_TO_ESTABLISH_CONNECTION,
"error connecting to Oracle: OCIAttrSet failed to set user name in session handle",
oraMessage);
}
/* set password */
if (checkerr(
OCIAttrSet(userhp, OCI_HTYPE_SESSION, password, strlen(password),
OCI_ATTR_PASSWORD, errhp),
(dvoid *)errhp, OCI_HTYPE_ERROR) != OCI_SUCCESS)
{
oracleError_d(FDW_UNABLE_TO_ESTABLISH_CONNECTION,
"error connecting to Oracle: OCIAttrSet failed to set password in session handle",
oraMessage);
}
/* authenticate; use external authentication if no username has been supplied */
if (checkerr(
OCISessionBegin(svchp, errhp, userhp,
(user[0] == '\0' ? OCI_CRED_EXT : OCI_CRED_RDBMS), OCI_DEFAULT),
(dvoid *)errhp, OCI_HTYPE_ERROR) != OCI_SUCCESS)
{
if (tablename)
oracleError_sd(FDW_UNABLE_TO_ESTABLISH_CONNECTION,
"connection for foreign table \"%s\" cannot be authenticated", tablename,
oraMessage);
else
oracleError_d(FDW_UNABLE_TO_ESTABLISH_CONNECTION,
"cannot authenticate connection to foreign Oracle server",
oraMessage);
}
/* set session handle in service handle */
if (checkerr(
OCIAttrSet(svchp, OCI_HTYPE_SVCCTX, userhp, 0,
OCI_ATTR_SESSION, errhp),
(dvoid *)errhp, OCI_HTYPE_ERROR) != OCI_SUCCESS)
{
oracleError_d(FDW_UNABLE_TO_ESTABLISH_CONNECTION,
"error connecting to Oracle: OCIAttrSet failed to set session handle in service handle",
oraMessage);
}
/* store the server version in the service handle cache */
getServerVersion(srvp, errhp);
/* add session handle to cache */
if ((connp = malloc(sizeof(struct connEntry))) == NULL)
{
oracleError_i(FDW_OUT_OF_MEMORY,
"error connecting to Oracle: failed to allocate %d bytes of memory",
sizeof(struct connEntry));
}
if ((connp->user = strdup(user)) == NULL)
{
oracleError_i(FDW_OUT_OF_MEMORY,
"error connecting to Oracle: failed to allocate %d bytes of memory",
strlen(user) + 1);
}
connp->svchp = svchp;
connp->userhp = userhp;
connp->geomtype = NULL;
connp->handlelist = NULL;
connp->xact_level = 0;
connp->next = srvp->connlist;
srvp->connlist = connp;
/* register callback for PostgreSQL transaction events */
oracleRegisterCallback(connp);
}
if (connp->xact_level <= 0)
{
oracleDebug2("oracle_fdw: begin remote transaction");
/* start a transaction */
if (checkerr(
OCITransStart(svchp, errhp, (uword)0, isolevel),
(dvoid *)errhp, OCI_HTYPE_ERROR) != OCI_SUCCESS)
{
/*
* Certain Oracle errors mean that the session or the server connection
* got terminated. Retry once in that case.
*/
if (retry && (err_code == 1012 || err_code == 28 || err_code == 3113 || err_code == 3135))
{
oracleDebug2("oracle_fdw: session has been terminated, try to reconnect");
silent = 1;
while (srvp->connlist != NULL)
{
closeSession(envhp, srvhp, srvp->connlist->userhp, 0);
}
disconnectServer(envhp, srvhp);
silent = 0;
srvp = NULL;
userhp = NULL;
retry = 0;
goto retry_connect;
}
else
oracleError_d(FDW_UNABLE_TO_ESTABLISH_CONNECTION,
"error connecting to Oracle: OCITransStart failed to start a transaction",
oraMessage);
}
connp->xact_level = 1;
}
/* palloc a data structure pointing to the cached entries */
session = oracleAlloc(sizeof(struct oracleSession));
session->envp = envp;
session->srvp = srvp;
session->connp = connp;
session->stmthp = NULL;
session->have_nchar = have_nchar;
for (i=0; i<5; ++i)
session->server_version[i] = srvp->server_version[i];
/* set savepoints up to the current level */
oracleSetSavepoint(session, curlevel);
return session;
}
/*
* oracleCloseStatement
* Close any open statement associated with the session.
*/
void
oracleCloseStatement(oracleSession *session)
{
/* free statement handle, if it exists */
if (session->stmthp != NULL)
{
/* free the statement handle */
freeHandle(session->stmthp, session->connp);
session->stmthp = NULL;
}
}
/*
* oracleCloseConnections
* Close everything in the cache.
*/
void
oracleCloseConnections(void)
{
while (envlist != NULL)
{
while (envlist->srvlist != NULL)
{
while (envlist->srvlist->connlist != NULL)
{
closeSession(envlist->envhp, envlist->srvlist->srvhp, envlist->srvlist->connlist->userhp, 0);
}
disconnectServer(envlist->envhp, envlist->srvlist->srvhp);
}
removeEnvironment(envlist->envhp);
}
}
/*
* oracleShutdown
* Close all open connections, free handles, terminate Oracle.
* This will be called at the end of the PostgreSQL session.
*/
void
oracleShutdown(void)
{
/* don't report error messages */
silent = 1;
oracleCloseConnections();
/* done with Oracle */
if (oci_initialized)
(void)OCITerminate(OCI_DEFAULT);
}
/*
* oracleCancel
* Cancel all running Oracle queries.
*/
void
oracleCancel(void)
{
struct envEntry *envp;
struct srvEntry *srvp;
/* send a cancel request for all servers ignoring errors */
for (envp = envlist; envp != NULL; envp = envp->next)
for (srvp = envp->srvlist; srvp != NULL; srvp = srvp->next)
(void)OCIBreak(srvp->srvhp, envp->errhp);
}
/*
* oracleEndTransaction
* Commit or rollback the transaction.
* The first argument must be a connEntry.
* If "noerror" is true, don't throw errors.
*/
void oracleEndTransaction(void *arg, int is_commit, int noerror)
{
struct connEntry *connp = NULL;
struct srvEntry *srvp = NULL;
struct envEntry *envp = NULL;
int found = 0;
/* do nothing if there is no transaction */
if (((struct connEntry *)arg)->xact_level == 0)
return;
/* find the cached handles for the argument */
envp = envlist;
while (envp)
{
srvp = envp->srvlist;
while (srvp)
{
connp = srvp->connlist;
while (connp)
{
if (connp == (struct connEntry *)arg)
{
found = 1;
break;
}
connp = connp->next;
}
if (found)
break;
srvp = srvp->next;
}
if (found)
break;
envp = envp->next;
}
if (! found)
oracleError(FDW_ERROR, "oracleEndTransaction internal error: handle not found in cache");
/* free handles */
while (connp->handlelist != NULL)
freeHandle(connp->handlelist->handlep, connp);
/* free objects in cache (might be left behind in case of errors) */
(void)OCICacheFree(envp->envhp, envp->errhp, NULL);
/* null_geometry has been freed */
null_geometry.geometry = NULL;
null_geometry.indicator = NULL;
/* commit or rollback */
if (is_commit)
{
oracleDebug2("oracle_fdw: commit remote transaction");
if (checkerr(
OCITransCommit(connp->svchp, envp->errhp, OCI_DEFAULT),
(dvoid *)envp->errhp, OCI_HTYPE_ERROR) != OCI_SUCCESS && !noerror)
{
oracleError_d(FDW_UNABLE_TO_CREATE_EXECUTION,
"error committing transaction: OCITransCommit failed",
oraMessage);
}
}
else
{
oracleDebug2("oracle_fdw: roll back remote transaction");
if (checkerr(
OCITransRollback(connp->svchp, envp->errhp, OCI_DEFAULT),
(dvoid *)envp->errhp, OCI_HTYPE_ERROR) != OCI_SUCCESS && !noerror)
{
oracleError_d(FDW_UNABLE_TO_CREATE_EXECUTION,
"error rolling back transaction: OCITransRollback failed",
oraMessage);
}
}
connp->xact_level = 0;
}
/*
* oracleEndSubtransaction
* Commit or rollback all subtransaction up to savepoint "nest_nevel".
* The first argument must be a connEntry.
* If "is_commit" is not true, rollback.
*/
void
oracleEndSubtransaction(void *arg, int nest_level, int is_commit)
{
char query[50], message[60];
struct connEntry *connp = NULL;
struct srvEntry *srvp = NULL;
struct envEntry *envp = NULL;
OCIStmt *stmthp = NULL;
int found = 0;
/* do nothing if the transaction level is lower than nest_level */
if (((struct connEntry *)arg)->xact_level < nest_level)
return;
((struct connEntry *)arg)->xact_level = nest_level - 1;
if (is_commit)
{
/*
* There is nothing to do as savepoints don't get released in Oracle:
* Setting the same savepoint again just overwrites the previous one.
*/
return;
}
/* find the cached handles for the argument */
envp = envlist;
while (envp)
{
srvp = envp->srvlist;
while (srvp)
{
connp = srvp->connlist;
while (connp)
{
if (connp == (struct connEntry *)arg)
{
found = 1;
break;
}
connp = connp->next;
}
if (found)
break;
srvp = srvp->next;
}
if (found)
break;
envp = envp->next;
}
if (! found)
oracleError(FDW_ERROR, "oracleRollbackSavepoint internal error: handle not found in cache");
snprintf(message, 59, "oracle_fdw: rollback to savepoint s%d", nest_level);
oracleDebug2(message);
snprintf(query, 49, "ROLLBACK TO SAVEPOINT s%d", nest_level);
/* create statement handle */
allocHandle((void **)&stmthp, OCI_HTYPE_STMT, 0, envp->envhp, connp,
FDW_OUT_OF_MEMORY,
"error rolling back to savepoint: OCIHandleAlloc failed to allocate statement handle");
/* prepare the query */
if (checkerr(
OCIStmtPrepare(stmthp, envp->errhp, (text *)query, (ub4) strlen(query),
(ub4) OCI_NTV_SYNTAX, (ub4) OCI_DEFAULT),
(dvoid *)envp->errhp, OCI_HTYPE_ERROR) != OCI_SUCCESS)
{
oracleError_d(FDW_UNABLE_TO_CREATE_EXECUTION,
"error rolling back to savepoint: OCIStmtPrepare failed to prepare rollback statement",
oraMessage);
}
/* rollback to savepoint */
if (checkerr(
OCIStmtExecute(connp->svchp, stmthp, envp->errhp, (ub4)1, (ub4)0,
(CONST OCISnapshot *)NULL, (OCISnapshot *)NULL, OCI_DEFAULT),
(dvoid *)envp->errhp, OCI_HTYPE_ERROR) != OCI_SUCCESS)
{
oracleError_d(FDW_UNABLE_TO_CREATE_EXECUTION,
"error rolling back to savepoint: OCIStmtExecute failed on ROLLBACK TO SAVEPOINT",
oraMessage);
}
/* free statement handle */
freeHandle(stmthp, connp);
}
/*
* oracleIsStatementOpen
* Return 1 if there is a statement handle, else 0.
*/
int
oracleIsStatementOpen(oracleSession *session)
{
return (session->stmthp != NULL);
}
/*
* oracleCreateTableName
* Return table name.
*/
char
*oracleCreateTableName(char *dblink, char *schema,char *table)
{
char *qtable, *qdblink = NULL, *qschema = NULL, *tablename;
int length;
/* get a complete quoted table name */
qtable = copyOraText(table, strlen(table), 1);
length = strlen(qtable);
if (dblink != NULL)
{
qdblink = copyOraText(dblink, strlen(dblink), 1);
length += strlen(qdblink) + 1;
}
if (schema != NULL)
{
qschema = copyOraText(schema, strlen(schema), 1);
length += strlen(qschema) + 1;
}
tablename = oracleAlloc(length + 1);
tablename[0] = '\0'; /* empty */
if (schema != NULL)
{
strcat(tablename, qschema);
strcat(tablename, ".");
}
strcat(tablename, qtable);
if (dblink != NULL)
{
strcat(tablename, "@");
strcat(tablename, qdblink);
}
oracleFree(qtable);
if (dblink != NULL)
oracleFree(qdblink);
if (schema != NULL)
oracleFree(qschema);
return tablename;
}
/*
* oracleDescribe
* Find the remote Oracle table and describe it.
* Returns a palloc'ed data structure with the results.
*/
struct oraTable
*oracleDescribe(oracleSession *session, char *query, char *tablename, char *pgname, long max_long)
{
struct oraTable *reply;
OCIStmt *stmthp;
OCIParam *colp;
ub2 oraType, charsize, bin_size;
ub1 csfrm;
sb2 precision;
sb1 scale;
OraText *ident, *typname, *typschema;
char *type_name, *type_schema;
ub4 ncols, ident_size, typname_size, typschema_size;
int i;
/* create statement handle */
allocHandle((void **)&stmthp, OCI_HTYPE_STMT, 0, session->envp->envhp, session->connp,
FDW_UNABLE_TO_CREATE_REPLY,
"error describing remote table: OCIHandleAlloc failed to allocate statement handle");
/* prepare the query */
if (checkerr(
OCIStmtPrepare(stmthp, session->envp->errhp, (text *)query, (ub4) strlen(query),
(ub4) OCI_NTV_SYNTAX, (ub4) OCI_DEFAULT),
(dvoid *)session->envp->errhp, OCI_HTYPE_ERROR) != OCI_SUCCESS)
{
oracleError_d(FDW_UNABLE_TO_CREATE_REPLY,
"error describing remote table: OCIStmtPrepare failed to prepare query",
oraMessage);
}
if (checkerr(
OCIStmtExecute(session->connp->svchp, stmthp, session->envp->errhp, (ub4)0, (ub4)0,
(CONST OCISnapshot *)NULL, (OCISnapshot *)NULL, OCI_DESCRIBE_ONLY),
(dvoid *)session->envp->errhp, OCI_HTYPE_ERROR) != OCI_SUCCESS)
{
if (err_code == 942)
oracleError_ssdh(FDW_TABLE_NOT_FOUND,
"Oracle table %s for foreign table \"%s\" does not exist or does not allow read access", tablename, pgname,
oraMessage, "Oracle table names are case sensitive (normally all uppercase).");
else
oracleError_d(FDW_UNABLE_TO_CREATE_REPLY,
"error describing remote table: OCIStmtExecute failed to describe table",
oraMessage);
}
/* allocate an oraTable struct for the results */
reply = oracleAlloc(sizeof(struct oraTable));
reply->name = tablename;
reply->pgname = pgname;
reply->npgcols = 0;
/* get the number of columns */
if (checkerr(
OCIAttrGet((dvoid *)stmthp, (ub4)OCI_HTYPE_STMT,
(dvoid *)&ncols, (ub4 *)0, (ub4)OCI_ATTR_PARAM_COUNT, session->envp->errhp),
(dvoid *)session->envp->errhp, OCI_HTYPE_ERROR) != OCI_SUCCESS)
{
oracleError_d(FDW_UNABLE_TO_CREATE_REPLY,
"error describing remote table: OCIAttrGet failed to get number of columns",
oraMessage);
}
reply->ncols = ncols;
reply->cols = (struct oraColumn **)oracleAlloc(sizeof(struct oraColumn *) * reply->ncols);
/* loop through the column list */
for (i=1; i<=reply->ncols; ++i)
{
/* allocate an oraColumn struct for the column */
reply->cols[i-1] = (struct oraColumn *)oracleAlloc(sizeof(struct oraColumn));
reply->cols[i-1]->pgname = NULL;
reply->cols[i-1]->pgattnum = 0;
reply->cols[i-1]->pgtype = 0;
reply->cols[i-1]->pgtypmod = 0;
reply->cols[i-1]->used = 0;
reply->cols[i-1]->strip_zeros = 0;
reply->cols[i-1]->pkey = 0;
reply->cols[i-1]->val = NULL;
reply->cols[i-1]->val_len = NULL;
reply->cols[i-1]->val_null = NULL;
/* get the parameter descriptor for the column */
if (checkerr(
OCIParamGet((void *)stmthp, OCI_HTYPE_STMT, session->envp->errhp, (dvoid **)&colp, i),
(dvoid *)session->envp->errhp, OCI_HTYPE_ERROR) != OCI_SUCCESS)
{
oracleError_d(FDW_UNABLE_TO_CREATE_REPLY,
"error describing remote table: OCIParamGet failed to get column data",
oraMessage);
}
/* get the column name */
if (checkerr(
OCIAttrGet((dvoid*)colp, OCI_DTYPE_PARAM, (dvoid*)&ident,
&ident_size, (ub4)OCI_ATTR_NAME, session->envp->errhp),
(dvoid *)session->envp->errhp, OCI_HTYPE_ERROR) != OCI_SUCCESS)
{
oracleError_d(FDW_UNABLE_TO_CREATE_REPLY,
"error describing remote table: OCIAttrGet failed to get column name",
oraMessage);
}
reply->cols[i-1]->name = copyOraText((char *)ident, (int)ident_size, 1);
/* get the data type */
if (checkerr(
OCIAttrGet((dvoid*)colp, OCI_DTYPE_PARAM, (dvoid*)&oraType,
(ub4 *)0, (ub4)OCI_ATTR_DATA_TYPE, session->envp->errhp),
(dvoid *)session->envp->errhp, OCI_HTYPE_ERROR) != OCI_SUCCESS)
{
oracleError_d(FDW_UNABLE_TO_CREATE_REPLY,