File: oci8.c

package info (click to toggle)
libdbd-oracle-perl 1.76-1
  • links: PTS, VCS
  • area: contrib
  • in suites: buster
  • size: 1,800 kB
  • sloc: ansic: 8,348; perl: 7,845; xml: 448; makefile: 18
file content (4948 lines) | stat: -rw-r--r-- 175,936 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
/*
	vim:sw=4:ts=8
	oci8.c

	Copyright (c) 1998-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.

*/

#include "Oracle.h"

#ifdef UTF8_SUPPORT
#include <utf8.h>
#endif

#define sv_set_undef(sv) if (SvROK(sv)) sv_unref(sv); else SvOK_off(sv)

DBISTATE_DECLARE;

int describe_obj_by_tdo(SV *sth,imp_sth_t *imp_sth,fbh_obj_t *obj,ub2 level );
int dump_struct(imp_sth_t *imp_sth,fbh_obj_t *obj,int level);


/*
char *
dbd_yes_no(int yes_no)
{
	dTHX;
	if (yes_no) {
		return "Yes";
	}
	return "No";
}
*/

void
dbd_init_oci(dbistate_t *dbistate)
{
	dTHX;
	DBIS = dbistate;
}

void
dbd_init_oci_drh(imp_drh_t * imp_drh)
{
	dTHX;
	imp_drh->ora_long	= perl_get_sv("Oraperl::ora_long",	  GV_ADDMULTI);
	imp_drh->ora_trunc	= perl_get_sv("Oraperl::ora_trunc",	 GV_ADDMULTI);
	imp_drh->ora_cache	= perl_get_sv("Oraperl::ora_cache",	 GV_ADDMULTI);
	imp_drh->ora_cache_o = perl_get_sv("Oraperl::ora_cache_o",	GV_ADDMULTI);

}

/*
char *
oci_sql_function_code_name(int sqlfncode)
{
	dTHX;
	SV *sv;
	switch (sqlfncode) {
		case 1 :	return "CREATE TABLE";
		case 3 :	return "INSERT";
		case 4 :	return "SELECT";
		case 5 :	return "UPDATE";
		case 8 :	return "DROP TABLE";
		case 9 :	return "DELETE";

	}
	sv = sv_2mortal(newSVpv("",0));
	sv_grow(sv, 50);
	sprintf(SvPVX(sv),"(UNKNOWN SQL FN Code %d)", sqlfncode);
	return SvPVX(sv);
}
*/

 /*
char *
oci_ptype_name(int ptype)
{
	dTHX;
	SV *sv;
	switch (ptype) {
		case OCI_PTYPE_UNK:			return "UNKNOWN";
		case OCI_PTYPE_TABLE:		return "TABLE";
		case OCI_PTYPE_VIEW:		return "VIEW";
		case OCI_PTYPE_PROC:		return "PROCEDURE";
		case OCI_PTYPE_FUNC:		return "FUNCTION";
		case OCI_PTYPE_PKG:			return "PACKAGE";
		case OCI_PTYPE_TYPE:		return "USER DEFINED TYPE";
		case OCI_PTYPE_SYN:			return "SYNONYM";
		case OCI_PTYPE_SEQ:			return "SEQUENCE";
		case OCI_PTYPE_COL:			return "COLUMN";
		case OCI_PTYPE_ARG:			return "ARGUMENT";
		case OCI_PTYPE_LIST:		return "LIST";
		case OCI_PTYPE_TYPE_ATTR:	return "USER-DEFINED TYPE'S ATTRIBUTE";
		case OCI_PTYPE_TYPE_COLL:	return "COLLECTION TYPE'S ELEMENT";
		case OCI_PTYPE_TYPE_METHOD:	return "USER-DEFINED TYPE'S METHOD";
		case OCI_PTYPE_TYPE_ARG:	return "USER-DEFINED TYPE METHOD'S ARGUMENT";
		case OCI_PTYPE_TYPE_RESULT:	return "USER-DEFINED TYPE METHOD'S RESULT";
		case OCI_PTYPE_SCHEMA:		return "SCHEMA";
		case OCI_PTYPE_DATABASE:		return "DATABASE";

	}
	sv = sv_2mortal(newSVpv("",0));
	sv_grow(sv, 50);
	sprintf(SvPVX(sv),"(UNKNOWN PTYPE Code %d)", ptype);
	return SvPVX(sv);
}
 */

char *
oci_exe_mode(ub4 mode)
{

	dTHX;
	SV *sv;
	switch (mode) {
	/*----------------------- Execution Modes -----------------------------------*/
		case OCI_DEFAULT:			return "DEFAULT";
		case OCI_BATCH_MODE:		return "BATCH_MODE"; /* batch the oci stmt for exec */
		case OCI_EXACT_FETCH:		return "EXACT_FETCH";	/* fetch exact rows specified */
		case OCI_STMT_SCROLLABLE_READONLY :		return "STMT_SCROLLABLE_READONLY";
		case OCI_DESCRIBE_ONLY:		return "DESCRIBE_ONLY";  /* only describe the statement */
		case OCI_COMMIT_ON_SUCCESS:	return "COMMIT_ON_SUCCESS";	/* commit, if successful exec */
		case OCI_NON_BLOCKING:		return "NON_BLOCKING";				/* non-blocking */
		case OCI_BATCH_ERRORS:		return "BATCH_ERRORS";	/* batch errors in array dmls */
		case OCI_PARSE_ONLY:		return "PARSE_ONLY";	 /* only parse the statement */
		case OCI_SHOW_DML_WARNINGS:	return "SHOW_DML_WARNINGS";
/*		case OCI_RESULT_CACHE:		return "RESULT_CACHE";	hint to use query caching only 11 so wait this one out*/
/*		case OCI_NO_RESULT_CACHE :	return "NO_RESULT_CACHE";	hint to bypass query caching*/
	}
	sv = sv_2mortal(newSVpv("",0));
	sv_grow(sv, 50);
	sprintf(SvPVX(sv),"(UNKNOWN OCI EXECUTE MODE %d)", mode);
	return SvPVX(sv);
}

/* SQL Types we support for placeholders basically we support types that can be returned as strings */
char *
sql_typecode_name(int dbtype) {
	dTHX;
	SV *sv;
	switch(dbtype) {
		case  0:	return "DEFAULT (varchar)";
		case  1:	return "VARCHAR";
		case  2:	return "NVARCHAR2";
		case  5:	return "STRING";
		case  8:	return "LONG";
		case 21:	return "BINARY FLOAT os-endian";
		case 22:	return "BINARY DOUBLE os-endian";
		case 23:	return "RAW";
		case 24:	return "LONG RAW";
		case 96:	return "CHAR";
		case 97:	return "CHARZ";
		case 100:	return "BINARY FLOAT oracle-endian";
		case 101:	return "BINARY DOUBLE oracle-endian";
                case 104:       return "ROWID";
		case 106:	return "MLSLABEL";
		case 102:	return "SQLT_CUR	OCI 7 cursor variable";
		case 112:	return "SQLT_CLOB / long";
		case 113:	return "SQLT_BLOB / long";
		case 116:	return "SQLT_RSET	OCI 8 cursor variable";
		case ORA_VARCHAR2_TABLE:return "ORA_VARCHAR2_TABLE";
		case ORA_NUMBER_TABLE: 	return "ORA_NUMBER_TABLE";
		case ORA_XMLTYPE:		return "ORA_XMLTYPE or SQLT_NTY";/* 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  */

	}
	 sv = sv_2mortal(newSVpv("",0));
	 sv_grow(sv, 50);
	 sprintf(SvPVX(sv),"(UNKNOWN SQL TYPECODE %d)", dbtype);
	 return SvPVX(sv);
}



char *
oci_typecode_name(int typecode){

	dTHX;
	SV *sv;

	switch (typecode) {
		case OCI_TYPECODE_INTERVAL_YM:		return "INTERVAL_YM";
		case OCI_TYPECODE_INTERVAL_DS:		return "NTERVAL_DS";
		case OCI_TYPECODE_TIMESTAMP_TZ:		return "TIMESTAMP_TZ";
		case OCI_TYPECODE_TIMESTAMP_LTZ:	return "TIMESTAMP_LTZ";
		case OCI_TYPECODE_TIMESTAMP:		return "TIMESTAMP";
		case OCI_TYPECODE_DATE:				return "DATE";
		case OCI_TYPECODE_CLOB:				return "CLOB";
		case OCI_TYPECODE_BLOB:				return "BLOB";
		case OCI_TYPECODE_BFILE:			return "BFILE";
		case OCI_TYPECODE_RAW:				return "RAW";
		case OCI_TYPECODE_CHAR:				return "CHAR";
		case OCI_TYPECODE_VARCHAR:			return "VARCHAR";
		case OCI_TYPECODE_VARCHAR2:			return "VARCHAR2";
		case OCI_TYPECODE_SIGNED8:			return "SIGNED8";
		case OCI_TYPECODE_UNSIGNED8:		return "DECLARE";
		case OCI_TYPECODE_UNSIGNED16 :		return "UNSIGNED8";
		case OCI_TYPECODE_UNSIGNED32 :		return "UNSIGNED32";
		case OCI_TYPECODE_REAL :			return "REAL";
		case OCI_TYPECODE_DOUBLE :			return "DOUBLE";
		case OCI_TYPECODE_INTEGER :			return "INT";
		case OCI_TYPECODE_SIGNED16 :		return "SHORT";
		case OCI_TYPECODE_SIGNED32 :		return "LONG";
		case OCI_TYPECODE_DECIMAL :			return "DECIMAL";
		case OCI_TYPECODE_FLOAT :			return "FLOAT";
		case OCI_TYPECODE_NUMBER : 			return "NUMBER";
		case OCI_TYPECODE_SMALLINT:			return "SMALLINT";
		case OCI_TYPECODE_OBJECT:			return "OBJECT";
		case OCI_TYPECODE_OPAQUE:			return "XMLTYPE~OPAQUE";
		case OCI_TYPECODE_VARRAY:			return "VARRAY";
		case OCI_TYPECODE_TABLE:			return "TABLE";
		case OCI_TYPECODE_NAMEDCOLLECTION:	return "NAMEDCOLLECTION";
	}

	sv = sv_2mortal(newSVpv("",0));
	sv_grow(sv, 50);
	sprintf(SvPVX(sv),"(UNKNOWN OCI TYPECODE %d)", typecode);
	return SvPVX(sv);

}

char *
oci_status_name(sword status)
{
	dTHX;
	SV *sv;
	switch (status) {
		case OCI_SUCCESS:			return "SUCCESS";
		case OCI_SUCCESS_WITH_INFO:	return "SUCCESS_WITH_INFO";
		case OCI_NEED_DATA:			return "NEED_DATA";
		case OCI_NO_DATA:			return "NO_DATA";
		case OCI_ERROR:				return "ERROR";
		case OCI_INVALID_HANDLE:	return "INVALID_HANDLE";
		case OCI_STILL_EXECUTING:	return "STILL_EXECUTING";
		case OCI_CONTINUE:			return "CONTINUE";
	}
	sv = sv_2mortal(newSVpv("",0));
	sv_grow(sv, 50);
	sprintf(SvPVX(sv),"(UNKNOWN OCI STATUS %d)", status);
	return SvPVX(sv);
}
/* the various modes used in OCI */
char *
oci_define_options(ub4 options)
{
	dTHX;
	SV *sv;
	switch (options) {
	/*------------------------Bind and Define Options----------------------------*/
		case OCI_DEFAULT:		return "DEFAULT";
		case OCI_DYNAMIC_FETCH: return "DYNAMIC_FETCH";				/* fetch dynamically */

	 }
	sv = sv_2mortal(newSVpv("",0));
	sv_grow(sv, 50);
	sprintf(SvPVX(sv),"(UNKNOWN OCI DEFINE MODE %d)", options);
	return SvPVX(sv);
}

char *
oci_bind_options(ub4 options)
{
	dTHX;
	SV *sv;
	switch (options) {
	/*------------------------Bind and Define Options----------------------------*/
		case OCI_DEFAULT:		return "DEFAULT";
		case OCI_SB2_IND_PTR:	return "SB2_IND_PTR";						  /* unused */
		case OCI_DATA_AT_EXEC:	return "DATA_AT_EXEC";			 /* data at execute time */
		case OCI_PIECEWISE:		return "PIECEWISE";		 /* piecewise DMLs or fetch */
/*		case OCI_BIND_SOFT:		return "BIND_SOFT";				soft bind or define */
/*		case OCI_DEFINE_SOFT:	return "DEFINE_SOFT";			soft bind or define */
/*		case OCI_IOV:			return "";	11g only release 1.23 me thinks For scatter gather bind/define */

	 }
	sv = sv_2mortal(newSVpv("",0));
	sv_grow(sv, 50);
	sprintf(SvPVX(sv),"(UNKNOWN BIND MODE %d)", options);
	return SvPVX(sv);
}

/* the various modes used in OCI */
char *
oci_mode(ub4  mode)
{
	dTHX;
	SV *sv;
	switch (mode) {
		case OCI_THREADED | OCI_OBJECT:	return "THREADED | OBJECT";
		case OCI_OBJECT | OCI_EVENTS:	return "OBJECT | EVENTS";
		case OCI_THREADED | OCI_OBJECT | OCI_EVENTS:	return "THREADED | OBJECT | EVENTS";
		case OCI_DEFAULT:		return "DEFAULT";
		/* the default value for parameters and attributes */
		/*-------------OCIInitialize Modes / OCICreateEnvironment Modes -------------*/
		case OCI_THREADED:		return "THREADED";	  /* appl. in threaded environment */
		case OCI_OBJECT:		return "OBJECT";  /* application in object environment */
		case OCI_EVENTS:		return "EVENTS";  /* application is enabled for events */
		case OCI_SHARED:		return "SHARED";  /* the application is in shared mode */
		/* The following *TWO* are only valid for OCICreateEnvironment call */
		case OCI_NO_UCB:		return "NO_UCB "; /* No user callback called during ini */
		case OCI_NO_MUTEX:		return "NO_MUTEX"; /* the environment handle will not be
											  protected by a mutex internally */
		case OCI_SHARED_EXT:	 return "SHARED_EXT";			  /* Used for shared forms */
		case OCI_ALWAYS_BLOCKING:return "ALWAYS_BLOCKING";	/* all connections always blocking */
		case OCI_USE_LDAP:		return "USE_LDAP";			/* allow  LDAP connections */
		case OCI_REG_LDAPONLY:	return "REG_LDAPONLY";			  /* only register to LDAP */
		case OCI_UTF16:			return "UTF16";		/* mode for all UTF16 metadata */
		case OCI_AFC_PAD_ON:	return "AFC_PAD_ON";  /* turn on AFC blank padding when rlenp present */
		case OCI_NEW_LENGTH_SEMANTICS: return "NEW_LENGTH_SEMANTICS";	/* adopt new length semantics
													the new length semantics, always bytes, is used by OCIEnvNlsCreate */
		case OCI_NO_MUTEX_STMT:	return "NO_MUTEX_STMT";			/* Do not mutex stmt handle */
		case OCI_MUTEX_ENV_ONLY:	return "MUTEX_ENV_ONLY";  /* Mutex only the environment handle */
/*		case OCI_SUPPRESS_NLS_VALIDATION:return "SUPPRESS_NLS_VALIDATION";	suppress nls validation*/
													 /* 	  nls validation suppression is on by default;*/
													  /*	use OCI_ENABLE_NLS_VALIDATION to disable it */
/*		case OCI_MUTEX_TRY:				return "MUTEX_TRY";	 try and acquire mutex */
/*		case OCI_NCHAR_LITERAL_REPLACE_ON: return "NCHAR_LITERAL_REPLACE_ON";  nchar literal replace on */
/*		case OCI_NCHAR_LITERAL_REPLACE_OFF:return "NCHAR_LITERAL_REPLACE_OFF";  nchar literal replace off*/
/*		case OCI_ENABLE_NLS_VALIDATION:	return "ENABLE_NLS_VALIDATION";	 enable nls validation */
		/*------------------------OCIConnectionpoolCreate Modes----------------------*/
		case OCI_CPOOL_REINITIALIZE:	return "CPOOL_REINITIALIZE";
		/*--------------------------------- OCILogon2 Modes -------------------------*/
/*case OCI_LOGON2_SPOOL:		return "LOGON2_SPOOL";	  Use session pool */
		case OCI_LOGON2_CPOOL:		return "LOGON2_CPOOL"; /* Use connection pool */
/*case OCI_LOGON2_STMTCACHE:	return "LOGON2_STMTCACHE";	  Use Stmt Caching */
		case OCI_LOGON2_PROXY:		return "LOGON2_PROXY";	 /* Proxy authentiaction */
		/*------------------------- OCISessionPoolCreate Modes ----------------------*/
/*case OCI_SPC_REINITIALIZE:		return "SPC_REINITIALIZE";	Reinitialize the session pool */
/*case OCI_SPC_HOMOGENEOUS: 		return "SPC_HOMOGENEOUS"; "";	Session pool is homogeneneous */
/*case OCI_SPC_STMTCACHE:			return "SPC_STMTCACHE";	Session pool has stmt cache */
/*case OCI_SPC_NO_RLB:			return "SPC_NO_RLB ";  Do not enable Runtime load balancing. */
		/*--------------------------- OCISessionGet Modes ---------------------------*/
/*case OCI_SESSGET_SPOOL:	 	return "SESSGET_SPOOL";	  SessionGet called in SPOOL mode */
/*case OCI_SESSGET_CPOOL:			return "SESSGET_CPOOL";	SessionGet called in CPOOL mode */
/*case OCI_SESSGET_STMTCACHE: 	return "SESSGET_STMTCACHE";				  Use statement cache */
/*case OCI_SESSGET_CREDPROXY: 	return "SESSGET_CREDPROXY";	  SessionGet called in proxy mode */
/*case OCI_SESSGET_CREDEXT:		return "SESSGET_CREDEXT";	 */
		case OCI_SESSGET_SPOOL_MATCHANY:return "SESSGET_SPOOL_MATCHANY";
/*case OCI_SESSGET_PURITY_NEW:	return "SESSGET_PURITY_NEW";
		case OCI_SESSGET_PURITY_SELF:	return "SESSGET_PURITY_SELF"; */
	}
	sv = sv_2mortal(newSVpv("",0));
	sv_grow(sv, 50);
	sprintf(SvPVX(sv),"(UNKNOWN OCI MODE %d)", mode);
	return SvPVX(sv);
}

char *
oci_stmt_type_name(int stmt_type)
{
	dTHX;
	SV *sv;
	switch (stmt_type) {
	case OCI_STMT_SELECT:	return "SELECT";
	case OCI_STMT_UPDATE:	return "UPDATE";
	case OCI_STMT_DELETE:	return "DELETE";
	case OCI_STMT_INSERT:	return "INSERT";
	case OCI_STMT_CREATE:	return "CREATE";
	case OCI_STMT_DROP:		return "DROP";
	case OCI_STMT_ALTER:	return "ALTER";
	case OCI_STMT_BEGIN:	return "BEGIN";
	case OCI_STMT_DECLARE:	return "DECLARE";
	}
	sv = sv_2mortal(newSVpv("",0));
	sv_grow(sv, 50);
	sprintf(SvPVX(sv),"(STMT TYPE %d)", stmt_type);
	return SvPVX(sv);
}

char *
oci_col_return_codes(int rc)
{
	dTHX;
	SV *sv;
	switch (rc) {
		case 1406:	return "TRUNCATED";
		case 0:		return "OK";
		case 1405:	return "NULL";
		case 1403:	return "NO DATA";

	}
	sv = sv_2mortal(newSVpv("",0));
	sv_grow(sv, 50);
	sprintf(SvPVX(sv),"UNKNOWN RC=%d)", rc);
	return SvPVX(sv);
}

char *
oci_hdtype_name(ub4 hdtype)
{
	dTHX;
	SV *sv;
	switch (hdtype) {
	/* Handles */
	case OCI_HTYPE_ENV:				return "OCI_HTYPE_ENV";
	case OCI_HTYPE_ERROR:			return "OCI_HTYPE_ERROR";
	case OCI_HTYPE_SVCCTX:			return "OCI_HTYPE_SVCCTX";
	case OCI_HTYPE_STMT:			return "OCI_HTYPE_STMT";
	case OCI_HTYPE_BIND:			return "OCI_HTYPE_BIND";
	case OCI_HTYPE_DEFINE:			return "OCI_HTYPE_DEFINE";
	case OCI_HTYPE_DESCRIBE:		return "OCI_HTYPE_DESCRIBE";
	case OCI_HTYPE_SERVER:			return "OCI_HTYPE_SERVER";
	case OCI_HTYPE_SESSION:			return "OCI_HTYPE_SESSION";
	case OCI_HTYPE_CPOOL:   		return "OCI_HTYPE_CPOOL";
	case OCI_HTYPE_SPOOL:   		return "OCI_HTYPE_SPOOL";
	/*case OCI_HTYPE_AUTHINFO:        return "OCI_HTYPE_AUTHINFO";*/
	/* Descriptors */
	case OCI_DTYPE_LOB:				return "OCI_DTYPE_LOB";
	case OCI_DTYPE_SNAP:			return "OCI_DTYPE_SNAP";
	case OCI_DTYPE_RSET:			return "OCI_DTYPE_RSET";
	case OCI_DTYPE_PARAM:			return "OCI_DTYPE_PARAM";
	case OCI_DTYPE_ROWID:			return "OCI_DTYPE_ROWID";
#ifdef OCI_DTYPE_REF
	case OCI_DTYPE_REF:				return "OCI_DTYPE_REF";
#endif
	}
	sv = sv_2mortal(newSViv((IV)hdtype));
	return SvPV(sv,PL_na);
}

/*used to look up the name of a csform value
  used only for debugging */
char *
oci_csform_name(ub4 attr)
{
	dTHX;
	SV *sv;
	switch (attr) {

/* CHAR/NCHAR/VARCHAR2/NVARCHAR2/CLOB/NCLOB char set "form" information */
	case SQLCS_IMPLICIT:			return "SQLCS_IMPLICIT";/* for CHAR, VARCHAR2, CLOB w/o a specified set */
	case SQLCS_NCHAR:				return "SQLCS_NCHAR";/* for NCHAR, NCHAR VARYING, NCLOB */
	case SQLCS_EXPLICIT:			return "SQLCS_EXPLICIT";/* for CHAR, etc, with "CHARACTER SET ..." syntax */
	case SQLCS_FLEXIBLE:			return "SQLCS_FLEXIBLE";/* for PL/SQL "flexible" parameters */
	case SQLCS_LIT_NULL:			return "SQLCS_LIT_NULL";/* for typecheck of NULL and empty_clob() lits */
	}

	sv = sv_2mortal(newSViv((IV)attr));
	return SvPV(sv,PL_na);
}

/*used to look up the name of a OCI_DTYPE_PARAM Attribute Types
  used only for debugging */
char *
oci_dtype_attr_name(ub4 attr)
{
	dTHX;
	SV *sv;
	switch (attr) {
/*=======================Describe Handle Parameter Attributes ===============*/
	case OCI_ATTR_DATA_SIZE:			return "OCI_ATTR_DATA_SIZE";	/* maximum size of the data */
	case OCI_ATTR_DATA_TYPE:			return "OCI_ATTR_DATA_TYPE";	/* the SQL type of the column/argument */
	case OCI_ATTR_DISP_SIZE:			return "OCI_ATTR_DISP_SIZE";	/* the display size */
	case OCI_ATTR_NAME:					return "OCI_ATTR_NAME";			/* the name of the column/argument */
	case OCI_ATTR_PRECISION:			return "OCI_ATTR_PRECISION";	/* precision if number type */
	case OCI_ATTR_SCALE:				return "OCI_ATTR_SCALE"; 		/* scale if number type */
	case OCI_ATTR_IS_NULL:				return "OCI_ATTR_IS_NULL";		/* is it null ? */
	case OCI_ATTR_TYPE_NAME: 			return "OCI_ATTR_TYPE_NAME";
  /* name of the named data type or a package name for package private types */
	case OCI_ATTR_SCHEMA_NAME: 			return "OCI_ATTR_SCHEMA_NAME";	/* the schema name */
	case OCI_ATTR_SUB_NAME: 			return "OCI_ATTR_SUB_NAME";		/* type name if package private type */
	case OCI_ATTR_POSITION:				return "OCI_ATTR_POSITION";
	case OCI_ATTR_CHAR_USED:            return "OCI_ATTR_CHAR_USED";	/* char length semantics */
	case OCI_ATTR_CHAR_SIZE:             return "OCI_ATTR_CHAR_SIZE";	/* char length */
	case OCI_ATTR_CHARSET_ID:			return "OCI_ATTR_CHARSET_ID";	/* Character Set ID */
	case OCI_ATTR_CHARSET_FORM:			return "OCI_ATTR_CHARSET_FORM";	/* Character Set Form */
	}

	sv = sv_2mortal(newSViv((IV)attr));
	return SvPV(sv,PL_na);

}

/*used to look up the name of non a OCI_DTYPE_PARAM Attribute Types
  used only for debugging */
