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
|
/*
Derby - Class org.apache.derbyTesting.functionTests.tests.lang.LangProcedureTest
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 governing permissions and
limitations under the License.
*/
package org.apache.derbyTesting.functionTests.tests.lang;
import java.math.BigDecimal;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;
import java.sql.Types;
import java.util.Arrays;
import java.util.Locale;
import junit.framework.Test;
import org.apache.derbyTesting.functionTests.util.ProcedureTest;
import org.apache.derbyTesting.functionTests.util.TestUtil;
import org.apache.derbyTesting.junit.BaseJDBCTestCase;
import org.apache.derbyTesting.junit.BaseTestSuite;
import org.apache.derbyTesting.junit.JDBC;
import org.apache.derbyTesting.junit.LocaleTestSetup;
import org.apache.derbyTesting.junit.TestConfiguration;
/**
* Test the syntax for creating procedures and semantics when
* executing them.
*/
public class LangProcedureTest extends BaseJDBCTestCase {
private final static String LANG_STRING_TRUNCATION = "22001";
private final static String LANG_INVALID_CALL_STATEMENT = "42X74";
private final String thisClassName = getClass().getName();
public LangProcedureTest(String name) {
super(name);
}
protected void tearDown() throws Exception {
super.tearDown();
}
/**
* Default suite for running this test (embedded and client).
*/
public static Test suite() {
if (JDBC.vmSupportsJSR169()) {
return new BaseTestSuite(
"Empty LangProcedureTest. " +
"JSR169 does not support jdbc:default:connection");
} else {
// fix locale since we need to check error messages
return TestConfiguration.singleUseDatabaseDecorator(
new LocaleTestSetup(
TestConfiguration.defaultSuite(LangProcedureTest.class),
Locale.ENGLISH));
}
}
/**
* Verifies the exception that gets raised for bad syntax or unsupported
* features.
*
* @throws SQLException
*/
public void testCreateRoutineErrors() throws SQLException {
Statement s = createStatement();
// The expected format is path.method (no '.').
assertStatementError("42Y04", s,
"create procedure asdf() language java "
+ "external name 'asdfasdf' parameter style java");
// The expected format is path.method (there's a trailing '.').
assertStatementError("42Y04", s,
"create procedure asdf() language java "
+ "external name 'asdfasdf.' parameter style java");
// The procedure name exceeds the max length.
assertStatementError(
"42622",
s,
"create procedure a234567890123456789012345678901234567890123"
+ "456789012345678901234567890123456789012345678901234567890123"
+ "45678901234567890123456789() language java external name "
+ "'asdf.asdf' parameter style java");
// "LANGUAGE C" is an incorrect language.
assertStatementError(
"42X01",
s,
"create procedure ASSEMBLY_PARTS (in ASSEMBLY_NUM integer, "
+ "out NUM_PARTS integer, out COST DOUBLE) external name "
+ "'parts!assembly' dynamic result sets 1 language C "
+ "parameter style GENERAL");
// Not allowed to create a routine in the SYS schema.
assertStatementError(
"42X62",
s,
"create procedure sys.proc1() language java "
+ "external name 'java.lang.System.gc' parameter style java");
// The 'LANGUAGE' clause has multiple or conflicting keywords ('java').
assertStatementError(
"42613",
s,
"create procedure noclass() language java "
+ "external name 'asdf.asdf' parameter style java language java");
assertStatementError(
"42613", s,
"create procedure noclass() parameter style java language java "
+ "external name 'asdf.asdf' parameter style java");
assertStatementError(
"42613",
s,
"create procedure noclass() external name 'asdf.xxxx' "
+ "language java external name 'asdf.asdf' parameter style java");
assertStatementError(
"42X01",
s,
"create procedure noclass() parameter style java language java "
+ "external name 'asdf.asdf' parameter style derby_rs_collection");
// The create statement is missing an element.
assertStatementError("42X01", s, "create procedure missing01()");
assertStatementError("42X01", s,
"create procedure missing02() language java");
assertStatementError(
"42X01", s,
"create procedure missing03() language java parameter style java");
assertStatementError("42X01", s,
"create procedure missing04() language java "
+ "external name 'foo.bar'");
assertStatementError("42X01", s,
"create procedure missing05() parameter style java");
assertStatementError("42X01", s,
"create procedure missing06() parameter style java "
+ "external name 'foo.bar'");
assertStatementError("42X01", s,
"create procedure missing07() external name 'goo.bar'");
assertStatementError("42X01", s,
"create procedure missing08() dynamic result sets 1");
assertStatementError("0A000", s,
"create procedure missing09() specific name fred");
// RETURNS NULL ON NULL INPUT isn't allowed in procedures.
assertStatementError(
"42X01",
s,
"create procedure nullinput2() returns null on null input "
+ "language java parameter style java external name 'foo.bar'");
// Duplicate parameter names.
assertStatementError("42734", s, "create procedure DUP_P1"
+ "(in FRED int, out RON char(10), in FRED int) "
+ "language java parameter style java external name 'no.dup1'");
assertStatementError("42734", s, "create procedure D2.DUP_P2"
+ "(in \"FreD\" int, out RON char(10), in \"FreD\" int) "
+ "language java parameter style java external name 'no.dup2'");
assertStatementError("42734", s, "create procedure D3.DUP_P3"
+ "(in \"FRED\" int, out RON char(10), in fred int) "
+ "language java parameter style java external name 'no.dup3'");
// This one should succeed.
s.execute(
"create procedure DUP_POK"
+ "(in \"FreD\" int, out RON char(10), in fred int) "
+ "language java parameter style java external name 'no.dupok'");
s.execute("drop procedure DUP_POK");
// procedure not found with explicit schema name
assertStatementError("42Y03", s, "call APP.NSP(?, ?)");
// Long ago this caused a null pointer exception.
assertStatementError(
"42X15", s,
"call syscs_util.syscs_set_database_property(\"foo\", \"bar\")");
// Not so long ago (DERBY-6212) this caused a NullPointerException.
assertCompileError(LANG_INVALID_CALL_STATEMENT,
"call syscs_util.syscs_set_database_property('foo', (values 'bar'))");
s.close();
}
/**
* Verifies the fix for DERBY-258: incorrect method resolution if an
* explicit method signature has Java type that does not match the correct
* SQL to Java mapping as defined by JDBC.
*
* @throws SQLException
*/
public void testMethodSignatureDerby258() throws SQLException {
Statement s = createStatement();
// int doesn't match String
s.execute("create procedure SIGNATURE_BUG_DERBY_258_A(in A int) "
+ "language java parameter style java "
+ "external name 'java.lang.System.load(java.lang.String)'");
assertStatementError("22005", s,
"call APP.SIGNATURE_BUG_DERBY_258_A(4)");
s.execute("drop PROCEDURE SIGNATURE_BUG_DERBY_258_A");
// Signature with too many arguments.
s.execute("create function SIGNATURE_BUG_DERBY_258_B(A int) "
+ "RETURNS varchar(128) language java parameter style java "
+ "external name 'java.lang.Integer.toString(int, int)'");
assertStatementError("46J02", s,
"values APP.SIGNATURE_BUG_DERBY_258_B(4)");
s.execute("drop FUNCTION SIGNATURE_BUG_DERBY_258_B");
// Signature with too few arguments.
s.execute("create procedure SIGNATURE_BUG_DERBY_258_C(in A int) "
+ "language java parameter style java "
+ "external name 'java.lang.System.gc()'");
assertStatementError("46J02", s,
"call APP.SIGNATURE_BUG_DERBY_258_C(4)");
s.execute("drop PROCEDURE SIGNATURE_BUG_DERBY_258_C");
// Java method signature has only a leading parenthesis.
s.execute("create procedure SIGNATURE_BUG_DERBY_258_F(in A int) "
+ "language java parameter style java "
+ "external name 'java.lang.System.gc('");
assertStatementError("46J01", s,
"call APP.SIGNATURE_BUG_DERBY_258_F(4)");
s.execute("drop PROCEDURE SIGNATURE_BUG_DERBY_258_F");
// Java method signature of (,,)
s.execute("create procedure SIGNATURE_BUG_DERBY_258_G(in A int) "
+ "language java parameter style java "
+ "external name 'java.lang.System.gc(,,)'");
assertStatementError("46J01", s,
"call APP.SIGNATURE_BUG_DERBY_258_G(4)");
s.execute("drop PROCEDURE SIGNATURE_BUG_DERBY_258_G");
// Java method signature of (, ,)
s.execute("create procedure SIGNATURE_BUG_DERBY_258_H(in A int) "
+ "language java parameter style java "
+ "external name 'java.lang.System.gc(, ,)'");
assertStatementError("46J01", s,
"call APP.SIGNATURE_BUG_DERBY_258_H(4)");
s.execute("drop PROCEDURE SIGNATURE_BUG_DERBY_258_H");
// Java method signature of (int,)
s.execute("create procedure SIGNATURE_BUG_DERBY_258_I(in A int) "
+ "language java parameter style java "
+ "external name 'java.lang.System.gc(int ,)'");
assertStatementError("46J01", s,
"call APP.SIGNATURE_BUG_DERBY_258_I(4)");
s.execute("drop PROCEDURE SIGNATURE_BUG_DERBY_258_I");
s.execute("create procedure DERBY_3304() "
+ " dynamic result sets 1 language java parameter style java "
+ " external name '" + getClass().getName() + ".DERBY_3304'"
+ " modifies sql data");
String[][] t1Results = { { "APP"} };
ResultSet rs = s.executeQuery("call APP.DERBY_3304()");
JDBC.assertFullResultSet(rs, t1Results);
s.execute("drop PROCEDURE DERBY_3304");
s.close();
}
/**
* This procedure does an explicit commit and then creates a resultset
* to be passed back to the caller. As part of commit, we should not
* close the resultset that will be returned by this procedure.
*
* @param rs1
* @throws SQLException
*/
public static void DERBY_3304(ResultSet[] rs1) throws SQLException
{
Connection conn =
DriverManager.getConnection("jdbc:default:connection");
Statement stm = conn.createStatement();
conn.commit();
ResultSet rs = stm.executeQuery("values current_user");
rs1[0] = rs;
}
/**
* Tests the exception that gets thrown at runtime when the external method
* for a SQL procedure doesn't exist -- there's no check for existence at
* CREATE time, the check occurs at runtime.
*
* @throws SQLException
*/
public void testDelayedClassChecking() throws SQLException {
Statement s = createStatement();
s.execute(
"create procedure noclass() language java " +
"external name 'asdf.asdf' parameter style java");
s.execute(
"create procedure nomethod() language java " +
"external name 'java.lang.Integer.asdf' parameter style java");
s.execute(
"create procedure notstatic() language java " +
"external name 'java.lang.Integer.equals' parameter style java");
s.execute(
"create procedure notvoid() language java " +
"external name " +
"'java.lang.Runtime.getRuntime' parameter style java");
assertCallError("42X51", "call noclass()");
assertCallError("42X50", "call nomethod()");
assertCallError("42X50", "call notstatic()");
assertCallError("42X50", "call notvoid()");
// Comment from old test: CHECK SYSALIAS
s.execute("drop procedure noclass");
s.execute("drop procedure nomethod");
s.execute("drop procedure notstatic");
s.execute("drop procedure notvoid");
s.close();
}
public void testDuplicates() throws SQLException {
Connection conn = getConnection();
Statement s = createStatement();
s.execute("create schema S1");
s.execute("create schema S2");
s.execute("create procedure PROCDUP() language java " +
"external name 'okAPP.ok0' parameter style java");
s.execute("create procedure s1.PROCDUP() language java " +
"external name 'oks1.ok0' parameter style java");
s.execute("create procedure s2.PROCDUP() language java " +
"external name 'oks2.ok0' parameter style java");
assertStatementError(
"X0Y68",
s,
"create procedure PROCDUP() language java " +
"external name 'failAPP.fail0' parameter style java");
assertStatementError(
"X0Y68",
s,
"create procedure s1.PROCDUP() language java " +
"external name 'fails1.fail0' parameter style java");
assertStatementError(
"X0Y68",
s,
"create procedure s2.PROCDUP() language java " +
"external name 'fails2.fail0' parameter style java");
String[] sysAliasDefinition = {
"APP.PROCDUP AS okAPP.ok0() " +
"LANGUAGE JAVA PARAMETER STYLE JAVA MODIFIES SQL DATA",
"S1.PROCDUP AS oks1.ok0() " +
"LANGUAGE JAVA PARAMETER STYLE JAVA MODIFIES SQL DATA",
"S2.PROCDUP AS oks2.ok0() " +
"LANGUAGE JAVA PARAMETER STYLE JAVA MODIFIES SQL DATA"};
String[] DBMetaDefinition = {
"APP.PROCDUP AS okAPP.ok0 type procedureNoResult",
"S1.PROCDUP AS oks1.ok0 type procedureNoResult",
"S2.PROCDUP AS oks2.ok0 type procedureNoResult" };
checkMatchingProcedures(conn, "PROCDUP", sysAliasDefinition,
DBMetaDefinition, null);
assertStatementError(
"0A000",
s,
"create procedure S1.NOTYET() SPECIFIC fred language java " +
"external name 'failAPP.fail0' parameter style java");
s.execute("drop procedure PROCDUP");
s.execute("drop procedure s1.PROCDUP");
s.execute("drop procedure s2.PROCDUP");
s.execute("drop schema S1 RESTRICT");
s.execute("drop schema S2 RESTRICT");
s.close();
}
public void testAmbigiousMethods() throws SQLException {
Statement s = createStatement();
// ambiguous resolution - with result sets
s.execute("create procedure ambiguous01(p1 integer, p2 char(20)) " +
"dynamic result sets 1 language java parameter style java " +
"external name '" + thisClassName + ".ambiguous1'");
assertCallError("42X73", "call AMBIGUOUS01(?, ?)");
s.execute("drop procedure AMBIGUOUS01");
// ambiguous in defined parameters
s.execute("create procedure ambiguous02(p1 integer, p2 integer) " +
"dynamic result sets 1 language java parameter style java " +
"external name '" + thisClassName + ".ambiguous2'");
assertCallError("42X50", "call AMBIGUOUS02(?, ?)");
s.execute("drop procedure AMBIGUOUS02");
// After DERBY-3652 these are also ambiguous:
s.execute("create procedure ambiguous03(p1 integer, p2 integer) " +
"dynamic result sets 1 language java parameter style java " +
"external name '" + thisClassName +
".ambiguous2(int,java.lang.Integer)'");
assertCallError("42X73", "{call ambiguous03(1,NULL)}");
s.execute("drop procedure AMBIGUOUS03");
s.execute("create procedure ambiguous04(p1 integer, p2 integer) " +
"dynamic result sets 1 language java parameter style java " +
"external name '" + thisClassName +
".ambiguous2(java.lang.Integer,int)'");
assertCallError("42X73", "{call ambiguous04(NULL, 1)}");
s.execute("drop procedure AMBIGUOUS04");
s.close();
}
private static void checkMatchingProcedures(Connection conn,
String procedureName, String[] sysAliasDefinition,
String[] DBMetaDefinition, String[] columnDefinition)
throws SQLException {
// Until cs defaults to hold cursor we need to turn autocommit off
// while we do this because one metadata call will close the other's
// cursor
boolean saveAutoCommit = conn.getAutoCommit();
conn.setAutoCommit(false);
PreparedStatement ps = conn.prepareStatement(
"select schemaname, alias, " +
" cast (((javaclassname || '.' ) || " +
" cast (aliasinfo as varchar(1000))) as varchar(2000))" +
" as signature " +
"from sys.sysaliases A, sys.sysschemas S " +
"where alias like ? and A.schemaid = S.schemaid order by 1,2,3");
ps.setString(1, procedureName);
ResultSet rs = ps.executeQuery();
int i = 0;
while (rs.next()) {
assertEquals(sysAliasDefinition[i++], rs.getString(1) + "."
+ rs.getString(2) + " AS " + rs.getString(3));
}
rs.close();
DatabaseMetaData dmd = conn.getMetaData();
rs = dmd.getProcedures(null, null, procedureName);
// with jcc 2.1 for now this will fail on the second round,
// because the resultset gets closed when we do getProcedureColumns.
// thus, catch that gracefully...
i = 0;
while (rs.next()) {
String schema = rs.getString(2);
String name = rs.getString(3);
assertEquals(schema + "." + name + " AS " + rs.getString(7)
+ " type " + TYPE(rs.getShort(8)), DBMetaDefinition[i++]);
// get the column information.
ResultSet rsc = dmd.getProcedureColumns(null, schema, name, null);
int j = 0;
while (rsc.next()) {
assertEquals(PARAMTYPE(rsc.getShort(5)) + " "
+ rsc.getString(4) + " " + rsc.getString(7),
columnDefinition[j++]);
}
rsc.close();
}
rs.close();
// restore previous autocommit mode
conn.setAutoCommit(saveAutoCommit);
}
public void testZeroArgProcedures() throws SQLException {
Connection conn = getConnection();
Statement s = createStatement();
s.execute("create procedure za() language java external name '" +
thisClassName + ".zeroArg' parameter style java");
s.execute("call za()");
assertUpdateCountForProcedureWithNoResults(s);
PreparedStatement ps = prepareStatement("call za()");
ps.execute();
assertUpdateCountForProcedureWithNoResults(ps);
ps.close();
ps = prepareStatement("{call za()}");
ps.execute();
assertUpdateCountForProcedureWithNoResults(ps);
ps.close();
try {
prepareStatement("call za(?)");
fail("FAIL - prepareStatement call za(?)");
} catch (SQLException sqle) {
assertSQLState("42Y03", sqle);
}
CallableStatement cs = prepareCall("call za()");
cs.execute();
assertUpdateCountForProcedureWithNoResults(cs);
cs.close();
cs = prepareCall("{call za()}");
cs.execute();
assertUpdateCountForProcedureWithNoResults(cs);
cs.close();
String[] sysAliasDefinition = {
"APP.ZA AS " + thisClassName + ".zeroArg() " +
"LANGUAGE JAVA PARAMETER STYLE JAVA MODIFIES SQL DATA"};
String[] dbMetadataDefinition = {
"APP.ZA AS " + thisClassName + ".zeroArg " +
"type procedureNoResult"};
checkMatchingProcedures(conn, "ZA", sysAliasDefinition,
dbMetadataDefinition, null);
s.execute("drop procedure za");
checkMatchingProcedures(conn, "ZA", null, null, null);
s.close();
}
public void testSqlProcedures() throws SQLException {
Connection conn = getConnection();
Statement s = createStatement();
s.execute("create table t1(i int not null primary key, b char(15))");
s.execute(
"create procedure ir(p1 int) " +
"modifies sql data dynamic result sets 0 language java " +
"external name '" + thisClassName + ".insertRow' " +
"parameter style java");
s.execute(
"create procedure ir2(p1 int, p2 char(10)) " +
" modifies sql data language java " +
"external name '" + thisClassName + ".insertRow' " +
"parameter style java");
String[] sysaliasDefinition = {
"APP.IR AS " + thisClassName + ".insertRow" +
"(IN \"P1\" INTEGER) LANGUAGE JAVA " +
"PARAMETER STYLE JAVA MODIFIES SQL DATA" };
String[] dbMetadataDefinition = {
"APP.IR AS " + thisClassName + ".insertRow" +
" type procedureNoResult" };
String[] columnDefinition = { "procedureColumnIn P1 INTEGER" };
checkMatchingProcedures(conn, "IR", sysaliasDefinition,
dbMetadataDefinition, columnDefinition);
sysaliasDefinition = new String[] {
"APP.IR2 AS " + thisClassName + ".insertRow" +
"(IN \"P1\" INTEGER,IN \"P2\" CHAR(10)) " +
"LANGUAGE JAVA PARAMETER STYLE JAVA MODIFIES SQL DATA" };
dbMetadataDefinition = new String[] {
"APP.IR2 AS " + thisClassName + ".insertRow" +
" type procedureNoResult" };
columnDefinition = new String[] { "procedureColumnIn P1 INTEGER",
"procedureColumnIn P2 CHAR" };
checkMatchingProcedures(conn, "IR2", sysaliasDefinition,
dbMetadataDefinition, columnDefinition);
assertCallError("42Y03", "call IR()");
CallableStatement ir1 = prepareCall("call IR(?)");
ir1.setInt(1, 1);
ir1.execute();
ir1.setInt(1, 2);
ir1.execute();
try {
ir1.execute();
fail("FAIL - duplicate key insertion through ir");
} catch (SQLException sqle) {
assertSQLState("23505", sqle);
}
ir1.setString(1, "3");
ir1.execute();
ir1.close();
ir1 = conn.prepareCall("call APP.IR(?)");
ir1.setInt(1, 7);
ir1.execute();
CallableStatement ir2 = conn.prepareCall("call IR2(?, ?)");
ir2.setInt(1, 4);
ir2.setInt(2, 4);
ir2.execute();
ir2.setInt(1, 5);
ir2.setString(2, "ir2");
ir2.execute();
ir2.setInt(1, 6);
ir2.setString(2, "'012345678");
ir2.execute();
ir1.close();
ir2.close();
if (!conn.getAutoCommit()) {
conn.commit();
}
String[][] t1Results = { { "1", "int" }, { "2", "int" },
{ "3", "int" }, { "7", "int" }, { "4", "4" }, { "5", "ir2" },
{ "6", "'012345678" } };
ResultSet rs = s.executeQuery("select * from t1");
JDBC.assertFullResultSet(rs, t1Results);
if (!conn.getAutoCommit()) {
conn.commit();
}
assertCallError("38000", "call IR2(2, 'no way')");
assertCallError("07000", "call IR2(?, 'no way')");
assertCallError("07000", "call IR2(2, ?)");
s.execute("drop procedure IR");
s.execute("drop procedure IR2");
s.execute("drop table t1");
commit();
}
/**
* 1. basic testing 2. correct auto commit logic 3. correct holdability
* (JDBC 3)
*/
public void testDynamicResultSets() throws SQLException {
Connection conn = getConnection();
Connection conn2 = openDefaultConnection();
Statement s = createStatement();
assertStatementError(
"42X01",
s,
"create procedure DRS(p1 int) " +
"parameter style java reads sql data dynamic result sets -1 " +
"language java external name '" +
thisClassName + ".selectRows'");
s.execute("create procedure DRS(p1 int) " +
"parameter style java reads sql data dynamic result sets 1 " +
"language java external name '" +
thisClassName + ".selectRows'");
// Create a test table with some data that can be accessed by the
// DRS procedure.
s.execute("create table t1(i int not null primary key, b char(15))");
s.execute("insert into t1 values (1, 'int'), (2, 'int'), (3, 'int'), "
+ "(7, 'int'), (4, '4'), (5, 'ir2'), (6, '''012345678')");
String[] sysaliasDefinition = {
"APP.DRS AS " + thisClassName + ".selectRows" +
"(IN \"P1\" INTEGER) " +
"LANGUAGE JAVA PARAMETER STYLE JAVA " +
"READS SQL DATA DYNAMIC RESULT SETS 1" };
String[] dbMetadataDefinition = {"APP.DRS AS " + thisClassName +
".selectRows type procedureNoResult" };
String[] columnDefinition = { "procedureColumnIn P1 INTEGER" };
checkMatchingProcedures(conn, "DRS", sysaliasDefinition,
dbMetadataDefinition, columnDefinition);
assertCallError("42Y03", "call DRS()");
assertCallError("42Y03","call DRS(?,?)");
CallableStatement drs1 = prepareCall("call DRS(?)");
drs1.setInt(1, 3);
drs1.execute();
ResultSet rs = drs1.getResultSet();
String[][] drsResult = { { "3", "int" } };
JDBC.assertFullResultSet(rs, drsResult);
drs1.close();
s.execute("create procedure DRS2(p1 int, p2 int) " +
"parameter style java reads sql data " +
"dynamic result sets 2 language java " +
"external name '" + thisClassName + ".selectRows'");
sysaliasDefinition = new String[] {
"APP.DRS2 AS " + thisClassName + ".selectRows" +
"(IN \"P1\" INTEGER,IN \"P2\" INTEGER) " +
"LANGUAGE JAVA PARAMETER STYLE JAVA " +
"READS SQL DATA DYNAMIC RESULT SETS 2" };
dbMetadataDefinition = new String[] {
"APP.DRS2 AS " +
thisClassName + ".selectRows type procedureNoResult" };
columnDefinition = new String[] { "procedureColumnIn P1 INTEGER",
"procedureColumnIn P2 INTEGER" };
checkMatchingProcedures(conn, "DRS2", sysaliasDefinition,
dbMetadataDefinition, columnDefinition);
CallableStatement drs2;
drs2 = conn.prepareCall("call DRS2(?, ?)");
drs2.setInt(1, 2);
drs2.setInt(2, 6);
drs2.execute();
rs = drs2.getResultSet();
String[][] drs2Results = { { "2", "int" } };
JDBC.assertFullResultSet(rs, drs2Results);
assertTrue(drs2.getMoreResults());
drs2Results = new String[][] { { "6", "'012345678" }, { "7", "int" } };
rs = drs2.getResultSet();
JDBC.assertFullResultSet(rs, drs2Results);
// execute it returning one closed result set
drs2.setInt(1, 2);
drs2.setInt(2, 99); // will close the second result set
assertTrue(drs2.execute());
rs = drs2.getResultSet();
drs2Results = new String[][] { { "2", "int" } };
JDBC.assertFullResultSet(rs, drs2Results);
assertFalse(drs2.getMoreResults());
// execute it returning no result sets
drs2.setInt(1, 2);
drs2.setInt(2, 199); // return no results at all
assertFalse(drs2.execute());
assertFalse(drs2.getMoreResults());
// execute it returning two result sets but with the order swapped in
// the parameters
// doesnot affect display order.
drs2.setInt(1, 2);
drs2.setInt(2, 299); // swap results
assertTrue(drs2.execute());
drs2Results = new String[][] { { "2", "int" } };
rs = drs2.getResultSet();
JDBC.assertFullResultSet(rs, drs2Results);
assertTrue(drs2.getMoreResults());
rs = drs2.getResultSet();
JDBC.assertEmpty(rs);
drs2.setInt(1, 2);
drs2.setInt(2, 2);
drs2.execute();
ResultSet lastResultSet = null;
int pass = 1;
do {
if (lastResultSet != null) {
try {
lastResultSet.next();
fail("FAILED - result set should be closed");
} catch (SQLException sqle) {
assertSQLState("XCL16", sqle);
}
}
lastResultSet = drs2.getResultSet();
if ((pass == 1) || (pass == 2)) {
assertNotNull("expected resultset pass " + pass, lastResultSet);
} else if (pass == 3) {
assertNull(lastResultSet);
}
pass++;
} while (drs2.getMoreResults() || lastResultSet != null);
checkCommitWithMultipleResultSets(drs2, conn2, "autocommit");
checkCommitWithMultipleResultSets(drs2, conn2, "noautocommit");
checkCommitWithMultipleResultSets(drs2, conn2, "statement");
drs2.close();
// use escape syntax
drs2 = conn.prepareCall("{call DRS2(?, ?)}");
drs2.setInt(1, 2);
drs2.setInt(2, 6);
drs2.execute();
rs = drs2.getResultSet();
String[][] expectedRows = { { "2", "int" } };
JDBC.assertFullResultSet(rs, expectedRows);
drs2.close();
// check that a procedure with dynamic result sets can not resolve to a
// method with no ResultSet argument.
s.execute(
"create procedure irdrs(p1 int) dynamic result sets 1 " +
"language java parameter style java external name '" +
thisClassName + ".missingDynamicParameter'");
assertCallError("42X50", "call IRDRS(?)");
s.execute("drop procedure irdrs");
// check that a procedure with dynamic result sets can not resolve to a
// method with an argument that is a ResultSet impl,
s.execute(
"create procedure rsi(p1 int) dynamic result sets 1 " +
"language java parameter style java external name " +
"'org.apache.derbyTesting.functionTests.util." +
"ProcedureTest.badDynamicParameter'");
assertCallError("42X50", "call rsi(?)");
s.execute("drop procedure rsi");
// simple check for a no-arg method that has dynamic result sets but
// does not return any
// System.out.println("no dynamic result sets");
s.execute(
"create procedure zadrs() dynamic result sets 4 " +
"language java parameter style java external name '" +
thisClassName + ".zeroArgDynamicResult'");
CallableStatement zadrs = conn.prepareCall("call ZADRS()");
zadrs.execute();
// DERBY-211
if (usingEmbedded()) {
assertEquals(0, zadrs.getUpdateCount());
} else {
assertEquals(-1, zadrs.getUpdateCount());
}
zadrs.close();
s.execute("drop procedure ZADRS");
// return too many result sets
s.execute(
"create procedure way.toomany(p1 int, p2 int) reads sql data " +
"dynamic result sets 1 language java parameter style java " +
"external name '" +
thisClassName + ".selectRows'");
CallableStatement toomany = conn.prepareCall("call way.toomany(?, ?)");
toomany.setInt(1, 2);
toomany.setInt(2, 6);
toomany.execute();
SQLWarning warn = toomany.getWarnings();
// DERBY-159. Network Server does not get warning
if (usingEmbedded()) {
assertNotNull(warn);
assertEquals("0100E", warn.getSQLState());
}
rs = toomany.getResultSet();
JDBC.assertFullResultSet(rs, new String[][] { { "2", "int" } });
JDBC.assertNoMoreResults(toomany);
toomany.setInt(1, 2);
toomany.setInt(2, 99); // will close the second result set.
toomany.execute();
rs = toomany.getResultSet();
// Single result set returned, therefore no warnings.
JDBC.assertNoWarnings(toomany.getWarnings());
JDBC.assertFullResultSet(rs, new String[][] { { "2", "int" } });
JDBC.assertNoMoreResults(toomany);
toomany.close();
s.execute("drop procedure way.toomany");
s.execute("drop schema way restrict");
// Run following test in embedded only until DERBY-3414 is fixed. As
// identified in DERBY-3414, the rollback inside the java procedure
// is not closing all the resultsets when run in network server mode.
if (usingEmbedded()) {
boolean oldAutoCommit = conn.getAutoCommit();
s.execute(
"create table dellater1(i int not null primary key, " +
" b char(15))");
s.executeUpdate(
"insert into dellater1 " +
" values(1,'a'),(2,'b'),(3,'c'),(4,'d')");
s.executeUpdate("create table dellater2(c11 int)");
s.executeUpdate("insert into dellater2 values(1),(2),(3),(4)");
conn.setAutoCommit(false);
ResultSet rs1 = s.executeQuery("select * from dellater2");
rs1.next();
Statement s1 =
conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY,
ResultSet.HOLD_CURSORS_OVER_COMMIT);
ResultSet resultSet = s1.executeQuery("values (1, 2), (3, 4)");
resultSet.next();
s.execute(
"create procedure procWithRollback(p1 int) " +
"parameter style java reads sql data dynamic result sets 1 " +
"language java external name '" +
thisClassName + ".rollbackInsideProc'");
drs1 = prepareCall("call procWithRollback(3)");
drs1.execute();
// Following shows that the rollback inside the java procedure will
// cause procedure to return no resultset (A procedure does
// not return closed resultsets). In 10.2 codeline though, java
// procedure returns a closed resultset if there is a rollback
// inside the java procedure.
JDBC.assertNoMoreResults(drs1);
JDBC.assertClosed(rs1);
JDBC.assertClosed(resultSet);
// Following shows that the rollback inside the java procedure will
// only close the resultset created before the rollback. The
// resultset created after the rollback will remain open and if it
// is a resultset returned through the procedure then it will be
// available to the caller of the procedure. Notice that even though
// the procedure is defined to 2 return dynamic resultsets, only one
// is returned because the other one was closed as a result of
// rollback.
s.execute(
"create procedure procWithRollbackAnd2Resulsets"+
"(p1 int) parameter style java reads sql data dynamic "+
"result sets 2 language java external name "+
"'" + thisClassName + ".rollbackInsideProcWith2ResultSets'");
drs1 = prepareCall("call procWithRollbackAnd2Resulsets(3)");
drs1.execute();
rs = drs1.getResultSet();
JDBC.assertDrainResults(rs);
JDBC.assertNoMoreResults(drs1);
// Create a procedure which does an insert into a table. Then call
// it with parameters such that insert will fail because of
// duplicate key. The procedure also has couple select statements
// The exception thrown for duplicate key should close the
// dynamic result sets in the procedure, and we should be
// able to drop the tables used in the select queries without
// running into locking issues.
s.execute(
"create procedure insertCausingRollback"+
"(p1 int, p2 char(20)) modifies sql data "+
"dynamic result sets 1 language java external name '" +
thisClassName + ".insertCausingRollback' "+
"parameter style java");
s.executeUpdate("create table DELLATER3(c31 int)");
s.executeUpdate("insert into DELLATER3 values(1),(2),(3),(4)");
conn.commit();
drs1 = prepareCall("call insertCausingRollback(3,'3')");
assertStatementError("23505",drs1);
JDBC.assertNoMoreResults(drs1);
s.execute("drop table DELLATER1");
s.execute("drop table DELLATER2");
s.execute("drop table DELLATER3");
s.execute("drop procedure insertCausingRollback");
conn.setAutoCommit(oldAutoCommit);
}
s.execute("drop procedure drs");
s.execute("drop procedure drs2");
s.execute("drop table t1");
commit();
conn2.close();
}
public void testResultSetsWithLobs() throws SQLException {
Connection conn = getConnection();
// Create objects.
Statement s = createStatement();
// Clob.
s.execute("create table lobCheckOne (c clob(30))");
s.execute("insert into lobCheckOne values (cast " +
"('yayorsomething' as clob(30)))");
s.execute("insert into lobCheckOne values (cast " +
"('yayorsomething2' as clob(30)))");
s.execute(
"create procedure clobproc () parameter style java " +
"language java dynamic result sets 3 reads sql data " +
"external name " +
"'org.apache.derbyTesting.functionTests.util." +
"ProcedureTest.clobselect'");
// Blob.
s.execute("create table lobCheckTwo (b blob(30))");
s.execute("insert into lobCheckTwo values (cast " + "(" +
TestUtil.stringToHexLiteral("101010001101") +
" as blob(30)))");
s.execute("insert into lobCheckTwo values (cast " +
"(" +
TestUtil.stringToHexLiteral("101010001101") +
" as blob(30)))");
s.execute("create procedure blobproc () parameter style java " +
"language java external name " +
"'org.apache.derbyTesting.functionTests.util." +
"ProcedureTest.blobselect' " +
"dynamic result sets 1 reads sql data");
// Clobs.
CallableStatement cs = conn.prepareCall("call clobproc()");
cs.execute();
ResultSet rs = cs.getResultSet();
JDBC.assertFullResultSet(rs,
new String[][] {{"yayorsomething"},{"yayorsomething2"}});
cs.close();
// Blobs.
cs = conn.prepareCall("call blobproc()");
cs.execute();
rs = cs.getResultSet();
String [][] expectedRows =
{{"003100300031003000310030003000300031003100300031"},
{"003100300031003000310030003000300031003100300031"}};
JDBC.assertFullResultSet(rs, expectedRows);
cs.close();
// Clean up.
s.execute("drop table lobCheckOne");
s.execute("drop table lobCheckTwo");
s.execute("drop procedure clobproc");
s.execute("drop procedure blobproc");
s.close();
}
/**
* Original harness results transcribed here:
-auto commit is true
-lock count before execution 0
-lock count after execution 1
-lock count after next on first rs 3 -> 2 now FIXME: explain
-lock count after first getMoreResults() 2
-lock count after next on second rs 7
-lock count after second getMoreResults() 0
-auto commit is false
-lock count before execution 0
-lock count after execution 1
-lock count after next on first rs 3 -> 2 now FIXME: explain
-lock count after first getMoreResults() 2
-lock count after next on second rs 7
-lock count after second getMoreResults() 7
-auto commit is true
-lock count before execution 0
-lock count after execution 1
-lock count after next on first rs 3 -> 2 now FIXME: explain
-executing statement to force auto commit on open call statement
-lock count after statement execution 0
-lock count after first getMoreResults() 0
-lock count after next on second rs 0
-lock count after second getMoreResults() 0
*/
private static void checkCommitWithMultipleResultSets(
CallableStatement drs1, Connection conn2, String action)
throws SQLException {
Connection conn = drs1.getConnection();
// Do not run with client until DERBY-2510 is fixed
if (usingDerbyNetClient()) {
return;
}
try {
conn.setHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT);
} catch (Exception e) {
fail("shouldn't get that error " + e.getMessage());
}
// check to see that the commit of the transaction happens at the
// correct time.
// switch isolation levels to keep the locks around.
int oldIsolation = conn.getTransactionIsolation();
boolean oldAutoCommit = conn.getAutoCommit();
if (action.equals("noautocommit")) {
conn.setAutoCommit(false);
} else {
conn.setAutoCommit(true);
}
conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
if (action.equals("noautocommit")) {
assertFalse(conn.getAutoCommit());
} else {
assertTrue(conn.getAutoCommit());
}
PreparedStatement psLocks = conn2
.prepareStatement("select count(*) from SYSCS_DIAG.LOCK_TABLE AS LT");
ResultSet lrs = psLocks.executeQuery();
// lock count before execution
JDBC.assertFullResultSet(lrs, new String[][] { { "0" } });
drs1.execute();
// lock count after execution
lrs = psLocks.executeQuery();
JDBC.assertFullResultSet(lrs, new String[][] { { "1" } });
ResultSet rs = drs1.getResultSet();
rs.next();
// lock count after next on first rs
lrs = psLocks.executeQuery();
JDBC.assertFullResultSet(lrs, new String[][] { { "2" } });
boolean expectClosed = false;
// execute another statement to ensure that the result sets close.
if (action.equals("statement")) {
// executing statement to force auto commit on open call statement")
conn.createStatement().executeQuery("values 1").next();
expectClosed = true;
lrs = psLocks.executeQuery();
JDBC.assertFullResultSet(lrs, new String[][] { { "0" } });
try {
rs.next();
if (action.equals("autocommit")) {
fail("FAIL - result set open in auto commit mode after " +
"another statement execution");
}
} catch (SQLException sqle) {
assertSQLState("XCL16", sqle);
}
}
boolean anyMore = drs1.getMoreResults();
assertTrue("is there a second result", anyMore);
lrs = psLocks.executeQuery();
if (action.equals("statement")) {
JDBC.assertFullResultSet(lrs, new String[][] { { "0" } });
} else {
JDBC.assertFullResultSet(lrs, new String[][] { { "2" } });
}
if (anyMore) {
rs = drs1.getResultSet();
try {
rs.next();
if (expectClosed && !usingDerbyNetClient()) {
fail("FAIL - result set open in auto commit mode " +
"after another statement execution");
}
} catch (SQLException sqle) {
if (expectClosed) {
assertSQLState("XCL16", sqle);
} else {
throw sqle;
}
}
// lock count after next on second rs
lrs = psLocks.executeQuery();
if (action.equals("statement")) {
JDBC.assertFullResultSet(lrs, new String[][] { { "0" } });
} else {
JDBC.assertFullResultSet(lrs, new String[][] { { "7" } });
}
// should commit here since all results are closed
boolean more = drs1.getMoreResults();
assertFalse("more results (should be false) ", more);
lrs = psLocks.executeQuery();
if (action.equals("autocommit") || action.equals("statement")) {
JDBC.assertFullResultSet(lrs, new String[][] { { "0" } });
}else {
JDBC.assertFullResultSet(lrs, new String[][] { { "7" } });
}
conn.setTransactionIsolation(oldIsolation);
conn.setAutoCommit(oldAutoCommit);
}
psLocks.close();
}
private void assertUpdateCountForProcedureWithNoResults(Statement s)
throws SQLException {
// DERBY-211 Network Server returns no result sets for a procedure call
// that returns no result
if (usingEmbedded()) {
assertEquals(0, s.getUpdateCount());
} else {
assertEquals(-1, s.getUpdateCount());
}
}
static String TYPE(short type) {
switch (type) {
case DatabaseMetaData.procedureResultUnknown:
return "procedureResultUnknown";
case DatabaseMetaData.procedureNoResult:
return "procedureNoResult";
case DatabaseMetaData.procedureReturnsResult:
return "procedureReturnsResult";
default:
return "??????";
}
}
static String PARAMTYPE(short type) {
switch (type) {
case DatabaseMetaData.procedureColumnUnknown:
return "procedureColumnUnknown";
case DatabaseMetaData.procedureColumnIn:
return "procedureColumnIn";
case DatabaseMetaData.procedureColumnInOut:
return "procedureColumnInOut";
case DatabaseMetaData.procedureColumnOut:
return "procedureColumnOut";
case DatabaseMetaData.procedureColumnReturn:
return "procedureColumnReturn";
case DatabaseMetaData.procedureColumnResult:
return "procedureColumnResult";
default:
return "???";
}
}
// PROCEDURES for ambiguous testing
public static void ambiguous1(int p1, String p2, ResultSet[] data1,
ResultSet[] data2) {
}
public static void ambiguous1(int p1, String p2, ResultSet[] data1) {
}
public static void ambiguous2(int p1, Integer p2) {
// System.out.println("ambiguous2(int,Integer) called");
};
public static void ambiguous2(Integer p1, int p2) {
// System.out.println("ambiguous2(Integer,int) called");
};
// PROCEDURES for zeroArgProcedures
public static void zeroArg() {
// System.out.println("zeroArg() called");
}
// PROCEDURES for sqlProcedures
public static void insertRow(int p1) throws SQLException {
insertRow(p1, "int");
}
public static void insertRow(int p1, String p2) throws SQLException {
Connection conn = DriverManager
.getConnection("jdbc:default:connection");
PreparedStatement ps = conn
.prepareStatement("insert into t1 values (?, ?)");
ps.setInt(1, p1);
ps.setString(2, p2);
ps.executeUpdate();
ps.close();
conn.close();
}
// PROCEDURES for dynamic results
public static void selectRows(int p1, ResultSet[] data) throws SQLException {
Connection conn = DriverManager
.getConnection("jdbc:default:connection");
PreparedStatement ps = conn
.prepareStatement("select * from t1 where i = ?");
ps.setInt(1, p1);
data[0] = ps.executeQuery();
conn.close();
}
/**
* A test case for DERBY-3304. An explicit rollback inside the procedure
* should close all the resultsets created before the call to the
* procedure and any resultsets created inside the procedure including
* the dynamic resultsets.
*
* @param p1
* @param data
* @throws SQLException
*/
public static void rollbackInsideProc(int p1, ResultSet[] data)
throws SQLException {
Connection conn = DriverManager.getConnection(
"jdbc:default:connection");
PreparedStatement ps = conn.prepareStatement(
"select * from dellater1 where i = ?");
ps.setInt(1, p1);
data[0] = ps.executeQuery();
conn.rollback();
conn.close();
}
/**
* A test case for DERBY-3304. An explicit rollback inside the procedure
* should close all the resultsets created before the call to the
* procedure and any resultsets created inside the procedure including
* the dynamic resultsets. But the resultset created after the rollback
* should stay open
*
* @param p1
* @param data1
* @param data2
* @throws SQLException
*/
public static void rollbackInsideProcWith2ResultSets(int p1,
ResultSet[] data1,
ResultSet[] data2)
throws SQLException {
Connection conn = DriverManager.getConnection(
"jdbc:default:connection");
PreparedStatement ps = conn.prepareStatement(
"select * from t1 where i = ?");
ps.setInt(1, p1);
data1[0] = ps.executeQuery();
conn.rollback();
ps = conn.prepareStatement(
"select * from dellater1 where i = ?");
ps.setInt(1, p1);
data2[0] = ps.executeQuery();
conn.close();
}
/**
* A test case for DERBY-3304. The procedure is attempting to insert a
* duplicate key into a table which causes an internal rollback (vs a
* user-initiated rollback). This internal rollback should close the
* internal CallStatementResultSet associated with the Java procedure
* and the dynamic result set.
*
* @param p1
* @param p2
* @param data
* @throws SQLException
*/
public static void insertCausingRollback(int p1, String p2, ResultSet[] data) throws SQLException {
Connection conn = DriverManager
.getConnection("jdbc:default:connection");
// The resultset created here is a dynamic resultset and will be
// available to the caller of the java procedure (provided that there
// is no SQL exception thrown inside of this procedure. An exception
// will cause Derby to close this resultset).
PreparedStatement ps = conn.prepareStatement(
"select * from dellater2 where c11 = ?");
ps.setInt(1, p1);
data[0] = ps.executeQuery();
// The resultset created here has the lifetime of this procedure
// and is not available to the caller of the procedure.
PreparedStatement ps1 = conn.prepareStatement(
"select * from dellater3 where c31 = ?");
ps1.setInt(1, p1);
ResultSet rs = ps1.executeQuery();
// DERBY-6038: When the procedure fails because of duplicate key
// exception below, all dynamic results set will be closed. Other
// open result sets will stay open until they have been garbage
// collected and finalized. Their staying open may cause problems
// later in the test, so close non-dynamic result sets before
// returning.
rs.close();
// Depending on the value of p1, following may throw duplicate key
// exception. If that happens, both the dynamic resultset and local
// resultset created above will get closed and locks held by them
// and insert statement will be released
PreparedStatement ps2 = conn
.prepareStatement("insert into dellater1 values (?, ?)");
ps2.setInt(1, p1);
ps2.setString(2, p2);
ps2.executeUpdate();
ps2.close();
conn.close();
}
public static void selectRows(int p1, int p2, ResultSet[] data1,
ResultSet[] data2) throws SQLException {
// selectRows - 2 arg - 2 rs
Connection conn = DriverManager
.getConnection("jdbc:default:connection");
PreparedStatement ps = conn
.prepareStatement("select * from t1 where i = ?");
ps.setInt(1, p1);
data1[0] = ps.executeQuery();
ps = conn.prepareStatement("select * from t1 where i >= ?");
ps.setInt(1, p2);
data2[0] = ps.executeQuery();
if (p2 == 99) {
data2[0].close();
}
// return no results
if (p2 == 199) {
data1[0].close();
data1[0] = null;
data2[0].close();
data2[0] = null;
}
// swap results
if (p2 == 299) {
ResultSet rs = data1[0];
data1[0] = data2[0];
data2[0] = rs;
}
conn.close();
}
// select all rows from a table
public static void selectRows(String table, ResultSet[] rs)
throws SQLException {
Connection conn = DriverManager
.getConnection("jdbc:default:connection");
Statement stmt = conn.createStatement();
rs[0] = stmt.executeQuery("select * FROM " + table);
conn.close();
}
public void testParameterTypes() throws SQLException {
Connection conn = getConnection();
Statement s = createStatement();
s.execute("create table PT1(A integer not null primary key, " +
" B char(10), C varchar(20))");
s.execute(
"create procedure PT1(in a int, in b char(10), c varchar(20)) " +
"parameter style java dynamic result sets 1 language java " +
"modifies sql data " +
"external name '" + thisClassName + ".parameter1'");
String[] sysaliasDefinition = {
"APP.PT1 AS " + thisClassName + ".parameter1" +
"(IN \"A\" INTEGER,IN \"B\" CHAR(10),IN \"C\" VARCHAR(20)) " +
"LANGUAGE JAVA PARAMETER STYLE JAVA " +
"MODIFIES SQL DATA DYNAMIC RESULT SETS 1" };
String[] dbMetadataDefinition = {
"APP.PT1 AS " + thisClassName + ".parameter1 " +
"type procedureNoResult" };
String[] columnDefinition = {
"procedureColumnIn A INTEGER",
"procedureColumnIn B CHAR", "procedureColumnIn C VARCHAR" };
checkMatchingProcedures(conn, "PT1", sysaliasDefinition,
dbMetadataDefinition, columnDefinition);
CallableStatement pt1 = conn.prepareCall("call PT1(?, ?, ?)");
pt1.setInt(1, 20);
pt1.setString(2, "abc");
pt1.setString(3, "efgh");
pt1.execute();
JDBC.assertFullResultSet(pt1.getResultSet(), new String[][] { { "20",
"abc", "10", "efgh", "4" } });
pt1.setInt(1, 30);
pt1.setString(2, "abc ");
pt1.setString(3, "efgh ");
pt1.execute();
JDBC.assertFullResultSet(pt1.getResultSet(), new String[][] { { "30",
"abc", "10", "efgh", "6" } });
pt1.setInt(1, 40);
// end blanks truncation of arguments
char[] c75 = new char[75]; Arrays.fill(c75, ' ');
char[] c77 = new char[77]; Arrays.fill(c77, ' ');
pt1.setString(2, "abc" + new String(c75));
pt1.setString(3, "efgh" + new String(c77));
pt1.execute();
JDBC.assertFullResultSet(pt1.getResultSet(), new String[][] { { "40",
"abc", "10", "efgh", "20" } });
pt1.setInt(1, 50);
pt1.setString(2, "0123456789X");
pt1.setString(3, "efgh ");
assertPreparedStatementError(LANG_STRING_TRUNCATION, pt1);
pt1.setInt(1, 50);
pt1.setString(2, "0123456789");
pt1.setString(3, "efgh ");
pt1.execute();
JDBC.assertFullResultSet(pt1.getResultSet(), new String[][] { { "50",
"0123456789", "10", "efgh", "6" } });
pt1.close();
s.execute("drop procedure PT1");
s.execute(
"create procedure PT2(in a int, in b DECIMAL(4), c DECIMAL(7,3)) " +
"parameter style java dynamic result sets 1 language java " +
"modifies sql data " +
"external name '" + thisClassName + ".parameter2'");
sysaliasDefinition = new String[] {
"APP.PT2 AS " + thisClassName + ".parameter2" +
"(IN \"A\" INTEGER,IN \"B\" DECIMAL(4,0),IN \"C\" DECIMAL(7,3)) " +
"LANGUAGE JAVA PARAMETER STYLE JAVA " +
"MODIFIES SQL DATA DYNAMIC RESULT SETS 1" };
dbMetadataDefinition = new String[] {
"APP.PT2 AS " +
thisClassName + ".parameter2 type procedureNoResult" };
columnDefinition = new String[] { "procedureColumnIn A INTEGER",
"procedureColumnIn B DECIMAL", "procedureColumnIn C DECIMAL" };
checkMatchingProcedures(conn, "PT2", sysaliasDefinition,
dbMetadataDefinition, columnDefinition);
CallableStatement pt2 = conn.prepareCall("call PT2(?, ?, ?)");
pt2.setInt(1, 60);
pt2.setString(2, "34");
pt2.setString(3, "54.1");
boolean firstIsResultSet = pt2.execute();
ResultSet rs = pt2.getResultSet();
JDBC.assertFullResultSet(rs,
new String[][] { { "60", "34", "54.100" } });
pt2.setInt(1, 70);
pt2.setBigDecimal(2, new BigDecimal("831"));
pt2.setBigDecimal(3, new BigDecimal("45.7"));
pt2.execute();
rs = pt2.getResultSet();
JDBC.assertFullResultSet(rs,
new String[][] { { "70", "831", "45.700" } });
pt2.setInt(1, -1);
pt2.setBigDecimal(2, new BigDecimal("10243"));
pt2.setBigDecimal(3, null);
try {
pt2.execute();
fail("FAIL - too many digits in decimal value accepted");
} catch (SQLException sqle) {
assertSQLState("22003", sqle);
}
pt2.setInt(1, 80);
pt2.setBigDecimal(2, new BigDecimal("993"));
pt2.setBigDecimal(3, new BigDecimal("1234.5678"));
pt2.execute();
rs = pt2.getResultSet();
JDBC.assertFullResultSet(rs,
new String[][] { { "80", "993", "1234.567" } });
pt2.close();
s.execute("drop procedure PT2");
s.execute(
"create procedure PTSMALLINT2(" +
" in p_in SMALLINT, " +
" inout p_inout SMALLINT, " +
" out p_out SMALLINT) " +
"parameter style java dynamic result sets 0 language java " +
"no sql " +
"external name '" + thisClassName + ".pSMALLINT'");
sysaliasDefinition = new String[] {
"APP.PTSMALLINT2 AS " + thisClassName + ".pSMALLINT" +
"(IN \"P_IN\" SMALLINT," +
"INOUT \"P_INOUT\" SMALLINT," +
"OUT \"P_OUT\" SMALLINT) " +
"LANGUAGE JAVA PARAMETER STYLE JAVA NO SQL" };
dbMetadataDefinition = new String[] {
"APP.PTSMALLINT2 AS " +
thisClassName + ".pSMALLINT type procedureNoResult" };
columnDefinition = new String[] { "procedureColumnIn P_IN SMALLINT",
"procedureColumnInOut P_INOUT SMALLINT",
"procedureColumnOut P_OUT SMALLINT" };
checkMatchingProcedures(conn, "PT2", sysaliasDefinition,
dbMetadataDefinition, columnDefinition);
CallableStatement ptsi = conn.prepareCall("call PTSMALLINT2(?, ?, ?)");
ptsi.registerOutParameter(2, Types.SMALLINT);
ptsi.registerOutParameter(3, Types.SMALLINT);
ptsi.setNull(1, Types.SMALLINT);
ptsi.setShort(2, (short) 7);
try {
ptsi.execute();
fail("FAIL NULL PASSED to primitive");
} catch (SQLException sqle) {
assertSQLState("39004", sqle);
}
ptsi.setShort(1, (short) 4);
ptsi.setNull(2, Types.SMALLINT);
try {
ptsi.execute();
fail("FAIL NULL PASSED to primitive");
} catch (SQLException sqle) {
assertSQLState("39004", sqle);
}
ptsi.setShort(1, (short) 6);
ptsi.setShort(2, (short) 3);
ptsi.execute();
assertEquals("wrong value for p_inout", "9", ptsi.getObject(2)
.toString());
assertEquals("wrong value for p_out", "6", ptsi.getObject(3).toString());
ptsi.setShort(2, (short) 3);
ptsi.execute();
assertEquals("wrong value for p_inout", "9", ptsi.getObject(2)
.toString());
assertEquals("wrong value for p_out", "6", ptsi.getObject(3).toString());
// with setObject . Beetle 5439
ptsi.setObject(1, 6);
ptsi.setObject(2, 3);
ptsi.execute();
assertEquals("wrong value for p_inout", "9", ptsi.getObject(2)
.toString());
assertFalse(ptsi.wasNull());
assertEquals("wrong value for p_out", "6", ptsi.getObject(3).toString());
assertFalse(ptsi.wasNull());
ptsi.close();
s.execute("drop procedure PTSMALLINT2");
s.execute("drop table PT1");
s.close();
}
public void testOutparams() throws SQLException {
Connection conn = getConnection();
Statement s = createStatement();
s.execute("create procedure OP1(out a int, in b int) " +
"parameter style java language java " +
"external name '" + thisClassName + ".outparams1'");
String[] sysaliasDefinition = {
"APP.OP1 AS " + thisClassName + ".outparams1" +
"(OUT \"A\" INTEGER,IN \"B\" INTEGER) " +
"LANGUAGE JAVA PARAMETER STYLE JAVA MODIFIES SQL DATA" };
String[] dbMetadataDefinition = {
"APP.OP1 AS " + thisClassName + ".outparams1 " +
"type procedureNoResult" };
String[] columnDefinition = { "procedureColumnOut A INTEGER",
"procedureColumnIn B INTEGER" };
checkMatchingProcedures(conn, "OP1", sysaliasDefinition,
dbMetadataDefinition, columnDefinition);
// check execute via a Statement fails for use of out parameter
try {
s.execute("call OP1(?, ?)");
fail("FAIL execute succeeded on out param with Statement");
} catch (SQLException sqle) {
String expectedSQLState = "XJ009";
if (usingDerbyNetClient()) {
expectedSQLState = "07004";
}
assertSQLState(expectedSQLState, sqle);
}
// check execute via a PreparedStatement fails for use of out parameter
// DERBY-2512 Network client allows prepare of a stored procedure with
// an output parameter using a PreparedStatement
if (usingEmbedded()) {
try {
prepareStatement("call OP1(?, ?)");
fail("FAIL prepare succeeded on out param " +
"with PreparedStatement");
} catch (SQLException sqle) {
String expectedSQLState = "XJ009";
assertSQLState(expectedSQLState, sqle);
}
}
CallableStatement op = prepareCall("call OP1(?, ?)");
op.registerOutParameter(1, Types.INTEGER);
op.setInt(2, 7);
op.execute();
assertEquals(14, op.getInt(1));
assertFalse(op.wasNull());
op.close();
s.execute("create procedure OP2(inout a int, in b int) " +
"parameter style java language java " +
"external name '" + thisClassName + ".inoutparams2'");
sysaliasDefinition = new String[] {
"APP.OP2 AS " + thisClassName + ".inoutparams2" +
"(INOUT \"A\" INTEGER,IN \"B\" INTEGER) " +
"LANGUAGE JAVA PARAMETER STYLE JAVA MODIFIES SQL DATA" };
dbMetadataDefinition = new String[] {
"APP.OP2 AS " +
thisClassName + ".inoutparams2 type procedureNoResult"};
columnDefinition = new String[] { "procedureColumnInOut A INTEGER",
"procedureColumnIn B INTEGER" };
checkMatchingProcedures(conn, "OP2", sysaliasDefinition,
dbMetadataDefinition, columnDefinition);
try {
s.execute("call OP2(?,?)");
fail("FAIL execute succeeded on inout param with Statement");
} catch (SQLException sqle) {
String expectedSQLState = "XJ009";
if (usingDerbyNetClient()) {
expectedSQLState = "07004";
}
assertSQLState(expectedSQLState, sqle);
}
if (!usingDerbyNetClient()) { // bug DERBY-2512
// check execute via a PreparedStatement fails for use of inout
// parameter
try {
prepareStatement("call OP2(?, ?)");
fail("FAIL prepare succeeded on inout param " +
"with PreparedStatement");
} catch (SQLException sqle) {
String expectedSQLState = "XJ009";
assertSQLState(expectedSQLState, sqle);
}
}
op = prepareCall("call OP2(?, ?)");
op.registerOutParameter(1, Types.INTEGER);
op.setInt(1, 3);
op.setInt(2, 7);
op.execute();
assertEquals(17, op.getInt(1));
assertFalse(op.wasNull());
op.close();
// inout & out procedures with variable length
s.execute(
"create procedure OP3(inout a char(10), in b int) " +
"parameter style java language java " +
"external name '" + thisClassName + ".inoutparams3'");
op = prepareCall("call OP3(?, ?)");
op.registerOutParameter(1, Types.CHAR);
op.setString(1, "dan");
op.setInt(2, 8);
op.execute();
assertEquals("nad ", op.getString(1));
assertFalse(op.wasNull());
op.close();
// inout & out DECIMAL procedures with variable length
s.execute(
"create procedure OP4(out a DECIMAL(4,2), in b varchar(255)) " +
"parameter style java language java " +
"external name '" + thisClassName + ".inoutparams4'");
sysaliasDefinition = new String[] {
"APP.OP4 AS " + thisClassName + ".inoutparams4" +
"(OUT \"A\" DECIMAL(4,2),IN \"B\" VARCHAR(255)) " +
"LANGUAGE JAVA PARAMETER STYLE JAVA MODIFIES SQL DATA" };
dbMetadataDefinition = new String[] {
"APP.OP4 AS " +
thisClassName + ".inoutparams4 type procedureNoResult"};
columnDefinition = new String[] { "procedureColumnOut A DECIMAL",
"procedureColumnIn B VARCHAR", };
checkMatchingProcedures(conn, "OP4", sysaliasDefinition,
dbMetadataDefinition, columnDefinition);
op = prepareCall("call OP4(?, ?)");
op.registerOutParameter(1, Types.DECIMAL);
op.setString(2, null);
op.execute();
assertNull(op.getBigDecimal(1));
assertTrue(op.wasNull());
op.setString(2, "14");
op.execute();
assertEquals("31.00", op.getBigDecimal(1).toString());
assertFalse(op.wasNull());
op.setString(2, "11.3");
op.execute();
assertEquals("28.30", op.getBigDecimal(1).toString());
assertFalse(op.wasNull());
op.setString(2, "39.345");
op.execute();
assertEquals("56.34", op.getBigDecimal(1).toString());
assertFalse(op.wasNull());
op.setString(2, "83");
try {
op.execute();
fail("FAIL - execution ok on out of range out parameter");
} catch (SQLException sqle) {
assertSQLState("22003", sqle);
}
op.clearParameters();
try {
// b not set
op.execute();
fail("FAIL - b not set");
} catch (SQLException sqle) {
assertSQLState("07000", sqle);
}
// DERBY-2513 Network client allows out parameter to be set
if (usingEmbedded()) {
// try to set an out param
try {
op.setBigDecimal(1, new BigDecimal("22.32"));
fail("FAIL - set out param to value");
} catch (SQLException sqle) {
assertSQLState("XCL27", sqle);
}
try {
op.setBigDecimal(1, null);
fail("FAIL - set out param to null");
} catch (SQLException sqle) {
assertSQLState("XCL27", sqle);
}
try {
op.setNull(1, Types.DECIMAL);
fail("FAIL - set out param to null");
} catch (SQLException sqle) {
assertSQLState("XCL27", sqle);
}
}
// can we get an in param?
op.setString(2, "49.345");
op.execute();
assertEquals("66.34", op.getBigDecimal(1).toString());
assertFalse(op.wasNull());
try {
op.getString(2);
fail("FAIL OP4 GET 49.345 >" + op.getString(2) + "< null ? "
+ op.wasNull());
} catch (SQLException sqle) {
if (usingDerbyNetClient()) {
assertSQLState("XJ091", sqle);
} else {
assertSQLState("XCL26", sqle);
}
}
op.close();
// check to see that a registration is required first for the out
// parameter.
op = conn.prepareCall("call OP4(?, ?)");
op.setString(2, "14");
try {
op.execute();
fail("FAIL - execute succeeded without registration of out parameter");
} catch (SQLException sqle) {
if (usingEmbedded()) {
assertSQLState("07004", sqle);
} else {
assertSQLState("07000", sqle);
}
}
op.close();
s.execute(
"create procedure OP4INOUT(" +
" inout a DECIMAL(4,2), in b varchar(255)) " +
"parameter style java language java " +
"external name '" + thisClassName + ".inoutparams4'");
op = conn.prepareCall("call OP4INOUT(?, ?)");
op.registerOutParameter(1, Types.DECIMAL);
op.setString(2, null);
op.setBigDecimal(1, null);
op.execute();
assertNull(op.getBigDecimal(1));
assertTrue(op.wasNull());
op.setBigDecimal(1, new BigDecimal("99"));
op.execute();
assertNull(op.getBigDecimal(1));
assertTrue(op.wasNull());
op.setString(2, "23.5");
op.setBigDecimal(1, new BigDecimal("14"));
op.execute();
assertEquals("37.50", op.getBigDecimal(1).toString());
op.setString(2, "23.505");
op.setBigDecimal(1, new BigDecimal("9"));
op.execute();
assertEquals("32.50", op.getBigDecimal(1).toString());
op.execute();
assertEquals("56.00", op.getBigDecimal(1).toString());
assertFalse(op.wasNull());
op.setString(2, "67.99");
op.setBigDecimal(1, new BigDecimal("32.01"));
try {
op.execute();
fail("FAIL OP4INOUT 32.01+67.99 >" + op.getBigDecimal(1)
+ "< null ? " + op.wasNull());
} catch (SQLException sqle) {
assertSQLState("22003", sqle);
}
op.setString(2, "1");
op.setBigDecimal(1, new BigDecimal("102.33"));
try {
op.execute();
fail("FAIL OP4INOUT 1+102.33 >" + op.getBigDecimal(1) + "< null ? "
+ op.wasNull());
} catch (SQLException sqle) {
assertSQLState("22003", sqle);
}
// now some checks to requirements for parameter setting.
op.clearParameters();
try {
// a,b not set
op.execute();
fail("FAIL - a,b not set");
} catch (SQLException sqle) {
assertSQLState("07000", sqle);
}
op.clearParameters();
op.setString(2, "2");
try {
// a not set
op.execute();
fail("FAIL - a not set");
} catch (SQLException sqle) {
assertSQLState("07000", sqle);
}
op.clearParameters();
op.setBigDecimal(1, new BigDecimal("33"));
try {
// b not set
op.execute();
fail("FAIL - b not set");
} catch (SQLException sqle) {
assertSQLState("07000", sqle);
}
op.close();
op = conn.prepareCall("call OP4INOUT(?, ?)");
op.setString(2, "14");
try {
op.execute();
fail("FAIL - execute succeeded without registration of " +
"inout parameter");
} catch (SQLException sqle) {
if (usingDerbyNetClient()) {
assertSQLState("07000", sqle);
} else {
assertSQLState("07004", sqle);
}
}
op.close();
s.execute("drop PROCEDURE OP1");
s.execute("drop PROCEDURE OP2");
s.execute("drop PROCEDURE OP3");
s.execute("drop PROCEDURE OP4");
s.execute("drop PROCEDURE OP4INOUT");
s.close();
}
public void testSQLControl() throws SQLException {
Connection conn = getConnection();
Statement s = createStatement();
s.execute("create schema SQLC");
s.execute("create table SQLC.SQLCONTROL_DML(I int)");
s.execute("insert into SQLC.SQLCONTROL_DML values 4");
String[] control = { "", "no sql", "contains sql", "reads sql data",
"modifies sql data" };
for (int i = 0; i < control.length; i++) {
StringBuilder cp = new StringBuilder(256);
cp.append("create procedure sqlc.sqlcontrol1_");
cp.append(i);
cp.append(
"(out e1 varchar(128), " +
" out e2 varchar(128), " +
" out e3 varchar(128), " +
" out e4 varchar(128), " +
" out e5 varchar(128), " +
" out e6 varchar(128), " +
" out e7 varchar(128)) ");
cp.append(control[i]);
cp.append(" parameter style java language java ");
cp.append("external name '");
cp.append(thisClassName);
cp.append(".sqlControl'");
String cpsql = cp.toString();
s.execute(cpsql);
cp.setLength(0);
cp.append("create procedure sqlc.sqlcontrol2_");
cp.append(i);
cp.append(
"(out e1 varchar(128)," +
" out e2 varchar(128)," +
" out e3 varchar(128)," +
" out e4 varchar(128)," +
" out e5 varchar(128)," +
" out e6 varchar(128)," +
" out e7 varchar(128)) ");
cp.append(control[i]);
cp.append(" parameter style java language java ");
cp.append("external name '");
cp.append(thisClassName);
cp.append(".sqlControl2'");
cpsql = cp.toString();
s.execute(cpsql);
cp.setLength(0);
cp.append("create procedure sqlc.sqlcontrol3_");
cp.append(i);
cp.append(
"(out e1 varchar(128)," +
" out e2 varchar(128)," +
" out e3 varchar(128)," +
" out e4 varchar(128)," +
" out e5 varchar(128)," +
" out e6 varchar(128)," +
" out e7 varchar(128)) ");
cp.append(control[i]);
cp.append(" parameter style java language java ");
cp.append("external name '");
cp.append(thisClassName);
cp.append(".sqlControl3'");
cpsql = cp.toString();
s.execute(cpsql);
cp.setLength(0);
cp.append("create procedure sqlc.sqlcontrol4_");
cp.append(i);
cp.append(
"(in sqlc integer," +
" out e1 varchar(128)," +
" out e2 varchar(128)," +
" out e3 varchar(128)," +
" out e4 varchar(128)," +
" out e5 varchar(128)," +
" out e6 varchar(128)," +
" out e7 varchar(128)," +
" out e8 varchar(128)) ");
cp.append(control[i]);
cp.append(" parameter style java language java ");
cp.append("external name '");
cp.append(thisClassName);
cp.append(".sqlControl4'");
cpsql = cp.toString();
s.execute(cpsql);
}
if (!conn.getAutoCommit()) {
conn.commit();
}
String[][] sqlControl_0 = /* sqlControl_0 */{
{ "CREATE TABLE SQ-UPDATE 0-EXECUTE OK",
"ALTER TABLE SQL-UPDATE 0-EXECUTE OK",
"INSERT INTO SQL-UPDATE 1-EXECUTE OK",
"UPDATE SQLC.SQL-UPDATE 2-EXECUTE OK",
"SELECT * FROM S- ROW(15)- ROW(12)-EXECUTE OK",
"DELETE FROM SQL-UPDATE 2-EXECUTE OK",
"DROP TABLE SQLC-UPDATE 0-EXECUTE OK" },
{ "CREATE VIEW SQL-42X05", "DROP VIEW SQLCO-X0X05",
"LOCK TABLE SQLC-42X05",
"VALUES 1,2,3- ROW(1)- ROW(2)- ROW(3)-EXECUTE OK",
"SET SCHEMA SQLC-UPDATE 0-EXECUTE OK",
"CREATE SCHEMA S-UPDATE 0-EXECUTE OK",
"DROP SCHEMA SQL-UPDATE 0-EXECUTE OK" },
{ "DERBY FEATURE", "DERBY FEATURE",
"SET ISOLATION C-UPDATE 0-EXECUTE OK",
"SET RUNTIMESTAT-42X01", "SET STATISTICS -42X01",
"VALUES 1- ROW(1)-EXECUTE OK",
"VALUES 1- ROW(1)-EXECUTE OK" } };
String[][] sqlControl_1 = /* sqlControl_1 */{
{ "CREATE TABLE SQ-38001", "ALTER TABLE SQL-42Y55",
"INSERT INTO SQL-42X05", "UPDATE SQLC.SQL-42X05",
"SELECT * FROM S-42X05", "DELETE FROM SQL-42X05",
"DROP TABLE SQLC-42Y55" },
{ "CREATE VIEW SQL-42X05", "DROP VIEW SQLCO-38001",
"LOCK TABLE SQLC-42X05", "VALUES 1,2,3-38001",
"SET SCHEMA SQLC-38001", "CREATE SCHEMA S-38001",
"DROP SCHEMA SQL-38001" },
{ "DERBY FEATURE", "DERBY FEATURE", "SET ISOLATION C-38001",
"SET RUNTIMESTAT-42X01", "SET STATISTICS -42X01",
"VALUES 1-38001", "VALUES 1-38001" } };
String[][] sqlControl_2 = /* sqlControl_2 */{
{ "CREATE TABLE SQ-38002", "ALTER TABLE SQL-42Y55",
"INSERT INTO SQL-42X05", "UPDATE SQLC.SQL-42X05",
"SELECT * FROM S-42X05", "DELETE FROM SQL-42X05",
"DROP TABLE SQLC-42Y55" },
{ "CREATE VIEW SQL-42X05", "DROP VIEW SQLCO-38002",
"LOCK TABLE SQLC-42X05", "VALUES 1,2,3-38004",
"SET SCHEMA SQLC-UPDATE 0-EXECUTE OK",
"CREATE SCHEMA S-38002", "DROP SCHEMA SQL-38002" },
{ "DERBY FEATURE", "DERBY FEATURE",
"SET ISOLATION C-UPDATE 0-EXECUTE OK",
"SET RUNTIMESTAT-42X01", "SET STATISTICS -42X01",
"VALUES 1-38004", "VALUES 1-38004" } };
String[][] sqlControl_3 = /* sqlControl_3 */{
{ "CREATE TABLE SQ-38002", "ALTER TABLE SQL-42Y55",
"INSERT INTO SQL-42X05", "UPDATE SQLC.SQL-42X05",
"SELECT * FROM S-42X05", "DELETE FROM SQL-42X05",
"DROP TABLE SQLC-42Y55" },
{ "CREATE VIEW SQL-42X05", "DROP VIEW SQLCO-38002",
"LOCK TABLE SQLC-42X05",
"VALUES 1,2,3- ROW(1)- ROW(2)- ROW(3)-EXECUTE OK",
"SET SCHEMA SQLC-UPDATE 0-EXECUTE OK",
"CREATE SCHEMA S-38002", "DROP SCHEMA SQL-38002" },
{ "DERBY FEATURE", "DERBY FEATURE",
"SET ISOLATION C-UPDATE 0-EXECUTE OK",
"SET RUNTIMESTAT-42X01", "SET STATISTICS -42X01",
"VALUES 1- ROW(1)-EXECUTE OK",
"VALUES 1- ROW(1)-EXECUTE OK" } };
String[][] sqlControl_4 =
/* sqlControl_4 */{
{ "CREATE TABLE SQ-UPDATE 0-EXECUTE OK",
"ALTER TABLE SQL-42Y55", "INSERT INTO SQL-42X05",
"UPDATE SQLC.SQL-42X05", "SELECT * FROM S-42X05",
"DELETE FROM SQL-42X05", "DROP TABLE SQLC-42Y55" },
{ "CREATE VIEW SQL-42X05", "DROP VIEW SQLCO-X0X05",
"LOCK TABLE SQLC-42X05",
"VALUES 1,2,3- ROW(1)- ROW(2)- ROW(3)-EXECUTE OK",
"SET SCHEMA SQLC-UPDATE 0-EXECUTE OK",
"CREATE SCHEMA S-UPDATE 0-EXECUTE OK",
"DROP SCHEMA SQL-UPDATE 0-EXECUTE OK" },
{ "DERBY FEATURE", "DERBY FEATURE",
"SET ISOLATION C-UPDATE 0-EXECUTE OK",
"SET RUNTIMESTAT-42X01", "SET STATISTICS -42X01",
"VALUES 1- ROW(1)-EXECUTE OK",
"VALUES 1- ROW(1)-EXECUTE OK" } };
String[][][] sqlControl = { sqlControl_0, sqlControl_1, sqlControl_2,
sqlControl_3, sqlControl_4 };
for (int i = 0; i < control.length; i++) {
for (int k = 1; k <= 3; k++) {
CallableStatement cs = conn.prepareCall("call SQLC.SQLCONTROL"
+ k + "_" + i + " (?, ?, ?, ?, ?, ?, ?)");
// System.out.println("{");
for (int rop = 1; rop <= 7; rop++) {
cs.registerOutParameter(rop, Types.VARCHAR);
}
cs.execute();
for (int p = 1; p <= 7; p++) {
// System.out.print("\"" + cs.getString(p) + "\"");
/*
* if (p != 7) System.out.println(","); else
* System.out.println("}");
*/
assertEquals(sqlControl[i][k - 1][p - 1], cs.getString(p));
}
cs.close();
/*
* if (k < 3) System.out.println(","); else
* System.out.println("}");
*/
}
if (control[i].length() == 0) {
// This default case (see test above) succeeds in
// creating the SQLCONTROL_DDL table, so remove it to
// avoid tripping the explicit "modifies sql data".
s.execute("drop table SQLCONTROL_DDL");
}
// System.out.println("}");
}
// test procedures that call others, e.g. to ensure that within a READS
// SQL DATA procedure, a "modifies sql data" cannot be called.
// table was dropped by previous executions.
String[][] dmlSqlControl_0 = /* dmlSqlControl_0 */{
{ "call SQLC.SQLCONTROL2_0 (?, ?, ?, ?, ?, ?, ?) ",
"CREATE VIEW SQL-UPDATE 0-EXECUTE OK",
"DROP VIEW SQLCO-UPDATE 0-EXECUTE OK",
"LOCK TABLE SQLC-UPDATE 0-EXECUTE OK",
"VALUES 1,2,3- ROW(1)- ROW(2)- ROW(3)-EXECUTE OK",
"SET SCHEMA SQLC-UPDATE 0-EXECUTE OK",
"CREATE SCHEMA S-UPDATE 0-EXECUTE OK",
"DROP SCHEMA SQL-UPDATE 0-EXECUTE OK" },
{ "call SQLC.SQLCONTROL2_1 (?, ?, ?, ?, ?, ?, ?) ",
"CREATE VIEW SQL-38001", "DROP VIEW SQLCO-38001",
"LOCK TABLE SQLC-38001", "VALUES 1,2,3-38001",
"SET SCHEMA SQLC-38001", "CREATE SCHEMA S-38001",
"DROP SCHEMA SQL-38001" },
{ "call SQLC.SQLCONTROL2_2 (?, ?, ?, ?, ?, ?, ?) ",
"CREATE VIEW SQL-38002", "DROP VIEW SQLCO-38002",
"LOCK TABLE SQLC-UPDATE 0-EXECUTE OK",
"VALUES 1,2,3-38004",
"SET SCHEMA SQLC-UPDATE 0-EXECUTE OK",
"CREATE SCHEMA S-38002", "DROP SCHEMA SQL-38002" },
{ "call SQLC.SQLCONTROL2_3 (?, ?, ?, ?, ?, ?, ?) ",
"CREATE VIEW SQL-38002", "DROP VIEW SQLCO-38002",
"LOCK TABLE SQLC-UPDATE 0-EXECUTE OK",
"VALUES 1,2,3- ROW(1)- ROW(2)- ROW(3)-EXECUTE OK",
"SET SCHEMA SQLC-UPDATE 0-EXECUTE OK",
"CREATE SCHEMA S-38002", "DROP SCHEMA SQL-38002" },
{ "call SQLC.SQLCONTROL2_4 (?, ?, ?, ?, ?, ?, ?) ",
"CREATE VIEW SQL-UPDATE 0-EXECUTE OK",
"DROP VIEW SQLCO-UPDATE 0-EXECUTE OK",
"LOCK TABLE SQLC-UPDATE 0-EXECUTE OK",
"VALUES 1,2,3- ROW(1)- ROW(2)- ROW(3)-EXECUTE OK",
"SET SCHEMA SQLC-UPDATE 0-EXECUTE OK",
"CREATE SCHEMA S-UPDATE 0-EXECUTE OK",
"DROP SCHEMA SQL-UPDATE 0-EXECUTE OK" } };
String[][] dmlSqlControl_1 = /* dmlSqlControl_1 */{
{ "call SQLC.SQLCONTROL2_0 (?, ?, ?, ?, ?, ?, ?) ",
"STATE-38001", "null", "null", "null", "null", "null",
"null" },
{ "call SQLC.SQLCONTROL2_1 (?, ?, ?, ?, ?, ?, ?) ",
"STATE-38001", "null", "null", "null", "null", "null",
"null" },
{ "call SQLC.SQLCONTROL2_2 (?, ?, ?, ?, ?, ?, ?) ",
"STATE-38001", "null", "null", "null", "null", "null",
"null" },
{ "call SQLC.SQLCONTROL2_3 (?, ?, ?, ?, ?, ?, ?) ",
"STATE-38001", "null", "null", "null", "null", "null",
"null" },
{ "call SQLC.SQLCONTROL2_4 (?, ?, ?, ?, ?, ?, ?) ",
"STATE-38001", "null", "null", "null", "null", "null",
"null" }
};
String[][] dmlSqlControl_2 = /* dmlSqlControl_2 */{
{ "call SQLC.SQLCONTROL2_0 (?, ?, ?, ?, ?, ?, ?) ",
"STATE-38002", "null", "null", "null", "null", "null",
"null" },
{ "call SQLC.SQLCONTROL2_1 (?, ?, ?, ?, ?, ?, ?) ",
"CREATE VIEW SQL-38001", "DROP VIEW SQLCO-38001",
"LOCK TABLE SQLC-38001", "VALUES 1,2,3-38001",
"SET SCHEMA SQLC-38001", "CREATE SCHEMA S-38001",
"DROP SCHEMA SQL-38001" },
{ "call SQLC.SQLCONTROL2_2 (?, ?, ?, ?, ?, ?, ?) ",
"CREATE VIEW SQL-38002", "DROP VIEW SQLCO-38002",
"LOCK TABLE SQLC-UPDATE 0-EXECUTE OK",
"VALUES 1,2,3-38004",
"SET SCHEMA SQLC-UPDATE 0-EXECUTE OK",
"CREATE SCHEMA S-38002", "DROP SCHEMA SQL-38002" },
{ "call SQLC.SQLCONTROL2_3 (?, ?, ?, ?, ?, ?, ?) ",
"STATE-38004", "null", "null", "null", "null", "null",
"null" },
{ "call SQLC.SQLCONTROL2_4 (?, ?, ?, ?, ?, ?, ?) ",
"STATE-38002", "null", "null", "null", "null", "null",
"null" } };
String[][] dmlSqlControl_3 = /* dmlSqlControl_3 */{
{ "call SQLC.SQLCONTROL2_0 (?, ?, ?, ?, ?, ?, ?) ",
"STATE-38002", "null", "null", "null", "null", "null",
"null" },
{ "call SQLC.SQLCONTROL2_1 (?, ?, ?, ?, ?, ?, ?) ",
"CREATE VIEW SQL-38001", "DROP VIEW SQLCO-38001",
"LOCK TABLE SQLC-38001", "VALUES 1,2,3-38001",
"SET SCHEMA SQLC-38001", "CREATE SCHEMA S-38001",
"DROP SCHEMA SQL-38001" },
{ "call SQLC.SQLCONTROL2_2 (?, ?, ?, ?, ?, ?, ?) ",
"CREATE VIEW SQL-38002", "DROP VIEW SQLCO-38002",
"LOCK TABLE SQLC-UPDATE 0-EXECUTE OK",
"VALUES 1,2,3-38004",
"SET SCHEMA SQLC-UPDATE 0-EXECUTE OK",
"CREATE SCHEMA S-38002", "DROP SCHEMA SQL-38002" },
{ "call SQLC.SQLCONTROL2_3 (?, ?, ?, ?, ?, ?, ?) ",
"CREATE VIEW SQL-38002", "DROP VIEW SQLCO-38002",
"LOCK TABLE SQLC-UPDATE 0-EXECUTE OK",
"VALUES 1,2,3- ROW(1)- ROW(2)- ROW(3)-EXECUTE OK",
"SET SCHEMA SQLC-UPDATE 0-EXECUTE OK",
"CREATE SCHEMA S-38002", "DROP SCHEMA SQL-38002" },
{ "call SQLC.SQLCONTROL2_4 (?, ?, ?, ?, ?, ?, ?) ",
"STATE-38002", "null", "null", "null", "null", "null",
"null" } };
String[][] dmlSqlControl_4 = /* dmlSqlControl_4 */{
{ "call SQLC.SQLCONTROL2_0 (?, ?, ?, ?, ?, ?, ?) ",
"CREATE VIEW SQL-UPDATE 0-EXECUTE OK",
"DROP VIEW SQLCO-UPDATE 0-EXECUTE OK",
"LOCK TABLE SQLC-UPDATE 0-EXECUTE OK",
"VALUES 1,2,3- ROW(1)- ROW(2)- ROW(3)-EXECUTE OK",
"SET SCHEMA SQLC-UPDATE 0-EXECUTE OK",
"CREATE SCHEMA S-UPDATE 0-EXECUTE OK",
"DROP SCHEMA SQL-UPDATE 0-EXECUTE OK" },
{ "call SQLC.SQLCONTROL2_1 (?, ?, ?, ?, ?, ?, ?) ",
"CREATE VIEW SQL-38001", "DROP VIEW SQLCO-38001",
"LOCK TABLE SQLC-38001", "VALUES 1,2,3-38001",
"SET SCHEMA SQLC-38001", "CREATE SCHEMA S-38001",
"DROP SCHEMA SQL-38001" },
{ "call SQLC.SQLCONTROL2_2 (?, ?, ?, ?, ?, ?, ?) ",
"CREATE VIEW SQL-38002", "DROP VIEW SQLCO-38002",
"LOCK TABLE SQLC-UPDATE 0-EXECUTE OK",
"VALUES 1,2,3-38004",
"SET SCHEMA SQLC-UPDATE 0-EXECUTE OK",
"CREATE SCHEMA S-38002", "DROP SCHEMA SQL-38002" },
{ "call SQLC.SQLCONTROL2_3 (?, ?, ?, ?, ?, ?, ?) ",
"CREATE VIEW SQL-38002", "DROP VIEW SQLCO-38002",
"LOCK TABLE SQLC-UPDATE 0-EXECUTE OK",
"VALUES 1,2,3- ROW(1)- ROW(2)- ROW(3)-EXECUTE OK",
"SET SCHEMA SQLC-UPDATE 0-EXECUTE OK",
"CREATE SCHEMA S-38002", "DROP SCHEMA SQL-38002" },
{ "call SQLC.SQLCONTROL2_4 (?, ?, ?, ?, ?, ?, ?) ",
"CREATE VIEW SQL-UPDATE 0-EXECUTE OK",
"DROP VIEW SQLCO-UPDATE 0-EXECUTE OK",
"LOCK TABLE SQLC-UPDATE 0-EXECUTE OK",
"VALUES 1,2,3- ROW(1)- ROW(2)- ROW(3)-EXECUTE OK",
"SET SCHEMA SQLC-UPDATE 0-EXECUTE OK",
"CREATE SCHEMA S-UPDATE 0-EXECUTE OK",
"DROP SCHEMA SQL-UPDATE 0-EXECUTE OK" } };
String[][][] dmlSqlControl = { dmlSqlControl_0, dmlSqlControl_1,
dmlSqlControl_2, dmlSqlControl_3, dmlSqlControl_4 };
s.execute("create table SQLC.SQLCONTROL_DML(I int)");
s.execute("insert into SQLC.SQLCONTROL_DML values 4");
for (int i = 0; i < control.length; i++) {
for (int t = 0; t < control.length; t++) {
CallableStatement cs = conn
.prepareCall("call SQLC.SQLCONTROL4_" + i
+ " (?, ?, ?, ?, ?, ?, ?, ?, ?)");
cs.setInt(1, t);
for (int rop = 2; rop <= 9; rop++) {
cs.registerOutParameter(rop, Types.VARCHAR);
}
cs.execute();
for (int p = 2; p <= 9; p++) {
String so = cs.getString(p);
// System.out.print("\"" + so + "\"");
/*
* if (p < 9) System.out.println(","); else
* System.out.println("}");
*/
if (so == null) {
continue;
}
assertEquals(dmlSqlControl[i][t][p - 2], so);
}
/*
* if (t< control.length -1) System.out.println(","); else
* System.out.println("}");
*/
cs.close();
}
}
// Make sure we throw proper error with network server
// if params are not registered
assertCallError(
usingEmbedded() ? "07004" : "07000",
"call SQLC.SQLCONTROL3_0 (?, ?, ?, ?, ?, ?, ?)");
s.execute("drop table SQLC.SQLCONTROL_DML");
for (int i = 0; i < control.length; i++) {
s.execute("drop PROCEDURE SQLC.SQLCONTROL1_" + i);
s.execute("drop PROCEDURE SQLC.SQLCONTROL2_" + i);
s.execute("drop PROCEDURE SQLC.SQLCONTROL3_" + i);
s.execute("drop PROCEDURE SQLC.SQLCONTROL4_" + i);
}
s.execute("drop table SQLCONTROL_DDL");
s.execute("set schema APP");
s.execute("drop schema SQLC RESTRICT");
s.close();
}
/**
* Better diagnostics when a function is being used as a procedure
* or vice versa
*/
public void testDerby5945() throws SQLException {
setAutoCommit(false);
Connection c = getConnection();
Statement s = c.createStatement();
s.executeUpdate(
"create procedure PROC( inout ret int ) parameter style java" +
" modifies sql data language java external name " +
"'" + thisClassName + ".PROC'");
s.executeUpdate(
"create function FUNC (i int) returns int parameter style java" +
" reads sql data language java external name " +
"'" + thisClassName + ".FUNC'");
try {
CallableStatement cs = c.prepareCall("{?=call PROC(?)}");
fail();
} catch (SQLException e) {
assertEquals(
"'PROC' is a procedure but it is being used as a function.",
e.getMessage());
assertSQLState("42Y03", e);
}
ResultSet rs = s.executeQuery("values func(3)");
rs.next();
assertEquals(9, rs.getInt(1));
try {
s.executeUpdate("call func(3)");
fail();
} catch (SQLException e) {
assertEquals(
"'FUNC' is a function but it is being called as a procedure.",
e.getMessage());
assertSQLState("42Y03", e);
}
rollback();
}
public static void PROC(int[] i) {}
public static int FUNC(int i) {
return i*i;
}
public static void pSMALLINT(short in, short[] inout, short[] out)
throws SQLException {
out[0] = in;
inout[0] += in;
}
public static void parameter1(int a, String b, String c,
java.sql.ResultSet[] rs) throws SQLException {
Connection conn = DriverManager
.getConnection("jdbc:default:connection");
PreparedStatement ps = conn
.prepareStatement("insert into PT1 values (?, ?, ?)");
ps.setInt(1, a);
ps.setString(2, b);
ps.setString(3, c);
ps.executeUpdate();
ps.close();
ps = conn.prepareStatement(
"select a,b, length(b), c, length(c) from PT1 where a = ?");
ps.setInt(1, a);
rs[0] = ps.executeQuery();
conn.close();
}
public static void parameter2(int a, java.math.BigDecimal b,
java.math.BigDecimal c, java.sql.ResultSet[] rs)
throws SQLException {
Connection conn = DriverManager
.getConnection("jdbc:default:connection");
PreparedStatement ps = conn
.prepareStatement("insert into PT1 values (?, ?, ?)");
ps.setInt(1, a);
ps.setString(2, b.toString());
ps.setString(3, c.toString());
ps.executeUpdate();
ps.close();
ps = conn.prepareStatement("select a,b,c from PT1 where a = ?");
ps.setInt(1, a);
rs[0] = ps.executeQuery();
conn.close();
}
public static void outparams1(int[] p1, int p2) {
p1[0] = p2 * 2;
}
public static void inoutparams2(int[] p1, int p2) {
p1[0] = p1[0] + (p2 * 2);
}
public static void inoutparams3(String[] p1, int p2) {
if (p2 == 8) {
p1[0] = "nad";
} else if (p2 == 9) {
p1[0] = null;
} else if (p2 == 10) {
p1[0] = "abcdefghijklmnopqrstuvwzyz";
}
}
public static void inoutparams4(java.math.BigDecimal[] p1, String p2) {
if (p2 == null) {
p1[0] = null;
} else {
if (p1[0] == null) {
p1[0] = new BigDecimal(p2).add(new BigDecimal("17"));
} else {
p1[0] = new BigDecimal(p2).add(p1[0]);
}
}
}
public static void sqlControl(String[] e1, String[] e2, String[] e3,
String[] e4, String[] e5, String[] e6, String[] e7)
throws SQLException {
Connection conn = DriverManager
.getConnection("jdbc:default:connection");
Statement s = conn.createStatement();
executeStatement(s, "CREATE TABLE SQLCONTROL_DDL (I INT)", e1);
executeStatement(
s,
"ALTER TABLE SQLC.SQLCONTROL_DML ADD COLUMN B INT DEFAULT NULL",
e2);
executeStatement(s, "INSERT INTO SQLC.SQLCONTROL_DML(I) VALUES (1)", e3);
executeStatement(s, "UPDATE SQLC.SQLCONTROL_DML SET I = I + 11", e4);
executeStatement(s, "SELECT * FROM SQLC.SQLCONTROL_DML", e5);
executeStatement(s, "DELETE FROM SQLC.SQLCONTROL_DML", e6);
executeStatement(s, "DROP TABLE SQLC.SQLCONTROL_DML", e7);
conn.close();
}
public static void sqlControl2(String[] e1, String[] e2, String[] e3,
String[] e4, String[] e5, String[] e6, String[] e7)
throws SQLException {
Connection conn = DriverManager
.getConnection("jdbc:default:connection");
Statement s = conn.createStatement();
executeStatement(
s,
"CREATE VIEW SQLCONTROL_VIEW AS SELECT * FROM SQLC.SQLCONTROL_DML",
e1);
executeStatement(s, "DROP VIEW SQLCONTROL_VIEW", e2);
executeStatement(s, "LOCK TABLE SQLC.SQLCONTROL_DML IN EXCLUSIVE MODE",
e3);
executeStatement(s, "VALUES 1,2,3", e4);
executeStatement(s, "SET SCHEMA SQLC", e5);
executeStatement(s, "CREATE SCHEMA SQLC_M", e6);
executeStatement(s, "DROP SCHEMA SQLC_M RESTRICT", e7);
conn.close();
}
public static void sqlControl3(String[] e1, String[] e2, String[] e3,
String[] e4, String[] e5, String[] e6, String[] e7)
throws SQLException {
Connection conn =
DriverManager.getConnection("jdbc:default:connection");
Statement s = conn.createStatement();
e1[0] = "DERBY FEATURE";
e2[0] = "DERBY FEATURE";
executeStatement(s, "SET ISOLATION CS", e3);
executeStatement(s, "SET RUNTIMESTATISTICS OFF", e4);
executeStatement(s, "SET STATISTICS TIMING OFF", e5);
executeStatement(s, "VALUES 1", e6);
executeStatement(s, "VALUES 1", e7);
conn.close();
}
public static void sqlControl4(int sqlc, String[] e1, String[] e2,
String[] e3, String[] e4, String[] e5, String[] e6, String[] e7,
String[] e8) throws SQLException {
Connection conn = DriverManager
.getConnection("jdbc:default:connection");
String sql = "call SQLC.SQLCONTROL2_" + sqlc
+ " (?, ?, ?, ?, ?, ?, ?) ";
e1[0] = sql;
CallableStatement cs1 = conn.prepareCall(sql);
try {
for (int rop = 1; rop <= 7; rop++) {
cs1.registerOutParameter(rop, Types.VARCHAR);
}
cs1.execute();
e2[0] = cs1.getString(1);
e3[0] = cs1.getString(2);
e4[0] = cs1.getString(3);
e5[0] = cs1.getString(4);
e6[0] = cs1.getString(5);
e7[0] = cs1.getString(6);
e8[0] = cs1.getString(7);
} catch (SQLException sqle) {
StringBuilder sb = new StringBuilder(128);
sb.append("STATE");
do {
sb.append("-");
String ss = sqle.getSQLState();
if (ss == null) {
ss = "?????";
}
sb.append(ss);
sqle = sqle.getNextException();
} while (sqle != null);
e2[0] = sb.toString();
}
cs1.close();
conn.close();
}
private static void executeStatement(Statement s, String sql,
String[] result) {
StringBuilder sb = new StringBuilder(128);
int len = sql.length();
if (len > 15) {
len = 15;
}
sb.append(sql.substring(0, len));
try {
if (s.execute(sql)) {
ResultSet rs = s.getResultSet();
while (rs.next()) {
sb.append("- ROW(");
sb.append(rs.getString(1));
sb.append(")");
}
rs.close();
} else {
sb.append("-UPDATE ");
sb.append(s.getUpdateCount());
}
sb.append("-EXECUTE OK");
} catch (SQLException sqle) {
do {
sb.append("-");
String ss = sqle.getSQLState();
if (ss == null) {
ss = "?????";
}
sb.append(ss);
sqle = sqle.getNextException();
} while (sqle != null);
}
result[0] = sb.toString();
}
public static void missingDynamicParameter(int p1) {
}
public static void missingDynamicParameter(int p1, Object p2) {
}
public static void badDynamicParameter(int p1, ProcedureTest[] data) {
}
public static void zeroArgDynamicResult(ResultSet[] data1,
ResultSet[] data2, ResultSet[] data3, ResultSet[] data4) {
}
}
|