forked from gwenshap/DBD-Oracle
-
Notifications
You must be signed in to change notification settings - Fork 25
/
dbdimp.c
4517 lines (4020 loc) · 142 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
#if defined(__CYGWIN__) || defined(__CYGWIN32__)
#include <w32api/windows.h>
#include <w32api/winbase.h>
#endif /* __CYGWIN__ */
#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 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 us7ascii_csid = 1;
ub2 utf8_csid = 871;
ub2 al32utf8_csid = 873;
ub2 al16utf16_csid = 2000;
/* reduce noise in the login6 function */
#define tracer(dlvl, vlvl, ...) if (DBIc_DBISTATE(imp_dbh)->debug >= (dlvl) || dbd_verbose >= (vlvl) )\
PerlIO_printf(DBIc_LOGPIO(imp_dbh), __VA_ARGS__)
typedef struct sql_fbh_st sql_fbh_t;
struct sql_fbh_st {
int dbtype;
int prec;
int scale;
};
typedef struct login_info_st login_info_t;
struct login_info_st {
SV * dbh;
imp_dbh_t * imp_dbh;
char *dbname;
char *uid;
char *pwd;
ub4 mode;
};
static sql_fbh_t ora2sql_type _((imp_fbh_t* fbh));
static void disable_taf(imp_dbh_t *imp_dbh);
static int enable_taf(pTHX_ 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;
}
/* report to DBI errors that are not comming from Oracle */
static int
local_error(pTHX_ SV * h, const char * fmt, ...)
{
va_list ap;
SV * txt_sv = sv_newmortal();
SV * code_sv = get_sv("DBI::stderr", 0);
D_imp_xxh(h);
if(code_sv == NULL)
{
code_sv = sv_newmortal();
sv_setiv(code_sv, 2000000000);
}
va_start(ap, fmt);
sv_vsetpvf(txt_sv, fmt, &ap);
va_end(ap);
DBIh_SET_ERR_SV(h, imp_xxh, code_sv, txt_sv, &PL_sv_undef, &PL_sv_undef);
return FALSE;
}
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, (LPBYTE)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;
}
#if defined(__CYGWIN__) || defined(__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 /* __CYGWIN__ */
void
ora_shared_release(pTHX_ SV * sv)
{
STRLEN len;
imp_dbh_t *imp_dbh;
while (SvROK(sv)) sv = SvRV(sv) ;
imp_dbh = (imp_dbh_t *)SvPV(sv, len);
if(len == sizeof(*imp_dbh))
{
if(dbd_verbose >= 3)
warn("clearing shared session %p\n", imp_dbh->seshp);
cnx_detach(aTHX_ imp_dbh);
cnx_clean(aTHX_ imp_dbh);
}
}
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;
if (dbd_verbose >= 3 )
PerlIO_printf(DBIc_LOGPIO(imp_drh), "dr_destroy %p\n", imp_drh);
cnx_drop_dr(aTHX_ imp_drh);
}
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);
}
/* ================================================================== */
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);
}
static sword
get_env_charset(OCIEnv *envhp, OCIError * errhp, ub2 * charset_p, ub2 * ncharset_p)
{
sword status = OCIAttrGet(envhp, OCI_HTYPE_ENV,
charset_p, NULL,
OCI_ATTR_ENV_CHARSET_ID,errhp
);
if (status != OCI_SUCCESS) return status;
return OCIAttrGet(envhp, OCI_HTYPE_ENV,
ncharset_p, NULL,
OCI_ATTR_ENV_NCHARSET_ID,errhp
);
}
/* this function makes final adjustments to connected handle */
static int
activate_dbh(pTHX_ dblogin_info_t * ctrl)
{
imp_dbh_t * imp_dbh = ctrl->imp_dbh;
DBIc_IMPSET_on(imp_dbh); /* just in case */
DBIc_ACTIVE_on(imp_dbh); /* call disconnect before freeing */
imp_dbh->ph_type = 1; /* SQLT_CHR "(ORANET TYPE) character string" */
imp_dbh->ph_csform = 0; /* meaning auto (see dbd_rebind_ph) */
if (DBIc_DBISTATE(imp_dbh)->debug >= 3 || dbd_verbose >= 3 ) {
sword status;
oratext charsetname[OCI_NLS_MAXBUFSZ];
oratext ncharsetname[OCI_NLS_MAXBUFSZ];
ub2 charsetid_l = 0;
ub2 ncharsetid_l = 0;
/* Report charsets used in the environment */
status = get_env_charset(imp_dbh->envhp, imp_dbh->errhp, &charsetid_l, &ncharsetid_l);
if (status != OCI_SUCCESS) {
oci_error(ctrl->dbh, imp_dbh->errhp, status,
"OCIAttrGet OCI_ATTR_ENV_CHARSET_ID");
return 0;
}
OCINlsCharSetIdToName(imp_dbh->envhp,charsetname, sizeof(charsetname),charsetid_l );
OCINlsCharSetIdToName(imp_dbh->envhp,ncharsetname, sizeof(ncharsetname),ncharsetid_l );
PerlIO_printf( DBIc_LOGPIO(imp_dbh),
" charset id=%d, name=%s, ncharset id=%d, name=%s, session=%p"
" (csid: utf8=%d al32utf8=%d)\n",
charsetid_l,charsetname, ncharsetid_l,ncharsetname, imp_dbh->seshp,
utf8_csid, al32utf8_csid);
#ifdef ORA_OCI_112
if (ctrl->pool_max)
PerlIO_printf(DBIc_LOGPIO(imp_dbh)," Using DRCP Connection\n ");
#endif
}
return TRUE;
}
int
dbd_db_login6(SV *dbh, imp_dbh_t *imp_dbh, char *dbname, char *uid, char *pwd, SV *attr)
{
dTHR;
dTHX;
dblogin_info_t ctrl = {0};
SV **svp;
#if defined(USE_ITHREADS) && defined(PERL_MAGIC_shared_scalar)
STRLEN shared_dbh_len = 0 ;
SV * shared_sv = NULL;
#endif
/* check to see if DBD_verbose or ora_verbose is set*/
DBD_ATTRIB_GET_IV( attr, "dbd_verbose", 11, svp, dbd_verbose);
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))
{
tracer(2, 3, "dbd_db_login6 impset. Env is %p\n", imp_dbh->envhp);
/* tell our parent we've adopted an active child */
++DBIc_ACTIVE_KIDS(DBIc_PARENT_COM(imp_dbh));
return TRUE;
}
/* not ACTIVE so connect not skipped */
tracer(2, 3, "dbd_db_login6 IMPSET but not ACTIVE\n");
}
ctrl.mode = OCI_OBJECT;/* needed for LOBs (8.0.4) */
if (DBD_ATTRIB_TRUE(attr, "ora_events", 10, svp))
{
ctrl.mode |= OCI_EVENTS;
/* Needed for Oracle Fast Application Notification (FAN). */
}
/* Undocumented, this overrides all previous settings */
DBD_ATTRIB_GET_IV(attr, "ora_init_mode",13, svp, ctrl.mode);
#if defined(USE_ITHREADS) || defined(MULTIPLICITY) || defined(USE_5005THREADS)
ctrl.mode |= OCI_THREADED;
#endif
ctrl.dbname = dbname;
ctrl.dbh = dbh;
ctrl.imp_dbh = imp_dbh;
ctrl.uid = uid;
ctrl.pwd = pwd;
#ifdef ORA_OCI_112
/*check to see if the user is connecting with DRCP */
if (DBD_ATTRIB_TRUE(attr,"ora_drcp",8,svp))
{
ctrl.pool_max = 40;
hv_delete((HV*)SvRV(attr), "ora_drcp", 8, G_DISCARD);
}
/* some connection pool attributes */
if(ctrl.pool_max)
{
svp = DBD_ATTRIB_GET_SVP(attr, "ora_drcp_min",12);
if (svp != NULL)
{
ctrl.pool_min = SvIV(*svp);
hv_delete((HV*)SvRV(attr), "ora_drcp_min", 12, G_DISCARD);
}
svp = DBD_ATTRIB_GET_SVP(attr,"ora_drcp_max",12);
if (svp != NULL)
{
ctrl.pool_max = SvIV(*svp);
if (ctrl.pool_max == 0) ctrl.pool_max = 40;
hv_delete((HV*)SvRV(attr), "ora_drcp_max", 12, G_DISCARD);
}
svp = DBD_ATTRIB_GET_SVP(attr,"ora_drcp_incr",13);
if (svp != NULL)
{
ctrl.pool_incr = SvIV(*svp);
hv_delete((HV*)SvRV(attr), "ora_drcp_incr", 13, G_DISCARD);
}
svp = DBD_ATTRIB_GET_SVP(attr,"ora_drcp_rlb",12);
if (svp != NULL)
{
ctrl.pool_rlb = (0 != SvIV(*svp)) ? 1 : 0;
hv_delete((HV*)SvRV(attr), "ora_drcp_rlb", 12, G_DISCARD);
}
svp = DBD_ATTRIB_GET_SVP(attr,"ora_drcp_class",14);
if (svp != NULL)
ctrl.pool_class = hv_delete((HV*)SvRV(attr), "ora_drcp_class", 14, 0);
/* save session tag to be used during session-get
* it won't be passed to STORE. The found tag shall
* be stored in imp_dbh->session_tag
*/
svp = DBD_ATTRIB_GET_SVP(attr,"ora_drcp_tag",12);
if (svp != NULL)
ctrl.pool_tag = hv_delete((HV*)SvRV(attr), "ora_drcp_tag", 12, 0);
/* pool Default values */
if (!ctrl.pool_incr) ctrl.pool_incr = 1;
}
svp = DBD_ATTRIB_GET_SVP(attr,"ora_driver_name",15);
if (svp != NULL)
{
STRLEN l;
SV * v = hv_delete((HV*)SvRV(attr), "ora_driver_name", 15, 0);
ctrl.driver_name = SvPV(v, l);
ctrl.driver_name_len = (ub4)l;
}
else
{
ctrl.driver_name = "DBD::Oracle : " VERSION;
ctrl.driver_name_len = strlen(ctrl.driver_name);
}
#endif
/* TAF Events shall be processed from STORE*/
imp_dbh->server_version = 0;
imp_dbh->get_oci_handle = oci_db_handle;
#if defined(USE_ITHREADS) && defined(PERL_MAGIC_shared_scalar)
imp_dbh->is_shared = 0;
svp = DBD_ATTRIB_GET_SVP(attr,"ora_dbh_share",13);
if(svp != NULL)
{
MAGIC * mg;
shared_sv = *svp;
tracer(2, 3, "trying to find shared session\n");
while (SvROK(shared_sv)) shared_sv = SvRV(shared_sv) ;
/* check if this is shared scalar by finding appropriate magic */
if(SvTYPE(shared_sv) < SVt_PVMG ||
(mg = mg_find(shared_sv, PERL_MAGIC_shared_scalar)) == NULL)
return local_error(
aTHX_ dbh,"value of ora_dbh_share must be a scalar that is shared"
) ;
/* now we should lock access. Note, locking of unshared variable croaks */
/* this is unlocked automatically when current XS function ends */
SvLOCK (shared_sv) ;
/* copy value from shared part, just in case */
SvGETMAGIC(shared_sv);
shared_dbh_len = SvCUR(shared_sv) ;
if (shared_dbh_len == sizeof (imp_dbh_t)) {
imp_dbh_t * shared_dbh = (imp_dbh_t *)SvPVX(shared_sv) ;
/* initialize from shared data */
memcpy (
((char *)imp_dbh) + DBH_DUP_OFF,
((char *)shared_dbh) + DBH_DUP_OFF,
DBH_DUP_LEN
);
imp_dbh->is_shared = 1;
/* using private errhp does not make sense really because
* one can not use this copy of connection at the same
* time in different threads. There are transactions and
* if some threads starts transaction, then other thread
* should not accidently finish it. So if the connections
* are used carefully, then they don't need separate errhp
*/
tracer(2, 3, "dbd_db_login: shared session %p\n", shared_dbh->seshp);
(void)hv_delete((HV*)SvRV(attr), "ora_dbh_share", 13, G_DISCARD);
/* nothing else to do with this handle */
return activate_dbh(aTHX_ &ctrl);
}
else if (shared_dbh_len != 0)
return local_error(aTHX_ dbh, "Invalid value for ora_dbh_share %d vs %d",
(int)shared_dbh_len, (int)sizeof(imp_dbh_t)) ;
/* indicate that this connection is shared */
imp_dbh->is_shared = 1;
}
#endif
/* Get desired charset and ncharset */
if ((svp = DBD_ATTRIB_GET_SVP(attr, "ora_charset", 11))) {
if (!SvPOK(*svp)) return local_error(
aTHX_ dbh, "ora_charset is not a string"
);
ctrl.cset = SvPV_nolen(*svp);
/* don't remove attribute because I need pointer */
}
if ((svp = DBD_ATTRIB_GET_SVP(attr, "ora_ncharset", 12))) {
if (!SvPOK(*svp)) return local_error(
aTHX_ dbh, "ora_ncharset is not a string"
);
ctrl.ncset = SvPV_nolen(*svp);
}
ctrl.session_mode = OCI_DEFAULT;
if ((svp = DBD_ATTRIB_GET_SVP(attr, "ora_session_mode", 16))) {
ctrl.session_mode = SvIV(*svp);
hv_delete((HV*)SvRV(attr), "ora_session_mode", 16, G_DISCARD);
}
if(!cnx_establish(aTHX_ &ctrl)) return FALSE;
#if defined(USE_ITHREADS) && defined(PERL_MAGIC_shared_scalar)
if (imp_dbh->is_shared != 0 && 0 == shared_dbh_len) {
#ifdef ORA_OCI_112
SV * tag = imp_dbh->session_tag;
imp_dbh->session_tag = NULL;
#endif
tracer(2, 3, "saving shared session %p\n", imp_dbh->seshp);
sv_setpvn_mg(shared_sv, (char*)imp_dbh, sizeof(*imp_dbh));
#ifdef ORA_OCI_112
imp_dbh->session_tag = tag;
#endif
}
#endif
return activate_dbh(aTHX_ &ctrl);
}
int
dbd_db_commit(SV *dbh, imp_dbh_t *imp_dbh)
{
dTHX;
sword status;
OCITransCommit_log_stat(imp_dbh, imp_dbh->svchp, imp_dbh->errhp, OCI_DEFAULT, status);
if (status != OCI_SUCCESS) {
oci_error(dbh, imp_dbh->errhp, status, "OCITransCommit");
return 0;
}
return 1;
}
int
dbd_st_cancel(SV *sth, imp_sth_t *imp_sth)
{
dTHX;
sword status;
status = OCIBreak(imp_sth->svchp, imp_sth->errhp);
if (status != OCI_SUCCESS) {
oci_error(sth, imp_sth->errhp, status, "OCIBreak");
return 0;
}
/* if we are using a scrolling cursor we should get rid of the
cursor by fetching row 0 */
if (imp_sth->exe_mode==OCI_STMT_SCROLLABLE_READONLY){
OCIStmtFetch_log_stat(imp_sth, imp_sth->stmhp, imp_sth->errhp, 0,OCI_FETCH_NEXT,0, status);
}
return 1;
}
int
dbd_db_rollback(SV *dbh, imp_dbh_t *imp_dbh)
{
dTHX;
sword status;
OCITransRollback_log_stat(imp_dbh, imp_dbh->svchp, imp_dbh->errhp, OCI_DEFAULT, status);
if (status != OCI_SUCCESS) {
oci_error(dbh, imp_dbh->errhp, status, "OCITransRollback");
return 0;
}
return 1;
}
int dbd_st_bind_col(SV *sth, imp_sth_t *imp_sth, SV *col, SV *ref, IV type, SV *attribs) {
dTHX;
int field;
if (!SvIOK(col)) {
croak ("Invalid column number") ;
}
field = SvIV(col);
if ((field < 1) || (field > DBIc_NUM_FIELDS(imp_sth))) {
croak("cannot bind to non-existent field %d", field);
}
if (type != 0) {
imp_sth->fbh[field-1].req_type = type;
}
if (attribs) {
imp_sth->fbh[field-1].bind_flags = 0; /* default to none */
}
#if DBIXS_REVISION >= 13590
/* DBIXS 13590 added StrictlyTyped and DiscardString attributes */
if (attribs) {
HV *attr_hash;
SV **attr;
if (!SvROK(attribs)) {
croak ("attributes is not a reference");
}
else if (SvTYPE(SvRV(attribs)) != SVt_PVHV) {
croak ("attributes not a hash reference");
}
attr_hash = (HV *)SvRV(attribs);
attr = hv_fetch(attr_hash, "StrictlyTyped", (U32)13, 0);
if (attr && SvTRUE(*attr)) {
imp_sth->fbh[field-1].bind_flags |= DBIstcf_STRICT;
}
attr = hv_fetch(attr_hash, "DiscardString", (U32)13, 0);
if (attr && SvTRUE(*attr)) {
imp_sth->fbh[field-1].bind_flags |= DBIstcf_DISCARD_STRING;
}
}
#endif /* DBIXS_REVISION >= 13590 */
return 1;
}
int
dbd_db_disconnect(SV *dbh, imp_dbh_t *imp_dbh)
{
dTHX;
dTHR;
/* We assume that disconnect will always work */
/* since most errors imply already disconnected. */
DBIc_ACTIVE_off(imp_dbh);
#if defined(USE_ITHREADS) && defined(PERL_MAGIC_shared_scalar)
/* do nothing for connections stored in shared variables */
if(imp_dbh->is_shared != 0)
{
#ifdef ORA_OCI_112
/* just in case, drop session_tag if it is set
* normally it is used during session release */
if(imp_dbh->session_tag != NULL)
{
SvREFCNT_dec(imp_dbh->session_tag);
imp_dbh->session_tag = NULL;
}
#endif
return TRUE;
}
#endif
cnx_detach(aTHX_ imp_dbh);
/* We don't free imp_dbh since a reference still exists */
/* The DESTROY method is the only one to 'free' memory. */
/* Note that statement objects may still exists for this dbh! */
return TRUE;
}
void
dbd_db_destroy(SV *dbh, imp_dbh_t *imp_dbh)
{
dTHX ;
if (DBIc_ACTIVE(imp_dbh)) dbd_db_disconnect(dbh, imp_dbh);
DBIc_IMPSET_off(imp_dbh);
if (imp_dbh->taf_function) {
disable_taf(imp_dbh);
SvREFCNT_dec(imp_dbh->taf_function);
imp_dbh->taf_function = NULL;
}
if (imp_dbh->taf_ctx.dbh_ref) {
SvREFCNT_dec(SvRV(imp_dbh->taf_ctx.dbh_ref));
imp_dbh->taf_ctx.dbh_ref = NULL;
}
#if defined(USE_ITHREADS) && defined(PERL_MAGIC_shared_scalar)
/* on shared cnx only decrement reference count */
if(imp_dbh->is_shared != 0) return;
#endif
tracer(3, 3, "clearing session %p\n", imp_dbh->seshp);
cnx_clean(aTHX_ imp_dbh);
}
SV *
dbd_take_imp_data(SV *dbh, imp_xxh_t *imp_xxh, void* foo)
{
dTHX;
D_imp_dbh(dbh);
tracer(3, 3, "take_imp for %p (session %p)\n", imp_dbh, imp_dbh->seshp);
if (imp_dbh->taf_function) {
disable_taf(imp_dbh);
SvREFCNT_dec(imp_dbh->taf_function);
imp_dbh->taf_function = NULL;
}
if (imp_dbh->taf_ctx.dbh_ref) {
SvREFCNT_dec(SvRV(imp_dbh->taf_ctx.dbh_ref));
imp_dbh->taf_ctx.dbh_ref = NULL;
}
#ifdef ORA_OCI_112
if(imp_dbh->session_tag != NULL)
{
SvREFCNT_dec(imp_dbh->session_tag);
imp_dbh->session_tag = NULL;
}
#endif
/* we do nothing else here because the copy of current state is saved by
* SUPER::take_imp_data and no other method shall be called on this
* handle. If user calls something, then it is not our problem.
* Wether DESTROY nor DISCONNECT are called by DBI
*/
/* Indicate that SUPER::take_imp_data should be called. */
return &PL_sv_no;
}
/* According to Oracle's documentation of OCISessionGet, attributes should not be changed
on the server and session handles attached to OCISessionGet's service context handle.
This would imply that dbd_db_STORE_attrib is wrong for session pooling, however
it seems to work just fine... */
int
dbd_db_STORE_attrib(SV *dbh, imp_dbh_t *imp_dbh, SV *keysv, SV *valuesv)
{
dTHX;
STRLEN kl;
STRLEN vl;
sword status;
char *key = SvPV(keysv,kl);
int on = SvTRUE(valuesv);
int cacheit = 1;
if (kl==17 && strEQ(key, "ora_ncs_buff_mtpl") ) {
ora_ncs_buff_mtpl = SvIV (valuesv);
}
#ifdef ORA_OCI_112
else if (kl==15 && strEQ(key, "ora_driver_name") ) {
(void)local_error(
aTHX_ dbh, "ora_driver_name can not be changed"
);
}
else if (kl==8 && strEQ(key, "ora_drcp") ) {
(void)local_error(
aTHX_ dbh, "ora_drcp can not be changed"
);
}
else if (kl==14 && strEQ(key, "ora_drcp_class") ) {
(void)local_error(
aTHX_ dbh, "ora_drcp_class can not be changed"
);
}
else if (kl==12 && strEQ(key, "ora_drcp_tag") ) {
#if defined(USE_ITHREADS) && defined(PERL_MAGIC_shared_scalar)
if(imp_dbh->is_shared != 0)
{
(void)local_error(
aTHX_ dbh, "ora_drcp_tag not supported for shared DBH"
);
}
else
#endif
if(cnx_is_pooled_session(aTHX_ dbh, imp_dbh))
{
if(imp_dbh->session_tag != NULL) SvREFCNT_dec(imp_dbh->session_tag);
imp_dbh->session_tag = newSVsv(valuesv);
}
else (void)local_error(
aTHX_ dbh, "ora_drcp_tag is not used without DRCP"
);
}
else if (kl==12 && strEQ(key, "ora_drcp_min") ) {
cnx_pool_min(aTHX_ dbh, imp_dbh, (ub4)SvIV(valuesv));
}
else if (kl==13 && strEQ(key, "ora_drcp_mode") ) {
cnx_pool_mode(aTHX_ dbh, imp_dbh, (ub4)SvIV(valuesv));
}
#if OCI_MAJOR_VERSION > 18
else if (kl==13 && strEQ(key, "ora_drcp_wait") ) {
cnx_pool_wait(aTHX_ dbh, imp_dbh, (ub4)SvIV(valuesv));
}
#endif
else if (kl==12 && strEQ(key, "ora_drcp_max") ) {
cnx_pool_max(aTHX_ dbh, imp_dbh, (ub4)SvIV(valuesv));
}
else if (kl==13 && strEQ(key, "ora_drcp_incr") ) {
cnx_pool_incr(aTHX_ dbh, imp_dbh, (ub4)SvIV(valuesv));
}
else if (kl==12 && strEQ(key, "ora_drcp_rlb") ) {
/* ignore it here, too late to do anything */
}
#endif
else if (kl==16 && strEQ(key, "ora_taf_function") ) {
if (imp_dbh->taf_function)
SvREFCNT_dec(imp_dbh->taf_function);
imp_dbh->taf_function = newSVsv(valuesv);
if (SvTRUE(valuesv)) {
if(!enable_taf(aTHX_ dbh, imp_dbh)) return FALSE;
} else {
disable_taf(imp_dbh);
}
}
#ifdef OCI_ATTR_ACTION
else if (kl==10 && strEQ(key, "ora_action") ) {
char * action = (char *) SvPV (valuesv, vl );
OCIAttrSet_log_stat(imp_dbh, imp_dbh->seshp, OCI_HTYPE_SESSION,
action,(ub4)vl, OCI_ATTR_ACTION, imp_dbh->errhp, status);
if(status != OCI_SUCCESS) (void)oci_error(
dbh, imp_dbh->errhp, status, "OCIAttrSet OCI_ATTR_ACTION");
}
#endif
else if (kl==21 && strEQ(key, "ora_client_identifier") ) {
char * cid = (char *) SvPV (valuesv, vl );
OCIAttrSet_log_stat(imp_dbh, imp_dbh->seshp, OCI_HTYPE_SESSION,
cid,(ub4)vl,OCI_ATTR_CLIENT_IDENTIFIER,
imp_dbh->errhp, status
);
if(status != OCI_SUCCESS) (void)oci_error(
dbh, imp_dbh->errhp, status,
"OCIAttrSet OCI_ATTR_CLIENT_IDENTIFIER");
}
#ifdef OCI_ATTR_CLIENT_INFO
else if (kl==15 && strEQ(key, "ora_client_info") ) {
char * client_info = (char *) SvPV (valuesv, vl );
OCIAttrSet_log_stat(imp_dbh,
imp_dbh->seshp,OCI_HTYPE_SESSION,
client_info,(ub4)vl,
OCI_ATTR_CLIENT_INFO,imp_dbh->errhp, status
);