char *
oci_attr_name(ub4 attr)
{
	dTHX;
	SV *sv;
	switch (attr) {
#ifdef ORA_OCI_102
	case OCI_ATTR_MODULE:                    return "OCI_ATTR_MODULE";        /* module for tracing */
	case OCI_ATTR_ACTION:                    return "OCI_ATTR_ACTION";        /* action for tracing */
	case OCI_ATTR_CLIENT_INFO:               return "OCI_ATTR_CLIENT_INFO";               /* client info */
	case OCI_ATTR_COLLECT_CALL_TIME:         return "OCI_ATTR_COLLECT_CALL_TIME";         /* collect call time */
	case OCI_ATTR_CALL_TIME:                 return "OCI_ATTR_CALL_TIME";         /* extract call time */
	case OCI_ATTR_ECONTEXT_ID:               return "OCI_ATTR_ECONTEXT_ID";      /* execution-id context */
	case OCI_ATTR_ECONTEXT_SEQ:              return "OCI_ATTR_ECONTEXT_SEQ";  /*execution-id sequence num */


	/*------------------------------ Session attributes -------------------------*/
	case OCI_ATTR_SESSION_STATE:             return "OCI_ATTR_SESSION_STATE";             /* session state */

	case OCI_ATTR_SESSION_STATETYPE:         return "OCI_ATTR_SESSION_STATETYPE";        /* session state type */
	case OCI_SESSION_STATELESS_DEF: 		 return "OCI_SESSION_STATELESS_DEF";                    /* valid state types */

	case OCI_ATTR_SESSION_STATE_CLEARED:     return "OCI_ATTR_SESSION_STATE_CLEARED";     /* session state cleared*/
	case OCI_ATTR_SESSION_MIGRATED:          return "OCI_ATTR_SESSION_MIGRATED";       /* did session migrate*/
	case OCI_ATTR_SESSION_PRESERVE_STATE:    return "OCI_ATTR_SESSION_PRESERVE_STATE";    /* preserve session state */
#endif
#ifdef ORA_OCI_112
	case OCI_ATTR_DRIVER_NAME:               return "OCI_ATTR_DRIVER_NAME";               /* Driver Name */
#endif
	case OCI_ATTR_CLIENT_IDENTIFIER:         return "OCI_ATTR_CLIENT_IDENTIFIER";   /* value of client id to set*/

	/*=============================Attribute Types===============================*/
#ifdef ORA_OCI_112
    case OCI_ATTR_PURITY:				return "OCI_ATTR_PURITY"; /* for DRCP session purity */
    case OCI_ATTR_CONNECTION_CLASS:		return "OCI_ATTR_CONNECTION_CLASS"; /* for DRCP connection class */
#endif
	case OCI_ATTR_FNCODE:				return "OCI_ATTR_FNCODE";		/* the OCI function code */
	case OCI_ATTR_OBJECT:				return "OCI_ATTR_OBJECT"; /* is the environment initialized in object mode */
	case OCI_ATTR_NONBLOCKING_MODE:		return "OCI_ATTR_NONBLOCKING_MODE";		/* non blocking mode */
	case OCI_ATTR_SQLCODE:				return "OCI_ATTR_SQLCODE";				/* the SQL verb */
	case OCI_ATTR_ENV:					return "OCI_ATTR_ENV";				/* the environment handle */
	case OCI_ATTR_SERVER:				return "OCI_ATTR_SERVER";			/* the server handle*/
	case OCI_ATTR_SESSION:				return "OCI_ATTR_SESSION";			/* the user session handle*/
	case OCI_ATTR_TRANS:				return "OCI_ATTR_TRANS";			/* the transaction handle */
	case OCI_ATTR_ROW_COUNT:			return "OCI_ATTR_ROW_COUNT";		/* the rows processed so far */
	case OCI_ATTR_SQLFNCODE:			return "OCI_ATTR_SQLFNCODE";		/* the SQL verb of the statement */
	case OCI_ATTR_PREFETCH_ROWS:		return "OCI_ATTR_PREFETCH_ROWS";	/* sets the number of rows to prefetch */
	case OCI_ATTR_NESTED_PREFETCH_ROWS:	return "OCI_ATTR_NESTED_PREFETCH_ROWS"; /* the prefetch rows of nested table*/
	case OCI_ATTR_PREFETCH_MEMORY:		return "OCI_ATTR_PREFETCH_MEMORY";		/* memory limit for rows fetched */
	case OCI_ATTR_NESTED_PREFETCH_MEMORY:return "OCI_ATTR_NESTED_PREFETCH_MEMORY";	/* memory limit for nested rows */
	case OCI_ATTR_CHAR_COUNT:			return "OCI_ATTR_CHAR_COUNT";			 /* this specifies the bind and define size in characters */
	case OCI_ATTR_PDSCL:				return "OCI_ATTR_PDSCL";			/* packed decimal scale*/
	/*case OCI_ATTR_FSPRECISION OCI_ATTR_PDSCL:return "";					 fs prec for datetime data types */
	case OCI_ATTR_PDPRC:				return "OCI_ATTR_PDPRC";			/* packed decimal format*/
	/*case OCI_ATTR_LFPRECISION OCI_ATTR_PDPRC: return "";					fs prec for datetime data types */
	case OCI_ATTR_PARAM_COUNT:			return "OCI_ATTR_PARAM_COUNT";		/* number of column in the select list */
	case OCI_ATTR_ROWID:				return "OCI_ATTR_ROWID";			/* the rowid */
	case OCI_ATTR_CHARSET:				return "OCI_ATTR_CHARSET";			/* the character set value */
	case OCI_ATTR_NCHAR:				return "OCI_ATTR_NCHAR";			/* NCHAR type */
	case OCI_ATTR_USERNAME:				return "OCI_ATTR_USERNAME";			/* username attribute */
	case OCI_ATTR_PASSWORD:				return "OCI_ATTR_PASSWORD";			/* password attribute */
	case OCI_ATTR_STMT_TYPE:			return "OCI_ATTR_STMT_TYPE";		/* statement type */
	case OCI_ATTR_INTERNAL_NAME:		return "OCI_ATTR_INTERNAL_NAME";	/* user friendly global name */
	case OCI_ATTR_EXTERNAL_NAME:		return "OCI_ATTR_EXTERNAL_NAME";	/* the internal name for global txn */
	case OCI_ATTR_XID:					return "OCI_ATTR_XID";				/* XOPEN defined global transaction id */
	case OCI_ATTR_TRANS_LOCK:			return "OCI_ATTR_TRANS_LOCK";		/* */
	case OCI_ATTR_TRANS_NAME:			return "OCI_ATTR_TRANS_NAME";		/* string to identify a global transaction */
	case OCI_ATTR_HEAPALLOC:			return "OCI_ATTR_HEAPALLOC";		/* memory allocated on the heap */
	case OCI_ATTR_CHARSET_FORM:			return "OCI_ATTR_CHARSET_FORM";		/* Character Set Form */
	case OCI_ATTR_MAXDATA_SIZE:			return "OCI_ATTR_MAXDATA_SIZE";		/* Maximumsize of data on the server  */
	case OCI_ATTR_CACHE_OPT_SIZE:		return "OCI_ATTR_CACHE_OPT_SIZE";	/* object cache optimal size */
	case OCI_ATTR_CACHE_MAX_SIZE:		return "OCI_ATTR_CACHE_MAX_SIZE";	/* object cache maximum size percentage */
	case OCI_ATTR_PINOPTION:			return "OCI_ATTR_PINOPTION";		/* object cache default pin option */
	case OCI_ATTR_ALLOC_DURATION:		return "OCI_ATTR_ALLOC_DURATION";	/* object cache default allocation duration */
	case OCI_ATTR_PIN_DURATION:			return "OCI_ATTR_PIN_DURATION";		/* object cache default pin duration */
	case OCI_ATTR_FDO:					return "OCI_ATTR_FDO";		/* Format Descriptor object attribute */
	case OCI_ATTR_POSTPROCESSING_CALLBACK:		return "OCI_ATTR_POSTPROCESSING_CALLBACK"; /* Callback to process outbind data */
	case OCI_ATTR_POSTPROCESSING_CONTEXT:		return "OCI_ATTR_POSTPROCESSING_CONTEXT";  /* Callback context to process outbind data */
	case OCI_ATTR_ROWS_RETURNED:		return "OCI_ATTR_ROWS_RETURNED"; 	/* Number of rows returned in current iter - for Bind handles */
	case OCI_ATTR_FOCBK:				return "OCI_ATTR_FOCBK";			/* Failover Callback attribute */
	case OCI_ATTR_IN_V8_MODE:			return "OCI_ATTR_IN_V8_MODE";		/* is the server/service context in V8 mode */
	case OCI_ATTR_LOBEMPTY:				return "OCI_ATTR_LOBEMPTY";			/* empty lob ? */
	case OCI_ATTR_SESSLANG:				return "OCI_ATTR_SESSLANG";			/* session language handle */
	case OCI_ATTR_VISIBILITY:			return "OCI_ATTR_VISIBILITY";		/* visibility */
	case OCI_ATTR_RELATIVE_MSGID:		return "OCI_ATTR_RELATIVE_MSGID";	/* relative message id */
	case OCI_ATTR_SEQUENCE_DEVIATION:	return "OCI_ATTR_SEQUENCE_DEVIATION";	/* sequence deviation */

	case OCI_ATTR_CONSUMER_NAME:		return "OCI_ATTR_CONSUMER_NAME";	/* consumer name */
	case OCI_ATTR_DEQ_MODE:				return "OCI_ATTR_DEQ_MODE";			/* dequeue mode */
	case OCI_ATTR_NAVIGATION:			return "OCI_ATTR_NAVIGATION";		/* navigation */
	case OCI_ATTR_WAIT:					return "OCI_ATTR_WAIT";				/* wait */
	case OCI_ATTR_DEQ_MSGID:			return "OCI_ATTR_DEQ_MSGID";		/* dequeue message id */

	case OCI_ATTR_PRIORITY:				return "OCI_ATTR_PRIORITY";			/* priority */
	case OCI_ATTR_DELAY:				return "OCI_ATTR_DELAY";			/* delay */
	case OCI_ATTR_EXPIRATION:			return "OCI_ATTR_EXPIRATION";		/* expiration */
	case OCI_ATTR_CORRELATION:			return "OCI_ATTR_CORRELATION";		/* correlation id */
	case OCI_ATTR_ATTEMPTS:				return "OCI_ATTR_ATTEMPTS";			/* # of attempts */
	case OCI_ATTR_RECIPIENT_LIST:		return "OCI_ATTR_RECIPIENT_LIST";	/* recipient list */
	case OCI_ATTR_EXCEPTION_QUEUE:		return "OCI_ATTR_EXCEPTION_QUEUE";	/* exception queue name */
	case OCI_ATTR_ENQ_TIME:				return "OCI_ATTR_ENQ_TIME";			/* enqueue time (only OCIAttrGet) */
	case OCI_ATTR_MSG_STATE:			return "OCI_ATTR_MSG_STATE";		/* message state (only OCIAttrGet) */
																			/* NOTE: 64-66 used below */
	case OCI_ATTR_AGENT_NAME:			return "OCI_ATTR_AGENT_NAME";		/* agent name */
	case OCI_ATTR_AGENT_ADDRESS:		return "OCI_ATTR_AGENT_ADDRESS";	/* agent address */
	case OCI_ATTR_AGENT_PROTOCOL:		return "OCI_ATTR_AGENT_PROTOCOL";	/* agent protocol */

	case OCI_ATTR_SENDER_ID:			return "OCI_ATTR_SENDER_ID";		/* sender id */
	case OCI_ATTR_ORIGINAL_MSGID:		return "OCI_ATTR_ORIGINAL_MSGID";	/* original message id */

	case OCI_ATTR_QUEUE_NAME:			return "OCI_ATTR_QUEUE_NAME";		/* queue name */
	case OCI_ATTR_NFY_MSGID:			return "OCI_ATTR_NFY_MSGID";		/* message id */
	case OCI_ATTR_MSG_PROP:				return "OCI_ATTR_MSG_PROP";			/* message properties */

	case OCI_ATTR_NUM_DML_ERRORS:		return "OCI_ATTR_NUM_DML_ERRORS";	/* num of errs in array DML */
	case OCI_ATTR_DML_ROW_OFFSET:		return "OCI_ATTR_DML_ROW_OFFSET";	/* row offset in the array */

	case OCI_ATTR_DATEFORMAT:			return "OCI_ATTR_DATEFORMAT";		/* default date format string */
	case OCI_ATTR_BUF_ADDR:				return "OCI_ATTR_BUF_ADDR";			/* buffer address */
	case OCI_ATTR_BUF_SIZE:				return "OCI_ATTR_BUF_SIZE";			/* buffer size */
	case OCI_ATTR_DIRPATH_MODE:			return "OCI_ATTR_DIRPATH_MODE";		/* mode of direct path operation */
	case OCI_ATTR_DIRPATH_NOLOG:		return "OCI_ATTR_DIRPATH_NOLOG";	/* nologging option */
	case OCI_ATTR_DIRPATH_PARALLEL:		return "OCI_ATTR_DIRPATH_PARALLEL";	/* parallel (temp seg) option */
	case OCI_ATTR_NUM_ROWS:				return "OCI_ATTR_NUM_ROWS"; 		/* number of rows in column array */
																			/* NOTE that OCI_ATTR_NUM_COLS is a column*/
																			/* array attribute too.*/
	case OCI_ATTR_COL_COUNT:			return "OCI_ATTR_COL_COUNT";        /* columns of column array*/
																			/*processed so far.       */
	case OCI_ATTR_STREAM_OFFSET:		return "OCI_ATTR_STREAM_OFFSET";	/* str off of last row processed*/
/*	case OCI_ATTR_SHARED_HEAPALLO:		return "";							Shared Heap Allocation Size */

	case OCI_ATTR_SERVER_GROUP:			return "OCI_ATTR_SERVER_GROUP";		/* server group name */

	case OCI_ATTR_MIGSESSION:			return "OCI_ATTR_MIGSESSION"; 		/* migratable session attribute */

	case OCI_ATTR_NOCACHE:				return "OCI_ATTR_NOCACHE";			/* Temporary LOBs */

	case OCI_ATTR_MEMPOOL_SIZE:			return "OCI_ATTR_MEMPOOL_SIZE";		/* Pool Size */
	case OCI_ATTR_MEMPOOL_INSTNAME:		return "OCI_ATTR_MEMPOOL_INSTNAME";	/* Instance name */
	case OCI_ATTR_MEMPOOL_APPNAME:		return "OCI_ATTR_MEMPOOL_APPNAME";	/* Application name */
	case OCI_ATTR_MEMPOOL_HOMENAME:		return "OCI_ATTR_MEMPOOL_HOMENAME";	/* Home Directory name */
	case OCI_ATTR_MEMPOOL_MODEL:		return "OCI_ATTR_MEMPOOL_MODEL";	/* Pool Model (proc,thrd,both)*/
	case OCI_ATTR_MODES:				return "OCI_ATTR_MODES";			/* Modes */

	case OCI_ATTR_SUBSCR_NAME:			return "OCI_ATTR_SUBSCR_NAME";		/* name of subscription */
	case OCI_ATTR_SUBSCR_CALLBACK:		return "OCI_ATTR_SUBSCR_CALLBACK";	/* associated callback */
	case OCI_ATTR_SUBSCR_CTX:			return "OCI_ATTR_SUBSCR_CTX";		/* associated callback context */
	case OCI_ATTR_SUBSCR_PAYLOAD:		return "OCI_ATTR_SUBSCR_PAYLOAD";	/* associated payload */
	case OCI_ATTR_SUBSCR_NAMESPACE:		return "OCI_ATTR_SUBSCR_NAMESPACE"; /* associated namespace */

	case OCI_ATTR_PROXY_CREDENTIALS:	return "OCI_ATTR_PROXY_CREDENTIALS";	/* Proxy user credentials */
	case OCI_ATTR_INITIAL_CLIENT_ROLES:	return "OCI_ATTR_INITIAL_CLIENT_ROLES";	/* Initial client role list */

	case OCI_ATTR_UNK:					return "OCI_ATTR_UNK";				/* unknown attribute */
	case OCI_ATTR_NUM_COLS:				return "OCI_ATTR_NUM_COLS";			/* number of columns */
	case OCI_ATTR_LIST_COLUMNS:			return "OCI_ATTR_LIST_COLUMNS";		/* parameter of the column list */
	case OCI_ATTR_RDBA:					return "OCI_ATTR_RDBA";				/* DBA of the segment header */
	case OCI_ATTR_CLUSTERED:			return "OCI_ATTR_CLUSTERED";		/* whether the table is clustered */
	case OCI_ATTR_PARTITIONED:			return "OCI_ATTR_PARTITIONED";		/* whether the table is partitioned */
	case OCI_ATTR_INDEX_ONLY:			return "OCI_ATTR_INDEX_ONLY";		/* whether the table is index only */
	case OCI_ATTR_LIST_ARGUMENTS:		return "OCI_ATTR_LIST_ARGUMENTS";	/* parameter of the argument list */
	case OCI_ATTR_LIST_SUBPROGRAMS:		return "OCI_ATTR_LIST_SUBPROGRAMS";	/* parameter of the subprogram list */
	case OCI_ATTR_REF_TDO:				return "OCI_ATTR_REF_TDO";			/* REF to the type descriptor */
	case OCI_ATTR_LINK:					return "OCI_ATTR_LINK";				/* the database link name */
	case OCI_ATTR_MIN:					return "OCI_ATTR_MIN";				/* minimum value */
	case OCI_ATTR_MAX:					return "OCI_ATTR_MAX";				/* maximum value */
	case OCI_ATTR_INCR:					return "OCI_ATTR_INCR";				/* increment value */
	case OCI_ATTR_CACHE:				return "OCI_ATTR_CACHE";			/* number of sequence numbers cached */
	case OCI_ATTR_ORDER:				return "OCI_ATTR_ORDER";			/* whether the sequence is ordered */
	case OCI_ATTR_HW_MARK:				return "OCI_ATTR_HW_MARK";			/* high-water mark */
	case OCI_ATTR_TYPE_SCHEMA:			return "OCI_ATTR_TYPE_SCHEMA";		/* type's schema name */
	case OCI_ATTR_TIMESTAMP:			return "OCI_ATTR_TIMESTAMP";		/* timestamp of the object */
	case OCI_ATTR_NUM_ATTRS:			return "OCI_ATTR_NUM_ATTRS";		/* number of sttributes */
	case OCI_ATTR_NUM_PARAMS:			return "OCI_ATTR_NUM_PARAMS";		/* number of parameters */
	case OCI_ATTR_OBJID:				return "OCI_ATTR_OBJID";			/* object id for a table or view */
	case OCI_ATTR_PTYPE:				return "OCI_ATTR_PTYPE";			/* type of info described by */
	case OCI_ATTR_PARAM:				return "OCI_ATTR_PARAM";			/* parameter descriptor */
	case OCI_ATTR_OVERLOAD_ID:			return "OCI_ATTR_OVERLOAD_ID";		/* overload ID for funcs and procs */
	case OCI_ATTR_TABLESPACE:			return "OCI_ATTR_TABLESPACE";		/* table name space */
	case OCI_ATTR_TDO:					return "OCI_ATTR_TDO";				/* TDO of a type */
	case OCI_ATTR_LTYPE:				return "OCI_ATTR_LTYPE";			/* list type */
	case OCI_ATTR_PARSE_ERROR_OFFSET:	return "OCI_ATTR_PARSE_ERROR_OFFSET";/* Parse Error offset */
	case OCI_ATTR_IS_TEMPORARY:			return "OCI_ATTR_IS_TEMPORARY";		/* whether table is temporary */
	case OCI_ATTR_IS_TYPED:				return "OCI_ATTR_IS_TYPED";			/* whether table is typed */
	case OCI_ATTR_DURATION:				return "OCI_ATTR_DURATION";			/* duration of temporary table */
	case OCI_ATTR_IS_INVOKER_RIGHTS:	return "OCI_ATTR_IS_INVOKER_RIGHTS";/* is invoker rights */
	case OCI_ATTR_OBJ_NAME:				return "OCI_ATTR_OBJ_NAME";			/* top level schema obj name */
	case OCI_ATTR_OBJ_SCHEMA:			return "OCI_ATTR_OBJ_SCHEMA";		/* schema name */
	case OCI_ATTR_OBJ_ID:				return "OCI_ATTR_OBJ_ID";			/* top level schema object id */

	case OCI_ATTR_DIRPATH_SORTED_INDEX:	return "OCI_ATTR_DIRPATH_SORTED_INDEX";/* index that data is sorted on */
																			   /* direct path index maint method (see oci8dp.h) */
	case OCI_ATTR_DIRPATH_INDEX_MAINT_METHOD:	return "OCI_ATTR_DIRPATH_INDEX_MAINT_METHOD";/* parallel load: db file, initial and next extent sizes */

	case OCI_ATTR_DIRPATH_FILE:			return "OCI_ATTR_DIRPATH_FILE";		/* DB file to load into */
	case OCI_ATTR_DIRPATH_STORAGE_INITIAL:		return "OCI_ATTR_DIRPATH_STORAGE_INITIAL";	/* initial extent size */
	case OCI_ATTR_DIRPATH_STORAGE_NEXT:	return "OCI_ATTR_DIRPATH_STORAGE_NEXT";	/* next extent size */


	case OCI_ATTR_TRANS_TIMEOUT:		return "OCI_ATTR_TRANS_TIMEOUT";	/* transaction timeout */
	case OCI_ATTR_SERVER_STATUS:		return "OCI_ATTR_SERVER_STATUS";	/* state of the server handle */
	case OCI_ATTR_STATEMENT:			return "OCI_ATTR_STATEMENT"; 		/* statement txt in stmt hdl */
																			/* statement should not be executed in cache*/
	/*case OCI_ATTR_NO_CACHE:			return "";*/
	case OCI_ATTR_DEQCOND:				return "OCI_ATTR_DEQCOND";			/* dequeue condition */
	case OCI_ATTR_RESERVED_2:			return "OCI_ATTR_RESERVED_2";		/* reserved */


	case OCI_ATTR_SUBSCR_RECPT:			return "OCI_ATTR_SUBSCR_RECPT";		/* recepient of subscription */
	case OCI_ATTR_SUBSCR_RECPTPROTO:	return "OCI_ATTR_SUBSCR_RECPTPROTO";/* protocol for recepient */

	/* 8.2 dpapi support of ADTs */
	case OCI_ATTR_DIRPATH_EXPR_TYPE:	return "OCI_ATTR_DIRPATH_EXPR_TYPE";	/* expr type of OCI_ATTR_NAME */

	case OCI_ATTR_DIRPATH_INPUT:		return "OCI_ATTR_DIRPATH_INPUT";	/* input in text or stream format*/
/*	case OCI_DIRPATH_INPUT_TEXT:				return "";
	case OCI_DIRPATH_INPUT_STREAM:				return "";
	case OCI_DIRPATH_INPUT_UNKNOWN:				return "";	*/
	case OCI_ATTR_LDAP_HOST:			return "OCI_ATTR_LDAP_HOST";		/* LDAP host to connect to */
	case OCI_ATTR_LDAP_PORT:			return "OCI_ATTR_LDAP_PORT";		/* LDAP port to connect to */
	case OCI_ATTR_BIND_DN:				return "OCI_ATTR_BIND_DN";			/* bind DN */
	case OCI_ATTR_LDAP_CRED:			return "OCI_ATTR_LDAP_CRED";		/* credentials to connect to LDAP */
	case OCI_ATTR_WALL_LOC:				return "OCI_ATTR_WALL_LOC";			/* client wallet location */
	case OCI_ATTR_LDAP_AUTH:			return "OCI_ATTR_LDAP_AUTH";		/* LDAP authentication method */
	case OCI_ATTR_LDAP_CTX:				return "OCI_ATTR_LDAP_CTX";			/* LDAP adminstration context DN */
	case OCI_ATTR_SERVER_DNS:			return "OCI_ATTR_SERVER_DNS";		/* list of registration server DNs */

	case OCI_ATTR_DN_COUNT:				return "OCI_ATTR_DN_COUNT";			/* the number of server DNs */
	case OCI_ATTR_SERVER_DN:			return "OCI_ATTR_SERVER_DN";		/* server DN attribute */

	case OCI_ATTR_MAXCHAR_SIZE:			return "OCI_ATTR_MAXCHAR_SIZE";		/* max char size of data */

	case OCI_ATTR_CURRENT_POSITION:		return "OCI_ATTR_CURRENT_POSITION"; /* for scrollable result sets*/

	/* Added to get attributes for ref cursor to statement handle */
	case OCI_ATTR_RESERVED_3:			return "OCI_ATTR_RESERVED_3";		/* reserved */
	case OCI_ATTR_RESERVED_4:			return "OCI_ATTR_RESERVED_4";		/* reserved */
	case OCI_ATTR_DIRPATH_FN_CTX:		return "";							/* fn ctx ADT attrs or args */
	case OCI_ATTR_DIGEST_ALGO:			return "OCI_ATTR_DIRPATH_FN_CTX";	/* digest algorithm */
	case OCI_ATTR_CERTIFICATE:			return "OCI_ATTR_CERTIFICATE";		/* certificate */
	case OCI_ATTR_SIGNATURE_ALGO:		return "OCI_ATTR_SIGNATURE_ALGO";	/* signature algorithm */
	case OCI_ATTR_CANONICAL_ALGO:		return "OCI_ATTR_CANONICAL_ALGO";	/* canonicalization algo. */
	case OCI_ATTR_PRIVATE_KEY:			return "OCI_ATTR_PRIVATE_KEY";		/* private key */
	case OCI_ATTR_DIGEST_VALUE:			return "OCI_ATTR_DIGEST_VALUE";		/* digest value */
	case OCI_ATTR_SIGNATURE_VAL:		return "OCI_ATTR_SIGNATURE_VAL";	/* signature value */
	case OCI_ATTR_SIGNATURE:			return "OCI_ATTR_SIGNATURE";		/* signature */

	/* attributes for setting OCI stmt caching specifics in svchp */
	case OCI_ATTR_STMTCACHESIZE :		return "OCI_ATTR_STMTCACHESIZE";	/* size of the stm cache */

	/* --------------------------- Connection Pool Attributes ------------------ */
	case OCI_ATTR_CONN_NOWAIT:			return "OCI_ATTR_CONN_NOWAIT";
	case OCI_ATTR_CONN_BUSY_COUNT:		return "OCI_ATTR_CONN_BUSY_COUNT";
	case OCI_ATTR_CONN_OPEN_COUNT:		return "OCI_ATTR_CONN_OPEN_COUNT";
	case OCI_ATTR_CONN_TIMEOUT:			return "OCI_ATTR_CONN_TIMEOUT";
	case OCI_ATTR_STMT_STATE:			return "OCI_ATTR_STMT_STATE";
	case OCI_ATTR_CONN_MIN:				return "OCI_ATTR_CONN_MIN";
	case OCI_ATTR_CONN_MAX:				return "OCI_ATTR_CONN_MAX";
	case OCI_ATTR_CONN_INCR:			return "OCI_ATTR_CONN_INCR";

	case OCI_ATTR_DIRPATH_OID:			return "OCI_ATTR_DIRPATH_OID";		/* loading into an OID col */

	case OCI_ATTR_NUM_OPEN_STMTS:		return "OCI_ATTR_NUM_OPEN_STMTS";	/* open stmts in session */
	case OCI_ATTR_DESCRIBE_NATIVE:		return "OCI_ATTR_DESCRIBE_NATIVE";	/* get native info via desc */

	case OCI_ATTR_BIND_COUNT:			return "OCI_ATTR_BIND_COUNT";		/* number of bind postions */
	case OCI_ATTR_HANDLE_POSITION:		return "OCI_ATTR_HANDLE_POSITION";	/* pos of bind/define handle */
	case OCI_ATTR_RESERVED_5:			return "OCI_ATTR_RESERVED_5";		/* reserverd */
	case OCI_ATTR_SERVER_BUSY:			return "OCI_ATTR_SERVER_BUSY";		/* call in progress on server*/

	case OCI_ATTR_DIRPATH_SID:			return "OCI_ATTR_DIRPATH_SID";		/* loading into an SID col */
	/* notification presentation for recipient */
	case OCI_ATTR_SUBSCR_RECPTPRES:		return "OCI_ATTR_SUBSCR_RECPTPRES";
	case OCI_ATTR_TRANSFORMATION:		return "OCI_ATTR_TRANSFORMATION"; 	/* AQ message transformation */

	case OCI_ATTR_ROWS_FETCHED:			return "OCI_ATTR_ROWS_FETCHED";		/* rows fetched in last call */

	/* --------------------------- Snapshot attributes ------------------------- */
	case OCI_ATTR_SCN_BASE:				return "OCI_ATTR_SCN_BASE";			/* snapshot base */
	case OCI_ATTR_SCN_WRAP:				return "OCI_ATTR_SCN_WRAP";			/* snapshot wrap */

	/* --------------------------- Miscellanous attributes --------------------- */
	case OCI_ATTR_RESERVED_6:			return "OCI_ATTR_RESERVED_6";		/* reserved */
	case OCI_ATTR_READONLY_TXN:			return "OCI_ATTR_READONLY_TXN";		/* txn is readonly */
	case OCI_ATTR_RESERVED_7:			return "OCI_ATTR_RESERVED_7";		/* reserved */
	case OCI_ATTR_ERRONEOUS_COLUMN:		return "OCI_ATTR_ERRONEOUS_COLUMN"; /* position of erroneous col */
	case OCI_ATTR_RESERVED_8:			return "OCI_ATTR_RESERVED_8";		/* reserved */

	/* -------------------- 8.2 dpapi support of ADTs continued ---------------- */
	case OCI_ATTR_DIRPATH_OBJ_CONSTR:	return "OCI_ATTR_DIRPATH_OBJ_CONSTR"; /* obj type of subst obj tbl */

	/************************FREE attribute     207      *************************/
	/************************FREE attribute     208      *************************/
	case OCI_ATTR_ENV_UTF16:			return "OCI_ATTR_ENV_UTF16";		/* is env in utf16 mode? */
	case OCI_ATTR_RESERVED_9:			return "OCI_ATTR_RESERVED_9";		/* reserved for TMZ */
	case OCI_ATTR_RESERVED_10:			return "OCI_ATTR_RESERVED_10";		/* reserved */

	/* Attr to allow setting of the stream version PRIOR to calling Prepare */
	case OCI_ATTR_DIRPATH_STREAM_VERSION:	return "OCI_ATTR_DIRPATH_STREAM_VERSION";	/* version of the stream*/
/*	case OCI_ATTR_RESERVED_11:				return "OCI_ATTR_RESERVED_11";	reserved */

	case OCI_ATTR_RESERVED_12:			return "OCI_ATTR_RESERVED_12";		/* reserved */
	case OCI_ATTR_RESERVED_13:			return "OCI_ATTR_RESERVED_13";		/* reserved */

	/* OCI_ATTR_RESERVED_14 */
#ifdef OCI_ATTR_RESERVED_15
	case OCI_ATTR_RESERVED_15:			return "OCI_ATTR_RESERVED_15";		/* reserved */
#endif
#ifdef OCI_ATTR_RESERVED_16
	case OCI_ATTR_RESERVED_16:			return "OCI_ATTR_RESERVED_16";		/* reserved */
#endif

	}
	sv = sv_2mortal(newSViv((IV)attr));
	return SvPV(sv,PL_na);
}

/*used to look up the name of a fetchtype constant
  used only for debugging */
char *
oci_fetch_options(ub4 fetchtype)
{
	dTHX;
	SV *sv;
	switch (fetchtype) {
	/* fetch options */
		case OCI_FETCH_CURRENT:		return "OCI_FETCH_CURRENT";
		case OCI_FETCH_NEXT:		return "OCI_FETCH_NEXT";
		case OCI_FETCH_FIRST:		return "OCI_FETCH_FIRST";
		case OCI_FETCH_LAST:		return "OCI_FETCH_LAST";
		case OCI_FETCH_PRIOR:		return "OCI_FETCH_PRIOR";
		case OCI_FETCH_ABSOLUTE:	return "OCI_FETCH_ABSOLUTE";
		case OCI_FETCH_RELATIVE:	return "OCI_FETCH_RELATIVE";
	}
	sv = sv_2mortal(newSViv((IV)fetchtype));
	return SvPV(sv,PL_na);
}




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*/)
			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;
}


