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
|
/*
* Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.ProcedureTest
*
* 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.jdbcapi;
import java.math.BigDecimal;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
import junit.framework.Test;
import org.apache.derby.iapi.types.HarmonySerialBlob;
import org.apache.derby.iapi.types.HarmonySerialClob;
import org.apache.derbyTesting.functionTests.tests.lang.Price;
import org.apache.derbyTesting.junit.BaseJDBCTestCase;
import org.apache.derbyTesting.junit.BaseTestSuite;
import org.apache.derbyTesting.junit.CleanDatabaseTestSetup;
import org.apache.derbyTesting.junit.JDBC;
import org.apache.derbyTesting.junit.TestConfiguration;
/**
* Tests of stored procedures.
*/
public class ProcedureTest extends BaseJDBCTestCase {
/**
* Creates a new <code>ProcedureTest</code> instance.
*
* @param name name of the test
*/
public ProcedureTest(String name) {
super(name);
}
// TESTS
/**
* Tests that <code>Statement.executeQuery()</code> fails when no
* result sets are returned.
* @exception SQLException if a database error occurs
*/
public void testExecuteQueryWithNoDynamicResultSets() throws SQLException {
Statement stmt = createStatement();
try {
stmt.executeQuery("CALL RETRIEVE_DYNAMIC_RESULTS(0)");
fail("executeQuery() didn't fail.");
} catch (SQLException sqle) {
assertNoResultSetFromExecuteQuery(sqle);
}
}
/**
* Tests that <code>Statement.executeQuery()</code> succeeds when
* one result set is returned from a stored procedure.
* @exception SQLException if a database error occurs
*/
public void testExecuteQueryWithOneDynamicResultSet() throws SQLException {
Statement stmt = createStatement();
ResultSet rs = stmt.executeQuery("CALL RETRIEVE_DYNAMIC_RESULTS(1)");
assertNotNull("executeQuery() returned null.", rs);
assertSame(stmt, rs.getStatement());
JDBC.assertDrainResultsHasData(rs);
}
/**
* Tests that <code>Statement.executeQuery()</code> fails when
* multiple result sets are returned.
* @exception SQLException if a database error occurs
*/
public void testExecuteQueryWithMoreThanOneDynamicResultSet()
throws SQLException
{
Statement stmt = createStatement();
try {
stmt.executeQuery("CALL RETRIEVE_DYNAMIC_RESULTS(2)");
fail("executeQuery() didn't fail.");
} catch (SQLException sqle) {
assertMultipleResultsFromExecuteQuery(sqle);
}
}
/**
* Tests that <code>Statement.executeUpdate()</code> succeeds when
* no result sets are returned.
*
* @exception SQLException if a database error occurs
*/
public void testExecuteUpdateWithNoDynamicResultSets()
throws SQLException
{
Statement stmt = createStatement();
assertUpdateCount(stmt, 0, "CALL RETRIEVE_DYNAMIC_RESULTS(0)");
JDBC.assertNoMoreResults(stmt);
}
/**
* Tests that <code>Statement.executeUpdate()</code> fails when a
* result set is returned from a stored procedure.
* @exception SQLException if a database error occurs
*/
public void testExecuteUpdateWithOneDynamicResultSet() throws SQLException {
Statement stmt = createStatement();
try {
stmt.executeUpdate("CALL RETRIEVE_DYNAMIC_RESULTS(1)");
fail("executeUpdate() didn't fail.");
} catch (SQLException sqle) {
assertResultsFromExecuteUpdate(sqle);
}
}
/**
* Tests that <code>PreparedStatement.executeQuery()</code> fails
* when no result sets are returned.
* @exception SQLException if a database error occurs
*/
public void testExecuteQueryWithNoDynamicResultSets_prepared()
throws SQLException
{
PreparedStatement ps =
prepareStatement("CALL RETRIEVE_DYNAMIC_RESULTS(?)");
ps.setInt(1, 0);
try {
ps.executeQuery();
fail("executeQuery() didn't fail.");
} catch (SQLException sqle) {
assertNoResultSetFromExecuteQuery(sqle);
}
}
/**
* Tests that <code>PreparedStatement.executeQuery()</code>
* succeeds when one result set is returned from a stored
* procedure.
* @exception SQLException if a database error occurs
*/
public void testExecuteQueryWithOneDynamicResultSet_prepared()
throws SQLException
{
PreparedStatement ps =
prepareStatement("CALL RETRIEVE_DYNAMIC_RESULTS(?)");
ps.setInt(1, 1);
ResultSet rs = ps.executeQuery();
assertNotNull("executeQuery() returned null.", rs);
assertSame(ps, rs.getStatement());
JDBC.assertDrainResultsHasData(rs);
}
/**
* Tests that <code>PreparedStatement.executeQuery()</code> fails
* when multiple result sets are returned.
* @exception SQLException if a database error occurs
*/
public void testExecuteQueryWithMoreThanOneDynamicResultSet_prepared()
throws SQLException
{
PreparedStatement ps =
prepareStatement("CALL RETRIEVE_DYNAMIC_RESULTS(?)");
ps.setInt(1, 2);
try {
ps.executeQuery();
fail("executeQuery() didn't fail.");
} catch (SQLException sqle) {
assertMultipleResultsFromExecuteQuery(sqle);
}
}
/**
* Tests that <code>PreparedStatement.executeUpdate()</code>
* succeeds when no result sets are returned.
*
* @exception SQLException if a database error occurs
*/
public void testExecuteUpdateWithNoDynamicResultSets_prepared()
throws SQLException
{
PreparedStatement ps =
prepareStatement("CALL RETRIEVE_DYNAMIC_RESULTS(?)");
ps.setInt(1, 0);
assertUpdateCount(ps, 0);
JDBC.assertNoMoreResults(ps);
}
/**
* Tests that <code>PreparedStatement.executeUpdate()</code> fails
* when a result set is returned from a stored procedure.
*
* @exception SQLException if a database error occurs
*/
public void testExecuteUpdateWithOneDynamicResultSet_prepared()
throws SQLException
{
PreparedStatement ps =
prepareStatement("CALL RETRIEVE_DYNAMIC_RESULTS(?)");
ps.setInt(1, 1);
try {
ps.executeUpdate();
fail("executeUpdate() didn't fail.");
} catch (SQLException sqle) {
assertResultsFromExecuteUpdate(sqle);
}
}
/**
* Tests that <code>CallableStatement.executeQuery()</code> fails
* when no result sets are returned.
* @exception SQLException if a database error occurs
*/
public void testExecuteQueryWithNoDynamicResultSets_callable()
throws SQLException
{
CallableStatement cs =
prepareCall("CALL RETRIEVE_DYNAMIC_RESULTS(?)");
cs.setInt(1, 0);
try {
cs.executeQuery();
fail("executeQuery() didn't fail.");
} catch (SQLException sqle) {
assertNoResultSetFromExecuteQuery(sqle);
}
}
/**
* Tests that <code>CallableStatement.executeQuery()</code>
* succeeds when one result set is returned from a stored
* procedure.
* @exception SQLException if a database error occurs
*/
public void testExecuteQueryWithOneDynamicResultSet_callable()
throws SQLException
{
CallableStatement cs =
prepareCall("CALL RETRIEVE_DYNAMIC_RESULTS(?)");
cs.setInt(1, 1);
ResultSet rs = cs.executeQuery();
assertNotNull("executeQuery() returned null.", rs);
assertSame(cs, rs.getStatement());
JDBC.assertDrainResultsHasData(rs);
}
/**
* Tests that <code>CallableStatement.executeQuery()</code> fails
* when multiple result sets are returned.
* @exception SQLException if a database error occurs
*/
public void testExecuteQueryWithMoreThanOneDynamicResultSet_callable()
throws SQLException
{
CallableStatement cs =
prepareCall("CALL RETRIEVE_DYNAMIC_RESULTS(?)");
cs.setInt(1, 2);
try {
cs.executeQuery();
fail("executeQuery() didn't fail.");
} catch (SQLException sqle) {
assertMultipleResultsFromExecuteQuery(sqle);
}
}
/**
* Tests that <code>CallableStatement.executeUpdate()</code>
* succeeds when no result sets are returned.
*
* @exception SQLException if a database error occurs
*/
public void testExecuteUpdateWithNoDynamicResultSets_callable()
throws SQLException
{
CallableStatement cs =
prepareCall("CALL RETRIEVE_DYNAMIC_RESULTS(?)");
cs.setInt(1, 0);
assertUpdateCount(cs, 0);
JDBC.assertNoMoreResults(cs);
}
/**
* Tests that <code>CallableStatement.executeUpdate()</code> fails
* when a result set is returned from a stored procedure.
* @exception SQLException if a database error occurs
*/
public void testExecuteUpdateWithOneDynamicResultSet_callable()
throws SQLException
{
CallableStatement cs =
prepareCall("CALL RETRIEVE_DYNAMIC_RESULTS(?)");
cs.setInt(1, 1);
try {
cs.executeUpdate();
fail("executeUpdate() didn't fail.");
} catch (SQLException sqle) {
assertResultsFromExecuteUpdate(sqle);
}
}
/**
* Tests that the effects of executing a stored procedure with
* <code>executeQuery()</code> are correctly rolled back when
* <code>Connection.rollback()</code> is called.
* @exception SQLException if a database error occurs
*/
public void testRollbackStoredProcWithExecuteQuery() throws SQLException {
Statement stmt = createStatement();
ResultSet rs = stmt.executeQuery("CALL PROC_WITH_SIDE_EFFECTS(1)");
rs.close();
rollback();
// Expect Side effects from stored procedure to be rolled back.
JDBC.assertEmpty(stmt.executeQuery("SELECT * FROM SIMPLE_TABLE"));
}
/**
* Tests that the effects of executing a stored procedure with
* <code>executeUpdate()</code> are correctly rolled back when
* <code>Connection.rollback()</code> is called.
* @exception SQLException if a database error occurs
*/
public void testRollbackStoredProcWithExecuteUpdate() throws SQLException {
Statement stmt = createStatement();
stmt.executeUpdate("CALL PROC_WITH_SIDE_EFFECTS(0)");
rollback();
// Expect Side effects from stored procedure to be rolled back.
JDBC.assertEmpty(stmt.executeQuery("SELECT * FROM SIMPLE_TABLE"));
}
/**
* Tests that the effects of executing a stored procedure with
* <code>executeQuery()</code> are correctly rolled back when the
* query fails because the number of returned result sets is zero.
*
* @exception SQLException if a database error occurs
*/
public void testRollbackStoredProcWhenExecuteQueryReturnsNothing()
throws SQLException
{
Connection conn = getConnection();
conn.setAutoCommit(true);
Statement stmt = createStatement();
try {
stmt.executeQuery("CALL PROC_WITH_SIDE_EFFECTS(0)");
fail("executeQuery() didn't fail.");
} catch (SQLException sqle) {
assertNoResultSetFromExecuteQuery(sqle);
}
// Expect Side effects from stored procedure to be rolled back.
JDBC.assertEmpty(stmt.executeQuery("SELECT * FROM SIMPLE_TABLE"));
}
/**
* Tests that the effects of executing a stored procedure with
* <code>executeQuery()</code> are correctly rolled back when the
* query fails because the number of returned result sets is more
* than one.
*
* @exception SQLException if a database error occurs
*/
public void testRollbackStoredProcWhenExecuteQueryReturnsTooMuch()
throws SQLException
{
Connection conn = getConnection();
conn.setAutoCommit(true);
Statement stmt = createStatement();
try {
stmt.executeQuery("CALL PROC_WITH_SIDE_EFFECTS(2)");
fail("executeQuery() didn't fail.");
} catch (SQLException sqle) {
assertMultipleResultsFromExecuteQuery(sqle);
}
// Expect Side effects from stored procedure to be rolled back.
JDBC.assertEmpty(stmt.executeQuery("SELECT * FROM SIMPLE_TABLE"));
}
/**
* Tests that the effects of executing a stored procedure with
* <code>executeUpdate()</code> are correctly rolled back when the
* query fails because the stored procedure returned a result set.
*
* @exception SQLException if a database error occurs
*/
public void testRollbackStoredProcWhenExecuteUpdateReturnsResults()
throws SQLException
{
Connection conn = getConnection();
conn.setAutoCommit(true);
Statement stmt = createStatement();
try {
stmt.executeUpdate("CALL PROC_WITH_SIDE_EFFECTS(1)");
fail("executeUpdate() didn't fail.");
} catch (SQLException sqle) {
assertResultsFromExecuteUpdate(sqle);
}
// Expect Side effects from stored procedure to be rolled back.
JDBC.assertEmpty(stmt.executeQuery("SELECT * FROM SIMPLE_TABLE"));
}
/**
* Tests that the effects of executing a stored procedure with
* <code>executeQuery()</code> are correctly rolled back when the
* query fails because the number of returned result sets is zero.
*
* @exception SQLException if a database error occurs
*/
public void testRollbackStoredProcWhenExecuteQueryReturnsNothing_prepared()
throws SQLException
{
Connection conn = getConnection();
conn.setAutoCommit(true);
PreparedStatement ps =
prepareStatement("CALL PROC_WITH_SIDE_EFFECTS(?)");
ps.setInt(1, 0);
try {
ps.executeQuery();
fail("executeQuery() didn't fail.");
} catch (SQLException sqle) {
assertNoResultSetFromExecuteQuery(sqle);
}
Statement stmt = createStatement();
// Expect Side effects from stored procedure to be rolled back.
JDBC.assertEmpty(stmt.executeQuery("SELECT * FROM SIMPLE_TABLE"));
}
/**
* Tests that the effects of executing a stored procedure with
* <code>executeQuery()</code> are correctly rolled back when the
* query fails because the number of returned result sets is more
* than one.
*
* @exception SQLException if a database error occurs
*/
public void testRollbackStoredProcWhenExecuteQueryReturnsTooMuch_prepared()
throws SQLException
{
Connection conn = getConnection();
conn.setAutoCommit(true);
PreparedStatement ps =
prepareStatement("CALL PROC_WITH_SIDE_EFFECTS(?)");
ps.setInt(1, 2);
try {
ps.executeQuery();
fail("executeQuery() didn't fail.");
} catch (SQLException sqle) {
assertMultipleResultsFromExecuteQuery(sqle);
}
Statement stmt = createStatement();
// Expect Side effects from stored procedure to be rolled back.
JDBC.assertEmpty(stmt.executeQuery("SELECT * FROM SIMPLE_TABLE"));
}
/**
* Tests that the effects of executing a stored procedure with
* <code>executeUpdate()</code> are correctly rolled back when the
* query fails because the stored procedure returned a result set.
*
* @exception SQLException if a database error occurs
*/
public void
testRollbackStoredProcWhenExecuteUpdateReturnsResults_prepared()
throws SQLException
{
Connection conn = getConnection();
conn.setAutoCommit(true);
PreparedStatement ps =
prepareStatement("CALL PROC_WITH_SIDE_EFFECTS(?)");
ps.setInt(1, 1);
try {
ps.executeUpdate();
fail("executeUpdate() didn't fail.");
} catch (SQLException sqle) {
assertResultsFromExecuteUpdate(sqle);
}
Statement stmt = createStatement();
// Expect Side effects from stored procedure to be rolled back.
JDBC.assertEmpty(stmt.executeQuery("SELECT * FROM SIMPLE_TABLE"));
}
/**
* Tests that closed result sets are not returned when calling
* <code>executeQuery()</code>.
* @exception SQLException if a database error occurs
*/
public void testClosedDynamicResultSetsFromExecuteQuery()
throws SQLException
{
Statement stmt = createStatement();
try {
ResultSet rs = stmt.executeQuery("CALL RETRIEVE_CLOSED_RESULT()");
fail("executeQuery() didn't fail.");
} catch (SQLException sqle) {
assertNoResultSetFromExecuteQuery(sqle);
}
}
/**
* Tests that closed result sets are ignored when calling
* <code>executeUpdate()</code>.
* @exception SQLException if a database error occurs
*/
public void testClosedDynamicResultSetsFromExecuteUpdate()
throws SQLException
{
Statement stmt = createStatement();
stmt.executeUpdate("CALL RETRIEVE_CLOSED_RESULT()");
JDBC.assertNoMoreResults(stmt);
}
/**
* Tests that dynamic result sets from other connections are
* ignored when calling <code>executeQuery</code>.
* @exception SQLException if a database error occurs
*/
public void testDynamicResultSetsFromOtherConnectionWithExecuteQuery()
throws SQLException
{
PreparedStatement ps =
prepareStatement("CALL RETRIEVE_EXTERNAL_RESULT(?,?,?)");
ps.setString(1, getTestConfiguration().getDefaultDatabaseName());
ps.setString(2, getTestConfiguration().getUserName());
ps.setString(3, getTestConfiguration().getUserPassword());
try {
ps.executeQuery();
fail("executeQuery() didn't fail.");
} catch (SQLException sqle) {
assertNoResultSetFromExecuteQuery(sqle);
}
}
/**
* Tests that dynamic result sets from other connections are
* ignored when calling <code>executeUpdate</code>.
* @exception SQLException if a database error occurs
*/
public void testDynamicResultSetsFromOtherConnectionWithExecuteUpdate()
throws SQLException
{
PreparedStatement ps =
prepareStatement("CALL RETRIEVE_EXTERNAL_RESULT(?,?,?)");
ps.setString(1, getTestConfiguration().getDefaultDatabaseName());
ps.setString(2, getTestConfiguration().getUserName());
ps.setString(3, getTestConfiguration().getUserPassword());
ps.executeUpdate();
JDBC.assertNoMoreResults(ps);
}
/**
* Test that a call to getBlob() to retrieve the value of a non-BLOB
* parameter fails with the expected SQLException. Used to throw
* ClassCastException, see DERBY-4970.
*/
public void testGetBlobFromIntParameter() throws SQLException {
CallableStatement cs = prepareCall("call int_out(?)");
cs.registerOutParameter(1, Types.INTEGER);
cs.execute();
try {
cs.getBlob(1);
fail("getBlob() on int parameter expected to fail");
} catch (SQLException sqle) {
assertSQLState("22005", sqle);
}
}
/**
* Test that a call to getClob() to retrieve the value of a non-CLOB
* parameter fails with the expected SQLException. Used to throw
* ClassCastException, see DERBY-4970.
*/
public void testGetClobFromIntParameter() throws SQLException {
CallableStatement cs = prepareCall("call int_out(?)");
cs.registerOutParameter(1, Types.INTEGER);
cs.execute();
try {
cs.getClob(1);
fail("getClob() on int parameter expected to fail");
} catch (SQLException sqle) {
assertSQLState("22005", sqle);
}
}
/**
* Test that a statement severity error inside a procedure doesn't kill
* the top-level statement that executes the stored procedure. Regression
* test case for DERBY-5280.
*/
public void testStatementSeverityErrorInProcedure() throws SQLException {
Statement s = createStatement();
s.execute("create procedure proc_5280() language java " +
"parameter style java external name '" +
getClass().getName() + ".proc_5280' reads sql data");
s.execute("call proc_5280()");
}
/**
* Procedure that drops a non-existent table and ignores the exception
* thrown because of it. Used by the regression test case for DERBY-5280.
*/
public static void proc_5280() throws SQLException {
Connection c = DriverManager.getConnection("jdbc:default:connection");
Statement s = c.createStatement();
// Drop a non-existent table and verify that it fails with the
// expected exception. Ignore the exception.
try {
s.execute("drop table this_table_does_not_exist");
fail("dropping non-existent table should fail");
} catch (SQLException sqle) {
assertSQLState("42Y55", sqle);
}
// The statement should still work.
JDBC.assertSingleValueResultSet(s.executeQuery("values 1"), "1");
}
/**
* Test that INOUT args are preserved over procedure invocations.
* See DERBY-2515.
*/
public void test_2515() throws Exception
{
Connection conn = getConnection();
PreparedStatement ps = conn.prepareStatement
(
"create type price_2515 external name 'org.apache.derbyTesting.functionTests.tests.lang.Price' language java\n"
);
ps.execute();
ps.close();
ps = conn.prepareStatement
(
"create procedure proc_2515\n" +
"(\n" +
"\tin passNumber int,\n" +
"\tout returnMessage varchar( 32672 ),\n" +
"\tinout bigintArg bigint,\n" +
"\tinout blobArg blob,\n" +
"inout booleanArg boolean,\n" +
"inout charArg char( 6 ),\n" +
"inout charForBitDataArg char( 3 ) for bit data,\n" +
"inout clobArg clob,\n" +
"inout dateArg date,\n" +
"inout decimalArg decimal,\n" +
"inout doubleArg double,\n" +
"inout intArg int,\n" +
"inout longVarcharArg long varchar,\n" +
"inout longVarcharForBitDataArg long varchar for bit data,\n" +
"inout realArg real,\n" +
"inout smallintArg smallint,\n" +
"inout timeArg time,\n" +
"inout timestampArg timestamp,\n" +
"inout priceArg price_2515,\n" +
"inout varcharArg varchar( 20 ),\n" +
"inout varcharForBitDataArg varchar( 3 ) for bit data\n" +
")\n" +
"parameter style java language java no sql\n" +
"external name '" + ProcedureTest.class.getName() + ".proc_2515'"
);
ps.execute();
ps.close();
CallableStatement cs = conn.prepareCall
( "call proc_2515( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )" );
AllTypesTuple firstArgs = makeFirstAllTypesTuple();
int idx = 2;
cs.registerOutParameter( idx++, Types.VARCHAR );
cs.registerOutParameter( idx, Types.BIGINT );
cs.setLong( idx++, firstArgs.get_bigintArg().longValue() );
cs.registerOutParameter( idx, Types.BLOB );
cs.setBlob( idx++, firstArgs.get_blobArg() );
cs.registerOutParameter( idx, Types.BOOLEAN );
cs.setBoolean( idx++, firstArgs.get_booleanArg().booleanValue() );
cs.registerOutParameter( idx, Types.CHAR );
cs.setString( idx++, firstArgs.get_charArg() );
cs.registerOutParameter( idx, Types.BINARY );
cs.setBytes( idx++, firstArgs.get_charForBitDataArg() );
cs.registerOutParameter( idx, Types.CLOB );
cs.setClob( idx++, firstArgs.get_clobArg() );
cs.registerOutParameter( idx, Types.DATE );
cs.setDate( idx++, firstArgs.get_dateArg() );
cs.registerOutParameter( idx, Types.DECIMAL );
cs.setBigDecimal( idx++, firstArgs.get_decimalArg() );
cs.registerOutParameter( idx, Types.DOUBLE );
cs.setDouble( idx++, firstArgs.get_doubleArg().doubleValue() );
cs.registerOutParameter( idx, Types.INTEGER );
cs.setInt( idx++, firstArgs.get_intArg().intValue() );
cs.registerOutParameter( idx, Types.LONGVARCHAR );
cs.setString( idx++, firstArgs.get_longVarcharArg() );
cs.registerOutParameter( idx, Types.LONGVARBINARY );
cs.setBytes( idx++, firstArgs.get_longVarcharForBitDataArg() );
cs.registerOutParameter( idx, Types.REAL );
cs.setFloat( idx++, firstArgs.get_realArg().floatValue() );
cs.registerOutParameter( idx, Types.SMALLINT );
cs.setShort( idx++, firstArgs.get_smallintArg().shortValue() );
cs.registerOutParameter( idx, Types.TIME );
cs.setTime( idx++, firstArgs.get_timeArg() );
cs.registerOutParameter( idx, Types.TIMESTAMP );
cs.setTimestamp( idx++, firstArgs.get_timestampArg() );
cs.registerOutParameter( idx, Types.JAVA_OBJECT );
cs.setObject( idx++, firstArgs.get_priceArg() );
cs.registerOutParameter( idx, Types.VARCHAR );
cs.setString( idx++, firstArgs.get_varcharArg() );
cs.registerOutParameter( idx, Types.VARBINARY );
cs.setBytes( idx++, firstArgs.get_varcharForBitDataArg() );
cs.setInt( 1, 0 );
cs.execute();
assertEquals( "", cs.getString( 2 ) ); // the return message should be empty, meaning the call args were what the procedure expected
assertEquals( "", makeSecondAllTypesTuple().compare( getActualReturnArgs( cs ) ) );
cs.setInt( 1, 1 );
cs.execute();
assertEquals( "", cs.getString( 2 ) ); // the return message should be empty, meaning the call args were what the procedure expected
assertEquals( "", makeThirdAllTypesTuple().compare( getActualReturnArgs( cs ) ) );
cs.setInt( 1, 2 );
cs.execute();
assertEquals( "", cs.getString( 2 ) ); // the return message should be empty, meaning the call args were what the procedure expected
assertEquals( "", makeFourthAllTypesTuple().compare( getActualReturnArgs( cs ) ) );
ps = conn.prepareStatement( "drop procedure proc_2515" );
ps.execute();
ps.close();
ps = conn.prepareStatement( "drop type price_2515 restrict" );
ps.execute();
ps.close();
}
private AllTypesTuple getActualReturnArgs( CallableStatement cs )
throws Exception
{
int idx = 3;
return new AllTypesTuple
(
(Long) cs.getObject( idx++ ),
(Blob) cs.getObject( idx++ ),
(Boolean) cs.getObject( idx++ ),
(String) cs.getObject( idx++ ),
(byte[]) cs.getObject( idx++ ),
(Clob) cs.getObject( idx++ ),
(Date) cs.getObject( idx++ ),
(BigDecimal) cs.getObject( idx++ ),
(Double) cs.getObject( idx++ ),
(Integer) cs.getObject( idx++ ),
(String) cs.getObject( idx++ ),
(byte[]) cs.getObject( idx++ ),
(Float) cs.getObject( idx++ ),
(Integer) cs.getObject( idx++ ),
(Time) cs.getObject( idx++ ),
(Timestamp) cs.getObject( idx++ ),
(Price) cs.getObject( idx++ ),
(String) cs.getObject( idx++ ),
(byte[]) cs.getObject( idx++ )
);
}
/**
* Regression test case for DERBY-2516. If an INOUT parameter had been
* registered as an output parameter, but no input value had been assigned
* to it, the client driver would go ahead and execute the statement
* using null as input.
*/
public void testInOutParamNotSet() throws SQLException {
setAutoCommit(false);
Statement s = createStatement();
s.execute("create procedure proc_2516 (inout i int) " +
"language java parameter style java external name '" +
getClass().getName() + ".proc_2516' no sql");
// Register an INOUT parameter, but don't set it. Expect failure.
// Client used to execute without error.
CallableStatement cs = prepareCall("call proc_2516(?)");
cs.registerOutParameter(1, Types.INTEGER);
assertStatementError("07000", cs);
// Should work if the parameter has been set.
cs.setInt(1, 0);
cs.execute();
assertEquals(10, cs.getInt(1));
// After clearing the parameters, execution should fail. Client used
// to succeed.
cs.clearParameters();
assertStatementError("07000", cs);
// Setting the parameter again should make it work.
cs.setInt(1, 1);
cs.execute();
assertEquals(10, cs.getInt(1));
}
/**
* Stored procedure used by the regression test case for DERBY-2516.
*
* @param i INOUT parameter that gets set to 10 by the procedure
*/
public static void proc_2516(Integer[] i) {
i[0] = 10;
}
/**
* Test that we create and execute stored procedures with as many
* parameters as the Java specification allows.
*/
public void testMaxNumberOfParameters() throws SQLException {
// Test with the maximum number of parameters allowed by the
// Java Virtual Machine specification. That is, 255 parameters.
testMaxNumberOfParameters(255, true);
// Test with one more parameter than allowed. Since we have no way
// to declare a method with that many parameters, expect execution to
// fail gracefully. The DDL will work, however.
testMaxNumberOfParameters(256, false);
// Test with a very high number of parameters. Again, expect DDL to
// succeed and execution to fail gracefully.
testMaxNumberOfParameters(10000, false);
}
/**
* Create and execute a stored procedure backed by a Java method with the
* specified number of parameters.
*
* @param params the number of parameters
* @param methodExists whether or not a method called
* {@code procWithManyParams} with the specified number of parameters
* exists
*/
private void testMaxNumberOfParameters(int params, boolean methodExists)
throws SQLException {
final String javaMethod = getClass().getName() + ".procWithManyParams";
final String sqlProc = "PROC_WITH_LOTS_OF_PARAMETERS";
// Disable auto-commit for easy cleanup with rollback().
setAutoCommit(false);
// Create a procedure with many parameters.
StringBuffer sb = new StringBuffer("create procedure ");
sb.append(sqlProc).append('(');
for (int i = 0; i < params; i++) {
if (i > 0) {
sb.append(',');
}
sb.append('p').append(i).append(" int");
}
sb.append(") language java parameter style java external name '");
sb.append(javaMethod).append("' no sql");
Statement s = createStatement();
s.execute(sb.toString());
// Check that the database meta-data has correct information.
DatabaseMetaData dmd = getConnection().getMetaData();
JDBC.assertFullResultSet(
dmd.getProcedures(
null, null, sqlProc),
new Object[][] {{
"", "APP", sqlProc, null, null, null,
javaMethod,
Integer.valueOf(DatabaseMetaData.procedureNoResult),
new JDBC.GeneratedId()
}},
false);
JDBC.assertDrainResults(
dmd.getProcedureColumns(null, null, sqlProc, "%"),
params);
// Execute the procedure.
sb.setLength(0);
sb.append("call ").append(sqlProc).append('(');
for (int i = 0; i < params; i++) {
if (i > 0) {
sb.append(',');
}
sb.append(i);
}
sb.append(')');
if (methodExists) {
s.execute(sb.toString());
} else {
assertCallError("42X50", sb.toString());
}
rollback();
}
public static void procWithManyParams(
int p001, int p002, int p003, int p004, int p005, int p006, int p007,
int p008, int p009, int p010, int p011, int p012, int p013, int p014,
int p015, int p016, int p017, int p018, int p019, int p020, int p021,
int p022, int p023, int p024, int p025, int p026, int p027, int p028,
int p029, int p030, int p031, int p032, int p033, int p034, int p035,
int p036, int p037, int p038, int p039, int p040, int p041, int p042,
int p043, int p044, int p045, int p046, int p047, int p048, int p049,
int p050, int p051, int p052, int p053, int p054, int p055, int p056,
int p057, int p058, int p059, int p060, int p061, int p062, int p063,
int p064, int p065, int p066, int p067, int p068, int p069, int p070,
int p071, int p072, int p073, int p074, int p075, int p076, int p077,
int p078, int p079, int p080, int p081, int p082, int p083, int p084,
int p085, int p086, int p087, int p088, int p089, int p090, int p091,
int p092, int p093, int p094, int p095, int p096, int p097, int p098,
int p099, int p100, int p101, int p102, int p103, int p104, int p105,
int p106, int p107, int p108, int p109, int p110, int p111, int p112,
int p113, int p114, int p115, int p116, int p117, int p118, int p119,
int p120, int p121, int p122, int p123, int p124, int p125, int p126,
int p127, int p128, int p129, int p130, int p131, int p132, int p133,
int p134, int p135, int p136, int p137, int p138, int p139, int p140,
int p141, int p142, int p143, int p144, int p145, int p146, int p147,
int p148, int p149, int p150, int p151, int p152, int p153, int p154,
int p155, int p156, int p157, int p158, int p159, int p160, int p161,
int p162, int p163, int p164, int p165, int p166, int p167, int p168,
int p169, int p170, int p171, int p172, int p173, int p174, int p175,
int p176, int p177, int p178, int p179, int p180, int p181, int p182,
int p183, int p184, int p185, int p186, int p187, int p188, int p189,
int p190, int p191, int p192, int p193, int p194, int p195, int p196,
int p197, int p198, int p199, int p200, int p201, int p202, int p203,
int p204, int p205, int p206, int p207, int p208, int p209, int p210,
int p211, int p212, int p213, int p214, int p215, int p216, int p217,
int p218, int p219, int p220, int p221, int p222, int p223, int p224,
int p225, int p226, int p227, int p228, int p229, int p230, int p231,
int p232, int p233, int p234, int p235, int p236, int p237, int p238,
int p239, int p240, int p241, int p242, int p243, int p244, int p245,
int p246, int p247, int p248, int p249, int p250, int p251, int p252,
int p253, int p254, int p255)
{
}
// UTILITY METHODS
/**
* Raises an exception if the exception is not caused by
* <code>executeQuery()</code> returning no result set.
*
* @param sqle a <code>SQLException</code> value
*/
private void assertNoResultSetFromExecuteQuery(SQLException sqle) {
assertSQLState("Unexpected SQL state.", "X0Y78", sqle);
}
/**
* Raises an exception if the exception is not caused by
* <code>executeQuery()</code> returning multiple result sets.
*
* @param sqle a <code>SQLException</code> value
*/
private void assertMultipleResultsFromExecuteQuery(SQLException sqle)
{
assertSQLState("Unexpected SQL state.", "X0Y78", sqle);
}
/**
* Raises an exception if the exception is not caused by
* <code>executeUpdate()</code> returning result sets.
*
* @param sqle a <code>SQLException</code> value
*/
private void assertResultsFromExecuteUpdate(SQLException sqle) {
assertSQLState("Unexpected SQL state.", "X0Y79", sqle);
}
// SETUP
/**
* Runs the test fixtures in embedded and client.
* @return test suite
*/
public static Test suite() {
BaseTestSuite suite = new BaseTestSuite("ProcedureTest");
suite.addTest(baseSuite("ProcedureTest:embedded"));
suite.addTest(
TestConfiguration.clientServerDecorator(
baseSuite("ProcedureTest:client")));
return suite;
}
/**
* Creates the test suite and wraps it in a <code>TestSetup</code>
* instance which sets up and tears down the test environment.
* @return test suite
*/
private static Test baseSuite(String name)
{
BaseTestSuite suite = new BaseTestSuite(name);
// Need JDBC DriverManager to run these tests
if (!JDBC.vmSupportsJDBC3())
return suite;
suite.addTestSuite(ProcedureTest.class);
return new CleanDatabaseTestSetup(suite) {
/**
* Creates the tables and the stored procedures used in the test
* cases.
* @exception SQLException if a database error occurs
*/
protected void decorateSQL(Statement s) throws SQLException
{
for (int i = 0; i < PROCEDURES.length; i++) {
s.execute(PROCEDURES[i]);
}
for (int i = 0; i < TABLES.length; i++) {
s.execute(TABLES[i][1]);
}
}
};
}
/**
* Sets up the connection for a test case and clears all tables
* used in the test cases.
* @exception SQLException if a database error occurs
*/
public void setUp() throws SQLException {
Connection conn = getConnection();
conn.setAutoCommit(false);
Statement s = createStatement();
for (int i = 0; i < TABLES.length; i++) {
s.execute("DELETE FROM " + TABLES[i][0]);
}
commit();
}
/**
* Procedures that should be created before the tests are run and
* dropped when the tests have finished. First element in each row
* is the name of the procedure, second element is SQL which
* creates it.
*/
private static final String[] PROCEDURES = {
"CREATE PROCEDURE RETRIEVE_DYNAMIC_RESULTS(number INT) " +
"LANGUAGE JAVA PARAMETER STYLE JAVA EXTERNAL NAME '" +
ProcedureTest.class.getName() + ".retrieveDynamicResults' " +
"DYNAMIC RESULT SETS 4",
"CREATE PROCEDURE RETRIEVE_CLOSED_RESULT() LANGUAGE JAVA " +
"PARAMETER STYLE JAVA EXTERNAL NAME '" +
ProcedureTest.class.getName() + ".retrieveClosedResult' " +
"DYNAMIC RESULT SETS 1",
"CREATE PROCEDURE RETRIEVE_EXTERNAL_RESULT(" +
"DBNAME VARCHAR(128), DBUSER VARCHAR(128), DBPWD VARCHAR(128)) LANGUAGE JAVA " +
"PARAMETER STYLE JAVA EXTERNAL NAME '" +
ProcedureTest.class.getName() + ".retrieveExternalResult' " +
"DYNAMIC RESULT SETS 1",
"CREATE PROCEDURE PROC_WITH_SIDE_EFFECTS(ret INT) LANGUAGE JAVA " +
"PARAMETER STYLE JAVA EXTERNAL NAME '" +
ProcedureTest.class.getName() + ".procWithSideEffects' " +
"DYNAMIC RESULT SETS 2",
"CREATE PROCEDURE NESTED_RESULT_SETS(proctext VARCHAR(128)) LANGUAGE JAVA " +
"PARAMETER STYLE JAVA EXTERNAL NAME '" +
ProcedureTest.class.getName() + ".nestedDynamicResultSets' " +
"DYNAMIC RESULT SETS 6",
"CREATE PROCEDURE INT_OUT(OUT X INTEGER) LANGUAGE JAVA " +
"PARAMETER STYLE JAVA EXTERNAL NAME '" +
ProcedureTest.class.getName() + ".intOut'",
};
/**
* Tables that should be created before the tests are run and
* dropped when the tests have finished. The tables will be
* cleared before each test case is run. First element in each row
* is the name of the table, second element is the SQL text which
* creates it.
*/
private static final String[][] TABLES = {
// SIMPLE_TABLE is used by PROC_WITH_SIDE_EFFECTS
{ "SIMPLE_TABLE", "CREATE TABLE SIMPLE_TABLE (id INT)" },
};
// PROCEDURES
/**
* Stored procedure which returns 0, 1, 2, 3 or 4 <code>ResultSet</code>s.
*
* @param number the number of <code>ResultSet</code>s to return
* @param rs1 first <code>ResultSet</code>
* @param rs2 second <code>ResultSet</code>
* @param rs3 third <code>ResultSet</code>
* @param rs4 fourth <code>ResultSet</code>
* @exception SQLException if a database error occurs
*/
public static void retrieveDynamicResults(int number,
ResultSet[] rs1,
ResultSet[] rs2,
ResultSet[] rs3,
ResultSet[] rs4)
throws SQLException
{
Connection c = DriverManager.getConnection("jdbc:default:connection");
if (number > 0) {
rs1[0] = c.createStatement().executeQuery("VALUES(1)");
}
if (number > 1) {
rs2[0] = c.createStatement().executeQuery("VALUES(1)");
}
if (number > 2) {
rs3[0] = c.createStatement().executeQuery("VALUES(1)");
}
if (number > 3) {
rs4[0] = c.createStatement().executeQuery("VALUES(1)");
}
c.close();
}
/**
* Stored procedure which produces a closed result set.
*
* @param closed holder for the closed result set
* @exception SQLException if a database error occurs
*/
public static void retrieveClosedResult(ResultSet[] closed)
throws SQLException
{
Connection c = DriverManager.getConnection("jdbc:default:connection");
closed[0] = c.createStatement().executeQuery("VALUES(1)");
closed[0].close();
c.close();
}
/**
* Stored procedure which produces a result set in another
* connection.
*
* @param external result set from another connection
* @exception SQLException if a database error occurs
*/
public static void retrieveExternalResult(String dbName,
String user, String password, ResultSet[] external)
throws SQLException
{
// Use a server-side connection to the same database.
String url = "jdbc:derby:" + dbName;
Connection conn = DriverManager.getConnection(url, user, password);
external[0] =
conn.createStatement().executeQuery("VALUES(1)");
}
/**
* Stored procedure which inserts a row into SIMPLE_TABLE and
* optionally returns result sets.
*
* @param returnResults if one, return one result set; if greater
* than one, return two result sets; otherwise, return no result
* set
* @param rs1 first result set to return
* @param rs2 second result set to return
* @exception SQLException if a database error occurs
*/
public static void procWithSideEffects(int returnResults,
ResultSet[] rs1,
ResultSet[] rs2)
throws SQLException
{
Connection c = DriverManager.getConnection("jdbc:default:connection");
Statement stmt = c.createStatement();
stmt.executeUpdate("INSERT INTO SIMPLE_TABLE VALUES (42)");
if (returnResults > 0) {
rs1[0] = c.createStatement().executeQuery("VALUES(1)");
}
if (returnResults > 1) {
rs2[0] = c.createStatement().executeQuery("VALUES(1)");
}
c.close();
}
/**
* Method for a Java procedure that calls another procedure
* and just passes on the dynamic results from that call.
*/
public static void nestedDynamicResultSets(String procedureText,
ResultSet[] rs1, ResultSet[] rs2, ResultSet[] rs3, ResultSet[] rs4,
ResultSet[] rs5, ResultSet[] rs6)
throws SQLException
{
Connection c = DriverManager.getConnection("jdbc:default:connection");
CallableStatement cs = c.prepareCall("CALL " + procedureText);
cs.execute();
// Mix up the order of the result sets in the returned
// parameters, ensures order is defined by creation
// and not parameter order.
rs6[0] = cs.getResultSet();
if (!cs.getMoreResults(Statement.KEEP_CURRENT_RESULT))
return;
rs3[0] = cs.getResultSet();
if (!cs.getMoreResults(Statement.KEEP_CURRENT_RESULT))
return;
rs4[0] = cs.getResultSet();
if (!cs.getMoreResults(Statement.KEEP_CURRENT_RESULT))
return;
rs2[0] = cs.getResultSet();
if (!cs.getMoreResults(Statement.KEEP_CURRENT_RESULT))
return;
rs1[0] = cs.getResultSet();
if (!cs.getMoreResults(Statement.KEEP_CURRENT_RESULT))
return;
rs5[0] = cs.getResultSet();
}
/**
* Stored procedure with an integer output parameter.
* @param out an output parameter
*/
public static void intOut(int[] out) {
out[0] = 42;
}
/**
* Procedure to test that INOUT args preserve their value when the
* procedure is re-executed (DERBY-2515). If you add a new datatype
* to Derby, you will need to add a new argument at the end of this
* procedure's signature.
*/
public static AllTypesTuple makeFirstAllTypesTuple() throws Exception
{
return new AllTypesTuple
(
1L,
new HarmonySerialBlob( new byte[] { (byte) 1, (byte) 1, (byte) 1 } ),
Boolean.TRUE,
"firstt",
new byte[] { (byte) 1, (byte) 1, (byte) 1 },
new HarmonySerialClob( "firstt" ),
new Date( 1L ),
new BigDecimal( "1" ),
1.0,
1 ,
new String( "firstt" ),
new byte[] { (byte) 1, (byte) 1, (byte) 1 },
1.0F,
1,
new Time( 1L ),
new Timestamp( 1L ),
new Price( "USD", new BigDecimal( "1" ), new Timestamp( 1 ) ),
"firstt",
new byte[] { (byte) 1, (byte) 1, (byte) 1 }
);
}
public static AllTypesTuple makeSecondAllTypesTuple() throws Exception
{
return new AllTypesTuple
(
2L,
new HarmonySerialBlob( new byte[] { (byte) 2, (byte) 2, (byte) 2 } ),
Boolean.FALSE,
"second",
new byte[] { (byte) 2, (byte) 2, (byte) 2 },
new HarmonySerialClob( "second" ),
new Date( 2L ),
new BigDecimal( "2" ),
2.0,
2,
new String( "second" ),
new byte[] { (byte) 2, (byte) 2, (byte) 2 },
2.0F,
2,
new Time( 2L ),
new Timestamp( 2L ),
new Price( "USD", new BigDecimal( "2" ), new Timestamp( 2 ) ),
"second",
new byte[] { (byte) 2, (byte) 2, (byte) 2 }
);
}
public static AllTypesTuple makeThirdAllTypesTuple() throws Exception
{
return new AllTypesTuple
(
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null
);
}
public static AllTypesTuple makeFourthAllTypesTuple() throws Exception
{
return makeFirstAllTypesTuple();
}
public static void proc_2515
(
int passNumber,
String[] message,
Long[] bigintArg,
Blob[] blobArg,
Boolean[] booleanArg,
String[] charArg,
byte[][] charForBitDataArg,
Clob[] clobArg,
Date[] dateArg,
BigDecimal[] decimalArg,
Double[] doubleArg,
Integer[] intArg,
String[] longVarcharArg,
byte[][] longVarcharForBitDataArg,
Float[] realArg,
Integer[] smallintArg,
Time[] timeArg,
Timestamp[] timestampArg,
Price[] priceArg,
String[] varcharArg,
byte[][] varcharForBitDataArg
)
throws Exception
{
AllTypesTuple actualCallSignature = new AllTypesTuple
(
bigintArg[ 0 ],
blobArg[ 0 ],
booleanArg[ 0 ],
charArg[ 0 ],
charForBitDataArg[ 0 ],
clobArg[ 0 ],
dateArg[ 0 ],
decimalArg[ 0 ],
doubleArg[ 0 ],
intArg[ 0 ],
longVarcharArg[ 0 ],
longVarcharForBitDataArg[ 0 ],
realArg[ 0 ],
smallintArg[ 0 ],
timeArg[ 0 ],
timestampArg[ 0 ],
priceArg[ 0 ],
varcharArg[ 0 ],
varcharForBitDataArg[ 0 ]
);
AllTypesTuple expectedCallSignature;
AllTypesTuple returnSignature;
switch( passNumber )
{
case 0:
expectedCallSignature = makeFirstAllTypesTuple();
returnSignature = makeSecondAllTypesTuple();
break;
case 1:
expectedCallSignature = makeSecondAllTypesTuple();
returnSignature = makeThirdAllTypesTuple();
break;
case 2:
default:
expectedCallSignature = makeThirdAllTypesTuple();
returnSignature = makeFourthAllTypesTuple();
break;
}
message[ 0 ] = expectedCallSignature.compare( actualCallSignature );
bigintArg[ 0 ] = returnSignature.get_bigintArg();
blobArg[ 0 ] = returnSignature.get_blobArg();
booleanArg[ 0 ] = returnSignature.get_booleanArg();
charArg[ 0 ] = returnSignature.get_charArg();
charForBitDataArg[ 0 ] = returnSignature.get_charForBitDataArg();
clobArg[ 0 ] = returnSignature.get_clobArg();
dateArg[ 0 ] = returnSignature.get_dateArg();
decimalArg[ 0 ] = returnSignature.get_decimalArg();
doubleArg[ 0 ] = returnSignature.get_doubleArg();
intArg[ 0 ] = returnSignature.get_intArg();
longVarcharArg[ 0 ] = returnSignature.get_longVarcharArg();
longVarcharForBitDataArg[ 0 ] = returnSignature.get_longVarcharForBitDataArg();
realArg[ 0 ] = returnSignature.get_realArg();
smallintArg[ 0 ] = returnSignature.get_smallintArg();
timeArg[ 0 ] = returnSignature.get_timeArg();
timestampArg[ 0 ] = returnSignature.get_timestampArg();
priceArg[ 0 ] = returnSignature.get_priceArg();
varcharArg[ 0 ] = returnSignature.get_varcharArg();
varcharForBitDataArg[ 0 ] = returnSignature.get_varcharForBitDataArg();
}
/**
* Test various combinations of getMoreResults
*
* @throws SQLException
*/
public void testGetMoreResults() throws SQLException {
Statement s = createStatement();
s.executeUpdate("create table MRS.FIVERS(i integer)");
PreparedStatement ps = prepareStatement("insert into MRS.FIVERS values (?)");
for (int i = 1; i <= 20; i++) {
ps.setInt(1, i);
ps.executeUpdate();
}
// create a procedure that returns 5 result sets.
s.executeUpdate("create procedure MRS.FIVEJP() parameter style JAVA READS SQL DATA dynamic result sets 5 language java external name 'org.apache.derbyTesting.functionTests.util.ProcedureTest.fivejp'");
CallableStatement cs = prepareCall("CALL MRS.FIVEJP()");
ResultSet[] allRS = new ResultSet[5];
defaultGetMoreResults(cs, allRS);
java.util.Arrays.fill(allRS, null);
closeCurrentGetMoreResults(cs, allRS);
java.util.Arrays.fill(allRS, null);
keepCurrentGetMoreResults(cs, allRS);
java.util.Arrays.fill(allRS, null);
mixedGetMoreResults(cs, allRS);
java.util.Arrays.fill(allRS, null);
checkExecuteClosesResults(cs, allRS);
java.util.Arrays.fill(allRS, null);
checkCSCloseClosesResults(cs,allRS);
java.util.Arrays.fill(allRS, null);
// a procedure that calls another procedure that returns
// dynamic result sets, see if the result sets are handled
// correctly through the nesting.
CallableStatement nestedCs = prepareCall(
"CALL NESTED_RESULT_SETS('MRS.FIVEJP()')");
defaultGetMoreResults(nestedCs, allRS);
}
/**
* Check that CallableStatement.execute() closes results
* @param cs
* @param allRS
* @throws SQLException
*/
private void checkExecuteClosesResults(CallableStatement cs, ResultSet[] allRS) throws SQLException {
//Fetching result sets with getMoreResults(Statement.KEEP_CURRENT_RESULT) and checking that cs.execute() closes them");
cs.execute();
int pass = 0;
do {
allRS[pass++] = cs.getResultSet();
assertSame(cs, allRS[pass-1].getStatement());
// expect everything to stay open.
} while (cs.getMoreResults(Statement.KEEP_CURRENT_RESULT));
//fetched all results
// All should still be open.
for (int i = 0; i < 5; i++)
JDBC.assertDrainResults(allRS[i]);
cs.execute();
// all should be closed.
for (int i = 0; i < 5; i++)
JDBC.assertClosed(allRS[i]);
}
/**
* Check that CallableStatement.close() closes results
* @param cs
* @param allRS
* @throws SQLException
*/
private void checkCSCloseClosesResults(CallableStatement cs, ResultSet[] allRS) throws SQLException {
cs.execute();
int pass = 0;
do {
allRS[pass++] = cs.getResultSet();
assertSame(cs, allRS[pass-1].getStatement());
// expect everything to stay open.
} while (cs.getMoreResults(Statement.KEEP_CURRENT_RESULT));
//fetched all results
// All should still be open.
for (int i = 0; i < 5; i++)
JDBC.assertDrainResults(allRS[i]);
cs.close();
// all should be closed.
for (int i = 0; i < 5; i++)
JDBC.assertClosed(allRS[i]);
}
private void mixedGetMoreResults(CallableStatement cs, ResultSet[] allRS) throws SQLException {
//Fetching result sets with getMoreResults(<mixture>)"
cs.execute();
//first two with KEEP_CURRENT_RESULT"
allRS[0] = cs.getResultSet();
assertSame(cs, allRS[0].getStatement());
boolean moreRS = cs.getMoreResults(Statement.KEEP_CURRENT_RESULT);
if (!moreRS)
fail("FAIL - no second result set");
allRS[1] = cs.getResultSet();
assertSame(cs, allRS[1].getStatement());
// two open
allRS[0].next();
assertEquals(2,allRS[0].getInt(1));
allRS[1].next();
assertEquals(3,allRS[1].getInt(1));
//third with CLOSE_CURRENT_RESULT"
moreRS = cs.getMoreResults(Statement.CLOSE_CURRENT_RESULT);
if (!moreRS)
fail("FAIL - no third result set");
// first and third open
allRS[2] = cs.getResultSet();
assertSame(cs, allRS[2].getStatement());
assertEquals(2,allRS[0].getInt(1));
JDBC.assertClosed(allRS[1]);
allRS[2].next();
assertEquals(4,allRS[2].getInt(1));
//fourth with KEEP_CURRENT_RESULT"
moreRS = cs.getMoreResults(Statement.KEEP_CURRENT_RESULT);
if (!moreRS)
fail("FAIL - no fourth result set");
allRS[3] = cs.getResultSet();
assertSame(cs, allRS[3].getStatement());
allRS[3].next();
// first, third and fourth open, second closed
assertEquals(2,allRS[0].getInt(1));
JDBC.assertClosed(allRS[1]);
assertEquals(4,allRS[2].getInt(1));
assertEquals(5,allRS[3].getInt(1));
//fifth with CLOSE_ALL_RESULTS"
moreRS = cs.getMoreResults(Statement.CLOSE_ALL_RESULTS);
if (!moreRS)
fail("FAIL - no fifth result set");
allRS[4] = cs.getResultSet();
assertSame(cs, allRS[4].getStatement());
allRS[4].next();
// only fifth open
JDBC.assertClosed(allRS[0]);
JDBC.assertClosed(allRS[1]);
JDBC.assertClosed(allRS[2]);
JDBC.assertClosed(allRS[3]);
assertEquals(6,allRS[4].getInt(1));
//no more results with with KEEP_CURRENT_RESULT"
moreRS = cs.getMoreResults(Statement.KEEP_CURRENT_RESULT);
if (moreRS)
fail("FAIL - too many result sets");
// only fifth open
JDBC.assertClosed(allRS[0]);
JDBC.assertClosed(allRS[1]);
JDBC.assertClosed(allRS[2]);
JDBC.assertClosed(allRS[3]);
assertEquals(6,allRS[4].getInt(1));
allRS[4].close();
}
/**
* Check getMoreResults(Statement.KEEP_CURRENT_RESULT)
*
* @param cs
* @param allRS
* @throws SQLException
*/
private void keepCurrentGetMoreResults(CallableStatement cs, ResultSet[] allRS) throws SQLException {
cs.execute();
for (int i = 0; i < 5; i++)
{
allRS[i] = cs.getResultSet();
assertSame(cs, allRS[i].getStatement());
allRS[i].next();
assertEquals(2+i, allRS[i].getInt(1));
if (i < 4)
assertTrue(cs.getMoreResults(Statement.KEEP_CURRENT_RESULT));
else
assertFalse(cs.getMoreResults(Statement.KEEP_CURRENT_RESULT));
}
// resultSets should still be open
for (int i = 0; i < 5; i++)
JDBC.assertDrainResults(allRS[i]);
}
private void closeCurrentGetMoreResults(CallableStatement cs, ResultSet[] allRS) throws SQLException {
cs.execute();
for (int i = 0; i < 5; i++)
{
allRS[i] = cs.getResultSet();
assertSame(cs, allRS[i].getStatement());
allRS[i].next();
assertEquals(2+i, allRS[i].getInt(1));
if (i < 4)
assertTrue(cs.getMoreResults(Statement.CLOSE_CURRENT_RESULT));
else
assertFalse(cs.getMoreResults(Statement.CLOSE_CURRENT_RESULT));
}
// verify resultSets are closed
for (int i = 0; i < 5; i++)
JDBC.assertClosed(allRS[i]);
}
/**
* Test default getMoreResults() closes result set.
* @param cs
* @param allRS
* @throws SQLException
*/
private void defaultGetMoreResults(CallableStatement cs, ResultSet[] allRS) throws SQLException {
// execute the procedure that returns 5 result sets and then use the various
// options of getMoreResults().
cs.execute();
for (int i = 0; i < 5; i++)
{
allRS[i] = cs.getResultSet();
assertSame(cs, allRS[i].getStatement());
allRS[i].next();
assertEquals(2+i, allRS[i].getInt(1));
if (i < 4)
assertTrue(cs.getMoreResults());
else
assertFalse(cs.getMoreResults());
}
// verify resultSets are closed
for (int i = 0; i < 5; i++)
JDBC.assertClosed(allRS[i]);
}
////////////////////////////////////////////
//
// Nested classes.
//
////////////////////////////////////////////
public static final class AllTypesTuple
{
private Long _bigintArg;
private Blob _blobArg;
private Boolean _booleanArg;
private String _charArg;
private byte[] _charForBitDataArg;
private Clob _clobArg;
private Date _dateArg;
private BigDecimal _decimalArg;
private Double _doubleArg;
private Integer _intArg;
private String _longVarcharArg;
private byte[] _longVarcharForBitDataArg;
private Float _realArg;
private Integer _smallintArg;
private Time _timeArg;
private Timestamp _timestampArg;
private Price _priceArg;
private String _varcharArg;
private byte[] _varcharForBitDataArg;
public AllTypesTuple
(
Long bigintArg,
Blob blobArg,
Boolean booleanArg,
String charArg,
byte[] charForBitDataArg,
Clob clobArg,
Date dateArg,
BigDecimal decimalArg,
Double doubleArg,
Integer intArg,
String longVarcharArg,
byte[] longVarcharForBitDataArg,
Float realArg,
Integer smallintArg,
Time timeArg,
Timestamp timestampArg,
Price priceArg,
String varcharArg,
byte[] varcharForBitDataArg
)
{
_bigintArg = bigintArg;
_blobArg = blobArg;
_booleanArg = booleanArg;
_charArg = charArg;
_charForBitDataArg = charForBitDataArg;
_clobArg = clobArg;
_dateArg = dateArg;
_decimalArg = decimalArg;
_doubleArg = doubleArg;
_intArg = intArg;
_longVarcharArg = longVarcharArg;
_longVarcharForBitDataArg = longVarcharForBitDataArg;
_realArg = realArg;
_smallintArg = smallintArg;
_timeArg = timeArg;
_timestampArg = timestampArg;
_priceArg = priceArg;
_varcharArg = varcharArg;
_varcharForBitDataArg = varcharForBitDataArg;
}
public Long get_bigintArg() { return _bigintArg; }
public Blob get_blobArg() { return _blobArg; }
public Boolean get_booleanArg() { return _booleanArg; }
public String get_charArg() { return _charArg; }
public byte[] get_charForBitDataArg() { return _charForBitDataArg; }
public Clob get_clobArg() { return _clobArg; }
public Date get_dateArg() { return _dateArg; }
public BigDecimal get_decimalArg() { return _decimalArg; }
public Double get_doubleArg() { return _doubleArg; }
public Integer get_intArg() { return _intArg; }
public String get_longVarcharArg() { return _longVarcharArg; }
public byte[] get_longVarcharForBitDataArg() { return _longVarcharForBitDataArg; }
public Float get_realArg() { return _realArg; }
public Integer get_smallintArg() { return _smallintArg; }
public Time get_timeArg() { return _timeArg; }
public Timestamp get_timestampArg() { return _timestampArg; }
public Price get_priceArg() { return _priceArg; }
public String get_varcharArg() { return _varcharArg; }
public byte[] get_varcharForBitDataArg() { return _varcharForBitDataArg; }
public String compare( AllTypesTuple that ) throws Exception
{
String message = "";
message = message + compare( "_bigintArg", this._bigintArg, that._bigintArg );
message = message + compare( "_blobArg", this.getBlobBytes(), that.getBlobBytes() );
message = message + compare( "_booleanArg", this._booleanArg, that._booleanArg );
message = message + compare( "_charArg", this._charArg, that._charArg );
message = message + compare( "_charForBitDataArg", this._charForBitDataArg, that._charForBitDataArg );
message = message + compare( "_clobArg", this.getClobString(), that.getClobString() );
message = message + compare( "_dateArg", this.getDateString(), that.getDateString() );
message = message + compare( "_decimalArg", this._decimalArg, that._decimalArg );
message = message + compare( "_doubleArg", this._doubleArg, that._doubleArg );
message = message + compare( "_intArg", this._intArg, that._intArg );
message = message + compare( "_longVarcharArg", this._longVarcharArg, that._longVarcharArg );
message = message + compare( "_longVarcharForBitDataArg", this._longVarcharForBitDataArg, that._longVarcharForBitDataArg );
message = message + compare( "_realArg", this._realArg, that._realArg );
message = message + compare( "_smallintArg", this._smallintArg, that._smallintArg );
message = message + compare( "_timeArg", this.getTimeString(), that.getTimeString() );
message = message + compare( "_timestampArg", this._timestampArg, that._timestampArg );
message = message + compare( "_priceArg", this._priceArg, that._priceArg );
message = message + compare( "_varcharArg", this._varcharArg, that._varcharArg );
message = message + compare( "_varcharForBitDataArg", this._varcharForBitDataArg, that._varcharForBitDataArg );
return message;
}
private byte[] getBlobBytes() throws Exception
{
if ( _blobArg == null ) { return null; }
else { return _blobArg.getBytes( 1, (int) _blobArg.length() ); }
}
private String getClobString() throws Exception
{
if ( _clobArg == null ) { return null; }
else { return _clobArg.getSubString( 1, (int) _clobArg.length() ); }
}
private String getDateString()
{
if ( _dateArg == null ) { return null; }
else { return _dateArg.toString(); }
}
private String getTimeString()
{
if ( _timeArg == null ) { return null; }
else { return _timeArg.toString(); }
}
private String compare( String argName, Object left, Object right )
{
if ( left == null )
{
if ( right == null ) { return ""; }
return (argName + ": left was null but right was " + right);
}
if ( right == null ) { return (argName + ": left = " + left + " but right is null" ); }
if ( left instanceof byte[] ) { return compareBytes( argName, (byte[]) left, (byte[]) right ); }
if ( left.equals( right ) ) { return ""; }
return (argName + ": left = " + left + " but right = " + right);
}
private String compareBytes( String argName, byte[] left, byte[] right )
{
int count = left.length;
if ( count != right.length )
{
return (argName + ": left count = " + count + " but right count = " + right.length );
}
for ( int i = 0; i < count; i++ )
{
if ( left[ i ] != right[ i ] )
{
return (argName + ": left[ " + i + " ] = " + left[ i ] + " but right[ " + i + " ] = " + right[ i ] );
}
}
return "";
}
}
}
|