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 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723
|
/* $Id: dbdimp.c 13931 2010-04-26 09:55:49Z mjevans $
*
* portions Copyright (c) 1994,1995,1996,1997 Tim Bunce
* portions Copyright (c) 1997 Thomas K. Wenrich
* portions Copyright (c) 1997-2001 Jeff Urlwin
* portions Copyright (c) 2007-2010 Martin J. Evans
*
* You may distribute under the terms of either the GNU General Public
* License or the Artistic License, as specified in the Perl README file.
*
*/
/*
* NOTES:
*
* o Trace levels 1 and 2 are reserved for DBI (see DBI::DBD) so don't
* use them here in DBIc_TRACE_LEVEL tests.
* "Trace Levels" in DBI defines trace levels as:
* 0 - Trace disabled.
* 1 - Trace DBI method calls returning with results or errors.
* 2 - Trace method entry with parameters and returning with results.
* 3 - As above, adding some high-level information from the driver
* and some internal information from the DBI.
* 4 - As above, adding more detailed information from the driver.
* 5 to 15 - As above but with more and more obscure information.
*
* SV Manipulation Functions
* http://perl.active-venture.com/pod/perlapi-svfunctions.html
* Formatted Printing of IVs, UVs, and NVs
* http://perldoc.perl.org/perlguts.html#Formatted-Printing-of-IVs,-UVs,-and-NVs
*/
#define NEED_newRV_noinc
#define NEED_sv_2pv_flags
#include "ODBC.h"
#if defined(WITH_UNICODE)
# include "unicode_helper.h"
#endif
#if defined(WITH_UNICODE) && (defined(_IODBCUNIX_H) || defined(_IODBCEXT_H))
#error DBD::ODBC will not run properly with iODBC in unicode mode as iODBC defines wide characters as being 4 bytes in size
#endif
/* Can't find a constant in DBI for SQL tracing but it is 256 */
#define SQL_TRACE_FLAG 0x100
#define TRACE0(a,b) PerlIO_printf(DBIc_LOGPIO(a), (b))
#define TRACE1(a,b,c) PerlIO_printf(DBIc_LOGPIO(a), (b), (c))
#define TRACE2(a,b,c,d) PerlIO_printf(DBIc_LOGPIO(a), (b), (c), (d))
#define TRACE3(a,b,c,d,e) PerlIO_printf(DBIc_LOGPIO(a), (b), (c), (d), (e))
static SQLSMALLINT default_parameter_type(imp_sth_t *imp_sth, phs_t *phs);
static int post_connect(SV *dbh, imp_dbh_t *imp_dbh, SV *attr);
static int set_odbc_version(SV *dbh, imp_dbh_t *imp_dbh, SV* attr);
static const char *S_SqlTypeToString (SWORD sqltype);
static const char *S_SqlCTypeToString (SWORD sqltype);
static const char *cSqlTables = "SQLTables(%s,%s,%s,%s)";
static const char *cSqlPrimaryKeys = "SQLPrimaryKeys(%s,%s,%s)";
static const char *cSqlStatistics = "SQLStatistics(%s,%s,%s,%d,%d)";
static const char *cSqlForeignKeys = "SQLForeignKeys(%s,%s,%s,%s,%s,%s)";
static const char *cSqlColumns = "SQLColumns(%s,%s,%s,%s)";
static const char *cSqlGetTypeInfo = "SQLGetTypeInfo(%d)";
static void AllODBCErrors(HENV henv, HDBC hdbc, HSTMT hstmt, int output, PerlIO *logfp);
static int check_connection_active(SV *h);
static int build_results(SV *sth, SV *dbh, RETCODE orc);
/* AV *dbd_st_fetch(SV * sth, imp_sth_t *imp_sth); */
int dbd_describe(SV *h, imp_sth_t *imp_sth, int more);
int dbd_db_login6_sv(SV *dbh, imp_dbh_t *imp_dbh, SV *dbname, SV *uid, SV *pwd, SV *attr);
int dbd_db_login6(SV *dbh, imp_dbh_t *imp_dbh, char *dbname, char *uid, char *pwd, SV *attr);
int dbd_st_finish(SV *sth, imp_sth_t *imp_sth);
#define av_sz(av) (av_len(av) + 1)
/* for sanity/ease of use with potentially null strings */
#define XXSAFECHAR(p) ((p) ? (p) : "(null)")
/* unique value for db attrib that won't conflict with SQL types, just
* increment by one if you are adding! */
#define ODBC_IGNORE_NAMED_PLACEHOLDERS 0x8332
#define ODBC_DEFAULT_BIND_TYPE 0x8333
#define ODBC_ASYNC_EXEC 0x8334
#define ODBC_ERR_HANDLER 0x8335
#define ODBC_ROWCACHESIZE 0x8336
#define ODBC_ROWSINCACHE 0x8337
#define ODBC_FORCE_REBIND 0x8338
#define ODBC_EXEC_DIRECT 0x8339
#define ODBC_VERSION 0x833A
#define ODBC_CURSORTYPE 0x833B
#define ODBC_QUERY_TIMEOUT 0x833C
#define ODBC_HAS_UNICODE 0x833D
#define ODBC_PUTDATA_START 0x833E
#define ODBC_OUTCON_STR 0x833F
#define ODBC_COLUMN_DISPLAY_SIZE 0x8340
#define ODBC_UTF8_ON 0x8341
#define ODBC_FORCE_BIND_TYPE 0x8342
/* This is the bind type for parameters we fall back to if the bind_param
method was not given a parameter type and SQLDescribeParam is not supported
or failed */
#ifdef WITH_UNICODE
# define ODBC_BACKUP_BIND_TYPE_VALUE SQL_WVARCHAR
# define ODBC_BACKUP_LONG_BIND_TYPE_VALUE SQL_WLONGVARCHAR
#else
# define ODBC_BACKUP_BIND_TYPE_VALUE SQL_VARCHAR
# define ODBC_BACKUP_LONG_BIND_TYPE_VALUE SQL_LONGVARCHAR
#endif
SV *dbd_param_err(SQLHANDLE h, int recno);
static int rebind_param(SV *sth, imp_sth_t *imp_sth, phs_t *phs);
static void get_param_type(SV *sth, imp_sth_t *imp_sth, phs_t *phs);
DBISTATE_DECLARE;
void dbd_init(dbistate_t *dbistate)
{
DBIS = dbistate;
}
static RETCODE odbc_set_query_timeout(SV *h, HSTMT hstmt, UV odbc_timeout)
{
RETCODE rc;
D_imp_xxh(h);
if (DBIc_TRACE(imp_xxh, 0, 0, 3)) {
TRACE1(imp_xxh, " Set timeout to: %"UVuf"\n", odbc_timeout);
}
rc = SQLSetStmtAttr(hstmt,(SQLINTEGER)SQL_ATTR_QUERY_TIMEOUT,
(SQLPOINTER)odbc_timeout,(SQLINTEGER)SQL_IS_INTEGER);
if (!SQL_SUCCEEDED(rc)) {
/* raise warnings if setting fails, but don't die? */
if (DBIc_TRACE(imp_xxh, 0, 0, 3))
TRACE1(
imp_xxh,
" !!Failed to set Statement ATTR Query Timeout to %"UVuf"\n",
odbc_timeout);
}
return rc;
}
#if 0
static UV odbc_get_query_timeout(imp_dbh_t *imp_dbh) {
RETCODE rc;
UV timeout;
rc = SQLGetConnectAttr(imp_dbh->hdbc,
(SQLINTEGER)SQL_ATTR_QUERY_TIMEOUT,
(SQLPOINTER)&timeout,sizeof(timeout), 0);
if (!SQL_SUCCEEDED(rc)) {
if (DBIc_TRACE(imp_dbh, 0, 0, 3))
TRACE1(imp_dbh, " !!Failed to get ATTR query Timeout %d\n", rc);
}
return timeout;
}
#endif /* 0 */
static void odbc_clear_result_set(SV *sth, imp_sth_t *imp_sth)
{
SV *value;
char *key;
I32 keylen;
Safefree(imp_sth->fbh);
Safefree(imp_sth->ColNames);
Safefree(imp_sth->RowBuffer);
/* dgood - Yikes! I don't want to go down to this level, */
/* but if I don't, it won't figure out that the */
/* number of columns have changed... */
if (DBIc_FIELDS_AV(imp_sth)) {
sv_free((SV*)DBIc_FIELDS_AV(imp_sth));
DBIc_FIELDS_AV(imp_sth) = Nullav;
}
while ( (value = hv_iternextsv((HV*)SvRV(sth), &key, &keylen)) ) {
if (strncmp(key, "NAME_", 5) == 0 ||
strncmp(key, "TYPE", 4) == 0 ||
strncmp(key, "PRECISION", 9) == 0 ||
strncmp(key, "SCALE", 5) == 0 ||
strncmp(key, "NULLABLE", 8) == 0
) {
hv_delete((HV*)SvRV(sth), key, keylen, G_DISCARD);
if (DBIc_TRACE(imp_sth, 0, 0, 4)) {
TRACE2(imp_sth, " ODBC_CLEAR_RESULTS '%s' => %s\n",
key, neatsvpv(value,0));
}
}
}
/*
PerlIO_printf(DBILOGFP,"CLEAR RESULTS cached attributes:\n");
while ( (value = hv_iternextsv((HV*)SvRV(inner), &key, &keylen)) ) {
PerlIO_printf(DBILOGFP,"CLEARRESULTS '%s' => %s\n", key, neatsvpv(value,0));
}
*/
imp_sth->fbh = NULL;
imp_sth->ColNames = NULL;
imp_sth->RowBuffer = NULL;
imp_sth->done_desc = 0;
}
static void odbc_handle_outparams(imp_sth_t *imp_sth, int debug)
{
int i = (imp_sth->out_params_av) ? AvFILL(imp_sth->out_params_av)+1 : 0;
if (debug >= 3)
TRACE1(imp_sth, " processing %d output parameters\n", i);
while (--i >= 0) {
phs_t *phs = (phs_t*)(void*)SvPVX(AvARRAY(imp_sth->out_params_av)[i]);
SV *sv = phs->sv;
if (debug >= 8) {
TRACE2(imp_sth, " outparam %s, length:%ld\n",
phs->name, (long)phs->strlen_or_ind);
}
/* phs->strlen_or_ind has been updated by ODBC to hold the length
* of the result */
if (phs->strlen_or_ind != SQL_NULL_DATA) {
/*
* When ODBC fills an output parameter buffer, the size of the
* data that were available is written into the memory location
* provided by strlen_or_ind pointer argument during the
* SQLBindParameter() call.
*
* If the number of bytes available exceeds the size of the output
* buffer, ODBC will truncate the data such that it fits in the
* available buffer. However, the strlen_or_ind will still reflect
* the size of the data before it was truncated.
*
* This fact provides us a way to detect truncation on this particular
* output parameter. Otherwise, the only way to detect truncation is
* through a follow-up to a SQL_SUCCESS_WITH_INFO result. Such a call
* cannot return enough information to state exactly where the
* truncation occurred.
*/
SvPOK_only(sv); /* string, disable other OK bits */
if (phs->strlen_or_ind > phs->maxlen) { /* out param truncated */
SvCUR_set(sv, phs->maxlen);
*SvEND(sv) = '\0'; /* null terminate */
if (debug >= 2) {
PerlIO_printf(
DBIc_LOGPIO(imp_sth),
" outparam %s = '%s'\t(TRUNCATED from %ld to %ld)\n",
phs->name, SvPV_nolen(sv), (long)phs->strlen_or_ind,
(long)phs->maxlen);
}
} else { /* no truncation occurred */
SvCUR_set(sv, phs->strlen_or_ind); /* new length */
*SvEND(sv) = '\0'; /* null terminate */
if (phs->strlen_or_ind == phs->maxlen &&
(phs->sql_type == SQL_NUMERIC ||
phs->sql_type == SQL_DECIMAL ||
phs->sql_type == SQL_INTEGER ||
phs->sql_type == SQL_SMALLINT ||
phs->sql_type == SQL_FLOAT ||
phs->sql_type == SQL_REAL ||
phs->sql_type == SQL_DOUBLE)) {
/*
* fix up for oracle, which leaves the buffer at the size
* requested, but only returns a few characters. The
* intent is to truncate down to the actual number of
* characters necessary. Need to find the first null
* byte and set the length there.
*/
char *pstart = SvPV_nolen(sv);
char *p = pstart;
while (*p != '\0') {
p++;
}
if (debug >= 2) {
PerlIO_printf(
DBIc_LOGPIO(imp_sth),
" outparam %s = '%s'\t(len %ld), is numeric end"
" of buffer = %d\n",
phs->name, SvPV(sv,PL_na), (long)phs->strlen_or_ind,
phs->sql_type, p - pstart);
}
SvCUR_set(sv, p - pstart);
}
}
} else { /* is NULL */
if (debug >= 2)
TRACE1(imp_sth, " outparam %s = undef (NULL)\n", phs->name);
(void)SvOK_off(phs->sv);
}
}
}
static int build_results(SV *sth, SV *dbh, RETCODE orc)
{
RETCODE rc;
D_imp_dbh(dbh);
D_imp_sth(sth);
dTHR;
if (DBIc_TRACE(imp_sth, 0, 0, 3))
TRACE2(imp_sth, " build_results sql %p\n\t%s\n",
imp_sth->hstmt, imp_sth->statement);
/* init sth pointers */
imp_sth->fbh = NULL;
imp_sth->ColNames = NULL;
imp_sth->RowBuffer = NULL;
imp_sth->RowCount = -1;
imp_sth->eod = -1;
imp_sth->odbc_column_display_size = imp_dbh->odbc_column_display_size;
imp_sth->odbc_utf8_on = imp_dbh->odbc_utf8_on;
if (!dbd_describe(sth, imp_sth, 0)) {
/* SQLFreeStmt(imp_sth->hstmt, SQL_DROP); */ /* TBD: 3.0 update */
if (DBIc_TRACE(imp_sth, 0, 0, 3)) {
TRACE0(imp_sth, " !!dbd_describe failed, build_results...!\n");
}
SQLFreeHandle(SQL_HANDLE_STMT, imp_sth->hstmt);
imp_sth->hstmt = SQL_NULL_HSTMT;
return 0; /* dbd_describe already called dbd_error() */
}
if (DBIc_TRACE(imp_sth, 0, 0, 3)) {
TRACE0(imp_sth, " dbd_describe build_results #2...!\n");
}
if (dbd_describe(sth, imp_sth, 0) <= 0) {
if (DBIc_TRACE(imp_sth, 0, 0, 3)) {
TRACE0(imp_sth, " dbd_describe build_results #3...!\n");
}
return 0;
}
DBIc_IMPSET_on(imp_sth);
if (orc != SQL_NO_DATA) {
imp_sth->RowCount = -1;
rc = SQLRowCount(imp_sth->hstmt, &imp_sth->RowCount);
dbd_error(sth, rc, "build_results/SQLRowCount");
if (rc != SQL_SUCCESS) {
return -1;
}
} else {
imp_sth->RowCount = 0;
}
DBIc_ACTIVE_on(imp_sth); /* XXX should only set for select ? */
imp_sth->eod = SQL_SUCCESS;
return 1;
}
int odbc_discon_all(SV *drh, imp_drh_t *imp_drh)
{
dTHR;
/* The disconnect_all concept is flawed and needs more work */
if (!PL_dirty && !SvTRUE(get_sv("DBI::PERL_ENDING",0))) {
DBIh_SET_ERR_CHAR(drh, (imp_xxh_t*)imp_drh, Nullch, 1,
"disconnect_all not implemented", Nullch, Nullch);
return FALSE;
}
return FALSE;
}
/* error : <=(-2), ok row count : >=0, unknown count : (-1) */
int dbd_db_execdirect( SV *dbh,
SV *statement )
{
D_imp_dbh(dbh);
SQLRETURN ret;
SQLLEN rows;
SQLHSTMT stmt;
int dbh_active;
char *sql;
STRLEN sql_len;
sql = SvPV(statement, sql_len);
if ((dbh_active = check_connection_active(dbh)) == 0) return 0;
ret = SQLAllocHandle(SQL_HANDLE_STMT, imp_dbh->hdbc, &stmt );
if (!SQL_SUCCEEDED(ret)) {
dbd_error( dbh, ret, "Statement allocation error" );
return(-2);
}
/* if odbc_query_timeout has been set, set it in the driver */
if (imp_dbh->odbc_query_timeout != -1) {
ret = odbc_set_query_timeout(dbh, stmt, imp_dbh->odbc_query_timeout);
if (!SQL_SUCCEEDED(ret)) {
dbd_error(dbh, ret, "execdirect set_query_timeout");
}
/* don't fail if the query timeout can't be set. */
}
if (DBIc_TRACE(imp_dbh, SQL_TRACE_FLAG, 0, 3)) {
TRACE1(imp_dbh, " SQLExecDirect %s\n", SvPV_nolen(statement));
}
#ifdef WITH_UNICODE
if (SvOK(statement) && DO_UTF8(statement)) {
SQLWCHAR *wsql;
STRLEN wsql_len;
SV *sql_copy;
if (DBIc_TRACE(imp_dbh, 0x02000000, 0, 0))
TRACE0(imp_dbh, " Processing utf8 sql in unicode mode\n");
sql_copy = sv_mortalcopy(statement);
SV_toWCHAR(sql_copy);
wsql = (SQLWCHAR *)SvPV(sql_copy, wsql_len);
ret = SQLExecDirectW(stmt, wsql, wsql_len / sizeof(SQLWCHAR));
} else {
if (DBIc_TRACE(imp_dbh, 0x02000000, 0, 0))
TRACE0(imp_dbh, " Processing utf8 sql in non-unicode mode\n");
ret = SQLExecDirect(stmt, (SQLCHAR *)sql, SQL_NTS);
}
#else
if (DBIc_TRACE(imp_dbh, 0x02000000, 0, 0))
TRACE0(imp_dbh, " Processing utf8 sql in non-unicode mode\n");
ret = SQLExecDirect(stmt, (SQLCHAR *)sql, SQL_NTS);
#endif
if (DBIc_TRACE(imp_dbh, 0, 0, 3))
TRACE1(imp_dbh, " SQLExecDirect = %d\n", ret);
if (!SQL_SUCCEEDED(ret) && ret != SQL_NO_DATA) {
dbd_error2(dbh, ret, "Execute immediate failed",
imp_dbh->henv, imp_dbh->hdbc, stmt );
if (ret < 0) {
rows = -2;
}
else {
rows = -3; /* ?? */
}
}
else {
if (ret == SQL_NO_DATA) {
rows = 0;
} else {
ret = SQLRowCount(stmt, &rows);
if (!SQL_SUCCEEDED(ret)) {
dbd_error( dbh, ret, "SQLRowCount failed" );
if (ret < 0)
rows = -1;
}
}
}
ret = SQLFreeHandle(SQL_HANDLE_STMT,stmt);
if (!SQL_SUCCEEDED(ret)) {
dbd_error2(dbh, ret, "Statement destruction error",
imp_dbh->henv, imp_dbh->hdbc, stmt);
}
return (int)rows;
}
void dbd_db_destroy(SV *dbh, imp_dbh_t *imp_dbh)
{
if (DBIc_ACTIVE(imp_dbh))
dbd_db_disconnect(dbh, imp_dbh);
/* Nothing in imp_dbh to be freed */
DBIc_IMPSET_off(imp_dbh);
if (DBIc_TRACE(imp_dbh, 0, 0, 8))
TRACE0(imp_dbh, " DBD::ODBC Disconnected!\n");
}
/*
* quick dumb function to handle case insensitivity for DSN= or DRIVER=
* in DSN...note this is becuase strncmpi is not available on all
* platforms using that name (VC++, Debian, etc most notably).
* Note, also, strupr doesn't seem to have a standard name, either...
*/
int dsnHasDriverOrDSN(char *dsn) {
char upper_dsn[512];
char *cp = upper_dsn;
strncpy(upper_dsn, dsn, sizeof(upper_dsn)-1);
upper_dsn[sizeof(upper_dsn)-1] = '\0';
while (*cp != '\0') {
*cp = toupper(*cp);
cp++;
}
return (strncmp(upper_dsn, "DSN=", 4) == 0 ||
strncmp(upper_dsn, "DRIVER=", 7) == 0);
}
int dsnHasUIDorPWD(char *dsn) {
char upper_dsn[512];
char *cp = upper_dsn;
strncpy(upper_dsn, dsn, sizeof(upper_dsn)-1);
upper_dsn[sizeof(upper_dsn)-1] = '\0';
while (*cp != '\0') {
*cp = toupper(*cp);
cp++;
}
return (strstr(upper_dsn, "UID=") != 0 || strstr(upper_dsn, "PWD=") != 0);
}
/************************************************************************/
/* */
/* dbd_db_login */
/* ============ */
/* */
/* Connect to a data source. */
/* Allocates henv and hdbc. */
/* NOTE: This is the old 5 argument version with no attribs */
/* */
/************************************************************************/
int dbd_db_login(
SV *dbh,
imp_dbh_t *imp_dbh,
char *dbname,
char *uid,
char *pwd)
{
return dbd_db_login6(dbh, imp_dbh, dbname, uid, pwd, Nullsv);
}
/************************************************************************/
/* */
/* dbd_db_login6_sv */
/* ================ */
/* */
/* This API was introduced in DBI after 1.607 (subversion revision */
/* 11723) and is the same as dbd_db_login6 except the connection */
/* strings are SVs so we can detect unicode strings and call */
/* SQLDriveConnectW. */
/* */
/************************************************************************/
int dbd_db_login6_sv(
SV *dbh,
imp_dbh_t *imp_dbh,
SV *dbname,
SV *uid,
SV *pwd,
SV *attr)
{
#ifndef WITH_UNICODE
if (DBIc_TRACE(imp_dbh, 0x04000000, 0, 0))
TRACE0(imp_dbh, "non-Unicode login6\n");
return dbd_db_login6(dbh, imp_dbh, SvPV_nolen(dbname),
(SvOK(uid) ? SvPV_nolen(uid) : NULL),
(SvOK(pwd) ? SvPV_nolen(pwd) : NULL), attr);
#else
D_imp_drh_from_dbh;
dTHR;
SQLRETURN rc;
SV *wconstr; /* copy of connection string in wide chrs */
/* decoded connection string in wide characters and its length to work
around an issue in older unixODBCs */
SQLWCHAR dc_constr[512];
STRLEN dc_constr_len;
if (DBIc_TRACE(imp_dbh, 0x04000000, 0, 0)) {
TRACE0(imp_dbh, "Unicode login6\n");
TRACE2(imp_dbh, "dbname=%s, uid=%s, pwd=xxxxx\n",
SvPV_nolen(dbname), neatsvpv(uid, 0));
}
imp_dbh->out_connect_string = Nullsv;
if (!imp_drh->connects) {
rc = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &imp_drh->henv);
dbd_error(dbh, rc, "db_login6_sv/SQLAllocHandle(env)");
if (!SQL_SUCCEEDED(rc)) return 0;
if (set_odbc_version(dbh, imp_dbh, attr) != 1) return 0;
}
imp_dbh->henv = imp_drh->henv; /* needed for dbd_error */
rc = SQLAllocHandle(SQL_HANDLE_DBC, imp_drh->henv, &imp_dbh->hdbc);
if (!SQL_SUCCEEDED(rc)) {
dbd_error(dbh, rc, "db_login6_sv/SQLAllocHandle(dbc)");
if (imp_drh->connects == 0) {
SQLFreeHandle(SQL_HANDLE_ENV, imp_drh->henv);
imp_drh->henv = SQL_NULL_HENV;
imp_dbh->henv = SQL_NULL_HENV; /* needed for dbd_error */
}
return 0;
}
/* If the connection string is too long to pass to SQLConnect or it
contains DSN or DRIVER, we've little choice to but to call
SQLDriverConnect and need to tag the uid/pwd on the end of the
connection string (unless they already exist). */
if ((SvCUR(dbname) > SQL_MAX_DSN_LENGTH ||
dsnHasDriverOrDSN(SvPV_nolen(dbname))) &&
!dsnHasUIDorPWD(SvPV_nolen(dbname))) {
if (SvOK(uid)) {
sv_catpv(dbname, ";UID=");
sv_catsv(dbname, uid);
}
if (SvOK(pwd)) {
sv_catpv(dbname, ";PWD=");
sv_catsv(dbname, pwd);
}
sv_catpv(dbname, ";");
/*sv_catpvf(dbname, ";UID=%s;PWD=%s;",
SvPV_nolen(uid), SvPV_nolen(pwd));*/
if (DBIc_TRACE(imp_dbh, 0x04000000, 0, 0))
TRACE1(imp_dbh, "Now using dbname = %s\n", SvPV_nolen(dbname));
}
if (DBIc_TRACE(imp_dbh, 0x04000000, 0, 0))
TRACE2(imp_dbh, " SQLDriverConnect '%s', '%s', 'xxxx'\n",
SvPV_nolen(dbname), neatsvpv(uid, 0));
wconstr = sv_mortalcopy(dbname);
utf8sv_to_wcharsv(wconstr);
/* The following is to work around a bug in SQLDriverConnectW in unixODBC
which in at least 2.2.11 (and probably up to 2.2.13 official release
[not pre-release]) core dumps if the wide connection string does not end
in a 0 (even though it should not matter as we pass the length. */
{
char *p;
memset(dc_constr, '\0', sizeof(dc_constr));
p = SvPV(wconstr, dc_constr_len);
if (dc_constr_len > (sizeof(dc_constr) - 2)) {
croak("Cannot process connection string - too long");
}
memcpy(dc_constr, p, dc_constr_len);
}
{
SQLWCHAR wout_str[512];
SQLSMALLINT wout_str_len;
rc = SQLDriverConnectW(imp_dbh->hdbc,
0, /* no hwnd */
dc_constr,
(SQLSMALLINT)(dc_constr_len / sizeof(SQLWCHAR)),
wout_str, sizeof(wout_str) / sizeof(wout_str[0]),
&wout_str_len,
SQL_DRIVER_NOPROMPT);
if (SQL_SUCCEEDED(rc)) {
imp_dbh->out_connect_string = sv_newwvn(wout_str, wout_str_len);
if (DBIc_TRACE(imp_dbh, 0x04000000, 0, 0))
TRACE1(imp_dbh, "Out connection string: %s\n",
SvPV_nolen(imp_dbh->out_connect_string));
}
}
if (!SQL_SUCCEEDED(rc)) {
SV *wuid, *wpwd;
SQLWCHAR *wuidp, *wpwdp;
SQLSMALLINT uid_len, pwd_len;
if (DBIc_TRACE(imp_dbh, 0x04000000, 0, 0))
TRACE0(imp_dbh, " SQLDriverConnectW failed:\n");
/*
* Added code for DBD::ODBC 0.39 to help return a better
* error code in the case where the user is using a
* DSN-less connection and the dbname doesn't look like a
* true DSN.
*/
if (SvCUR(dbname) > SQL_MAX_DSN_LENGTH ||
dsnHasDriverOrDSN(SvPV_nolen(dbname))) {
/* must be DSN= or some "direct" connection attributes,
* probably best to error here and give the user a real
* error code because the SQLConnect call could hide the
* real problem.
*/
dbd_error(dbh, rc, "db_login6sv/SQLDriverConnectW");
SQLFreeHandle(SQL_HANDLE_DBC, imp_dbh->hdbc);
if (imp_drh->connects == 0) {
SQLFreeHandle(SQL_HANDLE_ENV, imp_drh->henv);
imp_drh->henv = SQL_NULL_HENV;
imp_dbh->henv = SQL_NULL_HENV; /* needed for dbd_error */
}
return 0;
}
/* ok, the DSN is short, so let's try to use it to connect
* and quietly take all error messages */
AllODBCErrors(imp_dbh->henv, imp_dbh->hdbc, 0, 0, DBIc_LOGPIO(imp_dbh));
if (DBIc_TRACE(imp_dbh, 0x04000000, 0, 0))
TRACE2(imp_dbh, " SQLConnect '%s', '%s'\n",
neatsvpv(dbname, 0), neatsvpv(uid, 0));
wconstr = sv_mortalcopy(dbname);
utf8sv_to_wcharsv(wconstr);
if (SvOK(uid)) {
wuid = sv_mortalcopy(uid);
utf8sv_to_wcharsv(wuid);
wuidp = (SQLWCHAR *)SvPV_nolen(wuid);
uid_len = SvCUR(wuid) / sizeof(SQLWCHAR);
} else {
wuidp = NULL;
uid_len = 0;
}
if (SvOK(pwd)) {
wpwd = sv_mortalcopy(pwd);
utf8sv_to_wcharsv(wpwd);
wpwdp = (SQLWCHAR *)SvPV_nolen(wpwd);
pwd_len = SvCUR(wpwd) / sizeof(SQLWCHAR);
} else {
wpwdp = NULL;
pwd_len = 0;
}
rc = SQLConnectW(imp_dbh->hdbc,
(SQLWCHAR *)SvPV_nolen(wconstr),
(SQLSMALLINT)(SvCUR(wconstr) / sizeof(SQLWCHAR)),
wuidp, uid_len,
wpwdp, pwd_len);
}
if (!SQL_SUCCEEDED(rc)) {
dbd_error(dbh, rc, "db_login6sv/SQLConnectW");
SQLFreeHandle(SQL_HANDLE_DBC, imp_dbh->hdbc);
imp_dbh->hdbc = SQL_NULL_HDBC;
if (imp_drh->connects == 0) {
SQLFreeHandle(SQL_HANDLE_ENV, imp_drh->henv);
imp_drh->henv = SQL_NULL_HENV;
imp_dbh->henv = SQL_NULL_HENV; /* needed for dbd_error */
}
return 0;
} else if (rc == SQL_SUCCESS_WITH_INFO) {
dbd_error(dbh, rc, "db_login6sv/SQLConnectW");
}
if (post_connect(dbh, imp_dbh, attr) != 1) return 0;
imp_drh->connects++;
DBIc_IMPSET_on(imp_dbh); /* imp_dbh set up now */
DBIc_ACTIVE_on(imp_dbh); /* call disconnect before freeing */
return 1;
#endif /* WITH_UNICODE */
}
/************************************************************************/
/* */
/* dbd_db_login6 */
/* ============= */
/* */
/* A newer version of the dbd_db_login API with the additional attr as */
/* the sixth argument. Once everyone upgrades to at least */
/* DBI 1.60X (where X > 7) this API won't get called anymore since */
/* dbd_db_login6_sv will be favoured. */
/* */
/* NOTE: I had hoped to make dbd_db_login6_sv support Unicode and */
/* dbd_db_login6 to not support Unicode but as no one (except me) has */
/* a DBI which supports dbd_db_login6_sv and unixODBC REQUIRES us to */
/* call SQLDriverConnectW if we are going to call other SQLXXXW */
/* functions later I've got no choice but to convert the ASCII strings */
/* passed to dbd_db_login6 to wide characters when DBD::ODBC is built */
/* for Unicode. */
/* */
/************************************************************************/
int dbd_db_login6(
SV *dbh,
imp_dbh_t *imp_dbh,
char *dbname,
char *uid,
char *pwd,
SV *attr)
{
D_imp_drh_from_dbh;
dTHR;
RETCODE rc;
char dbname_local[512];
#ifdef WITH_UNICODE
SQLWCHAR wconstr[512];
STRLEN wconstr_len;
unsigned int i;
#endif
if (DBIc_TRACE(imp_dbh, 0x04000000, 0, 0))
TRACE0(imp_dbh, "dbd_db_login6\n");
if (!imp_drh->connects) {
rc = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &imp_drh->henv);
dbd_error(dbh, rc, "db_login/SQLAllocHandle(env)");
if (!SQL_SUCCEEDED(rc)) return 0;
if (set_odbc_version(dbh, imp_dbh, attr) != 1) return 0;
}
imp_dbh->henv = imp_drh->henv; /* needed for dbd_error */
imp_dbh->out_connect_string = NULL;
rc = SQLAllocHandle(SQL_HANDLE_DBC, imp_drh->henv, &imp_dbh->hdbc);
if (!SQL_SUCCEEDED(rc)) {
dbd_error(dbh, rc, "db_login/SQLAllocHandle(dbc)");
if (imp_drh->connects == 0) {
SQLFreeHandle(SQL_HANDLE_ENV, imp_drh->henv);
imp_drh->henv = SQL_NULL_HENV;
imp_dbh->henv = SQL_NULL_HENV; /* needed for dbd_error */
}
return 0;
}
#ifndef DBD_ODBC_NO_SQLDRIVERCONNECT
/* If the connection string is too long to pass to SQLConnect or it
contains DSN or DRIVER, we've little choice to but to call
SQLDriverConnect and need to tag the uid/pwd on the end of the
connection string (unless they already exist). */
if ((strlen(dbname) > SQL_MAX_DSN_LENGTH ||
dsnHasDriverOrDSN(dbname)) && !dsnHasUIDorPWD(dbname)) {
if ((strlen(dbname) +
(uid ? strlen(uid) : 0) +
(pwd ? strlen(pwd) : 0) +
12) >
sizeof(dbname_local)) {
croak("Connection string too long");
}
strcpy(dbname_local, dbname);
if (uid) {
strcat(dbname_local, ";UID=");
strcat(dbname_local, uid);
}
if (pwd) {
strcat(dbname_local, ";PWD=");
strcat(dbname_local, pwd);
}
dbname = dbname_local;
}
if (DBIc_TRACE(imp_dbh, 0x04000000, 0, 0))
TRACE2(imp_dbh, " SQLDriverConnect '%s', '%s', 'xxxx'\n",
dbname, (uid ? uid : ""));
# ifdef WITH_UNICODE
if (strlen(dbname) > (sizeof(wconstr) / sizeof(wconstr[0]))) {
croak("Connection string too big to convert to wide characters");
}
for (i = 0; i < strlen(dbname); i++) {
wconstr[i] = dbname[i];
}
wconstr[i] = 0;
wconstr_len = i;
{
SQLWCHAR wout_str[512];
SQLSMALLINT wout_str_len;
rc = SQLDriverConnectW(imp_dbh->hdbc,
0, /* no hwnd */
wconstr, (SQLSMALLINT)wconstr_len,
wout_str, sizeof(wout_str) / sizeof(wout_str[0]),
&wout_str_len,
SQL_DRIVER_NOPROMPT);
if (SQL_SUCCEEDED(rc)) {
imp_dbh->out_connect_string = sv_newwvn(wout_str, wout_str_len);
if (DBIc_TRACE(imp_dbh, 0x04000000, 0, 0))
TRACE1(imp_dbh, "Out connection string: %s\n",
SvPV_nolen(imp_dbh->out_connect_string));
}
}
# else /* WITH_UNICODE */
{
char out_str[512];
SQLSMALLINT out_str_len;
rc = SQLDriverConnect(imp_dbh->hdbc,
0, /* no hwnd */
dbname,
(SQLSMALLINT)strlen(dbname),
out_str, sizeof(out_str), &out_str_len,
SQL_DRIVER_NOPROMPT);
if (SQL_SUCCEEDED(rc)) {
imp_dbh->out_connect_string = newSVpv(out_str, out_str_len);
if (DBIc_TRACE(imp_dbh, 0x04000000, 0, 0))
TRACE1(imp_dbh, "Out connection string: %s\n",
SvPV_nolen(imp_dbh->out_connect_string));
}
}
# endif /* WITH_UNICODE */
#else
/* if we are using something that can not handle SQLDriverconnect,
* then set rc to a not OK state and we'll fall back on SQLConnect
*/
rc = SQL_ERROR;
#endif
if (!SQL_SUCCEEDED(rc)) {
if (DBIc_TRACE(imp_dbh, 0, 0, 4)) {
#ifdef DBD_ODBC_NO_SQLDRIVERCONNECT
TRACE0(imp_dbh, " !SQLDriverConnect unsupported.\n");
#else
TRACE0(imp_dbh, " SQLDriverConnect failed:\n");
#endif
}
#ifndef DBD_ODBC_NO_SQLDRIVERCONNECT
/*
* Added code for DBD::ODBC 0.39 to help return a better
* error code in the case where the user is using a
* DSN-less connection and the dbname doesn't look like a
* true DSN.
*/
if (strlen(dbname) > SQL_MAX_DSN_LENGTH || dsnHasDriverOrDSN(dbname)) {
/* must be DSN= or some "direct" connection attributes,
* probably best to error here and give the user a real
* error code because the SQLConnect call could hide the
* real problem.
*/
dbd_error(dbh, rc, "db_login/SQLConnect");
SQLFreeHandle(SQL_HANDLE_DBC, imp_dbh->hdbc);
if (imp_drh->connects == 0) {
SQLFreeHandle(SQL_HANDLE_ENV, imp_drh->henv);
imp_drh->henv = SQL_NULL_HENV;
imp_dbh->henv = SQL_NULL_HENV; /* needed for dbd_error */
}
return 0;
}
/* ok, the DSN is short, so let's try to use it to connect
* and quietly take all error messages */
AllODBCErrors(imp_dbh->henv, imp_dbh->hdbc, 0, 0, DBIc_LOGPIO(imp_dbh));
#endif /* DriverConnect supported */
if (DBIc_TRACE(imp_dbh, 0x04000000, 0, 0))
TRACE2(imp_dbh, " SQLConnect '%s', '%s'\n",
dbname, (uid ? uid : ""));
#ifdef WITH_UNICODE
{
SQLWCHAR wuid[100], wpwd[100];
SQLSMALLINT uid_len, pwd_len;
SQLWCHAR *wuidp, *wpwdp;
if (uid) {
for (i = 0; i < strlen(uid); i++) {
wuid[i] = uid[i];
}
wuid[i] = 0;
wuidp = wuid;
uid_len = strlen(uid);
} else {
wuidp = NULL;
uid_len = 0;
}
if (pwd) {
for (i = 0; i < strlen(pwd); i++) {
wpwd[i] = pwd[i];
}
wpwd[i] = 0;
wpwdp = wpwd;
pwd_len = strlen(pwd);
} else {
wpwdp = NULL;
pwd_len = 0;
}
for (i = 0; i < strlen(dbname); i++) {
wconstr[i] = dbname[i];
}
wconstr[i] = 0;
wconstr_len = i;
rc = SQLConnectW(imp_dbh->hdbc,
wconstr, wconstr_len,
wuidp, uid_len,
wpwdp, pwd_len);
}
#else
rc = SQLConnect(imp_dbh->hdbc,
dbname, (SQLSMALLINT)strlen(dbname),
uid, (SQLSMALLINT)(uid ? strlen(uid) : 0),
pwd, (SQLSMALLINT)(pwd ? strlen(pwd) : 0));
#endif
}
if (!SQL_SUCCEEDED(rc)) {
dbd_error(dbh, rc, "db_login/SQLConnect");
SQLFreeHandle(SQL_HANDLE_DBC, imp_dbh->hdbc);/* TBD: 3.0 update */
if (imp_drh->connects == 0) {
SQLFreeHandle(SQL_HANDLE_ENV, imp_drh->henv);
imp_drh->henv = SQL_NULL_HENV;
imp_dbh->henv = SQL_NULL_HENV; /* needed for dbd_error */
}
return 0;
} else if (rc == SQL_SUCCESS_WITH_INFO) {
dbd_error(dbh, rc, "db_login/SQLConnect");
}
if (post_connect(dbh, imp_dbh, attr) != 1) return 0;
imp_drh->connects++;
DBIc_IMPSET_on(imp_dbh); /* imp_dbh set up now */
DBIc_ACTIVE_on(imp_dbh); /* call disconnect before freeing */
return 1;
}
int dbd_db_disconnect(SV *dbh, imp_dbh_t *imp_dbh)
{
RETCODE rc;
D_imp_drh_from_dbh;
UDWORD autoCommit = 0;
dTHR;
/* We assume that disconnect will always work */
/* since most errors imply already disconnected. */
DBIc_ACTIVE_off(imp_dbh);
if (imp_dbh->out_connect_string) {
SvREFCNT_dec(imp_dbh->out_connect_string);
}
/* If not autocommit, should we rollback? I don't think that's
* appropriate. -- TBD: Need to check this, maybe we should
* rollback?
*/
/* TBD: 3.0 update */
rc = SQLGetConnectOption(imp_dbh->hdbc, SQL_AUTOCOMMIT, &autoCommit);
/* quietly handle a problem with SQLGetConnectOption() */
if (!SQL_SUCCEEDED(rc)) {
AllODBCErrors(imp_dbh->henv, imp_dbh->hdbc, 0,
DBIc_TRACE(imp_dbh, 0, 0, 4), DBIc_LOGPIO(imp_dbh));
}
else {
if (!autoCommit) {
rc = dbd_db_rollback(dbh, imp_dbh);
if (DBIc_TRACE(imp_dbh, 0, 0, 3)) {
TRACE1(imp_dbh,
"** auto-rollback due to disconnect without commit"
" returned %d\n", rc);
}
}
}
rc = SQLDisconnect(imp_dbh->hdbc);
if (!SQL_SUCCEEDED(rc)) {
dbd_error(dbh, rc, "db_disconnect/SQLDisconnect");
/* return 0; XXX if disconnect fails, fall through... */
}
if (DBIc_TRACE(imp_dbh, 0x04000000, 0, 0))
TRACE1(imp_dbh, "SQLDisconnect=%d\n", rc);
SQLFreeHandle(SQL_HANDLE_DBC, imp_dbh->hdbc);
imp_dbh->hdbc = SQL_NULL_HDBC;
imp_drh->connects--;
strcpy(imp_dbh->odbc_dbms_name, "disconnect");
if (imp_drh->connects == 0) {
SQLFreeHandle(SQL_HANDLE_ENV, imp_drh->henv);
imp_drh->henv = SQL_NULL_HENV;
imp_dbh->henv = SQL_NULL_HENV; /* needed for dbd_error */
}
/* We don't free imp_dbh since a reference still exists */
/* The DESTROY method is the only one to 'free' memory. */
/* Note that statement objects may still exists for this dbh! */
return 1;
}
int dbd_db_commit(SV *dbh, imp_dbh_t *imp_dbh)
{
RETCODE rc;
dTHR;
/* TBD: 3.0 update */
rc = SQLTransact(imp_dbh->henv, imp_dbh->hdbc, SQL_COMMIT);
if (!SQL_SUCCEEDED(rc)) {
dbd_error(dbh, rc, "db_commit/SQLTransact");
return 0;
}
/* support for DBI 1.20 begin_work */
if (DBIc_has(imp_dbh, DBIcf_BegunWork)) {
/* reset autocommit */
rc = SQLSetConnectOption(imp_dbh->hdbc, SQL_AUTOCOMMIT,
SQL_AUTOCOMMIT_ON);
DBIc_off(imp_dbh,DBIcf_BegunWork);
}
return 1;
}
int dbd_db_rollback(SV *dbh, imp_dbh_t *imp_dbh)
{
RETCODE rc;
dTHR;
/* TBD: 3.0 update */
rc = SQLTransact(imp_dbh->henv, imp_dbh->hdbc, SQL_ROLLBACK);
if (!SQL_SUCCEEDED(rc)) {
dbd_error(dbh, rc, "db_rollback/SQLTransact");
return 0;
}
/* support for DBI 1.20 begin_work */
if (DBIc_has(imp_dbh, DBIcf_BegunWork)) {
/* reset autocommit */
rc = SQLSetConnectOption(imp_dbh->hdbc, SQL_AUTOCOMMIT,
SQL_AUTOCOMMIT_ON);
DBIc_off(imp_dbh,DBIcf_BegunWork);
}
return 1;
}
void dbd_error2(
SV *h,
RETCODE err_rc,
char *what,
HENV henv,
HDBC hdbc,
HSTMT hstmt)
{
D_imp_xxh(h);
dTHR;
/*
* It's a shame to have to add all this stuff with imp_dbh and
* imp_sth, but imp_dbh is needed to get the odbc_err_handler
* and imp_sth is needed to get imp_dbh.
*/
struct imp_dbh_st *imp_dbh = NULL;
struct imp_sth_st *imp_sth = NULL;
if (DBIc_TRACE(imp_xxh, 0, 0, 4)) {
PerlIO_printf(
DBIc_LOGPIO(imp_xxh),
" !!dbd_error2(err_rc=%d, what=%s, handles=(%p,%p,%p)\n",
err_rc, (what ? what : "null"), henv, hdbc, hstmt);
}
switch(DBIc_TYPE(imp_xxh)) {
case DBIt_ST:
imp_sth = (struct imp_sth_st *)(imp_xxh);
imp_dbh = (struct imp_dbh_st *)(DBIc_PARENT_COM(imp_sth));
break;
case DBIt_DB:
imp_dbh = (struct imp_dbh_st *)(imp_xxh);
break;
default:
croak("panic: dbd_error2 on bad handle type");
}
while(henv != SQL_NULL_HENV) {
UCHAR sqlstate[SQL_SQLSTATE_SIZE+1];
/*
* ODBC spec says ErrorMsg must not be greater than
* SQL_MAX_MESSAGE_LENGTH (says spec) but we concatenate a little
* on the end later (e.g. sql state) so make room for more.
*/
UCHAR ErrorMsg[SQL_MAX_MESSAGE_LENGTH+512];
SWORD ErrorMsgLen;
SDWORD NativeError;
RETCODE rc = 0;
/* TBD: 3.0 update */
while(SQL_SUCCEEDED(rc=SQLError(
henv, hdbc, hstmt,
sqlstate, &NativeError,
ErrorMsg, sizeof(ErrorMsg)-1, &ErrorMsgLen))) {
ErrorMsg[ErrorMsgLen] = '\0';
sqlstate[SQL_SQLSTATE_SIZE] = '\0';
if (DBIc_TRACE(imp_dbh, 0, 0, 3)) {
PerlIO_printf(DBIc_LOGPIO(imp_dbh),
" !SQLError(%p,%p,%p) = "
"(%s, %ld, %s)\n",
henv, hdbc, hstmt, sqlstate, NativeError, ErrorMsg);
}
/*
* If there's an error handler, run it and see what it returns...
* (lifted from DBD:Sybase 0.21)
*/
if(imp_dbh->odbc_err_handler) {
dSP;
/* SV *sv, **svp; */
/* HV *hv; */
int retval, count;
ENTER;
SAVETMPS;
PUSHMARK(sp);
if (DBIc_TRACE(imp_dbh, 0, 0, 3))
TRACE0(imp_dbh, " Calling error handler\n");
/*
* Here are the args to the error handler routine:
* 1. sqlstate (string)
* 2. ErrorMsg (string)
* 3. NativeError (integer)
* That's it for now...
*/
XPUSHs(sv_2mortal(newSVpv(sqlstate, 0)));
XPUSHs(sv_2mortal(newSVpv(ErrorMsg, 0)));
XPUSHs(sv_2mortal(newSViv(NativeError)));
PUTBACK;
if((count = call_sv(imp_dbh->odbc_err_handler, G_SCALAR)) != 1)
croak("An error handler can't return a LIST.");
SPAGAIN;
retval = POPi;
PUTBACK;
FREETMPS;
LEAVE;
/* If the called sub returns 0 then ignore this error */
if(retval == 0) {
if (DBIc_TRACE(imp_dbh, 0, 0, 3))
TRACE0(imp_dbh,
" Handler caused error to be ignored\n");
continue;
}
}
strcat(ErrorMsg, " (SQL-");
strcat(ErrorMsg, sqlstate);
strcat(ErrorMsg, ")");
/* maybe bad way to add hint about invalid transaction
* state upon disconnect...
*/
if (what && !strcmp(sqlstate, "25000") &&
!strcmp(what, "db_disconnect/SQLDisconnect")) {
strcat(ErrorMsg, " You need to commit before disconnecting! ");
}
if (SQL_SUCCEEDED(err_rc)) {
DBIh_SET_ERR_CHAR(h, imp_xxh, "" /* information state */,
1, ErrorMsg, sqlstate, Nullch);
} else {
DBIh_SET_ERR_CHAR(h, imp_xxh, Nullch, 1, ErrorMsg,
sqlstate, Nullch);
}
continue;
}
if (rc != SQL_NO_DATA_FOUND) { /* should never happen */
if (DBIc_TRACE(imp_xxh, 0, 0, 3))
TRACE1(imp_dbh,
" !!SQLError returned %d unexpectedly.\n", rc);
DBIh_SET_ERR_CHAR(
h, imp_xxh, Nullch, 1,
"Unable to fetch information about the error", "IM008", Nullch);
}
/* climb up the tree each time round the loop */
if (hstmt != SQL_NULL_HSTMT) hstmt = SQL_NULL_HSTMT;
else if (hdbc != SQL_NULL_HDBC) hdbc = SQL_NULL_HDBC;
else henv = SQL_NULL_HENV; /* done the top */
}
}
/*------------------------------------------------------------
empties entire ODBC error queue.
------------------------------------------------------------*/
void dbd_error(SV *h, RETCODE err_rc, char *what)
{
D_imp_xxh(h);
dTHR;
struct imp_dbh_st *imp_dbh = NULL;
struct imp_sth_st *imp_sth = NULL;
HSTMT hstmt = SQL_NULL_HSTMT;
switch(DBIc_TYPE(imp_xxh)) {
case DBIt_ST:
imp_sth = (struct imp_sth_st *)(imp_xxh);
imp_dbh = (struct imp_dbh_st *)(DBIc_PARENT_COM(imp_sth));
hstmt = imp_sth->hstmt;
break;
case DBIt_DB:
imp_dbh = (struct imp_dbh_st *)(imp_xxh);
break;
default:
croak("panic: dbd_error on bad handle type");
}
/*
* If status is SQL_SUCCESS, there's no error, so we can just return.
* There may be status or other non-error messsages though.
* We want those messages if the debug level is set to at least 3.
* If an error handler is installed, let it decide what messages
* should or shouldn't be reported.
*/
if ((err_rc == SQL_SUCCESS) && !DBIc_TRACE(imp_dbh, 0, 0, 3) &&
!imp_dbh->odbc_err_handler)
return;
dbd_error2(h, err_rc, what, imp_dbh->henv, imp_dbh->hdbc, hstmt);
}
void dbd_caution(SV *h, char *what)
{
D_imp_xxh(h);
dTHR;
SV *errstr;
errstr = DBIc_ERRSTR(imp_xxh);
sv_setpvn(errstr, "", 0);
sv_setiv(DBIc_ERR(imp_xxh), (IV)-1);
/* sqlstate isn't set for SQL_NO_DATA returns */
sv_setpvn(DBIc_STATE(imp_xxh), "00000", 5);
if (what) {
sv_catpv(errstr, "(DBD: ");
sv_catpv(errstr, what);
sv_catpv(errstr, " err=-1)");
}
if (DBIc_TRACE(imp_xxh, 0, 0, 3))
PerlIO_printf(DBIc_LOGPIO(imp_xxh), " !!%s error %d recorded: %s\n",
what, -1, SvPV(errstr,PL_na));
}
/*-------------------------------------------------------------------------
dbd_preparse:
- scan for placeholders (? and :xx style) and convert them to ?.
- builds translation table to convert positional parameters of the
execute() call to :nn type placeholders.
We need two data structures to translate this stuff:
- a hash to convert positional parameters to placeholders
- an array, representing the actual '?' query parameters.
%param = (name1=>plh1, name2=>plh2, ..., name_n=>plh_n) #
@qm_param = (\$param{'name1'}, \$param{'name2'}, ...)
-------------------------------------------------------------------------*/
void dbd_preparse(imp_sth_t *imp_sth, char *statement)
{
dTHR;
bool in_literal = FALSE;
char *src, *start, *dest;
phs_t phs_tpl, *phs;
SV *phs_sv;
int idx=0, style=0, laststyle=0;
int param = 0;
STRLEN namelen;
char name[256];
SV **svpp;
char ch;
char literal_ch = '\0';
/* allocate room for copy of statement with spare capacity */
imp_sth->statement = (char*)safemalloc(strlen(statement)+1);
/* initialize phs ready to be cloned per placeholder */
memset(&phs_tpl, 0, sizeof(phs_tpl));
phs_tpl.value_type = SQL_C_CHAR;
phs_tpl.sv = &PL_sv_undef;
src = statement;
dest = imp_sth->statement;
if (DBIc_TRACE(imp_sth, 0, 0, 5))
TRACE1(imp_sth, " ignore named placeholders = %d\n",
imp_sth->odbc_ignore_named_placeholders);
while(*src) {
/*
* JLU 10/6/2000 fixed to make the literal a " instead of '
* JLU 1/28/2001 fixed to make literals either " or ', but deal
* with ' "foo" ' or " foo's " correctly (just to be safe).
*
*/
if (*src == '"' || *src == '\'') {
if (!in_literal) {
literal_ch = *src;
in_literal = 1;
} else {
if (*src == literal_ch) {
in_literal = 0;
}
}
}
if ((*src != ':' && *src != '?') || in_literal) {
*dest++ = *src++;
continue;
}
start = dest; /* save name inc colon */
ch = *src++;
if (ch == '?') { /* X/Open standard */
idx++;
sprintf(name, "%d", idx);
*dest++ = ch;
style = 3;
}
else if (isDIGIT(*src)) { /* ':1' */
char *p = name;
*dest++ = '?';
idx = atoi(src);
while(isDIGIT(*src))
*p++ = *src++;
*p = 0;
style = 1;
if (DBIc_TRACE(imp_sth, 0, 0, 5))
TRACE1(imp_sth, " found numbered parameter = %s\n", name);
}
else if (!imp_sth->odbc_ignore_named_placeholders && isALNUM(*src)) {
/* ':foo' is valid, only if we are ignoring named
* parameters
*/
char *p = name;
idx++;
*dest++ = '?';
while(isALNUM(*src)) /* includes '_' */
*p++ = *src++;
*p = 0;
style = 2;
if (DBIc_TRACE(imp_sth, 0, 0, 5))
TRACE1(imp_sth, " found named parameter = %s\n", name);
}
else { /* perhaps ':=' PL/SQL construct */
*dest++ = ch;
continue;
}
*dest = '\0'; /* handy for debugging */
if (laststyle && style != laststyle)
croak("Can't mix placeholder styles (%d/%d)",style,laststyle);
laststyle = style;
if (imp_sth->all_params_hv == NULL)
imp_sth->all_params_hv = newHV();
namelen = strlen(name);
svpp = hv_fetch(imp_sth->all_params_hv, name, (I32)namelen, 0);
if (svpp == NULL) {
if (DBIc_TRACE(imp_sth, 0, 0, 5))
TRACE1(imp_sth, " creating new parameter key %s\n", name);
/* create SV holding the placeholder */
phs_sv = newSVpv((char*)&phs_tpl, sizeof(phs_tpl)+namelen+1);
phs = (phs_t*)SvPVX(phs_sv);
strcpy(phs->name, name);
phs->idx = idx;
/* store placeholder to all_params_hv */
svpp = hv_store(imp_sth->all_params_hv, name, (I32)namelen, phs_sv, 0);
} else {
if (DBIc_TRACE(imp_sth, 0, 0, 5))
TRACE1(imp_sth, " parameter key %s already exists\n", name);
croak("DBD::ODBC does not yet support binding a named parameter more than once\n");
}
}
*dest = '\0';
if (imp_sth->all_params_hv) {
DBIc_NUM_PARAMS(imp_sth) = (int)HvKEYS(imp_sth->all_params_hv);
if (DBIc_TRACE(imp_sth, 0, 0, 4))
TRACE1(imp_sth,
" dbd_preparse scanned %d distinct placeholders\n",
(int)DBIc_NUM_PARAMS(imp_sth));
}
}
int dbd_st_tables(
SV *dbh,
SV *sth,
char *catalog,
char *schema,
char *table,
char *table_type)
{
D_imp_dbh(dbh);
D_imp_sth(sth);
RETCODE rc;
int dbh_active;
/* SV **svp; */
/* char cname[128]; */ /* cursorname */
dTHR;
imp_sth->henv = imp_dbh->henv; /* needed for dbd_error */
imp_sth->hdbc = imp_dbh->hdbc;
imp_sth->done_desc = 0;
if ((dbh_active = check_connection_active(dbh)) == 0) return 0;
rc = SQLAllocHandle(SQL_HANDLE_STMT, imp_dbh->hdbc, &imp_sth->hstmt);
if (rc != SQL_SUCCESS) {
dbd_error(sth, rc, "st_tables/SQLAllocHandle(stmt)");
return 0;
}
/* just for sanity, later. Any internals that may rely on this (including */
/* debugging) will have valid data */
imp_sth->statement = (char *)safemalloc(strlen(cSqlTables)+
strlen(XXSAFECHAR(catalog)) +
strlen(XXSAFECHAR(schema)) +
strlen(XXSAFECHAR(table)) +
strlen(XXSAFECHAR(table_type))+1);
sprintf(imp_sth->statement, cSqlTables, XXSAFECHAR(catalog),
XXSAFECHAR(schema), XXSAFECHAR(table), XXSAFECHAR(table_type));
rc = SQLTables(imp_sth->hstmt,
(catalog && *catalog) ? catalog : 0, SQL_NTS,
(schema && *schema) ? schema : 0, SQL_NTS,
(table && *table) ? table : 0, SQL_NTS,
table_type && *table_type ? table_type : 0,
SQL_NTS /* type (view, table, etc) */
);
if (DBIc_TRACE(imp_sth, 0, 0, 4))
TRACE2(imp_dbh, " Tables result %d (%s)\n",
rc, table_type ? table_type : "(null)");
dbd_error(sth, rc, "st_tables/SQLTables");
if (!SQL_SUCCEEDED(rc)) {
SQLFreeHandle(SQL_HANDLE_STMT,imp_sth->hstmt);
/* SQLFreeStmt(imp_sth->hstmt, SQL_DROP);*/ /* TBD: 3.0 update */
imp_sth->hstmt = SQL_NULL_HSTMT;
return 0;
}
return build_results(sth, dbh, rc);
}
int dbd_st_primary_keys(
SV *dbh,
SV *sth,
char *catalog,
char *schema,
char *table)
{
dTHR;
D_imp_dbh(dbh);
D_imp_sth(sth);
RETCODE rc;
int dbh_active;
imp_sth->henv = imp_dbh->henv; /* needed for dbd_error */
imp_sth->hdbc = imp_dbh->hdbc;
imp_sth->done_desc = 0;
if ((dbh_active = check_connection_active(dbh)) == 0) return 0;
rc = SQLAllocHandle(SQL_HANDLE_STMT, imp_dbh->hdbc, &imp_sth->hstmt);
if (rc != SQL_SUCCESS) {
dbd_error(sth, rc, "odbc_db_primary_key_info/SQLAllocHandle(stmt)");
return 0;
}
/* just for sanity, later. Any internals that may rely on this (including */
/* debugging) will have valid data */
imp_sth->statement = (char *)safemalloc(strlen(cSqlPrimaryKeys)+
strlen(XXSAFECHAR(catalog))+
strlen(XXSAFECHAR(schema))+
strlen(XXSAFECHAR(table))+1);
sprintf(imp_sth->statement,
cSqlPrimaryKeys, XXSAFECHAR(catalog), XXSAFECHAR(schema),
XXSAFECHAR(table));
rc = SQLPrimaryKeys(imp_sth->hstmt,
(catalog && *catalog) ? catalog : 0, SQL_NTS,
(schema && *schema) ? schema : 0, SQL_NTS,
(table && *table) ? table : 0, SQL_NTS);
if (DBIc_TRACE(imp_sth, 0, 0, 4))
PerlIO_printf(
DBIc_LOGPIO(imp_dbh),
"SQLPrimaryKeys call: cat = %s, schema = %s, table = %s\n",
XXSAFECHAR(catalog), XXSAFECHAR(schema), XXSAFECHAR(table));
dbd_error(sth, rc, "st_primary_key_info/SQLPrimaryKeys");
if (!SQL_SUCCEEDED(rc)) {
SQLFreeHandle(SQL_HANDLE_STMT,imp_sth->hstmt);
/* SQLFreeStmt(imp_sth->hstmt, SQL_DROP);*/ /* TBD: 3.0 update */
imp_sth->hstmt = SQL_NULL_HSTMT;
return 0;
}
return build_results(sth, dbh, rc);
}
int dbd_st_statistics(
SV *dbh,
SV *sth,
char *catalog,
char *schema,
char *table,
int unique,
int quick)
{
dTHR;
D_imp_dbh(dbh);
D_imp_sth(sth);
RETCODE rc;
int dbh_active;
SQLUSMALLINT odbc_unique;
SQLUSMALLINT odbc_quick;
imp_sth->henv = imp_dbh->henv; /* needed for dbd_error */
imp_sth->hdbc = imp_dbh->hdbc;
imp_sth->done_desc = 0;
if ((dbh_active = check_connection_active(dbh)) == 0) return 0;
rc = SQLAllocHandle(SQL_HANDLE_STMT, imp_dbh->hdbc, &imp_sth->hstmt);
if (rc != SQL_SUCCESS) {
dbd_error(sth, rc, "odbc_db_primary_key_info/SQLAllocHandle(stmt)");
return 0;
}
odbc_unique = (unique ? SQL_INDEX_UNIQUE : SQL_INDEX_ALL);
odbc_quick = (quick ? SQL_QUICK : SQL_ENSURE);
/* just for sanity, later. Any internals that may rely on this (including */
/* debugging) will have valid data */
imp_sth->statement = (char *)safemalloc(strlen(cSqlStatistics)+
strlen(XXSAFECHAR(catalog))+
strlen(XXSAFECHAR(schema))+
strlen(XXSAFECHAR(table))+1);
sprintf(imp_sth->statement,
cSqlStatistics, XXSAFECHAR(catalog), XXSAFECHAR(schema),
XXSAFECHAR(table), unique, quick);
rc = SQLStatistics(imp_sth->hstmt,
(catalog && *catalog) ? catalog : 0, SQL_NTS,
(schema && *schema) ? schema : 0, SQL_NTS,
(table && *table) ? table : 0, SQL_NTS,
odbc_unique, odbc_quick);
if (DBIc_TRACE(imp_sth, 0, 0, 4))
PerlIO_printf(
DBIc_LOGPIO(imp_dbh),
"SQLStatistics call: cat = %s, schema = %s, table = %s"
", unique=%d, quick = %d\n",
XXSAFECHAR(catalog), XXSAFECHAR(schema), XXSAFECHAR(table),
odbc_unique, odbc_quick);
dbd_error(sth, rc, "st_statistics/SQLStatistics");
if (!SQL_SUCCEEDED(rc)) {
SQLFreeHandle(SQL_HANDLE_STMT,imp_sth->hstmt);
/* SQLFreeStmt(imp_sth->hstmt, SQL_DROP);*/ /* TBD: 3.0 update */
imp_sth->hstmt = SQL_NULL_HSTMT;
return 0;
}
return build_results(sth, dbh, rc);
}
/************************************************************************/
/* */
/* odbc_st_prepare */
/* =============== */
/* */
/* dbd_st_prepare is the old API which is now replaced with */
/* dbd_st_prepare_sv (taking a perl scalar) so this is now: */
/* */
/* a) just a wrapper around dbd_st_prepare_sv and */
/* b) not used - see ODBC.c */
/* */
/************************************************************************/
int odbc_st_prepare(
SV *sth,
imp_sth_t *imp_sth,
char *statement,
SV *attribs)
{
dTHR;
SV *sql;
sql = sv_newmortal();
sv_setpvn(sql, statement, strlen(statement));
return dbd_st_prepare_sv(sth, imp_sth, sql, attribs);
}
/************************************************************************/
/* */
/* odbc_st_prepare_sv */
/* ================== */
/* */
/* dbd_st_prepare_sv is the newer version of dbd_st_prepare taking a */
/* a perl scalar for the sql statement instead of a char*. */
/* */
/************************************************************************/
int odbc_st_prepare_sv(
SV *sth,
imp_sth_t *imp_sth,
SV *statement,
SV *attribs)
{
dTHR;
D_imp_dbh_from_sth;
RETCODE rc;
int dbh_active;
char *sql;
STRLEN sql_len;
sql = SvPV(statement, sql_len);
imp_sth->done_desc = 0;
imp_sth->henv = imp_dbh->henv; /* needed for dbd_error */
imp_sth->hdbc = imp_dbh->hdbc;
imp_sth->odbc_ignore_named_placeholders =
imp_dbh->odbc_ignore_named_placeholders;
imp_sth->odbc_default_bind_type = imp_dbh->odbc_default_bind_type;
imp_sth->odbc_force_bind_type = imp_dbh->odbc_force_bind_type;
imp_sth->odbc_force_rebind = imp_dbh->odbc_force_rebind;
imp_sth->odbc_query_timeout = imp_dbh->odbc_query_timeout;
imp_sth->odbc_putdata_start = imp_dbh->odbc_putdata_start;
imp_sth->odbc_column_display_size = imp_dbh->odbc_column_display_size;
imp_sth->odbc_utf8_on = imp_dbh->odbc_utf8_on;
if (DBIc_TRACE(imp_dbh, 0, 0, 5))
TRACE1(imp_dbh, " initializing sth query timeout to %ld\n",
(long)imp_dbh->odbc_query_timeout);
if ((dbh_active = check_connection_active(sth)) == 0) return 0;
rc = SQLAllocHandle(SQL_HANDLE_STMT, imp_dbh->hdbc, &imp_sth->hstmt);
if (!SQL_SUCCEEDED(rc)) {
dbd_error(sth, rc, "st_prepare/SQLAllocHandle(stmt)");
return 0;
}
imp_sth->odbc_exec_direct = imp_dbh->odbc_exec_direct;
{
/*
* allow setting of odbc_execdirect in prepare() or overriding
*/
SV **odbc_exec_direct_sv;
/* if the attribute is there, let it override what the default
* value from the dbh is (set above).
* NOTE:
* There are unfortunately two possible attributes because of an early
* typo in DBD::ODBC which we keep for backwards compatibility.
*/
if ((odbc_exec_direct_sv =
DBD_ATTRIB_GET_SVP(attribs, "odbc_execdirect",
(I32)strlen("odbc_execdirect"))) != NULL) {
imp_sth->odbc_exec_direct = SvIV(*odbc_exec_direct_sv) != 0;
}
if ((odbc_exec_direct_sv =
DBD_ATTRIB_GET_SVP(attribs, "odbc_exec_direct",
(I32)strlen("odbc_exec_direct"))) != NULL) {
imp_sth->odbc_exec_direct = SvIV(*odbc_exec_direct_sv) != 0;
}
}
/* scan statement for '?', ':1' and/or ':foo' style placeholders */
dbd_preparse(imp_sth, sql);
/* Hold this statement for subsequent call of dbd_execute */
if (!imp_sth->odbc_exec_direct) {
/* parse the (possibly edited) SQL statement */
if (DBIc_TRACE(imp_dbh, SQL_TRACE_FLAG, 0, 3)) {
TRACE1(imp_dbh, " SQLPrepare %s\n", imp_sth->statement);
}
#ifdef WITH_UNICODE
if (SvOK(statement) && DO_UTF8(statement)) {
SQLWCHAR *wsql;
STRLEN wsql_len;
SV *sql_copy;
if (DBIc_TRACE(imp_dbh, 0x02000000, 0, 0))
TRACE0(imp_dbh, " Processing utf8 sql in unicode mode\n");
sql_copy = sv_newmortal();
sv_setpv(sql_copy, imp_sth->statement);
#ifdef sv_utf8_decode
sv_utf8_decode(sql_copy);
#else
SvUTF8_on(sql_copy);
#endif
SV_toWCHAR(sql_copy);
wsql = (SQLWCHAR *)SvPV(sql_copy, wsql_len);
rc = SQLPrepareW(imp_sth->hstmt,
wsql, wsql_len / sizeof(SQLWCHAR));
} else {
if (DBIc_TRACE(imp_dbh, 0x02000000, 0, 0))
TRACE0(imp_dbh, " Processing non-utf8 sql in unicode mode\n");
rc = SQLPrepare(imp_sth->hstmt, imp_sth->statement, SQL_NTS);
}
#else
if (DBIc_TRACE(imp_dbh, 0x02000000, 0, 0))
TRACE0(imp_dbh, " Processing sql in non-unicode mode\n");
rc = SQLPrepare(imp_sth->hstmt, imp_sth->statement, SQL_NTS);
#endif
if (DBIc_TRACE(imp_dbh, 0, 0, 3))
TRACE1(imp_dbh, " SQLPrepare = %d\n", rc);
if (!SQL_SUCCEEDED(rc)) {
dbd_error(sth, rc, "st_prepare/SQLPrepare");
SQLFreeHandle(SQL_HANDLE_STMT,imp_sth->hstmt);
/* SQLFreeStmt(imp_sth->hstmt, SQL_DROP);*/ /* TBD: 3.0 update */
imp_sth->hstmt = SQL_NULL_HSTMT;
return 0;
}
} else if (DBIc_TRACE(imp_dbh, 0, 0, 3)) {
TRACE1(imp_dbh, " odbc_exec_direct=1, statement (%s) "
"held for later exec\n", imp_sth->statement);
}
/* init sth pointers */
imp_sth->henv = imp_dbh->henv;
imp_sth->hdbc = imp_dbh->hdbc;
imp_sth->fbh = NULL;
imp_sth->ColNames = NULL;
imp_sth->RowBuffer = NULL;
imp_sth->RowCount = -1;
imp_sth->eod = -1;
/*
* If odbc_async_exec is set and odbc_async_type is SQL_AM_STATEMENT,
* we need to set the SQL_ATTR_ASYNC_ENABLE attribute.
*/
if (imp_dbh->odbc_async_exec &&
imp_dbh->odbc_async_type == SQL_AM_STATEMENT){
rc = SQLSetStmtAttr(imp_sth->hstmt,
SQL_ATTR_ASYNC_ENABLE,
(SQLPOINTER) SQL_ASYNC_ENABLE_ON,
SQL_IS_UINTEGER
);
if (!SQL_SUCCEEDED(rc)) {
dbd_error(sth, rc, "st_prepare/SQLSetStmtAttr");
SQLFreeStmt(imp_sth->hstmt, SQL_DROP);
imp_sth->hstmt = SQL_NULL_HSTMT;
return 0;
}
}
/*
* If odbc_query_timeout is set (not -1)
* we need to set the SQL_ATTR_QUERY_TIMEOUT
*/
if (imp_sth->odbc_query_timeout != -1){
odbc_set_query_timeout(sth, imp_sth->hstmt, imp_sth->odbc_query_timeout);
if (!SQL_SUCCEEDED(rc)) {
dbd_error(sth, rc, "set_query_timeout");
}
/* don't fail if the query timeout can't be set. */
}
DBIc_IMPSET_on(imp_sth);
return 1;
}
/* Given SQL type return string description - only used in debug output */
static const char *S_SqlTypeToString (SWORD sqltype)
{
switch(sqltype) {
case SQL_CHAR: return "CHAR";
case SQL_NUMERIC: return "NUMERIC";
case SQL_DECIMAL: return "DECIMAL";
case SQL_INTEGER: return "INTEGER";
case SQL_SMALLINT: return "SMALLINT";
case SQL_FLOAT: return "FLOAT";
case SQL_REAL: return "REAL";
case SQL_DOUBLE: return "DOUBLE";
case SQL_VARCHAR: return "VARCHAR";
#ifdef SQL_WCHAR
case SQL_WCHAR: return "UNICODE CHAR";
#endif
#ifdef SQL_WVARCHAR
/* added for SQLServer 7 ntext type 2/24/2000 */
case SQL_WVARCHAR: return "UNICODE VARCHAR";
#endif
#ifdef SQL_WLONGVARCHAR
case SQL_WLONGVARCHAR: return "UNICODE LONG VARCHAR";
#endif
case SQL_DATE: return "DATE";
case SQL_TYPE_DATE: return "DATE";
case SQL_TIME: return "TIME";
case SQL_TYPE_TIME: return "TIME";
case SQL_TIMESTAMP: return "TIMESTAMP";
case SQL_TYPE_TIMESTAMP: return "TIMESTAMP";
case SQL_LONGVARCHAR: return "LONG VARCHAR";
case SQL_BINARY: return "BINARY";
case SQL_VARBINARY: return "VARBINARY";
case SQL_LONGVARBINARY: return "LONG VARBINARY";
case SQL_BIGINT: return "BIGINT";
case SQL_TINYINT: return "TINYINT";
case SQL_BIT: return "BIT";
}
return "unknown";
}
static const char *S_SqlCTypeToString (SWORD sqltype)
{
static char s_buf[100];
#define s_c(x) case x: return #x
switch(sqltype) {
s_c(SQL_C_CHAR);
s_c(SQL_C_WCHAR);
s_c(SQL_C_BIT);
s_c(SQL_C_STINYINT);
s_c(SQL_C_UTINYINT);
s_c(SQL_C_SSHORT);
s_c(SQL_C_USHORT);
s_c(SQL_C_FLOAT);
s_c(SQL_C_DOUBLE);
s_c(SQL_C_BINARY);
s_c(SQL_C_DATE);
s_c(SQL_C_TIME);
s_c(SQL_C_TIMESTAMP);
s_c(SQL_C_TYPE_DATE);
s_c(SQL_C_TYPE_TIME);
s_c(SQL_C_TYPE_TIMESTAMP);
}
#undef s_c
sprintf(s_buf, "(CType %d)", sqltype);
return s_buf;
}
/*
* describes the output variables of a query,
* allocates buffers for result rows,
* and binds this buffers to the statement.
*/
int dbd_describe(SV *h, imp_sth_t *imp_sth, int more)
{
dTHR;
SQLRETURN rc; /* ODBC fn return value */
UCHAR *rbuf_ptr;
SQLSMALLINT i;
imp_fbh_t *fbh;
SQLLEN t_dbsize = 0; /* size of native type */
SQLSMALLINT num_fields;
SQLCHAR *cur_col_name;
struct imp_dbh_st *imp_dbh = NULL;
imp_dbh = (struct imp_dbh_st *)(DBIc_PARENT_COM(imp_sth));
if (DBIc_TRACE(imp_sth, 0, 0, 4))
TRACE1(imp_sth, " dbd_describe done_desc=%d\n", imp_sth->done_desc);
if (imp_sth->done_desc)
return 1; /* success, already done it */
rc = SQLNumResultCols(imp_sth->hstmt, &num_fields);
if (!SQL_SUCCEEDED(rc)) {
dbd_error(h, rc, "dbd_describe/SQLNumResultCols");
return 0;
} else if (DBIc_TRACE(imp_sth, 0, 0, 4))
TRACE1(imp_sth, " dbd_describe SQLNumResultCols=0 (columns=%d)\n",
num_fields);
/*
* A little extra check to see if SQLMoreResults is supported
* before trying to call it. This is to work around some strange
* behavior with SQLServer's driver and stored procedures which
* insert data.
*/
imp_sth->done_desc = 1; /* assume ok from here on */
if (!more) {
while (num_fields == 0 &&
imp_dbh->odbc_sqlmoreresults_supported == 1) {
rc = SQLMoreResults(imp_sth->hstmt);
if (DBIc_TRACE(imp_sth, 0, 0, 8))
TRACE1(imp_sth,
" Numfields = 0, SQLMoreResults = %d\n", rc);
if (rc == SQL_SUCCESS_WITH_INFO) {
dbd_error(h, rc, "dbd_describe/SQLMoreResults");
/* AllODBCErrors(imp_sth->henv, imp_sth->hdbc, imp_sth->hstmt, */
/* DBIc_TRACE(imp_sth, 0, 0, 4), */
/* DBIc_LOGPIO(imp_dbh)); */
}
if (rc == SQL_NO_DATA) {
imp_sth->moreResults = 0;
break;
} else if (!SQL_SUCCEEDED(rc)) {
break;
}
/* reset describe flags, so that we re-describe */
imp_sth->done_desc = 0;
/* force future executes to rebind automatically */
imp_sth->odbc_force_rebind = 1;
rc = SQLNumResultCols(imp_sth->hstmt, &num_fields);
if (!SQL_SUCCEEDED(rc)) {
dbd_error(h, rc, "dbd_describe/SQLNumResultCols");
return 0;
} else if (DBIc_TRACE(imp_sth, 0, 0, 8)) {
TRACE1(imp_dbh,
" num fields after MoreResults = %d\n",
num_fields);
}
} /* end of SQLMoreResults */
} /* end of more */
DBIc_NUM_FIELDS(imp_sth) = num_fields;
if (num_fields == 0) {
if (DBIc_TRACE(imp_sth, 0, 0, 4))
TRACE0(imp_dbh, " dbd_describe skipped (no result cols)\n");
imp_sth->done_desc = 1;
return 1;
}
/* allocate field buffers */
Newz(42, imp_sth->fbh, num_fields, imp_fbh_t);
/* the +255 below instead is due to an old comment in this code before
this change claiming foxpro wrote off end of memory
(and so 255 bytes were added - kept here without evidence) */
Newz(42, imp_sth->ColNames,
(num_fields + 1) * imp_dbh->max_column_name_len + 255, UCHAR);
cur_col_name = imp_sth->ColNames;
/* Pass 1: Get space needed for field names, display buffer and dbuf */
for (fbh=imp_sth->fbh, i=0; i < num_fields; i++, fbh++) {
fbh->imp_sth = imp_sth;
#ifdef WITH_UNICODE
rc = SQLDescribeColW(imp_sth->hstmt,
(SQLSMALLINT)(i+1),
(SQLWCHAR *) cur_col_name,
(SQLSMALLINT)imp_dbh->max_column_name_len,
&fbh->ColNameLen,
&fbh->ColSqlType,
&fbh->ColDef,
&fbh->ColScale,
&fbh->ColNullable);
/*
printf("strlen=%d, collen=%d\n", strlen(cur_col_name), fbh->ColNameLen);
{
int i;
SQLWCHAR *wp = (SQLWCHAR *)cur_col_name;
printf("SQLDescribeCol = ");
for (i = 0; i < fbh->ColNameLen; i++) {
printf("%ld, ", wp[i]);
}
printf("\n");
}
*/
#else /* WITH_UNICODE */
rc = SQLDescribeCol(imp_sth->hstmt,
(SQLSMALLINT)(i+1),
cur_col_name,
(SQLSMALLINT)imp_dbh->max_column_name_len,
&fbh->ColNameLen,
&fbh->ColSqlType,
/* column size or precision depending on type */
&fbh->ColDef,
&fbh->ColScale, /* decimal digits */
&fbh->ColNullable);
#endif /* WITH_UNICODE */
if (!SQL_SUCCEEDED(rc)) { /* should never fail */
dbd_error(h, rc, "describe/SQLDescribeCol");
break;
}
fbh->ColName = cur_col_name;
#ifdef WITH_UNICODE
cur_col_name += fbh->ColNameLen * sizeof(SQLWCHAR);
#else
cur_col_name += fbh->ColNameLen + 1;
cur_col_name[fbh->ColNameLen] = '\0'; /* should not be necessary */
#endif
#ifdef SQL_COLUMN_DISPLAY_SIZE
if (DBIc_TRACE(imp_sth, 0, 0, 8))
PerlIO_printf(DBIc_LOGPIO(imp_dbh),
" DescribeCol column = %d, name = %s, "
"namelen = %d, type = %s(%d), "
"precision/column size = %ld, scale = %d, "
"nullable = %d\n",
i+1, fbh->ColName,
fbh->ColNameLen,
S_SqlTypeToString(fbh->ColSqlType),
fbh->ColSqlType,
fbh->ColDef, fbh->ColScale, fbh->ColNullable);
rc = SQLColAttributes(imp_sth->hstmt,
(SQLSMALLINT)(i+1),SQL_COLUMN_DISPLAY_SIZE,
NULL, 0, NULL ,
&fbh->ColDisplaySize);/* TBD: 3.0 update */
if (!SQL_SUCCEEDED(rc)) {
/* Some ODBC drivers don't support SQL_COLUMN_DISPLAY_SIZE on
some result-sets. e.g., The "Infor Integration ODBC driver"
cannot handle SQL_COLUMN_DISPLAY_SIZE and SQL_COLUMN_LENGTH
for SQLTables and SQLColumns calls. We used to fail here but
there is a prescident not to as this code is already in an
ifdef for drivers that do not define SQL_COLUMN_DISPLAY_SIZE.
Since just about everyone will be using an ODBC driver manager
now it is unlikely these attributes will not be defined so we
default if the call fails now */
if( DBIc_TRACE(imp_sth, 0, 0, 8) ) {
TRACE0(imp_sth,
" describe/SQLColAttributes/SQL_COLUMN_DISPLAY_SIZE "
"not supported, will be equal to SQL_COLUMN_LENGTH\n");
}
/* ColDisplaySize will be made equal to ColLength */
fbh->ColDisplaySize = 0;
rc = SQL_SUCCESS;
} else if (DBIc_TRACE(imp_sth, 0, 0, 8)) {
TRACE1(imp_sth, " display size = %ld\n",
(long)fbh->ColDisplaySize);
}
/* TBD: should we only add a terminator if it's a char??? */
fbh->ColDisplaySize += 1; /* add terminator */
#else
fbh->ColDisplaySize = imp_sth->odbc_column_display_size;
#endif
#ifdef SQL_COLUMN_LENGTH
rc = SQLColAttributes(imp_sth->hstmt,(SQLSMALLINT)(i+1),
SQL_COLUMN_LENGTH,
NULL, 0, NULL ,&fbh->ColLength);
if (!SQL_SUCCEEDED(rc)) {
/* See comment above under SQL_COLUMN_DISPLAY_SIZE */
fbh->ColLength = imp_sth->odbc_column_display_size;
if( DBIc_TRACE(imp_sth, 0, 0, 8) ) {
TRACE1(imp_sth,
" describe/SQLColAttributes/SQL_COLUMN_LENGTH not "
"supported, fallback on %ld\n", (long)fbh->ColLength);
}
rc = SQL_SUCCESS;
} else if (DBIc_TRACE(imp_sth, 0, 0, 8)) {
TRACE1(imp_sth, " column length = %ld\n",
(long)fbh->ColLength);
}
# if defined(WITH_UNICODE)
fbh->ColLength += 1; /* add extra byte for double nul terminator */
# endif
#else
fbh->ColLength = imp_sth->odbc_column_display_size;
#endif
/* may want to ensure Display Size at least as large as column
* length -- workaround for some drivers which report a shorter
* display length
* */
fbh->ColDisplaySize =
fbh->ColDisplaySize > fbh->ColLength ?
fbh->ColDisplaySize : fbh->ColLength;
/*
* change fetched size, decimal digits etc for some types,
* The tests for ColDef = 0 are for when the driver does not give
* us a length for the column e.g., "max" column types in SQL Server
* like varbinary(max).
*/
fbh->ftype = SQL_C_CHAR;
switch(fbh->ColSqlType)
{
case SQL_VARBINARY:
case SQL_BINARY:
fbh->ftype = SQL_C_BINARY;
if (fbh->ColDef == 0) { /* cope with varbinary(max) */
fbh->ColDisplaySize = DBIc_LongReadLen(imp_sth);
}
break;
#if defined(WITH_UNICODE)
case SQL_WCHAR:
case SQL_WVARCHAR:
fbh->ftype = SQL_C_WCHAR;
/* MS SQL returns bytes, Oracle returns characters ... */
if (fbh->ColDef == 0) { /* cope with nvarchar(max) */
fbh->ColDisplaySize = DBIc_LongReadLen(imp_sth);
fbh->ColLength = DBIc_LongReadLen(imp_sth);
} else if (fbh->ColDef > 2147483590) {
/*
* The new MS Access driver ACEODBC.DLL cannot cope with the
* 40UnicodeRoundTrip test which contains a
* select ?, LEN(?)
* returning a massive number for the column display size of
* the first column. This leads to a memory allocation error
* unless we trap it as a large column.
*/
fbh->ColDisplaySize = DBIc_LongReadLen(imp_sth);
fbh->ColLength = DBIc_LongReadLen(imp_sth);
}
fbh->ColDisplaySize*=sizeof(SQLWCHAR);
fbh->ColLength*=sizeof(SQLWCHAR);
break;
#else
# if defined(SQL_WCHAR)
case SQL_WCHAR:
if (fbh->ColDef == 0) {
fbh->ColDisplaySize = DBIc_LongReadLen(imp_sth);
}
break;
# endif
# if defined(SQL_WVARCHAR)
case SQL_WVARCHAR:
if (fbh->ColDef == 0) {
fbh->ColDisplaySize = DBIc_LongReadLen(imp_sth);
}
break;
# endif
#endif /* WITH_UNICODE */
case SQL_LONGVARBINARY:
fbh->ftype = SQL_C_BINARY;
fbh->ColDisplaySize = DBIc_LongReadLen(imp_sth);
break;
#ifdef SQL_WLONGVARCHAR
case SQL_WLONGVARCHAR: /* added for SQLServer 7 ntext type */
# if defined(WITH_UNICODE)
fbh->ftype = SQL_C_WCHAR;
/* MS SQL returns bytes, Oracle returns characters ... */
fbh->ColLength*=sizeof(SQLWCHAR);
fbh->ColDisplaySize = DBIc_LongReadLen(imp_sth)+1;
# else
fbh->ColDisplaySize = DBIc_LongReadLen(imp_sth) + 1;
# endif /* WITH_UNICODE */
break;
#endif /* SQL_WLONGVARCHAR */
case SQL_VARCHAR:
if (fbh->ColDef == 0) {
fbh->ColDisplaySize = DBIc_LongReadLen(imp_sth)+1;
}
break;
case SQL_LONGVARCHAR:
fbh->ColDisplaySize = DBIc_LongReadLen(imp_sth)+1;
break;
#ifdef TIMESTAMP_STRUCT /* XXX! */
case SQL_TIMESTAMP:
case SQL_TYPE_TIMESTAMP:
fbh->ftype = SQL_C_TIMESTAMP;
fbh->ColDisplaySize = sizeof(TIMESTAMP_STRUCT);
break;
#endif
}
/* make sure alignment is accounted for on all types, including
* chars */
#if 0
if (fbh->ftype != SQL_C_CHAR) {
t_dbsize += t_dbsize % sizeof(int); /* alignment (JLU incorrect!) */
}
#endif
t_dbsize += fbh->ColDisplaySize;
/* alignment -- always pad so the next column is aligned on a
word boundary */
t_dbsize += (sizeof(int) - (t_dbsize % sizeof(int))) % sizeof(int);
if (DBIc_TRACE(imp_sth, 0, 0, 4))
PerlIO_printf(DBIc_LOGPIO(imp_dbh),
" now using col %d: type = %s (%d), len = %ld, "
"display size = %ld, prec = %ld, scale = %lu\n",
i+1, S_SqlTypeToString(fbh->ColSqlType),
fbh->ColSqlType,
(long)fbh->ColLength, (long)fbh->ColDisplaySize,
(long)fbh->ColDef, (unsigned long)fbh->ColScale);
}
if (!SQL_SUCCEEDED(rc)) {
/* dbd_error called above */
Safefree(imp_sth->fbh);
return 0;
}
/* allocate Row memory */
Newz(42, imp_sth->RowBuffer, t_dbsize + num_fields, UCHAR);
/* Second pass:
- get column names
- bind column output
*/
rbuf_ptr = imp_sth->RowBuffer;
for(i=0, fbh = imp_sth->fbh;
i < num_fields && SQL_SUCCEEDED(rc); i++, fbh++)
{
/* not sure I need this anymore, since we are trying to align
* the columns anyway
* */
switch(fbh->ftype)
{
case SQL_C_BINARY:
case SQL_C_TIMESTAMP:
case SQL_C_TYPE_TIMESTAMP:
/* make sure pointer is on word boundary for Solaris */
rbuf_ptr += (sizeof(int) -
((rbuf_ptr - imp_sth->RowBuffer) % sizeof(int))) %
sizeof(int);
break;
}
fbh->data = rbuf_ptr;
rbuf_ptr += fbh->ColDisplaySize;
/* alignment -- always pad so the next column is aligned on a word
boundary */
rbuf_ptr += (sizeof(int) - ((rbuf_ptr - imp_sth->RowBuffer) %
sizeof(int))) % sizeof(int);
/* Bind output column variables */
if (DBIc_TRACE(imp_sth, 0, 0, 4))
PerlIO_printf(DBIc_LOGPIO(imp_dbh),
" Bind %d: type = %s(%d), buf=%p, buflen=%ld\n",
i+1, S_SqlTypeToString(fbh->ftype), fbh->ftype,
fbh->data, fbh->ColDisplaySize);
rc = SQLBindCol(imp_sth->hstmt,
(SQLSMALLINT)(i+1),
fbh->ftype, fbh->data,
fbh->ColDisplaySize, &fbh->datalen);
if (!SQL_SUCCEEDED(rc)) {
dbd_error(h, rc, "describe/SQLBindCol");
break;
}
} /* end pass 2 */
if (!SQL_SUCCEEDED(rc)) {
/* dbd_error called above */
Safefree(imp_sth->fbh);
return 0;
}
return 1;
}
/*======================================================================*/
/* */
/* dbd_st_execute */
/* ============== */
/* */
/* returns: */
/* -2 - error */
/* >=0 - ok, row count */
/* -1 - unknown count */
/* */
/*======================================================================*/
int dbd_st_execute(
SV *sth, imp_sth_t *imp_sth)
{
dTHR;
RETCODE rc;
D_imp_dbh_from_sth;
int outparams = 0;
int ret;
if (DBIc_TRACE(imp_sth, 0, 0, 3))
TRACE1(imp_dbh, "+dbd_st_execute(%p)\n", sth);
/*
* if the handle is active, we need to finish it here.
* Note that dbd_st_finish already checks to see if it's active.
*/
dbd_st_finish(sth, imp_sth);;
/*
* bind_param_inout support
*/
outparams = (imp_sth->out_params_av) ? AvFILL(imp_sth->out_params_av)+1 : 0;
if (DBIc_TRACE(imp_sth, 0, 0, 4)) {
TRACE1(imp_dbh, " outparams = %d\n", outparams);
}
if (imp_dbh->odbc_defer_binding) {
rc = SQLFreeStmt(imp_sth->hstmt, SQL_RESET_PARAMS);
/* check bind input parameters */
if (imp_sth->all_params_hv) {
HV *hv = imp_sth->all_params_hv;
SV *sv;
char *key;
I32 retlen;
hv_iterinit(hv);
while( (sv = hv_iternextsv(hv, &key, &retlen)) != NULL ) {
if (sv != &PL_sv_undef) {
phs_t *phs = (phs_t*)(void*)SvPVX(sv);
if (!rebind_param(sth, imp_sth, phs)) return -2;
if (DBIc_TRACE(imp_sth, 0, 0, 8)) {
if (SvOK(phs->sv) && (phs->value_type == SQL_C_CHAR)) {
char sbuf[256];
unsigned int i = 0;
while((phs->sv_buf[i] != 0) && (i < (sizeof(sbuf) - 6))) {
sbuf[i] = phs->sv_buf[i];
i++;
}
strcpy(&sbuf[i], "...");
TRACE2(imp_dbh,
" rebind check char Param %d (%s)\n",
phs->idx, sbuf);
}
}
}
}
}
}
if (outparams) { /* check validity of bind_param_inout SV's */
int i = outparams;
while(--i >= 0) {
phs_t *phs = (phs_t*)(void*)SvPVX(AvARRAY(imp_sth->out_params_av)[i]);
/* Make sure we have the value in string format. Typically a number */
/* will be converted back into a string using the same bound buffer */
/* so the sv_buf test below will not trip. */
/* mutation check */
if (SvTYPE(phs->sv) != phs->sv_type /* has the type changed? */
|| (SvOK(phs->sv) && !SvPOK(phs->sv)) /* is there still a string? */
|| SvPVX(phs->sv) != phs->sv_buf /* has the string buffer moved? */
) {
if (!rebind_param(sth, imp_sth, phs))
croak("Can't rebind placeholder %s", phs->name);
} else {
/* no mutation found */
}
}
}
if (imp_sth->odbc_exec_direct) {
/* statement ready for SQLExecDirect */
if (DBIc_TRACE(imp_sth, 0, 0, 5)) {
TRACE0(imp_dbh,
" odbc_exec_direct=1, using SQLExecDirect\n");
}
rc = SQLExecDirect(imp_sth->hstmt, imp_sth->statement, SQL_NTS);
} else {
rc = SQLExecute(imp_sth->hstmt);
}
if (DBIc_TRACE(imp_sth, 0, 0, 8))
TRACE2(imp_dbh, " SQLExecute/SQLExecDirect(%p)=%d\n",
imp_sth->hstmt, rc);
/*
* If asynchronous execution has been enabled, SQLExecute will
* return SQL_STILL_EXECUTING until it has finished.
* Grab whatever messages occur during execution...
*/
while (rc == SQL_STILL_EXECUTING){
dbd_error(sth, rc, "st_execute/SQLExecute");
/*
* Wait a second so we don't loop too fast and bring the machine
* to its knees
*/
if (DBIc_TRACE(imp_sth, 0, 0, 5))
TRACE1(imp_dbh, " SQLExecute(%p) still executing", imp_sth->hstmt);
sleep(1);
rc = SQLExecute(imp_sth->hstmt);
}
/* patches to handle blobs better, via Jochen Wiedmann */
while (rc == SQL_NEED_DATA) {
phs_t* phs;
STRLEN len;
UCHAR* ptr;
if (DBIc_TRACE(imp_sth, 0, 0, 5))
TRACE1(imp_dbh, " NEED DATA\n", imp_sth->hstmt);
while ((rc = SQLParamData(imp_sth->hstmt, (PTR*) &phs)) ==
SQL_STILL_EXECUTING) {
if (DBIc_TRACE(imp_sth, 0, 0, 5))
TRACE1(imp_dbh, " SQLParamData(%p) still executing",
imp_sth->hstmt);
/*
* wait a while to avoid looping too fast waiting for SQLParamData
* to complete.
*/
sleep(1);
}
if (rc != SQL_NEED_DATA) {
break;
}
/* phs->sv is already upgraded to a PV in rebind_param.
* It is not NULL, because we otherwise won't be called here
* (value_len = 0).
*/
ptr = SvPV(phs->sv, len);
rc = SQLPutData(imp_sth->hstmt, ptr, len);
if (!SQL_SUCCEEDED(rc)) {
break;
}
rc = SQL_NEED_DATA; /* So the loop continues ... */
}
/*
* Call dbd_error regardless of the value of rc so we can
* get any status messages that are desired.
*/
dbd_error(sth, rc, "st_execute/SQLExecute");
if (!SQL_SUCCEEDED(rc) && rc != SQL_NO_DATA) {
/* dbd_error(sth, rc, "st_execute/SQLExecute"); */
if (DBIc_TRACE(imp_sth, 0, 0, 3))
TRACE1(imp_dbh, "-dbd_st_execute(%p)=-2\n", sth);
return -2;
}
if (rc != SQL_NO_DATA) {
/* SWORD num_fields; */
RETCODE rc2;
rc2 = SQLRowCount(imp_sth->hstmt, &imp_sth->RowCount);
if (DBIc_TRACE(imp_sth, 0, 0, 7))
TRACE2(imp_dbh, " SQLRowCount=%d (rows=%ld)\n",
rc2,
(long)(SQL_SUCCEEDED(rc2) ? imp_sth->RowCount : (SQLLEN)-1));
if (!SQL_SUCCEEDED(rc2)) {
dbd_error(sth, rc2, "st_execute/SQLRowCount"); /* XXX ? */
imp_sth->RowCount = -1;
}
/* sanity check for strange circumstances and multiple types of
* result sets. Crazy that it can happen, but it can with
* multiple result sets and stored procedures which return
* result sets.
* This seems to slow things down a bit and is rarely needed.
*
* This can happen in Sql Server in strange cases where stored
* procs have multiple result sets. Sometimes, if there is an
* select then an insert, etc. Maybe this should be a special
* attribute to force a re-describe after every execute? */
if (imp_sth->odbc_force_rebind) {
/* force calling dbd_describe after each execute */
odbc_clear_result_set(sth, imp_sth);
}
} else {
/* SQL_NO_DATA returned, must have no rows :) */
/* seem to need to reset the done_desc, but not sure if this is
* what we want yet */
if (DBIc_TRACE(imp_sth, 0, 0, 7))
TRACE0(imp_dbh,
" SQL_NO_DATA...resetting done_desc!\n");
imp_sth->done_desc = 0;
imp_sth->RowCount = 0;
}
/*
* MS SQL Server is very picky wrt to completing a procedure i.e.,
* it says the output bound parameters are not available until the procedure
* is complete and the procedure is not complete until you have called
* SQLMoreResults and it has returned SQL_NO_DATA. So, if you call a
* procedure multiple times in the same statement (e.g., by just calling
* execute) DBD::ODBC will call dbd_describe to describe the first
* execute, discover there is no result-set and call SQLMoreResults - ok,
* but after that, the dbd_describe is done and SQLMoreResults will not
* get called. The following is a kludge to get around this until
* a) DBD::ODBC can be changed to stop skipping over non-result-set
* generating statements and b) the SQLMoreResults calls move out of
* dbd_describe.
*/
{
SQLSMALLINT flds = 0;
SQLRETURN sts;
sts = SQLNumResultCols(imp_sth->hstmt, &flds);
if (flds == 0) { /* not a result-set */
if (DBIc_TRACE(imp_sth, 0, 0, 4))
TRACE2(imp_dbh, " nflds=(%d,%d), resetting done_desc\n",
flds, DBIc_NUM_FIELDS(imp_sth));
imp_sth->done_desc = 0;
}
}
if (!imp_sth->done_desc) {
/* This needs to be done after SQLExecute for some drivers! */
/* Especially for order by and join queries. */
/* See Microsoft Knowledge Base article (#Q124899) */
/* describe and allocate storage for results (if any needed) */
if (!dbd_describe(sth, imp_sth, 0)) {
if (DBIc_TRACE(imp_sth, 0, 0, 3)) {
TRACE0(imp_sth,
" !!dbd_describe failed, dbd_st_execute #1...!\n");
}
if (DBIc_TRACE(imp_sth, 0, 0, 3))
TRACE1(imp_dbh, "-dbd_st_execute(%p)=-2\n", sth);
return -2; /* dbd_describe already called dbd_error() */
}
}
if (DBIc_NUM_FIELDS(imp_sth) > 0) {
DBIc_ACTIVE_on(imp_sth); /* only set for select (?) */
if (DBIc_TRACE(imp_sth, 0, 0, 4)) {
TRACE1(imp_sth, " have %d fields\n",
DBIc_NUM_FIELDS(imp_sth));
}
} else {
if (DBIc_TRACE(imp_sth, 0, 0, 4)) {
TRACE0(imp_dbh, " got no rows: resetting ACTIVE, moreResults\n");
}
imp_sth->moreResults = 0;
/* flag that we've done the describe to avoid a problem
* where calling describe after execute returned no rows
* caused SQLServer to provide a description of a query
* that didn't quite apply. */
/* imp_sth->done_desc = 1; */
DBIc_ACTIVE_off(imp_sth);
}
imp_sth->eod = SQL_SUCCESS;
if (outparams) { /* check validity of bound output SV's */
odbc_handle_outparams(imp_sth, DBIc_TRACE_LEVEL(imp_sth));
}
/*
* JLU: Jon Smirl had:
* return (imp_sth->RowCount == -1 ? -1 : abs(imp_sth->RowCount));
* why? Why do you need the abs() of the rowcount? Special reason?
* The e-mail that accompanied the change indicated that Sybase would return
* a negative value for an estimate. Wouldn't you WANT that to stay
* negative?
*
* dgood: JLU had:
* return imp_sth->RowCount;
* Because you return -2 on errors so if you don't abs() it, a perfectly
* valid return value will get flagged as an error...
*/
ret = (imp_sth->RowCount == -1 ? -1 : abs(imp_sth->RowCount));
if (DBIc_TRACE(imp_sth, 0, 0, 3))
TRACE2(imp_dbh, "-dbd_st_execute(%p)=%d\n", sth, ret);
return ret;
/* return imp_sth->RowCount; */
}
/*----------------------------------------
* running $sth->fetch()
*----------------------------------------
*/
AV *dbd_st_fetch(SV *sth, imp_sth_t *imp_sth)
{
dTHR;
D_imp_dbh_from_sth;
int i;
AV *av;
RETCODE rc;
int num_fields;
#ifdef TIMESTAMP_STRUCT /* iODBC doesn't define this */
char cvbuf[512];
#endif
int ChopBlanks;
/* 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) ) {
dbd_error(sth, SQL_ERROR, "no select statement currently executing");
return Nullav;
}
rc = SQLFetch(imp_sth->hstmt);
if (DBIc_TRACE(imp_sth, 0, 0, 4))
TRACE1(imp_dbh, " SQLFetch rc %d\n", rc);
imp_sth->eod = rc;
if (!SQL_SUCCEEDED(rc)) {
if (SQL_NO_DATA_FOUND == rc) {
if (imp_dbh->odbc_sqlmoreresults_supported == 1) {
rc = SQLMoreResults(imp_sth->hstmt);
/* Check for multiple results */
if (DBIc_TRACE(imp_sth, 0, 0, 6))
TRACE1(imp_dbh, " Getting more results: %d\n", rc);
if (rc == SQL_SUCCESS_WITH_INFO) {
dbd_error(sth, rc, "st_fetch/SQLMoreResults");
/* imp_sth->moreResults = 0; */
}
if (SQL_SUCCEEDED(rc)){
/* More results detected. Clear out the old result */
/* stuff and re-describe the fields. */
if (DBIc_TRACE(imp_sth, 0, 0, 3)) {
TRACE0(imp_dbh, " MORE Results!\n");
}
odbc_clear_result_set(sth, imp_sth);
/* force future executes to rebind automatically */
imp_sth->odbc_force_rebind = 1;
/* tell the odbc driver that we need to unbind the
* bound columns. Fix bug for 0.35 (2/8/02) */
rc = SQLFreeStmt(imp_sth->hstmt, SQL_UNBIND);
if (!SQL_SUCCEEDED(rc)) {
AllODBCErrors(imp_dbh->henv, imp_dbh->hdbc, 0,
DBIc_TRACE(imp_sth, 0, 0, 3),
DBIc_LOGPIO(imp_dbh));
}
if (!dbd_describe(sth, imp_sth, 1)) {
if (DBIc_TRACE(imp_sth, 0, 0, 3))
TRACE0(imp_dbh,
" !!MORE Results dbd_describe failed...!\n");
return Nullav; /* dbd_describe already called dbd_error() */
}
if (DBIc_TRACE(imp_sth, 0, 0, 4)) {
TRACE0(imp_dbh,
" MORE Results dbd_describe success...!\n");
}
/* set moreResults so we'll know we can keep fetching */
imp_sth->moreResults = 1;
imp_sth->done_desc = 0;
return Nullav;
}
else if (rc == SQL_NO_DATA_FOUND || rc == SQL_NO_DATA ||
rc == SQL_SUCCESS_WITH_INFO){
/* No more results */
/* need to check output params here... */
int outparams = (imp_sth->out_params_av) ?
AvFILL(imp_sth->out_params_av)+1 : 0;
if (DBIc_TRACE(imp_sth, 0, 0, 6)) {
TRACE1(imp_sth, " No more results -- outparams = %d\n",
outparams);
}
imp_sth->moreResults = 0;
imp_sth->done_desc = 1;
if (outparams) {
odbc_handle_outparams(imp_sth, DBIc_TRACE_LEVEL(imp_sth));
}
/* XXX need to 'finish' here */
dbd_st_finish(sth, imp_sth);
return Nullav;
}
else {
dbd_error(sth, rc, "st_fetch/SQLMoreResults");
}
}
else {
/*
* SQLMoreResults not supported, just finish.
* per bug found by Jarkko Hyty [hyoty@medialab.sonera.fi]
* No more results
*/
imp_sth->moreResults = 0;
/* XXX need to 'finish' here */
dbd_st_finish(sth, imp_sth);
return Nullav;
}
} else {
dbd_error(sth, rc, "st_fetch/SQLFetch");
/* XXX need to 'finish' here */
dbd_st_finish(sth, imp_sth);
return Nullav;
}
}
if (imp_sth->RowCount == -1)
imp_sth->RowCount = 0;
imp_sth->RowCount++;
av = DBIc_DBISTATE(imp_sth)->get_fbav(imp_sth);
num_fields = AvFILL(av)+1;
if (DBIc_TRACE(imp_sth, 0, 0, 4))
TRACE1(imp_dbh, " fetch num_fields=%d\n", num_fields);
ChopBlanks = DBIc_has(imp_sth, DBIcf_ChopBlanks);
for(i=0; i < num_fields; ++i) {
imp_fbh_t *fbh = &imp_sth->fbh[i];
SV *sv = AvARRAY(av)[i]; /* Note: we (re)use the SV in the AV */
if (DBIc_TRACE(imp_sth, 0, 0, 4))
PerlIO_printf(
DBIc_LOGPIO(imp_dbh),
" fetch col#%d %s datalen=%ld displ=%lu\n",
i+1, fbh->ColName, (long)fbh->datalen,
(unsigned long)fbh->ColDisplaySize);
if (fbh->datalen == SQL_NULL_DATA) { /* NULL value */
SvOK_off(sv);
continue;
}
if (fbh->datalen > fbh->ColDisplaySize || fbh->datalen < 0) {
/* truncated LONG ??? DBIcf_LongTruncOk() */
/* DBIcf_LongTruncOk this should only apply to LONG type fields */
/* truncation of other fields should always be an error since it's */
/* a sign of an internal error */
if (!DBIc_has(imp_sth, DBIcf_LongTruncOk)
/* && rc == SQL_SUCCESS_WITH_INFO */) {
/*
* Since we've detected the problem locally via the datalen,
* we don't need to worry about the value of rc.
*
* This used to make sure rc was set to SQL_SUCCESS_WITH_INFO
* but since it's an error and not SUCCESS, call dbd_error()
* with SQL_ERROR explicitly instead.
*/
dbd_error(
sth, SQL_ERROR,
"st_fetch/SQLFetch (long truncated DBI attribute LongTruncOk "
"not set and/or LongReadLen too small)");
return Nullav;
}
/* LongTruncOk true, just ensure perl has the right length
* for the truncated data.
*/
sv_setpvn(sv, (char*)fbh->data, fbh->ColDisplaySize);
}
else switch(fbh->ftype) {
#ifdef TIMESTAMP_STRUCT /* iODBC doesn't define this */
TIMESTAMP_STRUCT *ts;
case SQL_C_TIMESTAMP:
case SQL_C_TYPE_TIMESTAMP:
ts = (TIMESTAMP_STRUCT *)fbh->data;
sprintf(cvbuf, "%04d-%02d-%02d %02d:%02d:%02d",
ts->year, ts->month, ts->day,
ts->hour, ts->minute, ts->second, ts->fraction);
sv_setpv(sv, cvbuf);
break;
#endif
#if defined(WITH_UNICODE)
case SQL_C_WCHAR:
if (ChopBlanks && fbh->ColSqlType == SQL_WCHAR && fbh->datalen > 0)
{
SQLWCHAR *p = (SQLWCHAR*)fbh->data;
while(fbh->datalen && p[fbh->datalen-1]==L' ') {
--fbh->datalen;
}
}
sv_setwvn(sv,(SQLWCHAR*)fbh->data,fbh->datalen/sizeof(SQLWCHAR));
break;
#endif /* WITH_UNICODE */
default:
if (ChopBlanks && fbh->ColSqlType == SQL_CHAR && fbh->datalen > 0)
{
char *p = (char*)fbh->data;
while(fbh->datalen && p[fbh->datalen - 1]==' ')
--fbh->datalen;
}
sv_setpvn(sv, (char*)fbh->data, fbh->datalen);
if (imp_sth->odbc_utf8_on && fbh->ftype != SQL_C_BINARY ) {
#ifdef sv_utf8_decode
sv_utf8_decode(sv);
#else
SvUTF8_on(sv);
#endif
}
}
}
return av;
}
int dbd_st_rows(SV *sth, imp_sth_t *imp_sth)
{
return (int)imp_sth->RowCount;
}
int dbd_st_finish(SV *sth, imp_sth_t *imp_sth)
{
dTHR;
D_imp_dbh_from_sth;
RETCODE rc;
if (DBIc_TRACE(imp_sth, 0, 0, 3))
TRACE1(imp_sth, " dbd_st_finish(%p)\n", sth);
/* Cancel further fetches from this cursor. */
/* We don't close the cursor till DESTROY (dbd_st_destroy). */
/* The application may re execute(...) it. */
/* XXX semantics of finish (eg oracle vs odbc) need lots more thought */
/* re-read latest DBI specs and ODBC manuals */
if (DBIc_ACTIVE(imp_sth) && imp_dbh->hdbc != SQL_NULL_HDBC) {
rc = SQLFreeStmt(imp_sth->hstmt, SQL_CLOSE);/* TBD: 3.0 update */
if (!SQL_SUCCEEDED(rc)) {
dbd_error(sth, rc, "finish/SQLFreeStmt(SQL_CLOSE)");
return 0;
}
if (DBIc_TRACE(imp_sth, 0, 0, 6)) {
TRACE0(imp_dbh, " dbd_st_finish closed query:\n");
}
}
DBIc_ACTIVE_off(imp_sth);
return 1;
}
void dbd_st_destroy(SV *sth, imp_sth_t *imp_sth)
{
dTHR;
D_imp_dbh_from_sth;
RETCODE rc;
/* Free contents of imp_sth */
/* PerlIO_printf(DBIc_LOGPIO(imp_dbh), " dbd_st_destroy\n"); */
Safefree(imp_sth->fbh);
Safefree(imp_sth->RowBuffer);
Safefree(imp_sth->ColNames);
Safefree(imp_sth->statement);
if (imp_sth->out_params_av)
sv_free((SV*)imp_sth->out_params_av);
if (imp_sth->all_params_hv) {
HV *hv = imp_sth->all_params_hv;
SV *sv;
char *key;
I32 retlen;
hv_iterinit(hv);
while( (sv = hv_iternextsv(hv, &key, &retlen)) != NULL ) {
if (sv != &sv_undef) {
phs_t *phs_tpl = (phs_t*)(void*)SvPVX(sv);
sv_free(phs_tpl->sv);
}
}
sv_free((SV*)imp_sth->all_params_hv);
}
/* SQLxxx functions dump core when no connection exists. This happens
* when the db was disconnected before perl ending. Hence,
* checking for the dirty flag.
*/
if (imp_dbh->hdbc != SQL_NULL_HDBC && !PL_dirty) {
rc = SQLFreeHandle(SQL_HANDLE_STMT,imp_sth->hstmt);
/* rc = SQLFreeStmt(imp_sth->hstmt, SQL_DROP);*/ /* TBD: 3.0 update */
if (DBIc_TRACE(imp_sth, 0, 0, 5))
TRACE1(imp_dbh, " SQLFreeStmt=%d.\n", rc);
if (!SQL_SUCCEEDED(rc)) {
dbd_error(sth, rc, "st_destroy/SQLFreeStmt(SQL_DROP)");
/* return 0; */
}
}
DBIc_IMPSET_off(imp_sth); /* let DBI know we've done it */
}
/************************************************************************/
/* */
/* get_param_type */
/* ============== */
/* */
/* Sets the following fields for a parameter in the phs_st: */
/* */
/* sql_type - the SQL type to use when binding this parameter */
/* describe_param_called - set to 1 if we called SQLDescribeParam */
/* describe_param_status - set to result of SQLDescribeParam if */
/* SQLDescribeParam called */
/* described_sql_type - the sql type returned by SQLDescribeParam */
/* param_size - the parameter size returned by SQLDescribeParam */
/* */
/* The sql_type field is set to one of the following: */
/* value passed in bind method call if specified */
/* if SQLDescribeParam not supported: */
/* value of odbc_default_bind_type attribute if set else */
/* SQL_VARCHAR */
/* if SQLDescribeParam supported: */
/* if SQLDescribeParam succeeds: */
/* parameter type returned by SQLDescribeParam */
/* else if SQLDescribeParam fails: */
/* value of odbc_default_bind_type attribute if set else */
/* SQL_VARCHAR */
/* */
/* NOTE: Just because an ODBC driver says it supports SQLDescribeParam */
/* does not mean you can call it successfully e.g., MS SQL Server */
/* implements SQLDescribeParam by examining your SQL and rewriting it */
/* to be a select statement so it can find the column types etc. This */
/* fails horribly when the statement does not contain a table */
/* e.g., "select ?, LEN(?)" and so do most other SQL Server drivers. */
/* */
/************************************************************************/
static void get_param_type(SV *sth, imp_sth_t *imp_sth, phs_t *phs)
{
SWORD fNullable;
SWORD ibScale;
D_imp_dbh_from_sth;
RETCODE rc;
if (DBIc_TRACE(imp_sth, 0, 0, 4))
TRACE2(imp_sth, " +get_param_type(%p,%s)\n", sth, phs->name);
if (imp_sth->odbc_force_bind_type != 0) {
phs->sql_type = imp_sth->odbc_force_bind_type;
if (DBIc_TRACE(imp_sth, 0, 0, 4))
TRACE1(imp_dbh, " forced param type to %d\n", phs->sql_type);
} else if (imp_dbh->odbc_sqldescribeparam_supported != 1) {
/* As SQLDescribeParam is not supported by the ODBC driver we need to
default a SQL type to bind the parameter as. The default is either
the value set with odbc_default_bind_type or a fallback of
SQL_VARCHAR. */
phs->sql_type = default_parameter_type(imp_sth, phs);
if (DBIc_TRACE(imp_sth, 0, 0, 4))
TRACE1(imp_dbh, " defaulted param type to %d\n", phs->sql_type);
} else if (!phs->describe_param_called) {
/* If we haven't had a go at calling SQLDescribeParam before for this
parameter, have a go now. If it fails we'll default the sql type
as above when driver does not have SQLDescribeParam */
rc = SQLDescribeParam(imp_sth->hstmt,
phs->idx, &phs->described_sql_type,
&phs->param_size, &ibScale,
&fNullable);
phs->describe_param_called = 1;
phs->describe_param_status = rc;
if (!SQL_SUCCEEDED(rc)) {
phs->sql_type = default_parameter_type(imp_sth, phs);
if (DBIc_TRACE(imp_sth, 0, 0, 3))
TRACE1(imp_dbh, " SQLDescribeParam failed reverting to "
"default SQL bind type %d\n", phs->sql_type);
/* show any odbc errors in log */
AllODBCErrors(imp_sth->henv, imp_sth->hdbc, imp_sth->hstmt,
DBIc_TRACE(imp_sth, 0, 0, 3),
DBIc_LOGPIO(imp_sth));
} else {
if (DBIc_TRACE(imp_sth, 0, 0, 5))
PerlIO_printf(DBIc_LOGPIO(imp_dbh),
" SQLDescribeParam %s: SqlType=%s(%d) "
"param_size=%ld Scale=%d Nullable=%d\n",
phs->name,
S_SqlTypeToString(phs->described_sql_type),
phs->described_sql_type,
(unsigned long)phs->param_size, ibScale,
fNullable);
/*
* for non-integral numeric types, let the driver/database handle
* the conversion for us
*/
switch(phs->described_sql_type) {
case SQL_NUMERIC:
case SQL_DECIMAL:
case SQL_FLOAT:
case SQL_REAL:
case SQL_DOUBLE:
if (DBIc_TRACE(imp_sth, 0, 0, 5))
TRACE3(imp_dbh,
" Param %s is numeric SQL type %s "
"(param size:%lu) changed to SQL_VARCHAR",
phs->name,
S_SqlTypeToString(phs->described_sql_type),
(unsigned long)phs->param_size);
phs->sql_type = SQL_VARCHAR;
break;
default:
phs->sql_type = phs->described_sql_type;
}
}
} else if (phs->describe_param_called) {
if (DBIc_TRACE(imp_sth, 0, 0, 5))
TRACE1(imp_dbh,
" SQLDescribeParam already run and returned rc=%d\n",
phs->describe_param_status);
}
if (phs->requested_type != 0) {
phs->sql_type = phs->requested_type;
if (DBIc_TRACE(imp_sth, 0, 0, 5))
TRACE1(imp_dbh, " Overriding sql type with requested type %d\n",
phs->requested_type);
}
#if defined(WITH_UNICODE)
/* for Unicode string types, change value_type to SQL_C_WCHAR*/
switch (phs->sql_type) {
case SQL_WCHAR:
case SQL_WVARCHAR:
case SQL_WLONGVARCHAR:
phs->value_type = SQL_C_WCHAR;
if (DBIc_TRACE(imp_sth, 0, 0, 8)) {
TRACE0(imp_dbh,
" get_param_type: modified value type to SQL_C_WCHAR\n");
}
break;
}
#endif /* WITH_UNICODE */
if (DBIc_TRACE(imp_sth, 0, 0, 8)) TRACE0(imp_dbh, " -get_param_type\n");
}
/*======================================================================*/
/* */
/* rebind_param */
/* ============ */
/* */
/*======================================================================*/
static int rebind_param(
SV *sth,
imp_sth_t *imp_sth,
phs_t *phs)
{
dTHR;
D_imp_dbh_from_sth;
SQLRETURN rc;
SQLULEN default_column_size;
STRLEN value_len = 0;
/* args of SQLBindParameter() call */
SQLSMALLINT param_io_type; /* SQL_PARAM_INPUT_OUTPUT || SQL_PARAM_INPUT */
SQLSMALLINT value_type; /* C data type of parameter */
UCHAR *value_ptr; /* ptr to actual parameter data */
SQLULEN column_size; /* size of column/expression of the parameter */
SQLSMALLINT d_digits; /* decimal digits of parameter */
SQLLEN buffer_length; /* length in bytes of parameter buffer */
SQLLEN strlen_or_ind; /* parameter length or indicator */
if (DBIc_TRACE(imp_sth, 0, 0, 4)) {
char *text = neatsvpv(phs->sv, value_len);
PerlIO_printf(
DBIc_LOGPIO(imp_dbh),
" +rebind_param %s '%.100s' (size svCUR=%d/SvLEN=%d/max=%ld) "
"svtype %u, value type:%d sql type:%d\n",
phs->name, text, SvOK(phs->sv) ? SvCUR(phs->sv) : -1,
SvOK(phs->sv) ? SvLEN(phs->sv) : -1 ,phs->maxlen,
SvTYPE(phs->sv), phs->value_type, phs->sql_type);
}
if (phs->is_inout) {
/*
* At the moment we always do sv_setsv() and rebind.
* Later we may optimise this so that more often we can
* just copy the value & length over and not rebind.
*/
if (SvREADONLY(phs->sv))
croak(PL_no_modify);
/* phs->sv _is_ the real live variable, it may 'mutate' later */
/* pre-upgrade high to reduce risk of SvPVX realloc/move */
(void)SvUPGRADE(phs->sv, SVt_PVNV);
/* ensure room for result, 28 is magic number (see sv_2pv) */
#if defined(WITH_UNICODE)
SvGROW(phs->sv,
(phs->maxlen+sizeof(SQLWCHAR) < 28) ?
28 : phs->maxlen+sizeof(SQLWCHAR));
#else
SvGROW(phs->sv, (phs->maxlen < 28) ? 28 : phs->maxlen+1);
#endif /* WITH_UNICODE */
} else {
/* phs->sv is copy of real variable, upgrade to at least string */
(void)SvUPGRADE(phs->sv, SVt_PV);
}
/*
* At this point phs->sv must be at least a PV with a valid buffer,
* even if it's undef (null)
*/
if (SvOK(phs->sv)) {
phs->sv_buf = SvPV(phs->sv, value_len);
} else {
/* it's undef but if it was inout param it would point to a
* valid buffer, at least */
phs->sv_buf = SvPVX(phs->sv);
value_len = 0;
}
get_param_type(sth, imp_sth, phs);
#if defined(WITH_UNICODE)
if (phs->value_type == SQL_C_WCHAR) {
if (DBIc_TRACE(imp_sth, 0, 0, 8)) {
TRACE1(imp_dbh,
" Need to modify phs->sv in place: old length = %i\n",
value_len);
}
/* Convert the sv in place to UTF-16 encoded characters
NOTE: the SV_toWCHAR may modify SvPV(phs->sv */
if (SvOK(phs->sv)) {
SV_toWCHAR(phs->sv);
/* get new buffer and length */
phs->sv_buf = SvPV(phs->sv, value_len);
} else { /* it is undef */
/* need a valid buffer at least */
phs->sv_buf = SvPVX(phs->sv);
value_len = 0;
}
if (DBIc_TRACE(imp_sth, 0, 0, 8)) {
TRACE1(imp_dbh,
" Need to modify phs->sv in place: new length = %i\n",
value_len);
}
}
/* value_len has current value length now */
phs->sv_type = SvTYPE(phs->sv); /* part of mutation check */
phs->maxlen = SvLEN(phs->sv) - sizeof(SQLWCHAR); /* avail buffer space */
#else /* !WITH_UNICODE */
/* value_len has current value length now */
phs->sv_type = SvTYPE(phs->sv); /* part of mutation check */
phs->maxlen = SvLEN(phs->sv) - 1; /* avail buffer space */
#endif /* WITH_UNICODE */
if (DBIc_TRACE(imp_sth, 0, 0, 4)) {
PerlIO_printf(
DBIc_LOGPIO(imp_dbh),
" bind %s '%.100s' value_len=%d maxlen=%ld null=%d)\n",
phs->name, SvOK(phs->sv) ? phs->sv_buf : "(null)",
value_len,(long)phs->maxlen, SvOK(phs->sv) ? 0 : 1);
}
/*
* JLU: was SQL_PARAM_OUTPUT only, but that caused a problem with
* Oracle's drivers and in/out parameters. Can't be output only
* with Oracle.
*/
param_io_type = phs->is_inout ? SQL_PARAM_INPUT_OUTPUT : SQL_PARAM_INPUT;
value_type = phs->value_type;
d_digits = value_len;
column_size = phs->is_inout ? phs->maxlen : value_len;
/* per patch from Paul G. Weiss, who was experiencing re-preparing
* of queries when the size of the bound string's were increasing
* for example select * from tabtest where name = ?
* then executing with 'paul' and then 'thomas' would cause
* SQLServer to prepare the query twice, but if we ran 'thomas'
* then 'paul', it would not re-prepare the query. The key seems
* to be allocating enough space for the largest parameter.
* TBD: the default for this should be a DBD::ODBC specific option
* or attribute.
*/
if (phs->sql_type == SQL_VARCHAR && !phs->is_inout) {
d_digits = 0;
/* default to at least 80 if this is the first time through */
if (phs->biggestparam == 0) {
phs->biggestparam = (value_len > 80) ? value_len : 80;
} else {
/* bump up max, if needed */
if (value_len > phs->biggestparam) {
phs->biggestparam = value_len;
}
}
}
if ((phs->describe_param_called == 1) &&
(SQL_SUCCEEDED(phs->describe_param_status)) &&
(phs->requested_type == 0)) { /* type not overriden */
default_column_size = phs->param_size;
} else {
if ((phs->sql_type == SQL_VARCHAR) && !phs->is_inout) {
default_column_size = phs->biggestparam;
} else {
default_column_size = value_len;
}
}
/* Default buffer_length to specified output length or actual input length */
buffer_length = phs->is_inout ? phs->maxlen : value_len;
/* When we fill a LONGVARBINARY, the CTYPE must be set to SQL_C_BINARY */
if (value_type == SQL_C_CHAR) { /* could be changed by bind_plh */
d_digits = 0; /* not relevent to char types */
switch(phs->sql_type) {
case SQL_LONGVARBINARY:
case SQL_BINARY:
case SQL_VARBINARY:
value_type = SQL_C_BINARY;
column_size = default_column_size;
break;
#ifdef SQL_WLONGVARCHAR
case SQL_WLONGVARCHAR: /* added for SQLServer 7 ntext type */
#endif
case SQL_CHAR:
case SQL_VARCHAR:
case SQL_LONGVARCHAR:
column_size = default_column_size;
break;
case SQL_DATE:
case SQL_TYPE_DATE:
case SQL_TIME:
case SQL_TYPE_TIME:
break;
case SQL_TIMESTAMP:
case SQL_TYPE_TIMESTAMP:
d_digits = 0; /* tbd: millisecondS?) */
if (SvOK(phs->sv)) {
/* Work out decimal digits value from milliseconds */
char *cp;
if (phs->sv_buf && *phs->sv_buf) {
cp = strchr(phs->sv_buf, '.');
if (cp) {
++cp;
while (*cp != '\0' && isdigit(*cp)) {
cp++;
d_digits++;
}
}
}
}
/*
* 23 is YYYY-MM-DD HH:MM:SS.sss
* We have to be really careful here to maintain the column size
* whether we are passing NULL/undef or not as the ODBC driver
* only has the values we pass to SQLBindParameter to go on and
* cannot know until execute time whether we are passing a NULL or
* not (i.e., although we pass the strlen_or_ind value - last arg to
* SQLBindParameter with a length or SQL_NULL_DATA, this is a ptr
* arg and the driver cannot look at it until execute time).
* We may know we are going to pass a NULL but if we reduce the
* the column size to 0 (or as this function used to do - 1) the
* driver might decide it is not a full datetime and decide to
* bind as a smalldatetime etc. In fact there is a test for
* MS SQL Server in 20SqlServer which binds a datetime and passes
* a NULL, a full datetime and lastly a NULL and if we don't maintain
* 23 for the first NULL MS SQL Server decides it is a smalldatetime
* and we lose the SS.sss in any full datetime passed later.
*/
column_size = 23;
break;
default:
break;
}
} else if ( value_type = SQL_C_WCHAR) {
d_digits = 0;
}
if (!SvOK(phs->sv)) {
strlen_or_ind = SQL_NULL_DATA;
/* if is_inout, shouldn't we null terminate the buffer and send
* it, instead?? */
if (!phs->is_inout) {
/*
* We have to be really careful here to maintain the column size
* whether we are passing NULL/undef or not as the ODBC driver
* only has the values we pass to SQLBindParameter to go on and
* cannot know until execute time whether we are passing a NULL or
* not (i.e., although we pass the strlen_or_ind value - last arg to
* SQLBindParameter with a length or SQL_NULL_DATA, this is a ptr
* arg and the driver cannot look at it until execute time).
* We may know we are going to pass a NULL but if we reduce the
* the column size to 0 (or as this function used to do - 1) the
* driver might decide it is a different type (e.g., smalldatetime
* instead of datetime).
* In fact there is a test for MS SQL Server in 20SqlServer which
* binds a datetime and passes a NULL, a full datetime and lastly a
* NULL and if we don't maintain the column_size for the first NULL
* MS SQL Server decides it is a smalldatetime and we lose the
* SS.sss in any full datetime passed later despite setting a correct
* column_size and decimal digits.
*/
/* column_size = 1; Used to be this but see comment above */
/*
* However, at this stage we could have column_size of 0 and
* that is no good either or we'll get invalid precision
*/
if (column_size == 0) column_size = 1;
}
if (phs->is_inout) {
if (!phs->sv_buf) {
croak("panic: DBD::ODBC binding undef with bad buffer!!!!");
}
/* just in case, we *know* we called SvGROW above */
phs->sv_buf[0] = '\0';
/* patch for binding undef inout params on sql server */
d_digits = 1;
value_ptr = phs->sv_buf;
} else {
value_ptr = NULL;
}
}
else {
value_ptr = phs->sv_buf;
strlen_or_ind = value_len;
/* not undef, may be a blank string or something */
if (!phs->is_inout && strlen_or_ind == 0) {
column_size = 1;
}
}
if (DBIc_TRACE(imp_sth, 0, 0, 4)) {
PerlIO_printf(DBIc_LOGPIO(imp_dbh),
" bind %s value_type:%d %s cs=%lu dd=%d bl=%ld\n",
phs->name, value_type, S_SqlTypeToString(phs->sql_type),
(unsigned long)column_size, d_digits, buffer_length);
}
if (value_len < imp_sth->odbc_putdata_start) {
/* already set and should be left alone JLU */
/* d_digits = value_len; */
} else {
SQLLEN vl = value_len;
if (DBIc_TRACE(imp_sth, 0, 0, 4))
TRACE1(imp_dbh, " using data_at_exec for size %lu\n",
(unsigned long)value_len);
d_digits = 0; /* not relevant to lobs */
strlen_or_ind = SQL_LEN_DATA_AT_EXEC(vl);
value_ptr = (UCHAR*) phs;
}
#if THE_FOLLOWING_CODE_IS_FLAWED_AND_BROKEN
/*
* value_ptr is not null terminated - it is a byte array so PVallocW
* won't work as it works on null terminated strings
*/
#if defined(WITH_UNICODE)
if (value_type==SQL_C_WCHAR) {
char * c1;
c1 = PVallocW((SQLWCHAR *)value_ptr);
TRACE1(imp_dbh, " Param value = L'%s'\n", c1);
PVfreeW(c1);
}
#endif /* WITH_UNICODE */
#endif
/*
* The following code is a workaround for a problem in SQL Server
* when inserting more than 400K into varbinary(max) or varchar(max)
* columns. The older SQL Server driver (not the native client driver):
*
* o reports the size of xxx(max) columns as 2147483647 bytes in size
* when in reality they can be a lot bigger than that.
* o if you bind more than 400K you get the following errors:
* (HY000, 0, [Microsoft][ODBC SQL Server Driver]
* Warning: Partial insert/update. The insert/update of a text or
* image column(s) did not succeed.)
* (42000, 7125, [Microsoft][ODBC SQL Server Driver][SQL Server]
* The text, ntext, or image pointer value conflicts with the column
* name specified.)
*
* There appear to be 2 workarounds but I was not prepared to do the first.
* The first is simply to set the indicator to SQL_LEN_DATA_AT_EXEC(409600)
* if the parameter was larger than 409600 - miraculously it works but
* shouldn't according to MSDN.
* The second workaround (used here) is to set the indicator to
* SQL_LEN_DATA_AT_EXEC(0) and the buffer_length to 0.
*
*/
if ((imp_dbh->driver_type == DT_SQL_SERVER) &&
((phs->sql_type == SQL_LONGVARCHAR) ||
(phs->sql_type == SQL_LONGVARBINARY) ||
(phs->sql_type == SQL_WLONGVARCHAR)) &&
/*(column_size == 2147483647) && (strlen_or_ind < 0) &&*/
((-strlen_or_ind + SQL_LEN_DATA_AT_EXEC_OFFSET) >= 409600)) {
strlen_or_ind = SQL_LEN_DATA_AT_EXEC(0);
buffer_length = 0;
}
#if defined(WITH_UNICODE)
/*
* rt43384 - MS Access does not seem to like us binding parameters as
* wide characters and then SQLBindParameter column_size to byte length.
* e.g., if you have a text(255) column and try and insert 190 ascii chrs
* then the unicode enabled version of DBD::ODBC will convert those 190
* ascii chrs to wide chrs and hence double the size to 380. If you pass
* 380 to Access for column_size it just returns an invalid precision
* value. This changes to column_size to chrs instead of bytes but
* only if column_size is not reduced to 0 - which also produces
* an access error e.g., in the empty string '' case.
*/
else if (((imp_dbh->driver_type == DT_MS_ACCESS_JET) ||
(imp_dbh->driver_type == DT_MS_ACCESS_ACE)) &&
(value_type == SQL_C_WCHAR) && (column_size > 1)) {
column_size = column_size / 2;
if (DBIc_TRACE(imp_sth, 0, 0, 4))
TRACE0(imp_dbh, " MSAccess - setting chrs not bytes\n");
}
#endif
/*
* workaround bug in SQL Server ODBC driver where it can describe some
* parameters (especially in SQL using sub selects) the wrong way.
* If this is a varchar then the column_size must be at least as big
* as the buffer size but if SQL Server associated the wrong column with
* our parameter it could get a totally different size. Without this
* a varchar(10) column can be desribed as a varchar(n) where n is less
* than 10 and this leads to data truncation errors - see rt 39841.
*/
if (((imp_dbh->driver_type == DT_SQL_SERVER) ||
(imp_dbh->driver_type == DT_SQL_SERVER_NATIVE_CLIENT)) &&
(phs->sql_type == SQL_VARCHAR) &&
(column_size < buffer_length)) {
column_size = buffer_length;
}
/*
* Yet another workaround for SQL Server native client.
* If you have a varbinary(max), varchar(max) or nvarchar(max) you have to
* pass 0 for the column_size or you get HY104 "Invalid precision value".
* See rt_38977.t which causes this.
* The versions of native client I've seen this with are:
* 2007.100.1600.22 sqlncli10.dll driver version = ?
* 2005.90.1399.00 SQLNCLI.DLL driver version = 09.00.1399
*
* Update, for nvarchar(max) it does not seem to simply be a driver issue
* as with the Easysoft SQL Server ODBC Driver going to Microsoft SQL Server
* 09.00.1399 we got the following error for all sizes between 4001 and 8000
* (inclusive).
* [SQL Server]The size (4001) given to the parameter '@P1' exceeds the
* maximum allowed (4000)
*
* So to sum up for the native client when the parameter size is 0 or
* when the database is sql server and wchar and sql type not overwritten
* we need to use column size 0. We cannot do this if the requested_type
* was specified as if someone specifies a bind type we haven't called
* SQLDescribeParam and it looks like param_size = 0 even when it is
* not a xxx(max). e.g., the 40UnicodeRoundTrip tests will fail with
* MS SQL Server because they override the type.
*/
if ((phs->param_size == 0) &&
(SQL_SUCCEEDED(phs->describe_param_status))) {
/* not point in believing param_size = 0 if SQLDescribeParam failed */
/* See rt 55736 */
if ((imp_dbh->driver_type == DT_SQL_SERVER_NATIVE_CLIENT) ||
((strcmp(imp_dbh->odbc_dbms_name, "Microsoft SQL Server") == 0) &&
(phs->sql_type == SQL_WVARCHAR) &&
(phs->requested_type == 0))) {
column_size = 0;
}
}
/* for rt_38977 we get:
* sloi = -500100 ps=0 sqlt=12 (SQL_VARCHAR)
* sloi = -500100 ps=0 sqlt=-3 (SQL_VARBINARY)
* sloi = 4001 ps=0 sqlt=-9 (SQL_WVARCHAR) <--- this one fails without above
*/
/*printf("sloi = %d ps=%d sqlt=%d\n", strlen_or_ind, phs->param_size, phs->sql_type);*/
/*
* Avoid calling SQLBindParameter again if nothing has changed.
* Why, because a) there is no point and b) MS SQL Server will
* re-prepare the statement.
*/
/* phs' copy of strlen_or_ind is permanently allocated and the other
strlen_or_ind is an automatic variable and won't survive this func
but needs to. */
phs->strlen_or_ind = strlen_or_ind;
if ((param_io_type == SQL_PARAM_INPUT_OUTPUT) ||
(!phs->bp_value_ptr) || /* not bound before */
((param_io_type == SQL_PARAM_INPUT) && /* input parameter */
(value_ptr != phs->bp_value_ptr) ||
(value_type != phs->value_type) ||
(column_size != phs->bp_column_size) ||
(d_digits != phs->bp_d_digits) ||
(buffer_length != phs->bp_buffer_length))) {
if (DBIc_TRACE(imp_sth, 0, 0, 5)) {
PerlIO_printf(
DBIc_LOGPIO(imp_dbh),
" SQLBindParameter: idx=%d: io_type=%d, name=%s, "
"value_type=%d, SQLType=%d, column_size=%lu, d_digits=%d, "
"value_ptr=%p, buffer_length=%ld, ind=%ld, param_size=%lu\n",
phs->idx, param_io_type, phs->name, value_type, phs->sql_type,
(unsigned long)column_size, d_digits, value_ptr,
(long)buffer_length, (long)strlen_or_ind,
(unsigned long)phs->param_size);
/* avoid tracing data_at_exec as value_ptr will point to phs */
if ((value_type == SQL_C_CHAR) && (strlen_or_ind > 0)) {
TRACE1(imp_sth, " Param value = %s\n", value_ptr);
}
}
#ifdef FRED
printf("SQLBindParameter idx=%d pt=%d vt=%d, st=%d, cs=%lu dd=%d vp=%p bl=%ld slorind=%ld %s\n",
phs->idx, param_io_type, value_type, phs->sql_type,
(unsigned long)column_size, d_digits, value_ptr,
buffer_length, (long)phs->strlen_or_ind, value_ptr);
#endif
rc = SQLBindParameter(imp_sth->hstmt,
phs->idx, param_io_type, value_type,
phs->sql_type, column_size, d_digits,
value_ptr, buffer_length,
&phs->strlen_or_ind);
if (!SQL_SUCCEEDED(rc)) {
dbd_error(sth, rc, "rebind_param/SQLBindParameter");
phs->bp_value_ptr = NULL;
return 0;
}
phs->bp_value_ptr = value_ptr;
phs->value_type = value_type;
phs->bp_column_size = column_size;
phs->bp_d_digits = d_digits;
phs->bp_buffer_length = buffer_length;
} else if (DBIc_TRACE(imp_sth, 0, 0, 5)) {
TRACE1(imp_sth, " Not rebinding param %d - no change\n", phs->idx);
}
if (DBIc_TRACE(imp_sth, 0, 0, 4)) TRACE0(imp_dbh, " -rebind_param\n");
return 1;
}
/*------------------------------------------------------------
* bind placeholder.
* Is called from ODBC.xs execute()
* AND from ODBC.xs bind_param()
*/
int dbd_bind_ph(
SV *sth,
imp_sth_t *imp_sth,
SV *ph_namesv,
SV *newvalue,
IV in_sql_type,
SV *attribs,
int is_inout,
IV maxlen)
{
dTHR;
SV **phs_svp;
STRLEN name_len;
char *name;
char namebuf[30];
phs_t *phs;
D_imp_dbh_from_sth;
SQLSMALLINT sql_type;
sql_type = (SQLSMALLINT)in_sql_type;
if (SvNIOK(ph_namesv) ) { /* passed as a number */
name = namebuf;
sprintf(name, "%d", (int)SvIV(ph_namesv));
name_len = strlen(name);
}
else {
name = SvPV(ph_namesv, name_len);
}
if (DBIc_TRACE(imp_sth, 0, 0, 4)) {
PerlIO_printf(
DBIc_LOGPIO(imp_dbh),
"+dbd_bind_ph(%p, name=%s, value='%.200s', attribs=%s, "
"sql_type=%d(%s), is_inout=%d, maxlen=%ld\n",
sth, name, SvOK(newvalue) ? SvPV_nolen(newvalue) : "undef",
attribs ? SvPV_nolen(attribs) : "", sql_type,
S_SqlTypeToString(sql_type), is_inout, maxlen);
}
/* the problem with the code below is we are getting SVt_PVLV when
* an "undef" value from a hash lookup that doesn't exist. It's an
* "undef" value, but it doesn't come in as a scalar.
* from a hash is arriving. Let's leave this out until we are
* handling arrays. JLU 7/12/02
*/
#if 0
if (SvTYPE(newvalue) > SVt_PVMG) { /* hook for later array logic */
if (DBIc_TRACE(imp_sth, 0, 0, 3))
TRACE2(imp_dbh, " !!bind %s perl type = %d -- croaking!\n",
name, SvTYPE(newvalue));
croak("Can't bind non-scalar value (currently)");
}
#endif
/*
* all_params_hv created during dbd_preparse.
*/
phs_svp = hv_fetch(imp_sth->all_params_hv, name, (I32)name_len, 0);
if (phs_svp == NULL)
croak("Can't bind unknown placeholder '%s'", name);
phs = (phs_t*)SvPVX(*phs_svp); /* placeholder struct */
if (phs->sv == &PL_sv_undef) { /* first bind for this placeholder */
phs->value_type = SQL_C_CHAR; /* default */
phs->requested_type = sql_type; /* save type requested */
phs->maxlen = maxlen; /* 0 if not inout */
phs->is_inout = is_inout;
if (is_inout) {
phs->sv = SvREFCNT_inc(newvalue); /* point to live var */
++imp_sth->has_inout_params;
/* build array of phs's so we can deal with out vars fast */
if (!imp_sth->out_params_av)
imp_sth->out_params_av = newAV();
av_push(imp_sth->out_params_av, SvREFCNT_inc(*phs_svp));
}
} else if (sql_type) {
/* parameter attributes are supposed to be sticky until overriden
so only replace requested_type if sql_type specified.
See https://rt.cpan.org/Ticket/Display.html?id=46597 */
phs->requested_type = sql_type; /* save type requested */
}
/* check later rebinds for any changes */
/*
* else if (is_inout || phs->is_inout) {
* croak("Can't rebind or change param %s in/out mode after first bind", phs->name);
* }
* */
else if (is_inout != phs->is_inout) {
croak("Can't rebind or change param %s in/out mode after first bind "
"(%d => %d)", phs->name, phs->is_inout, is_inout);
}
else if (maxlen && maxlen > phs->maxlen) {
if (DBIc_TRACE(imp_sth, 0, 0, 4))
PerlIO_printf(DBIc_LOGPIO(imp_dbh),
"!attempt to change param %s maxlen (%ld->$ld)\n",
phs->name, phs->maxlen, maxlen);
croak("Can't change param %s maxlen (%ld->%ld) after first bind",
phs->name, phs->maxlen, maxlen);
}
if (!is_inout) { /* normal bind to take a (new) copy of current value */
if (phs->sv == &PL_sv_undef) /* (first time bind) */
phs->sv = newSV(0);
sv_setsv(phs->sv, newvalue);
} else if (newvalue != phs->sv) {
if (phs->sv)
SvREFCNT_dec(phs->sv);
phs->sv = SvREFCNT_inc(newvalue); /* point to live var */
}
if (imp_dbh->odbc_defer_binding) {
get_param_type(sth, imp_sth, phs);
if (DBIc_TRACE(imp_sth, 0, 0, 4)) TRACE0(imp_dbh, "-dbd_bind_ph=1\n");
return 1;
}
/* fall through for "immediate" binding */
if (DBIc_TRACE(imp_sth, 0, 0, 4))
TRACE0(imp_dbh, "-dbd_bind_ph=rebind_param\n");
return rebind_param(sth, imp_sth, phs);
}
/*------------------------------------------------------------
* blob_read:
* read part of a BLOB from a table.
* XXX needs more thought
*/
int dbd_st_blob_read(sth, imp_sth, field, offset, len, destrv, destoffset)
SV *sth;
imp_sth_t *imp_sth;
int field;
long offset;
long len;
SV *destrv;
long destoffset;
{
dTHR;
SQLLEN retl;
SV *bufsv;
RETCODE rc;
bufsv = SvRV(destrv);
sv_setpvn(bufsv,"",0); /* ensure it's writable string */
SvGROW(bufsv, len+destoffset+1); /* SvGROW doesn't do +1 */
/* XXX for this to work be probably need to avoid calling SQLGetData in
* fetch. The definition of SQLGetData doesn't work well with the DBI's
* notion of how LongReadLen would work. Needs more thought. */
rc = SQLGetData(imp_sth->hstmt, (SQLSMALLINT)(field+1),
SQL_C_BINARY,
((UCHAR *)SvPVX(bufsv)) + destoffset, len, &retl
);
if (DBIc_TRACE(imp_sth, 0, 0, 4))
PerlIO_printf(
DBIc_LOGPIO(imp_sth),
"SQLGetData(...,off=%ld, len=%ld)->rc=%d,len=%ld SvCUR=%d\n",
destoffset, len, rc, (long)retl, SvCUR(bufsv));
if (!SQL_SUCCEEDED(rc)) {
dbd_error(sth, rc, "dbd_st_blob_read/SQLGetData");
return 0;
}
if (rc == SQL_SUCCESS_WITH_INFO) { /* XXX should check for 01004 */
retl = len;
}
if (retl == SQL_NULL_DATA) { /* field is null */
(void)SvOK_off(bufsv);
return 1;
}
#ifdef SQL_NO_TOTAL
if (retl == SQL_NO_TOTAL) { /* unknown length! */
(void)SvOK_off(bufsv);
return 0;
}
#endif
SvCUR_set(bufsv, destoffset+retl);
*SvEND(bufsv) = '\0'; /* consistent with perl sv_setpvn etc */
if (DBIc_TRACE(imp_sth, 0, 0, 4))
TRACE1(imp_sth, " blob_read: SvCUR=%d\n", SvCUR(bufsv));
return 1;
}
/*======================================================================*/
/* */
/* S_db_storeOptions */
/* ================= */
/* S_db_fetchOptions */
/* ================= */
/* */
/* An array of options/attributes we support on database handles for */
/* storing and fetching. */
/* */
/*======================================================================*/
typedef struct {
const char *str;
UWORD fOption;
UDWORD atrue;
UDWORD afalse;
} db_params;
static db_params S_db_storeOptions[] = {
{ "AutoCommit", SQL_AUTOCOMMIT, SQL_AUTOCOMMIT_ON, SQL_AUTOCOMMIT_OFF },
{ "ReadOnly", SQL_ATTR_ACCESS_MODE, SQL_MODE_READ_ONLY, SQL_MODE_READ_WRITE},
#if 0 /* not defined by DBI/DBD specification */
{ "TRANSACTION",
SQL_ACCESS_MODE, SQL_MODE_READ_ONLY, SQL_MODE_READ_WRITE },
{ "solid_trace", SQL_OPT_TRACE, SQL_OPT_TRACE_ON, SQL_OPT_TRACE_OFF },
{ "solid_timeout", SQL_LOGIN_TIMEOUT },
{ "ISOLATION", SQL_TXN_ISOLATION },
{ "solid_tracefile", SQL_OPT_TRACEFILE },
#endif
{ "odbc_SQL_ROWSET_SIZE", SQL_ROWSET_SIZE },
{ "odbc_ignore_named_placeholders", ODBC_IGNORE_NAMED_PLACEHOLDERS },
{ "odbc_default_bind_type", ODBC_DEFAULT_BIND_TYPE },
{ "odbc_force_bind_type", ODBC_FORCE_BIND_TYPE },
{ "odbc_force_rebind", ODBC_FORCE_REBIND },
{ "odbc_async_exec", ODBC_ASYNC_EXEC },
{ "odbc_err_handler", ODBC_ERR_HANDLER },
{ "odbc_exec_direct", ODBC_EXEC_DIRECT },
{ "odbc_version", ODBC_VERSION },
{ "odbc_cursortype", ODBC_CURSORTYPE },
{ "odbc_query_timeout", ODBC_QUERY_TIMEOUT },
{ "odbc_putdata_start", ODBC_PUTDATA_START },
{ "odbc_column_display_size", ODBC_COLUMN_DISPLAY_SIZE },
{ "odbc_utf8_on", ODBC_UTF8_ON },
{ NULL },
};
static db_params S_db_fetchOptions[] = {
{ "AutoCommit", SQL_AUTOCOMMIT, SQL_AUTOCOMMIT_ON, SQL_AUTOCOMMIT_OFF },
{ "ReadOnly", SQL_ATTR_ACCESS_MODE, SQL_MODE_READ_ONLY, SQL_MODE_READ_WRITE}, { "RowCacheSize", ODBC_ROWCACHESIZE },
{ "odbc_SQL_ROWSET_SIZE", SQL_ROWSET_SIZE },
{ "odbc_SQL_DRIVER_ODBC_VER", SQL_DRIVER_ODBC_VER },
{ "odbc_ignore_named_placeholders", ODBC_IGNORE_NAMED_PLACEHOLDERS },
{ "odbc_default_bind_type", ODBC_DEFAULT_BIND_TYPE },
{ "odbc_force_bind_type", ODBC_FORCE_BIND_TYPE },
{ "odbc_force_rebind", ODBC_FORCE_REBIND },
{ "odbc_async_exec", ODBC_ASYNC_EXEC },
{ "odbc_err_handler", ODBC_ERR_HANDLER },
{ "odbc_SQL_DBMS_NAME", SQL_DBMS_NAME },
{ "odbc_exec_direct", ODBC_EXEC_DIRECT },
{ "odbc_query_timeout", ODBC_QUERY_TIMEOUT},
{ "odbc_putdata_start", ODBC_PUTDATA_START},
{ "odbc_column_display_size", ODBC_COLUMN_DISPLAY_SIZE},
{ "odbc_utf8_on", ODBC_UTF8_ON},
{ "odbc_has_unicode", ODBC_HAS_UNICODE},
{ "odbc_out_connect_string", ODBC_OUTCON_STR},
{ NULL }
};
/*======================================================================*/
/* */
/* S_dbOption */
/* ========== */
/* */
/* Given a string and a length, locate this option in the specified */
/* array of valid options. Typically used by STORE and FETCH methods */
/* to decide if this option/attribute is supported by us. */
/* */
/*======================================================================*/
static const db_params *
S_dbOption(const db_params *pars, char *key, STRLEN len)
{
/* search option to set */
while (pars->str != NULL) {
if (strncmp(pars->str, key, len) == 0
&& len == strlen(pars->str))
break;
pars++;
}
if (pars->str == NULL) {
return NULL;
}
return pars;
}
/*======================================================================*/
/* */
/* dbd_db_STORE_attrib */
/* =================== */
/* */
/* This function handles: */
/* */
/* $dbh->{$key} = $value */
/* */
/* Method to handle the setting of driver specific attributes and DBI */
/* attributes AutoCommit and ChopBlanks (no other DBI attributes). */
/* */
/* Return TRUE if the attribute was handled, else FALSE. */
/* */
/*======================================================================*/
int dbd_db_STORE_attrib(SV *dbh, imp_dbh_t *imp_dbh, SV *keysv, SV *valuesv)
{
dTHR;
D_imp_drh_from_dbh;
RETCODE rc;
STRLEN kl;
STRLEN plen;
char *key = SvPV(keysv,kl);
int on;
UDWORD vParam;
const db_params *pars;
int bSetSQLConnectionOption;
if ((pars = S_dbOption(S_db_storeOptions, key, kl)) == NULL) {
if (DBIc_TRACE(imp_dbh, 0, 0, 3))
TRACE1(imp_dbh,
" !!DBD::ODBC unsupported attribute passed (%s)\n", key);
return FALSE;
}
bSetSQLConnectionOption = TRUE;
switch(pars->fOption)
{
case SQL_LOGIN_TIMEOUT:
case SQL_TXN_ISOLATION:
case SQL_ROWSET_SIZE:
vParam = SvIV(valuesv);
break;
case SQL_OPT_TRACEFILE:
vParam = (UDWORD) SvPV(valuesv, plen);
break;
case ODBC_IGNORE_NAMED_PLACEHOLDERS:
bSetSQLConnectionOption = FALSE;
/*
* set value to ignore placeholders. Will affect all
* statements from here on.
*/
imp_dbh->odbc_ignore_named_placeholders = SvTRUE(valuesv);
break;
case ODBC_DEFAULT_BIND_TYPE:
bSetSQLConnectionOption = FALSE;
/*
* set value of default bind type. Default is SQL_VARCHAR,
* but setting to 0 will cause SQLDescribeParam to be used.
*/
imp_dbh->odbc_default_bind_type = (SQLSMALLINT)SvIV(valuesv);
break;
case ODBC_FORCE_BIND_TYPE:
bSetSQLConnectionOption = FALSE;
/*
* set value of the forced bind type. Default is 0
* which means the bind type is not forced to be anything -
* we will use SQLDescribeParam or fall back on odbc_default_bind_type
*/
imp_dbh->odbc_force_bind_type = (SQLSMALLINT)SvIV(valuesv);
break;
case ODBC_FORCE_REBIND:
bSetSQLConnectionOption = FALSE;
/*
* set value to force rebind
*/
imp_dbh->odbc_force_rebind = SvTRUE(valuesv);
break;
case ODBC_QUERY_TIMEOUT:
bSetSQLConnectionOption = FALSE;
imp_dbh->odbc_query_timeout = (SQLINTEGER)SvIV(valuesv);
break;
case ODBC_PUTDATA_START:
bSetSQLConnectionOption = FALSE;
imp_dbh->odbc_putdata_start = SvIV(valuesv);
break;
case ODBC_COLUMN_DISPLAY_SIZE:
bSetSQLConnectionOption = FALSE;
imp_dbh->odbc_column_display_size = SvIV(valuesv);
break;
case ODBC_UTF8_ON:
bSetSQLConnectionOption = FALSE;
imp_dbh->odbc_utf8_on = SvIV(valuesv);
break;
case ODBC_EXEC_DIRECT:
bSetSQLConnectionOption = FALSE;
/*
* set value of odbc_exec_direct. Non-zero will
* make prepare, essentially a noop and make execute
* use SQLExecDirect. This is to support drivers that
* _only_ support SQLExecDirect.
*/
imp_dbh->odbc_exec_direct = SvTRUE(valuesv);
break;
case ODBC_ASYNC_EXEC:
bSetSQLConnectionOption = FALSE;
/*
* set asynchronous execution. It can only be turned on if
* the driver supports it, but will fail silently.
*/
if (SvTRUE(valuesv)) {
/* Only bother setting the attribute if it's not already set! */
if (imp_dbh->odbc_async_exec)
break;
/*
* Determine which method of async execution this
* driver allows -- per-connection or per-statement
*/
rc = SQLGetInfo(imp_dbh->hdbc,
SQL_ASYNC_MODE,
&imp_dbh->odbc_async_type,
sizeof(imp_dbh->odbc_async_type),
NULL
);
/*
* Normally, we'd do a if (!SQL_ok(rc)) ... here.
* Unfortunately, if the driver doesn't support async
* mode, it may return an error here. There doesn't
* seem to be any other way to check (other than doing
* a special check for the SQLSTATE). We'll just default
* to doing nothing and not bother checking errors.
*/
if (imp_dbh->odbc_async_type == SQL_AM_CONNECTION){
/*
* Driver has per-connection async option. Set it
* now in the dbh.
*/
if (DBIc_TRACE(imp_dbh, 0, 0, 4))
TRACE0(imp_dbh,
" Supported AsyncType is SQL_AM_CONNECTION\n");
rc = SQLSetConnectOption(imp_dbh->hdbc,
SQL_ATTR_ASYNC_ENABLE,
SQL_ASYNC_ENABLE_ON
);
if (!SQL_SUCCEEDED(rc)) {
dbd_error(dbh, rc, "db_STORE/SQLSetConnectOption");
return FALSE;
}
imp_dbh->odbc_async_exec = 1;
}
else if (imp_dbh->odbc_async_type == SQL_AM_STATEMENT){
/*
* Driver has per-statement async option. Just set
* odbc_async_exec and the rest will be handled by
* dbd_st_prepare.
*/
if (DBIc_TRACE(imp_dbh, 0, 0, 4))
TRACE0(imp_dbh,
" Supported AsyncType is SQL_AM_STATEMENT\n");
imp_dbh->odbc_async_exec = 1;
}
else { /* (imp_dbh->odbc_async_type == SQL_AM_NONE) */
/*
* We're out of luck.
*/
if (DBIc_TRACE(imp_dbh, 0, 0, 4))
TRACE0(imp_dbh, " Supported AsyncType is SQL_AM_NONE\n");
imp_dbh->odbc_async_exec = 0;
return FALSE;
}
} else {
/* Only bother turning it off if it was previously set... */
if (imp_dbh->odbc_async_exec == 1) {
/* We only need to do anything here if odbc_async_type is
* SQL_AM_CONNECTION since the per-statement async type
* is turned on only when the statement handle is created.
*/
if (imp_dbh->odbc_async_type == SQL_AM_CONNECTION){
rc = SQLSetConnectOption(imp_dbh->hdbc,
SQL_ATTR_ASYNC_ENABLE,
SQL_ASYNC_ENABLE_OFF
);
if (!SQL_SUCCEEDED(rc)) {
dbd_error(dbh, rc, "db_STORE/SQLSetConnectOption");
return FALSE;
}
}
}
imp_dbh->odbc_async_exec = 0;
}
break;
case ODBC_ERR_HANDLER:
bSetSQLConnectionOption = FALSE;
/* This was taken from DBD::Sybase 0.21 */
/* I believe the following if test which has been in DBD::ODBC
* for ages is wrong and should (at least now) use SvOK or
* it is impossible to reset the error handler
*
* if(valuesv == &PL_sv_undef) {
* imp_dbh->odbc_err_handler = NULL;
*/
if (!SvOK(valuesv)) {
imp_dbh->odbc_err_handler = NULL;
} else if(imp_dbh->odbc_err_handler == (SV*)NULL) {
imp_dbh->odbc_err_handler = newSVsv(valuesv);
} else {
sv_setsv(imp_dbh->odbc_err_handler, valuesv);
}
break;
case ODBC_VERSION:
/* set only in connect, nothing to store */
bSetSQLConnectionOption = FALSE;
break;
case ODBC_CURSORTYPE:
/* set only in connect, nothing to store */
bSetSQLConnectionOption = FALSE;
break;
case SQL_ATTR_ACCESS_MODE:
on = SvTRUE(valuesv);
vParam = on ? pars->atrue : pars->afalse;
break;
default:
on = SvTRUE(valuesv);
vParam = on ? pars->atrue : pars->afalse;
break;
}
if (bSetSQLConnectionOption) {
/* TBD: 3.0 update */
rc = SQLSetConnectOption(imp_dbh->hdbc, pars->fOption, vParam);
if (!SQL_SUCCEEDED(rc)) {
dbd_error(dbh, rc, "db_STORE/SQLSetConnectOption");
return FALSE;
}
/* keep our flags in sync */
if (kl == 10 && strEQ(key, "AutoCommit"))
DBIc_set(imp_dbh, DBIcf_AutoCommit, SvTRUE(valuesv));
}
return TRUE;
}
/*======================================================================*/
/* */
/* dbd_db_FETCH_attrib */
/* =================== */
/* */
/* Counterpart of dbd_db_STORE_attrib handing: */
/* */
/* $value = $dbh->{$key}; */
/* */
/* returns an "SV" with the value */
/* */
/*======================================================================*/
SV *dbd_db_FETCH_attrib(SV *dbh, imp_dbh_t *imp_dbh, SV *keysv)
{
dTHR;
D_imp_drh_from_dbh;
RETCODE rc;
STRLEN kl;
char *key = SvPV(keysv,kl);
UDWORD vParam = 0;
const db_params *pars;
SV *retsv = Nullsv;
/* checking pars we need FAST */
if (DBIc_TRACE(imp_dbh, 0, 0, 8))
TRACE1(imp_dbh, " FETCH %s\n", key);
if ((pars = S_dbOption(S_db_fetchOptions, key, kl)) == NULL)
return Nullsv;
switch (pars->fOption) {
case ODBC_OUTCON_STR:
if (!imp_dbh->out_connect_string) {
retsv = &PL_sv_undef;
} else {
retsv = newSVsv(imp_dbh->out_connect_string);
}
break;
case SQL_DRIVER_ODBC_VER:
retsv = newSVpv(imp_dbh->odbc_ver, 0);
break;
case SQL_DBMS_NAME:
retsv = newSVpv(imp_dbh->odbc_dbms_name, 0);
break;
case ODBC_IGNORE_NAMED_PLACEHOLDERS:
retsv = newSViv(imp_dbh->odbc_ignore_named_placeholders);
break;
case ODBC_QUERY_TIMEOUT:
/*
* fetch current value of query timeout
*
* -1 is our internal flag saying odbc_query_timeout has never been
* set so we map it back to the default for ODBC which is 0
*/
if (imp_dbh->odbc_query_timeout == -1) {
retsv = newSViv(0);
} else {
retsv = newSViv(imp_dbh->odbc_query_timeout);
}
break;
case ODBC_PUTDATA_START:
retsv = newSViv(imp_dbh->odbc_putdata_start);
break;
case ODBC_COLUMN_DISPLAY_SIZE:
retsv = newSViv(imp_dbh->odbc_column_display_size);
break;
case ODBC_UTF8_ON:
retsv = newSViv(imp_dbh->odbc_utf8_on);
break;
case ODBC_HAS_UNICODE:
retsv = newSViv(imp_dbh->odbc_has_unicode);
break;
case ODBC_DEFAULT_BIND_TYPE:
retsv = newSViv(imp_dbh->odbc_default_bind_type);
break;
case ODBC_FORCE_BIND_TYPE:
retsv = newSViv(imp_dbh->odbc_force_bind_type);
break;
case ODBC_FORCE_REBIND:
retsv = newSViv(imp_dbh->odbc_force_rebind);
break;
case ODBC_EXEC_DIRECT:
retsv = newSViv(imp_dbh->odbc_exec_direct);
break;
case ODBC_ASYNC_EXEC:
/*
* fetch current value of asynchronous execution (should be
* either 0 or 1).
*/
retsv = newSViv(imp_dbh->odbc_async_exec);
break;
case ODBC_ERR_HANDLER:
/* fetch current value of the error handler (a coderef). */
if(imp_dbh->odbc_err_handler) {
retsv = newSVsv(imp_dbh->odbc_err_handler);
} else {
retsv = &PL_sv_undef;
}
break;
case ODBC_ROWCACHESIZE:
retsv = newSViv(imp_dbh->RowCacheSize);
break;
default:
/*
* The remainders we support are ODBC attributes like
* AutoCommit (SQL_AUTOCOMMIT)
* odbc_SQL_ROWSET_SIZE (SQL_ROWSET_SIZE)
*
* Nothing else should get here for now unless any item is added
* to S_db_fetchOptions.
*/
rc = SQLGetConnectOption(imp_dbh->hdbc, pars->fOption, &vParam);/* TBD: 3.0 update */
dbd_error(dbh, rc, "db_FETCH/SQLGetConnectOption");
if (!SQL_SUCCEEDED(rc)) {
if (DBIc_TRACE(imp_dbh, 0, 0, 3))
TRACE1(imp_dbh,
" !!SQLGetConnectOption=%d in dbd_db_FETCH\n", rc);
return Nullsv;
}
switch(pars->fOption) {
case SQL_ROWSET_SIZE:
retsv = newSViv(vParam);
break;
default:
if (vParam == pars->atrue)
retsv = newSViv(1);
else
retsv = newSViv(0);
break;
} /* inner switch */
} /* outer switch */
return sv_2mortal(retsv);
}
/*======================================================================*/
/* */
/* S_st_fetch_params */
/* ================= */
/* S_st_store_params */
/* ================= */
/* */
/* An array of options/attributes we support on statement handles for */
/* storing and fetching. */
/* */
/*======================================================================*/
/*
* added "need_describe" flag to handle the situation where you don't
* have a result set yet to describe. Certain attributes don't need
* the result set to operate, hence don't do a describe unless you need
* to do one.
* DBD::ODBC 0.45_15
* */
typedef struct {
const char *str;
unsigned len:8;
unsigned array:1;
unsigned need_describe:1;
unsigned filler:22;
} T_st_params;
#define s_A(str,need_describe) { str, sizeof(str)-1,0,need_describe }
static T_st_params S_st_fetch_params[] =
{
s_A("NUM_OF_PARAMS",1), /* 0 */
s_A("NUM_OF_FIELDS",1), /* 1 */
s_A("NAME",1), /* 2 */
s_A("NULLABLE",1), /* 3 */
s_A("TYPE",1), /* 4 */
s_A("PRECISION",1), /* 5 */
s_A("SCALE",1), /* 6 */
s_A("sol_type",1), /* 7 */
s_A("sol_length",1), /* 8 */
s_A("CursorName",1), /* 9 */
s_A("odbc_more_results",1), /* 10 */
s_A("ParamValues",0), /* 11 */
s_A("LongReadLen",0), /* 12 */
s_A("odbc_ignore_named_placeholders",0), /* 13 */
s_A("odbc_default_bind_type",0), /* 14 */
s_A("odbc_force_rebind",0), /* 15 */
s_A("odbc_query_timeout",0), /* 16 */
s_A("odbc_putdata_start",0), /* 17 */
s_A("ParamTypes",0), /* 18 */
s_A("odbc_column_display_size",0), /* 19 */
s_A("odbc_force_bind_type",0), /* 20 */
s_A("",0), /* END */
};
static T_st_params S_st_store_params[] =
{
s_A("odbc_ignore_named_placeholders",0), /* 0 */
s_A("odbc_default_bind_type",0), /* 1 */
s_A("odbc_force_rebind",0), /* 2 */
s_A("odbc_query_timeout",0), /* 3 */
s_A("odbc_putdata_start",0), /* 4 */
s_A("odbc_column_display_size",0), /* 5 */
s_A("odbc_force_bind_type",0), /* 6 */
s_A("",0), /* END */
};
#undef s_A
/*======================================================================*/
/* */
/* dbd_st_FETCH_attrib */
/* =================== */
/* */
/*======================================================================*/
SV *dbd_st_FETCH_attrib(SV *sth, imp_sth_t *imp_sth, SV *keysv)
{
dTHR;
STRLEN kl;
char *key = SvPV(keysv,kl);
int i;
SV *retsv = NULL;
T_st_params *par;
char cursor_name[256];
SWORD cursor_name_len;
RETCODE rc;
for (par = S_st_fetch_params; par->len > 0; par++)
if (par->len == kl && strEQ(key, par->str))
break;
if (par->len <= 0)
return Nullsv;
if (par->need_describe && !imp_sth->done_desc && !dbd_describe(sth, imp_sth,0))
{
/* dbd_describe has already called dbd_error() */
/* we can't return Nullsv here because the xs code will */
/* then just pass the attribute name to DBI for FETCH. */
if (DBIc_TRACE(imp_sth, 0, 0, 4)) {
TRACE1(imp_sth,
" !!!dbd_st_FETCH_attrib (%s) needed query description, "
"but failed\n", par->str);
}
if (DBIc_WARN(imp_sth)) {
warn("Describe failed during %s->FETCH(%s,%d)",
SvPV(sth,PL_na), key,imp_sth->done_desc);
}
return &PL_sv_undef;
}
i = DBIc_NUM_FIELDS(imp_sth);
switch(par - S_st_fetch_params)
{
AV *av;
case 0: /* NUM_OF_PARAMS */
return Nullsv; /* handled by DBI */
case 1: /* NUM_OF_FIELDS */
if (DBIc_TRACE(imp_sth, 0, 0, 9)) {
TRACE1(imp_sth, " dbd_st_FETCH_attrib NUM_OF_FIELDS %d\n", i);
}
retsv = newSViv(i);
break;
case 2: /* NAME */
av = newAV();
retsv = newRV_inc(sv_2mortal((SV*)av));
if (DBIc_TRACE(imp_sth, 0, 0, 9)) {
int j;
TRACE1(imp_sth, " dbd_st_FETCH_attrib NAMES %d\n", i);
for (j = 0; j < i; j++)
TRACE1(imp_sth, "\t%s\n", imp_sth->fbh[j].ColName);
}
while(--i >= 0) {
if (DBIc_TRACE(imp_sth, 0, 0, 9)) {
TRACE2(imp_sth, " Colname %d => %s\n",
i, imp_sth->fbh[i].ColName);
}
#ifdef WITH_UNICODE
av_store(av, i,
sv_newwvn((SQLWCHAR *)imp_sth->fbh[i].ColName,
imp_sth->fbh[i].ColNameLen));
#else
av_store(av, i, newSVpv(imp_sth->fbh[i].ColName, 0));
#endif
}
break;
case 3: /* NULLABLE */
av = newAV();
retsv = newRV_inc(sv_2mortal((SV*)av));
while(--i >= 0)
av_store(av, i,
(imp_sth->fbh[i].ColNullable == SQL_NO_NULLS)
? &PL_sv_no : &PL_sv_yes);
break;
case 4: /* TYPE */
av = newAV();
retsv = newRV_inc(sv_2mortal((SV*)av));
while(--i >= 0)
av_store(av, i, newSViv(imp_sth->fbh[i].ColSqlType));
break;
case 5: /* PRECISION */
av = newAV();
retsv = newRV_inc(sv_2mortal((SV*)av));
while(--i >= 0)
av_store(av, i, newSViv(imp_sth->fbh[i].ColDef));
break;
case 6: /* SCALE */
av = newAV();
retsv = newRV_inc(sv_2mortal((SV*)av));
while(--i >= 0)
av_store(av, i, newSViv(imp_sth->fbh[i].ColScale));
break;
case 7: /* sol_type */
av = newAV();
retsv = newRV_inc(sv_2mortal((SV*)av));
while(--i >= 0)
av_store(av, i, newSViv(imp_sth->fbh[i].ColSqlType));
break;
case 8: /* sol_length */
av = newAV();
retsv = newRV_inc(sv_2mortal((SV*)av));
while(--i >= 0)
av_store(av, i, newSViv(imp_sth->fbh[i].ColLength));
break;
case 9: /* CursorName */
rc = SQLGetCursorName(imp_sth->hstmt, cursor_name,
sizeof(cursor_name), &cursor_name_len);
if (!SQL_SUCCEEDED(rc)) {
dbd_error(sth, rc, "st_FETCH/SQLGetCursorName");
return Nullsv;
}
retsv = newSVpv(cursor_name, cursor_name_len);
break;
case 10: /* odbc_more_results */
retsv = newSViv(imp_sth->moreResults);
if (i == 0 && imp_sth->moreResults == 0) {
int outparams = (imp_sth->out_params_av) ?
AvFILL(imp_sth->out_params_av)+1 : 0;
if (DBIc_TRACE(imp_sth, 0, 0, 4)) {
TRACE0(imp_sth,
" numfields == 0 && moreResults = 0 finish\n");
}
if (outparams) {
odbc_handle_outparams(imp_sth, DBIc_TRACE_LEVEL(imp_sth));
}
/* XXX need to 'finish' here */
dbd_st_finish(sth, imp_sth);
}
break;
case 11: /* ParamValues */
{
/* not sure if there's a memory leak here. */
HV *paramvalues = newHV();
if (imp_sth->all_params_hv) {
HV *hv = imp_sth->all_params_hv;
SV *sv;
char *key;
I32 retlen;
hv_iterinit(hv);
while( (sv = hv_iternextsv(hv, &key, &retlen)) != NULL ) {
if (sv != &PL_sv_undef) {
phs_t *phs = (phs_t*)(void*)SvPVX(sv);
hv_store(paramvalues, phs->name, (I32)strlen(phs->name),
newSVsv(phs->sv), 0);
}
}
}
/* ensure HV is freed when the ref is freed */
retsv = newRV_noinc((SV *)paramvalues);
break;
}
case 12: /* LongReadLen */
retsv = newSViv(DBIc_LongReadLen(imp_sth));
break;
case 13: /* odbc_ignore_named_placeholders */
retsv = newSViv(imp_sth->odbc_ignore_named_placeholders);
break;
case 14: /* odbc_default_bind_type */
retsv = newSViv(imp_sth->odbc_default_bind_type);
break;
case 15: /* odbc_force_rebind */
retsv = newSViv(imp_sth->odbc_force_rebind);
break;
case 16: /* odbc_query_timeout */
/*
* -1 is our internal flag saying odbc_query_timeout has never been
* set so we map it back to the default for ODBC which is 0
*/
if (imp_sth->odbc_query_timeout == -1) {
retsv = newSViv(0);
} else {
retsv = newSViv(imp_sth->odbc_query_timeout);
}
break;
case 17: /* odbc_putdata_start */
retsv = newSViv(imp_sth->odbc_putdata_start);
break;
case 18: /* ParamTypes */
{
/* not sure if there's a memory leak here. */
HV *paramtypes = newHV();
if (imp_sth->all_params_hv) {
HV *hv = imp_sth->all_params_hv;
SV *sv;
char *key;
I32 retlen;
hv_iterinit(hv);
while( (sv = hv_iternextsv(hv, &key, &retlen)) != NULL ) {
if (sv != &PL_sv_undef) {
HV *subh = newHV();
phs_t *phs = (phs_t*)(void*)SvPVX(sv);
hv_store(subh, "TYPE", 4, newSViv(phs->sql_type), 0);
hv_store(paramtypes, phs->name, (I32)strlen(phs->name),
newRV_noinc((SV *)subh), 0);
}
}
}
/* ensure HV is freed when the ref is freed */
retsv = newRV_noinc((SV *)paramtypes);
break;
}
case 19: /* odbc_column_display_size */
retsv = newSViv(imp_sth->odbc_column_display_size);
break;
case 20: /* odbc_force_bind_type */
retsv = newSViv(imp_sth->odbc_force_bind_type);
break;
default:
return Nullsv;
}
return sv_2mortal(retsv);
}
/*======================================================================*/
/* */
/* dbd_st_STORE_attrib */
/* =================== */
/* */
/*======================================================================*/
int dbd_st_STORE_attrib(SV *sth, imp_sth_t *imp_sth, SV *keysv, SV *valuesv)
{
dTHR;
D_imp_dbh_from_sth;
STRLEN kl;
STRLEN vl;
char *key = SvPV(keysv,kl);
char *value = SvPV(valuesv, vl);
T_st_params *par;
for (par = S_st_store_params; par->len > 0; par++)
if (par->len == kl && strEQ(key, par->str))
break;
if (par->len <= 0)
return FALSE;
switch(par - S_st_store_params)
{
case 0:
imp_sth->odbc_ignore_named_placeholders = SvTRUE(valuesv);
return TRUE;
case 1:
imp_sth->odbc_default_bind_type = (SQLSMALLINT)SvIV(valuesv);
return TRUE;
break;
case 2:
imp_sth->odbc_force_rebind = (int)SvIV(valuesv);
return TRUE;
break;
case 3:
imp_sth->odbc_query_timeout = SvIV(valuesv);
return TRUE;
break;
case 4:
imp_sth->odbc_putdata_start = SvIV(valuesv);
return TRUE;
break;
case 5:
imp_sth->odbc_column_display_size = SvIV(valuesv);
return TRUE;
break;
case 6:
imp_sth->odbc_force_bind_type = (SQLSMALLINT)SvIV(valuesv);
return TRUE;
break;
}
return FALSE;
}
SV *
odbc_get_info(dbh, ftype)
SV *dbh;
int ftype;
{
dTHR;
D_imp_dbh(dbh);
RETCODE rc;
SV *retsv = NULL;
int i;
int size = 256;
char *rgbInfoValue;
SWORD cbInfoValue = -2;
New(0, rgbInfoValue, size, char);
/* See fancy logic below */
for (i = 0; i < 6; i++)
rgbInfoValue[i] = (char)0xFF;
rc = SQLGetInfo(imp_dbh->hdbc, (SQLUSMALLINT)ftype,
rgbInfoValue, (SQLSMALLINT)(size-1), &cbInfoValue);
if (cbInfoValue > size-1) {
Renew(rgbInfoValue, cbInfoValue+1, char);
rc = SQLGetInfo(imp_dbh->hdbc, (SQLUSMALLINT)ftype,
rgbInfoValue, cbInfoValue, &cbInfoValue);
}
if (!SQL_SUCCEEDED(rc)) {
dbd_error(dbh, rc, "odbc_get_info/SQLGetInfo");
Safefree(rgbInfoValue);
/* patched 2/12/02, thanks to Steffen Goldner */
return &PL_sv_undef;
/* return Nullsv; */
}
/* Fancy logic here to determine if result is a string or int */
if (cbInfoValue == -2) /* is int */
retsv = newSViv(*(int *)rgbInfoValue); /* XXX cast */
else if (cbInfoValue != 2 && cbInfoValue != 4) /* must be string */
retsv = newSVpv(rgbInfoValue, 0);
else if (rgbInfoValue[cbInfoValue] == '\0') /* must be string */ /* patch from Steffen Goldner 0.37 2/12/02 */
retsv = newSVpv(rgbInfoValue, 0);
else if (cbInfoValue == 2) /* short */
retsv = newSViv(*(short *)rgbInfoValue); /* XXX cast */
else if (cbInfoValue == 4) /* int */
retsv = newSViv(*(int *)rgbInfoValue); /* XXX cast */
else
croak("panic: SQLGetInfo cbInfoValue == %d", cbInfoValue);
if (DBIc_TRACE(imp_dbh, 0, 0, 4))
PerlIO_printf(
DBIc_LOGPIO(imp_dbh),
" SQLGetInfo: ftype %d, cbInfoValue %d: %s\n",
ftype, cbInfoValue, neatsvpv(retsv,0));
Safefree(rgbInfoValue);
return sv_2mortal(retsv);
}
#ifdef THE_FOLLOWING_NO_LONGER_USED_REPLACE_BY_dbd_st_primary_keys
int
odbc_get_statistics(dbh, sth, CatalogName, SchemaName, TableName, Unique)
SV * dbh;
SV * sth;
char * CatalogName;
char * SchemaName;
char * TableName;
int Unique;
{
dTHR;
D_imp_dbh(dbh);
D_imp_sth(sth);
RETCODE rc;
int dbh_active;
imp_sth->henv = imp_dbh->henv; /* needed for dbd_error */
imp_sth->hdbc = imp_dbh->hdbc;
imp_sth->done_desc = 0;
if ((dbh_active = check_connection_active(dbh)) == 0) return 0;
rc = SQLAllocHandle(SQL_HANDLE_STMT, imp_dbh->hdbc, &imp_sth->hstmt);
if (rc != SQL_SUCCESS) {
dbd_error(sth, rc, "odbc_get_statistics/SQLAllocHandle(stmt)");
return 0;
}
rc = SQLStatistics(imp_sth->hstmt,
CatalogName, (SQLSMALLINT)strlen(CatalogName),
SchemaName, (SQLSMALLINT)strlen(SchemaName),
TableName, (SQLSMALLINT)strlen(TableName),
(SQLUSMALLINT)Unique, (SQLUSMALLINT)0);
if (!SQL_SUCCEEDED(rc)) {
dbd_error(sth, rc, "odbc_get_statistics/SQLGetStatistics");
return 0;
}
return build_results(sth, dbh, rc);
}
#endif /* THE_FOLLOWING_NO_LONGER_USED_REPLACE_BY_dbd_st_primary_keys */
#ifdef THE_FOLLOWING_NO_LONGER_USED_REPLACE_BY_dbd_st_primary_keys
int
odbc_get_primary_keys(dbh, sth, CatalogName, SchemaName, TableName)
SV * dbh;
SV * sth;
char * CatalogName;
char * SchemaName;
char * TableName;
{
dTHR;
D_imp_dbh(dbh);
D_imp_sth(sth);
RETCODE rc;
int dbh_active;
imp_sth->henv = imp_dbh->henv; /* needed for dbd_error */
imp_sth->hdbc = imp_dbh->hdbc;
imp_sth->done_desc = 0;
if ((dbh_active = check_connection_active(dbh)) == 0) return 0;
rc = SQLAllocHandle(SQL_HANDLE_STMT, imp_dbh->hdbc, &imp_sth->hstmt);
if (rc != SQL_SUCCESS) {
dbd_error(sth, rc, "odbc_get_primary_keys/SQLAllocHandle(stmt)");
return 0;
}
rc = SQLPrimaryKeys(imp_sth->hstmt,
CatalogName, (SQLSMALLINT)strlen(CatalogName),
SchemaName, (SQLSMALLINT)strlen(SchemaName),
TableName, (SQLSMALLINT)strlen(TableName));
if (DBIc_TRACE(imp_sth, 0, 0, 3))
TRACE1(imp_dbh, " SQLPrimaryKeys rc = %d\n", rc);
if (!SQL_SUCCEEDED(rc)) {
dbd_error(sth, rc, "odbc_get_primary_keys/SQLPrimaryKeys");
return 0;
}
return build_results(sth, dbh, rc);
}
#endif /* THE_FOLLOWING_NO_LONGER_USED_REPLACE_BY_dbd_st_primary_keys */
int
odbc_get_special_columns(dbh, sth, Identifier, CatalogName, SchemaName, TableName, Scope, Nullable)
SV * dbh;
SV * sth;
int Identifier;
char * CatalogName;
char * SchemaName;
char * TableName;
int Scope;
int Nullable;
{
dTHR;
D_imp_dbh(dbh);
D_imp_sth(sth);
RETCODE rc;
int dbh_active;
imp_sth->henv = imp_dbh->henv; /* needed for dbd_error */
imp_sth->hdbc = imp_dbh->hdbc;
imp_sth->done_desc = 0;
if ((dbh_active = check_connection_active(dbh)) == 0) return 0;
rc = SQLAllocHandle(SQL_HANDLE_STMT, imp_dbh->hdbc, &imp_sth->hstmt);
if (rc != SQL_SUCCESS) {
dbd_error(sth, rc, "odbc_get_special_columns/SQLAllocHandle(stmt)");
return 0;
}
rc = SQLSpecialColumns(imp_sth->hstmt,
(SQLSMALLINT)Identifier,
CatalogName, (SQLSMALLINT)strlen(CatalogName),
SchemaName, (SQLSMALLINT)strlen(SchemaName),
TableName, (SQLSMALLINT)strlen(TableName),
(SQLSMALLINT)Scope, (SQLSMALLINT)Nullable);
if (!SQL_SUCCEEDED(rc)) {
dbd_error(sth, rc, "odbc_get_special_columns/SQLSpecialClumns");
return 0;
}
return build_results(sth, dbh, rc);
}
int
odbc_get_foreign_keys(dbh, sth, PK_CatalogName, PK_SchemaName, PK_TableName, FK_CatalogName, FK_SchemaName, FK_TableName)
SV * dbh;
SV * sth;
char * PK_CatalogName;
char * PK_SchemaName;
char * PK_TableName;
char * FK_CatalogName;
char * FK_SchemaName;
char * FK_TableName;
{
dTHR;
D_imp_dbh(dbh);
D_imp_sth(sth);
RETCODE rc;
int dbh_active;
imp_sth->henv = imp_dbh->henv; /* needed for dbd_error */
imp_sth->hdbc = imp_dbh->hdbc;
imp_sth->done_desc = 0;
if ((dbh_active = check_connection_active(dbh)) == 0) return 0;
rc = SQLAllocHandle(SQL_HANDLE_STMT, imp_dbh->hdbc, &imp_sth->hstmt);
if (rc != SQL_SUCCESS) {
dbd_error(sth, rc, "odbc_get_foreign_keys/SQLAllocHandle(stmt)");
return 0;
}
/* just for sanity, later. Any internals that may rely on this (including */
/* debugging) will have valid data */
imp_sth->statement = (char *)safemalloc(strlen(cSqlForeignKeys)+
strlen(XXSAFECHAR(PK_CatalogName))+
strlen(XXSAFECHAR(PK_SchemaName))+
strlen(XXSAFECHAR(PK_TableName))+
strlen(XXSAFECHAR(FK_CatalogName))+
strlen(XXSAFECHAR(FK_SchemaName))+
strlen(XXSAFECHAR(FK_TableName))+
1);
sprintf(imp_sth->statement,
cSqlForeignKeys,
XXSAFECHAR(PK_CatalogName), XXSAFECHAR(PK_SchemaName),XXSAFECHAR(PK_TableName),
XXSAFECHAR(FK_CatalogName), XXSAFECHAR(FK_SchemaName),XXSAFECHAR(FK_TableName)
);
/* fix to handle "" (undef) calls -- thanks to Kevin Shepherd */
rc = SQLForeignKeys(
imp_sth->hstmt,
(PK_CatalogName && *PK_CatalogName) ? PK_CatalogName : 0, SQL_NTS,
(PK_SchemaName && *PK_SchemaName) ? PK_SchemaName : 0, SQL_NTS,
(PK_TableName && *PK_TableName) ? PK_TableName : 0, SQL_NTS,
(FK_CatalogName && *FK_CatalogName) ? FK_CatalogName : 0, SQL_NTS,
(FK_SchemaName && *FK_SchemaName) ? FK_SchemaName : 0, SQL_NTS,
(FK_TableName && *FK_TableName) ? FK_TableName : 0, SQL_NTS);
if (!SQL_SUCCEEDED(rc)) {
dbd_error(sth, rc, "odbc_get_foreign_keys/SQLForeignKeys");
return 0;
}
return build_results(sth, dbh, rc);
}
int
odbc_describe_col(sth, colno, ColumnName, BufferLength, NameLength, DataType, ColumnSize, DecimalDigits, Nullable)
SV *sth;
int colno;
char *ColumnName;
I16 BufferLength;
I16 *NameLength;
I16 *DataType;
U32 *ColumnSize;
I16 *DecimalDigits;
I16 *Nullable;
{
D_imp_sth(sth);
SQLULEN ColSize;
RETCODE rc;
rc = SQLDescribeCol(imp_sth->hstmt, (SQLSMALLINT)colno,
ColumnName, BufferLength, NameLength,
DataType, &ColSize, DecimalDigits, Nullable);
if (!SQL_SUCCEEDED(rc)) {
dbd_error(sth, rc, "DescribeCol/SQLDescribeCol");
return 0;
}
*ColumnSize = (U32)ColSize;
return 1;
}
int
odbc_get_type_info(dbh, sth, ftype)
SV *dbh;
SV *sth;
int ftype;
{
dTHR;
D_imp_dbh(dbh);
D_imp_sth(sth);
RETCODE rc;
int dbh_active;
#if 0
/* TBD: cursorname? */
char cname[128]; /* cursorname */
#endif
imp_sth->henv = imp_dbh->henv; /* needed for dbd_error */
imp_sth->hdbc = imp_dbh->hdbc;
imp_sth->done_desc = 0;
if ((dbh_active = check_connection_active(dbh)) == 0) return 0;
rc = SQLAllocHandle(SQL_HANDLE_STMT, imp_dbh->hdbc, &imp_sth->hstmt);
if (rc != SQL_SUCCESS) {
dbd_error(sth, rc, "odbc_get_type_info/SQLAllocHandle(stmt)");
return 0;
}
/* just for sanity, later. Any internals that may rely on this (including */
/* debugging) will have valid data */
imp_sth->statement = (char *)safemalloc(strlen(cSqlGetTypeInfo)+ftype/10+1);
sprintf(imp_sth->statement, cSqlGetTypeInfo, ftype);
rc = SQLGetTypeInfo(imp_sth->hstmt, (SQLSMALLINT)ftype);
dbd_error(sth, rc, "odbc_get_type_info/SQLGetTypeInfo");
if (!SQL_SUCCEEDED(rc)) {
SQLFreeHandle(SQL_HANDLE_STMT,imp_sth->hstmt);
/* SQLFreeStmt(imp_sth->hstmt, SQL_DROP);*/ /* TBD: 3.0 update */
imp_sth->hstmt = SQL_NULL_HSTMT;
return 0;
}
return build_results(sth, dbh, rc);
}
SV *
odbc_cancel(sth)
SV *sth;
{
dTHR;
D_imp_sth(sth);
RETCODE rc;
rc = SQLCancel(imp_sth->hstmt);
if (!SQL_SUCCEEDED(rc)) {
dbd_error(sth, rc, "odbc_cancel/SQLCancel");
return Nullsv;
}
return newSViv(1);
}
/************************************************************************/
/* */
/* odbc_col_attributes */
/* =================== */
/* */
/************************************************************************/
SV *odbc_col_attributes(SV *sth, int colno, int desctype)
{
dTHR;
D_imp_sth(sth);
RETCODE rc;
SV *retsv = NULL;
unsigned char str_attr[512];
SWORD str_attr_len = 0;
SQLLEN num_attr = 0;
memset(str_attr, '\0', sizeof(str_attr));
if ( !DBIc_ACTIVE(imp_sth) ) {
dbd_error(sth, SQL_ERROR, "no statement executing");
return Nullsv;
}
/*
* At least on Win95, calling this with colno==0 would "core" dump/GPF.
* protect, even though it's valid for some values of desctype
* (e.g. SQL_COLUMN_COUNT, since it doesn't depend on the colcount)
*/
if (colno == 0) {
dbd_error(sth, SQL_ERROR,
"cannot obtain SQLColAttributes for column 0");
return Nullsv;
}
/*
* workaround a problem in unixODBC 2.2.11 which can write off the
* end of the str_attr buffer when built with unicode - lie about
* buffer size - we've got more than we admit to.
*/
rc = SQLColAttributes(imp_sth->hstmt, (SQLUSMALLINT)colno,
(SQLUSMALLINT)desctype,
str_attr, sizeof(str_attr)/2,
&str_attr_len, &num_attr);
if (!SQL_SUCCEEDED(rc)) {
dbd_error(sth, rc, "odbc_col_attributes/SQLColAttributes");
return Nullsv;
} else if (SQL_SUCCESS_WITH_INFO == rc) {
warn("SQLColAttributes has truncated returned data");
}
if (DBIc_TRACE(imp_sth, 0, 0, 3)) {
PerlIO_printf(
DBIc_LOGPIO(imp_sth),
" SQLColAttributes: colno=%d, desctype=%d, str_attr=%s, "
"str_attr_len=%d, num_attr=%ld",
colno, desctype, str_attr, str_attr_len, (long)num_attr);
}
switch (desctype) {
case SQL_COLUMN_AUTO_INCREMENT:
case SQL_COLUMN_CASE_SENSITIVE:
case SQL_COLUMN_COUNT:
case SQL_COLUMN_DISPLAY_SIZE:
case SQL_COLUMN_LENGTH:
case SQL_COLUMN_MONEY:
case SQL_COLUMN_NULLABLE:
case SQL_COLUMN_PRECISION:
case SQL_COLUMN_SCALE:
case SQL_COLUMN_SEARCHABLE:
case SQL_COLUMN_TYPE:
case SQL_COLUMN_UNSIGNED:
case SQL_COLUMN_UPDATABLE:
{
retsv = newSViv(num_attr);
break;
}
case SQL_COLUMN_LABEL:
case SQL_COLUMN_NAME:
case SQL_COLUMN_OWNER_NAME:
case SQL_COLUMN_QUALIFIER_NAME:
case SQL_COLUMN_TABLE_NAME:
case SQL_COLUMN_TYPE_NAME:
{
/*
* NOTE: in unixODBC 2.2.11, if you called SQLDriverConnectW and
* then called SQLColAttributes for a string type it would often
* return half the number of characters it had written to
* str_attr in str_attr_len.
*/
retsv = newSVpv(str_attr, strlen(str_attr));
break;
}
default:
{
dbd_error(sth, SQL_ERROR,
"driver-specific column attributes not supported");
return Nullsv;
break;
}
}
#ifdef OLD_STUFF_THAT_SEEMS_FLAWED
/*
* sigh...Oracle's ODBC driver version 8.0.4 resets str_attr_len to 0, when
* putting a value in num_attr. This is a change!
*
* double sigh. SQL Server (and MySql under Unix) set str_attr_len
* but use num_attr, not str_attr. This change may be problematic
* for other drivers. (the additional || num_attr != -2...)
*/
if (str_attr_len == -2 || str_attr_len == 0 || num_attr != -2)
retsv = newSViv(num_attr);
else if (str_attr_len != 2 && str_attr_len != 4)
retsv = newSVpv(str_attr, 0);
else if (str_attr[str_attr_len] == '\0') /* fix for DBD::ODBC 0.39 thanks to Nicolas DeRico */
retsv = newSVpv(str_attr, 0);
else {
if (str_attr_len == 2)
retsv = newSViv(*(short *)str_attr);
else
retsv = newSViv(*(int *)str_attr);
}
#endif
return sv_2mortal(retsv);
}
int
odbc_db_columns(dbh, sth, catalog, schema, table, column)
SV *dbh;
SV *sth;
char *catalog;
char *schema;
char *table;
char *column;
{
dTHR;
D_imp_dbh(dbh);
D_imp_sth(sth);
RETCODE rc;
int dbh_active;
imp_sth->henv = imp_dbh->henv; /* needed for dbd_error */
imp_sth->hdbc = imp_dbh->hdbc;
imp_sth->done_desc = 0;
if ((dbh_active = check_connection_active(dbh)) == 0) return 0;
rc = SQLAllocHandle(SQL_HANDLE_STMT, imp_dbh->hdbc, &imp_sth->hstmt);
if (rc != SQL_SUCCESS) {
dbd_error(sth, rc, "odbc_db_columns/SQLAllocHandle(stmt)");
return 0;
}
/* just for sanity, later. Any internals that may rely on this (including */
/* debugging) will have valid data */
imp_sth->statement = (char *)safemalloc(strlen(cSqlColumns)+
strlen(XXSAFECHAR(catalog))+
strlen(XXSAFECHAR(schema))+
strlen(XXSAFECHAR(table))+
strlen(XXSAFECHAR(column))+1);
sprintf(imp_sth->statement,
cSqlColumns, XXSAFECHAR(catalog), XXSAFECHAR(schema),
XXSAFECHAR(table), XXSAFECHAR(column));
rc = SQLColumns(imp_sth->hstmt,
(catalog && *catalog) ? catalog : 0, SQL_NTS,
(schema && *schema) ? schema : 0, SQL_NTS,
(table && *table) ? table : 0, SQL_NTS,
(column && *column) ? column : 0, SQL_NTS);
if (DBIc_TRACE(imp_sth, 0, 0, 3))
PerlIO_printf(
DBIc_LOGPIO(imp_dbh),
" SQLColumns call: cat = %s, schema = %s, table = %s, "
"column = %s\n",
XXSAFECHAR(catalog), XXSAFECHAR(schema), XXSAFECHAR(table),
XXSAFECHAR(column));
dbd_error(sth, rc, "odbc_columns/SQLColumns");
if (!SQL_SUCCEEDED(rc)) {
SQLFreeHandle(SQL_HANDLE_STMT,imp_sth->hstmt);
/* SQLFreeStmt(imp_sth->hstmt, SQL_DROP);*/ /* TBD: 3.0 update */
imp_sth->hstmt = SQL_NULL_HSTMT;
return 0;
}
return build_results(sth, dbh, rc);
}
/*
* AllODBCErrors
* =============
*
* Given ODBC environment, connection and statement handles (any of which may
* be null) this function will retrieve all ODBC errors recorded and
* optionally (if output is not 0) output then to the specified log handle.
*
*/
static void AllODBCErrors(
HENV henv, HDBC hdbc, HSTMT hstmt, int output, PerlIO *logfp)
{
SQLRETURN rc;
do {
UCHAR sqlstate[SQL_SQLSTATE_SIZE+1];
/* ErrorMsg must not be greater than SQL_MAX_MESSAGE_LENGTH */
UCHAR ErrorMsg[SQL_MAX_MESSAGE_LENGTH];
SWORD ErrorMsgLen;
SDWORD NativeError;
/* TBD: 3.0 update */
rc=SQLError(henv, hdbc, hstmt,
sqlstate, &NativeError,
ErrorMsg, sizeof(ErrorMsg)-1, &ErrorMsgLen);
if (output && SQL_SUCCEEDED(rc))
PerlIO_printf(logfp, "%s %s\n", sqlstate, ErrorMsg);
} while(SQL_SUCCEEDED(rc));
return;
}
/************************************************************************/
/* */
/* check_connection_active */
/* ======================= */
/* */
/************************************************************************/
static int check_connection_active(SV *h)
{
D_imp_xxh(h);
struct imp_dbh_st *imp_dbh = NULL;
struct imp_sth_st *imp_sth = NULL;
switch(DBIc_TYPE(imp_xxh)) {
case DBIt_ST:
imp_sth = (struct imp_sth_st *)imp_xxh;
imp_dbh = (struct imp_dbh_st *)(DBIc_PARENT_COM(imp_sth));
break;
case DBIt_DB:
imp_dbh = (struct imp_dbh_st *)imp_xxh;
break;
default:
croak("panic: check_connection_active bad handle type");
}
if (!DBIc_ACTIVE(imp_dbh)) {
DBIh_SET_ERR_CHAR(
h, imp_xxh, Nullch, 1,
"Cannot allocate statement when disconnected from the database",
"08003", Nullch);
return 0;
}
return 1;
}
/************************************************************************/
/* */
/* set_odbc_version */
/* ================ */
/* */
/* Set the ODBC version we require. This defaults to ODBC 3 but if */
/* attr contains the odbc_version atttribute this overrides it. If we */
/* fail for any reason the env handle is freed, the error reported and */
/* 0 is returned. If all ok, 1 is returned. */
/* */
/************************************************************************/
static int set_odbc_version(
SV *dbh,
imp_dbh_t *imp_dbh,
SV* attr)
{
D_imp_drh_from_dbh;
SV **svp;
UV odbc_version = 0;
SQLRETURN rc;
DBD_ATTRIB_GET_IV(
attr, "odbc_version", 12, svp, odbc_version);
if (svp && odbc_version) {
rc = SQLSetEnvAttr(imp_drh->henv, SQL_ATTR_ODBC_VERSION,
(SQLPOINTER)odbc_version, SQL_IS_INTEGER);
} else {
/* make sure we request a 3.0 version */
rc = SQLSetEnvAttr(imp_drh->henv, SQL_ATTR_ODBC_VERSION,
(SQLPOINTER)SQL_OV_ODBC3, SQL_IS_INTEGER);
}
if (!SQL_SUCCEEDED(rc)) {
dbd_error2(
dbh, rc, "db_login/SQLSetEnvAttr", imp_drh->henv, 0, 0);
if (imp_drh->connects == 0) {
SQLFreeHandle(SQL_HANDLE_ENV, imp_drh->henv);
imp_drh->henv = SQL_NULL_HENV;
}
return 0;
}
return 1;
}
/************************************************************************/
/* */
/* post_connect */
/* ============ */
/* */
/* Operations to perform immediately after we have connected. */
/* */
/* NOTE: prior to DBI subversion version 11605 (fixed post 1.607) */
/* DBD_ATTRIB_DELETE segfaulted so instead of calling: */
/* DBD_ATTRIB_DELETE(attr, "odbc_cursortype", */
/* strlen("odbc_cursortype")); */
/* we do the following: */
/* hv_delete((HV*)SvRV(attr), "odbc_cursortype", */
/* strlen("odbc_cursortype"), G_DISCARD); */
/* */
/************************************************************************/
static int post_connect(
SV *dbh,
imp_dbh_t *imp_dbh,
SV *attr)
{
D_imp_drh_from_dbh;
SQLRETURN rc;
SWORD dbvlen;
UWORD supported;
if (DBIc_TRACE(imp_dbh, 0x04000000, 0, 0))
TRACE0(imp_dbh, "Turning autocommit on\n");
/* DBI spec requires AutoCommit on */
rc = SQLSetConnectAttr(imp_dbh->hdbc, SQL_AUTOCOMMIT,
(SQLPOINTER)SQL_AUTOCOMMIT_ON, 0);
if (!SQL_SUCCEEDED(rc)) {
dbd_error(dbh, rc, "post_connect/SQLSetConnectAttr(SQL_AUTOCOMMIT)");
SQLFreeHandle(SQL_HANDLE_DBC, imp_dbh->hdbc);
if (imp_drh->connects == 0) {
SQLFreeHandle(SQL_HANDLE_ENV, imp_drh->henv);
imp_drh->henv = SQL_NULL_HENV;
imp_dbh->henv = SQL_NULL_HENV; /* needed for dbd_error */
}
return 0;
}
DBIc_set(imp_dbh,DBIcf_AutoCommit, 1);
/* get the ODBC compatibility level for this driver */
rc = SQLGetInfo(imp_dbh->hdbc, SQL_DRIVER_ODBC_VER, &imp_dbh->odbc_ver,
(SWORD)sizeof(imp_dbh->odbc_ver), &dbvlen);
if (!SQL_SUCCEEDED(rc)) {
dbd_error(dbh, rc, "post_connect/SQLGetInfo(DRIVER_ODBC_VER)");
strcpy(imp_dbh->odbc_ver, "01.00");
}
if (DBIc_TRACE(imp_dbh, 0x04000000, 0, 0))
TRACE1(imp_dbh, "DRIVER_ODBC_VER = %s\n", imp_dbh->odbc_ver);
/* get ODBC driver name and version */
rc = SQLGetInfo(imp_dbh->hdbc, SQL_DRIVER_NAME, &imp_dbh->odbc_driver_name,
(SQLSMALLINT)sizeof(imp_dbh->odbc_driver_name), &dbvlen);
if (!SQL_SUCCEEDED(rc)) {
dbd_error(dbh, rc, "post_connect/SQLGetInfo(DRIVER_NAME)");
strcpy(imp_dbh->odbc_driver_name, "unknown");
imp_dbh->driver_type = DT_DONT_CARE;
} else {
if (strcmp(imp_dbh->odbc_driver_name, "SQLSRV32.DLL") == 0) {
imp_dbh->driver_type = DT_SQL_SERVER;
} else if ((strcmp(imp_dbh->odbc_driver_name, "sqlncli10.dll") == 0) ||
(strcmp(imp_dbh->odbc_driver_name, "SQLNCLI.DLL") == 0)) {
imp_dbh->driver_type = DT_SQL_SERVER_NATIVE_CLIENT;
} else if (strcmp(imp_dbh->odbc_driver_name, "odbcjt32.dll") == 0) {
imp_dbh->driver_type = DT_MS_ACCESS_JET;
} else if (strcmp(imp_dbh->odbc_driver_name, "ACEODBC.DLL") == 0) {
imp_dbh->driver_type = DT_MS_ACCESS_ACE;
} else if (strcmp(imp_dbh->odbc_driver_name, "esoobclient") == 0) {
imp_dbh->driver_type = DT_ES_OOB;
} else {
imp_dbh->driver_type = DT_DONT_CARE;
}
}
if (DBIc_TRACE(imp_dbh, 0x04000000, 0, 0))
TRACE1(imp_dbh, "DRIVER_NAME = %s\n", imp_dbh->odbc_driver_name);
rc = SQLGetInfo(imp_dbh->hdbc, SQL_DRIVER_VER,
&imp_dbh->odbc_driver_version,
(SQLSMALLINT)sizeof(imp_dbh->odbc_driver_version), &dbvlen);
if (!SQL_SUCCEEDED(rc)) {
dbd_error(dbh, rc, "post_connect/SQLGetInfo(DRIVER_VERSION)");
strcpy(imp_dbh->odbc_driver_name, "unknown");
}
if (DBIc_TRACE(imp_dbh, 0x04000000, 0, 0))
TRACE1(imp_dbh, "DRIVER_VERSION = %s\n", imp_dbh->odbc_driver_version);
rc = SQLGetInfo(imp_dbh->hdbc, SQL_DBMS_NAME, &imp_dbh->odbc_dbms_name,
(SQLSMALLINT)sizeof(imp_dbh->odbc_dbms_name), &dbvlen);
if (!SQL_SUCCEEDED(rc)) {
dbd_error(dbh, rc, "post_connect/SQLGetInfo(SQL_DBMS_NAME)");
strcpy(imp_dbh->odbc_dbms_name, "unknown");
}
rc = SQLGetInfo(imp_dbh->hdbc, SQL_DBMS_VER, &imp_dbh->odbc_dbms_version,
(SQLSMALLINT)sizeof(imp_dbh->odbc_dbms_version), &dbvlen);
if (!SQL_SUCCEEDED(rc)) {
dbd_error(dbh, rc, "post_connect/SQLGetInfo(SQL_DBMS_VER)");
strcpy(imp_dbh->odbc_dbms_version, "unknown");
}
/* find maximum column name length */
rc = SQLGetInfo(imp_dbh->hdbc, SQL_MAX_COLUMN_NAME_LEN,
&imp_dbh->max_column_name_len,
(SWORD) sizeof(imp_dbh->max_column_name_len), &dbvlen);
if (!SQL_SUCCEEDED(rc)) {
dbd_error(dbh, rc, "post_connect/SQLGetInfo(MAX_COLUMN_NAME_LEN)");
imp_dbh->max_column_name_len = 256;
} else if (imp_dbh->max_column_name_len == 0) {
imp_dbh->max_column_name_len = 256;
} else {
if (DBIc_TRACE(imp_dbh, 0x04000000, 0, 0))
TRACE1(imp_dbh, "MAX_COLUMN_NAME_LEN = %d\n",
imp_dbh->max_column_name_len);
}
#ifdef WITH_UNICODE
imp_dbh->max_column_name_len = imp_dbh->max_column_name_len *
sizeof(SQLWCHAR) + 2;
#endif
if (imp_dbh->max_column_name_len > 512) {
imp_dbh->max_column_name_len = 512;
DBIh_SET_ERR_CHAR(
dbh, (imp_xxh_t*)imp_drh, "0", 1,
"Max column name length pegged at 512", Nullch, Nullch);
}
/* default ignoring named parameters to false */
imp_dbh->odbc_ignore_named_placeholders = 0;
#ifdef WITH_UNICODE
imp_dbh->odbc_has_unicode = 1;
#else
imp_dbh->odbc_has_unicode = 0;
#endif
if (DBIc_TRACE(imp_dbh, 0x04000000, 0, 0))
TRACE1(imp_dbh, "DBD::ODBC is unicode built : %s\n",
imp_dbh->odbc_has_unicode ? "YES" : "NO");
imp_dbh->odbc_default_bind_type = 0;
imp_dbh->odbc_force_bind_type = 0;
/* flag to see if SQLDescribeParam is supported */
imp_dbh->odbc_sqldescribeparam_supported = -1;
/* flag to see if SQLDescribeParam is supported */
imp_dbh->odbc_sqlmoreresults_supported = -1;
imp_dbh->odbc_defer_binding = 0;
imp_dbh->odbc_force_rebind = 0;
/* default value for query timeout is -1 which means do not set the
query timeout at all. */
imp_dbh->odbc_query_timeout = -1;
imp_dbh->odbc_putdata_start = 32768;
imp_dbh->odbc_column_display_size = 2001;
imp_dbh->odbc_utf8_on = 0;
imp_dbh->odbc_exec_direct = 0; /* default to not having SQLExecDirect used */
imp_dbh->RowCacheSize = 1; /* default value for now */
if (!strcmp(imp_dbh->odbc_dbms_name, "Microsoft SQL Server")) {
if (DBIc_TRACE(imp_dbh, 0x04000000, 0, 0))
TRACE0(imp_dbh, "Deferring Binding\n");
imp_dbh->odbc_defer_binding = 0;
}
/* check to see if SQLMoreResults is supported */
rc = SQLGetFunctions(imp_dbh->hdbc, SQL_API_SQLMORERESULTS, &supported);
if (SQL_SUCCEEDED(rc)) {
if (DBIc_TRACE(imp_dbh, 0x04000000, 0, 0))
TRACE1(imp_dbh, "SQLMoreResults supported: %d\n", supported);
imp_dbh->odbc_sqlmoreresults_supported = supported ? 1 : 0;
} else {
imp_dbh->odbc_sqlmoreresults_supported = 0;
if (DBIc_TRACE(imp_dbh, 0x04000000, 0, 0))
TRACE0(imp_dbh,
" !!SQLGetFunctions(SQL_API_SQLMORERESULTS) failed:\n");
AllODBCErrors(imp_dbh->henv, imp_dbh->hdbc, 0,
DBIc_TRACE(imp_dbh, 0, 0, 3), DBIc_LOGPIO(imp_dbh));
}
/* call only once per connection / DBH -- may want to do
* this during the connect to avoid potential threading
* issues */
/* check to see if SQLDescribeParam is supported */
rc = SQLGetFunctions(imp_dbh->hdbc, SQL_API_SQLDESCRIBEPARAM, &supported);
if (SQL_SUCCEEDED(rc)) {
if (DBIc_TRACE(imp_dbh, 0x04000000, 0, 0))
TRACE1(imp_dbh, "SQLDescribeParam supported: %d\n", supported);
imp_dbh->odbc_sqldescribeparam_supported = supported ? 1 : 0;
} else {
imp_dbh->odbc_sqldescribeparam_supported = 0;
if (DBIc_TRACE(imp_dbh, 0x04000000, 0, 0))
TRACE0(imp_dbh,
" !!SQLGetFunctions(SQL_API_SQLDESCRIBEPARAM) failed:\n");
AllODBCErrors(imp_dbh->henv, imp_dbh->hdbc, 0,
DBIc_TRACE(imp_dbh, 0, 0, 3), DBIc_LOGPIO(imp_dbh));
}
if (DBIc_TRACE(imp_dbh, 0x04000000, 0, 0))
TRACE1(imp_dbh, "SQLDescribeParam supported: %d\n",
imp_dbh->odbc_sqldescribeparam_supported);
/* odbc_cursortype */
{
SV **svp;
UV odbc_cursortype = 0;
DBD_ATTRIB_GET_IV(attr, "odbc_cursortype", 15,
svp, odbc_cursortype);
if (svp && odbc_cursortype) {
if (DBIc_TRACE(imp_dbh, 0x04000000, 0, 0))
TRACE1(imp_dbh,
" Setting cursor type to: %"UVuf"\n", odbc_cursortype);
/* delete odbc_cursortype so we don't see it again via STORE */
hv_delete((HV*)SvRV(attr), "odbc_cursortype",
strlen("odbc_cursortype"), G_DISCARD);
rc = SQLSetConnectAttr(imp_dbh->hdbc,(SQLINTEGER)SQL_CURSOR_TYPE,
(SQLPOINTER)odbc_cursortype,
(SQLINTEGER)SQL_IS_INTEGER);
if (!SQL_SUCCEEDED(rc) && (DBIc_TRACE(imp_dbh, 0x04000000, 0, 0)))
TRACE1(imp_dbh, " !!Failed to set SQL_CURSORTYPE to %d\n",
(int)odbc_cursortype);
}
}
/* odbc_query_timeout */
{
SV **svp;
UV odbc_timeout = 0;
DBD_ATTRIB_GET_IV(
attr, "odbc_query_timeout", strlen("odbc_query_timeout"),
svp, odbc_timeout);
if (svp && odbc_timeout) {
imp_dbh->odbc_query_timeout = odbc_timeout;
if (DBIc_TRACE(imp_dbh, 0x04000000, 0, 0))
TRACE1(imp_dbh, " Setting DBH query timeout to %d\n",
(int)odbc_timeout);
/* delete odbc_cursortype so we don't see it again via STORE */
hv_delete((HV*)SvRV(attr), "odbc_query_timeout",
strlen("odbc_query_timeout"), G_DISCARD);
}
}
/* odbc_putdata_start */
{
SV **svp;
IV putdata_start_value;
DBD_ATTRIB_GET_IV(
attr, "odbc_putdata_start", strlen("odbc_putdata_start"),
svp, putdata_start_value);
if (svp) {
imp_dbh->odbc_putdata_start = putdata_start_value;
if (DBIc_TRACE(imp_dbh, 0x04000000, 0, 0))
TRACE1(imp_dbh, " Setting DBH putdata_start to %d\n",
(int)putdata_start_value);
/* delete odbc_putdata_start so we don't see it again via STORE */
hv_delete((HV*)SvRV(attr), "odbc_putdata_start",
strlen("odbc_putdata_start"), G_DISCARD);
}
}
/* odbc_column_display_size */
{
SV **svp;
IV column_display_size_value;
DBD_ATTRIB_GET_IV(
attr, "odbc_column_display_size",
strlen("odbc_column_display_size"),
svp, column_display_size_value);
if (svp) {
imp_dbh->odbc_column_display_size = column_display_size_value;
if (DBIc_TRACE(imp_dbh, 0x04000000, 0, 0))
TRACE1(imp_dbh,
" Setting DBH default column display size to %d\n",
(int)column_display_size_value);
/* delete odbc_column_display_size so we don't see it again via STORE */
hv_delete((HV*)SvRV(attr), "odbc_column_display_size",
strlen("odbc_column_display_size"), G_DISCARD);
}
}
/* odbc_utf8_on */
{
SV **svp;
IV column_display_size_value;
DBD_ATTRIB_GET_IV(
attr, "odbc_utf8_on",
strlen("odbc_utf8_on"),
svp, column_display_size_value);
if (svp) {
imp_dbh->odbc_utf8_on = 0;
if (DBIc_TRACE(imp_dbh, 0x04000000, 0, 0))
TRACE1(imp_dbh,
" Setting UTF8_ON to %d\n",
(int)column_display_size_value);
/* delete odbc_utf8_on so we don't see it again via STORE */
hv_delete((HV*)SvRV(attr), "odbc_utf8_on",
strlen("odbc_utf8_on"), G_DISCARD);
}
}
return 1;
}
static SQLSMALLINT default_parameter_type(imp_sth_t *imp_sth, phs_t *phs)
{
if (imp_sth->odbc_default_bind_type != 0) {
return imp_sth->odbc_default_bind_type;
} else {
/* MS Access can return an invalid precision error in the 12blob
test unless the large value is bound as an SQL_LONGVARCHAR
or SQL_WLONGVARCHAR. Who knows what large is, but for now it is
4000 */
if (!SvOK(phs->sv)) {
return ODBC_BACKUP_BIND_TYPE_VALUE;
} else if (SvCUR(phs->sv) > 4000) {
return ODBC_BACKUP_LONG_BIND_TYPE_VALUE;
} else {
return ODBC_BACKUP_BIND_TYPE_VALUE;
}
}
}
/* end */
|