int
oci_error_err(SV *h, OCIError *errhp, sword status, char *what, sb4 force_err)
{

	dTHX;
	D_imp_xxh(h);
	sb4 errcode;
	SV *errstr_sv = sv_newmortal();
	SV *errcode_sv = sv_newmortal();
	errcode = oci_error_get(imp_xxh, errhp, status, what, errstr_sv,
                            DBIc_DBISTATE(imp_xxh)->debug);
	if (CSFORM_IMPLIES_UTF8(SQLCS_IMPLICIT)) {
#ifdef sv_utf8_decode
	sv_utf8_decode(errstr_sv);
#else
	SvUTF8_on(errstr_sv);
#endif
	}

	/* DBIc_ERR *must* be SvTRUE (for RaiseError etc), some */
	/* errors, like OCI_INVALID_HANDLE, don't set errcode. */
	if (force_err)
		errcode = force_err;
	if (status == OCI_SUCCESS_WITH_INFO)
		errcode = 0; /* record as a "warning" for DBI>=1.43 */
	else if (errcode == 0)
		errcode = (status != 0) ? status : -10000;

	sv_setiv(errcode_sv, errcode);
	DBIh_SET_ERR_SV(h, imp_xxh, errcode_sv, errstr_sv, &PL_sv_undef, &PL_sv_undef);
	return 0; /* always returns 0 */

}


char *
ora_sql_error(imp_sth_t *imp_sth, char *msg)
{
	dTHX;
#ifdef OCI_ATTR_PARSE_ERROR_OFFSET
	D_imp_dbh_from_sth;
	SV  *msgsv, *sqlsv;
	char buf[99];
	sword status = 0;
	ub2 parse_error_offset = 0;
	OCIAttrGet_stmhp_stat(imp_sth, &parse_error_offset, 0,
						  OCI_ATTR_PARSE_ERROR_OFFSET, status);
	imp_dbh->parse_error_offset = parse_error_offset;
	if (!parse_error_offset)
		return msg;
	sprintf(buf,"error possibly near <*> indicator at char %d in '",
		parse_error_offset);
	msgsv = sv_2mortal(newSVpv(buf,0));
	sqlsv = sv_2mortal(newSVpv(imp_sth->statement,0));
	sv_insert(sqlsv, parse_error_offset, 0, "<*>", 3);
	sv_catsv(msgsv, sqlsv);
	sv_catpv(msgsv, "'");
	return SvPV(msgsv,PL_na);
#else
	imp_sth = imp_sth; /* not unused */
	return msg;
#endif
}


void *
oci_db_handle(imp_dbh_t *imp_dbh, int handle_type, int flags)
{
	dTHX;
	 switch(handle_type) {
	 	case OCI_HTYPE_ENV:		return imp_dbh->envhp;
	 	case OCI_HTYPE_ERROR:	return imp_dbh->errhp;
	 	case OCI_HTYPE_SERVER:	return imp_dbh->srvhp;
	 	case OCI_HTYPE_SVCCTX:	return imp_dbh->svchp;
	 	case OCI_HTYPE_SESSION:	return imp_dbh->seshp;
	 	/*case OCI_HTYPE_AUTHINFO:return imp_dbh->authp;*/
	 }
	 croak("Can't get OCI handle type %d from DBI database handle", handle_type);
	 if( flags ) {/* For GCC not to warn on unused parameter */}
	 /* satisfy compiler warning, even though croak will never return */
	 return 0;
}

void *
oci_st_handle(imp_sth_t *imp_sth, int handle_type, int flags)
{
	dTHX;
	 switch(handle_type) {
	 	case OCI_HTYPE_ENV:		return imp_sth->envhp;
		case OCI_HTYPE_ERROR:	return imp_sth->errhp;
	 	case OCI_HTYPE_SERVER:	return imp_sth->srvhp;
	 	case OCI_HTYPE_SVCCTX:	return imp_sth->svchp;
	 	case OCI_HTYPE_STMT:	return imp_sth->stmhp;
	 }
	 croak("Can't get OCI handle type %d from DBI statement handle", handle_type);
	 if( flags ) {/* For GCC not to warn on unused parameter */}
	 /* satisfy compiler warning, even though croak will never return */
	 return 0;
}


int
dbd_st_prepare(SV *sth, imp_sth_t *imp_sth, char *statement, SV *attribs)
{
	dTHX;
	D_imp_dbh_from_sth;
	sword status 		 = 0;
	IV  ora_piece_size	 = 0;
	IV  ora_pers_lob	 = 0;
	IV  ora_piece_lob	 = 0;
	IV  ora_clbk_lob	 = 0;
	int ora_check_sql 	 = 1;	/* to force a describe to check SQL	*/
	IV  ora_placeholders = 1;	/* find and handle placeholders */
	/* XXX we set ora_check_sql on for now to force setup of the	*/
	/* row cache. Change later to set up row cache using just a	*/
	/* a memory size, perhaps also default $RowCacheSize to a	*/
	/* negative value. OCI_ATTR_PREFETCH_MEMORY */


	if (!DBIc_ACTIVE(imp_dbh)) {
		oci_error(sth, NULL, OCI_ERROR, "Database disconnected");
		return 0;
	}

	imp_dbh->parse_error_offset = 0;

	imp_sth->done_desc = 0;
	imp_sth->get_oci_handle = oci_st_handle;

	if (DBIc_COMPAT(imp_sth)) {
		static SV *ora_pad_empty;
		if (!ora_pad_empty) {
			ora_pad_empty= perl_get_sv("Oraperl::ora_pad_empty", GV_ADDMULTI);
			if (!SvOK(ora_pad_empty) && getenv("ORAPERL_PAD_EMPTY"))
				sv_setiv(ora_pad_empty, atoi(getenv("ORAPERL_PAD_EMPTY")));
		}
		imp_sth->ora_pad_empty = (SvOK(ora_pad_empty)) ? SvIV(ora_pad_empty) : 0;
	}

	imp_sth->auto_lob = 1;
	imp_sth->exe_mode  = OCI_DEFAULT;

	if (attribs) {
		SV **svp;
		IV ora_auto_lob = 1;
		DBD_ATTRIB_GET_IV(  attribs, "ora_placeholders", 16, svp, ora_placeholders);
		DBD_ATTRIB_GET_IV(  attribs, "ora_auto_lob", 12, svp, ora_auto_lob);
		DBD_ATTRIB_GET_IV(  attribs, "ora_pers_lob", 12, svp, ora_pers_lob);
		DBD_ATTRIB_GET_IV(  attribs, "ora_clbk_lob", 12, svp, ora_clbk_lob);
		DBD_ATTRIB_GET_IV(  attribs, "ora_piece_lob", 13, svp, ora_piece_lob);
		DBD_ATTRIB_GET_IV(  attribs, "ora_piece_size", 14, svp, ora_piece_size);

		imp_sth->auto_lob	= (ora_auto_lob) ? 1 : 0;
		imp_sth->pers_lob	= (ora_pers_lob) ? 1 : 0;
		imp_sth->clbk_lob 	= (ora_clbk_lob) ? 1 : 0;
		imp_sth->piece_lob	= (ora_piece_lob) ? 1 : 0;
		imp_sth->piece_size	= (ora_piece_size) ? ora_piece_size : 0;
		imp_sth->prefetch_rows 	= 0;
		imp_sth->prefetch_memory= 0;
		/* ora_check_sql only works for selects owing to Oracle behaviour */
		DBD_ATTRIB_GET_IV(  attribs, "ora_check_sql", 13, svp, ora_check_sql);
		DBD_ATTRIB_GET_IV(  attribs, "ora_exe_mode", 12, svp, imp_sth->exe_mode);
		DBD_ATTRIB_GET_IV(  attribs, "ora_prefetch_memory",  19, svp, imp_sth->prefetch_memory);
		DBD_ATTRIB_GET_IV(  attribs, "ora_prefetch_rows",  17, svp, imp_sth->prefetch_rows);
		DBD_ATTRIB_GET_IV(  attribs, "ora_row_cache_off",  17, svp, imp_sth->row_cache_off);
		DBD_ATTRIB_GET_IV(  attribs, "ora_verbose",  11, svp, dbd_verbose);
		DBD_ATTRIB_GET_IV(  attribs, "ora_oci_success_warn",  20, svp, oci_warn);
		DBD_ATTRIB_GET_IV(  attribs, "ora_objects",  11, svp, ora_objects);
		DBD_ATTRIB_GET_IV(  attribs, "ora_ncs_buff_mtpl",  17, svp,ora_ncs_buff_mtpl);
        DBD_ATTRIB_GET_IV(  attribs, "RowCacheSize",12,svp, imp_sth->RowCacheSize);

		if (!dbd_verbose)
			DBD_ATTRIB_GET_IV(  attribs, "dbd_verbose",  11, svp, dbd_verbose);
	}


 	/* scan statement for '?', ':1' and/or ':foo' style placeholders	*/
	if (ora_placeholders)
		dbd_preparse(imp_sth, statement);
	else imp_sth->statement = savepv(statement);

	imp_sth->envhp = imp_dbh->envhp;
	imp_sth->errhp = imp_dbh->errhp;
	imp_sth->srvhp = imp_dbh->srvhp;
	imp_sth->svchp = imp_dbh->svchp;



	OCIHandleAlloc_ok(imp_dbh, imp_dbh->envhp, &imp_sth->stmhp, OCI_HTYPE_STMT, status);
	OCIStmtPrepare_log_stat(imp_sth, imp_sth->stmhp, imp_sth->errhp,
			(text*)imp_sth->statement, (ub4)strlen(imp_sth->statement),
			OCI_NTV_SYNTAX, OCI_DEFAULT, status);

	if (status != OCI_SUCCESS) {
		oci_error(sth, imp_sth->errhp, status, "OCIStmtPrepare");
		OCIHandleFree_log_stat(imp_sth, imp_sth->stmhp, OCI_HTYPE_STMT, status);

		return 0;
	}


	OCIAttrGet_stmhp_stat(imp_sth, &imp_sth->stmt_type, 0, OCI_ATTR_STMT_TYPE, status);

	if (DBIc_DBISTATE(imp_sth)->debug >= 3 || dbd_verbose >= 3 )
		PerlIO_printf(
            DBIc_LOGPIO(imp_sth),
            "	dbd_st_prepare'd sql %s ( auto_lob%d, check_sql%d)\n",
			oci_stmt_type_name(imp_sth->stmt_type),
			imp_sth->auto_lob, ora_check_sql);

	DBIc_IMPSET_on(imp_sth);

	if (ora_check_sql) {
		if (!dbd_describe(sth, imp_sth))
			return 0;
	}

	return 1;
}


sb4
dbd_phs_in(dvoid *octxp, OCIBind *bindp, ub4 iter, ub4 index,
		  dvoid **bufpp, ub4 *alenp, ub1 *piecep, dvoid **indpp)
{
	dTHX;
	phs_t *phs = (phs_t*)octxp;
	STRLEN phs_len;
	AV *tuples_av;
	SV *sv;
	AV *av;
	SV **sv_p;
	if( bindp ){ /* For GCC not to warn on unused parameter*/ }

	tuples_av = phs->imp_sth->bind_tuples;
	if(tuples_av) {
		/* NOTE: we already checked the validity in ora_st_bind_for_array_exec(). */
		sv_p = av_fetch(tuples_av, phs->imp_sth->rowwise ? (int)iter : phs->idx, 0);
		av = (AV*)SvRV(*sv_p);
		sv_p = av_fetch(av, phs->imp_sth->rowwise ? phs->idx : (int)iter, 0);
		sv = *sv_p;
		if(SvOK(sv)) {
			*bufpp = SvPV(sv, phs_len);
			phs->alen = (phs->alen_incnull) ? phs_len+1 : phs_len;
			phs->indp = 0;
		}
		else {
			*bufpp = SvPVX(sv);
			phs->alen = 0;
			phs->indp = -1;
		}
	}
	else
		if (phs->desc_h) {
			*bufpp  = phs->desc_h;
			phs->alen = 0;
			phs->indp = 0;
		}
		else
			if (SvOK(phs->sv)) {
				*bufpp  = SvPV(phs->sv, phs_len);
				phs->alen = (phs->alen_incnull) ? phs_len+1 : phs_len;;
				phs->indp = 0;
			}
			else {
				*bufpp  = SvPVX(phs->sv);	/* not actually used? */
				phs->alen = 0;
				phs->indp = -1;
			}
	*alenp  = phs->alen;
	*indpp  = &phs->indp;
	*piecep = OCI_ONE_PIECE;
	/* MJE commented out as we are avoiding DBIS now but as this is
	   an Oracle callback there is no way to pass something non
	   OCI into this func.

	if (DBIS->debug >= 3 || dbd_verbose >= 3 )
		PerlIO_printf(DBILOGFP, "		in  '%s' [%lu,%lu]: len %2lu, ind %d%s, value=%s\n",
			phs->name, ul_t(iter), ul_t(index), ul_t(phs->alen), phs->indp,
			(phs->desc_h) ? " via descriptor" : "",neatsvpv(phs->sv,10));
	*/
	if (!tuples_av && (index > 0 || iter > 0))
		croak(" Arrays and multiple iterations not currently supported by DBD::Oracle (in %d/%d)", index,iter);

	return OCI_CONTINUE;
}

/*
``Binding and Defining''

Binding RETURNING...INTO variables

As mentioned in the previous section, an OCI application implements the placeholders in the RETURNING clause as
pure OUT bind variables. An application must adhere to the following rules when working with these bind variables:

  1.Bind RETURNING clause placeholders in OCI_DATA_AT_EXEC mode using OCIBindByName() or
	OCIBindByPos(), followed by a call to OCIBindDynamic() for each placeholder.

	Note: The OCI only supports the callback mechanism for RETURNING clause binds. The polling mechanism is
	not supported.

  2.When binding RETURNING clause placeholders, you must supply a valid out bind function as the ocbfp
	parameter of the OCIBindDynamic() call. This function must provide storage to hold the returned data.
  3.The icbfp parameter of OCIBindDynamic() call should provide a "dummy" function which returns NULL values
	when called.
  4.The piecep parameter of OCIBindDynamic() must be set to OCI_ONE_PIECE.
  5.No duplicate binds are allowed in a DML statement with a RETURNING clause (i.e., no duplication between bind
	variables in the DML section and the RETURNING section of the statement).

When a callback function is called, the OCI_ATTR_ROWS_RETURNED attribute of the bind handle tells the
application the number of rows being returned in that particular iteration. Thus, when the callback is called the first
time in a particular iteration (i.e., index=0), the user can allocate space for all the rows which will be returned for that
bind variable. When the callback is called subsequently (with index>0) within the same iteration, the user can merely
increment the buffer pointer to the correct memory within the allocated space to retrieve the data.

Every bind handle has a OCI_ATTR_MAXDATA_SIZE attribute. This attribute specifies the number of bytes to be
allocated on the server to accommodate the client-side bind data after any necessary character set conversions.

	Note: Character set conversions performed when data is sent to the server may result in the data expanding or
	contracting, so its size on the client may not be the same as its size on the server.

An application will typically set OCI_ATTR_MAXDATA_SIZE to the maximum size of the column or the size of the
PL/SQL variable, depending on how it is used. Oracle issues an error if OCI_ATTR_MAXDATA_SIZE is not a large
enough value to accommodate the data after conversion, and the operation will fail.
*/

sb4
dbd_phs_out(dvoid *octxp, OCIBind *bindp,
	ub4 iter,	/* execution itteration (0...)	*/
	ub4 index,	/* array index (0..)		*/
	dvoid **bufpp,	/* A pointer to a buffer to write the bind value/piece.	*/
	ub4 **alenpp,	/* A pointer to a storage for OCI to fill in the size	*/
			/* of the bind value/piece after it has been read.	*/
	ub1 *piecep,	/* */
	dvoid **indpp,	/* Return a pointer to contain the indicator value which either an sb2	*/
			/* value or a pointer to an indicator structure for named data types.	*/
	ub2 **rcodepp)	/* Returns a pointer to contains the return code.	*/
{
	dTHX;
	phs_t *phs = (phs_t*)octxp;	/* context */
	/*imp_sth_t *imp_sth = phs->imp_sth;*/
	if( bindp ) { /* For GCC not to warn on unused parameter */ }

	if (phs->desc_h) { /* a  descriptor if present  (LOBs etc)*/
		*bufpp  = phs->desc_h;
		phs->alen = 0;

	}
	else {
		SV *sv = phs->sv;

		if (SvTYPE(sv) == SVt_RV && SvTYPE(SvRV(sv)) == SVt_PVAV) {
			sv = *av_fetch((AV*)SvRV(sv), (IV)iter, 1);
			if (!SvOK(sv))
				sv_setpv(sv,"");
		}

        *bufpp = SvGROW(sv, (size_t)(((phs->maxlen < 28) ? 28 : phs->maxlen)));
		phs->alen = SvLEN(sv);	/* max buffer size now, actual data len later */

	}
	*alenpp = &phs->alen;
	*indpp  = &phs->indp;
	*rcodepp= &phs->arcode;
    /* MJE commented out as we are avoiding DBIS now but as this is
       an Oracle callback there is no way to pass something non
       OCI into this func.

	if (DBIS->debug >= 3 || dbd_verbose >= 3 )
 		PerlIO_printf(DBILOGFP, "		out '%s' [%ld,%ld]: alen %2ld, piece %d%s\n",
			phs->name, ul_t(iter), ul_t(index), ul_t(phs->alen), *piecep,
			(phs->desc_h) ? " via descriptor" : "");
    */
	*piecep = OCI_ONE_PIECE;
	return OCI_CONTINUE;
}

/* --------------------------------------------------------------
	Fetch callback fill buffers.
	Finaly figured out how this fucntion works
	Seems it is like this. The function inits and then fills the
	buffer (fb_ary->abuf) with the data from the select until it
	either runs out of data or its piece size is reached
	(fb_ary->bufl).  If its piece size is reached it then goes and gets
	the the next piece and sets *piecep ==OCI_NEXT_PIECE at this point
	I take the data in the buffer and memcpy it onto my buffer
	(fb_ary->cb_abuf). This will go on until it runs out of full pieces
	so when it returns to back to the fetch I add what remains in
	(fb_ary->bufl) (the last piece) and memcpy onto my  buffer (fb_ary->cb_abuf)
	to get it all.  I also take set fb_ary->cb_abuf back to empty just
	to keep things clean
 -------------------------------------------------------------- */
sb4
presist_lob_fetch_cbk(dvoid *octxp, OCIDefine *dfnhp, ub4 iter, dvoid **bufpp,
					  ub4 **alenpp, ub1 *piecep, dvoid **indpp, ub2 **rcpp)
{
	dTHX;
	imp_fbh_t	*fbh =(imp_fbh_t*)octxp;
	fb_ary_t	*fb_ary;
	fb_ary	= fbh->fb_ary;
	*bufpp	= (dvoid *) fb_ary->abuf;
	*alenpp	= &fb_ary->bufl;
	*indpp	= (dvoid *) fb_ary->aindp;
	*rcpp	= fb_ary->arcode;


	if (dbd_verbose >= 5 ) {
		PerlIO_printf(DBILOGFP, " In presist_lob_fetch_cbk\n");
	}

	if ( *piecep ==OCI_NEXT_PIECE ){/*more than one piece*/

		memcpy(fb_ary->cb_abuf+fb_ary->piece_count*fb_ary->bufl,fb_ary->abuf,fb_ary->bufl );
	/*as we will be using both blobs and clobs we have to use
	  pointer arithmetic to get the values right.  in this case we simply
	  copy all of the memory of the buff into the cb buffer starting
	  at the piece count * the  buffer length
	  */

		fb_ary->piece_count++;/*used to tell me how many pieces I have, Might be able to use aindp for this?*/

	}


	return OCI_CONTINUE;

}

/* TAF or Transparent Application Failoever callback
   Works like this.  The fuction below is registered on the server,
   when the server is set up to use it, when an exe is called (not sure about other server round trips)
   and the server fails tt should get into this cbk error below.
   It will wait X seconds and then try to reconnect (up to n times if that is the users choice)
   That is how I see it working */

sb4
taf_cbk(dvoid *svchp, dvoid *envhp, dvoid *fo_ctx,ub4 fo_type, ub4 fo_event )
{
	dTHX;
    int return_count;
    int ret;
	taf_callback_t *cb =(taf_callback_t*)fo_ctx;

	dSP;
	PUSHMARK(SP);
	XPUSHs(sv_2mortal(newSViv(fo_event)));
	XPUSHs(sv_2mortal(newSViv(fo_type)));
    XPUSHs(SvRV(cb->dbh_ref));

	PUTBACK;
	return_count = call_sv(cb->function, G_SCALAR);

    SPAGAIN;

    if (return_count != 1)
        croak("Expected one scalar back from taf handler");

    ret = POPi;

	switch (fo_event){

		case OCI_FO_BEGIN:
		case OCI_FO_ABORT:
		case OCI_FO_END:
		case OCI_FO_REAUTH:
		{
			break;
		}
		case OCI_FO_ERROR:
		{
            if (ret == OCI_FO_RETRY) {
                return OCI_FO_RETRY;
            }
			break;
		}

		default:
		{
			break;
		}
	}
    PUTBACK;

	return 0;
}


sb4
reg_taf_callback(SV *dbh, imp_dbh_t *imp_dbh)
{
	dTHX;
	OCIFocbkStruct 	tafailover;
	sword 			status;

    imp_dbh->taf_ctx.function = imp_dbh->taf_function;
    imp_dbh->taf_ctx.dbh_ref = newRV_inc(dbh);

	if (dbd_verbose >= 5 ) {
  		PerlIO_printf(DBIc_LOGPIO(imp_dbh), " In reg_taf_callback\n");
	}

/* set the context up as a pointer to the taf callback struct*/
	tafailover.fo_ctx = &imp_dbh->taf_ctx;
	tafailover.callback_function = &taf_cbk;

/* register the callback */
	OCIAttrSet_log_stat(imp_dbh, imp_dbh->srvhp, (ub4) OCI_HTYPE_SERVER,
                        (dvoid *) &tafailover, (ub4) 0,
                        (ub4) OCI_ATTR_FOCBK, imp_dbh->errhp, status);

	return status;
}

#ifdef UTF8_SUPPORT
/* How many bytes are n utf8 chars in buffer */
static ub4
ora_utf8_to_bytes (ub1 *buffer, ub4 chars_wanted, ub4 max_bytes)
{
	dTHX;
	ub4 i = 0;
	while (i < max_bytes && (chars_wanted-- > 0)) {
		i += UTF8SKIP(&buffer[i]);
	}
	return (i < max_bytes)? i : max_bytes;
}


#if 0 /* save this for later just in case... */
/* Given the 5.6.0 implementation of utf8 handling in perl,
 * avoid setting the UTF8 flag as much as possible. Almost
 * every binary operator in Perl will do conversions when
 * strings marked as UTF8 are involved.
 * Maybe setting the flag should be default in Japan or
 * Europe? Deduce that from NLS_LANG? Possibly...
 */

int
set_utf8(SV *sv) {
	ub1 *c;
	for (c = (ub1*)SvPVX(sv); c < (ub1*)SvEND(sv); c++) {
		if (*c & 0x80) {
			SvUTF8_on(sv);
			return 1;
		}
	}
	return 0;
}
#endif
#endif

