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
|
/*
Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.DatabaseMetaDataTest
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language go1481
verning permissions and
limitations under the License.
*/
package org.apache.derbyTesting.functionTests.tests.jdbcapi;
import java.io.IOException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Properties;
import java.util.Random;
import java.util.StringTokenizer;
import junit.framework.Test;
import org.apache.derbyTesting.functionTests.tests.upgradeTests.Version;
import org.apache.derbyTesting.functionTests.util.Barrier;
import org.apache.derbyTesting.junit.BaseJDBCTestCase;
import org.apache.derbyTesting.junit.BaseTestSuite;
import org.apache.derbyTesting.junit.DatabasePropertyTestSetup;
import org.apache.derbyTesting.junit.JDBC;
import org.apache.derbyTesting.junit.TestConfiguration;
/**
* Test the DatabaseMetaData api.
* <P>
* For a number of methods that return a ResultSet to determine the
* attributes of SQL objects (e.g. getTables) two methods
* are provided. A non-modify and a modify one.
*
* <BR>
* The non-modify method tests that the getXXX method call works.
* This can be used by other tests where issues have been seen
* with database meta data, such as upgrade and read-only databases.
* The non-modify means that the test method does not change the database
* in order to test the return of the getXXX method is correct.
* <BR>
* <P>
* This test is also called from the upgrade tests to test that
* metadata continues to work at various points in the upgrade.
*/
public class DatabaseMetaDataTest extends BaseJDBCTestCase {
/*
** Escaped function testing
*/
private static final String[][] NUMERIC_FUNCTIONS =
{
// Section C.1 JDBC 3.0 spec.
{ "ABS", "-25.67" },
{ "ACOS", "0.0707" },
{ "ASIN", "0.997" },
{ "ATAN", "14.10" },
{ "ATAN2", "0.56", "1.2" },
{ "CEILING", "3.45" },
{ "COS", "1.2" },
{ "COT", "3.4" },
{ "DEGREES", "2.1" },
{ "EXP", "2.3" },
{ "FLOOR", "3.22" },
{ "LOG", "34.1" },
{ "LOG10", "18.7" },
{ "MOD", "124", "7" },
{ "PI" },
{ "POWER", "2", "3" },
{ "RADIANS", "54" },
{ "RAND", "17" },
{ "ROUND", "345.345", "1" },
{ "SIGN", "-34" },
{ "SIN", "0.32" },
{ "SQRT", "6.22" },
{ "TAN", "0.57", },
{ "TRUNCATE", "345.395", "1" }
};
private static final String[][] TIMEDATE_FUNCTIONS =
{
// Section C.3 JDBC 3.0 spec.
{ "CURDATE" },
{ "CURTIME" },
{ "DAYNAME", "{d '1995-12-19'h}" },
{ "DAYOFMONTH", "{d '1995-12-19'}" },
{ "DAYOFWEEK", "{d '1995-12-19'}" },
{ "DAYOFYEAR", "{d '1995-12-19'}" },
{ "HOUR", "{t '16:13:03'}" },
{ "MINUTE", "{t '16:13:03'}" },
{ "MONTH", "{d '1995-12-19'}" },
{ "MONTHNAME", "{d '1995-12-19'}" },
{ "NOW" },
{ "QUARTER", "{d '1995-12-19'}" },
{ "SECOND", "{t '16:13:03'}" },
{ "TIMESTAMPADD", "SQL_TSI_DAY", "7", "{ts '1995-12-19 12:15:54'}" },
{ "TIMESTAMPDIFF", "SQL_TSI_DAY", "{ts '1995-12-19 12:15:54'}", "{ts '1997-11-02 00:15:23'}" },
{ "WEEK", "{d '1995-12-19'}" },
{ "YEAR", "{d '1995-12-19'}" },
};
private static final String[][] SYSTEM_FUNCTIONS =
{
// Section C.4 JDBC 3.0 spec.
{ "DATABASE" },
{ "IFNULL", "'this'", "'that'" },
{ "USER"},
};
private static final String[][] STRING_FUNCTIONS =
{
// Section C.2 JDBC 3.0 spec.
{ "ASCII" , "'Yellow'" },
{ "CHAR", "65" },
{ "CONCAT", "'hello'", "'there'" },
{ "DIFFERENCE", "'Pires'", "'Piers'" },
{ "INSERT", "'Bill Clinton'", "4", "'William'" },
{ "LCASE", "'Fernando Alonso'" },
{ "LEFT", "'Bonjour'", "3" },
{ "LENGTH", "'four '" } ,
{ "LOCATE", "'jour'", "'Bonjour'" },
{ "LTRIM", "' left trim '"},
{ "REPEAT", "'echo'", "3" },
{ "REPLACE", "'to be or not to be'", "'be'", "'England'" },
{ "RTRIM", "' right trim '"},
{ "SOUNDEX", "'Derby'" },
{ "SPACE", "12"},
{ "SUBSTRING", "'Ruby the Rubicon Jeep'", "10", "7", },
{ "UCASE", "'Fernando Alonso'" }
};
/**
* Did the test modifiy the database.
*/
private boolean modifiedDatabase;
/**
* The schema used by the test.
* <p>
* Added to avoid other tests interfering with our metadata queries.
* Configured by running the test with a specific user, i.e. use
* {@linkplain TestConfiguration#changeUserDecorator}.
*/
private String schema;
public DatabaseMetaDataTest(String name) {
super(name);
}
/**
* Set the schema name to be used by the test.
* @throws java.lang.Exception
*/
@Override
protected void setUp()
throws Exception {
// Currently there are no tests that depend on data created outside
// of the test ficture itself. This means we can relax the retrictions
// on the user name, and thus schema; we don't need to keep it the same
// over all test fixtures.
schema = TestConfiguration.getCurrent().getUserName();
// Only a prerequisite for one test, but enforce it here (fail-fast).
assertTrue("schema name must be at least three characters long",
schema.length() > 2);
}
@Override
protected void tearDown() throws Exception
{
if (modifiedDatabase)
{
Connection conn = getConnection();
conn.setAutoCommit(false);
DatabaseMetaData dmd = getDMD();
for (String IDS1 : IDS) {
JDBC.dropSchema(dmd, getStoredIdentifier(IDS1));
}
commit();
}
super.tearDown();
}
/**
* Default suite for running this test.
* @return the suite
*/
public static Test suite() {
BaseTestSuite suite = new BaseTestSuite("DatabaseMetaDataTest");
suite.addTest(
TestConfiguration.defaultSuite(DatabaseMetaDataTest.class));
// Add some tests to be run with connection pooling enabled.
suite.addTest(connectionPoolingSuite("embedded"));
suite.addTest(TestConfiguration.clientServerDecorator(
connectionPoolingSuite("client")));
// Test for DERBY-2584 needs a fresh database to ensure that the
// meta-data queries haven't already been compiled. No need to run the
// test in client/server mode since it only tests the compilation of
// meta-data queries.
suite.addTest(
TestConfiguration.singleUseDatabaseDecorator(
new DatabaseMetaDataTest("initialCompilationTest")));
// The test for DERBY-4160 needs a fresh database to ensure that the
// meta-data queries haven't already been compiled.
suite.addTest(TestConfiguration.singleUseDatabaseDecorator(
new DatabaseMetaDataTest("concurrentCompilationTest")));
// Test for DERBY-3693 needs a fresh database to ensure that the size
// of SYSTABLES is so small that creating a relatively small number of
// tables will cause the query plan for getTables() to be invalidated.
// Also, set a high lock timeout explicitly so that we can check that
// an internal timeout followed by a retry didn't happen, and set
// derby.language.stalePlanCheckInterval to a low value so that the
// invalidation happens earlier.
Properties props = new Properties();
props.setProperty("derby.locks.waitTimeout", "90");
props.setProperty("derby.language.stalePlanCheckInterval", "5");
suite.addTest(
TestConfiguration.singleUseDatabaseDecorator(
new DatabasePropertyTestSetup(
new DatabaseMetaDataTest("recompileTimeoutTest"),
props, true)));
return suite;
}
/**
* Returns a suite of tests to be run with connection pooling enabled.
*
* @param jdbcClient name of the client being used (for verbosity only)
* @return A suite of tests.
*/
private static Test connectionPoolingSuite(String jdbcClient) {
// Return an empty suite if running in JavaME environment.
if (JDBC.vmSupportsJSR169()) {
return new BaseTestSuite("Base connection pooling suite:DISABLED");
}
BaseTestSuite baseCpSuite =
new BaseTestSuite("Base connection pooling suite");
// Add the tests here.
baseCpSuite.addTest(new DatabaseMetaDataTest("testConnectionSpecific"));
// Setup the two configurations; CPDS and XADS.
BaseTestSuite fullCpSuite = new BaseTestSuite(
"DatabaseMetaData with connection pooling:" + jdbcClient);
BaseTestSuite cpSuite = new BaseTestSuite("ConnectionPoolDataSource");
BaseTestSuite xaSuite = new BaseTestSuite("XADataSource");
cpSuite.addTest(TestConfiguration.connectionCPDecorator(baseCpSuite));
xaSuite.addTest(TestConfiguration.connectionXADecorator(baseCpSuite));
fullCpSuite.addTest(cpSuite);
fullCpSuite.addTest(xaSuite);
return fullCpSuite;
}
/**
* Return the identifiers used to create schemas,
* tables etc. in the order the database stores them.
* @return identifiers
*/
private String[] getSortedIdentifiers()
{
String[] dbIDS = new String[IDS.length];
// Remove any quotes from user schemas and upper case
// those without quotes.
for (int i = 0; i < IDS.length; i++)
{
dbIDS[i] = getStoredIdentifier(IDS[i]);
}
Arrays.sort(dbIDS);
return dbIDS;
}
private DatabaseMetaData getDMD() throws SQLException
{
return getConnection().getMetaData();
}
/**
* Tests that a meta-data query is compiled and stored correctly even when
* there's a lock on the system tables (DERBY-2584). This test must run on
* a fresh database (that is, <code>getIndexInfo</code> must not have been
* prepared and stored in <code>SYS.SYSSTATEMENTS</code>).
*
* @throws java.sql.SQLException
*/
public void initialCompilationTest() throws SQLException {
Connection c = getConnection();
c.setAutoCommit(false);
c.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
Statement s = createStatement();
// First get shared row locks on the SYSSTATEMENTS table.
JDBC.assertDrainResults(
s.executeQuery("SELECT * FROM SYS.SYSSTATEMENTS"));
s.close();
// Execute getIndexInfo() for the first time. Because of the shared
// locks on SYSSTATEMENTS, the query is compiled in the main
// transaction.
getDMD().getIndexInfo(null, null, "T", false, false).close();
// Re-use the previously compiled query from disk. Fails with
// ArrayIndexOutOfBoundsException before DERBY-2584.
getDMD().getIndexInfo(null, null, "T", false, false).close();
}
/**
* Test that a meta-data query is compiled and stored correctly even when
* there's a lock conflict that causes the first attempt to store it to
* stop midway (DERBY-4160). This test needs a fresh database so that the
* meta-data calls are not already compiled.
* @throws java.lang.Exception
*/
public void concurrentCompilationTest() throws Exception {
// Create a barrier that can be used to synchronize the two threads
// so they perform the meta-data compilation at the same time.
final Barrier barrier = new Barrier(2);
// Create a thread thread that attempts to compile meta-data queries.
final DatabaseMetaData dmd = getDMD();
final Exception[] exception = new Exception[1];
Thread th = new Thread() {
@Override
public void run() {
try {
concurrentCompilationTestHelper(barrier, dmd);
} catch (Exception e) {
exception[0] = e;
}
}
};
th.start();
// At the same time, in the main thread, attempt to compile the same
// meta-data queries.
Connection c2 = openDefaultConnection();
concurrentCompilationTestHelper(barrier, c2.getMetaData());
c2.close();
// Wait until both threads are done.
th.join();
// Check if the helper thread got any exceptions.
if (exception[0] != null) {
fail("Exception in other thread", exception[0]);
}
// Finally, verify that the two meta-data methods used in the test
// are working.
testGetBestRowIdentifier();
testGetIndexInfo();
}
private void concurrentCompilationTestHelper(
Barrier barrier, DatabaseMetaData dmd) throws Exception {
// Wait until the other thread is ready to start, so that the
// compilation happens at the same time in both threads.
barrier.await();
// Often, but not always, the getIndexInfo() call would fail
// in one of the threads with the following error message:
// ERROR X0Y68: Column 'PARAM1' already exists.
ResultSet rs1 = dmd.getBestRowIdentifier(null, null, "", 0, true);
ResultSet rs2 = dmd.getIndexInfo(null, null, "", true, true);
JDBC.assertDrainResults(rs1);
JDBC.assertDrainResults(rs2);
}
/**
* Tests that we don't get an internal timeout when a meta-data statement
* is recompiled because the size of the tables it queries has changed
* (DERBY-3693). The test must be run on a fresh database, to ensure that
* SYSTABLES initially has a relatively small number of records. The lock
* timeout must be high (more than 60 seconds) to enable us to see the
* difference between an internal lock timeout and slow execution.
* derby.language.stalePlanCheckInterval should be set to 5 (the lowest
* possible value) so that we don't have to wait long for the query plan
* to be invalidated.
* @throws java.sql.SQLException
*/
public void recompileTimeoutTest() throws SQLException {
DatabaseMetaData dmd = getDMD();
// Make sure getTables() is initially compiled while SYSTABLES is small
JDBC.assertDrainResults(dmd.getTables(null, "%", "%", null));
// Grow SYSTABLES
Statement s = createStatement();
for (int i = 0; i < 20; i++) {
s.executeUpdate("create table t" + i + "(x int)");
}
// Execute getTables() derby.language.stalePlanCheckInterval times so
// that its plan is invalidated. Before DERBY-3693 was fixed, the
// recompilation after the invalidation would get an internal timeout
// and take very long time to complete.
for (int i = 0; i < 5; i++) {
long time = System.currentTimeMillis();
JDBC.assertDrainResults(dmd.getTables(null, "%", "T0", null));
time = System.currentTimeMillis() - time;
if (time > 60000) {
fail("getTables() took a very long time, possibly because " +
"of an internal timeout. i=" + i + ", time=" + time);
}
}
}
/**
* Test the methods that indicate if a feature
* is supported or not. Methods start with
* 'support'. See secton 7.3 in JDBC 3.0 specification.
*
* @throws SQLException
*
*/
public void testDetermineFeatureSupport() throws SQLException
{
DatabaseMetaData dmd = getDMD();
assertTrue(dmd.supportsAlterTableWithAddColumn());
assertTrue(dmd.supportsAlterTableWithDropColumn());
/* DERBY-2243 Derby does support ANSI 92 standards
* and this behaviour is now consistant across drivers
*/
assertTrue(dmd.supportsANSI92EntryLevelSQL());
assertFalse(dmd.supportsANSI92FullSQL());
assertFalse(dmd.supportsANSI92IntermediateSQL());
assertTrue(dmd.supportsBatchUpdates());
assertFalse(dmd.supportsCatalogsInDataManipulation());
assertFalse(dmd.supportsCatalogsInIndexDefinitions());
assertFalse(dmd.supportsCatalogsInPrivilegeDefinitions());
assertFalse(dmd.supportsCatalogsInProcedureCalls());
assertFalse(dmd.supportsCatalogsInTableDefinitions());
assertTrue(dmd.supportsColumnAliasing());
assertFalse(dmd.supportsConvert());
// Simple check since convert is not supported.
// A comprehensive test should be added when convert
// is supported, though most likely in a test class
// specific to convert.
assertFalse(dmd.supportsConvert(Types.INTEGER, Types.SMALLINT));
assertFalse(dmd.supportsCoreSQLGrammar());
assertTrue(dmd.supportsCorrelatedSubqueries());
assertTrue(dmd.supportsDataDefinitionAndDataManipulationTransactions());
assertFalse(dmd.supportsDataManipulationTransactionsOnly());
assertTrue(dmd.supportsDifferentTableCorrelationNames());
/* DERBY-2244 Derby does support Order By clause
* thus the changing the assert condition to TRUE
*/
assertTrue(dmd.supportsExpressionsInOrderBy());
assertFalse(dmd.supportsExtendedSQLGrammar());
assertFalse(dmd.supportsFullOuterJoins());
assertFalse(dmd.supportsGetGeneratedKeys());
assertTrue(dmd.supportsGroupBy());
assertTrue(dmd.supportsGroupByBeyondSelect());
assertTrue(dmd.supportsGroupByUnrelated());
assertFalse(dmd.supportsIntegrityEnhancementFacility());
assertTrue(dmd.supportsLikeEscapeClause());
assertTrue(dmd.supportsLimitedOuterJoins());
assertTrue(dmd.supportsMinimumSQLGrammar());
assertFalse(dmd.supportsMixedCaseIdentifiers());
assertTrue(dmd.supportsMixedCaseQuotedIdentifiers());
assertTrue(dmd.supportsMultipleOpenResults());
assertTrue(dmd.supportsMultipleResultSets());
assertTrue(dmd.supportsMultipleTransactions());
assertFalse(dmd.supportsNamedParameters());
assertTrue(dmd.supportsNonNullableColumns());
// Open cursors are not supported across global
// (XA) transactions so the driver returns false.
assertFalse(dmd.supportsOpenCursorsAcrossCommit());
assertFalse(dmd.supportsOpenCursorsAcrossRollback());
assertTrue(dmd.supportsOpenStatementsAcrossCommit());
assertFalse(dmd.supportsOpenStatementsAcrossRollback());
assertFalse(dmd.supportsOrderByUnrelated());
assertTrue(dmd.supportsOuterJoins());
assertTrue(dmd.supportsPositionedDelete());
assertTrue(dmd.supportsPositionedUpdate());
assertTrue(dmd.supportsResultSetConcurrency(
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY));
assertTrue(dmd.supportsResultSetConcurrency(
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE));
assertTrue(dmd.supportsResultSetConcurrency(
ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY));
assertTrue(dmd.supportsResultSetConcurrency(
ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE));
assertFalse(dmd.supportsResultSetConcurrency(
ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY));
assertFalse(dmd.supportsResultSetConcurrency(
ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE));
assertTrue(dmd.supportsResultSetHoldability(
ResultSet.CLOSE_CURSORS_AT_COMMIT));
assertTrue(dmd.supportsResultSetHoldability(
ResultSet.HOLD_CURSORS_OVER_COMMIT));
assertTrue(dmd.supportsResultSetType(
ResultSet.TYPE_FORWARD_ONLY));
assertTrue(dmd.supportsResultSetType(
ResultSet.TYPE_SCROLL_INSENSITIVE));
assertFalse(dmd.supportsResultSetType(
ResultSet.TYPE_SCROLL_SENSITIVE));
assertTrue(dmd.supportsSavepoints());
assertTrue(dmd.supportsSchemasInDataManipulation());
assertTrue(dmd.supportsSchemasInIndexDefinitions());
assertTrue(dmd.supportsSchemasInPrivilegeDefinitions());
assertTrue(dmd.supportsSchemasInProcedureCalls());
assertTrue(dmd.supportsSchemasInTableDefinitions());
assertTrue(dmd.supportsSelectForUpdate());
assertFalse(dmd.supportsStatementPooling());
assertTrue(dmd.supportsStoredProcedures());
assertTrue(dmd.supportsSubqueriesInComparisons());
assertTrue(dmd.supportsSubqueriesInExists());
assertTrue(dmd.supportsSubqueriesInIns());
assertTrue(dmd.supportsSubqueriesInQuantifieds());
assertTrue(dmd.supportsTableCorrelationNames());
assertTrue(dmd.supportsTransactionIsolationLevel(
Connection.TRANSACTION_READ_COMMITTED));
assertTrue(dmd.supportsTransactionIsolationLevel(
Connection.TRANSACTION_READ_UNCOMMITTED));
assertTrue(dmd.supportsTransactionIsolationLevel(
Connection.TRANSACTION_REPEATABLE_READ));
assertTrue(dmd.supportsTransactionIsolationLevel(
Connection.TRANSACTION_SERIALIZABLE));
assertTrue(dmd.supportsTransactions());
assertTrue(dmd.supportsUnion());
assertTrue(dmd.supportsUnionAll());
}
/**
* Test group of methods provides the limits imposed by a given data source
* Methods start with
* 'getMax'. See section 7.4 in JDBC 3.0 specification.
*
* Note a return of zero from one of these functions means
* no limit or limit not known.
*
* @throws SQLException
*
*/
public void testDataSourceLimits() throws SQLException
{
DatabaseMetaData dmd = getDMD();
assertEquals(0, dmd.getMaxBinaryLiteralLength());
// Catalogs not supported.
assertEquals(0, dmd.getMaxCatalogNameLength());
assertEquals(0, dmd.getMaxCharLiteralLength());
assertEquals(128, dmd.getMaxColumnNameLength());
assertEquals(0, dmd.getMaxColumnsInGroupBy());
assertEquals(0, dmd.getMaxColumnsInIndex());
assertEquals(0, dmd.getMaxColumnsInOrderBy());
assertEquals(0, dmd.getMaxColumnsInSelect());
assertEquals(0, dmd.getMaxColumnsInTable());
assertEquals(0, dmd.getMaxConnections());
assertEquals(128, dmd.getMaxCursorNameLength());
assertEquals(0, dmd.getMaxIndexLength());
assertEquals(128, dmd.getMaxProcedureNameLength());
assertEquals(0, dmd.getMaxRowSize());
assertEquals(128, dmd.getMaxSchemaNameLength());
assertEquals(0, dmd.getMaxStatementLength());
assertEquals(0, dmd.getMaxStatements());
assertEquals(128, dmd.getMaxTableNameLength());
assertEquals(0, dmd.getMaxTablesInSelect());
assertEquals(128, dmd.getMaxUserNameLength());
}
public void testMiscellaneous() throws SQLException
{
DatabaseMetaData dmd = getDMD();
assertTrue(dmd.allProceduresAreCallable());
assertTrue(dmd.allTablesAreSelectable());
assertFalse(dmd.dataDefinitionCausesTransactionCommit());
assertFalse(dmd.dataDefinitionIgnoredInTransactions());
// Derby does not yet implement scroll sensitive resultsets, so can't
// see changes for those; all *AreDetected and *AreVisible methods
// return false.
// For Forward Only ResultSets, also see lang.UpdatableResultSetTest
// *AreDetected; expect true for updates and deletes of
// TYPE_SCROLL_INSENSITIVE, all others should be false
assertFalse(dmd.deletesAreDetected(ResultSet.TYPE_FORWARD_ONLY));
assertTrue(dmd.deletesAreDetected(ResultSet.TYPE_SCROLL_INSENSITIVE));
assertFalse(dmd.deletesAreDetected(ResultSet.TYPE_SCROLL_SENSITIVE));
assertFalse(dmd.insertsAreDetected(ResultSet.TYPE_FORWARD_ONLY));
assertFalse(dmd.insertsAreDetected(ResultSet.TYPE_SCROLL_INSENSITIVE));
assertFalse(dmd.insertsAreDetected(ResultSet.TYPE_SCROLL_SENSITIVE));
assertFalse(dmd.updatesAreDetected(ResultSet.TYPE_FORWARD_ONLY));
assertTrue(dmd.updatesAreDetected(ResultSet.TYPE_SCROLL_INSENSITIVE));
assertFalse(dmd.updatesAreDetected(ResultSet.TYPE_SCROLL_SENSITIVE));
// others*AreVisible
// Since Derby materializes a forward only ResultSet incrementally,
// it is possible to see changes for FORWARD_ONLY
// Scroll insensitive ResultSet by their definition do not see changes
// made by others
assertTrue(dmd.othersDeletesAreVisible(ResultSet.TYPE_FORWARD_ONLY));
assertFalse(dmd.othersDeletesAreVisible(ResultSet.TYPE_SCROLL_INSENSITIVE));
assertFalse(dmd.othersDeletesAreVisible(ResultSet.TYPE_SCROLL_SENSITIVE));
assertTrue(dmd.othersInsertsAreVisible(ResultSet.TYPE_FORWARD_ONLY));
assertFalse(dmd.othersInsertsAreVisible(ResultSet.TYPE_SCROLL_INSENSITIVE));
assertFalse(dmd.othersInsertsAreVisible(ResultSet.TYPE_SCROLL_SENSITIVE));
assertTrue(dmd.othersUpdatesAreVisible(ResultSet.TYPE_FORWARD_ONLY));
assertFalse(dmd.othersUpdatesAreVisible(ResultSet.TYPE_SCROLL_INSENSITIVE));
assertFalse(dmd.othersUpdatesAreVisible(ResultSet.TYPE_SCROLL_SENSITIVE));
// Scroll insensitive ResultSets see updates, deletes, but not inserts
assertFalse(dmd.ownDeletesAreVisible(ResultSet.TYPE_FORWARD_ONLY));
assertTrue(dmd.ownDeletesAreVisible(ResultSet.TYPE_SCROLL_INSENSITIVE));
assertFalse(dmd.ownDeletesAreVisible(ResultSet.TYPE_SCROLL_SENSITIVE));
assertFalse(dmd.ownInsertsAreVisible(ResultSet.TYPE_FORWARD_ONLY));
assertFalse(dmd.ownInsertsAreVisible(ResultSet.TYPE_SCROLL_INSENSITIVE));
assertFalse(dmd.ownInsertsAreVisible(ResultSet.TYPE_SCROLL_SENSITIVE));
assertFalse(dmd.ownUpdatesAreVisible(ResultSet.TYPE_FORWARD_ONLY));
assertTrue(dmd.ownUpdatesAreVisible(ResultSet.TYPE_SCROLL_INSENSITIVE));
assertFalse(dmd.ownUpdatesAreVisible(ResultSet.TYPE_SCROLL_SENSITIVE));
assertTrue(dmd.doesMaxRowSizeIncludeBlobs());
// Catalogs not supported, so empty string returned for separator.
assertEquals("", dmd.getCatalogSeparator());
assertEquals("CATALOG", dmd.getCatalogTerm());
assertEquals(Connection.TRANSACTION_READ_COMMITTED,
dmd.getDefaultTransactionIsolation());
assertEquals("", dmd.getExtraNameCharacters());
assertEquals("\"", dmd.getIdentifierQuoteString());
assertEquals("PROCEDURE", dmd.getProcedureTerm());
assertEquals(ResultSet.HOLD_CURSORS_OVER_COMMIT,
dmd.getResultSetHoldability());
assertEquals("SCHEMA", dmd.getSchemaTerm());
assertEquals("", dmd.getSearchStringEscape());
assertEquals(DatabaseMetaData.sqlStateSQL99,
dmd.getSQLStateType());
assertFalse(dmd.isCatalogAtStart());
assertTrue(dmd.locatorsUpdateCopy());
assertTrue(dmd.usesLocalFilePerTable());
assertTrue(dmd.usesLocalFiles());
}
/**
* Methods that describe the version of the
* driver and database.
*
* @throws java.sql.SQLException
*/
public void testVersionInfo() throws SQLException
{
DatabaseMetaData dmd = getDMD();
int databaseMajor = dmd.getDatabaseMajorVersion();
int databaseMinor = dmd.getDatabaseMinorVersion();
int driverMajor = dmd.getDriverMajorVersion();
int driverMinor = dmd.getDriverMinorVersion();
String databaseVersion = dmd.getDatabaseProductVersion();
String driverVersion = dmd.getDriverVersion();
if (usingEmbedded())
{
// Database *is* the driver.
assertEquals("Embedded Major version ",
databaseMajor, driverMajor);
assertEquals("Embedded Minor version ",
databaseMinor, driverMinor);
assertEquals("Embedded version",
databaseVersion, driverVersion);
}
assertEquals("Apache Derby", dmd.getDatabaseProductName());
String driverName = dmd.getDriverName();
if (usingEmbedded())
{
assertEquals("Apache Derby Embedded JDBC Driver",
driverName);
}
else if (usingDerbyNetClient())
{
assertEquals("Apache Derby Network Client JDBC Driver",
driverName);
}
int jdbcMajor = dmd.getJDBCMajorVersion();
int jdbcMinor = dmd.getJDBCMinorVersion();
int expectedJDBCMajor = -1;
int expectedJDBCMinor = -1;
// java 8 - jdbc 4.2
if (JDBC.vmSupportsJDBC42())
{
expectedJDBCMajor = 4;
expectedJDBCMinor = 2;
}
// java 7 - jdbc 4.1
else if (JDBC.vmSupportsJDBC41())
{
expectedJDBCMajor = 4;
expectedJDBCMinor = 1;
}
// java 6 - jdbc 4.0
else if (JDBC.vmSupportsJDBC4())
{
expectedJDBCMajor = 4;
expectedJDBCMinor = 0;
}
if (expectedJDBCMajor != -1)
{
assertEquals("JDBC Major version",
expectedJDBCMajor, jdbcMajor);
assertEquals("JDBC Minor version", expectedJDBCMinor, jdbcMinor);
}
}
/**
* getURL() method. Note that this method
* is the only JDBC 3 DatabaseMetaData method
* that is dropped in JSR169.
*
* @throws java.sql.SQLException
*/
public void testGetURL() throws SQLException
{
DatabaseMetaData dmd = getDMD();
String url;
try {
url = dmd.getURL();
} catch (NoSuchMethodError e) {
// JSR 169 - method must not be there!
assertTrue("getURL not supported", JDBC.vmSupportsJSR169());
assertFalse("getURL not supported", JDBC.vmSupportsJDBC3());
return;
}
assertFalse("getURL is supported!", JDBC.vmSupportsJSR169());
assertTrue("getURL is supported!", JDBC.vmSupportsJDBC3());
TestConfiguration config = getTestConfiguration();
String expectedURL = config.getJDBCUrl();
// DERBY-4886: Embedded returns the URL without connection attributes,
// client returns the URL with connection attributes.
if (usingDerbyNetClient()) {
List<String> urlComponents = Arrays.asList(url.split(";"));
// Only compare whatever comes before the first semi-colon with
// the expected URL. Check connection attributes separately.
url = urlComponents.get(0);
// Put each actual connection attribute in a HashSet for easy
// comparison.
HashSet<String> attrs = new HashSet<String>(
urlComponents.subList(1, urlComponents.size()));
// Put each expected connection attribute in a HashSet.
HashSet<String> expectedAttrs = new HashSet<String>();
Properties ca = config.getConnectionAttributes();
for (String key : ca.stringPropertyNames()) {
expectedAttrs.add(key + '=' + ca.getProperty(key));
}
// Verify that the actual connection attributes match the
// expected attributes. Order is irrelevant.
assertEquals("Connection attributes don't match",
expectedAttrs, attrs);
}
assertEquals("getURL match", expectedURL, url);
}
/**
* Derby stores unquoted identifiers as upper
* case and quoted ones as mixed case.
* They are always compared case sensitive.
*
* @throws java.sql.SQLException
*/
public void testIdentifierStorage() throws SQLException
{
DatabaseMetaData dmd = getDMD();
assertFalse(dmd.storesLowerCaseIdentifiers());
assertFalse(dmd.storesLowerCaseQuotedIdentifiers());
assertFalse(dmd.storesMixedCaseIdentifiers());
assertTrue(dmd.storesMixedCaseQuotedIdentifiers());
assertTrue(dmd.storesUpperCaseIdentifiers());
assertFalse(dmd.storesUpperCaseQuotedIdentifiers());
}
/**
* methods that return information about handling NULL.
*
* @throws java.sql.SQLException
*/
public void testNullInfo() throws SQLException
{
DatabaseMetaData dmd = getDMD();
assertTrue(dmd.nullPlusNonNullIsNull());
assertFalse(dmd.nullsAreSortedAtEnd());
assertFalse(dmd.nullsAreSortedAtStart());
assertTrue(dmd.nullsAreSortedHigh());
assertFalse(dmd.nullsAreSortedLow());
}
/**
* Method getSQLKeywords, returns list of SQL keywords
* that are not defined by SQL92.
*
* @throws java.sql.SQLException
*/
public void testSQLKeywords() throws SQLException
{
String keywords = getDMD().getSQLKeywords();
assertNotNull(keywords);
//TODO: more testing but not sure what!
}
/**
* Methods that return information specific to
* the current connection.
*
* @throws java.sql.SQLException
*/
public void testConnectionSpecific() throws SQLException
{
DatabaseMetaData dmd = getDMD();
assertSame(getConnection(), dmd.getConnection());
assertEquals(getTestConfiguration().getUserName(),
dmd.getUserName());
assertEquals(getConnection().isReadOnly(), dmd.isReadOnly());
}
/**
* This is not a test of Derby but JDBC constants for meta data
* that this test depends on.
* The constants for nullability are the same but let's check to make sure.
*
*/
public void testConstants()
{
assertEquals(DatabaseMetaData.columnNoNulls, ResultSetMetaData.columnNoNulls);
assertEquals(DatabaseMetaData.columnNullable, ResultSetMetaData.columnNullable);
assertEquals(DatabaseMetaData.columnNullableUnknown, ResultSetMetaData.columnNullableUnknown);
}
/*
** DatabaseMetaData calls that return ResultSets.
*/
/**
* Test UDT-related metadata methods.
*
* @throws java.lang.Exception
*/
public void testUDTs() throws Exception
{
//
// We only run this test if the database version is at least 10.6.
// Otherwise we can't create a UDT.
//
Version dataVersion = getDataVersion( getConnection() );
if ( dataVersion.compareTo( new Version( 10, 6, 0, 0 ) ) < 0 ) { return; }
DatabaseMetaData dmd = getDMD();
createObjectsForUDTTests();
ResultSet rs = dmd.getUDTs(null,null,null,null);
String[] columnNames = new String[] {
"TYPE_CAT", "TYPE_SCHEM", "TYPE_NAME", "CLASS_NAME",
"DATA_TYPE", "REMARKS", "BASE_TYPE"};
int[] columnTypes = new int[] {
Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.LONGVARCHAR,
Types.INTEGER, Types.VARCHAR, Types.SMALLINT
};
boolean[] nullability = new boolean[] {
true, true, false, false,
false, true, true
};
assertMetaDataResultSet(rs, columnNames, columnTypes, nullability);
String[][] expectedRows = new String[][]
{
{ null, schema, "PRICE", "org.apache.derbyTesting.functionTests.tests.lang.Price", "2000", null, null },
};
JDBC.assertFullResultSet( rs, expectedRows );
// now try the test, specifying a specific type of UDT
rs = dmd.getUDTs( null, null, null, new int[] { Types.JAVA_OBJECT } );
JDBC.assertFullResultSet( rs, expectedRows );
rs = dmd.getUDTs( null, null, null, new int[] { Types.DISTINCT, Types.JAVA_OBJECT } );
JDBC.assertFullResultSet( rs, expectedRows );
// no UDTs of these types
rs = dmd.getUDTs( null, null, null, new int[] { Types.DISTINCT, Types.STRUCT } );
JDBC.assertEmpty(rs);
// try explicit schema and type name
rs = dmd.getUDTs( null, schema, "PRICE",
new int[] { Types.DISTINCT, Types.JAVA_OBJECT } );
JDBC.assertFullResultSet( rs, expectedRows );
rs = dmd.getUDTs( null, schema.substring(0, 2) + "%", "PRI%",
new int[] { Types.DISTINCT, Types.JAVA_OBJECT } );
JDBC.assertFullResultSet( rs, expectedRows );
rs = dmd.getUDTs( null, "FOO", "PRICE", new int[] { Types.DISTINCT, Types.JAVA_OBJECT } );
JDBC.assertEmpty(rs);
// now make sure that getColumns() returns the right data
rs = dmd.getColumns( null, schema, "ORDERS", null );
expectedRows = new String[][]
{
{
"", schema, "ORDERS", "TOTALPRICE",
"2000", "\"" + schema + "\".\"PRICE\"", "-1", null,
null, null, "1", "",
null, null, null, null,
"1", "YES", null, null,
null, null, "NO", "NO", null
},
};
JDBC.assertFullResultSet( rs, expectedRows );
rs = dmd.getColumns( null, schema, "ORDERS", null );
crossCheckGetColumnsAndResultSetMetaData( rs, false, 0 );
dropObjectsForUDTTests();
}
/**
* Create objects needed to test the UDT-related metadata calls
*
* @throws java.sql.SQLException
*/
private void createObjectsForUDTTests() throws SQLException
{
getConnection().setAutoCommit(false);
Statement s = createStatement();
s.execute( "create type price external name 'org.apache.derbyTesting.functionTests.tests.lang.Price' language java" );
s.execute( "create table orders( totalPrice price )" );
commit();
getConnection().setAutoCommit(true);
}
/**
* Drop the objects needed for testing the UDT-related metadata methods
*
* @throws java.sql.SQLException
*/
private void dropObjectsForUDTTests() throws SQLException
{
getConnection().setAutoCommit(false);
Statement s = createStatement();
s.execute( "drop table orders" );
s.execute( "drop type price restrict" );
commit();
getConnection().setAutoCommit(true);
}
/**
* Test methods that describe attributes of SQL Objects
* that are not supported by derby. In each case the
* metadata should return an empty ResultSet of the
* correct shape, and with correct names, datatypes and
* nullability for the columns in the ResultSet.
*
* @throws java.sql.SQLException
*/
public void testUnimplementedSQLObjectAttributes() throws SQLException
{
DatabaseMetaData dmd = getDMD();
ResultSet rs;
rs = dmd.getAttributes(null,null,null,null);
String [] columnNames = {
"TYPE_CAT", "TYPE_SCHEM", "TYPE_NAME", "ATTR_NAME", "DATA_TYPE",
"ATTR_TYPE_NAME", "ATTR_SIZE", "DECIMAL_DIGITS", "NUM_PREC_RADIX",
"NULLABLE", "REMARKS", "ATTR_DEF", "SQL_DATA_TYPE",
"SQL_DATETIME_SUB", "CHAR_OCTET_LENGTH", "ORDINAL_POSITION",
"IS_NULLABLE", "SCOPE_CATALOG", "SCOPE_SCHEMA", "SCOPE_TABLE",
"SOURCE_DATA_TYPE"
};
int [] columnTypes = {
Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
Types.INTEGER, Types.VARCHAR, Types.INTEGER, Types.INTEGER, Types.INTEGER,
Types.INTEGER, Types.VARCHAR, Types.VARCHAR, Types.INTEGER,
Types.INTEGER, Types.INTEGER, Types.INTEGER,
Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.SMALLINT
};
// DERBY-3171; we get a different value back for nullability for
// a number of the columns with networkserver/client vs. embedded
boolean nullval = true;
if (usingDerbyNetClient())
nullval = false;
boolean [] nullability = {
true, true, false, nullval, nullval, nullval, nullval,
nullval, nullval, nullval, true, true, nullval, nullval,
nullval, nullval, nullval, true, true, true, true
};
assertMetaDataResultSet(rs, columnNames, columnTypes, nullability);
JDBC.assertEmpty(rs);
rs = dmd.getCatalogs();
checkCatalogsShape(rs);
JDBC.assertEmpty(rs);
rs = dmd.getSuperTables(null,null,null);
columnNames = new String[] {
"TABLE_CAT", "TABLE_SCHEM", "TABLE_NAME", "SUPERTABLE_NAME"};
columnTypes = new int[] {
Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR};
nullability = new boolean[] {
true, true, false, false};
assertMetaDataResultSet(rs, columnNames, columnTypes, nullability);
JDBC.assertEmpty(rs);
rs = dmd.getSuperTypes(null,null,null);
columnNames = new String[] {
"TYPE_CAT", "TYPE_SCHEM", "TYPE_NAME", "SUPERTYPE_CAT",
"SUPERTYPE_SCHEM", "SUPERTYPE_NAME"};
columnTypes = new int[] {
Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
Types.VARCHAR, Types.VARCHAR};
nullability = new boolean[] {
true, true, false, true, true, false};
assertMetaDataResultSet(rs, columnNames, columnTypes, nullability);
JDBC.assertEmpty(rs);
ResultSet rss[] = getVersionColumns(null,null, "No_such_table");
checkVersionColumnsShape(rss);
JDBC.assertEmpty(rss[0]);
JDBC.assertEmpty(rss[1]);
rs.close();
rss[0].close();
rss[1].close();
}
/**
* Implement ODBC equivalent for getVersionColumns - SYSIBM.SQLCOLUMNS
*
* @param catalog
* @param schema
* @param table
*
* @return version
* @throws java.sql.SQLException
*/
public ResultSet getVersionColumnsODBC(
String catalog, String schema, String table)
throws SQLException
{
CallableStatement cs = prepareCall("CALL SYSIBM.SQLSPECIALCOLUMNS " +
"(2, ?, ?, ?, 1, 1, 'DATATYPE=''ODBC''')");
cs.setString(1, catalog);
cs.setString(2, schema);
cs.setString(3, table);
cs.execute();
return cs.getResultSet();
}
/**
* Helper method for testing getVersionColumns - calls
* dmd.getVersionColumns for the JDBC call, and getVersionColumnsODBC for
* the ODBC procedure
* @param catalog
* @param schema
* @param table
* @return version
* @throws SQLException
*/
private ResultSet[] getVersionColumns(
String catalog, String schema, String table)
throws SQLException
{
ResultSet[] rss = new ResultSet[2];
DatabaseMetaData dmd = getDMD();
rss[0]= dmd.getVersionColumns(catalog, schema, table);
rss[1]= getVersionColumnsODBC(catalog, schema, table);
return rss;
}
/**
* Six combinations of valid identifiers with mixed
* case, to see how the various pattern matching
* and returned values handle them.
* This test only creates objects in these schemas.
*/
private static final String[] IDS =
{
"one_dmd_test",
"TWO_dmd_test",
"ThReE_dmd_test",
"\"four_dmd_test\"",
"\"FIVE_dmd_test\"",
"\"sIx_dmd_test\""
};
/**
* All the builtin schemas.
*/
private static final String[] BUILTIN_SCHEMAS = {
"APP", "NULLID", "SQLJ", "SYS", "SYSCAT", "SYSCS_DIAG",
"SYSCS_UTIL", "SYSFUN", "SYSIBM", "SYSPROC", "SYSSTAT"};
public static String getStoredIdentifier(String sqlIdentifier)
{
if (sqlIdentifier.charAt(0) == '"')
return sqlIdentifier.substring(1, sqlIdentifier.length() - 1);
else
return sqlIdentifier.toUpperCase(Locale.ENGLISH);
}
/**
* Test getSchemas() without modifying the database.
*
* @throws SQLException
*/
public void testGetSchemasReadOnly() throws SQLException {
DatabaseMetaData dmd = getDMD();
ResultSet rs = dmd.getSchemas();
checkSchemas(rs, new String[0]);
}
/**
* Test getSchemas().
*
* @throws SQLException
*/
public void testGetSchemasModify() throws SQLException {
createSchemasForTests();
DatabaseMetaData dmd = getDMD();
ResultSet rs = dmd.getSchemas();
checkSchemas(rs, IDS);
}
private void createSchemasForTests() throws SQLException
{
// Set to cleanup on teardown.
modifiedDatabase = true;
Statement s = createStatement();
for (String IDS1 : IDS) {
s.executeUpdate("CREATE SCHEMA " + IDS1);
}
s.close();
commit();
}
/**
* Check the returned information from a getSchemas().
* The passed in String[] expected is a list of the
* schemas expected to be present in the returned
* set. The returned set may contain additional
* schemas which will be ignored, thus this test
* can be used regardless of the database state.
* The builtin schemas are automatically checked
* and must not be part of the passed in list.
* @param rs
* @param userExpected
*
* @throws java.sql.SQLException
*/
public static void checkSchemas(ResultSet rs,
String[] userExpected) throws SQLException
{
checkSchemasShape(rs);
// Add in the system schemas
String[] expected =
new String[BUILTIN_SCHEMAS.length + userExpected.length];
System.arraycopy(BUILTIN_SCHEMAS, 0,
expected, 0, BUILTIN_SCHEMAS.length);
System.arraycopy(userExpected, 0,
expected, BUILTIN_SCHEMAS.length, userExpected.length);
// Remove any quotes from user schemas and upper case
// those without quotes.
for (int i = BUILTIN_SCHEMAS.length; i < expected.length; i++)
{
expected[i] = getStoredIdentifier(expected[i]);
}
//output is ordered by TABLE_SCHEM
Arrays.sort(expected);
int nextMatch = 0;
while (rs.next()) {
String schema = rs.getString("TABLE_SCHEM");
assertNotNull(schema);
// Catalogs not supported
assertNull(rs.getString("TABLE_CATALOG"));
if (nextMatch < expected.length)
{
if (expected[nextMatch].equals(schema))
nextMatch++;
}
}
rs.close();
assertEquals("Schemas missing ", expected.length, nextMatch);
}
/**
* Check the shape of the ResultSet from any
* getSchemas call.
*
* @param rs result set
* @throws java.sql.SQLException
*/
private static void checkSchemasShape(ResultSet rs) throws SQLException
{
assertMetaDataResultSet(rs,
new String[] {
"TABLE_SCHEM", "TABLE_CATALOG"
},
new int[] {
Types.VARCHAR, Types.VARCHAR
}
, new boolean[] {false, true}
);
}
/**
* Execute dmd.getTables() but perform additional checking
* of the ODBC variant.
* @param dmd
* @param catalog
* @param schema
* @param table
* @param tableTypes
* @return result set from query
* @throws java.sql.SQLException
* @throws IOException
*/
private ResultSet getDMDTables(DatabaseMetaData dmd,
String catalog, String schema, String table, String[] tableTypes)
throws SQLException, IOException
{
checkGetTablesODBC(catalog, schema, table, tableTypes);
return dmd.getTables(catalog, schema, table, tableTypes);
}
/**
* Test getTables() without modifying the database.
*
* @throws SQLException
* @throws IOException
*/
public void testGetTablesReadOnly() throws SQLException, IOException {
DatabaseMetaData dmd = getDMD();
ResultSet rs;
rs = getDMDTables(dmd, null, null, null, null);
checkTablesShape(rs);
int allTableCount = JDBC.assertDrainResults(rs);
assertTrue("getTables() on all was empty!", allTableCount > 0);
rs = getDMDTables(dmd, "%", "%", "%", null);
checkTablesShape(rs);
assertEquals("Different counts from getTables",
allTableCount, JDBC.assertDrainResults(rs));
rs = getDMDTables(dmd, null, "NO_such_schema", null, null);
checkTablesShape(rs);
JDBC.assertEmpty(rs);
rs = getDMDTables(dmd, null, "SQLJ", null, null);
checkTablesShape(rs);
JDBC.assertEmpty(rs);
rs = getDMDTables(dmd, null, "SQLJ", "%", null);
checkTablesShape(rs);
JDBC.assertEmpty(rs);
rs = getDMDTables(dmd, null, "SYS", "No_such_table", null);
checkTablesShape(rs);
JDBC.assertEmpty(rs);
String[] userTableOnly = new String[] {"TABLE"};
// no user tables in SYS
rs = getDMDTables(dmd, null, "SYS", null, userTableOnly);
checkTablesShape(rs);
JDBC.assertEmpty(rs);
rs = getDMDTables(dmd, null, "SYS", "%", userTableOnly);
checkTablesShape(rs);
JDBC.assertEmpty(rs);
String[] systemTableOnly = new String[] {"SYSTEM_TABLE"};
rs = getDMDTables(dmd, null, "SYS", null, systemTableOnly);
checkTablesShape(rs);
int systemTableCount = JDBC.assertDrainResults(rs);
assertTrue("getTables() on system tables was empty!", systemTableCount > 0);
rs = getDMDTables(dmd, null, "SYS", "%", systemTableOnly);
checkTablesShape(rs);
assertEquals(systemTableCount, JDBC.assertDrainResults(rs));
String[] viewOnly = new String[] {"VIEW"};
rs = getDMDTables(dmd, null, "SYS", null, viewOnly);
JDBC.assertEmpty(rs);
rs = getDMDTables(dmd, null, "SYS", "%", viewOnly);
JDBC.assertEmpty(rs);
String[] allTables = {"SYNONYM","SYSTEM TABLE","TABLE","VIEW"};
rs = getDMDTables(dmd, null, null, null, allTables);
checkTablesShape(rs);
assertEquals("Different counts from getTables",
allTableCount, JDBC.assertDrainResults(rs));
rs = getDMDTables(dmd, "%", "%", "%", allTables);
checkTablesShape(rs);
assertEquals("Different counts from getTables",
allTableCount, JDBC.assertDrainResults(rs));
}
/**
* Test getTables() with modifying the database.
*
* @throws SQLException
* @throws IOException
*/
public void testGetTablesModify() throws Exception {
int totalTables = createTablesForTest(false);
DatabaseMetaData dmd = getDMD();
ResultSet rs;
String[] userTableOnly = new String[] {"TABLE"};
// Get the list of idenifiers from IDS as the database
// would store them in the order required.
String[] dbIDS = getSortedIdentifiers();
// Check the contents, ordered by TABLE_CAT, TABLE_SCHEMA, TABLE_NAME
rs = getDMDTables(dmd, null, null, null, userTableOnly);
checkTablesShape(rs);
int rowPosition = 0;
while (rs.next())
{
//boolean ourTable;
assertEquals("TABLE_CAT", "", rs.getString("TABLE_CAT"));
String schema_ = rs.getString("TABLE_SCHEM");
// See if the table is in one of the schemas we created.
// If not we perform what checking we can.
boolean ourSchema = Arrays.binarySearch(dbIDS, schema_) >= 0;
if (ourSchema) {
assertEquals("TABLE_SCHEM",
dbIDS[rowPosition/dbIDS.length], schema_);
assertEquals("TABLE_NAME",
dbIDS[rowPosition%dbIDS.length], rs.getString("TABLE_NAME"));
}
assertEquals("TABLE_TYPE", "TABLE", rs.getString("TABLE_TYPE"));
assertEquals("REMARKS", "", rs.getString("REMARKS"));
assertNull("TYPE_CAT", rs.getString("TYPE_CAT"));
assertNull("TYPE_SCHEM", rs.getString("TYPE_SCHEM"));
assertNull("TYPE_NAME", rs.getString("TYPE_NAME"));
assertNull("SELF_REFERENCING_COL_NAME", rs.getString("SELF_REFERENCING_COL_NAME"));
assertNull("REF_GENERATION", rs.getString("REF_GENERATION"));
if (ourSchema)
rowPosition++;
}
rs.close();
assertEquals("getTables count for all user tables",
totalTables, rowPosition);
Random rand = new Random();
for (String schema_ : dbIDS) {
int pc = rand.nextInt(6);
String schemaPattern = schema_.substring(0, pc + 2) + "%";
rs = getDMDTables(dmd, null, schemaPattern, null, userTableOnly);
checkTablesShape(rs);
rowPosition = 0;
while (rs.next())
{
assertEquals("TABLE_SCHEM",
schema_, rs.getString("TABLE_SCHEM"));
assertEquals("TABLE_NAME",
dbIDS[rowPosition%dbIDS.length], rs.getString("TABLE_NAME"));
assertEquals("TABLE_TYPE", "TABLE", rs.getString("TABLE_TYPE"));
rowPosition++;
}
rs.close();
assertEquals("getTables count schema pattern",
dbIDS.length, rowPosition);
}
for (String table : dbIDS) {
int pc = rand.nextInt(6);
String tablePattern = table.substring(0, pc + 2) + "%";
rs = getDMDTables(dmd, null, null, tablePattern, userTableOnly);
checkTablesShape(rs);
rowPosition = 0;
while (rs.next())
{
assertEquals("TABLE_SCHEM",
dbIDS[rowPosition%dbIDS.length], rs.getString("TABLE_SCHEM"));
assertEquals("TABLE_TYPE", "TABLE", rs.getString("TABLE_TYPE"));
assertEquals("TABLE_NAME",
table, rs.getString("TABLE_NAME"));
rowPosition++;
}
rs.close();
assertEquals("getTables count schema pattern",
dbIDS.length, rowPosition);
}
}
/**
* Execute and check the ODBC variant of getTables which
* uses a procedure to provide the same information to ODBC clients.
* @param catalog
* @param schema
* @param table
* @param tableTypes
* @throws java.sql.SQLException
* @throws IOException
*/
private void checkGetTablesODBC(String catalog, String schema,
String table, String[] tableTypes) throws SQLException, IOException
{
String tableTypesAsString = null;
if (tableTypes != null) {
int count = tableTypes.length;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < count; i++) {
if (i > 0)
sb.append(",");
sb.append(tableTypes[i]);
}
tableTypesAsString = sb.toString();
}
CallableStatement cs = prepareCall(
"CALL SYSIBM.SQLTABLES(?, ?, ?, ?, 'DATATYPE=''ODBC''')");
cs.setString(1, catalog);
cs.setString(2, schema);
cs.setString(3, table);
cs.setString(4, tableTypesAsString);
cs.execute();
ResultSet odbcrs = cs.getResultSet();
assertNotNull(odbcrs);
// Returned ResultSet will have the same shape as
// DatabaseMetaData.getTables() even though ODBC
// only defines the first five columns.
checkTablesShape(odbcrs);
// Expect the contents of JDBC and ODBC metadata to be the same.
ResultSet dmdrs = getDMD().getTables(catalog, schema, table, tableTypes);
JDBC.assertSameContents(odbcrs, dmdrs);
cs.close();
}
/**
* Create a set of tables using the identifiers in IDS.
* For each identifier in IDS a schema is created.
* For each identifier in IDS create a table in every schema just created.
* Each table has five columns with names using the identifiers from IDS
* suffixed with _N where N is the column number in the table. The base
* name for each column is round-robined from the set of IDS.
* The type of each column is round-robined from the set of supported
* types returned by getSQLTypes.
*
* <BR>
* skipXML can be set to true to create tables without any XML
* columns. This is useful for getColumns() testing where
* the fixture compares the output of DatabaseMetaData to
* ResultSetMetaData by a SELCT * from the table. However
* for XML columns they cannot be returned through JDBC yet.
*
* @param skipXML true if tables with the XML column should not
* be created.
* @return total number of tables created
* @throws SQLException
*/
private int createTablesForTest(boolean skipXML) throws Exception
{
getConnection().setAutoCommit(false);
List<String> types = getSQLTypes(getConnection());
if (skipXML)
types.remove("XML");
//
// The BOOLEAN datatype is only allowed in databases
// at level 10.7 or higher.
//
Version dataVersion = getDataVersion( getConnection() );
if ( dataVersion.compareTo( new Version( 10, 7, 0, 0 ) ) < 0 )
{
types.remove("BOOLEAN");
}
int typeCount = types.size();
createSchemasForTests();
Statement s = createStatement();
int columnCounter = 0;
for (String IDS1 : IDS) {
for (String IDS2 : IDS) {
StringBuilder sb = new StringBuilder();
sb.append("CREATE TABLE ");
sb.append(IDS1);
sb.append('.');
sb.append(IDS2);
sb.append(" (");
// Five columns per table
for (int c = 1; c <= 5; c++) {
String colName = IDS[columnCounter % IDS.length];
boolean delimited = colName.charAt(colName.length() - 1) == '"';
if (delimited)
colName = colName.substring(0, colName.length() - 1);
sb.append(colName);
sb.append('_');
sb.append(c); // append the column number
if (delimited)
sb.append('"');
sb.append(' ');
sb.append(types.get(columnCounter++ % typeCount));
if (c < 5)
sb.append(", ");
}
sb.append(")");
s.execute(sb.toString());
}
}
s.close();
commit();
return IDS.length * IDS.length;
}
/**
* Test getTableColumns().
* Contents are compared to the ResultSetMetaData
* for a SELECT * from the table. All columns in
* all tables are checked.
*
* @throws java.lang.Exception
*/
public void testGetColumnsReadOnly() throws Exception
{
ResultSet[] rs = getColumns(null, null, null, null);
for ( int j =0 ; j<2 ; j++) {
checkColumnsShape(rs[j], j);
crossCheckGetColumnsAndResultSetMetaData(rs[j], false, j);
}
}
/**
* Test getColumns() with modifying the database.
*
* @throws SQLException
*/
public void testGetColumnsModify() throws Exception {
// skip XML datatype as our cross check with
// ResultSetMetaData will fail
int totalTables = createTablesForTest(true);
// First cross check all the columns in the database
// with the ResultSetMetaData.
testGetColumnsReadOnly();
Random rand = new Random();
String[] dbIDS = getSortedIdentifiers();
for (int i = 1; i < 20; i++) {
int seenColumnCount = 0;
// These are the pattern matching parameters
String schemaPattern = getPattern(rand, dbIDS);
String tableNamePattern = getPattern(rand, dbIDS);
String columnNamePattern = getPattern(rand, dbIDS);
ResultSet[] rs = getColumns(null,
schemaPattern, tableNamePattern, columnNamePattern);
for (int j=0 ; j<2 ; j++) {
checkColumnsShape(rs[j], j);
while (rs[j].next())
{
String schema_ = rs[j].getString("TABLE_SCHEM");
String table = rs[j].getString("TABLE_NAME");
String column = rs[j].getString("COLUMN_NAME");
assertMatchesPattern(schemaPattern, schema_);
assertMatchesPattern(tableNamePattern, table);
assertMatchesPattern(columnNamePattern, column);
seenColumnCount++;
}
rs[j].close();
}
// Re-run to check the correct data is returned
// when filtering is enabled
rs = getColumns(null,
schemaPattern, tableNamePattern, columnNamePattern);
for (int j=0 ; j<2 ; j++) {
crossCheckGetColumnsAndResultSetMetaData(rs[j], true, j);
}
// Now re-execute fetching all schemas, columns etc.
// and see we can the same result when we "filter"
// in the application
rs = getColumns(null,null, null, null);
int appColumnCount = 0;
for (int j=0 ; j<2 ; j++) {
while (rs[j].next())
{
String schema_ = rs[j].getString("TABLE_SCHEM");
String table = rs[j].getString("TABLE_NAME");
String column = rs[j].getString("COLUMN_NAME");
if (!doesMatch(schemaPattern, 0, schema_, 0))
continue;
if (!doesMatch(tableNamePattern, 0, table, 0))
continue;
if (!doesMatch(columnNamePattern, 0, column, 0))
continue;
appColumnCount++;
}
rs[j].close();
}
assertEquals("Mismatched column count on getColumns() filtering",
seenColumnCount, appColumnCount);
}
}
private void assertMatchesPattern(String pattern, String result)
{
if (!doesMatch(pattern, 0, result, 0))
{
fail("Bad pattern matching:" + pattern +
" result:" + result);
}
}
/**
* See if a string matches the pattern as defined by
* DatabaseMetaData. By passing in non-zero values
* can check sub-sets of the pattern against the
* sub strings of the result.
* <BR>
* _ matches a single character
* <BR>
* % matches zero or more characters
* <BR>
* Other characters match themselves.
* @param pattern Pattern
* @param pp Position in pattern to start the actual pattern from
* @param result result string
* @param rp position in result to starting checking
* @return true if a match is found
*/
private boolean doesMatch(String pattern, int pp,
String result, int rp)
{
// Find a match
for (;;)
{
if (pp == pattern.length() && rp == result.length())
return true;
// more characters to match in the result but
// no more pattern.
if (pp == pattern.length())
return false;
char pc = pattern.charAt(pp);
if (pc == '_')
{
// need to match a single character but
// exhausted result, so no match.
if (rp == result.length())
return false;
pp++;
rp++;
}
else if (pc == '%')
{
// % at end, complete match regardless of
// position of result since % matches zero or more.
if (pp == pattern.length() - 1)
{
return true;
}
// Brut force, we have a pattern like %X
// and we are say in the third character of
// abCdefgX
// then start a 'CdefgX' and look for a match,
// then 'defgX' etc.
for (int sp = rp; sp < result.length(); sp++)
{
if (doesMatch(pattern, pp+1, result, sp))
{
// Have a match for the pattern after the %
// which means we have a match for the pattern
// with the % since we can match 0 or mor characters
// with %.
return true;
}
}
// Could not match the pattern after the %
return false;
}
else
{
// need to match a single character but
// exhausted result, so no match.
if (rp == result.length())
return false;
// Single character, must match exactly.
if (pc != result.charAt(rp))
{
//Computer says no.
return false;
}
pp++;
rp++;
}
}
}
private String getPattern(Random rand, String[] dbIDS)
{
int y = rand.nextInt(100);
if (y < 10)
return "%"; // All
if (y < 30)
return dbIDS[rand.nextInt(dbIDS.length)]; // exact match
String base;
if (y < 40)
{
// Base for some pattern that can never match
base = "XxZZzXXZZZxxXxZz";
}
else
{
base = dbIDS[rand.nextInt(dbIDS.length)];
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < base.length();)
{
int x = rand.nextInt(10);
if (x < 5)
x = 0; // bias towards keeping the characters.
boolean inWild;
if (sb.length() == 0)
inWild = false;
else
{
char last = sb.charAt(sb.length() - 1);
inWild = last == '_' || last == '%';
}
if (x == 0)
{
// character from base
sb.append(base.charAt(i++));
}
else if (x == 5)
{
i++;
// single character match
if (!inWild)
sb.append('_');
}
else
{
i += (x - 5);
// replace a number of characters with %
if (!inWild)
sb.append('%');
}
}
// Some pattern involving
return sb.toString();
}
/**
* Compare a ResultSet from getColumns() with ResultSetMetaData
* returned from a SELECT * against the table. This method
* handles situations where a full set of the columns are in
* the ResultSet.
* The first action is to call rs.next().
* The ResultSet will be closed by this method.
* @param rs resultset to crossCheck
* @param partial used to indicate if ordinal position should get checked
* @param odbc - flag to indicate if this was a resultset obtained
* from a JDBC (0) or ODBC (1) call.
* @throws SQLException
*/
private void crossCheckGetColumnsAndResultSetMetaData(ResultSet rs,
boolean partial, int odbc)
throws Exception
{
Statement s = createStatement();
while (rs.next())
{
String schema_ = rs.getString("TABLE_SCHEM");
String table = rs.getString("TABLE_NAME");
ResultSet rst = s.executeQuery(
"SELECT * FROM " + JDBC.escape(schema_, table));
ResultSetMetaData rsmdt = rst.getMetaData();
for (int col = 1; col <= rsmdt.getColumnCount() ; col++)
{
if (!partial) {
if (col != 1)
assertTrue(rs.next());
assertEquals("ORDINAL_POSITION",
col, rs.getInt("ORDINAL_POSITION"));
}
assertEquals("TABLE_CAT",
"", rs.getString("TABLE_CAT"));
assertEquals("TABLE_SCHEM",
schema_, rs.getString("TABLE_SCHEM"));
assertEquals("TABLE_NAME",
table, rs.getString("TABLE_NAME"));
crossCheckGetColumnRowAndResultSetMetaData(rs, rsmdt, odbc);
if (partial)
break;
}
rst.close();
}
rs.close();
s.close();
}
/**
* Cross check a single row from getColumns() with ResultSetMetaData
* for a SELECT * from the same table.
* @param rs ResultSet from getColumns already positioned on the row.
* @param rsmdt ResultSetMetaData for the SELECT *
* @param odbc 0 for JDBC call, 1 for ODBC. Needed to allow for difference
* in using BUFFER_LENGTH (ODBC) or no(JDBC).
* @throws SQLException
*/
public void crossCheckGetColumnRowAndResultSetMetaData(
ResultSet rs, ResultSetMetaData rsmdt, int odbc)
throws Exception
{
int col = rs.getInt("ORDINAL_POSITION");
Version dataVersion = getDataVersion( getConnection() );
String catalogName = rs.getString("TABLE_CAT");
String schemaName = rs.getString("TABLE_SCHEM");
String tableName = rs.getString("TABLE_NAME");
// Check that the catalog/schema/table names reported by the
// ResultSetMetaData are correct. Note that for views, RSMD will
// return data for the underlying table, not for the view itself.
// Therefore, skip the check for views.
ResultSet views =
rs.getStatement().getConnection().getMetaData().getTables(
catalogName, schemaName, tableName, JDBC.GET_TABLES_VIEW);
boolean isView = JDBC.assertDrainResults(views) > 0;
if (!isView) {
assertEquals("RSMD.getCatalogName",
catalogName, rsmdt.getCatalogName(col));
assertEquals("RSMD.getSchemaName",
schemaName, rsmdt.getSchemaName(col));
assertEquals("RSMD.getTableName",
tableName, rsmdt.getTableName(col));
}
assertEquals("COLUMN_NAME",
rsmdt.getColumnName(col), rs.getString("COLUMN_NAME"));
// DERBY-2285 BOOLEAN columns appear different on
// network client.
// DMD returns BOOLEAN
// RSMD returns SMALLINT
int dmdColumnType = rs.getInt("DATA_TYPE");
if (dmdColumnType == Types.JAVA_OBJECT && usingDerbyNetClient()
&& ( dataVersion.compareTo( new Version( 10, 6, 0, 0 ) ) < 0 ) )
{
// DMD returns JAVA_OBJECT
// RSMD returns LONGVARBINARY!
assertEquals("DATA_TYPE",
Types.LONGVARBINARY, rsmdt.getColumnType(col));
}
else if (dmdColumnType == Types.VARBINARY && usingDerbyNetClient())
{
// DMD returns different type name to RSMD
assertEquals("DATA_TYPE",
Types.VARBINARY, rsmdt.getColumnType(col));
}
else if (dmdColumnType == Types.BINARY && usingDerbyNetClient())
{
// DMD returns different type name to RSMD
assertEquals("DATA_TYPE",
Types.BINARY, rsmdt.getColumnType(col));
}
else if (dmdColumnType == Types.NUMERIC && usingDerbyNetClient())
{
// DERBY-584 inconsistency in numeric & decimal
assertEquals("DATA_TYPE",
Types.DECIMAL, rsmdt.getColumnType(col));
assertEquals("TYPE_NAME",
"DECIMAL", rsmdt.getColumnTypeName(col));
assertEquals("TYPE_NAME",
"NUMERIC", rs.getString("TYPE_NAME"));
}
else
{
assertEquals("DATA_TYPE",
rsmdt.getColumnType(col), rs.getInt("DATA_TYPE"));
assertEquals("TYPE_NAME",
rsmdt.getColumnTypeName(col), rs.getString("TYPE_NAME"));
}
/*
if (dmdColumnType != Types.JAVA_OBJECT) {
System.out.println("TYPE " + rs.getInt("DATA_TYPE"));
System.out.println(JDBC.escape(schema, table) + " " + rs.getString("COLUMN_NAME"));
assertEquals("COLUMN_SIZE",
rsmdt.getPrecision(col), rs.getInt("COLUMN_SIZE"));
}
*/
// not used by JDBC spec, but by ODBC
if (odbc == 0)
{
assertEquals("BUFFER_LENGTH", 0, rs.getInt("BUFFER_LENGTH"));
assertTrue("BUFFER_LENGTH", rs.wasNull());
}
else
{
if (col == 0)
assertEquals("BUFFER_LENGTH", 0, rs.getInt("BUFFER_LENGTH"));
else
assertTrue(rs.getInt("BUFFER_LENGTH") != 0);
}
/*
assertEquals("DECIMAL_DIGITS",
rsmdt.getScale(col), rs.getInt("DECIMAL_DIGITS"));
*/
// This assumes the constants defined by DMD and ResultSet
// for nullability are equal. They are by inspection
// and since they are static final and part of a defined
// api by definition they cannot change. We also
// check statically this is true in the testConstants fixture.
assertEquals("NULLABLE",
rsmdt.isNullable(col), rs.getInt("NULLABLE"));
// REMARKS set to empty string by Derby
assertEquals("REMARKS", "", rs.getString("REMARKS"));
// COLUMN_DEF ??
if (odbc == 0)
{
// both unused by JDBC spec
assertEquals("SQL_DATA_TYPE", 0, rs.getInt("SQL_DATA_TYPE"));
assertTrue(rs.wasNull());
assertEquals("SQL_DATETIME_SUB", 0, rs.getInt("SQL_DATETIME_SUB"));
assertTrue(rs.wasNull());
}
else {
// ODBC uses formula:
// DATA_TYPE = 10 * SQL_DATA_TYPE + SQL_DATETIME_SUB,
// e.g SQL_TIME_STAMP = 10 * SQL_DATETIME + SQL_CODE_TIMESTAMP
// 93 = 10 * 9 + 3
if (dmdColumnType == 91)
{
assertTrue(rs.getInt("SQL_DATA_TYPE")== 9);
assertTrue(rs.getInt("SQL_DATETIME_SUB")== 1);
}
else if (dmdColumnType == 92)
{
assertTrue(rs.getInt("SQL_DATA_TYPE")== 9);
assertTrue(rs.getInt("SQL_DATETIME_SUB")== 2);
}
else if (dmdColumnType == 93)
{
assertTrue(rs.getInt("SQL_DATA_TYPE")== 9);
assertTrue(rs.getInt("SQL_DATETIME_SUB")== 3);
}
else
assertTrue(rs.getInt("SQL_DATA_TYPE") == dmdColumnType);
}
// IS_NULLABLE
switch (rsmdt.isNullable(col))
{
case ResultSetMetaData.columnNoNulls:
assertEquals("IS_NULLABLE", "NO", rs.getString("IS_NULLABLE"));
break;
case ResultSetMetaData.columnNullable:
assertEquals("IS_NULLABLE", "YES", rs.getString("IS_NULLABLE"));
break;
case ResultSetMetaData.columnNullableUnknown:
assertEquals("IS_NULLABLE", "", rs.getString("IS_NULLABLE"));
break;
default:
fail("invalid return from rsmdt.isNullable(col)");
}
// SCOPE not supported
assertNull("SCOPE_CATALOG", rs.getString("SCOPE_CATALOG"));
assertNull("SCOPE_CATLOG", rs.getString("SCOPE_CATLOG"));
assertNull("SCOPE_SCHEMA", rs.getString("SCOPE_SCHEMA"));
assertNull("SCOPE_TABLE", rs.getString("SCOPE_TABLE"));
// DISTINCT not supported
assertEquals("SOURCE_DATA_TYPE", 0, rs.getShort("SOURCE_DATA_TYPE"));
assertTrue(rs.wasNull());
// IS_AUTOINCREMENT added in JDBC 4.0
assertEquals("IS_AUTOINCREMENT",
rsmdt.isAutoIncrement(col) ? "YES" : "NO",
rs.getString("IS_AUTOINCREMENT"));
assertFalse(rs.wasNull());
}
/**
* Implement ODBC equivalent for getColumns - SYSIBM.SQLCOLUMNS
*
* @param catalog
* @param schemaPattern
* @param tableNamePattern
* @param columnNamePattern
*
* @return result set of query
* @throws java.sql.SQLException
*/
private ResultSet getColumnsODBC(
String catalog, String schemaPattern, String tableNamePattern,
String columnNamePattern)
throws SQLException
{
CallableStatement cs = prepareCall("CALL SYSIBM.SQLCOLUMNS(" +
"?, ?, ?, ?, 'DATATYPE=''ODBC''')");
cs.setString(1, catalog);
cs.setString(2, schemaPattern);
cs.setString(3, tableNamePattern);
cs.setString(4, columnNamePattern);
cs.execute();
return cs.getResultSet();
}
/**
* Helper method for testing getColumns - calls dmd.getColumns for
* the JDBC call, and getColumnsODBC for the ODBC procedure
* @param catalog
* @param schemaPattern
* @param tableNamePattern
* @param columnNamePattern
* @return Array of the two result sets of columns
* @throws SQLException
*/
private ResultSet[] getColumns(
String catalog, String schemaPattern, String tableNamePattern,
String columnNamePattern)
throws SQLException
{
ResultSet[] rss = new ResultSet[2];
DatabaseMetaData dmd = getDMD();
rss[0]= dmd.getColumns(catalog, schemaPattern, tableNamePattern,
columnNamePattern);
rss[1]= getColumnsODBC(catalog, schemaPattern, tableNamePattern,
columnNamePattern);
return rss;
}
/**
* Test getTableTypes()
*
* @throws java.sql.SQLException
*/
public void testTableTypes() throws SQLException
{
DatabaseMetaData dmd = getDMD();
ResultSet rs = dmd.getTableTypes();
assertMetaDataResultSet(rs,
new String[] {
"TABLE_TYPE"
},
new int[] {
Types.VARCHAR
}
, null
);
JDBC.assertFullResultSet(rs, new String[][]
{
{"SYNONYM"},{"SYSTEM TABLE"},{"TABLE"},{"VIEW"},
}, true);
rs.close();
}
/**
* Test getTypeInfo
*
* @throws java.lang.Exception
*/
public void testGetTypeInfo() throws Exception
{
// Client returns BOOLEAN type from the engine as SMALLINT
int BOOLEAN = Types.BOOLEAN;
String[] JDBC_COLUMN_NAMES = new String[] {
"TYPE_NAME", "DATA_TYPE", "PRECISION", "LITERAL_PREFIX",
"LITERAL_SUFFIX", "CREATE_PARAMS", "NULLABLE", "CASE_SENSITIVE",
"SEARCHABLE", "UNSIGNED_ATTRIBUTE", "FIXED_PREC_SCALE",
"AUTO_INCREMENT", "LOCAL_TYPE_NAME",
"MINIMUM_SCALE", "MAXIMUM_SCALE",
"SQL_DATA_TYPE", "SQL_DATETIME_SUB",
"NUM_PREC_RADIX"
};
int[] JDBC_COLUMN_TYPES = new int[] {
Types.VARCHAR, Types.INTEGER, Types.INTEGER, Types.VARCHAR,
Types.VARCHAR, Types.VARCHAR, Types.SMALLINT, BOOLEAN,
Types.SMALLINT, BOOLEAN, BOOLEAN,
BOOLEAN, Types.VARCHAR,
Types.SMALLINT, Types.SMALLINT,
Types.INTEGER, Types.INTEGER,
Types.INTEGER
};
boolean[] JDBC_COLUMN_NULLABILITY = {
false, false, true, true,
true, true, false, false,
false, true, false,
true, true,
true, true,
true, true,
true
};
// DERBY-2307 Nullablity is wrong for column 1 (1-based)
// Modify JDBC_COLUMN_NULLABILITY to reflect current reality
JDBC_COLUMN_NULLABILITY[1 - 1] = true;
ResultSet rs = getDMD().getTypeInfo();
assertMetaDataResultSet(rs, JDBC_COLUMN_NAMES, JDBC_COLUMN_TYPES
, JDBC_COLUMN_NULLABILITY
);
/*
Derby-2258 Removed 3 data types which are not supported by Derby
and added XML data type which is supported by Derby
*/
List<Integer> supportedTypes = new ArrayList<Integer>(Arrays.asList(
Types.BIGINT, Types.BINARY, Types.BLOB, Types.BOOLEAN,
Types.CHAR, Types.CLOB, Types.DATE,
Types.DECIMAL, Types.DOUBLE, Types.FLOAT,
Types.INTEGER, Types.LONGVARBINARY, Types.LONGVARCHAR,
Types.NUMERIC, Types.REAL, Types.SMALLINT,
Types.TIME, Types.TIMESTAMP, Types.VARBINARY,
Types.VARCHAR, JDBC.SQLXML, Types.JAVA_OBJECT
));
Version dataVersion = getDataVersion(getConnection());
boolean booleanSupported =
dataVersion.compareTo(new Version(10, 7, 0, 0)) >= 0;
// DERBY-4946: Boolean isn't supported if DB is soft-upgraded from
// pre-10.7 version
if (!booleanSupported) {
supportedTypes.remove(Integer.valueOf(Types.BOOLEAN));
}
// Rows are returned from getTypeInfo in order of
// "DATA_TYPE" (which is a constant from java.sql.Types)
Collections.sort(supportedTypes);
int offset = 0;
while (rs.next()) {
// TYPE_NAME (column 1)
String typeName = rs.getString("TYPE_NAME");
assertNotNull(typeName);
// DATA_TYPE (column 2)
int type = rs.getInt("DATA_TYPE");
assertFalse(rs.wasNull());
if (!supportedTypes.get(offset).equals(type))
{
fail("Unexpected type " + typeName);
}
else
{
offset++;
}
// PRECISION (column 3)
int precision = -1;
switch (type)
{
case Types.BOOLEAN:
precision = 1;
break;
case Types.BINARY:
case Types.CHAR:
precision = 254;
break;
case Types.BLOB:
case Types.CLOB:
precision = Integer.MAX_VALUE;
break;
case Types.DATE:
precision = 10;
break;
case Types.TIME:
precision = 8;
break;
case Types.TIMESTAMP:
precision = 29;
break;
case Types.DECIMAL:
case Types.NUMERIC:
precision = 31;
break;
case Types.DOUBLE:
case Types.FLOAT:
precision = 52;
break;
case Types.REAL:
precision = 23;
break;
case Types.BIGINT:
precision = 19;
break;
case Types.INTEGER:
precision = 10;
break;
case Types.SMALLINT:
precision = 5;
break;
case Types.LONGVARBINARY:
case Types.LONGVARCHAR:
precision = 32700;
break;
/*
Derby-2260 Correcting the precision value for VARCHAR FOR BIT DATA
Thus this test also now expects the correct value i.e. 32672
Also adding precision check for SQLXML data type
*/
case Types.VARBINARY:
precision = 32672;
break;
case Types.VARCHAR:
precision = 32672;
break;
case Types.JAVA_OBJECT:
case JDBC.SQLXML:
precision = 0;
break;
}
assertEquals("PRECISION " + typeName,
precision, rs.getInt("PRECISION"));
/*
Precision value is null for XML and OBJECT data type
*/
if ( (typeName.equals("XML" )) || (typeName.equals("OBJECT" )) )
{ assertTrue(rs.wasNull()); }
else
{ assertFalse(rs.wasNull()); }
// LITERAL_PREFIX (column 4)
// LITERAL_SUFFIX (column 5)
// CREATE_PARAMS (column 6)
String createParams;
switch (type)
{
case Types.CHAR:
case Types.VARCHAR:
case Types.BLOB:
case Types.CLOB:
case Types.BINARY:
case Types.VARBINARY:
createParams = "length";
break;
case Types.DECIMAL:
case Types.NUMERIC:
createParams = "precision,scale";
break;
case Types.FLOAT:
createParams = "precision";
break;
default:
createParams = null;
break;
}
assertEquals("CREATE_PARAMS " + typeName,
createParams, rs.getString("CREATE_PARAMS"));
// NULLABLE (column 7) - all types are nullable in Derby
assertEquals("NULLABLE " + typeName,
DatabaseMetaData.typeNullable, rs.getInt("NULLABLE"));
assertFalse(rs.wasNull());
// CASE_SENSITIVE (column 8)
// SEARCHABLE (column 9) - most types searchable
{
int searchable;
switch (type)
{
/*
Derby-2259 Correcting the searchable value for
LONGVARBINARY, LONGVARCHAR & BLOB data type
also adding SQLXML data type in the test.
*/
case Types.LONGVARBINARY:
searchable = DatabaseMetaData.typePredNone;
break;
case Types.LONGVARCHAR:
searchable = DatabaseMetaData.typePredChar;
break;
case Types.BLOB:
searchable = DatabaseMetaData.typePredNone;
break;
case Types.CLOB:
searchable = DatabaseMetaData.typePredChar;
break;
case Types.CHAR:
case Types.VARCHAR:
searchable = DatabaseMetaData.typeSearchable;
break;
case JDBC.SQLXML:
searchable = DatabaseMetaData.typePredNone;
break;
default:
searchable = DatabaseMetaData.typePredBasic;
break;
}
assertEquals("SEARCHABLE " + typeName,
searchable, rs.getInt("SEARCHABLE"));
}
// UNSIGNED_ATTRIBUTE (column 10)
//assertFalse("UNSIGNED_ATTRIBUTE " + typeName,
// rs.getBoolean("UNSIGNED_ATTRIBUTE"));
// FIXED_PREC_SCALE (column 11)
boolean fixedScale = type == Types.DECIMAL || type == Types.NUMERIC;
assertEquals("FIXED_PREC_SCALE " + typeName,
fixedScale, rs.getBoolean("FIXED_PREC_SCALE"));
assertFalse(rs.wasNull());
// AUTO_INCREMENT (column 12)
boolean autoIncrement;
switch (type)
{
case Types.BIGINT:
case Types.INTEGER:
case Types.SMALLINT:
autoIncrement = true;
break;
default:
autoIncrement = false;
break;
}
assertEquals("AUTO_INCREMENT " + typeName,
autoIncrement, rs.getBoolean("AUTO_INCREMENT"));
// LOCAL_TYPE_NAME (column 13) always the same as TYPE_NAME
assertEquals("LOCAL_TYPE_NAME " + typeName,
typeName, rs.getString("LOCAL_TYPE_NAME"));
int maxScale;
boolean hasScale = true;
switch (type)
{
case Types.DECIMAL:
case Types.NUMERIC:
maxScale = 31; // Max Scale for Decimal & Numeric is 31: Derby-2262
break;
case Types.TIMESTAMP:
maxScale = 9;
break;
case Types.SMALLINT:
case Types.INTEGER:
case Types.BIGINT:
case Types.DATE:
case Types.TIME:
maxScale = 0;
break;
default:
maxScale = 0;
hasScale = false;
break;
}
// MINIMUM_SCALE (column 14)
assertEquals("MINIMUM_SCALE " + typeName,
0, rs.getInt("MINIMUM_SCALE"));
assertEquals("MINIMUM_SCALE (wasNull) " + typeName,
!hasScale, rs.wasNull());
// MAXIMUM_SCALE (column 15)
assertEquals("MAXIMUM_SCALE " + typeName,
maxScale, rs.getInt("MAXIMUM_SCALE"));
assertEquals("MAXIMUM_SCALE (wasNull)" + typeName,
!hasScale, rs.wasNull());
// SQL_DATA_TYPE (column 16) - Unused
assertEquals("SQL_DATA_TYPE " + typeName,
0, rs.getInt("SQL_DATA_TYPE"));
assertTrue(rs.wasNull());
// SQL_DATETIME_SUB (column 17) - Unused
assertEquals("SQL_DATETIME_SUB " + typeName,
0, rs.getInt("SQL_DATETIME_SUB"));
assertTrue(rs.wasNull());
// NUM_PREC_RADIX (column 18)
}
rs.close();
// Now check the ODBC version:
// ODBC column names & types differ from JDBC slightly.
// ODBC has one more column.
String[] ODBC_COLUMN_NAMES = new String[19];
System.arraycopy(JDBC_COLUMN_NAMES, 0, ODBC_COLUMN_NAMES, 0,
JDBC_COLUMN_NAMES.length);
ODBC_COLUMN_NAMES[2] = "COLUMN_SIZE";
ODBC_COLUMN_NAMES[11] = "AUTO_UNIQUE_VAL";
ODBC_COLUMN_NAMES[18] = "INTERVAL_PRECISION";
int[] ODBC_COLUMN_TYPES = new int[ODBC_COLUMN_NAMES.length];
System.arraycopy(JDBC_COLUMN_TYPES, 0, ODBC_COLUMN_TYPES, 0,
JDBC_COLUMN_TYPES.length);
ODBC_COLUMN_TYPES[1] = Types.SMALLINT; // DATA_TYPE
ODBC_COLUMN_TYPES[7] = Types.SMALLINT; // CASE_SENSITIVE
ODBC_COLUMN_TYPES[9] = Types.SMALLINT; // UNSIGNED_ATTRIBUTE
ODBC_COLUMN_TYPES[10] = Types.SMALLINT; // FIXED_PREC_SCALE
ODBC_COLUMN_TYPES[11] = Types.SMALLINT; // AUTO_UNIQUE_VAL
ODBC_COLUMN_TYPES[15] = Types.SMALLINT; // SQL_DATA_TYPE
ODBC_COLUMN_TYPES[16] = Types.SMALLINT; // SQL_DATETIME_SUB
ODBC_COLUMN_TYPES[18] = Types.SMALLINT; // INTERVAL_PRECISION
// ODBC_COLUMN_NULLABILTY is the same as JDBC except for:
// column 16 - SQL_DATA_TYPE is NULL in JDBC but a valid non-null value in ODBC
// column 19 - INTERVAL_PRECISION (extra column comapred to JDBC)
boolean[] ODBC_COLUMN_NULLABILITY = {
true, false, true, true,
true, true, false, false,
false, true, false,
true, true,
true, true,
false, true,
true,
true
};
CallableStatement cs = prepareCall(
"CALL SYSIBM.SQLGETTYPEINFO (0, 'DATATYPE=''ODBC''')");
cs.execute();
ResultSet odbcrs = cs.getResultSet();
assertNotNull(odbcrs);
assertMetaDataResultSet(odbcrs, ODBC_COLUMN_NAMES, ODBC_COLUMN_TYPES,
ODBC_COLUMN_NULLABILITY);
odbcrs.close();
cs.close();
}
/*
* Check the shape of the ResultSet from any getColumns call.
*/
private void checkColumnsShape(ResultSet rs, int odbc) throws SQLException
{
int[] columnTypes = new int[] {
Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
Types.INTEGER, Types.VARCHAR, Types.INTEGER, Types.INTEGER,
Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.VARCHAR,
Types.VARCHAR, Types.INTEGER, Types.INTEGER, Types.INTEGER,
Types.INTEGER, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
Types.VARCHAR, Types.SMALLINT, Types.VARCHAR, Types.VARCHAR,
Types.VARCHAR
};
boolean[] nullability =
{
false, false, false, false,
true, true, true, true,
true, true, true, false,
true, true, true, true,
false, false, true, true,
true, true, false, false,
true
};
if (odbc == 1)
{
columnTypes[4] = Types.SMALLINT;
columnTypes[8] = Types.SMALLINT;
columnTypes[9] = Types.SMALLINT;
columnTypes[10] = Types.SMALLINT;
columnTypes[13] = Types.SMALLINT;
columnTypes[14] = Types.SMALLINT;
}
assertMetaDataResultSet
(
rs,
new String[]
{
"TABLE_CAT", "TABLE_SCHEM", "TABLE_NAME", "COLUMN_NAME",
"DATA_TYPE", "TYPE_NAME", "COLUMN_SIZE", "BUFFER_LENGTH",
"DECIMAL_DIGITS", "NUM_PREC_RADIX", "NULLABLE", "REMARKS",
"COLUMN_DEF", "SQL_DATA_TYPE", "SQL_DATETIME_SUB", "CHAR_OCTET_LENGTH",
"ORDINAL_POSITION", "IS_NULLABLE", "SCOPE_CATALOG", "SCOPE_SCHEMA",
"SCOPE_TABLE", "SOURCE_DATA_TYPE", "IS_AUTOINCREMENT", "IS_GENERATEDCOLUMN",
"SCOPE_CATLOG"
},
columnTypes,
nullability
);
}
/**
* Check the shape of the ResultSet from any getTables call.
* Note nullability of TABLE_CAT is not nullable for Derby
* even though it doesn't support catalogs because the
* SQL query returns a constant (empty string) for
* a table's catalog.
* @param rs
* @throws java.sql.SQLException
*/
private void checkTablesShape(ResultSet rs) throws SQLException
{
assertMetaDataResultSet(rs,
new String[] {
"TABLE_CAT", "TABLE_SCHEM", "TABLE_NAME", "TABLE_TYPE",
"REMARKS", "TYPE_CAT", "TYPE_SCHEM", "TYPE_NAME",
"SELF_REFERENCING_COL_NAME", "REF_GENERATION"
},
new int[] {
Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
Types.VARCHAR, Types.VARCHAR
}
, new boolean[] {
false, false, false, true, // TABLE_SCHEM cannot be NULL in Derby
false, true, true, true,
true, true
}
);
}
/**
* Check the shape of the ResultSet from any getCatlogs call.
* @param rs
* @throws java.sql.SQLException
*/
private void checkCatalogsShape(ResultSet rs) throws SQLException
{
assertMetaDataResultSet(rs,
new String[] {
"TABLE_CAT"
},
new int[] {
Types.CHAR
}
, new boolean[] {false}
);
}
/**
* Check the shape of the ResultSet from any
* getVersionColumns call.
* @param rs
* @throws java.sql.SQLException
*/
private static void checkVersionColumnsShape(ResultSet[] rs) throws SQLException
{
String [] columnNames = new String[] {
"SCOPE", "COLUMN_NAME", "DATA_TYPE", "TYPE_NAME",
"COLUMN_SIZE", "BUFFER_LENGTH", "DECIMAL_DIGITS", "PSEUDO_COLUMN"
};
int[] columnTypes = new int[] {
Types.SMALLINT, Types.VARCHAR, Types.INTEGER, Types.VARCHAR,
Types.INTEGER, Types.INTEGER, Types.SMALLINT, Types.SMALLINT};
assertMetaDataResultSet(rs[0], columnNames, columnTypes, null);
columnTypes[2] = Types.SMALLINT;
assertMetaDataResultSet(rs[1], columnNames, columnTypes, null);
}
public static void assertMetaDataResultSet(ResultSet rs,
String[] columnNames, int[] columnTypes,
boolean[] nullability) throws SQLException
{
assertEquals(ResultSet.TYPE_FORWARD_ONLY, rs.getType());
assertEquals(ResultSet.CONCUR_READ_ONLY, rs.getConcurrency());
//assertNull(rs.getStatement());
if (columnNames != null)
JDBC.assertDatabaseMetaDataColumns(rs,
nullability, columnTypes, columnNames);
}
/*
** Set of escaped functions.
*/
/**
* JDBC escaped numeric functions - JDBC 3.0 C.1
* @throws SQLException
*/
public void testNumericFunctions() throws SQLException
{
escapedFunctions(NUMERIC_FUNCTIONS,
getDMD().getNumericFunctions());
}
/**
* JDBC escaped string functions - JDBC 3.0 C.2
* @throws SQLException
*/
public void testStringFunctions() throws SQLException
{
escapedFunctions(STRING_FUNCTIONS,
getDMD().getStringFunctions());
}
/**
* JDBC escaped date time functions - JDBC 3.0 C.3
* @throws SQLException
*/
public void testTimeDataFunctions() throws SQLException
{
escapedFunctions(TIMEDATE_FUNCTIONS,
getDMD().getTimeDateFunctions());
}
/**
* JDBC escaped system functions - JDBC 3.0 C.4
* @throws SQLException
*/
public void testSystemFunctions() throws SQLException
{
escapedFunctions(SYSTEM_FUNCTIONS,
getDMD().getSystemFunctions());
}
/**
* Check that the list of escaped functions provided by
* the driver is a strict subet of the specified set,
* the list does not contain duplicates, all the functions
* listed can be executed and that if a function is not
* in the list but is specified it cannot be executed.
*
* @param specList
* @param metaDataList
*
* @throws java.sql.SQLException
*/
private void escapedFunctions(String[][] specList, String metaDataList)
throws SQLException
{
boolean[] seenFunction = new boolean[specList.length];
StringTokenizer st = new StringTokenizer(metaDataList, ",");
while (st.hasMoreTokens())
{
String function = st.nextToken();
// find this function in the list
boolean isSpecFunction = false;
for (int f = 0; f < specList.length; f++)
{
String[] specDetails = specList[f];
if (function.equals(specDetails[0]))
{
// Matched spec.
if (seenFunction[f])
fail("Function in list twice: " + function);
seenFunction[f] = true;
isSpecFunction = true;
executeEscaped(specDetails);
break;
}
}
if (!isSpecFunction)
{
fail("Non-JDBC spec function in list: " + function);
}
}
// Now see if any speced functions are not in the metadata list
for (int f = 0; f < specList.length; f++)
{
if (seenFunction[f])
continue;
String[] specDetails = specList[f];
// bug DERBY-723 CHAR maps to wrong function
if ("CHAR".equals(specDetails[0]))
continue;
try {
executeEscaped(specDetails);
fail("function works but not declared in list: " + specDetails[0]);
} catch (SQLException e) {
assertSQLState("42X01", e);
}
}
}
/**
* Test we can execute a function listed as a supported
* JDBC escaped function. We don't care about the actual
* return value, that should be tested elsewhere in
* the specific test of a function.
*
* @param specDetails
*
* @throws java.sql.SQLException
*/
private void executeEscaped(String[] specDetails)
throws SQLException
{
String sql = "VALUES { fn " + specDetails[0] + "(";
for (int p = 0; p < specDetails.length - 1; p++)
{
if (p != 0)
sql = sql + ", ";
sql = sql + specDetails[p + 1];
}
sql = sql + ") }";
PreparedStatement ps = prepareStatement(sql);
ResultSet rs = ps.executeQuery();
JDBC.assertDrainResults(rs);
rs.close();
ps.close();
}
/**
* Return a list of all valid supported datatypes as Strings
* suitable for use in any SQL statement where a SQL type is
* expected. For variable sized types the string will
* have random valid length information. E.g. CHAR(37).
* @param conn current connection
* @return list of types
* @throws java.sql.SQLException
*/
public static List<String> getSQLTypes(Connection conn) throws SQLException
{
List<String> list = new ArrayList<String>();
Random rand = new Random();
ResultSet rs = conn.getMetaData().getTypeInfo();
while (rs.next())
{
String typeName = rs.getString("TYPE_NAME");
// skip the OBJECT type because this does not correspond to an
// actual data type but merely flags the fact that the database
// supports the creation of user defined types
if ( "OBJECT".equals( typeName ) ) { continue; }
String createParams = rs.getString("CREATE_PARAMS");
if (createParams == null) {
// Type name stands by itself.
list.add(typeName);
continue;
}
if (createParams.contains("length"))
{
int maxLength = rs.getInt("PRECISION");
// nextInt returns a value between 0 and maxLength-1
int length = rand.nextInt(maxLength) + 1;
int paren = typeName.indexOf('(');
if (paren == -1) {
list.add(typeName + "(" + length + ")");
} else {
StringBuilder sb = new StringBuilder();
sb.append(typeName.substring(0, paren+1));
sb.append(length);
sb.append(typeName.substring(paren+1));
list.add(sb.toString());
}
continue;
}
if (createParams.contains("scale"))
{
int maxPrecision = rs.getInt("PRECISION");
StringBuilder sb = new StringBuilder();
int precision = rand.nextInt(maxPrecision) + 1;
sb.append(typeName);
sb.append("(");
sb.append(precision);
// Most DECIMAL usage does have a scale
// but randomly pick some that do not.
if (rand.nextInt(100) < 95) {
sb.append(",");
sb.append(rand.nextInt(precision+1));
}
sb.append(")");
list.add(sb.toString());
continue;
}
if (createParams.contains("precision"))
{
list.add(typeName);
continue;
}
fail("unknown how to generate valid type for " + typeName
+ " CREATE_PARAMS=" + createParams);
}
return list;
}
/**
* Given a valid SQL type return the corresponding
* JDBC type identifier from java.sql.Types.
* Will assert if the type is not known
* (in future, currently just return Types.NULL).
*
* @param type
* @return JDBC type identifier (java.sql.Types.*)
*/
public static int getJDBCType(String type)
{
if ("SMALLINT".equals(type))
return Types.SMALLINT;
if ("INTEGER".equals(type) || "INT".equals(type))
return Types.INTEGER;
if ("BIGINT".equals(type))
return Types.BIGINT;
if (type.equals("FLOAT") || type.startsWith("FLOAT("))
return Types.FLOAT;
if (type.equals("REAL"))
return Types.REAL;
if ("DOUBLE".equals(type) || "DOUBLE PRECISION".equals(type))
return Types.DOUBLE;
if ("DATE".equals(type))
return Types.DATE;
if ("TIME".equals(type))
return Types.TIME;
if ("TIMESTAMP".equals(type))
return Types.TIMESTAMP;
if (type.equals("DECIMAL") || type.startsWith("DECIMAL("))
return Types.DECIMAL;
if (type.equals("NUMERIC") || type.startsWith("NUMERIC("))
return Types.NUMERIC;
if (type.endsWith("FOR BIT DATA")) {
if (type.startsWith("CHAR"))
return Types.BINARY;
if (type.startsWith("CHARACTER"))
return Types.BINARY;
if (type.startsWith("VARCHAR"))
return Types.VARBINARY;
if (type.startsWith("CHARACTER VARYING"))
return Types.VARBINARY;
if (type.startsWith("CHAR VARYING"))
return Types.VARBINARY;
}
if ("LONG VARCHAR".equals(type))
return Types.LONGVARCHAR;
if ("LONG VARCHAR FOR BIT DATA".equals(type))
return Types.LONGVARBINARY;
if (type.equals("CHAR") || type.startsWith("CHAR("))
return Types.CHAR;
if (type.equals("CHARACTER") ||
type.startsWith("CHARACTER("))
return Types.CHAR;
if (type.equals("VARCHAR") || type.startsWith("VARCHAR("))
return Types.VARCHAR;
if (type.equals("CHARACTER VARYING") ||
type.startsWith("CHARACTER VARYING("))
return Types.VARCHAR;
if (type.equals("CHAR VARYING") ||
type.startsWith("CHAR VARYING("))
return Types.VARCHAR;
if (type.equals("BLOB") || type.startsWith("BLOB("))
return Types.BLOB;
if (type.equals("BINARY LARGE OBJECT") ||
type.startsWith("BINARY LARGE OBJECT("))
return Types.BLOB;
if (type.equals("CLOB") || type.startsWith("CLOB("))
return Types.CLOB;
if (type.equals("CHARACTER LARGE OBJECT") ||
type.startsWith("CHARACTER LARGE OBJECT("))
return Types.CLOB;
if ("XML".equals(type))
return JDBC.SQLXML;
if (type.equals("BOOLEAN"))
return Types.BOOLEAN;
fail("Unexpected SQL type: " + type);
return Types.NULL;
}
/**
* Given a valid SQL type return the corresponding
* precision/length for this specific value
* if the type is variable, e.g. CHAR(5) will
* return 5, but LONG VARCHAR will return 0.
*
* @param jdbcType
* @param type
*
* @return precision or length
*/
public static int getPrecision(int jdbcType, String type)
{
switch (jdbcType)
{
case Types.CHAR:
case Types.VARCHAR:
case Types.CLOB:
case Types.BINARY:
case Types.VARBINARY:
case Types.BLOB:
int lp = type.indexOf('(');
int rp = type.indexOf(')');
int precision =
Integer.parseInt(type.substring(lp+1, rp));
return precision;
default:
return 0;
}
}
/**
* Execute and check the ODBC variant of getImported/Exported keys, which
* uses the SQLFOREIGNKEYS system procedure to provide the same information
* to ODBC clients. Note that for "correctness" we just compare the results
* to those of the equivalent JDBC calls; this fixture assumes that the
* the JDBC calls return correct results (testing of the JDBC results occurs
* elsewhere, see fixtures testGetXXportedKeys()
*
* @throws java.sql.SQLException
* @throws java.io.IOException
*/
public void testGetXXportedKeysODBC() throws SQLException, IOException
{
Statement st = createStatement();
// Create some simple tables with primary/foreign keys.
st.execute("create table pkt1 (i int not null, c char(1) not null)");
st.execute("create table pkt2 (i int not null, c char(1) not null)");
st.execute("create table pkt3 (i int not null, c char(1) not null)");
st.execute("alter table pkt1 add constraint pk1 primary key (i)");
st.execute("alter table pkt2 add constraint pk2 primary key (c)");
st.execute("alter table pkt3 add constraint pk3 primary key (i, c)");
st.execute("create table fkt1 (fi int, fc char(1), vc varchar(80))");
st.execute("create table fkt2 (fi int, fc char(1), vc varchar(80))");
st.execute("alter table fkt1 add constraint fk1 foreign key (fi) " +
"references pkt1(i)");
st.execute("alter table fkt1 add constraint fk2 foreign key (fc) " +
"references pkt2(c)");
st.execute("alter table fkt2 add constraint fk3 foreign key (fi, fc) " +
"references pkt3(i, c)");
/* Check for all arguments NULL; SQLFOREIGNKEYS allows this, though
* there is no equivalent in JDBC.
*/
checkODBCKeys(null, schema, null, null, null, null);
/* Run equivalent of getImportedKeys(), getExportedKeys(),
* and getCrossReference for each of the primary/foreign
* key pairs.
*/
checkODBCKeys(null, schema, null, null, null, "FKT1");
checkODBCKeys(null, schema, "PKT1", null, null, null);
checkODBCKeys(null, schema, "PKT1", null, null, "FKT1");
checkODBCKeys(null, schema, null, null, null, "FKT2");
checkODBCKeys(null, schema, "PKT2", null, null, null);
checkODBCKeys(null, schema, "PKT2", null, null, "FKT2");
checkODBCKeys(null, schema, null, null, null, "FKT3");
checkODBCKeys(null, schema, "PKT3", null, null, null);
checkODBCKeys(null, schema, "PKT3", null, null, "FKT3");
// Reverse primary and foreign tables.
checkODBCKeys(null, schema, "FKT1", null, null, null);
checkODBCKeys(null, schema, null, null, null, "PKT3");
checkODBCKeys(null, schema, "FKT1", null, null, "PKT1");
checkODBCKeys(null, schema, "FKT2", null, null, "PKT2");
checkODBCKeys(null, schema, "FKT3", null, null, "PKT3");
// Mix-and-match primary key tables and foreign key tables.
checkODBCKeys(null, schema, "PKT1", null, null, "FKT2");
checkODBCKeys(null, schema, "PKT1", null, null, "FKT3");
checkODBCKeys(null, schema, "PKT2", null, null, "FKT3");
checkODBCKeys(null, schema, "FKT1", null, null, "PKT2");
checkODBCKeys(null, schema, "FKT1", null, null, "PKT3");
checkODBCKeys(null, schema, "FKT2", null, null, "PKT3");
// Cleanup.
st.execute("drop table fkt1");
st.execute("drop table fkt2");
st.execute("drop table pkt1");
st.execute("drop table pkt2");
st.execute("drop table pkt3");
st.close();
}
/**
* Execute a call to the ODBC system procedure "SQLFOREIGNKEYS"
* and verify the results by comparing them with the results of
* an equivalent JDBC call (if one exists).
*
* @param pCatalog
* @param pSchema
* @param pTable
* @param fCatalog
* @param fSchema
* @param fTable
*
* @throws java.sql.SQLException
* @throws java.io.IOException
*/
private void checkODBCKeys(String pCatalog, String pSchema,
String pTable, String fCatalog, String fSchema, String fTable)
throws SQLException, IOException
{
/* To mimic the behavior of the issue which prompted this test
* (DERBY-2758) we only send the "ODBC" option; we do *not*
* explicitly send the "IMPORTEDKEY=1" nor "EXPORTEDKEY=1"
* options, as DB2 Runtime Client does not send those, either.
* This effectively means that the SQLFOREIGNKEYS function
* will always be mapped to getCrossReference() internally.
* Since that worked fine prior to 10.3, we need to preserve
* that behavior if we want to maintain backward compatibility.
*/
CallableStatement cs = prepareCall(
"CALL SYSIBM.SQLFOREIGNKEYS(?, ?, ?, ?, ?, ?, " +
"'DATATYPE=''ODBC''')");
cs.setString(1, pCatalog);
cs.setString(2, pSchema);
cs.setString(3, pTable);
cs.setString(4, fCatalog);
cs.setString(5, fSchema);
cs.setString(6, fTable);
cs.execute();
ResultSet odbcrs = cs.getResultSet();
assertNotNull(odbcrs);
/* Returned ResultSet will have the same shape as
* DatabaseMetaData.getImportedKeys()
*/
checkODBCKeysShape(odbcrs);
/* Expect the contents of JDBC and ODBC metadata to be the same,
* except if both pTable and cTable are null. In that case
* ODBC treats everything as a wildcard (and so effectively
* returns all foreign key columns), while JDBC throws
* an error.
*/
ResultSet dmdrs = null;
if ((pTable != null) && (fTable == null))
dmdrs = getDMD().getExportedKeys(pCatalog, pSchema, pTable);
else if ((pTable == null) && (fTable != null))
dmdrs = getDMD().getImportedKeys(fCatalog, fSchema, fTable);
else if (pTable != null)
{
dmdrs = getDMD().getCrossReference(
pCatalog, pSchema, pTable, fCatalog, fSchema, fTable);
}
else
{
/* Must be the case of pTable and fTable both null. Check
* results for ODBC (one row for each foreign key column)
* and assert error for JDBC.
*/
JDBC.assertFullResultSet(odbcrs,
new String [][] {
{"",schema,"PKT1","I","",schema,"FKT1","FI",
"1","3","3","FK1","PK1","7"},
{"",schema,"PKT2","C","",schema,"FKT1","FC",
"1","3","3","FK2","PK2","7"},
{"",schema,"PKT3","I","",schema,"FKT2","FI",
"1","3","3","FK3","PK3","7"},
{"",schema,"PKT3","C","",schema,"FKT2","FC",
"2","3","3","FK3","PK3","7"}
});
try {
getDMD().getCrossReference(
pCatalog, pSchema, pTable, fCatalog, fSchema, fTable);
fail("Expected error from call to DMD.getCrossReference() " +
"with NULL primary and foreign key tables.");
} catch (SQLException se) {
/* Looks like embedded and client have different (but similar)
* errors for this...
*/
assertSQLState(usingEmbedded() ? "XJ103" : "XJ110", se);
}
}
/* If both pTable and fTable are null then dmdrs will be null, as
* well. So nothing to compare in that case.
*/
if (dmdrs != null)
{
// Next call closes both results sets as a side effect.
JDBC.assertSameContents(odbcrs, dmdrs);
}
cs.close();
}
/**
* Check the shape of the ResultSet from a call to the ODBC function
* SQLForeignKeys.
*
* @param rs
* @throws java.sql.SQLException
*/
private void checkODBCKeysShape(ResultSet rs) throws SQLException
{
assertMetaDataResultSet(rs,
// ODBC and JDBC agree on column names and types.
new String[] {
"PKTABLE_CAT", "PKTABLE_SCHEM", "PKTABLE_NAME", "PKCOLUMN_NAME",
"FKTABLE_CAT", "FKTABLE_SCHEM", "FKTABLE_NAME", "FKCOLUMN_NAME",
"KEY_SEQ", "UPDATE_RULE", "DELETE_RULE", "FK_NAME",
"PK_NAME", "DEFERRABILITY"
},
new int[] {
Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
Types.SMALLINT, Types.SMALLINT, Types.SMALLINT, Types.VARCHAR,
Types.VARCHAR, Types.SMALLINT
},
// Nullability comes from ODBC spec, not JDBC.
/* DERBY-2797: Nullability of columns in ODBC's SQLForeignKey
* result set is incorrect. Un-comment the correct boolean array
* when DERBY-2797 has been fixed.
*/
// incorrect
new boolean[] {
false, false, false, false,
false, false, false, false,
true, true, true, false,
false, true
}
// correct
/* new boolean[] {
true, true, false, false,
true, true, false, false,
false, true, true, true,
true, true
} */
);
}
/**
* Test getBestRowIdentifier
* @throws SQLException
*/
public void testGetBestRowIdentifier() throws SQLException
{
Statement st = createStatement();
// First, create the test tables and indexes/keys
// Create 5 tables which have only one best row identifier
st.execute("create table brit1 (i int not null primary key, j int)");
st.execute("create table brit2 (i int not null unique, j int)");
// adding not null unique to j - otherwise brit2 & brit3 would be same.
st.execute("create table brit3 (i int not null unique, " +
"j int not null unique)");
st.execute("create table brit4 (i int, j int)");
st.execute("create unique index brit4i on brit4(i)");
st.execute("create table brit5 (i int, j int)");
// following have more than one best row identifier
st.execute("create table brit6 (i int not null unique, " +
"j int not null primary key)");
// PK preferred to unique index
st.execute("create table brit7 (i int not null, " +
"j int not null primary key)");
st.execute("create unique index brit7i on brit7(i)");
// unique con preferred to unique index
st.execute("create table brit8 (i int not null, " +
"j int not null unique)");
st.execute("create unique index brit8i on brit8(i)");
// non-unique index just ignored
st.execute("create table brit9 (i int, j int)");
st.execute("create index brit9i on brit9(i)");
// fewer cols unique con still ignored over primary key
st.execute("create table brit10 " +
"(i int unique not null , j int not null, primary key (i,j))");
// fewer cols unique index still ignored over primary key
st.execute("create table brit11 (i int not null, j int not null, "
+ "primary key (i,j))");
st.execute("create unique index brit11i on brit11(i)");
// fewer cols unique index still ignored over unique con
st.execute("create table brit12 (i int not null, j int not null, "
+ "unique (i,j))");
st.execute("create unique index brit12i on brit12(i)");
st.execute("create table brit13 (i int not null, j int)");
// fewest cols unique con is the one picked of several
st.execute("create table brit14 (i int not null unique, j int not "
+ "null, k int, unique (i,j))");
// fewest cols unique index is the one picked of several
st.execute("create table brit15 (i int not null, j int not null, k int)");
st.execute("create unique index brit15ij on brit15(i,j)");
st.execute("create unique index brit15i on brit15(i)");
st.execute("create table brit16 (i int not null primary key, j int)");
// from old metadata test
// DERBY-3180; if this table gets created here, running the entire test
// twice with defaultSuite runs into into trouble.
// Moving into separate fixture does not have this problem.
st.execute("create table brit17 (i int not null default 10, " +
"s smallint not null, c30 char(30) not null, " +
"vc10 varchar(10) not null default 'asdf', " +
"constraint PRIMKEY primary key(vc10, i), " +
"constraint UNIQUEKEY unique(c30, s), ai bigint " +
"generated always as identity " +
"(start with -10, increment by 2001))");
// Create another unique index on brit17
st.execute("create unique index brit17i on brit17(s, i)");
// Create a non-unique index on brit17
st.execute("create index brit17ij on brit17(s)");
getConnection().setAutoCommit(false);
// except for the last table, the expected results are
// column i, column j, or columns i and j.
String [][] expRSI = {
{"2", "I", "4", "INTEGER", "4", null, "10", "1"}};
String [][] expRSJ = {
{"2", "J", "4", "INTEGER", "4", null, "10", "1"}};
String [][] expRSIJ = {
{"2", "I", "4", "INTEGER", "4", null, "10", "1"},
{"2", "J", "4", "INTEGER", "4", null, "10", "1"}};
// result: column i
ResultSet[] rs = getBestRowIdentifier(null,schema,"BRIT1",
DatabaseMetaData.bestRowTemporary, true);
verifyBRIResults(rs, expRSI);
// result: column i
rs = getBestRowIdentifier(null,schema,"BRIT2",
DatabaseMetaData.bestRowTemporary, true);
verifyBRIResults(rs, expRSI);
// result: column i (j is equally good but its index descriptor
// sorts after i: DERBY-6623)
rs = getBestRowIdentifier(null,schema,"BRIT3",
DatabaseMetaData.bestRowTemporary,true);
verifyBRIResults(rs, expRSI);
// result: column i
rs = getBestRowIdentifier(null,schema,"BRIT4",
DatabaseMetaData.bestRowTemporary,true);
verifyBRIResults(rs, expRSI);
// result: columns i and j
rs = getBestRowIdentifier(null,schema,"BRIT5",
DatabaseMetaData.bestRowTemporary,true);
verifyBRIResults(rs, expRSIJ);
// result: column j
rs = getBestRowIdentifier(null,schema,"BRIT6",
DatabaseMetaData.bestRowTemporary,true);
verifyBRIResults(rs, expRSJ);
// result: column j
rs = getBestRowIdentifier(null,schema,"BRIT7",
DatabaseMetaData.bestRowTemporary,true);
verifyBRIResults(rs, expRSJ);
// result: column j
rs = getBestRowIdentifier(null,schema,"BRIT8",
DatabaseMetaData.bestRowTemporary,true);
verifyBRIResults(rs, expRSJ);
// result: columns i,j
rs = getBestRowIdentifier(null,schema,"BRIT9",
DatabaseMetaData.bestRowTemporary,true);
verifyBRIResults(rs, expRSIJ);
// result: columns i,j
rs = getBestRowIdentifier(null,schema,"BRIT10",
DatabaseMetaData.bestRowTemporary,true);
verifyBRIResults(rs, expRSIJ);
// result: columns i,j
rs = getBestRowIdentifier(null,schema,"BRIT11",
DatabaseMetaData.bestRowTemporary,true);
verifyBRIResults(rs, expRSIJ);
// result: columns i,j
rs = getBestRowIdentifier(null,schema,"BRIT12",
DatabaseMetaData.bestRowTemporary,true);
verifyBRIResults(rs, expRSIJ);
// Verify nullOK flags makes a difference. See also DERBY-3182
// result: column i, should've ignored null column
rs = getBestRowIdentifier(null,schema,"BRIT13",
DatabaseMetaData.bestRowTemporary,false);
verifyBRIResults(rs, expRSI);
// result: columns i, j
rs = getBestRowIdentifier(null,schema,"BRIT13",
DatabaseMetaData.bestRowTemporary,true);
verifyBRIResults(rs, expRSIJ);
// result: columns i
rs = getBestRowIdentifier(null,schema,"BRIT14",
DatabaseMetaData.bestRowTemporary,true);
verifyBRIResults(rs, expRSI);
// result: columns i
rs = getBestRowIdentifier(null,schema,"BRIT15",
DatabaseMetaData.bestRowTemporary,true);
verifyBRIResults(rs, expRSI);
// we don't do anything with SCOPE except detect bad values
// result: columns i
rs = getBestRowIdentifier(null,schema,"BRIT16",
DatabaseMetaData.bestRowTransaction,true);
verifyBRIResults(rs, expRSI);
// result: columns i
rs = getBestRowIdentifier(null,schema,"BRIT16",
DatabaseMetaData.bestRowSession,true);
verifyBRIResults(rs, expRSI);
// result: exception (invalid scope -1)
try {
rs = getBestRowIdentifier(null,schema,"BRIT16",-1,true);
} catch (SQLException sqle) {
assertSQLState( "42XAT", sqle);
}
// result: exception (invalid scope 3)
try {
rs = getBestRowIdentifier(null,schema,"BRIT16",3,true);
} catch (SQLException sqle) {
assertSQLState( "42XAT", sqle);
}
rs = getBestRowIdentifier(null, schema,"BRIT17",
DatabaseMetaData.bestRowTemporary,true);
String [][] expRS = new String [][] {
{"2", "I", "4", "INTEGER", "4", null, "10", "1"},
{"2", "VC10", "12", "VARCHAR", "10", null, null, "1"}
};
JDBC.assertUnorderedResultSet(rs[0], expRS, true);
// set buffer_length expected for ODBC; for most of the simple
// tables/rows in our test it's "4" so set in verifyBRIResults
expRS[0][5] = "4";
expRS[1][5] = "20";
JDBC.assertUnorderedResultSet(rs[1], expRS, true);
// test DERBY-2610 for fun; can't pass in null table name
try {
getBestRowIdentifier(null,schema,null,
DatabaseMetaData.bestRowTemporary,true);
} catch (SQLException sqle) {
assertSQLState( "XJ103", sqle);
}
// check on systables
rs = getBestRowIdentifier(null,"SYS","SYSTABLES",
DatabaseMetaData.bestRowTemporary,true);
expRS = new String [][] {
{"2", "TABLEID", "1", "CHAR", "36", null, null, "1"}
};
JDBC.assertUnorderedResultSet(rs[0], expRS, true);
// set buffer_length expected for ODBC
expRS[0][5] = "72";
JDBC.assertUnorderedResultSet(rs[1], expRS, true);
getConnection().setAutoCommit(true);
st.execute("drop table brit1");
st.execute("drop table brit2");
st.execute("drop table brit3");
st.execute("drop index brit4i");
st.execute("drop table brit4");
st.execute("drop table brit5");
st.execute("drop table brit6");
st.execute("drop index brit7i");
st.execute("drop table brit7");
st.execute("drop index brit8i");
st.execute("drop table brit8");
st.execute("drop index brit9i");
st.execute("drop table brit9");
st.execute("drop table brit10");
st.execute("drop index brit11i");
st.execute("drop table brit11");
st.execute("drop index brit12i");
st.execute("drop table brit12");
st.execute("drop table brit13");
st.execute("drop table brit14");
st.execute("drop index brit15i");
st.execute("drop index brit15ij");
st.execute("drop table brit15");
st.execute("drop table brit16");
st.execute("drop index brit17i");
st.execute("drop index brit17ij");
st.execute("drop table brit17");
st.close();
}
/**
* Helper method for testing getBestRowIdentifier - calls the ODBC procedure
*
* @param catalog
* @param schema
* @param table
* @param scope
* @param nullable
* @return the result set of the query
*
* @throws SQLException
*/
private ResultSet getBestRowIdentifierODBC(String catalog, String schema,
String table, int scope, boolean nullable) throws SQLException
{
CallableStatement cs = prepareCall(
"CALL SYSIBM.SQLSPECIALCOLUMNS(1, ?, ?, ?, ?, ?, " +
"'DATATYPE=''ODBC''')");
cs.setString(1, catalog);
cs.setString(2, schema);
cs.setString(3, table);
cs.setInt(4, scope);
cs.setBoolean(5, nullable);
cs.execute();
return cs.getResultSet();
}
/**
* Helper method for testing getBestRowIdentifier - calls
* dmd.getBestRowIdentifier for the JDBC call, and getBestRowIdentifierODBC
*
* for the ODBC procedure
* @param catalog
* @param schema
* @param table
* @param scope
* @param nullable
* @return array of two result sets corresponding to JDBC and ODBC
*
* @throws SQLException
*/
private ResultSet[] getBestRowIdentifier(String catalog, String schema, String table,
int scope, boolean nullable) throws SQLException
{
ResultSet[] rss = new ResultSet[2];
DatabaseMetaData dmd = getDMD();
rss[0]= dmd.getBestRowIdentifier(catalog, schema, table, scope, nullable);
rss[1]= getBestRowIdentifierODBC(catalog, schema, table, scope, nullable);
String[] columnNames = {
"SCOPE", "COLUMN_NAME", "DATA_TYPE", "TYPE_NAME",
"COLUMN_SIZE", "BUFFER_LENGTH", "DECIMAL_DIGITS",
"PSEUDO_COLUMN"};
int[] columnTypes = {
Types.SMALLINT, Types.VARCHAR, Types.INTEGER, Types.VARCHAR,
Types.INTEGER, Types.INTEGER, Types.SMALLINT, Types.SMALLINT};
int[] odbcColumnTypes = {
Types.SMALLINT, Types.VARCHAR, Types.SMALLINT, Types.VARCHAR,
Types.INTEGER, Types.INTEGER, Types.SMALLINT, Types.SMALLINT};
boolean [] nullability = {
true, false, true, true, true, true, true, true};
// column nullability is opposite to with scope=1 or 2...DERBY-3181
// When running the ODBC version, the datatype returned for
// column 3 is SMALLINT, vs. INTEGER when scope=1 or 2...
// So, in this case, the columnTypes are the same for ODBC and JDBC,
// but with calls with a valid scope, they are different.
if (scope != DatabaseMetaData.bestRowTemporary &&
scope != DatabaseMetaData.bestRowTransaction &&
scope != DatabaseMetaData.bestRowSession)
{
nullability = new boolean [] {
false, false, false, false, false, false, false, false};
odbcColumnTypes = columnTypes;
}
assertMetaDataResultSet(rss[0], columnNames, columnTypes, nullability);
assertMetaDataResultSet(rss[1], columnNames, odbcColumnTypes, nullability);
return rss;
}
/**
* helper method for test testGetBestRowIdentifier
* @param rss - ResultSet array from getBestRowIdentifier;
* rss[0] will have the JDBC result, rss[1] the ODBC result
* @param expRS - bi-dimensional String array with expected result row(s)
* @throws SQLException
*/
public void verifyBRIResults(ResultSet[] rss, String[][] expRS) throws SQLException {
JDBC.assertFullResultSet(rss[0], expRS, true);
for (String[] expRS1 : expRS) {
expRS1[5] = "4";
}
JDBC.assertFullResultSet(rss[1], expRS, true);
for (String[] expRS1 : expRS) {
expRS1[5] = null;
}
}
/**
* Test getGetColumnPrivileges; does not modify database
* For further testing see test lang.grantRevokeTest
* @throws SQLException
*/
public void testGetColumnPrivileges() throws SQLException
{
// unlike for instance getTables() and getUDTs trying to call
// getColumnPrivileges with all nulls gets stopped because
// the spec indicates it takes a table name, not just a pattern
try {
getColumnPrivileges(null,null,null,null);
fail ("expected error XJ103");
} catch (SQLException sqle) {
assertSQLState("XJ103", sqle);
}
ResultSet[] rs = getColumnPrivileges(null,null,"",null);
JDBC.assertEmpty(rs[0]);
JDBC.assertEmpty(rs[1]);
rs = getColumnPrivileges("","","","");
JDBC.assertEmpty(rs[0]);
JDBC.assertEmpty(rs[1]);
rs = getColumnPrivileges("%","%","%","%");
JDBC.assertEmpty(rs[0]);
JDBC.assertEmpty(rs[1]);
// we didn't (can't) grant any privileges to the systabels, so no row
rs = getColumnPrivileges(null,"SYS","SYSTABLES","TABLEID");
JDBC.assertEmpty(rs[0]);
JDBC.assertEmpty(rs[1]);
}
/**
* Helper method for testing getColumnPrivileges - calls the ODBC procedure
* @param catalog
* @param schema
* @param table
* @param columnNamePattern
* @return the result set of the query
* @throws SQLException
*/
private ResultSet getColumnPrivilegesODBC(String catalog, String schema,
String table, String columnNamePattern) throws SQLException
{
CallableStatement cs = prepareCall(
"CALL SYSIBM.SQLCOLPRIVILEGES(?, ?, ?, ?, 'DATATYPE=''ODBC''')");
cs.setString(1, catalog);
cs.setString(2, schema);
cs.setString(3, table);
cs.setString(4, columnNamePattern);
cs.execute();
return cs.getResultSet();
}
/**
* Helper method for testing getColumnPrivileges - calls dmd.getIndexInfo for the
* JDBC call, and getColumnPrivilegesODBC for the ODBC procedure
*
* @param catalog
* @param schema
* @param table
* @param columnNamePattern
* @return array of two result sets corresponding to JDBC and ODBC
*
* @throws SQLException
*/
private ResultSet[] getColumnPrivileges(String catalog, String schema, String table,
String columnNamePattern) throws SQLException
{
ResultSet[] rss = new ResultSet[2];
DatabaseMetaData dmd = getDMD();
rss[0]= dmd.getColumnPrivileges(catalog, schema, table, columnNamePattern);
rss[1]= getColumnPrivilegesODBC(catalog, schema, table, columnNamePattern);
String [] columnNames = {"TABLE_CAT","TABLE_SCHEM","TABLE_NAME",
"COLUMN_NAME","GRANTOR","GRANTEE","PRIVILEGE","IS_GRANTABLE"};
int [] columnTypes = {
Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,
Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,Types.VARCHAR};
boolean [] nullability = {false,false,false,false,false,false,false,false};
assertMetaDataResultSet(rss[0], columnNames, columnTypes, nullability);
assertMetaDataResultSet(rss[1], columnNames, columnTypes, nullability);
return rss;
}
/**
* Test getGetTablePrivileges; does not modify database
* For further testing see test lang.grantRevokeTest
* @throws SQLException
*/
public void testGetTablePrivileges() throws SQLException
{
ResultSet rs[] = getTablePrivileges(null,null,null);
JDBC.assertEmpty(rs[0]);
JDBC.assertEmpty(rs[1]);
rs = getTablePrivileges("","","");
JDBC.assertEmpty(rs[0]);
JDBC.assertEmpty(rs[1]);
rs = getTablePrivileges("%","%","%");
JDBC.assertEmpty(rs[0]);
JDBC.assertEmpty(rs[1]);
// we didn't (can't) grant any privileges to the systabels, so no row
rs = getTablePrivileges(null,"SYS","SYSTABLES");
JDBC.assertEmpty(rs[0]);
JDBC.assertEmpty(rs[1]);
}
/**
* Helper method for testing getTablePrivileges - calls the ODBC procedure
*
* @param catalog
* @param schema
* @param tableNamePattern
* @return the result set of the query
*
* @throws SQLException
*/
private ResultSet getTablePrivilegesODBC(String catalog, String schema,
String tableNamePattern) throws SQLException
{
CallableStatement cs = prepareCall(
"CALL SYSIBM.SQLTABLEPRIVILEGES(?, ?, ?, 'DATATYPE=''ODBC''')");
cs.setString(1, catalog);
cs.setString(2, schema);
cs.setString(3, tableNamePattern);
cs.execute();
return cs.getResultSet();
}
/**
* Helper method for testing getTablePrivileges - calls dmd.getIndexInfo for the
* JDBC call, and getTablePrivilegesODBC for the ODBC procedure
*
* @param catalog
* @param schema
* @param tableNamePattern
* @return array of two result sets corresponding to JDBC and ODBC
*
* @throws SQLException
*/
private ResultSet[] getTablePrivileges(
String catalog, String schema, String tableNamePattern)
throws SQLException
{
ResultSet[] rss = new ResultSet[2];
DatabaseMetaData dmd = getDMD();
rss[0]= dmd.getTablePrivileges(catalog, schema, tableNamePattern);
rss[1]= getTablePrivilegesODBC(catalog, schema, tableNamePattern);
String [] columnNames = {"TABLE_CAT","TABLE_SCHEM","TABLE_NAME",
"GRANTOR","GRANTEE","PRIVILEGE","IS_GRANTABLE"};
int [] columnTypes = {
Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,
Types.VARCHAR,Types.VARCHAR,Types.VARCHAR};
boolean [] nullability = {false,false,false,false,false,false,false};
assertMetaDataResultSet(rss[0], columnNames, columnTypes, nullability);
assertMetaDataResultSet(rss[1], columnNames, columnTypes, nullability);
return rss;
}
/**
* Test getIndexInfo; does not modify database
* @throws SQLException
*/
public void testGetIndexInfo() throws SQLException
{
// unlike for instance getTables() and getUDTs trying to call
// getIndexInfo with all nulls gets stopped because
// the spec indicates it takes a table name, not just a pattern
try {
getIndexInfo(null,null,null,true,true);
fail ("expected error XJ103");
} catch (SQLException sqle) {
assertSQLState("XJ103", sqle);
}
// do a call which selects unique indexes only
ResultSet rss[] = getIndexInfo("","SYS","SYSCOLUMNS",true,false);
String[][] expRS = {
{"","SYS","SYSCOLUMNS","false","","SYSCOLUMNS_INDEX1","3","1",
"REFERENCEID","A",null,null,null},
{"","SYS","SYSCOLUMNS","false","","SYSCOLUMNS_INDEX1","3","2",
"COLUMNNAME","A",null,null,null}};
assertFullResultSet(rss, expRS, true);
// same table, but select all indexes (unique=false)
// note, that true for approximate does nothing in Derby
rss = getIndexInfo("","SYS","SYSCOLUMNS",false,false);
expRS = new String[][] {
{"","SYS","SYSCOLUMNS","false","","SYSCOLUMNS_INDEX1","3","1",
"REFERENCEID","A",null,null,null},
{"","SYS","SYSCOLUMNS","false","","SYSCOLUMNS_INDEX1","3","2",
"COLUMNNAME","A",null,null,null},
{"","SYS","SYSCOLUMNS","true","","SYSCOLUMNS_INDEX2","3","1",
"COLUMNDEFAULTID","A",null,null,null}};
assertFullResultSet(rss, expRS, true);
rss = getIndexInfo("","SYS","SYSTABLES",true,false);
expRS = new String[][] {
{"","SYS","SYSTABLES","false","","SYSTABLES_INDEX1","3","1",
"TABLENAME","A",null,null,null},
{"","SYS","SYSTABLES","false","","SYSTABLES_INDEX1","3","2",
"SCHEMAID","A",null,null,null},
{"","SYS","SYSTABLES","false","","SYSTABLES_INDEX2","3","1",
"TABLEID","A",null,null,null}};
assertFullResultSet(rss, expRS, true);
// should return no rows
rss = getIndexInfo("","SYS","SYSSTABLES",true,false);
JDBC.assertEmpty(rss[0]);
JDBC.assertEmpty(rss[1]);
}
/**
* Test getIndexInfo further; does modify database
* @throws SQLException
*/
public void testMoreGetIndexInfo() throws SQLException
{
// test to see that we are correctly returning D for ASC_OR_DESC.
// As Derby only supports tableIndexHashed Type, and
// CARDINALITY, PAGES, nor FILTER_CONDITION get set, no further
// tests seem necessary.
Statement st = createStatement();
// First, create the test table and indexes/keys
st.execute("create table iit (i int not null, j int)");
st.execute("create unique index iii on iit(i asc, j desc)");
DatabaseMetaData dmd = getDMD();
ResultSet rs = dmd.getIndexInfo("",schema,"IIT",false,false);
boolean more = rs.next();
if (more) {
assertEquals("A",rs.getString(10));
}
more = rs.next();
if (more) {
assertEquals("D",rs.getString(10));
}
rs = getIndexInfoODBC("",schema,"IIT",false,false);
more = rs.next();
if (more) {
assertEquals("A",rs.getString(10));
}
more = rs.next();
if (more) {
assertEquals("D",rs.getString(10));
}
st.execute("drop index iii");
st.execute("drop table iit");
st.close();
}
/**
* Helper method for testing getIndexInfo - calls the ODBC procedure
*
* @param catalog
* @param schema
* @param table
* @param approximate
* @param unique
* @return the result set of the query
*
* @throws SQLException
*/
private ResultSet getIndexInfoODBC(String catalog, String schema,
String table, boolean unique, boolean approximate) throws SQLException
{
CallableStatement cs = prepareCall(
"CALL SYSIBM.SQLSTATISTICS(?, ?, ?, ?, ?, " +
"'DATATYPE=''ODBC''')");
cs.setString(1, catalog);
cs.setString(2, schema);
cs.setString(3, table);
// the unique parameter needs to be flopped...See the call
// in SystemProcedures.
cs.setBoolean(4, unique? false : true);
cs.setBoolean(5, approximate);
cs.execute();
return cs.getResultSet();
}
/**
* Helper method for testing getIndexInfo - calls dmd.getIndexInfo for the
* JDBC call, and getIndexInfoODBC for the ODBC procedure
* @param catalog
* @param schema
* @param table
* @param unique
* @param approximate
* @return an array of two result sets corresponding to JDBC and ODBC
* respectively
* @throws SQLException
*/
private ResultSet[] getIndexInfo(String catalog, String schema, String table,
boolean unique, boolean approximate) throws SQLException
{
ResultSet[] rss = new ResultSet[2];
DatabaseMetaData dmd = getDMD();
rss[0]= dmd.getIndexInfo(catalog, schema, table, unique, approximate);
rss[1]= getIndexInfoODBC(catalog, schema, table, unique, approximate);
String [] columnNames = {"TABLE_CAT","TABLE_SCHEM","TABLE_NAME",
"NON_UNIQUE","INDEX_QUALIFIER","INDEX_NAME","TYPE",
"ORDINAL_POSITION","COLUMN_NAME","ASC_OR_DESC","CARDINALITY",
"PAGES","FILTER_CONDITION"};
int [] columnTypes = {
Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,
Types.BOOLEAN,Types.VARCHAR,Types.VARCHAR,Types.SMALLINT,
// ASC_OR_DESC is Types.CHAR rather than VARCHAR...
Types.SMALLINT,Types.VARCHAR,Types.CHAR,Types.BIGINT,
Types.BIGINT,Types.VARCHAR};
boolean [] nullability = {false,false,false,
false,false,true,true,true,false,false,true,true,true};
// JDBC result set
assertMetaDataResultSet(rss[0], columnNames, columnTypes, nullability);
// Change shape for ODBC.
columnTypes[4 - 1] = Types.SMALLINT; // types.boolean is not supported with ODBC
// ODBC result set
assertMetaDataResultSet(rss[1], columnNames, columnTypes, nullability);
return rss;
}
/**
* Helper method - unravels a ResultSet array created e.g.
* with this.getIndexInfo, i.e. Resultset[0] has the JDBC resultset
* and ResultSet[1] the ODBC resultset
*
* @param rs
* @param expRS
* @param trim
*
* @throws SQLException
*/
private void assertFullResultSet(
ResultSet rs[], String[][] expRS, boolean trim) throws SQLException
{
JDBC.assertFullResultSet(rs[0], expRS, trim);
JDBC.assertFullResultSet(rs[1], expRS, trim);
}
/**
* Helper method - unravels a ResultSet array created e.g.
* with this.getIndexInfo, i.e. Resultset[0] has the JDBC resultset
* and ResultSet[1] the ODBC resultset
*
* @param rs
* @param expRS
* @param trim
*
* @throws SQLException
*/
private void assertFullUnorderedResultSet(
ResultSet rs[], String[][] expRS, boolean trim) throws SQLException
{
JDBC.assertUnorderedResultSet(rs[0], expRS, trim);
JDBC.assertUnorderedResultSet(rs[1], expRS, trim);
}
/**
* Create the tables for get*Keys tests
* @throws SQLException
*/
private void createObjectsForKeysTests() throws SQLException
{
getConnection().setAutoCommit(false);
Statement s = createStatement();
s.execute("create table kt1 (" +
"i int not null default 10, " +
"s smallint not null, " +
"c30 char(30) not null, " +
"vc10 varchar(10) not null default 'asdf', " +
"constraint PRIMKEY primary key(vc10, i), " +
"constraint UNIQUEKEY unique(c30, s), " +
"ai bigint generated always as identity " +
"(start with -10, increment by 2001))");
// Create another unique index on kt1
s.execute("create unique index u1 on kt1(s, i)");
// Create a non-unique index on kt1
s.execute("create index u2 on kt1(s)");
// Create a view on key table 1
s.execute("create view kv as select * from kt1");
// Create a foreign key
s.execute("create table reftab (vc10 varchar(10), i int, " +
"s smallint, c30 char(30), " +
"s2 smallint, c302 char(30), " +
"dprim decimal(5,1) not null, dfor decimal(5,1) not null, "+
"constraint PKEY_REFTAB primary key (dprim), " +
"constraint FKEYSELF " +
"foreign key (dfor) references reftab, "+
"constraint FKEY1 " +
"foreign key(vc10, i) references kt1, " +
"constraint FKEY2 " +
"foreign key(c30, s2) references kt1 (c30, s), "+
"constraint FKEY3 " +
"foreign key(c30, s) references kt1 (c30, s))");
s.execute("create table reftab2 (t2_vc10 varchar(10), t2_i int, " +
"constraint T2_FKEY1 " +
"foreign key(t2_vc10, t2_i) references kt1)");
commit();
getConnection().setAutoCommit(true);
}
/**
* Drop the database objects for get*Keys tests
* @throws SQLException
*/
private void dropObjectsForKeysTests() throws SQLException
{
getConnection().setAutoCommit(false);
Statement s = createStatement();
s.execute("drop table reftab2");
s.execute("drop table reftab");
commit();
s.execute("drop view kv");
s.execute("drop index u2");
s.execute("drop index u1");
s.execute("drop table kt1");
commit();
getConnection().setAutoCommit(true);
}
/**
* Test getPrimaryKeys; does modify database
* @throws SQLException
*/
public void testGetPrimaryKeys() throws SQLException
{
String[][] expRS = new String[][] {
{"",schema,"KT1","I","2","PRIMKEY"},
{"",schema,"KT1","VC10","1","PRIMKEY"}};
createObjectsForKeysTests();
// try with valid search criteria
// although, % may not actually be appropriate?
ResultSet rs[] = getPrimaryKeys("", "%", "KT1");
assertFullResultSet(rs, expRS, true);
rs = getPrimaryKeys(null, schema, "KT1");
assertFullResultSet(rs, expRS, true);
rs = getPrimaryKeys(null, null, "KT1");
assertFullResultSet(rs, expRS, true);
rs = getPrimaryKeys(null, "", "KT1");
JDBC.assertEmpty(rs[0]);
JDBC.assertEmpty(rs[1]);
// tablename may not be null
DatabaseMetaData dmd = getDMD();
try {
rs[0] = dmd.getPrimaryKeys(null, null, null);
fail ("table name may not be null, should've given error");
} catch (SQLException sqle) {
assertSQLState("XJ103", sqle);
}
try {
rs[1] = getPrimaryKeysODBC(null, null, null);
fail ("table name may not be null, should've given error");
} catch (SQLException sqle) {
assertSQLState("XJ103", sqle);
}
// DERBY-2610, tablename must be given as stored - % means no rows
rs = getPrimaryKeys(null, null, "%");
JDBC.assertEmpty(rs[0]);
JDBC.assertEmpty(rs[1]);
dropObjectsForKeysTests();
}
/**
* Helper method for testing getPrimaryKeys - calls the ODBC procedure
*
* @param catalog
* @param schema
* @param table
* @return result set of the query
* @throws SQLException
*/
private ResultSet getPrimaryKeysODBC(
String catalog, String schema, String table) throws SQLException
{
CallableStatement cs = prepareCall(
"CALL SYSIBM.SQLPRIMARYKEYS(?, ?, ?, 'DATATYPE=''ODBC''')");
cs.setString(1, catalog);
cs.setString(2, schema);
cs.setString(3, table);
cs.execute();
return cs.getResultSet();
}
/**
* Helper method for testing getPrimaryKeys - calls dmd.getPrimaryKeys for
* the JDBC call, and getPrimaryKeysODBC for the ODBC procedure
* @param catalog
* @param schema
* @param table
* @return an array of two result sets corresponding to JDBC and ODBC
* respectively
* @throws SQLException
*/
private ResultSet[] getPrimaryKeys(
String catalog, String schema, String table) throws SQLException
{
ResultSet[] rss = new ResultSet[2];
DatabaseMetaData dmd = getDMD();
rss[0]= dmd.getPrimaryKeys(catalog, schema, table);
rss[1]= getPrimaryKeysODBC(catalog, schema, table);
String [] columnNames = {"TABLE_CAT","TABLE_SCHEM","TABLE_NAME",
"COLUMN_NAME","KEY_SEQ","PK_NAME"};
int [] columnTypes = {
Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,
Types.VARCHAR,Types.SMALLINT,Types.VARCHAR};
boolean [] nullability = {false,false,false,false,true,false};
assertMetaDataResultSet(rss[0], columnNames, columnTypes, nullability);
assertMetaDataResultSet(rss[1], columnNames, columnTypes, nullability);
return rss;
}
/**
* Test getImportedKeys, getExportedKeys, getCrossReference; modifies db
* @throws SQLException
*/
public void testGetXXportedKeys() throws SQLException
{
// getExportedKeys
String[][] expRS1 = new String[][] {
{"",schema,"KT1","VC10","",schema,"REFTAB","VC10","1","3","3","FKEY1","PRIMKEY","7"},
{"",schema,"KT1","I","",schema,"REFTAB","I","2","3","3","FKEY1","PRIMKEY","7"},
{"",schema,"KT1","C30","",schema,"REFTAB","C30","1","3","3","FKEY3","UNIQUEKEY","7"},
{"",schema,"KT1","C30","",schema,"REFTAB","C30","1","3","3","FKEY2","UNIQUEKEY","7"},
{"",schema,"KT1","S","",schema,"REFTAB","S","2","3","3","FKEY3","UNIQUEKEY","7"},
{"",schema,"KT1","S","",schema,"REFTAB","S2","2","3","3","FKEY2","UNIQUEKEY","7"},
{"",schema,"REFTAB","DPRIM","",schema,"REFTAB","DFOR","1","3","3","FKEYSELF","PKEY_REFTAB","7"}};
String[][] expRS2 = new String[][] {
{"",schema,"KT1","VC10","",schema,"REFTAB2","T2_VC10","1","3","3","T2_FKEY1","PRIMKEY","7"},
{"",schema,"KT1","I","",schema,"REFTAB2","T2_I","2","3","3","T2_FKEY1","PRIMKEY","7"}};
createObjectsForKeysTests();
// try with valid search criteria
// although, % may not actually be appropriate?
ResultSet rs[] = getImportedKeys("", "%", "REFTAB");
assertFullUnorderedResultSet(rs, expRS1, true);
rs = getImportedKeys("", "%", "REFTAB2");
assertFullResultSet(rs, expRS2, true);
rs = getImportedKeys(null, schema, "REFTAB");
assertFullUnorderedResultSet(rs, expRS1, true);
rs = getImportedKeys(null, schema, "REFTAB2");
assertFullResultSet(rs, expRS2, true);
rs = getImportedKeys(null, null, "REFTAB");
assertFullUnorderedResultSet(rs, expRS1, true);
rs = getImportedKeys(null, "", "REFTAB");
JDBC.assertEmpty(rs[0]);
JDBC.assertEmpty(rs[1]);
// tablename may not be null
DatabaseMetaData dmd = getDMD();
try {
rs[0] = dmd.getImportedKeys(null, null, null);
fail ("table name may not be null, should've given error");
} catch (SQLException sqle) {
assertSQLState("XJ103", sqle);
}
try {
rs[1] = getImportedKeysODBC(null, null, null);
fail ("table name may not be null, should've given error");
} catch (SQLException sqle) {
assertSQLState("XJ103", sqle);
}
// DERBY-2610, tablename must be given as stored - % means no rows
rs = getImportedKeys(null, null, "%");
JDBC.assertEmpty(rs[0]);
JDBC.assertEmpty(rs[1]);
// getExportedKeys
expRS1 = new String[][] {
{"",schema,"KT1","VC10","",schema,"REFTAB","VC10","1","3","3","FKEY1","PRIMKEY","7"},
{"",schema,"KT1","I","",schema,"REFTAB","I","2","3","3","FKEY1","PRIMKEY","7"},
{"",schema,"KT1","C30","",schema,"REFTAB","C30","1","3","3","FKEY2","UNIQUEKEY","7"},
{"",schema,"KT1","S","",schema,"REFTAB","S2","2","3","3","FKEY2","UNIQUEKEY","7"},
{"",schema,"KT1","C30","",schema,"REFTAB","C30","1","3","3","FKEY3","UNIQUEKEY","7"},
{"",schema,"KT1","S","",schema,"REFTAB","S","2","3","3","FKEY3","UNIQUEKEY","7"},
{"",schema,"KT1","VC10","",schema,"REFTAB2","T2_VC10","1","3","3","T2_FKEY1","PRIMKEY","7"},
{"",schema,"KT1","I","",schema,"REFTAB2","T2_I","2","3","3","T2_FKEY1","PRIMKEY","7"}};
expRS2 = new String[][] {
{"",schema,"REFTAB","DPRIM","",schema,"REFTAB","DFOR","1","3","3","FKEYSELF","PKEY_REFTAB","7"}};
rs = getExportedKeys("", "%", "KT1");
assertFullResultSet(rs, expRS1, true);
rs = getExportedKeys("", "%", "REFTAB");
assertFullResultSet(rs, expRS2, true);
rs = getExportedKeys(null, schema, "KT1");
assertFullResultSet(rs, expRS1, true);
rs = getExportedKeys(null, schema, "REFTAB");
assertFullResultSet(rs, expRS2, true);
rs = getExportedKeys(null, null, "KT1");
assertFullResultSet(rs, expRS1, true);
rs = getExportedKeys(null, "", "KT1");
JDBC.assertEmpty(rs[0]);
JDBC.assertEmpty(rs[1]);
// tablename may not be null
try {
rs[0] = dmd.getExportedKeys(null, schema, null);
fail ("table name may not be null, should've given error");
} catch (SQLException sqle) {
assertSQLState("XJ103", sqle);
}
try {
rs[1] = getExportedKeysODBC(null, schema, null);
fail ("table name may not be null, should've given error");
} catch (SQLException sqle) {
assertSQLState("XJ103", sqle);
}
// DERBY-2610, tablename must be given as stored - % means no rows
rs = getExportedKeys(null, null, "%");
JDBC.assertEmpty(rs[0]);
JDBC.assertEmpty(rs[1]);
// getCrossReference
expRS1 = new String[][] {
{"",schema,"KT1","VC10","",schema,"REFTAB","VC10","1","3","3","FKEY1","PRIMKEY","7"},
{"",schema,"KT1","I","",schema,"REFTAB","I","2","3","3","FKEY1","PRIMKEY","7"},
{"",schema,"KT1","C30","",schema,"REFTAB","C30","1","3","3","FKEY2","UNIQUEKEY","7"},
{"",schema,"KT1","S","",schema,"REFTAB","S2","2","3","3","FKEY2","UNIQUEKEY","7"},
{"",schema,"KT1","C30","",schema,"REFTAB","C30","1","3","3","FKEY3","UNIQUEKEY","7"},
{"",schema,"KT1","S","",schema,"REFTAB","S","2","3","3","FKEY3","UNIQUEKEY","7"}};
expRS2 = new String[][] {
{"",schema,"REFTAB","DPRIM","",schema,"REFTAB","DFOR","1","3","3","FKEYSELF","PKEY_REFTAB","7"}};
// try with valid search criteria
rs = getCrossReference("", schema, "KT1", "", schema, "REFTAB");
assertFullResultSet(rs, expRS1, true);
rs = getCrossReference("", schema, "REFTAB", "", schema, "REFTAB");
assertFullResultSet(rs, expRS2, true);
rs = getCrossReference("", schema, "KT1", "", schema, "REFTAB");
assertFullResultSet(rs, expRS1, true);
rs = getCrossReference("", schema, "REFTAB", "", schema, "REFTAB");
assertFullResultSet(rs, expRS2, true);
rs = getCrossReference(null, schema, "KT1", null, schema, "REFTAB");
assertFullResultSet(rs, expRS1, true);
rs = getCrossReference(null, schema, "REFTAB", null, schema, "REFTAB");
assertFullResultSet(rs, expRS2, true);
// DERBY-2758; query should return a different value for odbc vs. jdbc
// only experiment jdbc here, odbc is handled elsewhere.
rs = getCrossReference(null, schema, "%", null, schema, "%");
JDBC.assertEmpty(rs[0]);
String[][] expRS = new String[][] {
{"",schema,"KT1","VC10","",schema,"REFTAB","VC10","1","3","3","FKEY1","PRIMKEY","7"},
{"",schema,"KT1","I","",schema,"REFTAB","I","2","3","3","FKEY1","PRIMKEY","7"},
{"",schema,"KT1","C30","",schema,"REFTAB","C30","1","3","3","FKEY2","UNIQUEKEY","7"},
{"",schema,"KT1","S","",schema,"REFTAB","S2","2","3","3","FKEY2","UNIQUEKEY","7"},
{"",schema,"KT1","C30","",schema,"REFTAB","C30","1","3","3","FKEY3","UNIQUEKEY","7"},
{"",schema,"KT1","S","",schema,"REFTAB","S","2","3","3","FKEY3","UNIQUEKEY","7"},
{"",schema,"REFTAB","DPRIM","",schema,"REFTAB","DFOR","1","3","3","FKEYSELF","PKEY_REFTAB","7"},
{"",schema,"KT1","VC10","",schema,"REFTAB2","T2_VC10","1","3","3","T2_FKEY1","PRIMKEY","7"},
{"",schema,"KT1","I","",schema,"REFTAB2","T2_I","2","3","3","T2_FKEY1","PRIMKEY","7"}};
JDBC.assertFullResultSet(rs[1], expRS, true);
// Test also with null for schema.
rs = getCrossReference(null, null, "%", null, null, "%");
JDBC.assertEmpty(rs[0]);
JDBC.assertResultSetContains(rs[1], expRS);
// tablename may not be null
try {
rs[0] = dmd.getCrossReference(null, null, null, null, null, null);
fail ("table name may not be null, should've given error");
} catch (SQLException sqle) {
if (usingDerbyNetClient())
assertSQLState("XJ110", sqle);
else
assertSQLState("XJ103", sqle);
}
// Note: With ODBC, this does *not* give an error.
// If that changes, uncomment the fail.
try {
rs[1] = getCrossReferenceODBC(null, schema, null, null, schema, null);
// fail ("table name may not be null, should've given error");
} catch (SQLException sqle) {
if (usingDerbyNetClient())
assertSQLState("XJ110", sqle);
else
assertSQLState("XJ103", sqle);
}
// tablename may not be null
try {
rs[0] = dmd.getCrossReference(null, null, "", null, null, null);
fail ("table name may not be null, should've given error");
} catch (SQLException sqle) {
if (usingDerbyNetClient())
assertSQLState("XJ111", sqle);
else
assertSQLState("XJ103", sqle);
}
try {
rs[1] = getCrossReferenceODBC(null, schema, "", null, schema, null);
//fail ("table name may not be null, should've given error");
} catch (SQLException sqle) {
if (usingDerbyNetClient())
assertSQLState("XJ111", sqle);
else
assertSQLState("XJ103", sqle);
}
// DERBY-2610, tablename must be given as stored - % means no rows
rs = getCrossReference(null, schema, "%", null, schema, "%");
JDBC.assertEmpty(rs[0]);
// But it *is* allowed with ODBC, see DERBY-2758
JDBC.assertFullResultSet(rs[1], expRS, true);
rs[0].close();
rs[1].close();
dropObjectsForKeysTests();
}
/**
* Helper method for testing getImportedKeys - calls the ODBC procedure
*
* @param catalog
* @param schema
* @param table
* @return result set of the query
*
* @throws SQLException
*/
private ResultSet getImportedKeysODBC(
String catalog, String schema, String table) throws SQLException
{
CallableStatement cs = prepareCall("CALL SYSIBM.SQLFOREIGNKEYS(" +
"null, null, null, ?, ?, ?, 'IMPORTEDKEY=1;DATATYPE=''ODBC''')");
cs.setString(1, catalog);
cs.setString(2, schema);
cs.setString(3, table);
cs.execute();
return cs.getResultSet();
}
/**
* Helper method for testing getImportedKeys - calls dmd.getImportedKeys for
* the JDBC call, and getImportedKeysODBC for the ODBC procedure
* @param catalog
* @param schema
* @param table
* @return an array of two result sets corresponding to JDBC and ODBC
* respectively
* @throws SQLException
*/
private ResultSet[] getImportedKeys(
String catalog, String schema, String table) throws SQLException
{
ResultSet[] rss = new ResultSet[2];
DatabaseMetaData dmd = getDMD();
rss[0]= dmd.getImportedKeys(catalog, schema, table);
rss[1]= getImportedKeysODBC(catalog, schema, table);
assertGetImportedAndExportedKeysShape(rss);
return rss;
}
/**
* Assert the shape of the ResultSets for getImportedKeys,
* getExportedKeys and getCrossReference.
* @param rss ResultSets from JDBC and ODBC calls.
* @throws SQLException
*/
private void assertGetImportedAndExportedKeysShape(ResultSet[] rss)
throws SQLException
{
String [] columnNames = {
"PKTABLE_CAT","PKTABLE_SCHEM","PKTABLE_NAME","PKCOLUMN_NAME",
"FKTABLE_CAT","FKTABLE_SCHEM","FKTABLE_NAME","FKCOLUMN_NAME",
"KEY_SEQ","UPDATE_RULE","DELETE_RULE",
"FK_NAME","PK_NAME","DEFERRABILITY"};
int [] columnTypes = {
Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,
Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,
Types.SMALLINT,Types.SMALLINT,Types.SMALLINT,
Types.VARCHAR,Types.VARCHAR,Types.SMALLINT};
boolean [] nullability = {false,false,false,false,
false,false,false,false,true,true,true,false,false,true};
assertMetaDataResultSet(rss[0], columnNames, columnTypes, nullability);
assertMetaDataResultSet(rss[1], columnNames, columnTypes, nullability);
}
/**
* Helper method for testing getExportedKeys - calls the ODBC procedure
*
* @param catalog
* @param schema
* @param table
* @return the result set of the query
*
* @throws SQLException
*/
private ResultSet getExportedKeysODBC(
String catalog, String schema, String table) throws SQLException
{
CallableStatement cs = prepareCall("CALL SYSIBM.SQLFOREIGNKEYS(" +
"?, ?, ?, null, null, null, 'EXPORTEDKEY=1;DATATYPE=''ODBC''')");
cs.setString(1, catalog);
cs.setString(2, schema);
cs.setString(3, table);
cs.execute();
return cs.getResultSet();
}
/**
* Helper method for testing getExportedKeys - calls dmd.getExportedKeys for
* the JDBC call, and getExportedKeysODBC for the ODBC procedure
* @param catalog
* @param schema
* @param table
* @return an array of two result sets corresponding to JDBC and ODBC
* respectively
* @throws SQLException
*/
private ResultSet[] getExportedKeys(
String catalog, String schema, String table) throws SQLException
{
ResultSet[] rss = new ResultSet[2];
DatabaseMetaData dmd = getDMD();
rss[0]= dmd.getExportedKeys(catalog, schema, table);
rss[1]= getExportedKeysODBC(catalog, schema, table);
assertGetImportedAndExportedKeysShape(rss);
return rss;
}
/**
* Helper method for testing getCrossReference - calls the ODBC procedure
* @param parentcatalog
* @param parentschema
* @param parenttable
* @param foreigncatalog
* @param foreignschema
* @param foreigntable
* @return the result set of the query
* @throws SQLException
*/
private ResultSet getCrossReferenceODBC(
String parentcatalog, String parentschema, String parenttable,
String foreigncatalog, String foreignschema, String foreigntable)
throws SQLException
{
CallableStatement cs = prepareCall("CALL SYSIBM.SQLFOREIGNKEYS(" +
"?, ?, ?, ?, ?, ?, 'DATATYPE=''ODBC''')");
cs.setString(1, parentcatalog);
cs.setString(2, parentschema);
cs.setString(3, parenttable);
cs.setString(4, foreigncatalog);
cs.setString(5, foreignschema);
cs.setString(6, foreigntable);
cs.execute();
return cs.getResultSet();
}
/**
* Helper method for testing getCrossReference - calls dmd.getCrossReference for
* the JDBC call, and getCrossReferenceODBC for the ODBC procedure
*
* @param parentcatalog
* @param parentschema
* @param parenttable
* @param foreigncatalog
* @param foreignschema
* @param foreigntable
* @return an array of two result sets corresponding to JDBC and ODBC
* respectively
*
* @throws SQLException
*/
private ResultSet[] getCrossReference(
String parentcatalog, String parentschema, String parenttable,
String foreigncatalog, String foreignschema, String foreigntable)
throws SQLException
{
ResultSet[] rss = new ResultSet[2];
DatabaseMetaData dmd = getDMD();
rss[0]= dmd.getCrossReference(parentcatalog, parentschema, parenttable,
foreigncatalog, foreignschema, foreigntable);
rss[1]= getCrossReferenceODBC(parentcatalog, parentschema, parenttable,
foreigncatalog, foreignschema, foreigntable);
assertGetImportedAndExportedKeysShape(rss);
return rss;
}
/**
* Test referential action values; modifies database
* @throws SQLException
*/
public void testReferentialAction() throws SQLException
{
Statement s = createStatement();
getConnection().setAutoCommit(false);
// First, create the test table and indexes/keys
// note: apparently we have no test for setdefault.
s.execute("create table refaction1(a int not null primary key)");
s.execute("create table refactnone(a int references refaction1(a))");
s.execute("create table refactrestrict(a int references refaction1(a) on delete restrict)");
s.execute("create table refactnoaction(a int references refaction1(a) on delete no action)");
s.execute("create table refactcascade(a int references refaction1(a) on delete cascade)");
s.execute("create table refactsetnull(a int references refaction1(a) on delete set null)");
s.execute("create table refactupdrestrict(a int references refaction1(a) on update restrict)");
s.execute("create table refactupdnoaction(a int references refaction1(a) on update no action)");
short restrict = DatabaseMetaData.importedKeyRestrict;
short no_action = DatabaseMetaData.importedKeyNoAction;
short cascade = DatabaseMetaData.importedKeyCascade;
short setnull = DatabaseMetaData.importedKeySetNull;
short setdefault = DatabaseMetaData.importedKeySetDefault;
ResultSet rs[] = getCrossReference("",schema,"REFACTION1","",schema,"REFACTNONE");
verifyReferentialAction(rs, new short[] {no_action, no_action});
rs = getCrossReference("",schema,"REFACTION1","",schema,"REFACTRESTRICT");
verifyReferentialAction(rs, new short[] {no_action, restrict});
rs = getCrossReference("",schema,"REFACTION1","",schema,"REFACTNOACTION");
verifyReferentialAction(rs, new short[] {no_action, no_action});
rs = getCrossReference("",schema,"REFACTION1","",schema,"REFACTCASCADE");
verifyReferentialAction(rs, new short[] {no_action, cascade});
rs = getCrossReference("",schema,"REFACTION1","",schema,"REFACTSETNULL");
verifyReferentialAction(rs, new short[] {no_action, setnull});
rs = getCrossReference("",schema,"REFACTION1","",schema,"REFACTUPDRESTRICT");
verifyReferentialAction(rs, new short[] {restrict, no_action});
rs = getCrossReference("",schema,"REFACTION1","",schema,"REFACTUPDNOACTION");
verifyReferentialAction(rs, new short[] {no_action, no_action});
rs = getImportedKeys(null, schema, "REFACTNONE");
verifyReferentialAction(rs, new short[] {no_action, no_action});
rs = getImportedKeys(null, schema, "REFACTRESTRICT");
verifyReferentialAction(rs, new short[] {restrict, restrict});
rs = getImportedKeys(null, schema, "REFACTNOACTION");
verifyReferentialAction(rs, new short[] {no_action, no_action});
rs = getImportedKeys(null, schema, "REFACTCASCADE");
verifyReferentialAction(rs, new short[] {no_action, cascade});
rs = getImportedKeys(null, schema, "REFACTSETNULL");
verifyReferentialAction(rs, new short[] {no_action, setnull});
rs = getImportedKeys(null, schema, "REFACTUPDRESTRICT");
verifyReferentialAction(rs, new short[] {no_action, no_action});
rs = getImportedKeys(null, schema, "REFACTUPDNOACTION");
verifyReferentialAction(rs, new short[] {no_action, no_action});
rs = getExportedKeys(null, schema, "REFACTION1");
short [][] expkeyresults = {
{no_action, cascade},
{no_action, no_action},
{no_action, no_action},
{no_action, restrict},
{no_action, setnull},
{no_action, no_action},
{restrict, no_action}};
for (int i = 0 ; i < 6 ; i++)
{
rs[0].next();
assertEquals(expkeyresults[i][0], rs[0].getShort(10));
assertEquals(expkeyresults[i][1], rs[0].getShort(11));
}
for (int i = 0 ; i < 6 ; i++)
{
rs[1].next();
assertEquals(expkeyresults[i][0], rs[1].getShort(10));
assertEquals(expkeyresults[i][1], rs[1].getShort(11));
}
s.execute("drop table refactnone");
s.execute("drop table refactupdrestrict");
s.execute("drop table refactupdnoaction");
s.execute("drop table refactrestrict");
s.execute("drop table refactnoaction");
s.execute("drop table refactcascade");
s.execute("drop table refactsetnull");
s.execute("drop table refaction1");
commit();
rs[0].close();
rs[1].close();
s.close();
getConnection().setAutoCommit(true);
}
// helper method for Referential Action test; verifies result
// of various calls.
// rs[0] is for JDBC calls, rs[1] for ODBC
public void verifyReferentialAction(ResultSet[] rs, short[] expRes)
throws SQLException {
rs[0].next();
assertEquals(expRes[0], rs[0].getShort(10));
assertEquals(expRes[1], rs[0].getShort(11));
rs[1].next();
assertEquals(expRes[0], rs[1].getShort(10));
assertEquals(expRes[1], rs[1].getShort(11));
}
/**
* Test DatabaseMetaData.getProcedures and .getProcedureColumns,
* Both for JDBC and ODBC.
* Further testing of these methods is done in lang/LangProcedureTest
*
*
* @throws Exception
*/
// Possible TODO:
// rewrite data portion of this test to compare results from
// metadata with sys.sys* query results (leave shape check in place)
public void testGetProceduresGetProcColumns() throws Exception {
boolean supportsBoolean = true;
Version dataVersion = getDataVersion( getConnection() );
if ( dataVersion.compareTo( new Version( 10, 7, 0, 0 ) ) < 0 ) { supportsBoolean = false; }
Statement s = createStatement();
getConnection().setAutoCommit(false);
s.execute("create procedure GETPCTEST1 (" +
// for creating, the procedure's params do not need to exactly match the method's
"out outb VARCHAR(3), a VARCHAR(3), b NUMERIC, c SMALLINT, " +
"e SMALLINT, f INTEGER, g BIGINT, h FLOAT, i DOUBLE PRECISION, " +
"k DATE, l TIME, T TIMESTAMP )"+
"language java external name " +
"'org.apache.derbyTesting.functionTests.tests.jdbcapi.DatabaseMetaDataTest.getpc'" +
" parameter style java");
s.execute("create procedure GETPCTEST2 (pa INTEGER, pb BIGINT)"+
"language java external name " +
"'org.apache.derbyTesting.functionTests.tests.jdbcapi.DatabaseMetaDataTest.getpc'" +
" parameter style java");
s.execute("create procedure GETPCTEST3A (STRING1 VARCHAR(5), out STRING2 VARCHAR(5))"+
"language java external name " +
"'org.apache.derbyTesting.functionTests.tests.jdbcapi.DatabaseMetaDataTest.getpc'" +
" parameter style java");
s.execute("create procedure GETPCTEST3B (in STRING3 VARCHAR(5), inout STRING4 VARCHAR(5))"+
"language java external name " +
"'org.apache.derbyTesting.functionTests.tests.jdbcapi.DatabaseMetaDataTest.getpc'" +
" parameter style java");
s.execute("create procedure GETPCTEST4A() "+
"language java external name " +
"'org.apache.derbyTesting.functionTests.tests.jdbcapi.DatabaseMetaDataTest.getpc4a'"+
" parameter style java");
s.execute("create procedure GETPCTEST4B() "+
"language java external name " +
"'org.apache.derbyTesting.functionTests.tests.jdbcapi.DatabaseMetaDataTest.getpc4b'" +
" parameter style java");
s.execute("create procedure GETPCTEST4Bx(out retparam INTEGER) "+
"language java external name " +
"'org.apache.derbyTesting.functionTests.tests.jdbcapi.DatabaseMetaDataTest.getpc4b'" +
" parameter style java");
if ( supportsBoolean )
{
s.execute("create procedure GETPCTEST5(in inarg boolean, out outarg boolean, inout inoutarg boolean) "+
"language java external name " +
"'org.apache.derbyTesting.functionTests.tests.jdbcapi.DatabaseMetaDataTest.foo'" +
" parameter style java");
}
JDBC.GeneratedId genid = new JDBC.GeneratedId();
String classname = getClass().getName();
String getpc = classname + ".getpc";
String getpc4a = classname + ".getpc4a";
String getpc4b = classname + ".getpc4b";
String foo = classname + ".foo";
ResultSet rs[] = getProcedures(null, "%", "GETPCTEST%");
Object[][] expRS = new Object[][] {
{"",schema,"GETPCTEST1",null,null,null,getpc,i(1),genid},
{"",schema,"GETPCTEST2",null,null,null,getpc,i(1),genid},
{"",schema,"GETPCTEST3A",null,null,null,getpc,i(1),genid},
{"",schema,"GETPCTEST3B",null,null,null,getpc,i(1),genid},
{"",schema,"GETPCTEST4A",null,null,null,getpc4a,i(1),genid},
{"",schema,"GETPCTEST4B",null,null,null,getpc4b,i(1),genid},
{"",schema,"GETPCTEST4BX",null,null,null,getpc4b,i(1),genid},
};
if ( supportsBoolean )
{
expRS = appendArray
(
expRS,
new Object[][]
{
{"",schema,"GETPCTEST5",null,null,null,foo,i(1),genid},
}
);
}
// Check the JDBC variant of getProcedures().
JDBC.assertFullResultSet(rs[0], expRS, false);
// Check the ODBC variant of getProcedures(). It's identical to the
// JDBC variant, except that it lacks the last column.
for (int i = 0; i < expRS.length; i++) {
Object[] jdbcRow = expRS[i];
Object[] odbcRow = new Object[jdbcRow.length - 1];
System.arraycopy(jdbcRow, 0, odbcRow, 0, odbcRow.length);
expRS[i] = odbcRow;
}
JDBC.assertFullResultSet(rs[1], expRS, false);
rs = getProcedureColumns(null, "%", "GETPCTEST%", "%");
expRS = new Object[][] {
{null,schema,"GETPCTEST1","OUTB",i(4),i(12),"VARCHAR",i(3),i(6),null,null,i(1),null,null,i(12),null,i(6),i(1),"YES",genid,i(12),i(0)},
{null,schema,"GETPCTEST1","A",i(1),i(12),"VARCHAR",i(3),i(6),null,null,i(1),null,null,i(12),null,i(6),i(2),"YES",genid,i(12),i(1)},
{null,schema,"GETPCTEST1","B",i(1),i(2),"NUMERIC",i(5),i(14),i(0),i(10),i(1),null,null,i(2),null,null,i(3),"YES",genid,i(12),i(2)},
{null,schema,"GETPCTEST1","C",i(1),i(5),"SMALLINT",i(5),i(2),i(0),i(10),i(1),null,null,i(5),null,null,i(4),"YES",genid,i(12),i(3)},
{null,schema,"GETPCTEST1","E",i(1),i(5),"SMALLINT",i(5),i(2),i(0),i(10),i(1),null,null,i(5),null,null,i(5),"YES",genid,i(12),i(4)},
{null,schema,"GETPCTEST1","F",i(1),i(4),"INTEGER",i(10),i(4),i(0),i(10),i(1),null,null,i(4),null,null,i(6),"YES",genid,i(12),i(5)},
{null,schema,"GETPCTEST1","G",i(1),i(-5),"BIGINT",i(19),i(40),i(0),i(10),i(1),null,null,i(-5),null,null,i(7),"YES",genid,i(12),i(6)},
{null,schema,"GETPCTEST1","H",i(1),i(8),"DOUBLE",i(52),i(8),null,i(2),i(1),null,null,i(8),null,null,i(8),"YES",genid,i(12),i(7)},
{null,schema,"GETPCTEST1","I",i(1),i(8),"DOUBLE",i(52),i(8),null,i(2),i(1),null,null,i(8),null,null,i(9),"YES",genid,i(12),i(8)},
{null,schema,"GETPCTEST1","K",i(1),i(91),"DATE",i(10),i(6),i(0),i(10),i(1),null,null,i(9),i(1),null,i(10),"YES",genid,i(12),i(9)},
{null,schema,"GETPCTEST1","L",i(1),i(92),"TIME",i(8),i(6),i(0),i(10),i(1),null,null,i(9),i(2),null,i(11),"YES",genid,i(12),i(10)},
{null,schema,"GETPCTEST1","T",i(1),i(93),"TIMESTAMP",i(29),i(16),i(9),i(10),i(1),null,null,i(9),i(3),null,i(12),"YES",genid,i(12),i(11)},
{null,schema,"GETPCTEST2","PA",i(1),i(4),"INTEGER",i(10),i(4),i(0),i(10),i(1),null,null,i(4),null,null,i(1),"YES",genid,i(2),i(0)},
{null,schema,"GETPCTEST2","PB",i(1),i(-5),"BIGINT",i(19),i(40),i(0),i(10),i(1),null,null,i(-5),null,null,i(2),"YES",genid,i(2),i(1)},
{null,schema,"GETPCTEST3A","STRING1",i(1),i(12),"VARCHAR",i(5),i(10),null,null,i(1),null,null,i(12),null,i(10),i(1),"YES",genid,i(2),i(0)},
{null,schema,"GETPCTEST3A","STRING2",i(4),i(12),"VARCHAR",i(5),i(10),null,null,i(1),null,null,i(12),null,i(10),i(2),"YES",genid,i(2),i(1)},
{null,schema,"GETPCTEST3B","STRING3",i(1),i(12),"VARCHAR",i(5),i(10),null,null,i(1),null,null,i(12),null,i(10),i(1),"YES",genid,i(2),i(0)},
{null,schema,"GETPCTEST3B","STRING4",i(2),i(12),"VARCHAR",i(5),i(10),null,null,i(1),null,null,i(12),null,i(10),i(2),"YES",genid,i(2),i(1)},
{null,schema,"GETPCTEST4BX","RETPARAM",i(4),i(4),"INTEGER",i(10),i(4),i(0),i(10),i(1),null,null,i(4),null,null,i(1),"YES",genid,i(1),i(0)},
};
if ( supportsBoolean )
{
expRS = appendArray
(
expRS,
new Object[][]
{
{null,schema,"GETPCTEST5","INARG",i(1),i(16),"BOOLEAN",i(1),i(1),null,null,i(1),null,null,i(16),null,null,i(1),"YES",genid,i(3),i(0)},
{null,schema,"GETPCTEST5","OUTARG",i(4),i(16),"BOOLEAN",i(1),i(1),null,null,i(1),null,null,i(16),null,null,i(2),"YES",genid,i(3),i(1)},
{null,schema,"GETPCTEST5","INOUTARG",i(2),i(16),"BOOLEAN",i(1),i(1),null,null,i(1),null,null,i(16),null,null,i(3),"YES",genid,i(3),i(2)},
}
);
}
Object[][] jdbcExpRS = new Object[expRS.length][];
Object[][] odbcExpRS = new Object[expRS.length][];
for (int i = 0; i < jdbcExpRS.length; i++) {
Object[] row = expRS[i];
// JDBC variant has always null in column #15 (SQL_DATA_TYPE) and
// column #16 (SQL_DATETIME_SUB)
Object[] jdbcRow = row.clone();
jdbcRow[14] = jdbcRow[15] = null;
// ODBC variant lacks column #20 (SPECIFIC_NAME)...
ArrayList<Object> odbcRow =
new ArrayList<Object>(Arrays.asList(row));
odbcRow.remove(19);
// ... and it is a bit different in the datetime rows
if (i == 9) {
odbcRow.set(9, null);
}
if (i >= 9 && i <= 11) {
odbcRow.set(10, i(2));
}
jdbcExpRS[i] = jdbcRow;
odbcExpRS[i] = odbcRow.toArray();
}
JDBC.assertFullResultSet(rs[0], jdbcExpRS, false);
JDBC.assertFullResultSet(rs[1], odbcExpRS, false);
if ( supportsBoolean ) { s.execute("drop procedure GETPCTEST5"); }
s.execute("drop procedure GETPCTEST4Bx");
s.execute("drop procedure GETPCTEST4B");
s.execute("drop procedure GETPCTEST4A");
s.execute("drop procedure GETPCTEST3B");
s.execute("drop procedure GETPCTEST3A");
s.execute("drop procedure GETPCTEST2");
s.execute("drop procedure GETPCTEST1");
commit();
}
/**
* Append one two-dimensional array to another.
* @param target
* @param suffix
* @return appended array
*/
private Object[][] appendArray(Object[][] target, Object[][] suffix)
{
int targetLength = target.length;
int suffixLength = suffix.length;
int resultLength = targetLength + suffixLength;
Object[][] result = new Object[resultLength][];
System.arraycopy(target, 0, result, 0, targetLength);
System.arraycopy(suffix, 0, result, targetLength, suffixLength);
println( "Appended array" );
return result;
}
/**
* Helper method for testing getProcedures - calls the ODBC procedure
* @param catalog
* @param schemaPattern
* @param procedureNamePattern
* @return the result set of the query
* @throws SQLException
*/
private ResultSet getProceduresODBC(
String catalog, String schemaPattern, String procedureNamePattern)
throws SQLException
{
CallableStatement cs = prepareCall("CALL SYSIBM.SQLPROCEDURES(" +
"?, ?, ?, 'DATATYPE=''ODBC''')");
cs.setString(1, catalog);
cs.setString(2, schemaPattern);
cs.setString(3, procedureNamePattern);
cs.execute();
return cs.getResultSet();
}
/**
* Helper method for testing getProcedures - calls dmd.getProcedures for
* the JDBC call, and getProceduresODBC for the ODBC procedure
*
* @param catalog
* @param schemaPattern
* @param procedureNamePattern
* @return an array of two result sets corresponding to JDBC and ODBC
* respectively
*
* @throws SQLException
*/
private ResultSet[] getProcedures(
String catalog, String schemaPattern, String procedureNamePattern)
throws SQLException
{
ResultSet[] rs = new ResultSet[2];
DatabaseMetaData dmd = getDMD();
rs[0]= dmd.getProcedures(catalog, schemaPattern, procedureNamePattern);
rs[1]= getProceduresODBC(catalog, schemaPattern, procedureNamePattern);
String[] columnNames = new String[] {
"PROCEDURE_CAT","PROCEDURE_SCHEM","PROCEDURE_NAME",
"RESERVED1","RESERVED2","RESERVED3",
"REMARKS","PROCEDURE_TYPE","SPECIFIC_NAME"};
int[] columnTypes = new int[] {
Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.INTEGER,
Types.INTEGER, Types.INTEGER, Types.VARCHAR, Types.SMALLINT,
Types.VARCHAR};
boolean[] nullability = new boolean[] {
false, false, false, true, true, true, true, true, false};
// JDBC result set
assertMetaDataResultSet(rs[0], columnNames, columnTypes, nullability);
// change the shape for ODBC - one less column, no SPECIFIC_NAME (9)
String[] odbcColumnNames = new String[columnNames.length - 1];
System.arraycopy(columnNames, 0, odbcColumnNames, 0, odbcColumnNames.length);
int[] odbcColumnTypes = new int[columnTypes.length - 1];
System.arraycopy(columnTypes, 0, odbcColumnTypes, 0, odbcColumnTypes.length);
boolean[] odbcNullability = new boolean[nullability.length - 1];
System.arraycopy(nullability, 0, odbcNullability, 0, odbcNullability.length);
// Change column names
odbcColumnNames[4 - 1] = "NUM_INPUT_PARAMS";
odbcColumnNames[5 - 1] = "NUM_OUTPUT_PARAMS";
odbcColumnNames[6 - 1] = "NUM_RESULT_SETS";
// ODBC result set.
assertMetaDataResultSet(
rs[1], odbcColumnNames, odbcColumnTypes, odbcNullability);
return rs;
}
/**
* Helper method for testing getProcedureColumns - calls the ODBC procedure
* @param catalog
* @param schemaPattern
* @param procedureNamePattern
* @param columnNamePattern
* @return the result set of the query
* @throws SQLException
*/
private ResultSet getProcedureColumnsODBC(String catalog,
String schemaPattern, String procedureNamePattern,
String columnNamePattern) throws SQLException
{
CallableStatement cs = prepareCall("CALL SYSIBM.SQLPROCEDURECOLS(" +
"?, ?, ?, ?, 'DATATYPE=''ODBC''')");
cs.setString(1, catalog);
cs.setString(2, schemaPattern);
cs.setString(3, procedureNamePattern);
cs.setString(4, columnNamePattern);
cs.execute();
return cs.getResultSet();
}
/**
* Helper method for testing getProcedureColumns - calls
* dmd.getProcedureColumns for the JDBC call, and
* getProcedureColumnssODBC for the ODBC procedure
* @param catalog
* @param schemaPattern
* @param procedureNamePattern
* @param columnNamePattern
* @return an array of two result sets corresponding to JDBC and ODBC
* respectively
* @throws SQLException
*/
private ResultSet[] getProcedureColumns(String catalog,
String schemaPattern, String procedureNamePattern,
String columnNamePattern) throws SQLException
{
ResultSet[] rss = new ResultSet[2];
DatabaseMetaData dmd = getDMD();
rss[0]= dmd.getProcedureColumns(catalog, schemaPattern,
procedureNamePattern, columnNamePattern);
rss[1]= getProcedureColumnsODBC(catalog, schemaPattern,
procedureNamePattern, columnNamePattern);
String[] columnNames = new String[] {
"PROCEDURE_CAT","PROCEDURE_SCHEM","PROCEDURE_NAME",
"COLUMN_NAME", "COLUMN_TYPE", "DATA_TYPE", "TYPE_NAME",
"PRECISION", "LENGTH", "SCALE",
"RADIX", "NULLABLE", "REMARKS", "COLUMN_DEF",
"SQL_DATA_TYPE", "SQL_DATETIME_SUB", "CHAR_OCTET_LENGTH",
"ORDINAL_POSITION", "IS_NULLABLE", "SPECIFIC_NAME" //};
// interesting, we seem to have two extra columns vs the API
,"METHOD_ID", "PARAMETER_ID"};
int[] columnTypes = new int[] {
Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
Types.SMALLINT, Types.INTEGER, Types.VARCHAR, Types.INTEGER,
Types.INTEGER, Types.SMALLINT, Types.SMALLINT, Types.SMALLINT,
Types.VARCHAR, Types.VARCHAR, Types.INTEGER, Types.INTEGER,
Types.INTEGER, Types.INTEGER, Types.VARCHAR, Types.VARCHAR //};
, Types.SMALLINT, Types.SMALLINT};
boolean[] nullability = new boolean[] {
true, false, false, false, false, false, false, false, false, true,
true, false, true, true, true, true, true, false, false, false//};
, false, false};
// JDBC result set
assertMetaDataResultSet(rss[0], columnNames, columnTypes, nullability);
// Change expected shape for ODBC
// One less column for ODBC, (20) SPECIFIC_NAME is missing.
String[] odbcColumnNames = new String[columnNames.length - 1];
System.arraycopy(columnNames, 0, odbcColumnNames, 0, 19);
System.arraycopy(columnNames, 21 - 1, odbcColumnNames, 20 - 1, 2);
int[] odbcColumnTypes = new int[columnTypes.length - 1];
System.arraycopy(columnTypes, 0, odbcColumnTypes, 0, 19);
System.arraycopy(columnTypes, 21 - 1, odbcColumnTypes, 20 - 1, 2);
// SQL_DATA_TYPE NULL in JDBC, valid type in ODBC.
// otherwise the same as JDBC
boolean[] odbcNullability = new boolean[] {
true, false, false, false, false, false, false, false, false, true,
true, false, true, true, false, true, true, false, false, false
, false};
// And change some column names.
odbcColumnNames[8 - 1] = "COLUMN_SIZE";
odbcColumnNames[9 - 1] = "BUFFER_LENGTH";
odbcColumnNames[10 - 1] = "DECIMAL_DIGITS";
odbcColumnNames[11 - 1] = "NUM_PREC_RADIX";
// And some column types.
odbcColumnTypes[6 - 1] = Types.SMALLINT;
odbcColumnTypes[15 - 1] = Types.SMALLINT;
odbcColumnTypes[16 - 1] = Types.SMALLINT;
// odbc result set
assertMetaDataResultSet(
rss[1], odbcColumnNames, odbcColumnTypes, odbcNullability);
return rss;
}
/**
* Test DatabaseMetaData.getFunctionColumns()
*
* @throws java.lang.Exception
*/
public void testGetFunctionColumns() throws Exception
{
// this method is supported in database meta data only from 10.2 onward
boolean supportsBoolean = true;
Version dataVersion = getDataVersion( getConnection() );
if ( dataVersion.compareTo( new Version( 10, 7, 0, 0 ) ) < 0 ) { supportsBoolean = false; }
if ( dataVersion.compareTo( new Version( 10, 2, 0, 0 ) ) < 0 ) { return; }
DatabaseMetaData dmd = getDMD();
Statement s = createStatement();
getConnection().setAutoCommit(false);
s.execute("create function f_gfc_1 (" +
"a VARCHAR(3), b NUMERIC, c SMALLINT, " +
"e CHAR(3), f INTEGER, g BIGINT, h FLOAT, i DOUBLE PRECISION, " +
"k DATE, l TIME, T TIMESTAMP ) returns int "+
"language java external name " +
"'org.apache.derbyTesting.BlahBlah.blah'" +
" parameter style java");
if ( supportsBoolean )
{
s.execute("create function f_gfc_2 ( a boolean) returns boolean "+
"language java external name " +
"'org.apache.derbyTesting.functionTests.BlahBlah.blah'" +
" parameter style java");
}
// We have to use reflection to get the getFunctionColumns() method.
// That is because we compile this test to run on old versions of the
// vm whose DatabaseMetaData doesn't include this method, even though
// our actual drivers do.
Method gfcMethod = dmd.getClass().getMethod
( "getFunctionColumns", new Class<?>[] {
String.class, String.class, String.class, String.class, } );
ResultSet rs = (ResultSet) gfcMethod.invoke(
dmd, (Object[])new String[] { null, "%", "F_GFC_%", "%" } );
JDBC.GeneratedId genid = new JDBC.GeneratedId();
Object[][] expRS = new Object[][] {
{null,schema,"F_GFC_1","",i(4),i(4),"INTEGER",i(10),i(4),i(0),i(10),i(1),null,null,i(0),"YES",genid,i(11),i(-1)},
{null,schema,"F_GFC_1","A",i(1),i(12),"VARCHAR",i(3),i(6),null,null,i(1),null,i(6),i(1),"YES",genid,i(11),i(0)},
{null,schema,"F_GFC_1","B",i(1),i(2),"NUMERIC",i(5),i(14),i(0),i(10),i(1),null,null,i(2),"YES",genid,i(11),i(1)},
{null,schema,"F_GFC_1","C",i(1),i(5),"SMALLINT",i(5),i(2),i(0),i(10),i(1),null,null,i(3),"YES",genid,i(11),i(2)},
{null,schema,"F_GFC_1","E",i(1),i(1),"CHAR",i(3),i(6),null,null,i(1),null,i(6),i(4),"YES",genid,i(11),i(3)},
{null,schema,"F_GFC_1","F",i(1),i(4),"INTEGER",i(10),i(4),i(0),i(10),i(1),null,null,i(5),"YES",genid,i(11),i(4)},
{null,schema,"F_GFC_1","G",i(1),i(-5),"BIGINT",i(19),i(40),i(0),i(10),i(1),null,null,i(6),"YES",genid,i(11),i(5)},
{null,schema,"F_GFC_1","H",i(1),i(8),"DOUBLE",i(52),i(8),null,i(2),i(1),null,null,i(7),"YES",genid,i(11),i(6)},
{null,schema,"F_GFC_1","I",i(1),i(8),"DOUBLE",i(52),i(8),null,i(2),i(1),null,null,i(8),"YES",genid,i(11),i(7)},
{null,schema,"F_GFC_1","K",i(1),i(91),"DATE",i(10),i(6),i(0),i(10),i(1),null,null,i(9),"YES",genid,i(11),i(8)},
{null,schema,"F_GFC_1","L",i(1),i(92),"TIME",i(8),i(6),i(0),i(10),i(1),null,null,i(10),"YES",genid,i(11),i(9)},
{null,schema,"F_GFC_1","T",i(1),i(93),"TIMESTAMP",i(29),i(16),i(9),i(10),i(1),null,null,i(11),"YES",genid,i(11),i(10)},
};
if ( supportsBoolean )
{
expRS = appendArray
(
expRS,
new Object[][]
{
{null,schema,"F_GFC_2","",i(4),i(16),"BOOLEAN",i(1),i(1),null,null,i(1),null,null,i(0),"YES",genid,i(1),i(-1)},
{null,schema,"F_GFC_2","A",i(1),i(16),"BOOLEAN",i(1),i(1),null,null,i(1),null,null,i(1),"YES",genid,i(1),i(0)},
}
);
}
JDBC.assertFullResultSet(rs, expRS, false);
if ( supportsBoolean ) { s.execute("drop function f_gfc_2"); }
s.execute("drop function f_gfc_1");
commit();
}
/**
* Convert an {@code int} to a {@code java.lang.Integer}.
* @param i
* @return an Integer
*/
private static Integer i(int i) {
return i;
}
/**
* Test methods added by JDBC 4.1
*
* @throws java.lang.Exception
*/
public void test_jdbc4_1() throws Exception
{
Version dataVersion = getDataVersion( getConnection() );
if ( dataVersion.compareTo( new Version( 10, 8, 0, 0 ) ) < 0 ) { return; }
Statement s = createStatement();
DatabaseMetaData dmd = getDMD();
println( "Testing JDBC 4.1 methods on a " + dmd.getClass().getName() );
Wrapper41DBMD wrapper = new Wrapper41DBMD( dmd );
//
// generatedKeyAlwaysReturned()
//
assertEquals( true, wrapper.generatedKeyAlwaysReturned() );
//
// getPseudoColumns()
//
ResultSet rs = wrapper.getPseudoColumns( null, null, null, null );
assertMetaDataResultSet
(
rs,
new String[]
{
"TABLE_CAT", "TABLE_SCHEM", "TABLE_NAME", "COLUMN_NAME",
"DATA_TYPE", "COLUMN_SIZE", "DECIMAL_DIGITS", "NUM_PREC_RADIX",
"COLUMN_USAGE", "REMARKS", "CHAR_OCTET_LENGTH", "IS_NULLABLE",
},
new int[]
{
Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.INTEGER,
Types.VARCHAR, Types.VARCHAR, Types.INTEGER, Types.VARCHAR,
},
new boolean[]
{
true, true, false, false,
false, false, true, true,
false, true, true, false,
}
);
JDBC.assertFullResultSet( rs, new String[][] {} );
//
// IS_GENERATEDCOLUMN added to ResultSet returned by getColumns()
//
s.execute( "create table t_jdbc41( a int, b int, c generated always as ( -a ) )" );
ResultSet rs2 = dmd.getColumns( null, schema, "T_JDBC41", null );
String[][] expectedRows = new String[][]
{
{
"", schema, "T_JDBC41", "A",
"4", "INTEGER", "10", null,
"0", "10", "1", "",
null, null, null, null,
"1", "YES", null, null,
null, null, "NO", "NO",
null
},
{
"", schema, "T_JDBC41", "B",
"4", "INTEGER", "10", null,
"0", "10", "1", "",
null, null, null, null,
"2", "YES", null, null,
null, null, "NO", "NO",
null
},
{
"", schema, "T_JDBC41", "C",
"4", "INTEGER", "10", null,
"0", "10", "1", "",
"GENERATED ALWAYS AS ( -a )", null, null, null,
"3", "YES", null, null,
null, null, "NO", "YES",
null
},
};
JDBC.assertFullResultSet( rs2, expectedRows );
s.execute( "drop table t_jdbc41" );
}
/**
* Test methods added by JDBC 4.2
*
* @throws java.lang.Exception
*/
public void test_jdbc4_2() throws Exception
{
Version dataVersion = getDataVersion( getConnection() );
if ( dataVersion.compareTo( new Version( 10, 10, 0, 0 ) ) < 0 ) { return; }
Statement s = createStatement();
DatabaseMetaData dmd = getDMD();
println( "Testing JDBC 4.2 methods on a " + dmd.getClass().getName() );
Wrapper42DBMD wrapper = new Wrapper42DBMD( dmd );
//
// getMaxLogicalLobSize()
//
assertEquals( 0L, wrapper.getMaxLogicalLobSize() );
//
// supportsRefCursors()
//
assertEquals( false, wrapper.supportsRefCursors() );
}
public void testBugFixes() throws SQLException {
Statement s = createStatement();
getConnection().setAutoCommit(false);
DatabaseMetaData dmd = getDMD();
// test DERBY-655, DERBY-1343
// If a table has duplicate backing index, then it will share the
// physical conglomerate with the existing index, but the duplicate
// indexes should have their own unique logical congomerates
// associated with them. That way, it will be possible to
// distinguish the 2 indexes in SYSCONGLOMERATES from each other.
s.execute("CREATE TABLE Derby655t1(c11_ID BIGINT NOT NULL)");
s.execute("CREATE TABLE Derby655t2 (c21_ID BIGINT NOT NULL primary key)");
s.execute("ALTER TABLE Derby655t1 ADD CONSTRAINT F_12 Foreign Key (c11_ID) REFERENCES Derby655t2 (c21_ID) ON DELETE CASCADE ON UPDATE NO ACTION");
s.execute("CREATE TABLE Derby655t3(c31_ID BIGINT NOT NULL primary key)");
s.execute("ALTER TABLE Derby655t2 ADD CONSTRAINT F_443 Foreign Key (c21_ID) REFERENCES Derby655t3(c31_ID) ON DELETE CASCADE ON UPDATE NO ACTION");
ResultSet rs = dmd.getImportedKeys("", schema, "DERBY655T1");
JDBC.assertDrainResults(rs, 1);
s.execute("drop table Derby655t1");
s.execute("drop table Derby655t2");
s.execute("drop table Derby655t3");
// This checks for a bug where you get incorrect behavior on a nested connection.
// if you do not get an error, the bug does not occur.
if(JDBC.vmSupportsJDBC3()){
s.execute("create procedure isReadO() language java external name " +
"'org.apache.derbyTesting.functionTests.tests.jdbcapi.DatabaseMetaDataTest.isro'" +
" parameter style java");
s.execute("call isReadO()");
}
}
/**
* method used in testBugFixes, for testing nexted connection metadata
* @throws SQLException
*/
public static void isro() throws SQLException {
DriverManager.getConnection(
"jdbc:default:connection").getMetaData().isReadOnly();
}
/**
* getColumns() used to fail with a truncation error if an auto-increment
* column had a start value or an increment that was very large (that is,
* when its CHAR representation exceeded 12 characters). DERBY-5274.
*
* @throws java.sql.SQLException
*/
public void testGetColumns_DERBY5274() throws SQLException {
// Disable auto-commit to allow easy cleanup.
setAutoCommit(false);
Statement s = createStatement();
// Create a test table with an identity column whose start value and
// increment are very large.
final long bignum = 648518346341351400L;
s.execute("create table derby5274(x bigint not null " +
"generated always as identity (" +
"start with " + bignum + ", increment by " + bignum + "))");
// Expected values for various columns in the meta-data.
String[][] expected = {
{"TABLE_SCHEM", schema},
{"TABLE_NAME", "DERBY5274"},
{"COLUMN_NAME", "X"},
{"COLUMN_DEF",
"AUTOINCREMENT: start " + bignum + " increment " + bignum},
{"IS_NULLABLE", "NO"},
};
// Get meta-data for the column in the test table. This used to fail
// with a truncation error before DERBY-5274.
ResultSet rs = getDMD().getColumns(null, null, "DERBY5274", null);
// Verify that the returned meta-data looks right.
assertTrue("No columns found", rs.next());
for (String[] expected1 : expected) {
String label = expected1[0];
String expectedVal = expected1[1];
assertEquals(label, expectedVal, rs.getString(label));
}
// There's only one column in the test table, so there should be no
// more rows in the meta-data.
JDBC.assertEmpty(rs);
// Clean up.
rollback();
}
/**
* Dummy method to test getProcedureColumns
*
* @param a
* @param b
* @param k
* @param c
* @param e
* @param d
* @param f
* @param g
* @param j
* @param i
* @param l
* @param h
* @param T
* @return the {@code j} byte array
*/
public static byte[] getpc(
String a, BigDecimal b, short c, byte d, short e,
int f, long g, float h, double i, byte[] j, Date k, Time l, Timestamp T)
{
return j;
}
/**
* Overload getpc to further test getProcedureColumns
*
* @param a
* @param b
*/
public static void getpc(int a, long[] b)
{
}
/**
* Overload getpc to further test getProcedureColumns
* private method shouldn't be returned with alias, ok with procedure
*
* @param a
* @param b
*/
private static void getpc(int a, long b)
{
}
/**
* instance method for getProcedureColumns testing
* with method alias, this should not be returned by getProcedureColumns
* but DB2 returns this with a java procedure
* @param a
* @param b
*/
public void getpc(String a, String b) {
}
/**
* this method should notbe seen by getProcedureColumns as
* it has no parameters and no return value.
*/
public static void getpc4a() {
}
/**
* Check a method with no parameters and a return value works
* for getProcedureColumns
*
* @return the int 4
*/
public static int getpc4b() {
return 4;
}
/**
* Reading of DatabaseMetaData obtained earlier, after a connection
* is closed.
*
* @throws java.sql.SQLException
*/
public void testDMDconnClosed() throws SQLException {
ResultSet rs_ = getConnection().getMetaData().
getTables("%","%","%",null); // should work
getConnection().close();
try {
//should throw exception since the connection is closed
rs_.next();
fail("No Exception throw when getting metadata.");
} catch(SQLException sqle) {
assertSQLState("XCL16", sqle);
}
}
/**
* <p>
* Get the version of the data in the database.
* </p>
* @param conn the current connection
* @return data base version
* @throws java.lang.Exception
*/
@SuppressWarnings({"BroadCatchBlock", "TooBroadCatch", "UseSpecificCatch"})
private Version getDataVersion( Connection conn )
throws Exception
{
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.prepareStatement( "values syscs_util.syscs_get_database_property('DataDictionaryVersion')" );
rs = ps.executeQuery();
rs.next();
String retval = rs.getString( 1 );
int dotIdx = retval.indexOf( '.' );
int major = Integer.parseInt( retval.substring( 0, dotIdx ) );
int minor = Integer.parseInt( retval.substring( dotIdx + 1, retval.length() ) );
return new Version( major, minor, 0, 0 );
}
catch (Exception se)
{
printStackTrace( se );
return null;
}
finally
{
if ( rs != null ) { rs.close(); }
if ( ps != null ) { ps.close(); }
}
}
}
|