forked from dr-kd/DBD-Oracle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbdimp.c
4805 lines (4210 loc) · 151 KB
/
dbdimp.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
/*
vim: sw=4:ts=8
dbdimp.c
Copyright (c) 1994-2006 Tim Bunce Ireland
Copyright (c) 2006-2008 John Scoles (The Pythian Group), Canada
See the COPYRIGHT section in the Oracle.pm file for terms.
*/
#ifdef WIN32
#define strcasecmp strcmpi
#endif
#ifdef __CYGWIN32__
#include "w32api/windows.h"
#include "w32api/winbase.h"
#endif /* __CYGWIN32__ */
#include "Oracle.h"
/* XXX DBI should provide a better version of this */
#define IS_DBI_HANDLE(h) \
(SvROK(h) && SvTYPE(SvRV(h)) == SVt_PVHV && \
SvRMAGICAL(SvRV(h)) && (SvMAGIC(SvRV(h)))->mg_type == 'P')
#ifndef SvPOK_only_UTF8
#define SvPOK_only_UTF8(sv) SvPOK_only(sv)
#endif
DBISTATE_DECLARE;
int ora_fetchtest; /* internal test only, not thread safe */
int is_extproc = 0; /* not ProC but ExtProc.pm */
int dbd_verbose = 0; /* DBD only debugging*/
int oci_warn = 0; /* show oci warnings */
int ora_objects = 0; /* get oracle embedded objects as instance of DBD::Oracle::Object */
int ora_ncs_buff_mtpl = 4; /* a mulitplyer for ncs clob buffers */
/* bitflag constants for figuring out how to handle utf8 for array binds */
#define ARRAY_BIND_NATIVE 0x01
#define ARRAY_BIND_UTF8 0x02
#define ARRAY_BIND_MIXED (ARRAY_BIND_NATIVE|ARRAY_BIND_UTF8)
ub2 charsetid = 0;
ub2 ncharsetid = 0;
ub2 us7ascii_csid = 1;
ub2 utf8_csid = 871;
ub2 al32utf8_csid = 873;
ub2 al16utf16_csid = 2000;
typedef struct sql_fbh_st sql_fbh_t;
struct sql_fbh_st {
int dbtype;
int prec;
int scale;
};
static sql_fbh_t ora2sql_type _((imp_fbh_t* fbh));
static void disable_taf(imp_dbh_t *imp_dbh);
static int enable_taf(SV *dbh, imp_dbh_t *imp_dbh);
void ora_free_phs_contents _((imp_sth_t *imp_sth, phs_t *phs));
static void dump_env_to_trace(imp_dbh_t *imp_dbh);
static sb4
oci_error_get(imp_xxh_t *imp_xxh,
OCIError *errhp, sword status, char *what, SV *errstr, int debug)
{
dTHX;
text errbuf[1024];
ub4 recno = 0;
sb4 errcode = 0;
sb4 eg_errcode = 0;
sword eg_status;
if (!SvOK(errstr))
sv_setpv(errstr,"");
if (!errhp) {
sv_catpv(errstr, oci_status_name(status));
if (what) {
sv_catpv(errstr, " ");
sv_catpv(errstr, what);
}
return status;
}
while( ++recno
&& OCIErrorGet_log_stat(imp_xxh, errhp, recno, (text*)NULL, &eg_errcode, errbuf,
(ub4)sizeof(errbuf), OCI_HTYPE_ERROR, eg_status) != OCI_NO_DATA
&& eg_status != OCI_INVALID_HANDLE
&& recno < 100
) {
if (debug >= 4 || recno>1/*XXX temp*/ || dbd_verbose >= 4 )
PerlIO_printf(DBIc_LOGPIO(imp_xxh),
" OCIErrorGet after %s (er%ld:%s): %d, %ld: %s\n",
what ? what : "<NULL>", (long)recno,
(eg_status==OCI_SUCCESS) ? "ok" : oci_status_name(eg_status),
status, (long)eg_errcode, errbuf);
errcode = eg_errcode;
sv_catpv(errstr, (char*)errbuf);
if (*(SvEND(errstr)-1) == '\n')
--SvCUR(errstr);
}
if (what || status != OCI_ERROR) {
sv_catpv(errstr, (debug<0) ? " (" : " (DBD ");
sv_catpv(errstr, oci_status_name(status));
if (what) {
sv_catpv(errstr, ": ");
sv_catpv(errstr, what);
}
sv_catpv(errstr, ")");
}
return errcode;
}
static int
GetRegKey(char *key, char *val, char *data, unsigned long *size)
{
#ifdef WIN32
unsigned long len = *size - 1;
HKEY hKey;
long ret;
ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, KEY_QUERY_VALUE, &hKey);
if (ret != ERROR_SUCCESS)
return 0;
ret = RegQueryValueEx(hKey, val, NULL, NULL, data, size);
RegCloseKey(hKey);
if ((ret != ERROR_SUCCESS) || (*size >= len))
return 0;
return 1;
#else
/* For gcc not to warn on unused parameters. */
if( key ){}
if( val ){}
if( data ){}
if( size ){}
return 0;
#endif
}
char *
ora_env_var(char *name, char *buf, unsigned long size)
{
#define WIN32_REG_BUFSIZE 80
dTHX;
char last_home_id[WIN32_REG_BUFSIZE+1];
char ora_home_key[WIN32_REG_BUFSIZE+1];
unsigned long len = WIN32_REG_BUFSIZE;
char *e = getenv(name);
if (e)
return e;
if (!GetRegKey("SOFTWARE\\ORACLE\\ALL_HOMES", "LAST_HOME", last_home_id, &len))
return Nullch;
last_home_id[2] = 0;
sprintf(ora_home_key, "SOFTWARE\\ORACLE\\HOME%s", last_home_id);
size -= 1; /* allow room for null termination */
if (!GetRegKey(ora_home_key, name, buf, &size))
return Nullch;
buf[size] = 0;
return buf;
}
#ifdef __CYGWIN32__
/* Under Cygwin there are issues with setting environment variables
* at runtime such that Windows-native libraries loaded by a Cygwin
* process can see those changes.
*
* Cygwin maintains its own cache of environment variables, and also
* only writes to the Windows environment using the "_putenv" win32
* call. This call writes to a Windows C runtime cache, rather than
* the true process environment block.
*
* In order to change environment variables so that the Oracle client
* DLL can see the change, the win32 function SetEnvironmentVariable
* must be called. This function gives an interface to that API.
*
* It is only available when building under Cygwin, and is used by
* the testsuite.
*
* Whilst it could be called by end users, it should be used with
* caution, as it bypasses the environment variable conversions that
* Cygwin typically performs.
*/
void
ora_cygwin_set_env(char *name, char *value)
{
SetEnvironmentVariable(name, value);
}
#endif /* __CYGWIN32__ */
void
dbd_init(dbistate_t *dbistate)
{
dTHX;
DBIS = dbistate;
dbd_init_oci(dbistate);
}
void
dbd_dr_destroy(SV *drh, imp_drh_t *imp_drh)
{
dTHX;
sword status;
/* We rely on the DBI dispatcher to destroy all child handles before we get here (DBI >= 1.623). */
if (imp_drh->leak_handles) {
/* By using ithread, handles will leak in dbd_dr_destroy() */
return;
}
#ifdef ORA_OCI_112
/* Free session pool resources. */
if (imp_drh->pool_hv) {
HE *pool_he;
hv_iterinit(imp_drh->pool_hv);
while ((pool_he = hv_iternext(imp_drh->pool_hv))) {
session_pool_t *pool = (session_pool_t*)SvPVX(HeVAL(pool_he));
/* Only destroy the session pool if there are no active sessions left.
If there are active sessions left, this is because "InactiveDestroy"
is set on one or more db handles. */
if (!pool->active_sessions) {
OCISessionPoolDestroy_log_stat(imp_drh, pool->poolhp, pool->errhp, status);
}
OCIHandleFree_log_stat(imp_drh, pool->poolhp, OCI_HTYPE_SPOOL, status);
OCIHandleFree_log_stat(imp_drh, pool->errhp, OCI_HTYPE_ERROR, status);
OCIHandleFree_log_stat(imp_drh, pool->envhp, OCI_HTYPE_ENV, status);
}
hv_undef(imp_drh->pool_hv);
}
if (imp_drh->charset_hv) {
hv_undef(imp_drh->charset_hv);
}
#endif
}
int
dbd_discon_all(SV *drh, imp_drh_t *imp_drh)
{
dTHR;
dTHX;
/* The disconnect_all concept is flawed and needs more work */
if (!PL_dirty && !SvTRUE(perl_get_sv("DBI::PERL_ENDING",0))) {
DBIh_SET_ERR_CHAR(drh, (imp_xxh_t*)imp_drh, Nullch, 1, "disconnect_all not implemented", Nullch, Nullch);
return FALSE;
}
return FALSE;
}
void
dbd_fbh_dump(imp_sth_t *imp_sth, imp_fbh_t *fbh, int i, int aidx)
{
dTHX;
PerlIO_printf(DBIc_LOGPIO(imp_sth), " fbh %d: '%s'\t%s, ",
i, fbh->name, (fbh->nullok) ? "NULLable" : "NO null ");
PerlIO_printf(DBIc_LOGPIO(imp_sth), "otype %3d->%3d, dbsize %ld/%ld, p%d.s%d\n",
fbh->dbtype, fbh->ftype, (long)fbh->dbsize,(long)fbh->disize,
fbh->prec, fbh->scale);
if (fbh->fb_ary) {
PerlIO_printf(DBIc_LOGPIO(imp_sth), " out: ftype %d, bufl %d. indp %d, rlen %d, rcode %d\n",
fbh->ftype, fbh->fb_ary->bufl, fbh->fb_ary->aindp[aidx],
fbh->fb_ary->arlen[aidx], fbh->fb_ary->arcode[aidx]);
}
}
int
ora_dbtype_is_long(int dbtype)
{
/* Is it a LONG, LONG RAW, LONG VARCHAR or LONG VARRAW type? */
/* Return preferred type code to use if it's a long, else 0. */
if (dbtype == 8 || dbtype == 24) /* LONG or LONG RAW */
return dbtype; /* --> same */
if (dbtype == 94) /* LONG VARCHAR */
return 8; /* --> LONG */
if (dbtype == 95) /* LONG VARRAW */
return 24; /* --> LONG RAW */
return 0;
}
static int
oratype_bind_ok(int dbtype) /* It's a type we support for placeholders */
{
/* basically we support types that can be returned as strings */
switch(dbtype) {
case 1: /* VARCHAR2 */
case 2: /* NVARCHAR2 */
case 5: /* STRING */
case 8: /* LONG */
case 21: /* BINARY FLOAT os-endian */
case 22: /* BINARY DOUBLE os-endian */
case 23: /* RAW */
case 24: /* LONG RAW */
case 96: /* CHAR */
case 97: /* CHARZ */
case 100: /* BINARY FLOAT oracle-endian */
case 101: /* BINARY DOUBLE oracle-endian */
case 106: /* MLSLABEL */
case 102: /* SQLT_CUR OCI 7 cursor variable */
case 112: /* SQLT_CLOB / long */
case 113: /* SQLT_BLOB / long */
case 116: /* SQLT_RSET OCI 8 cursor variable */
case ORA_VARCHAR2_TABLE: /* 201 */
case ORA_NUMBER_TABLE: /* 202 */
case ORA_XMLTYPE: /* SQLT_NTY must be careful here as its value (108) is the same for an embedded object Well really only XML clobs not embedded objects */
return 1;
}
return 0;
}
#ifdef THIS_IS_NOT_CURRENTLY_USED
static int
oratype_rebind_ok(int dbtype) /* all are vrcar any way so just use it */
{
/* basically we support types that can be returned as strings */
switch(dbtype) {
case 1: /* VARCHAR2 */
case 2: /* NVARCHAR2 */
case 5: /* STRING */
case 8: /* LONG */
case 21: /* BINARY FLOAT os-endian */
case 22: /* BINARY DOUBLE os-endian */
case 23: /* RAW */
case 24: /* LONG RAW */
case 96: /* CHAR */
case 97: /* CHARZ */
case 100: /* BINARY FLOAT oracle-endian */
case 101: /* BINARY DOUBLE oracle-endian */
case 106: /* MLSLABEL */
case 102: /* SQLT_CUR OCI 7 cursor variable */
case 116: /* SQLT_RSET OCI 8 cursor variable */
case ORA_VARCHAR2_TABLE: /* 201 */
case ORA_NUMBER_TABLE: /* 202 */
case ORA_XMLTYPE: /* SQLT_NTY must be carefull here as its value (108) is the same for an embedded object Well realy only XML clobs not embedded objects */
case 113: /* SQLT_BLOB / long */
return SQLT_BIN;
case 112: /* SQLT_CLOB / long */
return SQLT_CHR;
}
return dbtype;
}
#endif /* THIS_IS_NOT_CURRENTLY_USED */
/* --- allocate and free oracle oci 'array' buffers --- */
/* --- allocate and free oracle oci 'array' buffers for callback--- */
fb_ary_t *
fb_ary_cb_alloc(ub4 piece_size, ub4 max_len, int size)
{
fb_ary_t *fb_ary;
/* these should be reworked to only to one Newz() */
/* and setup the pointers in the head fb_ary struct */
Newz(42, fb_ary, sizeof(fb_ary_t), fb_ary_t);
Newz(42, fb_ary->abuf, size * piece_size, ub1);
Newz(42, fb_ary->cb_abuf, size * max_len, ub1);
Newz(42, fb_ary->aindp,(unsigned)size,sb2);
Newz(42, fb_ary->arlen,(unsigned)size,ub2);
Newz(42, fb_ary->arcode,(unsigned)size,ub2);
fb_ary->bufl = piece_size;
fb_ary->cb_bufl = max_len;
return fb_ary;
}
/* --- allocate and free oracle oci 'array' buffers --- */
fb_ary_t *
fb_ary_alloc(ub4 bufl, int size)
{
fb_ary_t *fb_ary;
/* these should be reworked to only to one Newz() */
/* and setup the pointers in the head fb_ary struct */
Newz(42, fb_ary, sizeof(fb_ary_t), fb_ary_t);
Newz(42, fb_ary->abuf, size * bufl, ub1);
Newz(42, fb_ary->aindp, (unsigned)size,sb2);
Newz(42, fb_ary->arlen, (unsigned)size,ub2);
Newz(42, fb_ary->arcode,(unsigned)size,ub2);
fb_ary->bufl = bufl;
/* fb_ary->cb_bufl = bufl;*/
return fb_ary;
}
void
fb_ary_free(fb_ary_t *fb_ary)
{
Safefree(fb_ary->abuf);
Safefree(fb_ary->aindp);
Safefree(fb_ary->arlen);
Safefree(fb_ary->arcode);
Safefree(fb_ary->cb_abuf);
Safefree(fb_ary);
}
#ifdef ORA_OCI_112
/* Helper functions to fetch cached session pool. */
static SV *
pool_key(imp_dbh_t *imp_dbh, char *dbname, char *uid, char *pwd, int csid, int ncsid)
{
return newSVpvf("%s/%s@%s class=%.*s,csid=%d,ncsid=%d", uid, pwd, dbname, imp_dbh->pool_classl, imp_dbh->pool_class, csid, ncsid);
}
static session_pool_t *
pool_fetch(imp_drh_t *imp_drh, SV *key)
{
dTHX;
HE *pool_he = hv_fetch_ent(imp_drh->pool_hv, key, 0, 0);
return pool_he ? (session_pool_t *)SvPVX(HeVAL(pool_he)) : NULL;
}
#endif
/* ================================================================== */
int
dbd_db_login(SV *dbh, imp_dbh_t *imp_dbh, char *dbname, char *uid, char *pwd)
{
return dbd_db_login6(dbh, imp_dbh, dbname, uid, pwd, Nullsv);
}
/* from shared.xs */
typedef struct {
SV *sv; /* The actual SV - in shared space */
/* we don't need the following two */
/*recursive_lock_t lock; */
/*perl_cond user_cond;*/ /* For user-level conditions */
} shared_sv;
int
dbd_db_login6(SV *dbh, imp_dbh_t *imp_dbh, char *dbname, char *uid, char *pwd, SV *attr)
{
dTHR;
dTHX;
sword status;
SV **svp;
shared_sv * shared_dbh_ssv = NULL ;
imp_dbh_t * shared_dbh = NULL ;
D_imp_drh_from_dbh;
ub2 new_charsetid = 0;
ub2 new_ncharsetid = 0;
#if defined(USE_ITHREADS) && defined(PERL_MAGIC_shared_scalar)
SV ** shared_dbh_priv_svp ;
SV * shared_dbh_priv_sv ;
STRLEN shared_dbh_len = 0 ;
#endif
#ifdef ORA_OCI_112
session_pool_t *pool = NULL;
/*check to see if the user is connecting with DRCP */
if (DBD_ATTRIB_TRUE(attr,"ora_drcp",8,svp))
imp_dbh->using_drcp = 1;
/* some connection pool attributes */
if ((svp=DBD_ATTRIB_GET_SVP(attr, "ora_drcp_class", 14)) && SvOK(*svp)) {
STRLEN svp_len;
if (!SvPOK(*svp))
croak("ora_drcp_class is not a string");
imp_dbh->pool_class = (text *) SvPV (*svp, svp_len );
imp_dbh->pool_classl= (ub4) svp_len;
}
if (DBD_ATTRIB_TRUE(attr,"ora_drcp_min",12,svp))
DBD_ATTRIB_GET_IV( attr, "ora_drcp_min", 12, svp, imp_dbh->pool_min);
if (DBD_ATTRIB_TRUE(attr,"ora_drcp_max",12,svp))
DBD_ATTRIB_GET_IV( attr, "ora_drcp_max", 12, svp, imp_dbh->pool_max);
if (DBD_ATTRIB_TRUE(attr,"ora_drcp_incr",13,svp))
DBD_ATTRIB_GET_IV( attr, "ora_drcp_incr", 13, svp, imp_dbh->pool_incr);
if (DBD_ATTRIB_TRUE(attr,"ora_drcp_rlb",12,svp))
DBD_ATTRIB_GET_IV( attr, "ora_drcp_rlb", 12, svp, imp_dbh->pool_rlb);
imp_dbh->driver_name = "DBD::Oracle : " VERSION;
#endif
/* TAF Events */
if ((svp=DBD_ATTRIB_GET_SVP(attr, "ora_taf_function", 16)) && SvOK(*svp)) {
if ((SvROK(*svp) && (SvTYPE(SvRV(*svp)) == SVt_PVCV)) ||
(SvPOK(*svp))) {
imp_dbh->taf_function = newSVsv(*svp);
} else {
croak("ora_taf_function needs to be a string or code reference");
}
/* avoid later STORE: */
/* See DBI::DBB problem with ATTRIB_DELETE until DBI 1.607 */
/* DBD_ATTRIB_DELETE(attr, "ora_taf_function", 16); */
(void)hv_delete((HV*)SvRV(attr), "ora_taf_function", 16, G_DISCARD);
}
imp_dbh->server_version = 0;
/* check to see if DBD_verbose or ora_verbose is set*/
if (DBD_ATTRIB_TRUE(attr,"dbd_verbose",11,svp))
DBD_ATTRIB_GET_IV( attr, "dbd_verbose", 11, svp, dbd_verbose);
if (DBD_ATTRIB_TRUE(attr,"ora_verbose",11,svp))
DBD_ATTRIB_GET_IV( attr, "ora_verbose", 11, svp, dbd_verbose);
if (DBIc_DBISTATE(imp_dbh)->debug >= 6 || dbd_verbose >= 6 )
dump_env_to_trace(imp_dbh);
/* dbi_imp_data code adapted from DBD::mysql */
if (DBIc_has(imp_dbh, DBIcf_IMPSET)) {
/* dbi_imp_data from take_imp_data */
if (DBIc_has(imp_dbh, DBIcf_ACTIVE)) {
if (DBIc_DBISTATE(imp_dbh)->debug >= 2 || dbd_verbose >= 3 )
PerlIO_printf(DBIc_LOGPIO(imp_dbh), "dbd_db_login6 skip connect\n");
/* tell our parent we've adopted an active child */
++DBIc_ACTIVE_KIDS(DBIc_PARENT_COM(imp_dbh));
return 1;
}
/* not ACTIVE so connect not skipped */
if (DBIc_DBISTATE(imp_dbh)->debug >= 2 || dbd_verbose >= 3 )
PerlIO_printf(DBIc_LOGPIO(imp_dbh),
"dbd_db_login6 IMPSET but not ACTIVE so connect not skipped\n");
}
imp_dbh->envhp = imp_drh->envhp; /* will be NULL on first connect */
#if defined(USE_ITHREADS) && defined(PERL_MAGIC_shared_scalar)
shared_dbh_priv_svp = (DBD_ATTRIB_OK(attr)?hv_fetch((HV*)SvRV(attr), "ora_dbh_share", 13, 0):NULL) ;
shared_dbh_priv_sv = shared_dbh_priv_svp?*shared_dbh_priv_svp:NULL ;
if (shared_dbh_priv_sv && SvROK(shared_dbh_priv_sv))
shared_dbh_priv_sv = SvRV(shared_dbh_priv_sv) ;
if (shared_dbh_priv_sv) {
MAGIC * mg ;
SvLOCK (shared_dbh_priv_sv) ;
/* some magic from shared.xs (no public api yet :-( */
mg = mg_find(shared_dbh_priv_sv, PERL_MAGIC_shared_scalar) ;
shared_dbh_ssv = (shared_sv * )(mg?mg -> mg_ptr:NULL) ; /*sharedsv_find(*shared_dbh_priv_sv) ;*/
if (!shared_dbh_ssv)
croak ("value of ora_dbh_share must be a scalar that is shared") ;
shared_dbh = (imp_dbh_t *)SvPVX(shared_dbh_ssv -> sv) ;
shared_dbh_len = SvCUR((shared_dbh_ssv -> sv)) ;
if (shared_dbh_len > 0 && shared_dbh_len != sizeof (imp_dbh_t))
croak ("Invalid value for ora_dbh_share") ;
if (shared_dbh_len == sizeof (imp_dbh_t)) {
/* initialize from shared data */
memcpy (((char *)imp_dbh) + DBH_DUP_OFF, ((char *)shared_dbh) + DBH_DUP_OFF, DBH_DUP_LEN) ;
shared_dbh -> refcnt++ ;
imp_dbh -> shared_dbh_priv_sv = shared_dbh_priv_sv ;
imp_dbh -> shared_dbh = shared_dbh ;
if (DBIc_DBISTATE(imp_dbh)->debug >= 2 || dbd_verbose >= 3 )
PerlIO_printf(DBIc_LOGPIO(imp_dbh), " dbd_db_login: use shared Oracle database handles.\n");
} else {
shared_dbh = NULL ;
}
/* By using ithread, handles will leak in dbd_dr_destroy() */
imp_drh->leak_handles = 1;
}
#endif
imp_dbh->get_oci_handle = oci_db_handle;
#ifdef ORA_OCI_112
if (!imp_dbh->using_drcp) {
#endif
if ((svp=DBD_ATTRIB_GET_SVP(attr, "ora_envhp", 9)) && SvOK(*svp)) {
if (!SvTRUE(*svp)) {
imp_dbh->envhp = NULL; /* force new environment */
}
}
/* Test if a cached environment handle it still usable (see RT46739) */
if (imp_dbh->envhp) {
OCIHandleAlloc_ok(imp_dbh, imp_dbh->envhp, &imp_dbh->errhp, OCI_HTYPE_ERROR, status);
if (status != OCI_SUCCESS) {
imp_dbh->envhp = NULL;
}
}
#ifdef ORA_OCI_112
}
else if (!shared_dbh) {
/* Try to find session pool in cache. */
imp_dbh->envhp = NULL;
if (!imp_drh->charset_hv) {
imp_drh->charset_hv = newHV();
}
if (!imp_drh->pool_hv) {
imp_drh->pool_hv = newHV();
}
/* Get charset and ncharset IDs. */
if ((svp = DBD_ATTRIB_GET_SVP(attr, "ora_charset", 11))) {
/* Charset name from parameter; try looking up previously used ID. */
HE *charset_he = hv_fetch_ent(imp_drh->charset_hv, *svp, 0, 0);
charsetid = charset_he ? SvIV(HeVAL(charset_he)) : 0;
}
else {
/* Get charset ID from the NLS environment. */
size_t rsize;
OCINlsEnvironmentVariableGet_log_stat(imp_dbh, &charsetid, 0, OCI_NLS_CHARSET_ID, 0, &rsize, status);
if (status != OCI_SUCCESS) {
oci_error(dbh, NULL, status,
"OCINlsEnvironmentVariableGet(OCI_NLS_CHARSET_ID) Check NLS settings etc.");
return 0;
}
}
if ((svp = DBD_ATTRIB_GET_SVP(attr, "ora_ncharset", 12))) {
/* Charset name from parameter; try looking up previously used ID. */
HE *charset_he = hv_fetch_ent(imp_drh->charset_hv, *svp, 0, 0);
ncharsetid = charset_he ? SvIV(HeVAL(charset_he)) : 0;
}
else {
/* Get charset ID from the NLS environment. */
size_t rsize;
OCINlsEnvironmentVariableGet_log_stat(imp_dbh, &ncharsetid, 0, OCI_NLS_NCHARSET_ID, 0, &rsize, status);
if (status != OCI_SUCCESS) {
oci_error(dbh, NULL, status,
"OCINlsEnvironmentVariableGet(OCI_NLS_NCHARSET_ID) Check NLS settings etc.");
return 0;
}
}
if (charsetid && ncharsetid) {
/* Look up session pool initialized with the same dbname, uid/pwd, connection class, and charsets. */
SV *key_sv = pool_key(imp_dbh, dbname, uid, pwd, charsetid, ncharsetid);
if ((pool = pool_fetch(imp_drh, key_sv))) {
imp_dbh->pool = pool;
imp_dbh->envhp = pool->envhp;
}
sv_free(key_sv);
}
}
#endif
if (!imp_dbh->envhp ) {
SV **init_mode_sv;
ub4 init_mode = OCI_OBJECT;/* needed for LOBs (8.0.4) */
if (DBD_ATTRIB_TRUE(attr, "ora_events", 10, svp))
init_mode |= OCI_EVENTS; /* Needed for Oracle Fast Application Notification (FAN). */
DBD_ATTRIB_GET_IV(attr, "ora_init_mode",13, init_mode_sv, init_mode);
#if defined(USE_ITHREADS) || defined(MULTIPLICITY) || defined(USE_5005THREADS)
init_mode |= OCI_THREADED;
#endif
{
size_t rsize = 0;
/* Get CLIENT char and nchar charset id values */
OCINlsEnvironmentVariableGet_log_stat(imp_dbh, &charsetid,(size_t) 0, OCI_NLS_CHARSET_ID, 0, &rsize ,status );
if (status != OCI_SUCCESS) {
oci_error(dbh, NULL, status,
"OCINlsEnvironmentVariableGet(OCI_NLS_CHARSET_ID) Check NLS settings etc.");
return 0;
}
OCINlsEnvironmentVariableGet_log_stat(imp_dbh, &ncharsetid,(size_t) 0, OCI_NLS_NCHARSET_ID, 0, &rsize ,status );
if (status != OCI_SUCCESS) {
oci_error(dbh, NULL, status,
"OCINlsEnvironmentVariableGet(OCI_NLS_NCHARSET_ID) Check NLS settings etc.");
return 0;
}
/*{
After using OCIEnvNlsCreate() to create the environment handle,
**the actual lengths and returned lengths of bind and define handles are
always in number of bytes**. This applies to the following calls:
* OCIBindByName() * OCIBindByPos() * OCIBindDynamic()
* OCIDefineByPos() * OCIDefineDynamic()
This function enables you to set charset and ncharset ids at
environment creation time. [...]
This function sets nonzero charset and ncharset as client side
database and national character sets, replacing the ones specified
by NLS_LANG and NLS_NCHAR. When charset and ncharset are 0, it
behaves exactly the same as OCIEnvCreate(). Specifically, charset
controls the encoding for metadata and data with implicit form
attribute and ncharset controls the encoding for data with SQLCS_NCHAR
form attribute.
}*/
OCIEnvNlsCreate_log_stat(imp_dbh, &imp_dbh->envhp, init_mode, 0, NULL, NULL, NULL, 0, NULL,
charsetid, ncharsetid, status );
if (status != OCI_SUCCESS) {
oci_error(dbh, NULL, status,
"OCIEnvNlsCreate. Check ORACLE_HOME (Linux) env var or PATH (Windows) and or NLS settings, permissions, etc.");
return 0;
}
svp = DBD_ATTRIB_GET_SVP(attr, "ora_charset", 11);/*get the charset passed in by the user*/
if (svp) {
if (!SvPOK(*svp)) {
croak("ora_charset is not a string");
}
new_charsetid = OCINlsCharSetNameToId(imp_dbh->envhp, (oratext*)SvPV_nolen(*svp));
if (!new_charsetid) {
croak("ora_charset value (%s) is not valid", SvPV_nolen(*svp));
}
#ifdef ORA_OCI_112
if (imp_dbh->using_drcp) {
/* Store lookup from charset name to charset ID. */
(void)hv_store_ent(imp_drh->charset_hv, *svp, newSViv(new_charsetid), 0);
}
#endif
}
svp = DBD_ATTRIB_GET_SVP(attr, "ora_ncharset", 12); /*get the ncharset passed in by the user*/
if (svp) {
if (!SvPOK(*svp)) {
croak("ora_ncharset is not a string");
}
new_ncharsetid = OCINlsCharSetNameToId(imp_dbh->envhp, (oratext*)SvPV_nolen(*svp));
if (!new_ncharsetid) {
croak("ora_ncharset value (%s) is not valid", SvPV_nolen(*svp));
}
#ifdef ORA_OCI_112
if (imp_dbh->using_drcp) {
/* Store lookup from charset name to charset ID. */
(void)hv_store_ent(imp_drh->charset_hv, *svp, newSViv(new_ncharsetid), 0);
}
#endif
}
if (new_charsetid || new_ncharsetid) { /* reset the ENV with the new charset from above*/
if (new_charsetid) charsetid = new_charsetid;
if (new_ncharsetid) ncharsetid = new_ncharsetid;
OCIHandleFree_log_stat(imp_dbh, imp_dbh->envhp, OCI_HTYPE_ENV, status);
OCIEnvNlsCreate_log_stat(imp_dbh, &imp_dbh->envhp, init_mode, 0, NULL, NULL, NULL, 0, 0,
charsetid, ncharsetid, status );
if (status != OCI_SUCCESS) {
oci_error(dbh, NULL, status,
"OCIEnvNlsCreate. Check ORACLE_HOME (Linux) env var or PATH (Windows) and or NLS settings, permissions, etc");
return 0;
}
}
#ifdef ORA_OCI_112
if (!imp_dbh->using_drcp)
#endif
if (!imp_drh->envhp) /* cache first envhp info drh as future default */
imp_drh->envhp = imp_dbh->envhp;
/* update the hard-coded csid constants for unicode charsets */
utf8_csid = OCINlsCharSetNameToId(imp_dbh->envhp, (void*)"UTF8");
al32utf8_csid = OCINlsCharSetNameToId(imp_dbh->envhp, (void*)"AL32UTF8");
al16utf16_csid = OCINlsCharSetNameToId(imp_dbh->envhp, (void*)"AL16UTF16");
}
#ifdef ORA_OCI_112
if (imp_dbh->using_drcp) {
/* Try looking up session pool again, in case ora_charsetid/ora_ncharsetid were used to specify previously used charset IDs from the NLS environment. */
SV *key_sv = pool_key(imp_dbh, dbname, uid, pwd, charsetid, ncharsetid);
if ((pool = pool_fetch(imp_drh, key_sv))) {
imp_dbh->pool = pool;
/* Free the current environment handle and replace it with the session pool's environment handle. */
OCIHandleFree_log_stat(imp_dbh, imp_dbh->envhp, OCI_HTYPE_ENV, status);
imp_dbh->envhp = pool->envhp;
}
sv_free(key_sv);
}
#endif
}
if (!imp_dbh->errhp) {
OCIHandleAlloc_ok(imp_dbh, imp_dbh->envhp, &imp_dbh->errhp, OCI_HTYPE_ERROR, status);
}
OCIAttrGet_log_stat(imp_dbh, imp_dbh->envhp, OCI_HTYPE_ENV, &charsetid, NULL,
OCI_ATTR_ENV_CHARSET_ID, imp_dbh->errhp, status);
if (status != OCI_SUCCESS) {
oci_error(dbh, imp_dbh->errhp, status, "OCIAttrGet OCI_ATTR_ENV_CHARSET_ID");
return 0;
}
OCIAttrGet_log_stat(imp_dbh, imp_dbh->envhp, OCI_HTYPE_ENV, &ncharsetid, NULL,
OCI_ATTR_ENV_NCHARSET_ID, imp_dbh->errhp, status);
if (status != OCI_SUCCESS) {
oci_error(dbh, imp_dbh->errhp, status, "OCIAttrGet OCI_ATTR_ENV_NCHARSET_ID");
return 0;
}
/* At this point we have charsetid & ncharsetid
* note that it is possible for charsetid and ncharestid to
* be distinct if NLS_LANG and NLS_NCHAR are both used.
* BTW: NLS_NCHAR is set as follows: NSL_LANG=AL32UTF8
*/
if (DBIc_DBISTATE(imp_dbh)->debug >= 3 || dbd_verbose >= 3 ) {
oratext charsetname[OCI_NLS_MAXBUFSZ];
oratext ncharsetname[OCI_NLS_MAXBUFSZ];
OCINlsCharSetIdToName(imp_dbh->envhp,charsetname, sizeof(charsetname),charsetid );
OCINlsCharSetIdToName(imp_dbh->envhp,ncharsetname, sizeof(ncharsetname),ncharsetid );
PerlIO_printf(
DBIc_LOGPIO(imp_dbh),
" charset id=%d, name=%s, ncharset id=%d, name=%s"
" (csid: utf8=%d al32utf8=%d)\n",
charsetid,charsetname, ncharsetid,ncharsetname, utf8_csid, al32utf8_csid);
#ifdef ORA_OCI_112
if (imp_dbh->using_drcp)
PerlIO_printf(DBIc_LOGPIO(imp_dbh)," Using DRCP Connection\n ");
#endif
}
if (!shared_dbh) {
#ifdef ORA_OCI_112
if (imp_dbh->using_drcp) { /* connect using a DRCP */
OCIAuthInfo *authp;
ub4 purity = OCI_ATTR_PURITY_SELF;
OraText *rettag;
ub4 rettagl;
/* pool Default values */
if (!imp_dbh->pool_min )
imp_dbh->pool_min = 0;
if (!imp_dbh->pool_max )
imp_dbh->pool_max = 40;
if (!imp_dbh->pool_incr)
imp_dbh->pool_incr = 1;
if (!imp_dbh->pool_rlb)
imp_dbh->pool_rlb = 0;
if (!pool) {
/* Create and cache new session pool struct. */
SV *key_sv = pool_key(imp_dbh, dbname, uid, pwd, charsetid, ncharsetid);
session_pool_t pool_data = {0};
SV *pool_sv = newSVpvn((char*)&pool_data, sizeof(pool_data));
HE *pool_he = hv_store_ent(imp_drh->pool_hv, key_sv, pool_sv, 0);
imp_dbh->pool = pool = (session_pool_t*)SvPVX(HeVAL(pool_he));
pool->envhp = imp_dbh->envhp;
sv_free(key_sv);
OCIHandleAlloc_ok(imp_dbh, pool->envhp, &pool->poolhp, OCI_HTYPE_SPOOL, status);
OCIHandleAlloc_ok(imp_dbh, pool->envhp, &authp, OCI_HTYPE_AUTHINFO, status);
/* Create an unshared error handle for use in pool creation and destruction. */
OCIHandleAlloc_ok(imp_dbh, pool->envhp, &pool->errhp, OCI_HTYPE_ERROR, status);
OCIAttrSet_log_stat(imp_dbh, authp, OCI_HTYPE_AUTHINFO,
imp_dbh->driver_name, (ub4)strlen(imp_dbh->driver_name),
OCI_ATTR_DRIVER_NAME, pool->errhp, status);
OCIAttrSet_log_stat(imp_dbh, pool->poolhp, OCI_HTYPE_SPOOL,
authp, (ub4)0, OCI_ATTR_SPOOL_AUTH, pool->errhp, status);
ora_parse_uid(imp_dbh, &uid, &pwd);
OCISessionPoolCreate_log_stat(
imp_dbh,
pool->envhp,
pool->errhp,
pool->poolhp,
&pool->pool_name,
&pool->pool_namel,
(OraText *) dbname,
(ub4)strlen(dbname),
imp_dbh->pool_min,
imp_dbh->pool_max,
imp_dbh->pool_incr,
(OraText *) uid,
(ub4)strlen(uid),
(OraText *) pwd,
(ub4)strlen(pwd),
OCI_SPC_HOMOGENEOUS | (imp_dbh->pool_rlb ? 0 : OCI_SPC_NO_RLB),
status);
if (status != OCI_SUCCESS) {
oci_error(dbh, pool->errhp, status, "OCISessionPoolCreate");
OCIHandleFree_log_stat(imp_dbh, authp, OCI_HTYPE_AUTHINFO, status);
OCIHandleFree_log_stat(imp_dbh, pool->poolhp, OCI_HTYPE_SPOOL,status);
OCIHandleFree_log_stat(imp_dbh, pool->errhp, OCI_HTYPE_ERROR, status);
/* Free the global error handle as well. */
OCIHandleFree_log_stat(imp_dbh, imp_dbh->errhp, OCI_HTYPE_ERROR, status);
OCIHandleFree_log_stat(imp_dbh, pool->envhp, OCI_HTYPE_ENV, status);
(void)hv_delete_ent(imp_drh->pool_hv, HeSVKEY(pool_he), 0, 0);
return 0;
}
OCIHandleFree_log_stat(imp_dbh, authp, OCI_HTYPE_AUTHINFO, status);
}
OCIHandleAlloc_ok(imp_dbh, imp_dbh->envhp, &authp, OCI_HTYPE_AUTHINFO, status);
OCIAttrSet_log_stat(imp_dbh, authp, (ub4) OCI_HTYPE_AUTHINFO,
&purity, (ub4) 0,(ub4) OCI_ATTR_PURITY, imp_dbh->errhp, status);
if (imp_dbh->pool_class) /*pool_class may or may not be used */
OCIAttrSet_log_stat(imp_dbh, authp, (ub4) OCI_HTYPE_AUTHINFO,
(OraText *) imp_dbh->pool_class, (ub4) imp_dbh->pool_classl,
(ub4) OCI_ATTR_CONNECTION_CLASS, imp_dbh->errhp, status);
/* Use session tagging to get a server session initalized with correct charsets. */
sprintf((char*)imp_dbh->session_tag, "csid=%d,ncsid=%d", charsetid, ncharsetid);
OCISessionGet_log_stat(imp_dbh, imp_dbh->envhp, imp_dbh->errhp, &imp_dbh->svchp, authp,
pool->pool_name, (ub4)strlen((char *)pool->pool_name),
imp_dbh->session_tag, (ub4)strlen((char *)imp_dbh->session_tag), &rettag, &rettagl, &imp_dbh->session_tag_found, status);
if (status != OCI_SUCCESS) {
oci_error(dbh, imp_dbh->errhp, status, "OCISessionGet");
OCIHandleFree_log_stat(imp_dbh, authp, OCI_HTYPE_AUTHINFO, status);
return 0;
}
/* Update number of active sessions in the pool. */
++imp_dbh->pool->active_sessions;
OCIHandleFree_log_stat(imp_dbh, authp, OCI_HTYPE_AUTHINFO, status);
/* Get server and session handles from service context handle, allocated by OCISessionGet. */
OCIAttrGet_log_stat(imp_dbh, imp_dbh->svchp, OCI_HTYPE_SVCCTX, &imp_dbh->srvhp, NULL,
OCI_ATTR_SERVER, imp_dbh->errhp, status);
OCIAttrGet_log_stat(imp_dbh, imp_dbh->svchp, OCI_HTYPE_SVCCTX, &imp_dbh->seshp, NULL,
OCI_ATTR_SESSION, imp_dbh->errhp, status);
if (DBIc_DBISTATE(imp_dbh)->debug >= 4 || dbd_verbose >= 4 ) {
PerlIO_printf(
DBIc_LOGPIO(imp_dbh),
"Using DRCP with session settings min=%d, max=%d, and increment=%d\n",
imp_dbh->pool_min,
imp_dbh->pool_max,
imp_dbh->pool_incr);
if (imp_dbh->pool_class)
PerlIO_printf(
DBIc_LOGPIO(imp_dbh),
"with connection class=%s\n",imp_dbh->pool_class);
}
}
else {
#endif /* ORA_OCI_112 */
SV **sess_mode_type_sv;
ub4 sess_mode_type = OCI_DEFAULT;
ub4 cred_type;
DBD_ATTRIB_GET_IV(attr, "ora_session_mode",16, sess_mode_type_sv, sess_mode_type);
OCIHandleAlloc_ok(imp_dbh, imp_dbh->envhp, &imp_dbh->srvhp, OCI_HTYPE_SERVER, status);
OCIHandleAlloc_ok(imp_dbh, imp_dbh->envhp, &imp_dbh->svchp, OCI_HTYPE_SVCCTX, status);
OCIHandleAlloc_ok(imp_dbh, imp_dbh->envhp, &imp_dbh->seshp, OCI_HTYPE_SESSION, status);
OCIServerAttach_log_stat(imp_dbh, dbname,OCI_DEFAULT, status);
if (status != OCI_SUCCESS) {
oci_error(dbh, imp_dbh->errhp, status, "OCIServerAttach");
OCIHandleFree_log_stat(imp_dbh, imp_dbh->seshp, OCI_HTYPE_SESSION,status);
OCIHandleFree_log_stat(imp_dbh, imp_dbh->srvhp, OCI_HTYPE_SERVER, status);
OCIHandleFree_log_stat(imp_dbh, imp_dbh->errhp, OCI_HTYPE_ERROR, status);
OCIHandleFree_log_stat(imp_dbh, imp_dbh->svchp, OCI_HTYPE_SVCCTX, status);
if (imp_dbh->envhp != imp_drh->envhp) {
OCIHandleFree_log_stat(imp_dbh, imp_dbh->envhp, OCI_HTYPE_ENV, status);
}
return 0;
}
OCIAttrSet_log_stat(imp_dbh, imp_dbh->svchp, OCI_HTYPE_SVCCTX, imp_dbh->srvhp,
(ub4) 0, OCI_ATTR_SERVER, imp_dbh->errhp, status);
cred_type = ora_parse_uid(imp_dbh, &uid, &pwd);
#ifdef ORA_OCI_112
OCIAttrSet_log_stat(imp_dbh, imp_dbh->seshp, (ub4)OCI_HTYPE_SESSION,
imp_dbh->driver_name, (ub4)strlen(imp_dbh->driver_name),
(ub4)OCI_ATTR_DRIVER_NAME, imp_dbh->errhp, status);
#endif
OCISessionBegin_log_stat(imp_dbh, imp_dbh->svchp, imp_dbh->errhp, imp_dbh->seshp,cred_type, sess_mode_type, status);
if (status == OCI_SUCCESS_WITH_INFO) {