/* PerlIO_printf(DBILOGFP, "lab datalen=%d long_readlen=%d bytelen=%d\n" ,datalen ,imp_sth->long_readlen, bytelen ); */
static int	/* LONG and LONG RAW */
fetch_func_varfield(SV *sth, imp_fbh_t *fbh, SV *dest_sv)
{
	dTHX;
	D_imp_sth(sth);
	D_imp_dbh_from_sth ;
	D_imp_drh_from_dbh ;
	fb_ary_t *fb_ary = fbh->fb_ary;
	char *p = (char*)&fb_ary->abuf[0];
	ub4 datalen = *(ub4*)p;	 /* XXX alignment ? */
	p += 4;

#ifdef UTF8_SUPPORT
	if (fbh->ftype == 94) {
		if (datalen > imp_sth->long_readlen) {
			ub4 bytelen = ora_utf8_to_bytes((ub1*)p, (ub4)imp_sth->long_readlen, datalen);

			if (bytelen < datalen ) {	/* will be truncated */
				int oraperl = DBIc_COMPAT(imp_sth);
				if (DBIc_has(imp_sth,DBIcf_LongTruncOk) || (oraperl && SvIV(imp_drh->ora_trunc))) {
					/* user says truncation is ok */
					/* Oraperl recorded the truncation in ora_errno so we	*/
					/* so also but only for Oraperl mode handles.		*/
					if (oraperl) sv_setiv(DBIc_ERR(imp_sth), 1406);
				} else {
					char buf[300];
					sprintf(buf,"fetching field %d of %d. LONG value truncated from %lu to %lu. %s",
						fbh->field_num+1, DBIc_NUM_FIELDS(imp_sth), ul_t(datalen), ul_t(bytelen),
						"DBI attribute LongReadLen too small and/or LongTruncOk not set");
					oci_error_err(sth, NULL, OCI_ERROR, buf, 24345); /* appropriate ORA error number */
					sv_set_undef(dest_sv);
					return 0;
				}

                if (DBIc_DBISTATE(imp_sth)->debug >= 3 || dbd_verbose >= 3 )
				PerlIO_printf(
                    DBIc_LOGPIO(imp_sth),
                    "		fetching field %d of %d. LONG value truncated from "
                    "%lu to %lu.\n",
					fbh->field_num+1, DBIc_NUM_FIELDS(imp_sth),
					ul_t(datalen), ul_t(bytelen));
					datalen = bytelen;
			}
	}
	sv_setpvn(dest_sv, p, (STRLEN)datalen);
	if (CSFORM_IMPLIES_UTF8(fbh->csform))
		SvUTF8_on(dest_sv);
	} else {
#else
	{
#endif
	sv_setpvn(dest_sv, p, (STRLEN)datalen);
	}

	return 1;
}

static void
fetch_cleanup_rset(SV *sth, imp_fbh_t *fbh)
{
	dTHX;
    D_imp_sth(sth);
	SV *sth_nested = (SV *)fbh->special;
	fbh->special = NULL;

	if( sth ) { /* For GCC not to warn on unused parameter */ }
	if (sth_nested) {
	dTHR;
	D_impdata(imp_sth_nested, imp_sth_t, sth_nested);
		int fields = DBIc_NUM_FIELDS(imp_sth_nested);
	int i;
	for(i=0; i < fields; ++i) {
		imp_fbh_t *fbh_nested = &imp_sth_nested->fbh[i];
		if (fbh_nested->fetch_cleanup)
		fbh_nested->fetch_cleanup(sth_nested, fbh_nested);
	}
	if (DBIc_DBISTATE(imp_sth)->debug >= 3 || dbd_verbose >= 3 )
		PerlIO_printf(DBIc_LOGPIO(imp_sth),
			"	fetch_cleanup_rset - deactivating handle %s (defunct nested cursor)\n",
						neatsvpv(sth_nested, 0));

	DBIc_ACTIVE_off(imp_sth_nested);
	SvREFCNT_dec(sth_nested);
	}
}

static int
fetch_func_rset(SV *sth, imp_fbh_t *fbh, SV *dest_sv)
{
	dTHX;
	OCIStmt *stmhp_nested = ((OCIStmt **)fbh->fb_ary->abuf)[0];
	dTHR;
	D_imp_sth(sth);
	D_imp_dbh_from_sth;
	dSP;
	HV *init_attr = newHV();
	int count;

	if (DBIc_DBISTATE(imp_sth)->debug >= 3 || dbd_verbose >= 3 )
		PerlIO_printf(
            DBIc_LOGPIO(imp_sth),
            "	fetch_func_rset - allocating handle for cursor nested within %s ...\n",
            neatsvpv(sth, 0));

	ENTER; SAVETMPS; PUSHMARK(SP);
	XPUSHs(sv_2mortal(newRV((SV*)DBIc_MY_H(imp_dbh))));
	XPUSHs(sv_2mortal(newRV((SV*)init_attr)));
	PUTBACK;
	count = perl_call_pv("DBI::_new_sth", G_ARRAY);
	SPAGAIN;
	if (count != 2)
		croak("panic: DBI::_new_sth returned %d values instead of 2", count);

	if(POPs){} /* For GCC not to warn on unused result */

	sv_setsv(dest_sv, POPs);
	SvREFCNT_dec(init_attr);
	PUTBACK; FREETMPS; LEAVE;

	if (DBIc_DBISTATE(imp_sth)->debug >= 3 || dbd_verbose >= 3 )
		PerlIO_printf(
            DBIc_LOGPIO(imp_sth),
            "	fetch_func_rset - ... allocated %s for nested cursor\n",
            neatsvpv(dest_sv, 0));

	fbh->special = (void *)newSVsv(dest_sv);

	{
		D_impdata(imp_sth_nested, imp_sth_t, dest_sv);
		imp_sth_nested->envhp = imp_sth->envhp;
		imp_sth_nested->errhp = imp_sth->errhp;
		imp_sth_nested->srvhp = imp_sth->srvhp;
		imp_sth_nested->svchp = imp_sth->svchp;

		imp_sth_nested->stmhp = stmhp_nested;
		imp_sth_nested->nested_cursor = 1;
		imp_sth_nested->stmt_type = OCI_STMT_SELECT;

		DBIc_IMPSET_on(imp_sth_nested);
		DBIc_ACTIVE_on(imp_sth_nested);  /* So describe won't do an execute */

		if (!dbd_describe(dest_sv, imp_sth_nested))
			return 0;
	}

	return 1;
}
/* ------ */


int
dbd_rebind_ph_rset(SV *sth, imp_sth_t *imp_sth, phs_t *phs)
{
	dTHX;

	if (DBIc_DBISTATE(imp_sth)->debug >= 6 || dbd_verbose >= 6 )
		PerlIO_printf(
            DBIc_LOGPIO(imp_sth),
            "	 dbd_rebind_ph_rset phs->is_inout=%d\n",
            phs->is_inout);

/* Only do this part for inout cursor refs because pp_exec_rset only gets called for all the output params */
	if (phs->is_inout) {
		phs->out_prepost_exec = pp_exec_rset;
		return 2;	/* OCI bind done */
	}
	else {
	/* Call a special rebinder for cursor ref "in" params */
		return(pp_rebind_ph_rset_in(sth, imp_sth, phs));
	}
}


/* ------ */
static int
fetch_lob(SV *sth, imp_sth_t *imp_sth, OCILobLocator* lobloc, int ftype, SV *dest_sv, char *name);

static int
lob_phs_post_execute(SV *sth, imp_sth_t *imp_sth, phs_t *phs, int pre_exec)
{
	dTHX;
	if (pre_exec)
		return 1;
	/* fetch PL/SQL LOB data */
	if (imp_sth->auto_lob && (
		imp_sth->stmt_type == OCI_STMT_BEGIN ||
		imp_sth->stmt_type == OCI_STMT_DECLARE )) {
		return fetch_lob(sth, imp_sth, (OCILobLocator*) phs->desc_h, phs->ftype, phs->sv, phs->name);
	}

	sv_setref_pv(phs->sv, "OCILobLocatorPtr", (void*)phs->desc_h);

	return 1;
}

int
dbd_rebind_ph_lob(SV *sth, imp_sth_t *imp_sth, phs_t *phs)
{
	dTHX;
	D_imp_dbh_from_sth ;
	sword status;
	ub4 lobEmpty = 0;
    if (phs->desc_h && phs->desc_t == OCI_DTYPE_LOB)
		ora_free_templob(sth, imp_sth, (OCILobLocator*)phs->desc_h);

	if (!phs->desc_h) {
		++imp_sth->has_lobs;
		phs->desc_t = OCI_DTYPE_LOB;
		OCIDescriptorAlloc_ok(imp_sth, imp_sth->envhp,
				&phs->desc_h, phs->desc_t);
	}

	OCIAttrSet_log_stat(imp_sth, phs->desc_h, phs->desc_t,
			&lobEmpty, 0, OCI_ATTR_LOBEMPTY, imp_sth->errhp, status);

	if (status != OCI_SUCCESS)
		return oci_error(sth, imp_sth->errhp, status, "OCIAttrSet OCI_ATTR_LOBEMPTY");

	if (!SvPOK(phs->sv)) {	 /* normalizations for special cases	 */
		if (SvOK(phs->sv)) {	/* ie a number, convert to string ASAP  */
			if (!(SvROK(phs->sv) && phs->is_inout))
				sv_2pv(phs->sv, &PL_na);
		}
		else { /* ensure we're at least an SVt_PV (so SvPVX etc work)	 */
			(void)SvUPGRADE(phs->sv, SVt_PV);
		}
	}

	phs->indp	= (SvOK(phs->sv)) ? 0 : -1;
	phs->progv  = (char*)&phs->desc_h;
	phs->maxlen = sizeof(OCILobLocator*);

	if (phs->is_inout)
		phs->out_prepost_exec = lob_phs_post_execute;
	/* accept input LOBs */

	if (sv_isobject(phs->sv) && sv_derived_from(phs->sv, "OCILobLocatorPtr")) {

		OCILobLocator *src;
		OCILobLocator **dest;
		src = INT2PTR(OCILobLocator *, SvIV(SvRV(phs->sv)));
		dest = (OCILobLocator **) phs->progv;

		OCILobLocatorAssign_log_stat(imp_dbh, imp_dbh->svchp, imp_sth->errhp, src, dest, status);
		if (status != OCI_SUCCESS) {
			oci_error(sth, imp_sth->errhp, status, "OCILobLocatorAssign");
			return 0;
		}
	}

	/* create temporary LOB for PL/SQL placeholder */
	else if (imp_sth->stmt_type == OCI_STMT_BEGIN ||
		imp_sth->stmt_type == OCI_STMT_DECLARE) {
		ub4 amtp;

		(void)SvUPGRADE(phs->sv, SVt_PV);

		amtp = SvCUR(phs->sv);		/* XXX UTF8? */

		/* Create a temp lob for non-empty string */

		if (amtp > 0) {
			ub1 lobtype = (phs->ftype == 112 ? OCI_TEMP_CLOB : OCI_TEMP_BLOB);
			OCILobCreateTemporary_log_stat(imp_dbh, imp_dbh->svchp, imp_sth->errhp,
				(OCILobLocator *) phs->desc_h, (ub2) OCI_DEFAULT,
				(ub1) OCI_DEFAULT, lobtype, TRUE, OCI_DURATION_SESSION, status);
			if (status != OCI_SUCCESS) {
				oci_error(sth, imp_sth->errhp, status, "OCILobCreateTemporary");
				return 0;
			}

			if( ! phs->csid ) {
				ub1 csform = SQLCS_IMPLICIT;
				ub2 csid = 0;
				OCILobCharSetForm_log_stat(imp_sth,
                                           imp_sth->envhp,
                                           imp_sth->errhp,
                                           (OCILobLocator*)phs->desc_h,
                                           &csform,
                                           status );
				if (status != OCI_SUCCESS)
					return oci_error(sth, imp_sth->errhp, status, "OCILobCharSetForm");
#ifdef OCI_ATTR_CHARSET_ID
			/* Effectively only used so AL32UTF8 works properly */
				OCILobCharSetId_log_stat(imp_sth,
                                         imp_sth->envhp,
                                         imp_sth->errhp,
                                         (OCILobLocator*)phs->desc_h,
                                         &csid,
                                         status );
				if (status != OCI_SUCCESS)
					return oci_error(sth, imp_sth->errhp, status, "OCILobCharSetId");
#endif /* OCI_ATTR_CHARSET_ID */
		/* if data is utf8 but charset isn't then switch to utf8 csid */
				csid = (SvUTF8(phs->sv) && !CS_IS_UTF8(csid)) ? utf8_csid : CSFORM_IMPLIED_CSID(csform);
				phs->csid = csid;
				phs->csform = csform;
			}

			if (DBIc_DBISTATE(imp_sth)->debug >= 3 || dbd_verbose >= 3 )
				PerlIO_printf(
                    DBIc_LOGPIO(imp_sth),
                    "	  calling OCILobWrite phs->csid=%d phs->csform=%d amtp=%d\n",
					phs->csid, phs->csform, amtp );

		/* write lob data */

			OCILobWrite_log_stat(imp_sth, imp_sth->svchp, imp_sth->errhp,
				(OCILobLocator*)phs->desc_h, &amtp, 1, SvPVX(phs->sv), amtp, OCI_ONE_PIECE,
					0,0, phs->csid, phs->csform, status);
			if (status != OCI_SUCCESS) {
				return oci_error(sth, imp_sth->errhp, status, "OCILobWrite in dbd_rebind_ph_lob");
			}
		}
	}
	return 1;
}


#ifdef UTF8_SUPPORT
ub4
ora_blob_read_mb_piece(SV *sth, imp_sth_t *imp_sth, imp_fbh_t *fbh,
  SV *dest_sv, long offset, ub4 len, long destoffset)
{
	dTHX;
	ub4 loblen = 0;
	ub4 buflen;
	ub4 amtp = 0;
	ub4 byte_destoffset = 0;
	OCILobLocator *lobl = (OCILobLocator*)fbh->desc_h;
	sword ftype = fbh->ftype;
	sword status;

	/*
	 * We assume our caller has already done the
	 * equivalent of the following:
	 *		(void)SvUPGRADE(dest_sv, SVt_PV);
	 */
	ub1 csform = SQLCS_IMPLICIT;

	OCILobCharSetForm_log_stat(imp_sth,
                               imp_sth->envhp,
                               imp_sth->errhp,
                               lobl,
                               &csform,
                               status );
	if (status != OCI_SUCCESS) {
		oci_error(sth, imp_sth->errhp, status, "OCILobCharSetForm");
		sv_set_undef(dest_sv);	/* signal error */
		return 0;
	}
	if (ftype != ORA_CLOB) {
		oci_error(sth, imp_sth->errhp, OCI_ERROR,
			"blob_read not currently supported for non-CLOB types with OCI 8 "
			"(but with OCI 8 you can set $dbh->{LongReadLen} to the length you need,"
		"so you don't need to call blob_read at all)");
		sv_set_undef(dest_sv);	/* signal error */
		return 0;
	}

	OCILobGetLength_log_stat(imp_sth, imp_sth->svchp, imp_sth->errhp,
				 lobl, &loblen, status);
	if (status != OCI_SUCCESS) {
		oci_error(sth, imp_sth->errhp, status, "OCILobGetLength ora_blob_read_mb_piece");
		sv_set_undef(dest_sv);	/* signal error */
		return 0;
	}

	loblen -= offset;	/* only count from offset onwards */
	amtp = (loblen > len) ? len : loblen;
	buflen = 4 * amtp;

	byte_destoffset = ora_utf8_to_bytes((ub1 *)(SvPVX(dest_sv)),
					(ub4)destoffset, SvCUR(dest_sv));

	if (loblen > 0) {
		ub1 *dest_bufp;
		ub1 *buffer;

		New(42, buffer, buflen, ub1);

		OCILobRead_log_stat(imp_sth, imp_sth->svchp, imp_sth->errhp, lobl,
				&amtp, (ub4)1 + offset, buffer, buflen,
				0, 0, (ub2)0 ,csform ,status );
			  /* lab  0, 0, (ub2)0, (ub1)SQLCS_IMPLICIT, status); */

		if (dbis->debug >= 3 || dbd_verbose >= 3 )
			PerlIO_printf(
                DBIc_LOGPIO(imp_sth),
                "		OCILobRead field %d %s: LOBlen %lu, LongReadLen %lu, "
                "BufLen %lu, Got %lu\n",
				fbh->field_num+1, oci_status_name(status), ul_t(loblen),
				ul_t(imp_sth->long_readlen), ul_t(buflen), ul_t(amtp));
		if (status != OCI_SUCCESS) {
			oci_error(sth, imp_sth->errhp, status, "OCILobRead");
			sv_set_undef(dest_sv);	/* signal error */
			return 0;
		}

		amtp = ora_utf8_to_bytes(buffer, len, amtp);
		SvGROW(dest_sv, byte_destoffset + amtp + 1);
		dest_bufp = (ub1 *)(SvPVX(dest_sv));
		dest_bufp += byte_destoffset;
		memcpy(dest_bufp, buffer, amtp);
		Safefree(buffer);
	}
	else {
		assert(amtp == 0);
		SvGROW(dest_sv, byte_destoffset + 1);
		if (dbis->debug >= 3 || dbd_verbose >= 3 )
			PerlIO_printf(
                DBIc_LOGPIO(imp_sth),
				"		OCILobRead field %d %s: LOBlen %lu, LongReadLen %lu, "
                "BufLen %lu, Got %lu\n",
                fbh->field_num+1, "SKIPPED", (unsigned long)loblen,
                (unsigned long)imp_sth->long_readlen, (unsigned long)buflen,
                (unsigned long)amtp);
	}

	if (dbis->debug >= 3 || dbd_verbose >= 3 )
		PerlIO_printf(
            DBIc_LOGPIO(imp_sth),
            "	blob_read field %d, ftype %d, offset %ld, len %lu, "
            "destoffset %ld, retlen %lu\n",
			fbh->field_num+1, ftype, offset, (unsigned long) len,
                        destoffset, ul_t(amtp));

	SvCUR_set(dest_sv, byte_destoffset+amtp);
	*SvEND(dest_sv) = '\0'; /* consistent with perl sv_setpvn etc	*/
	SvPOK_on(dest_sv);
	if (ftype == ORA_CLOB && CSFORM_IMPLIES_UTF8(csform))
		SvUTF8_on(dest_sv);

	return 1;
}
#endif /* ifdef UTF8_SUPPORT */

ub4
ora_blob_read_piece(SV *sth, imp_sth_t *imp_sth, imp_fbh_t *fbh, SV *dest_sv,
			long offset, UV len, long destoffset)
{
	dTHX;
	ub4 loblen	= 0;
	ub4 buflen;
	ub4 amtp 	= 0;
	ub1 csform	= 0;
	OCILobLocator *lobl = (OCILobLocator*)fbh->desc_h;
	sword ftype	= fbh->ftype;
	sword status;
	char *type_name;

	if (ftype == ORA_CLOB)
		type_name = "CLOB";
	else if (ftype == ORA_BLOB)
		type_name = "BLOB";
	else if (ftype == ORA_BFILE)
		type_name = "BFILE";
	else {
		oci_error(sth, imp_sth->errhp, OCI_ERROR,
			"blob_read not currently supported for non-LOB types with OCI 8 "
			"(but with OCI 8 you can set $dbh->{LongReadLen} to the length you need,"
			"so you don't need to call blob_read at all)");
		sv_set_undef(dest_sv);	/* signal error */
		return 0;
	}

	OCILobGetLength_log_stat(imp_sth, imp_sth->svchp, imp_sth->errhp, lobl, &loblen, status);
	if (status != OCI_SUCCESS) {
		oci_error(sth, imp_sth->errhp, status, "OCILobGetLength ora_blob_read_piece");
		sv_set_undef(dest_sv);	/* signal error */
		return 0;
	}

	OCILobCharSetForm_log_stat(imp_sth,
                               imp_sth->envhp,
                               imp_sth->errhp,
                               lobl,
                               &csform,
                               status );
	if (status != OCI_SUCCESS) {
		oci_error(sth, imp_sth->errhp, status, "OCILobCharSetForm");
		sv_set_undef(dest_sv);	/* signal error */
		return 0;
	}
	if (ftype == ORA_CLOB && csform == SQLCS_NCHAR)
		type_name = "NCLOB";

	/*
	 * We assume our caller has already done the
	 * equivalent of the following:
	 *		(void)SvUPGRADE(dest_sv, SVt_PV);
	 *		SvGROW(dest_sv, buflen+destoffset+1);
	 */

	/*	amtp is:	  LOB/BFILE  CLOB/NCLOB
	Input		 bytes	  characters
	Output FW	 bytes	  characters	(FW=Fixed Width charset, VW=Variable)
	Output VW	 bytes	  characters(in), bytes returned (afterwards)
	*/

	amtp = (loblen > len) ? len : loblen;

	/* buflen: length of buffer in bytes */
	/* so for CLOBs that'll be returned as UTF8 we need more bytes that chars */
	/* XXX the x4 here isn't perfect - really the code should be changed to loop */

	if (ftype == ORA_CLOB && CSFORM_IMPLIES_UTF8(csform)) {
		buflen = amtp * 4;
	/* XXX destoffset would be counting chars here as well */
		SvGROW(dest_sv, (destoffset*4) + buflen + 1);
		if (destoffset) {
			oci_error(sth, imp_sth->errhp, OCI_ERROR,
			"blob_read with non-zero destoffset not currently supported for UTF8 values");
			sv_set_undef(dest_sv);	/* signal error */
			return 0;
		}
	}
	else {
		buflen = amtp;
	}

	if (DBIc_DBISTATE(imp_sth)->debug >= 3 || dbd_verbose >= 3 )
		PerlIO_printf(
            DBIc_LOGPIO(imp_sth),
            "		blob_read field %d: ftype %d %s, offset %ld, len %lu."
            "LOB csform %d, len %lu, amtp %lu, (destoffset=%ld)\n",
			fbh->field_num+1, ftype, type_name, offset, ul_t(len),
			csform,(unsigned long) (loblen), ul_t(amtp), destoffset);

	if (loblen > 0) {
		ub1 * bufp = (ub1 *)(SvPVX(dest_sv));
		bufp += destoffset;

		OCILobRead_log_stat(imp_sth, imp_sth->svchp, imp_sth->errhp, lobl,
			&amtp, (ub4)1 + offset, bufp, buflen,
			0, 0, (ub2)0 , csform, status);

		if (DBIc_DBISTATE(imp_sth)->debug >= 3 || dbd_verbose >= 3 )
			PerlIO_printf(
                DBIc_LOGPIO(imp_sth),
                "		OCILobRead field %d %s: LOBlen %lu, LongReadLen %lu,"
                "BufLen %lu, amtp %lu\n",
				fbh->field_num+1, oci_status_name(status), ul_t(loblen),
				ul_t(imp_sth->long_readlen), ul_t(buflen), ul_t(amtp));
		if (status != OCI_SUCCESS) {
			oci_error(sth, imp_sth->errhp, status, "OCILobRead");
			sv_set_undef(dest_sv);	/* signal error */
			return 0;
		}
		if (ftype == ORA_CLOB && CSFORM_IMPLIES_UTF8(csform))
			SvUTF8_on(dest_sv);
	}
	else {
		assert(amtp == 0);
		if (DBIc_DBISTATE(imp_sth)->debug >= 3 || dbd_verbose >= 3 )
			PerlIO_printf(
                DBIc_LOGPIO(imp_sth),
				"		OCILobRead field %d %s: LOBlen %lu, LongReadLen %lu, "
                "BufLen %lu, Got %lu\n",
				fbh->field_num+1, "SKIPPED", ul_t(loblen),
				ul_t(imp_sth->long_readlen), ul_t(buflen), ul_t(amtp));
	}

	/*
	 * We assume our caller will perform
	 * the equivalent of the following:
	 *		SvCUR(dest_sv) = amtp;
	 *		*SvEND(dest_sv) = '\0';
	 *		SvPOK_on(dest_sv);
	 */

	return(amtp);
}



static int
fetch_lob(SV *sth, imp_sth_t *imp_sth, OCILobLocator* lobloc, int ftype, SV *dest_sv, char *name)
{
	dTHX;
	ub4 loblen	= 0;
	ub4 buflen	= 0;
	ub4 amtp 	= 0;
	sword status;


	if (!name)
		name = "an unknown field";

	/* this function is not called for NULL lobs */

	/* The length is expressed in terms of bytes for BLOBs and BFILEs,	*/
	/* and in terms of characters for CLOBs	and NCLOBS			*/
	OCILobGetLength_log_stat(imp_sth, imp_sth->svchp, imp_sth->errhp, lobloc, &loblen, status);
	if (status != OCI_SUCCESS) {
		oci_error(sth, imp_sth->errhp, status, "OCILobGetLength fetch_lob");
		return 0;
	}

	if (loblen > imp_sth->long_readlen) {	/* LOB will be truncated */
		int oraperl = DBIc_COMPAT(imp_sth);
		D_imp_dbh_from_sth ;
		D_imp_drh_from_dbh ;

		/* move setting amtp up to ensure error message OK */
		amtp = imp_sth->long_readlen;
		if (DBIc_has(imp_sth,DBIcf_LongTruncOk) || (oraperl && SvIV(imp_drh -> ora_trunc))) {
			/* user says truncation is ok */
			/* Oraperl recorded the truncation in ora_errno so we	*/
			/* so also but only for Oraperl mode handles.		*/
			if (oraperl) sv_setiv(DBIc_ERR(imp_sth), 1406);
		}
		else {
			char buf[300];
			sprintf(buf,"fetching %s. LOB value truncated from %ld to %ld. %s",
				name, ul_t(loblen), ul_t(amtp),
				"DBI attribute LongReadLen too small and/or LongTruncOk not set");
			oci_error_err(sth, NULL, OCI_ERROR, buf, 24345); /* appropriate ORA error number */
			sv_set_undef(dest_sv);
			return 0;
		}
	}
	else
		amtp = loblen;

	(void)SvUPGRADE(dest_sv, SVt_PV);

	/* XXXX I've hacked on this and left it probably broken
	because I didn't have time to research which args to OCI funcs need
	to be in char or byte units. That still needs to be done.
	better variable names may help.
	(The old version (1.15) duplicated too much code here because
	I applied a contributed patch that wasn't ideal, I had too little time
	to sort it out.)
	Whatever is done here, similar changes are probably needed for the
	ora_lob_*() methods when handling CLOBs.
	*/

	/* Yep you did bust it good and bad.  Seem that when the charset of
	the client and the DB are comptiable the buflen and amtp are both in chars
	no matter how many bytes make up the chars. If it is the case were the Client's
	NLS_LANG or NLS_NCHAR is not a subset of the Server's the server will try to traslate
	the data to the Client's wishes and that is wen it uses will send the ampt value will be in bytes*/

    buflen = amtp;
    if (ftype == ORA_CLOB)
		buflen = buflen*ora_ncs_buff_mtpl;


	SvGROW(dest_sv, buflen+1);

	if (loblen > 0) {
		ub1  csform = 0;
		OCILobCharSetForm_log_stat(imp_sth,
                                   imp_sth->envhp,
                                   imp_sth->errhp,
                                   lobloc,
                                   &csform,
                                   status );
		if (status != OCI_SUCCESS) {
			oci_error(sth, imp_sth->errhp, status, "OCILobCharSetForm");
			sv_set_undef(dest_sv);
			return 0;
		}

	if (ftype == ORA_BFILE) {
		OCILobFileOpen_log_stat(imp_sth, imp_sth->svchp, imp_sth->errhp, lobloc,
				(ub1)OCI_FILE_READONLY, status);
		if (status != OCI_SUCCESS) {
			oci_error(sth, imp_sth->errhp, status, "OCILobFileOpen");
			sv_set_undef(dest_sv);
			return 0;
		}
	}

	OCILobRead_log_stat(imp_sth, imp_sth->svchp, imp_sth->errhp, lobloc,
		&amtp, (ub4)1, SvPVX(dest_sv), buflen,
		0, 0, (ub2)0, csform, status);

	if (status != OCI_SUCCESS ) {

		if (status == OCI_NEED_DATA ){
			char buf[300];
			sprintf(buf,"fetching %s. LOB and the read bufer is only  %lubytes, and the ora_ncs_buff_mtpl is %d, which is too small. Try setting ora_ncs_buff_mtpl to %d",
				name,  (unsigned long)buflen, ora_ncs_buff_mtpl,ora_ncs_buff_mtpl+1);

			oci_error_err(sth, NULL, OCI_ERROR, buf, OCI_NEED_DATA); /* appropriate ORA error number */
			/*croak("DBD::Oracle has returned a %s status when doing a LobRead!! \n",oci_status_name(status));*/

		/*why a croak here well if it goes on it will result in a
		  	ORA-03127: no new operations allowed until the active operation ends
		  This will result in a crash if there are any other fetchst*/
		}
		else {
			oci_error(sth, imp_sth->errhp, status, "OCILobRead");
				sv_set_undef(dest_sv);

		}
		return 0;
	}



	if (DBIc_DBISTATE(imp_sth)->debug >= 3 || dbd_verbose >= 3 || oci_warn){
		char buf[11];
		strcpy(buf,"bytes");
		if (ftype == ORA_CLOB)
			strcpy(buf,"characters");

		PerlIO_printf(
            DBIc_LOGPIO(imp_sth),
            "		OCILobRead %s %s: csform %d (%s), LOBlen %lu(%s), "
            "LongReadLen %lu(%s), BufLen %lu(%s), Got %lu(%s)\n",
            name, oci_status_name(status), csform, oci_csform_name(csform),
            ul_t(loblen),buf ,
            ul_t(imp_sth->long_readlen),buf, ul_t(buflen),buf, ul_t(amtp),buf);

    }
	if (ftype == ORA_BFILE) {
		OCILobFileClose_log_stat(imp_sth, imp_sth->svchp, imp_sth->errhp,
		lobloc, status);
	}

	if (status != OCI_SUCCESS) {
		oci_error(sth, imp_sth->errhp, status, "OCILobFileClose");
		sv_set_undef(dest_sv);
		return 0;
	}

	/* tell perl what we've put in its dest_sv */
	SvCUR(dest_sv) = amtp;
	*SvEND(dest_sv) = '\0';
	if (ftype == ORA_CLOB && CSFORM_IMPLIES_UTF8(csform)) /* Don't set UTF8 on BLOBs */
 		SvUTF8_on(dest_sv);
		ora_free_templob(sth, imp_sth, lobloc);
	}
	else {			/* LOB length is 0 */
		assert(amtp == 0);
		/* tell perl what we've put in its dest_sv */
		SvCUR(dest_sv) = amtp;
		*SvEND(dest_sv) = '\0';
		if (DBIc_DBISTATE(imp_sth)->debug >= 3 || dbd_verbose >= 3 )
			PerlIO_printf(
                DBIc_LOGPIO(imp_sth),
                "		OCILobRead %s %s: LOBlen %lu, LongReadLen %lu, "
                "BufLen %lu, Got %lu\n",
				name, "SKIPPED", ul_t(loblen),
 				ul_t(imp_sth->long_readlen), ul_t(buflen), ul_t(amtp));
	}

	SvPOK_on(dest_sv);

	return 1;
}

static int
fetch_func_autolob(SV *sth, imp_fbh_t *fbh, SV *dest_sv)
{
	dTHX;
	char name[64];
	sprintf(name, "field %d of %d", fbh->field_num, DBIc_NUM_FIELDS(fbh->imp_sth));
	return fetch_lob(sth, fbh->imp_sth, (OCILobLocator*)fbh->desc_h, fbh->ftype, dest_sv, name);
}


static int
fetch_func_getrefpv(SV *sth, imp_fbh_t *fbh, SV *dest_sv)
{
	dTHX;
	if( sth ) { /* For GCC not to warn on unused parameter */ }
	/* See the Oracle::OCI module for how to actually use this! */
	sv_setref_pv(dest_sv, fbh->bless, (void*)fbh->desc_h);
	return 1;
}

#ifdef OCI_DTYPE_REF
static void
fbh_setup_getrefpv(imp_sth_t *imp_sth, imp_fbh_t *fbh, int desc_t, char *bless)
{
	dTHX;
	if (DBIc_DBISTATE(imp_sth)->debug >= 2 || dbd_verbose >= 3 )
        PerlIO_printf(DBIc_LOGPIO(imp_sth),
		"	col %d: otype %d, desctype %d, %s", fbh->field_num, fbh->dbtype, desc_t, bless);
	fbh->ftype  = fbh->dbtype;
	fbh->disize = fbh->dbsize;
	fbh->fetch_func = fetch_func_getrefpv;
	fbh->bless  = bless;
	fbh->desc_t = desc_t;
	OCIDescriptorAlloc_ok(imp_sth, fbh->imp_sth->envhp, &fbh->desc_h, fbh->desc_t);
}
#endif


static int
calc_cache_rows(int cache_rows, int num_fields, int est_width, int has_longs,ub4 prefetch_memory)
{
	dTHX;
	/* Use guessed average on-the-wire row width calculated above &	*/
	/* add in overhead of 5 bytes per field plus 8 bytes per row.	*/
	/* The n*5+8 was determined by studying SQL*Net v2 packets.	*/
	/* It could probably benefit from a more detailed analysis.	*/

	est_width += num_fields*5 + 8;

	if (has_longs) {			/* override/disable caching	*/
		cache_rows = 1;		/* else read_blob can't work	*/
	}
	else if (prefetch_memory) { /*set rows by memory*/

		cache_rows=prefetch_memory/est_width;
	}
	else{
		if (cache_rows == 0) {		/* automatically size the cache	*/
		/* automatically size the cache	*/

		/* Oracle packets on ethernet have max size of around 1460.	*/
		/* We'll aim to fill our row cache with around 10 per go.	*/
		/* Using 10 means any 'runt' packets will have less impact.	*/
		/* orginally set up as above but playing around with newer versions*/
		/* I found that 500 was much faster*/
		int txfr_size  = 10 * 1460;	/* desired transfer/cache size	*/

		cache_rows = txfr_size / est_width;		  /* (maybe 1 or 0)	*/

		/* To ensure good performance with large rows (near or larger	*/
		/* than our target transfer size) we set a minimum cache size.	*/
		/* I made them all at least 10* what they were before this */
		/* main reasoning this old value reprewneted a norm in the oralce 7~8 */
		/* 9 to 11 can handel much much more */
		if (cache_rows < 60)	/* is cache a 'useful' size?	*/
			cache_rows = (cache_rows > 0) ? 60 : 40;
		}
	}
	if (cache_rows > 10000000)	/* keep within Oracle's limits  */
		cache_rows = 10000000;	/* seems it was ub2 at one time now ub4 this number is arbitary on my part*/


	return cache_rows;
}

/* called by get_object to return the actual value in the property */

static void get_attr_val(SV *sth,AV *list,imp_fbh_t *fbh, text  *name , OCITypeCode  typecode, dvoid	*attr_value )
{
	dTHX;
    D_imp_sth(sth);
	text		str_buf[200];
	double		dnum;
	size_t		str_len;
	ub4			ub4_str_len;
	OCIRaw		*raw 	= (OCIRaw *) 0;
	OCIString	*vs 	= (OCIString *) 0;
	ub1			*temp	= (ub1 *)0;
	ub4			rawsize = 0;
	ub4			i 		= 0;
	sword		status;
	SV			*raw_sv;

  /* get the data based on the type code*/
	if (DBIc_DBISTATE(imp_sth)->debug >= 5 || dbd_verbose >= 5 ) {
		PerlIO_printf(DBIc_LOGPIO(imp_sth),
                      " getting value of object attribute named  %s with typecode=%s\n",
                      name,oci_typecode_name(typecode));
	}

	switch (typecode)
	{

	case OCI_TYPECODE_INTERVAL_YM  :
	case OCI_TYPECODE_INTERVAL_DS  :

      OCIIntervalToText_log_stat(fbh->imp_sth,
                                 fbh->imp_sth->envhp,
                                 fbh->imp_sth->errhp,
                                 attr_value,
                                 str_buf,
                                 (size_t) 200,
                                 &str_len,
                                 status);
		str_buf[str_len+1] = '\0';
		av_push(list, newSVpv( (char *) str_buf,0));
		break;

	case OCI_TYPECODE_TIMESTAMP_TZ :
	case OCI_TYPECODE_TIMESTAMP_LTZ :
	case OCI_TYPECODE_TIMESTAMP :


		ub4_str_len = 200;
		OCIDateTimeToText_log_stat(fbh->imp_sth,
                                   fbh->imp_sth->envhp,
                                   fbh->imp_sth->errhp,
                                   attr_value,
                                   &ub4_str_len,
                                   str_buf,
                                   status);

		if (typecode == OCI_TYPECODE_TIMESTAMP_TZ || typecode == OCI_TYPECODE_TIMESTAMP_LTZ){
			char s_tz_hour[3]="000";
			char s_tz_min[3]="000";
			sb1 tz_hour;
			sb1 tz_minute;
			status = OCIDateTimeGetTimeZoneOffset (fbh->imp_sth->envhp,
												 fbh->imp_sth->errhp,
												 *(OCIDateTime**)attr_value,
												 &tz_hour,
									&tz_minute );

			if (  (tz_hour<0) && (tz_hour>-10) ){
				sprintf(s_tz_hour," %03d",tz_hour);
			} else {
				sprintf(s_tz_hour," %02d",tz_hour);
			}

			sprintf(s_tz_min,":%02d", tz_minute);
			strcat((signed char*)str_buf, s_tz_hour);
			strcat((signed char*)str_buf, s_tz_min);
			str_buf[ub4_str_len+7] = '\0';

		} else {
		  str_buf[ub4_str_len+1] = '\0';
		}

		av_push(list, newSVpv( (char *) str_buf,0));
		break;

	case OCI_TYPECODE_DATE :						 /* fixed length string*/
		ub4_str_len = 200;
		OCIDateToText_log_stat(fbh->imp_sth,
                               fbh->imp_sth->errhp,
                               (CONST OCIDate *) attr_value,
                               &ub4_str_len,
                               str_buf,
                               status);
		str_buf[ub4_str_len+1] = '\0';
		av_push(list, newSVpv( (char *) str_buf,0));
		break;


	case OCI_TYPECODE_CLOB:
	case OCI_TYPECODE_BLOB:
	case OCI_TYPECODE_BFILE:
		raw_sv = newSV(0);
		fetch_lob(sth, fbh->imp_sth,*(OCILobLocator**)attr_value, typecode, raw_sv, (signed char*)name);


		av_push(list, raw_sv);
		break;

	case OCI_TYPECODE_RAW :/* RAW*/

		raw_sv = newSV(0);
		raw = *(OCIRaw **) attr_value;
		temp = OCIRawPtr(fbh->imp_sth->envhp, raw);
		rawsize = OCIRawSize (fbh->imp_sth->envhp, raw);
		for (i=0; i < rawsize; i++) {
			sv_catpvf(raw_sv,"0x%x ", temp[i]);
		}
		sv_catpv(raw_sv,"\n");

		av_push(list, raw_sv);

		 break;
	case OCI_TYPECODE_CHAR :						 /* fixed length string */
	case OCI_TYPECODE_VARCHAR :								 /* varchar  */
	case OCI_TYPECODE_VARCHAR2 :								/* varchar2 */
		vs = *(OCIString **) attr_value;
		av_push(list, newSVpv((char *) OCIStringPtr(fbh->imp_sth->envhp, vs),0));
		break;
	case OCI_TYPECODE_SIGNED8 :							  /* BYTE - sb1  */
		av_push(list, newSVuv(*(sb1 *)attr_value));
		break;
	case OCI_TYPECODE_UNSIGNED8 :					/* UNSIGNED BYTE - ub1  */
		av_push(list, newSViv(*(ub1 *)attr_value));
		break;
	case OCI_TYPECODE_OCTET :										/* OCT*/
		av_push(list, newSViv(*(ub1 *)attr_value));
		break;
	case OCI_TYPECODE_UNSIGNED16 :						/* UNSIGNED SHORT  */
	case OCI_TYPECODE_UNSIGNED32 :						/* UNSIGNED LONG  */
	case OCI_TYPECODE_REAL :									 /* REAL	*/
	case OCI_TYPECODE_DOUBLE :									/* DOUBLE  */
	case OCI_TYPECODE_INTEGER :									 /* INT  */
	case OCI_TYPECODE_SIGNED16 :								  /* SHORT  */
	case OCI_TYPECODE_SIGNED32 :									/* LONG  */
	case OCI_TYPECODE_DECIMAL :								 /* DECIMAL  */
	case OCI_TYPECODE_FLOAT :									/* FLOAT	*/
	case OCI_TYPECODE_NUMBER :								  /* NUMBER	*/
	case OCI_TYPECODE_SMALLINT :								/* SMALLINT */
		(void) OCINumberToReal(fbh->imp_sth->errhp, (CONST OCINumber *) attr_value,
								(uword) sizeof(dnum), (dvoid *) &dnum);

		av_push(list, newSVnv(dnum));
		break;
	default:
		break;
	}
}


SV* new_ora_object (AV* list, OCITypeCode typecode) {
	dTHX;
	SV* objref = newRV_noinc((SV*) list);

	if (ora_objects && typecode == OCI_TYPECODE_OBJECT) {
		HV* self = newHV();
		(void)hv_store(self, "type_name", 9, av_shift(list), 0);
		(void)hv_store(self, "attributes", 10, objref, 0);
		objref = newRV_noinc((SV*) self);
		objref = sv_bless(objref, gv_stashpv("DBD::Oracle::Object", 0));

	}
	return objref;
}

/*gets the properties of an object from a fetch by using the attributes saved in the describe */

int
get_object (SV *sth, AV *list, imp_fbh_t *fbh,fbh_obj_t *base_obj,OCIComplexObject *value, OCIType *instance_tdo, dvoid *obj_ind){

	dTHX;
    D_imp_sth(sth);
	sword 		status;
	dvoid		*element ;
	dvoid		*attr_value;
	boolean		eoc;
	ub2	 		pos;
	dvoid 		*attr_null_struct;
	OCIInd		attr_null_status;
	OCIInd		*element_null;
	OCIType 	*attr_tdo;
	OCIIter		*itr;
	fbh_obj_t	*fld;
	fbh_obj_t	*obj = base_obj;

	 OCIType	*tdo = instance_tdo ? instance_tdo : obj->tdo;

     if (DBIc_DBISTATE(imp_sth)->debug >= 5 || dbd_verbose >= 5 ) {
         PerlIO_printf(DBIc_LOGPIO(imp_sth),
                       " getting attributes of object named  %s with typecode=%s\n",
                       obj->type_name,oci_typecode_name(obj->typecode));
	}

	switch (obj->typecode) {

		case OCI_TYPECODE_OBJECT:	/* embedded ADT */
		case OCI_TYPECODE_OPAQUE: /*doesn't do anything though*/
			if (ora_objects){


				sword	status;
				if (!instance_tdo && !obj->is_final_type) {
					OCIRef	*type_ref=0;
					status = OCIObjectNew(fbh->imp_sth->envhp, fbh->imp_sth->errhp, fbh->imp_sth->svchp,
											OCI_TYPECODE_REF, (OCIType *)0,
											(dvoid *)0, OCI_DURATION_DEFAULT, TRUE,
											(dvoid **) &type_ref);
					if (status != OCI_SUCCESS) {
						oci_error(sth, fbh->imp_sth->errhp, status, "OCIObjectNew");
						return 0;
					}

					status=OCIObjectGetTypeRef(fbh->imp_sth->envhp,fbh->imp_sth->errhp, (dvoid*)value, type_ref);
					if (status != OCI_SUCCESS) {
						oci_error(sth, fbh->imp_sth->errhp, status, "OCIObjectGetTypeRef");
						return 0;
					}

					OCITypeByRef_log_stat(fbh->imp_sth,
                                          fbh->imp_sth->envhp,
                                          fbh->imp_sth->errhp,
                                          type_ref,
                                          &tdo,status);

					if (status != OCI_SUCCESS) {
						oci_error(sth, fbh->imp_sth->errhp, status, "OCITypeByRef");
						return 0;
					}

					status = OCIObjectFree(fbh->imp_sth->envhp, fbh->imp_sth->errhp, type_ref, (ub2)0);

					if (status != OCI_SUCCESS) {
						oci_error(sth, fbh->imp_sth->errhp, status, "OCIObjectFree");
						return 0;
					}

				}


				if (tdo != obj->tdo) {
					/* this is subtype -> search for subtype obj */
					while (obj->next_subtype && tdo != obj->tdo) {
						obj = obj->next_subtype;
					}
					if (tdo != obj->tdo) {
						/* new subtyped -> get obj description */
						if (DBIc_DBISTATE(imp_sth)->debug >= 5 || dbd_verbose >= 5 ) {
							PerlIO_printf(DBIc_LOGPIO(imp_sth), " describe subtype (tdo=%p) of object type %s (tdo=%p)\n",(void*)tdo,base_obj->type_name,(void*)base_obj->tdo);
						}

						Newz(1, obj->next_subtype, 1, fbh_obj_t);
						obj->next_subtype->tdo = tdo;
						if ( describe_obj_by_tdo(sth, fbh->imp_sth, obj->next_subtype, 0 /*unknown level there*/) ) {
							obj = obj->next_subtype;
							if (DBIc_DBISTATE(imp_sth)->debug >= 5 || dbd_verbose >= 5 ){
								dump_struct(fbh->imp_sth,obj,0);
							}
						}
						else {
							obj->next_subtype = 0;
						}
					}

					if (DBIc_DBISTATE(imp_sth)->debug >= 5 || dbd_verbose >= 5 ) {
						PerlIO_printf(DBIc_LOGPIO(imp_sth), " getting attributes of object subtype  %s\n",obj->type_name);
					}
				}

				av_push(list, newSVpv((char*)obj->type_name, obj->type_namel));
			}



			for (pos = 0; pos < obj->field_count; pos++){

				fld = &obj->fields[pos]; /*get the field */

				if (ora_objects) {
					/* add field name */
					av_push(list, newSVpv((char*)fld->type_name, fld->type_namel));
				}

/*
the little bastard above took me ages to find out
seems Oracle does not like people to know that it can do this
the concept is simple really
 1. pin the object
 2. bind with dty = SQLT_NTY
 3. OCIDefineObject using the TDO
 4. one gets the null indicator of the objcet with OCIObjectGetInd
	The the obj_ind is for the entier object not the properties so you call it once it
	gets all of the indicators for the objects so you pass it into OCIObjectGetAttr and that
	function will set attr_null_status as in the get below.
 5. interate over the attributes of the object

The thing to remember is that OCI and C have no way of representing a DB NULLs so we use the OCIInd find out
if the object or any of its properties are NULL, This is one little line in a 20 chapter book and even then
id only shows you examples with the C struct built in and only a single record. Nowhere does it say you can do it this way.
*/

				OCIObjectGetAttr_log_stat(
                    fbh->imp_sth,
                    fbh->imp_sth->envhp,
                    fbh->imp_sth->errhp,
                    value,                      /* instance */
                    obj_ind,                    /* null_struct */
                    tdo,                        /* tdo */
                    (CONST oratext**)&fld->type_name, /* names */
                    &fld->type_namel,                 /* lengths */
                    1,                                /* name_count */
                    (ub4 *)0,                         /* indexes */
                    0,                                /* index_count */
                    &attr_null_status,                /* attr_null_status */
                    &attr_null_struct,                /* attr_null_struct */
                    &attr_value,                      /* attr_value */
                    &attr_tdo,                        /* attr_tdo */
                    status);

				if (status != OCI_SUCCESS) {
					oci_error(sth, fbh->imp_sth->errhp, status, "OCIObjectGetAttr");
					return 0;
				}

				if (attr_null_status==OCI_IND_NULL){
					 av_push(list,  &PL_sv_undef);
				} else {
					if (fld->typecode == OCI_TYPECODE_OBJECT || fld->typecode == OCI_TYPECODE_VARRAY || fld->typecode == OCI_TYPECODE_TABLE || fld->typecode == OCI_TYPECODE_NAMEDCOLLECTION){

						fld->fields[0].value = newAV();
						if (fld->typecode != OCI_TYPECODE_OBJECT)
							attr_value = *(dvoid **)attr_value;

						if (!get_object (sth,fld->fields[0].value, fbh, &fld->fields[0],attr_value, attr_tdo, attr_null_struct))
							return 0;
						av_push(list, new_ora_object(fld->fields[0].value, fld->typecode));

					} else{  /* else, display the scaler type attribute */

						get_attr_val(sth,list, fbh, fld->type_name, fld->typecode, attr_value);

					}
				}
			 }
			break;

		case OCI_TYPECODE_REF :								/* embedded ADT */
			croak("panic: OCI_TYPECODE_REF objets () are not supported ");
			break;

		case OCI_TYPECODE_NAMEDCOLLECTION : /*this works for both as I am using CONST OCIColl */

			switch (obj->col_typecode) { /*there may be more thatn two I havn't found them yet mmight be XML??*/
				case OCI_TYPECODE_TABLE :					/* nested table */
				case OCI_TYPECODE_VARRAY :					/* variable array */
					fld = &obj->fields[0]; /*get the field */
					OCIIterCreate_log_stat(fbh->imp_sth,
                                           fbh->imp_sth->envhp,
                                           fbh->imp_sth->errhp,
                                           (OCIColl*) value,
                                           &itr,
                                           status);
					if (status != OCI_SUCCESS) {
						/*not really an error just no data
						oci_error(sth, fbh->imp_sth->errhp, status, "OCIIterCreate");*/
						status = OCI_SUCCESS;
						av_push(list,  &PL_sv_undef);
						return 0;
					}
					for(eoc = FALSE;!OCIIterNext(fbh->imp_sth->envhp, fbh->imp_sth->errhp, itr,
						(dvoid **) &element,
						(dvoid **) &element_null, &eoc) && !eoc;)
					{

						if (*element_null==OCI_IND_NULL){
							av_push(list,  &PL_sv_undef);
						} else {
							if (obj->element_typecode == OCI_TYPECODE_OBJECT || obj->element_typecode == OCI_TYPECODE_VARRAY || obj->element_typecode== OCI_TYPECODE_TABLE || obj->element_typecode== OCI_TYPECODE_NAMEDCOLLECTION){
								fld->value = newAV();
								if(!get_object (sth,fld->value, fbh, fld,element,0,element_null))
									return 0;
								av_push(list, new_ora_object(fld->value, obj->element_typecode));
							} else{  /* else, display the scaler type attribute */
								get_attr_val(sth,list, fbh, obj->type_name, obj->element_typecode, element);
							}
						}
					}
					/*nasty surprise here. one has to get rid of the iterator or you will leak memory
					  not documented in oci or in demos */
					OCIIterDelete_log_stat(fbh->imp_sth,
                                           fbh->imp_sth->envhp,
                                           fbh->imp_sth->errhp,
                                           &itr,
                                           status );
					if (status != OCI_SUCCESS) {
						oci_error(sth, fbh->imp_sth->errhp, status, "OCIIterDelete");
						return 0;
					}
					break;
				default:
					break;
				}
			break;
		default:
			if (value) {
				get_attr_val(sth,list, fbh, obj->type_name, obj->typecode, value);
			}
			else
				return 1;
			break;
		}
		return 1;
 }



/*cutsom fetch for embedded objects */

static int
fetch_func_oci_object(SV *sth, imp_fbh_t *fbh,SV *dest_sv)
{
	dTHX;
    D_imp_sth(sth);

	if (DBIc_DBISTATE(imp_sth)->debug >= 4 || dbd_verbose >= 4 ) {
		PerlIO_printf(DBIc_LOGPIO(imp_sth),
                      " getting an embedded object named  %s with typecode=%s\n",
                      fbh->obj->type_name,oci_typecode_name(fbh->obj->typecode));
	}

	if (fbh->obj->obj_ind && fbh->obj->obj_ind[0] == OCI_IND_NULL) {
		sv_set_undef(dest_sv);
		return 1;
	}

	fbh->obj->value=newAV();

	/*will return referance to an array of scalars*/
	if (!get_object(sth,fbh->obj->value,fbh,fbh->obj,fbh->obj->obj_value,0,fbh->obj->obj_ind)){
 		return 0;
	} else {
		sv_setsv(dest_sv, sv_2mortal(new_ora_object(fbh->obj->value, fbh->obj->typecode)));
		return 1;
	}

}



static int
fetch_clbk_lob(SV *sth, imp_fbh_t *fbh,SV *dest_sv){

	dTHX;
	D_imp_sth(sth);
	fb_ary_t *fb_ary = fbh->fb_ary;

	ub4 actual_bufl=imp_sth->piece_size*(fb_ary->piece_count)+fb_ary->bufl;

	if (fb_ary->piece_count==0){
		if (DBIc_DBISTATE(imp_sth)->debug >= 6 || dbd_verbose >= 6 )
			PerlIO_printf(
                DBIc_LOGPIO(imp_sth),
                "  Fetch persistent lob of %d (char/bytes) with callback in 1 "
                "piece of %d (Char/Bytes)\n",
                actual_bufl,fb_ary->bufl);

		memcpy(fb_ary->cb_abuf,fb_ary->abuf,fb_ary->bufl );

	} else {
        if (DBIc_DBISTATE(imp_sth)->debug >= 6 || dbd_verbose >= 6 )
			PerlIO_printf(
                DBIc_LOGPIO(imp_sth),
                "  Fetch persistent lob of %d (Char/Bytes) with callback in %d "
                "piece(s) of %d (Char/Bytes) and one piece of %d (Char/Bytes)\n",
                actual_bufl,fb_ary->piece_count,fbh->piece_size,fb_ary->bufl);

		memcpy(fb_ary->cb_abuf+imp_sth->piece_size*(fb_ary->piece_count),fb_ary->abuf,fb_ary->bufl );
	}

	if (fbh->ftype == SQLT_BIN){
		*(fb_ary->cb_abuf+(actual_bufl))='\0'; /* add a null teminator*/
		sv_setpvn(dest_sv, (char*)fb_ary->cb_abuf,(STRLEN)actual_bufl);
	} else {
		sv_setpvn(dest_sv, (char*)fb_ary->cb_abuf,(STRLEN)actual_bufl);
		if (CSFORM_IMPLIES_UTF8(fbh->csform) ){
			SvUTF8_on(dest_sv);
		}
	}
	return 1;
}
/* This is another way to get lobs as a alternate to callback */

static int
fetch_get_piece(SV *sth, imp_fbh_t *fbh,SV *dest_sv)
{
	dTHX;
	D_imp_sth(sth);
	fb_ary_t *fb_ary = fbh->fb_ary;
	ub4 buflen		 = fb_ary->bufl;
	ub4 actual_bufl	 = 0;
	ub1	piece  = OCI_FIRST_PIECE;
	void *hdlptr = (dvoid *) 0;
	ub4 hdltype  = OCI_HTYPE_DEFINE, iter = 0, idx = 0;
	ub1	in_out = 0;
	sb2	indptr = 0;
	ub2	rcode  = 0;
	sword status = OCI_NEED_DATA;

	if (DBIc_DBISTATE(imp_sth)->debug >= 4 || dbd_verbose >= 4 ) {
		PerlIO_printf(DBIc_LOGPIO(imp_sth), "in fetch_get_piece  \n");
	}

	while (status == OCI_NEED_DATA){

        OCIStmtGetPieceInfo_log_stat(fbh->imp_sth,
                                     fbh->imp_sth->stmhp,
                                     fbh->imp_sth->errhp,
                                     &hdlptr,
                                     &hdltype,
                                     &in_out,
                                     &iter,
                                     &idx,
                                     &piece,
                                     status);

		/* This is how this works
		First we get the piece Info above
		the bugger thing is that this will get the piece info in sequential order so on each call to the above
		you have to check to ensure you have the right define handle from the OCIDefineByPos
		I do it in the next if statement.  So this will loop untill the handle changes at that point it exits the loop
		during the loop I add the abuf to the  cb_abuf  using the buflen that is set above.
		I get the actual buffer length by adding up all the pieces (buflen) as I go along
		Another really anoying thing is once can only find out if there is data left over at the very end of the fetching of the colums
		so I make it warn if the LongTruncOk. I could also do this before but that would not result in any of the good data getting
		in
		*/
		if ( hdlptr==fbh->defnp){

			OCIStmtSetPieceInfo_log_stat(fbh->imp_sth,
                                         fbh->defnp,
										 fbh->imp_sth->errhp,
										 fb_ary->abuf,
										 &buflen,
										 piece,
										 (dvoid *)&indptr,
										 &rcode,status);


            OCIStmtFetch_log_stat(fbh->imp_sth, fbh->imp_sth->stmhp,fbh->imp_sth->errhp,1,(ub2)OCI_FETCH_NEXT,OCI_DEFAULT,status);


			if (status==OCI_SUCCESS_WITH_INFO && !DBIc_has(fbh->imp_sth,DBIcf_LongTruncOk)){
			 	dTHR; 			/* for DBIc_ACTIVE_off	*/
				DBIc_ACTIVE_off(fbh->imp_sth);	/* eg finish		*/
				oci_error(sth, fbh->imp_sth->errhp, status, "OCIStmtFetch, LongReadLen too small and/or LongTruncOk not set");
			}
 			memcpy(fb_ary->cb_abuf+fb_ary->piece_count*imp_sth->piece_size,fb_ary->abuf,buflen );
			fb_ary->piece_count++;/*used to tell me how many pieces I have, for debuffing in this case */
			actual_bufl=actual_bufl+buflen;

		}else {
			status=OCI_LAST_PIECE;
		}
	}


	if (DBIc_DBISTATE(imp_sth)->debug >= 6 || dbd_verbose >= 6 ){
		if (fb_ary->piece_count==1){
			PerlIO_printf(
                DBIc_LOGPIO(imp_sth),
                "	 Fetch persistent lob of %d (Char/Bytes) with Polling "
                "in 1 piece\n",
                actual_bufl);

		} else {
			PerlIO_printf(
                DBIc_LOGPIO(imp_sth),
                "	 Fetch persistent lob of %d (Char/Bytes) with Polling "
                "in %d piece(s) of %d (Char/Bytes) and one piece of %d (Char/Bytes)\n",
                actual_bufl,fb_ary->piece_count,fbh->piece_size,buflen);
		}
	}

	if (actual_bufl > 0){
		sv_setpvn(dest_sv, (char*)fb_ary->cb_abuf,(STRLEN)actual_bufl);
		if (fbh->ftype != SQLT_BIN){

			if (CSFORM_IMPLIES_UTF8(fbh->csform) ){ /* do the UTF 8 magic*/
				SvUTF8_on(dest_sv);
			}
		}
	} else {
		sv_set_undef(dest_sv);
	}

	return 1;
}


int
empty_oci_object(fbh_obj_t *obj){
	dTHX;
	int			pos =0;
	fbh_obj_t	*fld=NULL;



	switch (obj->element_typecode) {

		case OCI_TYPECODE_OBJECT :		/* embedded ADT */
		case OCI_TYPECODE_OPAQUE : /*usually an XML object*/
			if (obj->next_subtype) {
				empty_oci_object(obj->next_subtype);
			}

			for (pos = 0; pos < obj->field_count; pos++){
				fld = &obj->fields[pos]; /*get the field */
				if (fld->typecode == OCI_TYPECODE_OBJECT || fld->typecode == OCI_TYPECODE_VARRAY || fld->typecode == OCI_TYPECODE_TABLE || fld->typecode == OCI_TYPECODE_NAMEDCOLLECTION){
					empty_oci_object(fld);
					if (fld->value && SvTYPE(fld->value) == SVt_PVAV){
						av_clear(fld->value);
			 			av_undef(fld->value);
					}
				}
				else {
					return 1;
				}
			}
			break;

		case OCI_TYPECODE_NAMEDCOLLECTION :
			fld = &obj->fields[0]; /*get the field */
			if (obj->element_typecode == OCI_TYPECODE_OBJECT){
				empty_oci_object(fld);
			}
			if (fld->value && SvTYPE(fld->value)){
				if (SvTYPE(fld->value) == SVt_PVAV){
					av_clear(fld->value);
					av_undef(fld->value);
				}
			}
			break;

		default:
		 	break;
	}
	if ( fld && fld->value && (SvTYPE(fld->value) == SVt_PVAV) ){
			av_clear(obj->value);
		av_undef(obj->value);
	}

	return 1;

}

static void
fetch_cleanup_pres_lobs(SV *sth,imp_fbh_t *fbh){
	dTHX;
    D_imp_sth(sth);

	fb_ary_t *fb_ary = fbh->fb_ary;

	if( sth ) { /* For GCC not to warn on unused parameter*/  }
	fb_ary->piece_count=0;/*reset the peice counter*/
	memset( fb_ary->abuf, '\0', fb_ary->bufl); /*clean out the piece fetch buffer*/
	fb_ary->bufl=fbh->piece_size; /*reset this back to the piece length */
	fb_ary->cb_bufl=fbh->disize; /*reset this back to the max size for the fetch*/
	memset( fb_ary->cb_abuf, '\0', fbh->disize ); /*clean out the call back buffer*/

 	if (DBIc_DBISTATE(imp_sth)->debug >= 5 || dbd_verbose >= 5 )
		PerlIO_printf(DBIc_LOGPIO(imp_sth),"  fetch_cleanup_pres_lobs \n");

	return;
}

static void
fetch_cleanup_oci_object(SV *sth, imp_fbh_t *fbh){
	dTHX;
    D_imp_sth(sth);

	if( sth ) { /* For GCC not to warn on unused parameter*/  }

	if (fbh->obj){
		if(fbh->obj->obj_value || fbh->obj->obj_ind){
			empty_oci_object(fbh->obj);
		}
	}

	if (DBIc_DBISTATE(imp_sth)->debug >= 3 || dbd_verbose >= 3 )
        PerlIO_printf(DBIc_LOGPIO(imp_sth),"  fetch_cleanup_oci_object \n");
	return;
}

void rs_array_init(imp_sth_t *imp_sth)
{
	dTHX;

	imp_sth->rs_array_num_rows	=0;
	imp_sth->rs_array_idx		=0;
	imp_sth->rs_fetch_count		=0;
	imp_sth->rs_array_status	=OCI_SUCCESS;

	if (DBIc_DBISTATE(imp_sth)->debug >= 3 || dbd_verbose >= 3 )
		PerlIO_printf(
            DBIc_LOGPIO(imp_sth),
            "	rs_array_init:imp_sth->rs_array_size=%d, rs_array_idx=%d, "
            "prefetch_rows=%d, rs_array_status=%s\n",
            imp_sth->rs_array_size, imp_sth->rs_array_idx, imp_sth->prefetch_rows,
            oci_status_name(imp_sth->rs_array_status));
}

static int			/* --- Setup the row cache for this sth --- */
sth_set_row_cache(SV *h, imp_sth_t *imp_sth, int max_cache_rows, int num_fields, int has_longs)
{
	dTHX;
	D_imp_dbh_from_sth;
	D_imp_drh_from_dbh;
	int num_errors		= 0;
	ub4 prefetch_mem	= 0; /*Oracle prefetch memory buffer*/
	sb4 prefetch_rows	= 0; /*Oracle prefetch Row Buffer*/
	sb4 cache_rows		= 0;/* set high so memory is the limit */
	sword status;



	if (imp_sth->RowCacheSize ) { /*Statment value will crump the handle value */
		cache_rows=imp_sth->RowCacheSize;
	}
	else if (imp_dbh->RowCacheSize){
		cache_rows=imp_dbh->RowCacheSize;

	}

	/* seems that RowCacheSize was incorrectly used in the past
	   in the DBI Spect  RowCacheSize is to be used for a local row cache
	   and can be set on both the handle and the statement and the statement will take
	   precideace

	   From DBI POD
	      A hint to the driver indicating the size of the local
	      row cache that the application would like the driver to
	      use for future SELECT statements.

	   so RowCacheSize is for a local cache to cut down on round trips

	   The OCI doc state that both OCI_ATTR_PREFETCH_ROWS OCI_ATTR_PREFETCH_MEMORY
	   sets up a cleint side cache but in earlier version than 1.24 we only selected
	   one record at a time from the fetch this means a round trip (at least to the local cache)
	   at each fetch.

	   With the new array fetch we truly have a local cache so I will use it
	   RowCacheSize to set the value of that cache or the array fetch*/



	/* number of rows to cache	 if using oraperl  will leave this in for now*/


	if (SvOK(imp_drh->ora_cache_o)){
		imp_sth->cache_rows = SvIV(imp_drh->ora_cache_o);
	}
	else if (SvOK(imp_drh->ora_cache)){
		imp_sth->cache_rows = SvIV(imp_drh->ora_cache);
	}


	prefetch_rows	=imp_sth->prefetch_rows;
	prefetch_mem	=imp_sth->prefetch_memory;


	if (!cache_rows) { /*start with this value if not set then set default cache */

		cache_rows=calc_cache_rows(imp_sth->cache_rows,(int)num_fields, imp_sth->est_width, has_longs,0);

		if(!prefetch_rows && !prefetch_mem){ /*if there are not prefetch rows make sure I set it here to the default*/
			  prefetch_rows=cache_rows;
		}
	}
	else if (imp_dbh->RowCacheSize < 0) {/* for compaibility with DBI doc negitive value here means use the value as memory*/
		prefetch_mem	=-imp_dbh->RowCacheSize; /* cache_mem always +ve here */
		prefetch_rows	=0;
		cache_rows=calc_cache_rows(imp_sth->cache_rows,(int)num_fields, imp_sth->est_width, has_longs,prefetch_mem);
		/*The above fucntion will set the cache_rows using memory as the limit*/
	}
	else {

	   if (!prefetch_mem){
			prefetch_rows = cache_rows; /*use the RowCacheSize*/
	   }
	}

	if (cache_rows <= prefetch_rows){
		cache_rows=prefetch_rows;
		/* is prefetch_rows are greater than the RowCahceSize then use prefetch_rows*/
	}

	OCIAttrSet_log_stat(imp_sth, imp_sth->stmhp, OCI_HTYPE_STMT,
						&prefetch_mem,  sizeof(prefetch_mem), OCI_ATTR_PREFETCH_MEMORY,
						imp_sth->errhp, status);

	if (status != OCI_SUCCESS) {
		oci_error(h, imp_sth->errhp, status,
				"OCIAttrSet OCI_ATTR_PREFETCH_MEMORY");
		++num_errors;
	}

	OCIAttrSet_log_stat(imp_sth, imp_sth->stmhp, OCI_HTYPE_STMT,
					&prefetch_rows, sizeof(prefetch_rows), OCI_ATTR_PREFETCH_ROWS,
				imp_sth->errhp, status);

	if (status != OCI_SUCCESS) {
		oci_error(h, imp_sth->errhp, status, "OCIAttrSet OCI_ATTR_PREFETCH_ROWS");
		++num_errors;
	}


	imp_sth->rs_array_size=cache_rows;

    if (max_cache_rows){/* limited to 1 by a cursor or something else*/
		imp_sth->rs_array_size=1;
	}


	if (imp_sth->row_cache_off){/*set the size of the Rows in Cache value*/
		imp_dbh->RowsInCache =1;
		imp_sth->RowsInCache =1;
	}
	 else {
		imp_dbh->RowsInCache=imp_sth->rs_array_size;
		imp_sth->RowsInCache=imp_sth->rs_array_size;
	}



	if (DBIc_DBISTATE(imp_sth)->debug >= 3 || dbd_verbose >= 3 || oci_warn) /*will also display if oci_warn is on*/
		PerlIO_printf(
            DBIc_LOGPIO(imp_sth),
			"	cache settings DB Handle RowCacheSize=%d,Statement Handle "
            "RowCacheSize=%d, OCI_ATTR_PREFETCH_ROWS=%lu, "
            "OCI_ATTR_PREFETCH_MEMORY=%lu, Rows per Fetch=%d, Multiple Row Fetch=%s\n",
			imp_dbh->RowCacheSize, imp_sth->RowCacheSize,
            (unsigned long) (prefetch_rows), (unsigned long) (prefetch_mem),
            cache_rows,(imp_sth->row_cache_off)?"Off":"On");

	return num_errors;
}



/*recurses down the field's TDOs and saves the little bits it need for later use on a fetch fbh->obj */
int
describe_obj(SV *sth,imp_sth_t *imp_sth,OCIParam *parm,fbh_obj_t *obj,int level )
{
	dTHX;
	sword status;
	OCIRef *type_ref;

	if (DBIc_DBISTATE(imp_sth)->debug >= 5 || dbd_verbose >= 5 ) {
		PerlIO_printf(
            DBIc_LOGPIO(imp_sth),
            "At level=%d in description an embedded object \n",level);
	}
	/*Describe the field (OCIParm) we know it is a object or a collection */

	/* Get the Actual TDO */
	OCIAttrGet_parmdp(imp_sth,parm, &type_ref, 0, OCI_ATTR_REF_TDO, status);

	if (status != OCI_SUCCESS) {
		oci_error(sth, imp_sth->errhp, status, "OCIAttrGet");
		return 0;
	}

	OCITypeByRef_log_stat(imp_sth,
                          imp_sth->envhp,
                          imp_sth->errhp,
                          type_ref,
                          &obj->tdo,
                          status);

	if (status != OCI_SUCCESS) {
		oci_error(sth, imp_sth->errhp, status, "OCITypeByRef");
		return 0;
	}

	return describe_obj_by_tdo(sth, imp_sth, obj, level);
	}

int
describe_obj_by_tdo(SV *sth,imp_sth_t *imp_sth,fbh_obj_t *obj,ub2 level ) {
	dTHX;
	sword status;
	text *type_name, *schema_name;
	ub4  type_namel, schema_namel;


	OCIDescribeAny_log_stat(imp_sth, imp_sth->svchp,imp_sth->errhp,obj->tdo,(ub4)0,OCI_OTYPE_PTR,(ub1)1,OCI_PTYPE_TYPE,imp_sth->dschp,status);
	/*we have the Actual TDO  so lets see what it is made up of by a describe*/

	if (status != OCI_SUCCESS) {
		oci_error(sth,imp_sth->errhp, status, "OCIDescribeAny");
		return 0;
	}

	OCIAttrGet_parmap(imp_sth, imp_sth->dschp,OCI_HTYPE_DESCRIBE,  &obj->parmdp, 0, status);

	if (status != OCI_SUCCESS) {
		oci_error(sth,imp_sth->errhp, status, "OCIAttrGet");
		return 0;
	}

	/*and we store it in the object's paramdp for now*/

	OCIAttrGet_parmdp(imp_sth, obj->parmdp, &schema_name, &schema_namel, OCI_ATTR_SCHEMA_NAME, status);

	if (status != OCI_SUCCESS) {
		oci_error(sth,imp_sth->errhp, status, "OCIAttrGet");
		return 0;
	}

	OCIAttrGet_parmdp(imp_sth, obj->parmdp, &type_name, &type_namel, OCI_ATTR_NAME, status);

	if (status != OCI_SUCCESS) {
		oci_error(sth,imp_sth->errhp, status, "OCIAttrGet");
		return 0;
	}

	/* make full type_name: schema_name + "." + type_name */
	obj->full_type_name = newSVpv((char*)schema_name, schema_namel);
	sv_catpvn(obj->full_type_name, ".", 1);
	sv_catpvn(obj->full_type_name, (char*)type_name, type_namel);
	obj->type_name = (text*)SvPV(obj->full_type_name,PL_na);

	/*we need to know its type code*/

	OCIAttrGet_parmdp(imp_sth, obj->parmdp, (dvoid *)&obj->typecode, 0, OCI_ATTR_TYPECODE, status);

	if (status != OCI_SUCCESS) {
		oci_error(sth,imp_sth->errhp, status, "OCIAttrGet");
		return 0;
	}

	if (DBIc_DBISTATE(imp_sth)->debug >= 6 || dbd_verbose >= 6 ) {
		PerlIO_printf(
            DBIc_LOGPIO(imp_sth),
            "Getting the properties of object named =%s at level %d typecode=%d\n",
            obj->type_name,level,obj->typecode);
	}

	if (obj->typecode == OCI_TYPECODE_OBJECT || obj->typecode == OCI_TYPECODE_OPAQUE){
		OCIParam *list_attr= (OCIParam *) 0;
		ub2	  pos;
		if (DBIc_DBISTATE(imp_sth)->debug >= 6 || dbd_verbose >= 6 ) {
			PerlIO_printf(
                DBIc_LOGPIO(imp_sth),
                "Object named =%s at level %d is an Object\n",
                obj->type_name,level);
		}

		OCIAttrGet_parmdp(imp_sth, obj->parmdp, (dvoid *)&obj->obj_ref, 0, OCI_ATTR_REF_TDO, status);

		if (status != OCI_SUCCESS) {
			oci_error(sth,imp_sth->errhp, status, "OCIAttrGet");
			return 0;
		}
		/*we will need a reff to the TDO for the pin operation*/

		OCIObjectPin_log_stat(imp_sth, imp_sth->envhp,imp_sth->errhp, obj->obj_ref,(dvoid  **)&obj->obj_type,status);

		if (status != OCI_SUCCESS) {
			oci_error(sth,imp_sth->errhp, status, "OCIObjectPin");
			return 0;
		}

		OCIAttrGet_parmdp(imp_sth,  obj->parmdp, (dvoid *)&obj->is_final_type,(ub4 *) 0, OCI_ATTR_IS_FINAL_TYPE, status);

		if (status != OCI_SUCCESS) {
			oci_error(sth,imp_sth->errhp, status, "OCIAttrGet");
			return 0;
		}
		OCIAttrGet_parmdp(imp_sth,  obj->parmdp, (dvoid *)&obj->field_count,(ub4 *) 0, OCI_ATTR_NUM_TYPE_ATTRS, status);

		if (status != OCI_SUCCESS) {
			oci_error(sth,imp_sth->errhp, status, "OCIAttrGet");
			return 0;
		}

		/*now get the differnt fields of this object add one field object for property*/
		Newz(1, obj->fields, (unsigned) obj->field_count, fbh_obj_t);

		/*a field is just another instance of an obj not a new struct*/

		OCIAttrGet_parmdp(imp_sth,  obj->parmdp, (dvoid *)&list_attr,(ub4 *) 0, OCI_ATTR_LIST_TYPE_ATTRS, status);

		if (status != OCI_SUCCESS) {
			oci_error(sth,imp_sth->errhp, status, "OCIAttrGet");
			return 0;
		}


		for (pos = 1; pos <= obj->field_count; pos++){
			OCIParam *parmdf= (OCIParam *) 0;
			fbh_obj_t *fld = &obj->fields[pos-1]; /*get the field holder*/

			OCIParamGet_log_stat(imp_sth, (dvoid *) list_attr,(ub4) OCI_DTYPE_PARAM, imp_sth->errhp,(dvoid *)&parmdf, (ub4) pos ,status);

			if (status != OCI_SUCCESS) {
				oci_error(sth,imp_sth->errhp, status, "OCIParamGet");
				return 0;
			}

			OCIAttrGet_parmdp(imp_sth,  (dvoid*)parmdf, (dvoid *)&fld->type_name,(ub4 *) &fld->type_namel, OCI_ATTR_NAME, status);

			/* get the name of the attribute */

			if (status != OCI_SUCCESS) {
				oci_error(sth,imp_sth->errhp, status, "OCIAttrGet");
				return 0;
			}

				OCIAttrGet_parmdp(imp_sth,  (dvoid*)parmdf, (void *)&fld->typecode,(ub4 *) 0, OCI_ATTR_TYPECODE, status);

			if (status != OCI_SUCCESS) {
				oci_error(sth,imp_sth->errhp, status, "OCIAttrGet");
				return 0;
			}

			if (DBIc_DBISTATE(imp_sth)->debug >= 6 || dbd_verbose >= 6 ) {
				PerlIO_printf(
                    DBIc_LOGPIO(imp_sth),
                    "Getting property #%d, named=%s and its typecode is %d \n",
                    pos, fld->type_name, fld->typecode);
			}

			if (fld->typecode == OCI_TYPECODE_OBJECT || fld->typecode == OCI_TYPECODE_VARRAY || fld->typecode == OCI_TYPECODE_TABLE || fld->typecode == OCI_TYPECODE_NAMEDCOLLECTION){
				 /*this is some sort of object or collection so lets drill down some more*/
				Newz(1, fld->fields, 1, fbh_obj_t);
				fld->field_count=1;/*not really needed but used internally*/
					status=describe_obj(sth,imp_sth,parmdf,fld->fields,level+1);
			}
		}
	} else {
		/*well this is an embedded table or varray of some form so find out what is in it*/

		if (DBIc_DBISTATE(imp_sth)->debug >= 6 || dbd_verbose >= 6 ) {
			PerlIO_printf(
                DBIc_LOGPIO(imp_sth),
                "Object named =%s at level %d is an Varray or Table\n",
                obj->type_name,level);
		}

		OCIAttrGet_parmdp(imp_sth,  obj->parmdp, (dvoid *)&obj->col_typecode, 0, OCI_ATTR_COLLECTION_TYPECODE, status);

		if (status != OCI_SUCCESS) {
			oci_error(sth,imp_sth->errhp, status, "OCIAttrGet");
			return 0;
		}
		/* first get what sort of collection it is by coll typecode*/
			OCIAttrGet_parmdp(imp_sth,  obj->parmdp, (dvoid *)&obj->parmap, 0, OCI_ATTR_COLLECTION_ELEMENT, status);

		if (status != OCI_SUCCESS) {
			oci_error(sth,imp_sth->errhp, status, "OCIAttrGet");
			return 0;
		}

		OCIAttrGet_parmdp(imp_sth, obj->parmap, (dvoid *)&obj->element_typecode, 0, OCI_ATTR_TYPECODE, status);

		if (status != OCI_SUCCESS) {
			oci_error(sth,imp_sth->errhp, status, "OCIAttrGet");
			return 0;
		}

		if (obj->element_typecode == OCI_TYPECODE_OBJECT || obj->element_typecode == OCI_TYPECODE_VARRAY || obj->element_typecode == OCI_TYPECODE_TABLE || obj->element_typecode == OCI_TYPECODE_NAMEDCOLLECTION){
			 /*this is some sort of object or collection so lets drill down some more*/
			fbh_obj_t *fld;
			Newz(1, obj->fields, 1, fbh_obj_t);
			fld = &obj->fields[0]; /*get the field holder*/
			obj->field_count=1; /*not really needed but used internally*/
				status=describe_obj(sth,imp_sth,obj->parmap,fld,level+1);
		}

	}
	return 1;

}


int
dump_struct(imp_sth_t *imp_sth,fbh_obj_t *obj,int level){
	dTHX;
	int i;
/*dumps the contents of the current fbh->obj*/

	PerlIO_printf(
        DBIc_LOGPIO(imp_sth), " level=%d	type_name = %s\n",level,obj->type_name);
	PerlIO_printf(
        DBIc_LOGPIO(imp_sth), "	type_namel = %u\n",obj->type_namel);
	PerlIO_printf(
        DBIc_LOGPIO(imp_sth), "	parmdp = %p\n",obj->parmdp);
	PerlIO_printf(
        DBIc_LOGPIO(imp_sth), "	parmap = %p\n",obj->parmap);
	PerlIO_printf(
        DBIc_LOGPIO(imp_sth), "	tdo = %p\n",obj->tdo);
	PerlIO_printf(
        DBIc_LOGPIO(imp_sth), "	typecode = %s\n",oci_typecode_name(obj->typecode));
	PerlIO_printf(
        DBIc_LOGPIO(imp_sth), "	col_typecode = %d\n",obj->col_typecode);
	PerlIO_printf(
        DBIc_LOGPIO(imp_sth),
        "	element_typecode = %s\n",oci_typecode_name(obj->element_typecode));
	PerlIO_printf(
        DBIc_LOGPIO(imp_sth), "	obj_ref = %p\n",obj->obj_ref);
	PerlIO_printf(DBIc_LOGPIO(imp_sth), "	obj_value = %p\n",obj->obj_value);
	PerlIO_printf(DBIc_LOGPIO(imp_sth), "	obj_type = %p\n",obj->obj_type);
	PerlIO_printf(DBIc_LOGPIO(imp_sth), "	is_final_type = %u\n",obj->is_final_type);
	PerlIO_printf(DBIc_LOGPIO(imp_sth), "	field_count = %d\n",obj->field_count);
	PerlIO_printf(DBIc_LOGPIO(imp_sth), "	fields = %p\n",obj->fields);

	for (i = 0; i < obj->field_count;i++){
		fbh_obj_t *fld = &obj->fields[i];
		PerlIO_printf(
            DBIc_LOGPIO(imp_sth),
            "  \n--->sub objects\n  ");
		dump_struct(imp_sth,fld,level+1);
	}

	PerlIO_printf(DBIc_LOGPIO(imp_sth), "  \n--->done %s\n  ",obj->type_name);

	return 1;
}





int
dbd_describe(SV *h, imp_sth_t *imp_sth)
{
	dTHX;
	D_imp_dbh_from_sth;
	D_imp_drh_from_dbh;
	UV	long_readlen;
	ub4 num_fields;
	int num_errors	= 0;
	int has_longs	= 0;
	int est_width	= 0;		/* estimated avg row width (for cache)	*/
	int nested_cursors = 0;
	ub4 i = 0;
	sword status;


	if (imp_sth->done_desc)
		return 1;	/* success, already done it */

	imp_sth->done_desc = 1;

	/* ora_trunc is checked at fetch time */
	/* long_readlen:	length for long/longraw (if >0), else 80 (ora app dflt)	*/
	/* Ought to be for COMPAT mode only but was relaxed before LongReadLen existed */
	long_readlen = (SvOK(imp_drh -> ora_long) && SvUV(imp_drh->ora_long)>0)
        ? SvUV(imp_drh->ora_long) : DBIc_LongReadLen(imp_sth);

	/* set long_readlen for SELECT or PL/SQL with output placeholders */
	imp_sth->long_readlen = long_readlen;


	if (imp_sth->stmt_type != OCI_STMT_SELECT) { /* XXX DISABLED, see num_fields test below */
		if (DBIc_DBISTATE(imp_sth)->debug >= 3 || dbd_verbose >= 3 )
			PerlIO_printf(
                DBIc_LOGPIO(imp_sth),
                "	dbd_describe skipped for %s\n",
				oci_stmt_type_name(imp_sth->stmt_type));
        /* imp_sth memory was cleared when created so no setup required here	*/
		return 1;
	}

	if (DBIc_DBISTATE(imp_sth)->debug >= 3 || dbd_verbose >= 3 )
		PerlIO_printf(
            DBIc_LOGPIO(imp_sth),
            "	dbd_describe %s (%s, lb %lu)...\n",
			oci_stmt_type_name(imp_sth->stmt_type),
			DBIc_ACTIVE(imp_sth) ? "implicit" : "EXPLICIT", (unsigned long)long_readlen);

	/* We know it's a select and we've not got the description yet, so if the	*/
	/* sth is not 'active' (executing) then we need an explicit describe.	*/
	if ( !DBIc_ACTIVE(imp_sth) ) {

		OCIStmtExecute_log_stat(imp_sth, imp_sth->svchp, imp_sth->stmhp, imp_sth->errhp,
                                0, 0, 0, 0, OCI_DESCRIBE_ONLY, status);
		if (status != OCI_SUCCESS) {
			oci_error(h, imp_sth->errhp, status,
                      ora_sql_error(imp_sth, "OCIStmtExecute/Describe"));
			if (status != OCI_SUCCESS_WITH_INFO)
                return 0;
		}
	}
	OCIAttrGet_stmhp_stat(imp_sth, &num_fields, 0, OCI_ATTR_PARAM_COUNT, status);
	if (status != OCI_SUCCESS) {
		oci_error(h, imp_sth->errhp, status, "OCIAttrGet OCI_ATTR_PARAM_COUNT");
		return 0;
	}
	if (num_fields == 0) {
		if (DBIc_DBISTATE(imp_sth)->debug >= 3 || dbd_verbose >= 3 )
			PerlIO_printf(
                DBIc_LOGPIO(imp_sth),
                "	dbd_describe skipped for %s (no fields returned)\n",
                oci_stmt_type_name(imp_sth->stmt_type));
		/* imp_sth memory was cleared when created so no setup required here	*/
		return 1;
	}

	DBIc_NUM_FIELDS(imp_sth) = num_fields;
	Newz(42, imp_sth->fbh, num_fields, imp_fbh_t);

	/* Get number of fields and space needed for field names	*/
    /* loop though the fields and get all the fileds and thier types to get back*/

	for(i = 1; i <= num_fields; ++i) { /*start define of filed struct[i] fbh */
		char *p;
		ub4 atrlen;
		int avg_width	= 0;
		imp_fbh_t *fbh	= &imp_sth->fbh[i-1];
		fbh->imp_sth	 = imp_sth;
		fbh->field_num	= i;
		fbh->define_mode = OCI_DEFAULT;

		OCIParamGet_log_stat(imp_sth, imp_sth->stmhp, OCI_HTYPE_STMT, imp_sth->errhp,
                             (dvoid**)&fbh->parmdp, (ub4)i, status);

		if (status != OCI_SUCCESS) {
			oci_error(h, imp_sth->errhp, status, "OCIParamGet");
			return 0;
		}

		OCIAttrGet_parmdp(imp_sth, fbh->parmdp, &fbh->dbtype, 0, OCI_ATTR_DATA_TYPE, status);
		OCIAttrGet_parmdp(imp_sth, fbh->parmdp, &fbh->dbsize, 0, OCI_ATTR_DATA_SIZE, status);
		/*may be a bug in 11 where the OCI_ATTR_DATA_SIZE my return 0 which should never happen*/
		/*to fix or kludge for this I added a little code for ORA_VARCHAR2 below */

#ifdef OCI_ATTR_CHAR_USED
		/* 0 means byte-length semantics, 1 means character-length semantics */
		OCIAttrGet_parmdp(imp_sth, fbh->parmdp, &fbh->len_char_used, 0, OCI_ATTR_CHAR_USED, status);
		/* OCI_ATTR_CHAR_SIZE: like OCI_ATTR_DATA_SIZE but measured in chars	*/
		OCIAttrGet_parmdp(imp_sth, fbh->parmdp, &fbh->len_char_size, 0, OCI_ATTR_CHAR_SIZE, status);
#endif
		fbh->csid = 0; fbh->csform = 0; /* just to be sure */
#ifdef OCI_ATTR_CHARSET_ID
		OCIAttrGet_parmdp(imp_sth, fbh->parmdp, &fbh->csid,	0, OCI_ATTR_CHARSET_ID,	status);
		OCIAttrGet_parmdp(imp_sth, fbh->parmdp, &fbh->csform, 0, OCI_ATTR_CHARSET_FORM, status);
#endif
        /* OCI_ATTR_PRECISION returns 0 for most types including some numbers		*/
		OCIAttrGet_parmdp(imp_sth, fbh->parmdp, &fbh->prec,	0, OCI_ATTR_PRECISION, status);
		OCIAttrGet_parmdp(imp_sth, fbh->parmdp, &fbh->scale,  0, OCI_ATTR_SCALE,	 status);
		OCIAttrGet_parmdp(imp_sth, fbh->parmdp, &fbh->nullok, 0, OCI_ATTR_IS_NULL,	status);
		OCIAttrGet_parmdp(imp_sth, fbh->parmdp, &fbh->name,	&atrlen, OCI_ATTR_NAME,status);
		if (atrlen == 0) { /* long names can cause oracle to return 0 for atrlen */
			char buf[99];
			sprintf(buf,"field_%d_name_too_long", i);
			fbh->name = &buf[0];
			atrlen = strlen(fbh->name);
		}
		fbh->name_sv = newSVpv(fbh->name,atrlen);
		fbh->name	= SvPVX(fbh->name_sv);
		fbh->ftype	= 5;	/* default: return as null terminated string */

        /* TO_DO there is something wrong with the tracing below as sql_typecode_name
           returns NVARCHAR2 for type 2 and ORA_NUMBER is 2 */
		if (DBIc_DBISTATE(imp_sth)->debug >= 4 || dbd_verbose >= 4 )
			PerlIO_printf(
                DBIc_LOGPIO(imp_sth),
                "Describe col #%d type=%d(%s)\n",
                i,fbh->dbtype,sql_typecode_name(fbh->dbtype));

		switch (fbh->dbtype) {
            /*	the simple types	*/
          case	ORA_VARCHAR2:				/* VARCHAR2	*/

            if (fbh->dbsize == 0){
                fbh->dbsize=4000;
            }
            avg_width = fbh->dbsize / 2;
            /* FALLTHRU */
          case	ORA_CHAR:				/* CHAR		*/
            if ( CSFORM_IMPLIES_UTF8(fbh->csform) && !CS_IS_UTF8(fbh->csid) )
                fbh->disize = fbh->dbsize * 4;
            else
                fbh->disize = fbh->dbsize;

            fbh->prec	= fbh->disize;
            break;
          case	ORA_RAW:				/* RAW		*/
            fbh->disize = fbh->dbsize * 2;
            fbh->prec	= fbh->disize;
            break;
          case	ORA_NUMBER:				/* NUMBER	*/
          case	21:				/* BINARY FLOAT os-endian	*/
          case	22:				/* BINARY DOUBLE os-endian	*/
          case	100:				/* BINARY FLOAT oracle-endian	*/
          case	101:				/* BINARY DOUBLE oracle-endian	*/
            fbh->disize = 130+38+3;		/* worst case	*/
            avg_width = 4;	 /* NUMBER approx +/- 1_000_000 */
            break;

          case	ORA_DATE:				/* DATE		*/
            /* actually dependent on NLS default date format*/
            fbh->disize = 75;	/* a generous default	*/
            fbh->prec	= fbh->disize;
            avg_width = 8;	/* size in SQL*Net packet  */
            break;

          case	ORA_LONG:				/* LONG		*/
            imp_sth->row_cache_off	= 1;
            has_longs++;
            if (imp_sth->clbk_lob){ /*get by peice with callback a slow*/

                fbh->clbk_lob		= 1;
                fbh->define_mode	= OCI_DYNAMIC_FETCH; /* piecwise fetch*/
                fbh->disize 		= imp_sth->long_readlen; /*user set max value for the fetch*/
                fbh->piece_size		= imp_sth->piece_size; /*the size for each piece*/
                fbh->fetch_cleanup	= fetch_cleanup_pres_lobs; /* clean up buffer before each fetch*/

                if (!imp_sth->piece_size){ /*if not set use max value*/
                    imp_sth->piece_size=imp_sth->long_readlen;
                }

                fbh->ftype		= SQLT_CHR;
                fbh->fetch_func = fetch_clbk_lob;

            }
            else if (imp_sth->piece_lob){ /*get by peice with polling slowest*/

                fbh->piece_lob		= 1;
                fbh->define_mode	= OCI_DYNAMIC_FETCH; /* piecwise fetch*/
                fbh->disize 		= imp_sth->long_readlen; /*user set max value for the fetch*/
                fbh->piece_size		= imp_sth->piece_size; /*the size for each piece*/
                fbh->fetch_cleanup	= fetch_cleanup_pres_lobs; /* clean up buffer before each fetch*/

                if (!imp_sth->piece_size){ /*if not set use max value*/
                    imp_sth->piece_size=imp_sth->long_readlen;
                }
                fbh->ftype = SQLT_CHR;
                fbh->fetch_func = fetch_get_piece;
            }
            else {

                if ( CSFORM_IMPLIES_UTF8(fbh->csform) && !CS_IS_UTF8(fbh->csid) )
                    fbh->disize = long_readlen * 4;
                else
                    fbh->disize = long_readlen;

                /* not governed by else: */
                fbh->dbsize = (fbh->disize>65535) ? 65535 : fbh->disize;
                fbh->ftype  = 94; /* VAR form */
                fbh->fetch_func = fetch_func_varfield;

            }
            break;
          case	ORA_LONGRAW:				/* LONG RAW	*/
            has_longs++;
            if (imp_sth->clbk_lob){ /*get by peice with callback a slow*/

                fbh->clbk_lob		= 1;
                fbh->define_mode	= OCI_DYNAMIC_FETCH; /* piecwise fetch*/
                fbh->disize 		= imp_sth->long_readlen; /*user set max value for the fetch*/
                fbh->piece_size		= imp_sth->piece_size; /*the size for each piece*/
                fbh->fetch_cleanup	= fetch_cleanup_pres_lobs; /* clean up buffer before each fetch*/

                if (!imp_sth->piece_size){ /*if not set use max value*/
                    imp_sth->piece_size=imp_sth->long_readlen;
                }

                fbh->ftype = SQLT_BIN;
                fbh->fetch_func = fetch_clbk_lob;

            }
            else if (imp_sth->piece_lob){ /*get by peice with polling slowest*/

                fbh->piece_lob		= 1;
                fbh->define_mode	= OCI_DYNAMIC_FETCH; /* piecwise fetch*/
                fbh->disize 		= imp_sth->long_readlen; /*user set max value for the fetch*/
                fbh->piece_size		= imp_sth->piece_size; /*the size for each piece*/
                fbh->fetch_cleanup	= fetch_cleanup_pres_lobs; /* clean up buffer before each fetch*/

                if (!imp_sth->piece_size){ /*if not set use max value*/
                    imp_sth->piece_size=imp_sth->long_readlen;
                }
                fbh->ftype = SQLT_BIN;
                fbh->fetch_func = fetch_get_piece;
            }
            else {
                fbh->disize = long_readlen * 2;
                fbh->dbsize = (fbh->disize>65535) ? 65535 : fbh->disize;
                fbh->ftype  = 95; /* VAR form */
                fbh->fetch_func = fetch_func_varfield;
            }
            break;

          case	ORA_ROWID:				/* ROWID	*/
            fbh->disize = 20;
            fbh->prec	= fbh->disize;
            break;
          case	104:				/* ROWID Desc	*/
            fbh->disize = 2000;
            fbh->prec	= fbh->disize;
            break;
          case	108:				 /* some sort of embedded object */
            imp_sth->row_cache_off	= 1;/* cant fetch more thatn one at a time */
            fbh->ftype  = fbh->dbtype;  /*varray or alike */
            fbh->fetch_func = fetch_func_oci_object; /* need a new fetch function for it */
            fbh->fetch_cleanup = fetch_cleanup_oci_object; /* clean up any AV  from the fetch*/
            fbh->desc_t = SQLT_NTY;
            if (!imp_sth->dschp){
                OCIHandleAlloc_ok(imp_sth, imp_sth->envhp, &imp_sth->dschp, OCI_HTYPE_DESCRIBE, status);
                if (status != OCI_SUCCESS) {
                    oci_error(h,imp_sth->errhp, status, "OCIHandleAlloc");
                    ++num_errors;
                }
            }
            break;
          case	ORA_CLOB:			/* CLOB	& NCLOB	*/
          case	ORA_BLOB:			/* BLOB		*/
          case	ORA_BFILE:			/* BFILE	*/
            has_longs++;
            fbh->ftype  	  		= fbh->dbtype;
            imp_sth->ret_lobs 		= 1;
            imp_sth->row_cache_off	= 1; /* Cannot use mulit fetch for a lob*/
            /* Unless they are just getting the locator */

            if (imp_sth->pers_lob){  /*get as one peice fasted but limited to 64k big you can get.*/

                fbh->pers_lob	= 1;

                if (long_readlen){
                    fbh->disize 	=long_readlen;/*user set max value for the fetch*/
                }
                else {
                    fbh->disize 	= fbh->dbsize*10; /*default size*/
                }


                if (fbh->dbtype == ORA_CLOB){
                    fbh->ftype  = SQLT_CHR;/*SQLT_LNG*/
                }
                else {
                    fbh->ftype = SQLT_LVB; /*Binary form seems this is the only value where we can get the length correctly*/
                }
            }
            else if (imp_sth->clbk_lob){ /*get by peice with callback a slow*/
                fbh->clbk_lob		= 1;
                fbh->define_mode	= OCI_DYNAMIC_FETCH; /* piecwise fetch*/
                fbh->disize 		= imp_sth->long_readlen; /*user set max value for the fetch*/
                fbh->piece_size		= imp_sth->piece_size; /*the size for each piece*/
                fbh->fetch_cleanup	= fetch_cleanup_pres_lobs; /* clean up buffer before each fetch*/
                if (!imp_sth->piece_size){ /*if not set use max value*/
                    imp_sth->piece_size=imp_sth->long_readlen;
                }
                if (fbh->dbtype == ORA_CLOB){
                    fbh->ftype = SQLT_CHR;
                } else {
                    fbh->ftype = SQLT_BIN; /*other Binary*/
                }
                fbh->fetch_func = fetch_clbk_lob;

            }
            else if (imp_sth->piece_lob){ /*get by peice with polling slowest*/
                fbh->piece_lob		= 1;
                fbh->define_mode	= OCI_DYNAMIC_FETCH; /* piecwise fetch*/
                fbh->disize 		= imp_sth->long_readlen; /*user set max value for the fetch*/
                fbh->piece_size		= imp_sth->piece_size; /*the size for each piece*/
                fbh->fetch_cleanup 	= fetch_cleanup_pres_lobs; /* clean up buffer before each fetch*/
                if (!imp_sth->piece_size){ /*if not set use max value*/
                    imp_sth->piece_size=imp_sth->long_readlen;
                }
                if (fbh->dbtype == ORA_CLOB){
                    fbh->ftype = SQLT_CHR;
                }
                else {
                    fbh->ftype = SQLT_BIN; /*other Binary */
                }
                fbh->fetch_func = fetch_get_piece;

            }
            else { /*auto lob fetch with locator by far the fastest*/
                fbh->disize =  sizeof(OCILobLocator*);/* Size of the lob locator ar we do not really get the lob! */
                if (imp_sth->auto_lob) {
                    fbh->fetch_func = fetch_func_autolob;
                }
                else {
                    fbh->fetch_func = fetch_func_getrefpv;
                }

                fbh->bless  = "OCILobLocatorPtr";
                fbh->desc_t = OCI_DTYPE_LOB;
                OCIDescriptorAlloc_ok(imp_sth, imp_sth->envhp, &fbh->desc_h, fbh->desc_t);


            }

            break;

#ifdef OCI_DTYPE_REF
          case	111:				/* REF		*/
            fbh_setup_getrefpv(imp_sth, fbh, OCI_DTYPE_REF, "OCIRefPtr");
            break;
#endif

          case	ORA_RSET:				/* RSET		*/
            fbh->ftype  = fbh->dbtype;
            fbh->disize = sizeof(OCIStmt *);
            fbh->fetch_func = fetch_func_rset;
            fbh->fetch_cleanup = fetch_cleanup_rset;
            nested_cursors++;
            break;

          case	182:				  /* INTERVAL YEAR TO MONTH */
          case	183:				  /* INTERVAL DAY TO SECOND */
          case	185:				  /* TIME (ocidfn.h) */
          case	186:				  /* TIME WITH TIME ZONE (ocidfn.h) */
          case	187:				  /* TIMESTAMP */
          case	188: 				/* TIMESTAMP WITH TIME ZONE	*/
          case	189:				  /* INTERVAL YEAR TO MONTH (ocidfn.h) */
          case	190:				  /* INTERVAL DAY TO SECOND */
          case	232:				  /* TIMESTAMP WITH LOCAL TIME ZONE */
            /* actually dependent on NLS default date format*/
            fbh->disize = 75;		/* XXX */
            break;

          default:
			/* XXX unhandled type may lead to errors or worse */
            fbh->ftype  = fbh->dbtype;
            fbh->disize = fbh->dbsize;
            p = "Field %d has an Oracle type (%d) which is not explicitly supported%s";
            if (DBIc_DBISTATE(imp_sth)->debug >= 1 || dbd_verbose >= 3 )
                PerlIO_printf(DBIc_LOGPIO(imp_sth), p, i, fbh->dbtype, "\n");
            if (PL_dowarn)
                warn(p, i, fbh->dbtype, "");
            break;
		}

		if (DBIc_DBISTATE(imp_sth)->debug >= 3 || dbd_verbose >= 3 )
            PerlIO_printf(
                DBIc_LOGPIO(imp_sth),
                "Described col %2d: dbtype %d(%s), scale %d, prec %d, nullok %d, "
                "name %s\n		  : dbsize %d, char_used %d, char_size %d, "
                "csid %d, csform %d(%s), disize %d\n",
                i, fbh->dbtype, sql_typecode_name(fbh->dbtype), fbh->scale,
                fbh->prec, fbh->nullok, fbh->name, fbh->dbsize,
                fbh->len_char_used, fbh->len_char_size,
                fbh->csid,fbh->csform,oci_csform_name(fbh->csform), fbh->disize);

		if (fbh->ftype == 5)	/* XXX need to handle wide chars somehow */
			fbh->disize += 1;	/* allow for null terminator */

        /* dbsize can be zero for 'select NULL ...'			*/

		imp_sth->t_dbsize += fbh->dbsize;

		if (!avg_width)
			avg_width = fbh->dbsize;

		est_width += avg_width;

		if (DBIc_DBISTATE(imp_sth)->debug >= 2 || dbd_verbose >= 3 )
			dbd_fbh_dump(imp_sth, fbh, (int)i, 0);

	}/* end define of filed struct[i] fbh*/

	imp_sth->est_width = est_width;

	sth_set_row_cache(h, imp_sth,
                      (imp_dbh->max_nested_cursors) ? 0 :nested_cursors ,
                      (int)num_fields, has_longs );
	/* Initialise cache counters */
	imp_sth->in_cache  = 0;
	imp_sth->eod_errno = 0;
	/*rs_array_init(imp_sth);*/



	/* now set up the oci call with define by pos*/
	for(i=1; i <= num_fields; ++i) {
		imp_fbh_t *fbh = &imp_sth->fbh[i-1];
		int ftype = fbh->ftype;
		/* add space for STRING null term, or VAR len prefix */
		sb4 define_len = (ftype==94||ftype==95) ? fbh->disize+4 : fbh->disize;
		fb_ary_t  *fb_ary;

		if (fbh->clbk_lob || fbh->piece_lob  ){/*init the cb_abuf with this call*/
			fbh->fb_ary = fb_ary_cb_alloc(imp_sth->piece_size,define_len, imp_sth->rs_array_size);

		} else {
			fbh->fb_ary = fb_ary_alloc(define_len, imp_sth->rs_array_size);
		}

		fb_ary = fbh->fb_ary;

		if (fbh->ftype == SQLT_BIN)  {
			define_len++;
			/*add one extra byte incase the size of the lob is equal to the define_len*/
		}

		if (fbh->ftype == ORA_RSET) { /* RSET */
			OCIHandleAlloc_ok(imp_sth, imp_sth->envhp,
                              (dvoid*)&((OCIStmt **)fb_ary->abuf)[0],
                              OCI_HTYPE_STMT, status);
		}

		OCIDefineByPos_log_stat(imp_sth, imp_sth->stmhp,
                                &fbh->defnp,
                                imp_sth->errhp,
                                (ub4) i,
                                (fbh->desc_h) ? (dvoid*)&fbh->desc_h : fbh->clbk_lob  ? (dvoid *) 0: fbh->piece_lob  ? (dvoid *) 0:(dvoid*)fb_ary->abuf,
                                (fbh->desc_h) ?					0 :		define_len,
                                (ub2)fbh->ftype,
                                fb_ary->aindp,
                                (ftype==94||ftype==95) ? NULL : fb_ary->arlen,
                                fb_ary->arcode,
                                fbh->define_mode,
                                status);


		if (fbh->clbk_lob){
            /* use a dynamic callback for persistent binary and char lobs*/
			OCIDefineDynamic_log_stat(imp_sth, fbh->defnp,imp_sth->errhp,(dvoid *) fbh,status);
		}

		if (fbh->ftype == 108)  { /* Embedded object bind it differently*/
			if (DBIc_DBISTATE(imp_sth)->debug >= 5 || dbd_verbose >= 5 ){
				PerlIO_printf(
                    DBIc_LOGPIO(imp_sth),
                    "Field #%d is a  object or colection of some sort. "
                    "Using OCIDefineObject and or OCIObjectPin \n",i);
			}
			Newz(1, fbh->obj, 1, fbh_obj_t);
			fbh->obj->typecode=fbh->dbtype;
			if (!describe_obj(h,imp_sth,fbh->parmdp,fbh->obj,0)){
				++num_errors;
			}

			if (DBIc_DBISTATE(imp_sth)->debug >= 5 || dbd_verbose >= 5 ){
				dump_struct(imp_sth,fbh->obj,0);
			}
			OCIDefineObject_log_stat(imp_sth,fbh->defnp,imp_sth->errhp,fbh->obj->tdo,(dvoid**)&fbh->obj->obj_value,(dvoid**)&fbh->obj->obj_ind,status);

			if (status != OCI_SUCCESS) {
				oci_error(h,imp_sth->errhp, status, "OCIDefineObject");
				++num_errors;
			}

		}

		if (status != OCI_SUCCESS) {
			oci_error(h, imp_sth->errhp, status, "OCIDefineByPos");
			++num_errors;
		}


#ifdef OCI_ATTR_CHARSET_FORM
		if ( (fbh->dbtype == 1) && fbh->csform ) {
            /* csform may be 0 when talking to Oracle 8.0 database*/
			if (DBIc_DBISTATE(imp_sth)->debug >= 3 || dbd_verbose >= 3 )
				PerlIO_printf(
                    DBIc_LOGPIO(imp_sth),
                    "	calling OCIAttrSet OCI_ATTR_CHARSET_FORM with csform=%d (%s)\n",
                    fbh->csform,oci_csform_name(fbh->csform) );
            OCIAttrSet_log_stat(imp_sth, fbh->defnp, (ub4) OCI_HTYPE_DEFINE, (dvoid *) &fbh->csform,
                                (ub4) 0, (ub4) OCI_ATTR_CHARSET_FORM, imp_sth->errhp, status );
			if (status != OCI_SUCCESS) {
				oci_error(h, imp_sth->errhp, status, "OCIAttrSet OCI_ATTR_CHARSET_FORM");
				++num_errors;
			}
		}
#endif /* OCI_ATTR_CHARSET_FORM */

	}

	if (DBIc_DBISTATE(imp_sth)->debug >= 3 || dbd_verbose >= 3 )
		PerlIO_printf(
            DBIc_LOGPIO(imp_sth),
			"	dbd_describe'd %d columns (row bytes: %d max, %d est avg, cache: %d)\n",
			(int)num_fields, imp_sth->t_dbsize, imp_sth->est_width,
            imp_sth->prefetch_rows);

	return (num_errors>0) ? 0 : 1;
}


AV *
dbd_st_fetch(SV *sth, imp_sth_t *imp_sth){
	dTHX;
    D_imp_xxh(sth);
	sword status;
	D_imp_dbh_from_sth;
	int num_fields = DBIc_NUM_FIELDS(imp_sth);
	int ChopBlanks;
	int err;
	int i;
	AV *av;


	/* Check that execute() was executed sucessfully. This also implies	*/
	/* that dbd_describe() executed sucessfuly so the memory buffers	*/
	/* are allocated and bound.						*/
	if ( !DBIc_ACTIVE(imp_sth) ) {
		oci_error(sth, NULL, OCI_ERROR, imp_sth->nested_cursor ?
		"nested cursor is defunct (parent row is no longer current)" :
		"no statement executing (perhaps you need to call execute first)");
		return Nullav;
	}

	for(i=0; i < num_fields; ++i) {
		imp_fbh_t *fbh = &imp_sth->fbh[i];
		if (fbh->fetch_cleanup)
			fbh->fetch_cleanup(sth, fbh);
	}

	if (ora_fetchtest && DBIc_ROW_COUNT(imp_sth)>0) {
		--ora_fetchtest; /* trick for testing performance */
		status = OCI_SUCCESS;
	}
	else {
		if (DBIc_DBISTATE(imp_sth)->debug >= 3 || dbd_verbose >= 3 ){
			PerlIO_printf(
                DBIc_LOGPIO(imp_sth),
                "	dbd_st_fetch %d fields...\n", DBIc_NUM_FIELDS(imp_sth));
		}

		if (imp_sth->fetch_orient != OCI_DEFAULT) {
			if (imp_sth->exe_mode!=OCI_STMT_SCROLLABLE_READONLY)
				croak ("attempt to use a scrollable cursor without first setting ora_exe_mode to OCI_STMT_SCROLLABLE_READONLY\n") ;

			if (DBIc_DBISTATE(imp_sth)->debug >= 4 || dbd_verbose >= 4 )
				PerlIO_printf(
                    DBIc_LOGPIO(imp_sth),
                    "	Scrolling Fetch, position before fetch=%d, "
                    "Orientation = %s , Fetchoffset =%d\n",
					imp_sth->fetch_position, oci_fetch_options(imp_sth->fetch_orient),
                    imp_sth->fetch_offset);

			OCIStmtFetch_log_stat(imp_sth, imp_sth->stmhp, imp_sth->errhp,1, imp_sth->fetch_orient,imp_sth->fetch_offset, status);
				/*this will work without a round trip so might as well open it up for all statments handles*/
				/* default and OCI_FETCH_NEXT are the same so this avoids miscaluation on the next value*/
			if (status==OCI_NO_DATA){
                return Nullav;
            }

			OCIAttrGet_stmhp_stat(imp_sth, &imp_sth->fetch_position, 0, OCI_ATTR_CURRENT_POSITION, status);

			if (DBIc_DBISTATE(imp_sth)->debug >= 4 || dbd_verbose >= 4 )
				PerlIO_printf(
                    DBIc_LOGPIO(imp_sth),
                    "	Scrolling Fetch, postion after fetch=%d\n",
                    imp_sth->fetch_position);
		}
		else {

			if (imp_sth->row_cache_off){ /*Do not use array fetch or local cache */
				OCIStmtFetch_log_stat(imp_sth, imp_sth->stmhp, imp_sth->errhp,1,(ub2)OCI_FETCH_NEXT, OCI_DEFAULT, status);
				imp_sth->rs_fetch_count++;
				imp_sth->rs_array_idx=0;

			}
			else {  /*Array Fetch the New Normal Super speedy and very nice*/


 				imp_sth->rs_array_idx++;
				if (imp_sth->rs_array_num_rows<=imp_sth->rs_array_idx && (imp_sth->rs_array_status==OCI_SUCCESS || imp_sth->rs_array_status==OCI_SUCCESS_WITH_INFO)) {
/* 			PerlIO_printf(DBIc_LOGPIO(imp_sth), "	dbd_st_fetch fields...b\n");*/

					OCIStmtFetch_log_stat(imp_sth, imp_sth->stmhp,imp_sth->errhp,imp_sth->rs_array_size,(ub2)OCI_FETCH_NEXT,OCI_DEFAULT,status);

					imp_sth->rs_array_status=status;
					imp_sth->rs_fetch_count++;
					if (oci_warn &&  (imp_sth->rs_array_status == OCI_SUCCESS_WITH_INFO)) {
						oci_error(sth, imp_sth->errhp, status, "OCIStmtFetch");
					}
					OCIAttrGet_stmhp_stat(imp_sth, &imp_sth->rs_array_num_rows,0,OCI_ATTR_ROWS_FETCHED, status);
					imp_sth->rs_array_idx=0;
					imp_dbh->RowsInCache =imp_sth->rs_array_size;
					imp_sth->RowsInCache =imp_sth->rs_array_size;

					if (DBIc_DBISTATE(imp_sth)->debug >= 4 || dbd_verbose >= 4 || oci_warn)
						PerlIO_printf(
                            DBIc_LOGPIO(imp_sth),
                            "...Fetched %d rows\n",imp_sth->rs_array_num_rows);

				}
				imp_dbh->RowsInCache--;
			    imp_sth->RowsInCache--;




				if (imp_sth->rs_array_num_rows>imp_sth->rs_array_idx)	/* set status to success if rows in cache */
					status=OCI_SUCCESS;
				else
					status=imp_sth->rs_array_status;
			}
		}
	}

	if (status != OCI_SUCCESS && status !=OCI_NEED_DATA) {
		ora_fetchtest = 0;

		if (status == OCI_NO_DATA) {
			dTHR; 			/* for DBIc_ACTIVE_off	*/
			DBIc_ACTIVE_off(imp_sth);	/* eg finish		*/
			if (DBIc_DBISTATE(imp_sth)->debug >= 3 || dbd_verbose >= 3 || oci_warn)
				PerlIO_printf(
                    DBIc_LOGPIO(imp_sth),
                    "	dbd_st_fetch no-more-data, fetch count=%d\n",
                    imp_sth->rs_fetch_count-1);
			return Nullav;
		}
		if (status != OCI_SUCCESS_WITH_INFO) {
			dTHR; 			/* for DBIc_ACTIVE_off	*/
			DBIc_ACTIVE_off(imp_sth);	/* eg finish		*/
			oci_error(sth, imp_sth->errhp, status, "OCIStmtFetch");
			return Nullav;
		}
		if (oci_warn && (status == OCI_SUCCESS_WITH_INFO)) {
			oci_error(sth, imp_sth->errhp, status, "OCIStmtFetch");
		}


	/* for OCI_SUCCESS_WITH_INFO we fall through and let the	*/
	/* per-field rcode value be dealt with as we fetch the data	*/
	}

	av = DBIc_DBISTATE(imp_sth)->get_fbav(imp_sth);

	if (DBIc_DBISTATE(imp_sth)->debug >= 3 || dbd_verbose >= 3 ) {
		PerlIO_printf(
            DBIc_LOGPIO(imp_sth),
            "	dbd_st_fetched %d fields with status of %d(%s)\n",
            num_fields,status, oci_status_name(status));
	}

	ChopBlanks = DBIc_has(imp_sth, DBIcf_ChopBlanks);
	err = 0;

	for(i=0; i < num_fields; ++i) {
		imp_fbh_t *fbh		= &imp_sth->fbh[i];
		fb_ary_t *fb_ary	= fbh->fb_ary;
		int rc 				= fb_ary->arcode[imp_sth->rs_array_idx];
		ub1* row_data		= &fb_ary->abuf[0]+(fb_ary->bufl*imp_sth->rs_array_idx);
		SV *sv 				= AvARRAY(av)[i]; /* Note: we (re)use the SV in the AV	*/;


		if (DBIc_DBISTATE(imp_sth)->debug >= 4 || dbd_verbose >= 4 ) {
			PerlIO_printf(
                DBIc_LOGPIO(imp_sth),
                "	field #%d with rc=%d(%s)\n",i+1,rc,oci_col_return_codes(rc));
		}

		if (rc == 1406				/* field was truncated	*/
			&& ora_dbtype_is_long(fbh->dbtype)/* field is a LONG	*/
		){
			int oraperl = DBIc_COMPAT(imp_sth);
			D_imp_dbh_from_sth ;
			D_imp_drh_from_dbh ;
			if (DBIc_has(imp_sth,DBIcf_LongTruncOk) || (oraperl && SvIV(imp_drh -> ora_trunc))) {
			/* user says truncation is ok */
			/* Oraperl recorded the truncation in ora_errno so we	*/
			/* so also but only for Oraperl mode handles.		*/
				if (oraperl) sv_setiv(DBIc_ERR(imp_sth), (IV)rc);
					rc = 0;		/* but don't provoke an error here	*/
			}
		/* else fall through and let rc trigger failure below	*/
		}

		if  (rc == 0	|| 	/* the normal case*/
			(rc == 1406 && DBIc_has(imp_sth,DBIcf_LongTruncOk))/*Field Truncaded*/) {

			if (fbh->fetch_func) {
 				if (!fbh->fetch_func(sth, fbh, sv)){
					++err;	/* fetch_func already called oci_error */
				}
			}
			else {
				int datalen = fb_ary->arlen[imp_sth->rs_array_idx];
				char *p = (char*)row_data;
                if (rc == 1406 ){
			        datalen= fbh->disize;
				}


				if (fbh->ftype == SQLT_LVB){
					/* very special case for binary lobs that are directly fetched.
						Seems I have to use SQLT_LVB to get the length all other will fail*/
					datalen = *(ub4*)row_data;
					sv_setpvn(sv, (char*)row_data+ sizeof(ub4), datalen);
				}
				else {
					if (ChopBlanks && fbh->dbtype == 96) {
						while(datalen && p[datalen - 1]==' ')
							--datalen;
					}
					sv_setpvn(sv, p, (STRLEN)datalen);
#if DBIXS_REVISION > 13590
		/* If a bind type was specified we use DBI's sql_type_cast
			to cast it - currently only number types are handled */
					if ((fbh->req_type != 0) && (fbh->bind_flags != 0)) {
						int sts;
						char errstr[256];

						sts = DBIc_DBISTATE(imp_sth)->sql_type_cast_svpv(
                            aTHX_ sv, fbh->req_type, fbh->bind_flags, NULL);

						if (sts == 0) {
							sprintf(errstr,
								"over/under flow converting column %d to type %"IVdf"",
								i+1, fbh->req_type);
							oci_error(sth, imp_sth->errhp, OCI_ERROR, errstr);
							return Nullav;

						}
						else if (sts == -2) {
							sprintf(errstr,
								"unsupported bind type %"IVdf" for column %d",
								fbh->req_type, i+1);
                            /* issue warning */
                            DBIh_SET_ERR_CHAR(sth, imp_xxh, "0", 1, errstr, Nullch, Nullch);
                            if (CSFORM_IMPLIES_UTF8(fbh->csform) ){
                                SvUTF8_on(sv);
                            }
						}
					}
					else
#endif /* DBISTATE_VERSION > 94 */
					{
						if (CSFORM_IMPLIES_UTF8(fbh->csform) ){
							SvUTF8_on(sv);
						}
					}
				}
			}

		}
		else if (rc == 1405) {	/* field is null - return undef	*/
			sv_set_undef(sv);
		}
		else {  /* See odefin rcode arg description in OCI docs	*/
			char buf[200];
			char *hint = "";
			/* These may get more case-by-case treatment eventually.	*/
			if (rc == 1406) { /* field truncated (see above)  */
				if (!fbh->fetch_func) {
					/* Copy the truncated value anyway, it may be of use,	*/
					/* but it'll only be accessible via prior bind_column()	*/
					sv_setpvn(sv, (char *)row_data,fb_ary->arlen[imp_sth->rs_array_idx]);
 					if ((CSFORM_IMPLIES_UTF8(fbh->csform)) && (fbh->ftype != SQLT_BIN)){
						SvUTF8_on(sv);
					}
				}

				if (ora_dbtype_is_long(fbh->dbtype)){	/* double check */
					hint = ", LongReadLen too small and/or LongTruncOk not set";
				}

			}
			else {	/* set field that caused error to undef */
				sv_set_undef(sv);
			}
			++err;	/* 'fail' this fetch but continue getting fields */
					/* Some should probably be treated as warnings but	*/
					/* for now we just treat them all as errors		*/
			sprintf(buf,"ORA-%05d error on field %d of %d, ora_type %d%s",rc, i+1, num_fields, fbh->dbtype, hint);
			oci_error(sth, imp_sth->errhp, OCI_ERROR, buf);
		}

		if (DBIc_DBISTATE(imp_sth)->debug >= 5 || dbd_verbose >= 5 ){
			PerlIO_printf(
                DBIc_LOGPIO(imp_sth),
                "\n		%p (field=%d): %s\n",	 av, i,neatsvpv(sv,10));
		}
	}
	return (err) ? Nullav : av;
}


ub4
ora_parse_uid(imp_dbh_t *imp_dbh, char **uidp, char **pwdp)
{
	dTHX;
	sword status;

	/* OCI 8 does not seem to allow uid to be "name/pass" :-( */
	/* so we have to split it up ourselves */
	if (strlen(*pwdp)==0 && strchr(*uidp,'/')) {
		SV *tmpsv	= sv_2mortal(newSVpv(*uidp,0));
		*uidp 		= SvPVX(tmpsv);
		*pwdp 		= strchr(*uidp, '/');
		*(*pwdp)++ 	= '\0';
		/* XXX look for '@', e.g. "u/p@d" and "u@d" and maybe "@d"? */
	}
	if (**uidp == '\0' && **pwdp == '\0') {
		return OCI_CRED_EXT;
	}
#ifdef ORA_OCI_112
	if (!imp_dbh->using_drcp) {
#endif
		OCIAttrSet_log_stat(imp_dbh, imp_dbh->seshp, OCI_HTYPE_SESSION,
				*uidp, strlen(*uidp),
				(ub4) OCI_ATTR_USERNAME, imp_dbh->errhp, status);

		OCIAttrSet_log_stat(imp_dbh, imp_dbh->seshp, OCI_HTYPE_SESSION,
				(strlen(*pwdp)) ? *pwdp : NULL, strlen(*pwdp),
			(ub4) OCI_ATTR_PASSWORD, imp_dbh->errhp, status);
#ifdef ORA_OCI_112
	}
#endif
	return OCI_CRED_RDBMS;
}


int
ora_db_reauthenticate(SV *dbh, imp_dbh_t *imp_dbh, char *uid, char *pwd)
{
	dTHX;
	sword status;
#ifdef ORA_OCI_112
	if (imp_dbh->using_drcp) {
		return 0;
	}
#endif
	/* XXX should possibly create new session before ending the old so	*/
	/* that if the new one can't be created, the old will still work.	*/
	OCISessionEnd_log_stat(imp_dbh, imp_dbh->svchp, imp_dbh->errhp,
			imp_dbh->seshp, OCI_DEFAULT, status); /* XXX check status here?*/
	OCISessionBegin_log_stat(imp_dbh, imp_dbh->svchp, imp_dbh->errhp, imp_dbh->seshp,
			 ora_parse_uid(imp_dbh, &uid, &pwd), (ub4) OCI_DEFAULT, status);
	if (status != OCI_SUCCESS) {
		oci_error(dbh, imp_dbh->errhp, status, "OCISessionBegin");
		return 0;
	}
	return 1;
}


#ifdef not_used_curently
static char *
rowid2hex(OCIRowid *rowid)
{
	int i;
	SV *sv = sv_2mortal(newSVpv("",0));
	for (i = 0; i < OCI_ROWID_LEN; i++) {
		char buf[6];
		sprintf(buf, "%02X ", (int)(((ub1*)rowid)[i]));
		sv_catpv(sv, buf);
	}
	return SvPVX(sv);
}
#endif


static void *
alloc_via_sv(STRLEN len, SV **svp, int mortal)
{
	dTHX;
	SV *sv = newSVpv("",0);
	sv_grow(sv, len+1);
	memset(SvPVX(sv), 0, len);
	if (mortal)
	sv_2mortal(sv);
	if (svp)
	*svp = sv;
	return SvPVX(sv);
}


char *
find_ident_after(char *src, char *after, STRLEN *len, int copy)
{

	int seen_key = 0;
	char *orig = src;
	char *p;


	while(*src){
		if (*src == '\'') {
			char delim = *src;
			while(*src && *src != delim) ++src;
		}
		else if (*src == '-' && src[1] == '-') {
			while(*src && *src != '\n') ++src;
		}
		else if (*src == '/' && src[1] == '*') {
			while(*src && !(*src == '*' && src[1]=='/')) ++src;
		}
		else if (isALPHA(*src)) {
			if (seen_key) {
				char *start = src;
				while(*src && (isALNUM(*src) || *src=='.' || *src=='$' || *src=='"'))
					++src;
				*len = src - start;
				if (copy) {
					p = (char*)alloc_via_sv(*len, 0, 1);
					strncpy(p, start, *len);
					p[*len] = '\0';
					return p;
				}
				return start;
			}
			else if (  toLOWER(*src)==toLOWER(*after)
					&& (src==orig ? 1 : !isALPHA(src[-1]))) {
				p = after;
				while(*p && *src && toLOWER(*p)==toLOWER(*src))
					++p, ++src;
				if (!*p)
					seen_key = 1;
			}
			++src;
		}
	else
		++src;
	}
	return NULL;
}




struct lob_refetch_st {
	OCIStmt *stmthp;
	OCIBind *bindhp;
	OCIRowid *rowid;
	OCIParam *parmdp_tmp;
	OCIParam *parmdp_lob;
	int num_fields;
	SV *fbh_ary_sv;
	imp_fbh_t *fbh_ary;
};


static int
init_lob_refetch(SV *sth, imp_sth_t *imp_sth)
{
	dTHX;
	SV *sv;
	SV *sql_select;
	HV *lob_cols_hv = NULL;
	sword status;
	OCIError *errhp = imp_sth->errhp;
	OCIParam *parmhp = NULL, *collisthd = NULL, *colhd = NULL;
	ub2 numcols = 0;
	imp_fbh_t *fbh;
	int unmatched_params;
	I32 i,j;
	char *p;
	lob_refetch_t *lr = NULL;
	STRLEN tablename_len;
	char *tablename;
	char new_tablename[100];
	switch (imp_sth->stmt_type) {
		case OCI_STMT_UPDATE:
			tablename = find_ident_after(imp_sth->statement,
				"update", &tablename_len, 1);
			break;
		case OCI_STMT_INSERT:
			tablename = find_ident_after(imp_sth->statement,
				"into", &tablename_len, 1);
			break;
		default:
		return oci_error(sth, errhp, OCI_ERROR,
			"LOB refetch attempted for unsupported statement type (see also ora_auto_lob attribute)");
	}

	if (!tablename)
		return oci_error(sth, errhp, OCI_ERROR,
		"Unable to parse table name for LOB refetch");

 	if (!imp_sth->dschp){
        OCIHandleAlloc_ok(imp_sth, imp_sth->envhp, &imp_sth->dschp, OCI_HTYPE_DESCRIBE, status);
			if (status != OCI_SUCCESS) {
			oci_error(sth,imp_sth->errhp, status, "OCIHandleAlloc");
		}

	 }

	OCIDescribeAny_log_stat(imp_sth, imp_sth->svchp, errhp, tablename, strlen(tablename),
		(ub1)OCI_OTYPE_NAME, (ub1)1, (ub1)OCI_PTYPE_SYN, imp_sth->dschp, status);

	if (status == OCI_SUCCESS) { /* There is a synonym, get the schema */
		char *syn_schema=NULL;
		char syn_name[100];
		ub4  tn_len = 0, syn_schema_len = 0;

		strncpy(syn_name,tablename,strlen(tablename));
		/* Put the synonym name here for later user */

		OCIAttrGet_log_stat(imp_sth, imp_sth->dschp,  OCI_HTYPE_DESCRIBE,
				&parmhp, 0, OCI_ATTR_PARAM, errhp, status);

		OCIAttrGet_log_stat(imp_sth, parmhp, OCI_DTYPE_PARAM,
				&syn_schema, &syn_schema_len, OCI_ATTR_SCHEMA_NAME, errhp, status);


		OCIAttrGet_log_stat(imp_sth, parmhp, OCI_DTYPE_PARAM,
				&tablename, &tn_len, OCI_ATTR_NAME, errhp, status);

		strncpy(new_tablename,syn_schema,syn_schema_len);
		new_tablename[syn_schema_len+1] = '\0';
		new_tablename[syn_schema_len]='.';
		strncat(new_tablename, tablename,tn_len);

		tablename=new_tablename;

		if (DBIc_DBISTATE(imp_sth)->debug >= 3 || dbd_verbose >= 3 )
			PerlIO_printf(
                DBIc_LOGPIO(imp_sth),
                "		lob refetch using a synonym named=%s for %s \n",
                syn_name,tablename);


	}
	OCIDescribeAny_log_stat(imp_sth, imp_sth->svchp, errhp, tablename, strlen(tablename),
		(ub1)OCI_OTYPE_NAME, (ub1)1, (ub1)OCI_PTYPE_TABLE, imp_sth->dschp, status);

	if (status != OCI_SUCCESS) {
	/* XXX this OCI_PTYPE_TABLE->OCI_PTYPE_VIEW fallback should actually be	*/
	/* a loop that includes synonyms etc */
		OCIDescribeAny_log_stat(imp_sth, imp_sth->svchp, errhp, tablename, strlen(tablename),
			(ub1)OCI_OTYPE_NAME, (ub1)1, (ub1)OCI_PTYPE_VIEW, imp_sth->dschp, status);
		if (status != OCI_SUCCESS) {
			OCIHandleFree_log_stat(imp_sth, imp_sth->dschp, OCI_HTYPE_DESCRIBE, status);
			return oci_error(sth, errhp, status, "OCIDescribeAny(view)/LOB refetch");
		}
	}

	OCIAttrGet_log_stat(imp_sth, imp_sth->dschp,  OCI_HTYPE_DESCRIBE,
				&parmhp, 0, OCI_ATTR_PARAM, errhp, status);
	if (!status ) {
		OCIAttrGet_log_stat(imp_sth, parmhp, OCI_DTYPE_PARAM,
				&numcols, 0, OCI_ATTR_NUM_COLS, errhp, status);
	}

	if (!status ) {
		OCIAttrGet_log_stat(imp_sth, parmhp, OCI_DTYPE_PARAM,
				&collisthd, 0, OCI_ATTR_LIST_COLUMNS, errhp, status);
	}

	if (status != OCI_SUCCESS) {
		OCIHandleFree_log_stat(imp_sth, imp_sth->dschp, OCI_HTYPE_DESCRIBE, status);
		return oci_error(sth, errhp, status, "OCIDescribeAny/OCIAttrGet/LOB refetch");
	}

	if (DBIc_DBISTATE(imp_sth)->debug >= 3 || dbd_verbose >= 3 )
		PerlIO_printf(
            DBIc_LOGPIO(imp_sth),
            "		lob refetch from table %s, %d columns:\n",
            tablename, numcols);

	for (i = 1; i <= (long)numcols; i++) {
		ub2 col_dbtype;
		char *col_name;
		ub4  col_name_len;
		OCIParamGet_log_stat(imp_sth, collisthd, OCI_DTYPE_PARAM, errhp, (dvoid**)&colhd, i, status);
		if (status)
			break;

		OCIAttrGet_log_stat(imp_sth, colhd, OCI_DTYPE_PARAM, &col_dbtype, 0,
							OCI_ATTR_DATA_TYPE, errhp, status);
		if (status)
			break;

		OCIAttrGet_log_stat(imp_sth, colhd, OCI_DTYPE_PARAM, &col_name, &col_name_len,
				OCI_ATTR_NAME, errhp, status);
		if (status)
			break;

		if (DBIc_DBISTATE(imp_sth)->debug >= 3 || dbd_verbose >= 3 )
			PerlIO_printf(
                DBIc_LOGPIO(imp_sth),
                "		lob refetch table col %d: '%.*s' otype %d\n",
				(int)i, (int)col_name_len,col_name, col_dbtype);

		if (col_dbtype != SQLT_CLOB && col_dbtype != SQLT_BLOB)
			continue;

		if (!lob_cols_hv)
			lob_cols_hv = newHV();

		sv = newSViv(col_dbtype);
		(void)sv_setpvn(sv, col_name, col_name_len);

		if (CSFORM_IMPLIES_UTF8(SQLCS_IMPLICIT))
			SvUTF8_on(sv);

		(void)SvIOK_on(sv);	/* "what a wonderful hack!" */
		(void)hv_store(lob_cols_hv, col_name,col_name_len, sv,0);
		OCIDescriptorFree_log(imp_sth, colhd, OCI_DTYPE_PARAM);
		colhd = NULL;
	}

	if (colhd)
		OCIDescriptorFree_log(imp_sth, colhd, OCI_DTYPE_PARAM);

	if (status != OCI_SUCCESS) {
		oci_error(sth, errhp, status,
			"OCIDescribeAny/OCIParamGet/OCIAttrGet/LOB refetch");
		OCIHandleFree_log_stat(imp_sth, imp_sth->dschp, OCI_HTYPE_DESCRIBE, status);
		return 0;
	}

	if (!lob_cols_hv)
		return oci_error(sth, errhp, OCI_ERROR,
			"LOB refetch failed, no lobs in table");

	/*	our bind params are in %imp_sth->all_params_hv
	our table cols are in %lob_cols_hv
	we now iterate through our bind params
	and allocate them to the appropriate table columns
	*/
	Newz(1, lr, 1, lob_refetch_t);
	unmatched_params = 0;
	lr->num_fields = 0;
	lr->fbh_ary = (imp_fbh_t*)alloc_via_sv(sizeof(imp_fbh_t) * HvKEYS(lob_cols_hv)+1,
	&lr->fbh_ary_sv, 0);

	sql_select = sv_2mortal(newSVpv("select ",0));

	hv_iterinit(imp_sth->all_params_hv);
	while( (sv = hv_iternextsv(imp_sth->all_params_hv, &p, &i)) != NULL ) {
		int matched = 0;
		phs_t *phs = (phs_t*)(void*)SvPVX(sv);

		if (sv == &PL_sv_undef || !phs)
			croak("panic: unbound params");

		if (phs->ftype != SQLT_CLOB && phs->ftype != SQLT_BLOB)
			continue;

		hv_iterinit(lob_cols_hv);

		while( (sv = hv_iternextsv(lob_cols_hv, &p, &j)) != NULL ) {
			char sql_field[200];
			if (phs->ora_field) {	/* must match this phs by field name	*/
				char *ora_field_name = SvPV(phs->ora_field,PL_na);
				if (SvCUR(phs->ora_field) != SvCUR(sv)
					|| ibcmp(ora_field_name, SvPV(sv,PL_na), (I32)SvCUR(sv) ) )
					continue;
			}
			else {			/* basic dumb match by type		*/
				if (phs->ftype != SvIV(sv)){
					continue;
				}
				else {			/* got a type match - check it's safe	*/
					SV *sv_other;
					char *p_other;
					/* would any other lob field match this type? */
					while( (sv_other = hv_iternextsv(lob_cols_hv, &p_other, &i)) != NULL ) {
						if (phs->ftype != SvIV(sv_other))
							continue;
						if (DBIc_DBISTATE(imp_sth)->debug >= 3 || dbd_verbose >= 3 )
							PerlIO_printf(
                                DBIc_LOGPIO(imp_sth),
                                "		both %s and %s have type %d - ambiguous\n",
                                neatsvpv(sv,0), neatsvpv(sv_other,0),
                                (int)SvIV(sv_other));
						Safefree(lr);
						sv_free((SV*)lob_cols_hv);
						return oci_error(sth, errhp, OCI_ERROR,
						"Need bind_param(..., { ora_field=>... }) attribute to identify table LOB field names");
					}
				}
			}

			matched = 1;
			sprintf(sql_field, "%s%s \"%s\"",
			(SvCUR(sql_select)>7)?", ":"", p, &phs->name[1]);
			sv_catpv(sql_select, sql_field);

			if (DBIc_DBISTATE(imp_sth)->debug >= 3 || dbd_verbose >= 3 )
				PerlIO_printf(
                    DBIc_LOGPIO(imp_sth),
                    "		lob refetch %s param: otype %d, matched field '%s' %s(%s)\n",
					phs->name, phs->ftype, p,
					(phs->ora_field) ? "by name " : "by type ", sql_field);
					(void)hv_delete(lob_cols_hv, p, i, G_DISCARD);
					fbh = &lr->fbh_ary[lr->num_fields++];
					fbh->name	= phs->name;
					fbh->ftype  = phs->ftype;
					fbh->dbtype = phs->ftype;
					fbh->disize = 99;
					fbh->desc_t = OCI_DTYPE_LOB;
					OCIDescriptorAlloc_ok(imp_sth, imp_sth->envhp, &fbh->desc_h, fbh->desc_t);

			break;	/* we're done with this placeholder now	*/

		}
		if (!matched) {
			++unmatched_params;
			if (DBIc_DBISTATE(imp_sth)->debug >= 3 || dbd_verbose >= 3 )
				PerlIO_printf(
                    DBIc_LOGPIO(imp_sth),
					"		lob refetch %s param: otype %d, UNMATCHED\n",
					phs->name, phs->ftype);
		}
	}
	sv_free((SV*)lob_cols_hv);

	if (unmatched_params) {
		Safefree(lr);
		return oci_error(sth, errhp, OCI_ERROR,
			"Can't match some parameters to LOB fields in the table, check type and name");
	}

	sv_catpv(sql_select, " from ");
	sv_catpv(sql_select, tablename);
	sv_catpv(sql_select, " where rowid = :rid for update"); /* get row with lock */
	if (DBIc_DBISTATE(imp_sth)->debug >= 3 || dbd_verbose >= 3 )
		PerlIO_printf(
            DBIc_LOGPIO(imp_sth),
			"		lob refetch sql: %s\n", SvPVX(sql_select));
	lr->stmthp = NULL;
	lr->bindhp = NULL;
	lr->rowid  = NULL;
	lr->parmdp_tmp = NULL;
	lr->parmdp_lob = NULL;
	OCIHandleAlloc_ok(imp_sth, imp_sth->envhp, &lr->stmthp, OCI_HTYPE_STMT, status);
	OCIStmtPrepare_log_stat(imp_sth, lr->stmthp, errhp,
		(text*)SvPVX(sql_select), SvCUR(sql_select), OCI_NTV_SYNTAX,
			OCI_DEFAULT, status);

	if (status != OCI_SUCCESS) {
		OCIHandleFree(lr->stmthp, OCI_HTYPE_STMT);
		Safefree(lr);
		return oci_error(sth, errhp, status, "OCIStmtPrepare/LOB refetch");
	}

	/* bind the rowid input */
	OCIDescriptorAlloc_ok(imp_sth, imp_sth->envhp, &lr->rowid, OCI_DTYPE_ROWID);
	OCIBindByName_log_stat(imp_sth, lr->stmthp, &lr->bindhp, errhp, (text*)":rid", 4,
		&lr->rowid, sizeof(OCIRowid*), SQLT_RDD, 0,0,0,0,0, OCI_DEFAULT, status);
	if (status != OCI_SUCCESS) {
		OCIDescriptorFree_log(imp_sth, lr->rowid, OCI_DTYPE_ROWID);
		OCIHandleFree(lr->stmthp, OCI_HTYPE_STMT);
		Safefree(lr);
		return oci_error(sth, errhp, status, "OCIBindByPos/LOB refetch");
	}

		/* define the output fields */
	for(i=0; i < lr->num_fields; ++i) {
		OCIDefine *defnp = NULL;
		imp_fbh_t *fbh = &lr->fbh_ary[i];
		phs_t *phs;
		SV **phs_svp = hv_fetch(imp_sth->all_params_hv, fbh->name,strlen(fbh->name), 0);
		if (!phs_svp)
			croak("panic: LOB refetch for '%s' param (%ld) - name not found",fbh->name,(unsigned long)i+1);
		phs = (phs_t*)(void*)SvPVX(*phs_svp);
		fbh->special = phs;
		if (DBIc_DBISTATE(imp_sth)->debug >= 3 || dbd_verbose >= 3 )
			PerlIO_printf(
                DBIc_LOGPIO(imp_sth),
				"		lob refetch %d for '%s' param: ftype %d setup\n",
		(int)i+1,fbh->name, fbh->dbtype);
		fbh->fb_ary = fb_ary_alloc(fbh->disize, 1);
		OCIDefineByPos_log_stat(imp_sth, lr->stmthp, &defnp, errhp, (ub4)i+1,
			&fbh->desc_h, -1, (ub2)fbh->ftype,
		fbh->fb_ary->aindp, 0, fbh->fb_ary->arcode, OCI_DEFAULT, status);
		if (status != OCI_SUCCESS) {
			OCIDescriptorFree_log(imp_sth, lr->rowid, OCI_DTYPE_ROWID);
			OCIHandleFree(lr->stmthp, OCI_HTYPE_STMT);
			Safefree(lr);
			fb_ary_free(fbh->fb_ary);
			fbh->fb_ary = NULL;
			return oci_error(sth, errhp, status, "OCIDefineByPos/LOB refetch");
		}
	}

	OCIHandleFree_log_stat(imp_sth, imp_sth->dschp, OCI_HTYPE_DESCRIBE, status);

	imp_sth->lob_refetch = lr;	/* structure copy */
	return 1;
}

int
post_execute_lobs(SV *sth, imp_sth_t *imp_sth, ub4 row_count)	/* XXX leaks handles on error */
{

	/* To insert a new LOB transparently (without using 'INSERT . RETURNING .')	*/
	/* we have to insert an empty LobLocator and then fetch it back from the	*/
	/* server before we can call OCILobWrite on it! This function handles that.	*/
	dTHX;
	sword status;
	int i;
	OCIError *errhp = imp_sth->errhp;
	lob_refetch_t *lr;
	D_imp_dbh_from_sth;
	SV *dbh = (SV*)DBIc_MY_H(imp_dbh);

	if (!imp_sth->auto_lob)
		 return 1;	/* application doesn't want magical lob handling */

	if (imp_sth->stmt_type == OCI_STMT_BEGIN || imp_sth->stmt_type == OCI_STMT_DECLARE){
	/* PL/SQL is handled by lob_phs_ora_free_templobpost_execute */
		if (imp_sth->has_lobs) { 	  /*get rid of OCILob Temporary used in non inout bind*/
			SV *phs_svp;
			I32 i;
			char *p;
			hv_iterinit(imp_sth->all_params_hv);
			while( (phs_svp = hv_iternextsv(imp_sth->all_params_hv, &p, &i)) != NULL ) {
				phs_t *phs = (phs_t*)(void*)SvPVX(phs_svp);



				if (phs->desc_h && !phs->is_inout){
                    OCILobFreeTemporary_log_stat(imp_sth, imp_sth->svchp, imp_sth->errhp, phs->desc_h, status);


				/*	boolean lobEmpty=1;*/
				/*	OCIAttrSet_log_stat(phs->desc_h, phs->desc_t,&lobEmpty, 0, OCI_ATTR_LOBEMPTY, imp_sth->errhp, status);*/
				/*	OCIHandleFree_log_stat(phs->desc_h, phs->desc_t, status);*/
				}
				/*this seem to cause an error later on so I just got rid of it for Now does */
				/* not seem to kill anything */
			}
		}
		return 1;
	}

	if (row_count == 0)
		return 1;	/* nothing to do */
	if (row_count  > 1)
		return oci_error(sth, errhp, OCI_ERROR, "LOB refetch attempted for multiple rows");

	if (!imp_sth->lob_refetch) {
		if (!init_lob_refetch(sth, imp_sth))
			return 0;	/* init_lob_refetch already called oci_error */
	}
	lr = imp_sth->lob_refetch;

	OCIAttrGet_stmhp_stat(imp_sth, lr->rowid, 0, OCI_ATTR_ROWID,status);

	if (status != OCI_SUCCESS)
		return oci_error(sth, errhp, status, "OCIAttrGet OCI_ATTR_ROWID /LOB refetch");

	OCIStmtExecute_log_stat(imp_sth, imp_sth->svchp, lr->stmthp, errhp,1, 0, NULL, NULL, OCI_DEFAULT, status);	/* execute and fetch */

	if (status != OCI_SUCCESS)
		return oci_error(sth, errhp, status,

	ora_sql_error(imp_sth,"OCIStmtExecute/LOB refetch"));

	for(i=0; i < lr->num_fields; ++i) {
		imp_fbh_t *fbh = &lr->fbh_ary[i];
		int rc = fbh->fb_ary->arcode[0];
		phs_t *phs = (phs_t*)fbh->special;
		ub4 amtp;

		(void)SvUPGRADE(phs->sv, SVt_PV);

		amtp = SvCUR(phs->sv);		/* XXX UTF8? */
		if (rc == 1405) {		/* NULL - return undef */
			sv_set_undef(phs->sv);
			status = OCI_SUCCESS;
		}
		else if (amtp > 0) {	/* since amtp==0 & OCI_ONE_PIECE fail (OCI 8.0.4) */
			if( ! fbh->csid ) {
				ub1 csform = SQLCS_IMPLICIT;
				ub2 csid = 0;
				OCILobCharSetForm_log_stat(imp_sth,
                                           imp_sth->envhp,
                                           errhp,
                                           (OCILobLocator*)fbh->desc_h,
                                           &csform,
                                           status );
				if (status != OCI_SUCCESS)
					return oci_error(sth, errhp, status, "OCILobCharSetForm");
#ifdef OCI_ATTR_CHARSET_ID
		/* Effectively only used so AL32UTF8 works properly */
				OCILobCharSetId_log_stat(imp_sth,
                                         imp_sth->envhp,
                                         errhp,
                                         (OCILobLocator*)fbh->desc_h,
                                         &csid,
                                         status );
				if (status != OCI_SUCCESS)
					return oci_error(sth, errhp, status, "OCILobCharSetId");
#endif /* OCI_ATTR_CHARSET_ID */
		/* if data is utf8 but charset isn't then switch to utf8 csid */
				csid = (SvUTF8(phs->sv) && !CS_IS_UTF8(csid)) ? utf8_csid : CSFORM_IMPLIED_CSID(csform);
				fbh->csid = csid;
				fbh->csform = csform;
			}

			if (DBIc_DBISTATE(imp_sth)->debug >= 3 || dbd_verbose >= 3 )
				PerlIO_printf(
                    DBIc_LOGPIO(imp_sth),
                    "	  calling OCILobWrite fbh->csid=%d fbh->csform=%d amtp=%d\n",
					fbh->csid, fbh->csform, amtp );

			OCILobWrite_log_stat(imp_sth, imp_sth->svchp, errhp,
				(OCILobLocator*)fbh->desc_h, &amtp, 1, SvPVX(phs->sv), amtp, OCI_ONE_PIECE,
				0,0, fbh->csid ,fbh->csform, status);

			if (status != OCI_SUCCESS) {
				return oci_error(sth, errhp, status, "OCILobWrite in post_execute_lobs");
			}

		} else {			/* amtp==0 so truncate LOB to zero length */
			OCILobTrim_log_stat(imp_sth, imp_sth->svchp, errhp, (OCILobLocator*)fbh->desc_h, 0, status);

			if (status != OCI_SUCCESS) {
				return oci_error(sth, errhp, status, "OCILobTrim in post_execute_lobs");
			}

		}

		if (DBIc_DBISTATE(imp_sth)->debug >= 3 || dbd_verbose >= 3 )
			PerlIO_printf(
                DBIc_LOGPIO(imp_sth),
			"		lob refetch %d for '%s' param: ftype %d, len %ld: %s %s\n",
			i+1,fbh->name, fbh->dbtype, ul_t(amtp),
			(rc==1405 ? "NULL" : (amtp > 0) ? "LobWrite" : "LobTrim"), oci_status_name(status));

		if (status != OCI_SUCCESS) {
			return oci_error(sth, errhp, status, "OCILobTrim/OCILobWrite/LOB refetch");
		}
	}

	if (DBIc_has(imp_dbh,DBIcf_AutoCommit))
		dbd_db_commit(dbh, imp_dbh);

	return 1;
}

void
ora_free_lob_refetch(SV *sth, imp_sth_t *imp_sth)
{
	dTHX;
	lob_refetch_t *lr = imp_sth->lob_refetch;
	int i;
	sword status;
	if (lr->rowid)
		OCIDescriptorFree_log(imp_sth, lr->rowid, OCI_DTYPE_ROWID);
	OCIHandleFree_log_stat(imp_sth, lr->stmthp, OCI_HTYPE_STMT, status);

	if (status != OCI_SUCCESS)
		oci_error(sth, imp_sth->errhp, status, "ora_free_lob_refetch/OCIHandleFree");

	for(i=0; i < lr->num_fields; ++i) {
		imp_fbh_t *fbh = &lr->fbh_ary[i];
		ora_free_fbh_contents(sth, fbh);
	}
	sv_free(lr->fbh_ary_sv);
	Safefree(imp_sth->lob_refetch);
	imp_sth->lob_refetch = NULL;
}

ub4
ora_db_version(SV *dbh, imp_dbh_t *imp_dbh)
{
	dTHX;
	sword status;
	text buf[2];
	ub4 vernum;

	if( imp_dbh->server_version > 0 ) {
		return imp_dbh->server_version;
	}


	/* XXX should possibly create new session before ending the old so	*/
	/* that if the new one can't be created, the old will still work.	*/
	OCIServerRelease_log_stat(imp_dbh, imp_dbh->svchp, imp_dbh->errhp, buf, 2,OCI_HTYPE_SVCCTX, &vernum , status);
	if (status != OCI_SUCCESS) {
		oci_error(dbh, imp_dbh->errhp, status, "OCISessionServerRelease");
		return 0;
	}
	imp_dbh->server_version = vernum;
	return vernum;
}