File size: 70,334 Bytes
2795186 |
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 |
/*
Copyright 2002-2007 MySQL AB, 2008 Sun Microsystems
This program is free software; you can redistribute it and/or modify
it under the terms of version 2 of the GNU General Public License as
published by the Free Software Foundation.
There are special exceptions to the terms and conditions of the GPL
as it is applied to this software. View the full text of the
exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
software distribution.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package com.mysql.jdbc;
import java.io.InputStream;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Constructor;
import java.math.BigDecimal;
import java.net.URL;
import java.sql.Array;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Date;
import java.sql.ParameterMetaData;
import java.sql.Ref;
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 java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* Representation of stored procedures for JDBC
*
* @author Mark Matthews
* @version $Id: CallableStatement.java,v 1.1.2.1 2005/05/13 18:58:38 mmatthews
* Exp $
*/
public class CallableStatement extends PreparedStatement implements
java.sql.CallableStatement {
protected final static Constructor JDBC_4_CSTMT_2_ARGS_CTOR;
protected final static Constructor JDBC_4_CSTMT_4_ARGS_CTOR;
static {
if (Util.isJdbc4()) {
try {
JDBC_4_CSTMT_2_ARGS_CTOR = Class.forName(
"com.mysql.jdbc.JDBC4CallableStatement")
.getConstructor(
new Class[] { ConnectionImpl.class,
CallableStatementParamInfo.class });
JDBC_4_CSTMT_4_ARGS_CTOR = Class.forName(
"com.mysql.jdbc.JDBC4CallableStatement")
.getConstructor(
new Class[] { ConnectionImpl.class,
String.class, String.class,
Boolean.TYPE });
} catch (SecurityException e) {
throw new RuntimeException(e);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
} else {
JDBC_4_CSTMT_4_ARGS_CTOR = null;
JDBC_4_CSTMT_2_ARGS_CTOR = null;
}
}
protected class CallableStatementParam {
int desiredJdbcType;
int index;
int inOutModifier;
boolean isIn;
boolean isOut;
int jdbcType;
short nullability;
String paramName;
int precision;
int scale;
String typeName;
CallableStatementParam(String name, int idx, boolean in, boolean out,
int jdbcType, String typeName, int precision, int scale,
short nullability, int inOutModifier) {
this.paramName = name;
this.isIn = in;
this.isOut = out;
this.index = idx;
this.jdbcType = jdbcType;
this.typeName = typeName;
this.precision = precision;
this.scale = scale;
this.nullability = nullability;
this.inOutModifier = inOutModifier;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#clone()
*/
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
protected class CallableStatementParamInfo {
String catalogInUse;
boolean isFunctionCall;
String nativeSql;
int numParameters;
List parameterList;
Map parameterMap;
/**
* synchronized externally in checkReadOnlyProcedure()
*/
boolean isReadOnlySafeProcedure = false;
/**
* synchronized externally in checkReadOnlyProcedure()
*/
boolean isReadOnlySafeChecked = false;
/**
* Constructor that converts a full list of parameter metadata into one
* that only represents the placeholders present in the {CALL ()}.
*
* @param fullParamInfo the metadata for all parameters for this stored
* procedure or function.
*/
CallableStatementParamInfo(CallableStatementParamInfo fullParamInfo) {
this.nativeSql = originalSql;
this.catalogInUse = currentCatalog;
isFunctionCall = fullParamInfo.isFunctionCall;
int[] localParameterMap = placeholderToParameterIndexMap;
int parameterMapLength = localParameterMap.length;
this.isReadOnlySafeProcedure = fullParamInfo.isReadOnlySafeProcedure;
this.isReadOnlySafeChecked = fullParamInfo.isReadOnlySafeChecked;
parameterList = new ArrayList(fullParamInfo.numParameters);
parameterMap = new HashMap(fullParamInfo.numParameters);
if (isFunctionCall) {
// Take the return value
parameterList.add(fullParamInfo.parameterList.get(0));
}
int offset = isFunctionCall ? 1 : 0;
for (int i = 0; i < parameterMapLength; i++) {
if (localParameterMap[i] != 0) {
CallableStatementParam param = (CallableStatementParam)fullParamInfo.parameterList.get(localParameterMap[i] + offset);
parameterList.add(param);
parameterMap.put(param.paramName, param);
}
}
this.numParameters = parameterList.size();
}
CallableStatementParamInfo(java.sql.ResultSet paramTypesRs)
throws SQLException {
boolean hadRows = paramTypesRs.last();
this.nativeSql = originalSql;
this.catalogInUse = currentCatalog;
isFunctionCall = callingStoredFunction;
if (hadRows) {
this.numParameters = paramTypesRs.getRow();
this.parameterList = new ArrayList(this.numParameters);
this.parameterMap = new HashMap(this.numParameters);
paramTypesRs.beforeFirst();
addParametersFromDBMD(paramTypesRs);
} else {
this.numParameters = 0;
}
if (isFunctionCall) {
this.numParameters += 1;
}
}
private void addParametersFromDBMD(java.sql.ResultSet paramTypesRs)
throws SQLException {
int i = 0;
while (paramTypesRs.next()) {
String paramName = paramTypesRs.getString(4);
int inOutModifier = paramTypesRs.getInt(5);
boolean isOutParameter = false;
boolean isInParameter = false;
if (i == 0 && isFunctionCall) {
isOutParameter = true;
isInParameter = false;
} else if (inOutModifier == DatabaseMetaData.procedureColumnInOut) {
isOutParameter = true;
isInParameter = true;
} else if (inOutModifier == DatabaseMetaData.procedureColumnIn) {
isOutParameter = false;
isInParameter = true;
} else if (inOutModifier == DatabaseMetaData.procedureColumnOut) {
isOutParameter = true;
isInParameter = false;
}
int jdbcType = paramTypesRs.getInt(6);
String typeName = paramTypesRs.getString(7);
int precision = paramTypesRs.getInt(8);
int scale = paramTypesRs.getInt(10);
short nullability = paramTypesRs.getShort(12);
CallableStatementParam paramInfoToAdd = new CallableStatementParam(
paramName, i++, isInParameter, isOutParameter,
jdbcType, typeName, precision, scale, nullability,
inOutModifier);
this.parameterList.add(paramInfoToAdd);
this.parameterMap.put(paramName, paramInfoToAdd);
}
}
protected void checkBounds(int paramIndex) throws SQLException {
int localParamIndex = paramIndex - 1;
if ((paramIndex < 0) || (localParamIndex >= this.numParameters)) {
throw SQLError.createSQLException(
Messages.getString("CallableStatement.11") + paramIndex //$NON-NLS-1$
+ Messages.getString("CallableStatement.12") + numParameters //$NON-NLS-1$
+ Messages.getString("CallableStatement.13"), SQLError.SQL_STATE_ILLEGAL_ARGUMENT, getExceptionInterceptor()); //$NON-NLS-1$
}
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#clone()
*/
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
CallableStatementParam getParameter(int index) {
return (CallableStatementParam) this.parameterList.get(index);
}
CallableStatementParam getParameter(String name) {
return (CallableStatementParam) this.parameterMap.get(name);
}
public String getParameterClassName(int arg0) throws SQLException {
String mysqlTypeName = getParameterTypeName(arg0);
boolean isBinaryOrBlob = StringUtils.indexOfIgnoreCase(mysqlTypeName, "BLOB") != -1 ||
StringUtils.indexOfIgnoreCase(mysqlTypeName, "BINARY") != -1;
boolean isUnsigned = StringUtils.indexOfIgnoreCase(mysqlTypeName, "UNSIGNED") != -1;
int mysqlTypeIfKnown = 0;
if (StringUtils.startsWithIgnoreCase(mysqlTypeName, "MEDIUMINT")) {
mysqlTypeIfKnown = MysqlDefs.FIELD_TYPE_INT24;
}
return ResultSetMetaData.getClassNameForJavaType(getParameterType(arg0),
isUnsigned, mysqlTypeIfKnown, isBinaryOrBlob, false);
}
public int getParameterCount() throws SQLException {
if (this.parameterList == null) {
return 0;
}
return this.parameterList.size();
}
public int getParameterMode(int arg0) throws SQLException {
checkBounds(arg0);
return getParameter(arg0 - 1).inOutModifier;
}
public int getParameterType(int arg0) throws SQLException {
checkBounds(arg0);
return getParameter(arg0 - 1).jdbcType;
}
public String getParameterTypeName(int arg0) throws SQLException {
checkBounds(arg0);
return getParameter(arg0 - 1).typeName;
}
public int getPrecision(int arg0) throws SQLException {
checkBounds(arg0);
return getParameter(arg0 - 1).precision;
}
public int getScale(int arg0) throws SQLException {
checkBounds(arg0);
return getParameter(arg0 - 1).scale;
}
public int isNullable(int arg0) throws SQLException {
checkBounds(arg0);
return getParameter(arg0 - 1).nullability;
}
public boolean isSigned(int arg0) throws SQLException {
checkBounds(arg0);
return false;
}
Iterator iterator() {
return this.parameterList.iterator();
}
int numberOfParameters() {
return this.numParameters;
}
}
/**
* Can't implement this directly, as then you can't use callable statements
* on JDK-1.3.1, which unfortunately isn't EOL'd yet, and still present
* quite a bit out there in the wild (Websphere, FreeBSD, anyone?)
*/
protected class CallableStatementParamInfoJDBC3 extends CallableStatementParamInfo
implements ParameterMetaData {
CallableStatementParamInfoJDBC3(java.sql.ResultSet paramTypesRs)
throws SQLException {
super(paramTypesRs);
}
public CallableStatementParamInfoJDBC3(CallableStatementParamInfo paramInfo) {
super(paramInfo);
}
/**
* Returns true if this either implements the interface argument or is directly or indirectly a wrapper
* for an object that does. Returns false otherwise. If this implements the interface then return true,
* else if this is a wrapper then return the result of recursively calling <code>isWrapperFor</code> on the wrapped
* object. If this does not implement the interface and is not a wrapper, return false.
* This method should be implemented as a low-cost operation compared to <code>unwrap</code> so that
* callers can use this method to avoid expensive <code>unwrap</code> calls that may fail. If this method
* returns true then calling <code>unwrap</code> with the same argument should succeed.
*
* @param interfaces a Class defining an interface.
* @return true if this implements the interface or directly or indirectly wraps an object that does.
* @throws java.sql.SQLException if an error occurs while determining whether this is a wrapper
* for an object with the given interface.
* @since 1.6
*/
public boolean isWrapperFor(Class iface) throws SQLException {
checkClosed();
// This works for classes that aren't actually wrapping
// anything
return iface.isInstance(this);
}
/**
* Returns an object that implements the given interface to allow access to non-standard methods,
* or standard methods not exposed by the proxy.
* The result may be either the object found to implement the interface or a proxy for that object.
* If the receiver implements the interface then that is the object. If the receiver is a wrapper
* and the wrapped object implements the interface then that is the object. Otherwise the object is
* the result of calling <code>unwrap</code> recursively on the wrapped object. If the receiver is not a
* wrapper and does not implement the interface, then an <code>SQLException</code> is thrown.
*
* @param iface A Class defining an interface that the result must implement.
* @return an object that implements the interface. May be a proxy for the actual implementing object.
* @throws java.sql.SQLException If no object found that implements the interface
* @since 1.6
*/
public Object unwrap(Class iface) throws java.sql.SQLException {
try {
// This works for classes that aren't actually wrapping
// anything
return Util.cast(iface, this);
} catch (ClassCastException cce) {
throw SQLError.createSQLException("Unable to unwrap to " + iface.toString(),
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, getExceptionInterceptor());
}
}
}
private final static int NOT_OUTPUT_PARAMETER_INDICATOR = Integer.MIN_VALUE;
private final static String PARAMETER_NAMESPACE_PREFIX = "@com_mysql_jdbc_outparam_"; //$NON-NLS-1$
private static String mangleParameterName(String origParameterName) {
if (origParameterName == null) {
return null;
}
int offset = 0;
if (origParameterName.length() > 0
&& origParameterName.charAt(0) == '@') {
offset = 1;
}
StringBuffer paramNameBuf = new StringBuffer(PARAMETER_NAMESPACE_PREFIX
.length()
+ origParameterName.length());
paramNameBuf.append(PARAMETER_NAMESPACE_PREFIX);
paramNameBuf.append(origParameterName.substring(offset));
return paramNameBuf.toString();
}
private boolean callingStoredFunction = false;
private ResultSetInternalMethods functionReturnValueResults;
private boolean hasOutputParams = false;
// private List parameterList;
// private Map parameterMap;
private ResultSetInternalMethods outputParameterResults;
protected boolean outputParamWasNull = false;
private int[] parameterIndexToRsIndex;
protected CallableStatementParamInfo paramInfo;
private CallableStatementParam returnValueParam;
/**
* Creates a new CallableStatement
*
* @param conn
* the connection creating this statement
* @param paramInfo
* the SQL to prepare
*
* @throws SQLException
* if an error occurs
*/
public CallableStatement(ConnectionImpl conn,
CallableStatementParamInfo paramInfo) throws SQLException {
super(conn, paramInfo.nativeSql, paramInfo.catalogInUse);
this.paramInfo = paramInfo;
this.callingStoredFunction = this.paramInfo.isFunctionCall;
if (this.callingStoredFunction) {
this.parameterCount += 1;
}
this.retrieveGeneratedKeys = true; // not provided for in the JDBC spec
}
/**
* Creates a callable statement instance -- We need to provide factory-style methods
* so we can support both JDBC3 (and older) and JDBC4 runtimes, otherwise
* the class verifier complains when it tries to load JDBC4-only interface
* classes that are present in JDBC4 method signatures.
*/
protected static CallableStatement getInstance(ConnectionImpl conn, String sql,
String catalog, boolean isFunctionCall) throws SQLException {
if (!Util.isJdbc4()) {
return new CallableStatement(conn, sql, catalog, isFunctionCall);
}
return (CallableStatement) Util.handleNewInstance(
JDBC_4_CSTMT_4_ARGS_CTOR, new Object[] { conn, sql, catalog,
Boolean.valueOf(isFunctionCall) }, conn.getExceptionInterceptor());
}
/**
* Creates a callable statement instance -- We need to provide factory-style methods
* so we can support both JDBC3 (and older) and JDBC4 runtimes, otherwise
* the class verifier complains when it tries to load JDBC4-only interface
* classes that are present in JDBC4 method signatures.
*/
protected static CallableStatement getInstance(ConnectionImpl conn,
CallableStatementParamInfo paramInfo) throws SQLException {
if (!Util.isJdbc4()) {
return new CallableStatement(conn, paramInfo);
}
return (CallableStatement) Util.handleNewInstance(
JDBC_4_CSTMT_2_ARGS_CTOR, new Object[] { conn, paramInfo }, conn.getExceptionInterceptor());
}
private int[] placeholderToParameterIndexMap;
private void generateParameterMap() throws SQLException {
if (this.paramInfo == null) {
return;
}
// if the user specified some parameters as literals, we need to
// provide a map from the specified placeholders to the actual
// parameter numbers
int parameterCountFromMetaData = this.paramInfo.getParameterCount();
// Ignore the first ? if this is a stored function, it doesn't count
if (this.callingStoredFunction) {
parameterCountFromMetaData--;
}
if (this.paramInfo != null &&
this.parameterCount != parameterCountFromMetaData) {
this.placeholderToParameterIndexMap = new int[this.parameterCount];
int startPos = this.callingStoredFunction ? StringUtils.indexOfIgnoreCase(this.originalSql,
"SELECT") : StringUtils.indexOfIgnoreCase(this.originalSql, "CALL");
if (startPos != -1) {
int parenOpenPos = this.originalSql.indexOf('(', startPos + 4);
if (parenOpenPos != -1) {
int parenClosePos = StringUtils.indexOfIgnoreCaseRespectQuotes(parenOpenPos,
this.originalSql, ")", '\'', true);
if (parenClosePos != -1) {
List parsedParameters = StringUtils.split(this.originalSql.substring(parenOpenPos + 1, parenClosePos), ",", "'\"", "'\"", true);
int numParsedParameters = parsedParameters.size();
// sanity check
if (numParsedParameters != this.parameterCount) {
// bail?
}
int placeholderCount = 0;
for (int i = 0; i < numParsedParameters; i++) {
if (((String)parsedParameters.get(i)).equals("?")) {
this.placeholderToParameterIndexMap[placeholderCount++] = i;
}
}
}
}
}
}
}
/**
* Creates a new CallableStatement
*
* @param conn
* the connection creating this statement
* @param sql
* the SQL to prepare
* @param catalog
* the current catalog
*
* @throws SQLException
* if an error occurs
*/
public CallableStatement(ConnectionImpl conn, String sql, String catalog,
boolean isFunctionCall) throws SQLException {
super(conn, sql, catalog);
this.callingStoredFunction = isFunctionCall;
if (!this.callingStoredFunction) {
if (!StringUtils.startsWithIgnoreCaseAndWs(sql, "CALL")) {
// not really a stored procedure call
fakeParameterTypes(false);
} else {
determineParameterTypes();
}
generateParameterMap();
} else {
determineParameterTypes();
generateParameterMap();
this.parameterCount += 1;
}
this.retrieveGeneratedKeys = true; // not provided for in the JDBC spec
}
/*
* (non-Javadoc)
*
* @see java.sql.PreparedStatement#addBatch()
*/
public void addBatch() throws SQLException {
setOutParams();
super.addBatch();
}
private CallableStatementParam checkIsOutputParam(int paramIndex)
throws SQLException {
if (this.callingStoredFunction) {
if (paramIndex == 1) {
if (this.returnValueParam == null) {
this.returnValueParam = new CallableStatementParam("", 0,
false, true, Types.VARCHAR, "VARCHAR", 0, 0,
DatabaseMetaData.attributeNullableUnknown,
DatabaseMetaData.procedureColumnReturn);
}
return this.returnValueParam;
}
// Move to position in output result set
paramIndex--;
}
checkParameterIndexBounds(paramIndex);
int localParamIndex = paramIndex - 1;
if (this.placeholderToParameterIndexMap != null) {
localParamIndex = this.placeholderToParameterIndexMap[localParamIndex];
}
CallableStatementParam paramDescriptor = this.paramInfo
.getParameter(localParamIndex);
// We don't have reliable metadata in this case, trust
// the caller
if (this.connection.getNoAccessToProcedureBodies()) {
paramDescriptor.isOut = true;
paramDescriptor.isIn = true;
paramDescriptor.inOutModifier = DatabaseMetaData.procedureColumnInOut;
} else if (!paramDescriptor.isOut) {
throw SQLError.createSQLException(
Messages.getString("CallableStatement.9") + paramIndex //$NON-NLS-1$
+ Messages.getString("CallableStatement.10"), //$NON-NLS-1$
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, getExceptionInterceptor());
}
this.hasOutputParams = true;
return paramDescriptor;
}
/**
* DOCUMENT ME!
*
* @param paramIndex
*
* @throws SQLException
*/
private void checkParameterIndexBounds(int paramIndex) throws SQLException {
this.paramInfo.checkBounds(paramIndex);
}
/**
* Checks whether or not this statement is supposed to be providing
* streamable result sets...If output parameters are registered, the driver
* can not stream the results.
*
* @throws SQLException
* DOCUMENT ME!
*/
private void checkStreamability() throws SQLException {
if (this.hasOutputParams && createStreamingResultSet()) {
throw SQLError.createSQLException(Messages.getString("CallableStatement.14"), //$NON-NLS-1$
SQLError.SQL_STATE_DRIVER_NOT_CAPABLE, getExceptionInterceptor());
}
}
public synchronized void clearParameters() throws SQLException {
super.clearParameters();
try {
if (this.outputParameterResults != null) {
this.outputParameterResults.close();
}
} finally {
this.outputParameterResults = null;
}
}
/**
* Used to fake up some metadata when we don't have access to
* SHOW CREATE PROCEDURE or mysql.proc.
*
* @throws SQLException if we can't build the metadata.
*/
private void fakeParameterTypes(boolean isReallyProcedure) throws SQLException {
Field[] fields = new Field[13];
fields[0] = new Field("", "PROCEDURE_CAT", Types.CHAR, 0);
fields[1] = new Field("", "PROCEDURE_SCHEM", Types.CHAR, 0);
fields[2] = new Field("", "PROCEDURE_NAME", Types.CHAR, 0);
fields[3] = new Field("", "COLUMN_NAME", Types.CHAR, 0);
fields[4] = new Field("", "COLUMN_TYPE", Types.CHAR, 0);
fields[5] = new Field("", "DATA_TYPE", Types.SMALLINT, 0);
fields[6] = new Field("", "TYPE_NAME", Types.CHAR, 0);
fields[7] = new Field("", "PRECISION", Types.INTEGER, 0);
fields[8] = new Field("", "LENGTH", Types.INTEGER, 0);
fields[9] = new Field("", "SCALE", Types.SMALLINT, 0);
fields[10] = new Field("", "RADIX", Types.SMALLINT, 0);
fields[11] = new Field("", "NULLABLE", Types.SMALLINT, 0);
fields[12] = new Field("", "REMARKS", Types.CHAR, 0);
String procName = isReallyProcedure ? extractProcedureName() : null;
byte[] procNameAsBytes = null;
try {
procNameAsBytes = procName == null ? null : procName.getBytes("UTF-8");
} catch (UnsupportedEncodingException ueEx) {
procNameAsBytes = StringUtils.s2b(procName, this.connection);
}
ArrayList resultRows = new ArrayList();
for (int i = 0; i < this.parameterCount; i++) {
byte[][] row = new byte[13][];
row[0] = null; // PROCEDURE_CAT
row[1] = null; // PROCEDURE_SCHEM
row[2] = procNameAsBytes; // PROCEDURE/NAME
row[3] = StringUtils.s2b(String.valueOf(i), this.connection); // COLUMN_NAME
row[4] = StringUtils.s2b(String
.valueOf(DatabaseMetaData.procedureColumnIn),
this.connection);
row[5] = StringUtils.s2b(String.valueOf(Types.VARCHAR),
this.connection); // DATA_TYPE
row[6] = StringUtils.s2b("VARCHAR", this.connection); // TYPE_NAME
row[7] = StringUtils.s2b(Integer.toString(65535), this.connection); // PRECISION
row[8] = StringUtils.s2b(Integer.toString(65535), this.connection); // LENGTH
row[9] = StringUtils.s2b(Integer.toString(0), this.connection); // SCALE
row[10] = StringUtils.s2b(Integer.toString(10), this.connection); // RADIX
row[11] = StringUtils.s2b(Integer
.toString(DatabaseMetaData.procedureNullableUnknown),
this.connection); // nullable
row[12] = null;
resultRows.add(new ByteArrayRow(row, getExceptionInterceptor()));
}
java.sql.ResultSet paramTypesRs = DatabaseMetaData.buildResultSet(
fields, resultRows, this.connection);
convertGetProcedureColumnsToInternalDescriptors(paramTypesRs);
}
private void determineParameterTypes() throws SQLException {
if (this.connection.getNoAccessToProcedureBodies()) {
fakeParameterTypes(true);
return;
}
java.sql.ResultSet paramTypesRs = null;
try {
String procName = extractProcedureName();
java.sql.DatabaseMetaData dbmd = this.connection.getMetaData();
boolean useCatalog = false;
if (procName.indexOf(".") == -1) {
useCatalog = true;
}
paramTypesRs = dbmd.getProcedureColumns(this.connection
.versionMeetsMinimum(5, 0, 2)
&& useCatalog ? this.currentCatalog : null, null, procName,
"%"); //$NON-NLS-1$
convertGetProcedureColumnsToInternalDescriptors(paramTypesRs);
} finally {
SQLException sqlExRethrow = null;
if (paramTypesRs != null) {
try {
paramTypesRs.close();
} catch (SQLException sqlEx) {
sqlExRethrow = sqlEx;
}
paramTypesRs = null;
}
if (sqlExRethrow != null) {
throw sqlExRethrow;
}
}
}
private void convertGetProcedureColumnsToInternalDescriptors(java.sql.ResultSet paramTypesRs) throws SQLException {
if (!this.connection.isRunningOnJDK13()) {
this.paramInfo = new CallableStatementParamInfoJDBC3(
paramTypesRs);
} else {
this.paramInfo = new CallableStatementParamInfo(paramTypesRs);
}
}
/*
* (non-Javadoc)
*
* @see java.sql.PreparedStatement#execute()
*/
public boolean execute() throws SQLException {
boolean returnVal = false;
checkClosed();
checkStreamability();
synchronized (this.connection.getMutex()) {
setInOutParamsOnServer();
setOutParams();
returnVal = super.execute();
if (this.callingStoredFunction) {
this.functionReturnValueResults = this.results;
this.functionReturnValueResults.next();
this.results = null;
}
retrieveOutParams();
}
if (!this.callingStoredFunction) {
return returnVal;
}
// Functions can't return results
return false;
}
/*
* (non-Javadoc)
*
* @see java.sql.PreparedStatement#executeQuery()
*/
public java.sql.ResultSet executeQuery() throws SQLException {
checkClosed();
checkStreamability();
java.sql.ResultSet execResults = null;
synchronized (this.connection.getMutex()) {
setInOutParamsOnServer();
setOutParams();
execResults = super.executeQuery();
retrieveOutParams();
}
return execResults;
}
/*
* (non-Javadoc)
*
* @see java.sql.PreparedStatement#executeUpdate()
*/
public int executeUpdate() throws SQLException {
int returnVal = -1;
checkClosed();
checkStreamability();
if (this.callingStoredFunction) {
execute();
return -1;
}
synchronized (this.connection.getMutex()) {
setInOutParamsOnServer();
setOutParams();
returnVal = super.executeUpdate();
retrieveOutParams();
}
return returnVal;
}
private String extractProcedureName() throws SQLException {
String sanitizedSql = StringUtils.stripComments(this.originalSql,
"`\"'", "`\"'", true, false, true, true);
// TODO: Do this with less memory allocation
int endCallIndex = StringUtils.indexOfIgnoreCase(sanitizedSql,
"CALL "); //$NON-NLS-1$
int offset = 5;
if (endCallIndex == -1) {
endCallIndex = StringUtils.indexOfIgnoreCase(sanitizedSql,
"SELECT ");
offset = 7;
}
if (endCallIndex != -1) {
StringBuffer nameBuf = new StringBuffer();
String trimmedStatement = sanitizedSql.substring(
endCallIndex + offset).trim();
int statementLength = trimmedStatement.length();
for (int i = 0; i < statementLength; i++) {
char c = trimmedStatement.charAt(i);
if (Character.isWhitespace(c) || (c == '(') || (c == '?')) {
break;
}
nameBuf.append(c);
}
return nameBuf.toString();
}
throw SQLError.createSQLException(Messages.getString("CallableStatement.1"), //$NON-NLS-1$
SQLError.SQL_STATE_GENERAL_ERROR, getExceptionInterceptor());
}
/**
* Adds 'at' symbol to beginning of parameter names if needed.
*
* @param paramNameIn
* the parameter name to 'fix'
*
* @return the parameter name with an 'a' prepended, if needed
*
* @throws SQLException
* if the parameter name is null or empty.
*/
protected String fixParameterName(String paramNameIn) throws SQLException {
if ((paramNameIn == null) || (paramNameIn.length() == 0)) {
throw SQLError.createSQLException(
((Messages.getString("CallableStatement.0") + paramNameIn) == null) //$NON-NLS-1$
? Messages.getString("CallableStatement.15") : Messages.getString("CallableStatement.16"), SQLError.SQL_STATE_ILLEGAL_ARGUMENT,
getExceptionInterceptor()); //$NON-NLS-1$ //$NON-NLS-2$
}
if (this.connection.getNoAccessToProcedureBodies()) {
throw SQLError.createSQLException("No access to parameters by name when connection has been configured not to access procedure bodies",
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, getExceptionInterceptor());
}
return mangleParameterName(paramNameIn);
/*
* if (paramNameIn.startsWith("@")) { return paramNameIn; } else {
* StringBuffer paramNameBuf = new StringBuffer("@");
* paramNameBuf.append(paramNameIn);
*
* return paramNameBuf.toString(); }
*/
}
/**
* @see java.sql.CallableStatement#getArray(int)
*/
public synchronized Array getArray(int i) throws SQLException {
ResultSetInternalMethods rs = getOutputParameters(i);
Array retValue = rs.getArray(mapOutputParameterIndexToRsIndex(i));
this.outputParamWasNull = rs.wasNull();
return retValue;
}
/**
* @see java.sql.CallableStatement#getArray(java.lang.String)
*/
public synchronized Array getArray(String parameterName)
throws SQLException {
ResultSetInternalMethods rs = getOutputParameters(0); // definitely not going to be
// from ?=
Array retValue = rs.getArray(fixParameterName(parameterName));
this.outputParamWasNull = rs.wasNull();
return retValue;
}
/**
* @see java.sql.CallableStatement#getBigDecimal(int)
*/
public synchronized BigDecimal getBigDecimal(int parameterIndex)
throws SQLException {
ResultSetInternalMethods rs = getOutputParameters(parameterIndex);
BigDecimal retValue = rs
.getBigDecimal(mapOutputParameterIndexToRsIndex(parameterIndex));
this.outputParamWasNull = rs.wasNull();
return retValue;
}
/**
* DOCUMENT ME!
*
* @param parameterIndex
* DOCUMENT ME!
* @param scale
* DOCUMENT ME!
*
* @return DOCUMENT ME!
*
* @throws SQLException
* DOCUMENT ME!
*
* @see java.sql.CallableStatement#getBigDecimal(int, int)
* @deprecated
*/
public synchronized BigDecimal getBigDecimal(int parameterIndex, int scale)
throws SQLException {
ResultSetInternalMethods rs = getOutputParameters(parameterIndex);
BigDecimal retValue = rs.getBigDecimal(
mapOutputParameterIndexToRsIndex(parameterIndex), scale);
this.outputParamWasNull = rs.wasNull();
return retValue;
}
/**
* @see java.sql.CallableStatement#getBigDecimal(java.lang.String)
*/
public synchronized BigDecimal getBigDecimal(String parameterName)
throws SQLException {
ResultSetInternalMethods rs = getOutputParameters(0); // definitely not going to be
// from ?=
BigDecimal retValue = rs.getBigDecimal(fixParameterName(parameterName));
this.outputParamWasNull = rs.wasNull();
return retValue;
}
/**
* @see java.sql.CallableStatement#getBlob(int)
*/
public synchronized Blob getBlob(int parameterIndex) throws SQLException {
ResultSetInternalMethods rs = getOutputParameters(parameterIndex);
Blob retValue = rs
.getBlob(mapOutputParameterIndexToRsIndex(parameterIndex));
this.outputParamWasNull = rs.wasNull();
return retValue;
}
/**
* @see java.sql.CallableStatement#getBlob(java.lang.String)
*/
public synchronized Blob getBlob(String parameterName) throws SQLException {
ResultSetInternalMethods rs = getOutputParameters(0); // definitely not going to be
// from ?=
Blob retValue = rs.getBlob(fixParameterName(parameterName));
this.outputParamWasNull = rs.wasNull();
return retValue;
}
/**
* @see java.sql.CallableStatement#getBoolean(int)
*/
public synchronized boolean getBoolean(int parameterIndex)
throws SQLException {
ResultSetInternalMethods rs = getOutputParameters(parameterIndex);
boolean retValue = rs
.getBoolean(mapOutputParameterIndexToRsIndex(parameterIndex));
this.outputParamWasNull = rs.wasNull();
return retValue;
}
/**
* @see java.sql.CallableStatement#getBoolean(java.lang.String)
*/
public synchronized boolean getBoolean(String parameterName)
throws SQLException {
ResultSetInternalMethods rs = getOutputParameters(0); // definitely not going to be
// from ?=
boolean retValue = rs.getBoolean(fixParameterName(parameterName));
this.outputParamWasNull = rs.wasNull();
return retValue;
}
/**
* @see java.sql.CallableStatement#getByte(int)
*/
public synchronized byte getByte(int parameterIndex) throws SQLException {
ResultSetInternalMethods rs = getOutputParameters(parameterIndex);
byte retValue = rs
.getByte(mapOutputParameterIndexToRsIndex(parameterIndex));
this.outputParamWasNull = rs.wasNull();
return retValue;
}
/**
* @see java.sql.CallableStatement#getByte(java.lang.String)
*/
public synchronized byte getByte(String parameterName) throws SQLException {
ResultSetInternalMethods rs = getOutputParameters(0); // definitely not going to be
// from ?=
byte retValue = rs.getByte(fixParameterName(parameterName));
this.outputParamWasNull = rs.wasNull();
return retValue;
}
/**
* @see java.sql.CallableStatement#getBytes(int)
*/
public synchronized byte[] getBytes(int parameterIndex) throws SQLException {
ResultSetInternalMethods rs = getOutputParameters(parameterIndex);
byte[] retValue = rs
.getBytes(mapOutputParameterIndexToRsIndex(parameterIndex));
this.outputParamWasNull = rs.wasNull();
return retValue;
}
/**
* @see java.sql.CallableStatement#getBytes(java.lang.String)
*/
public synchronized byte[] getBytes(String parameterName)
throws SQLException {
ResultSetInternalMethods rs = getOutputParameters(0); // definitely not going to be
// from ?=
byte[] retValue = rs.getBytes(fixParameterName(parameterName));
this.outputParamWasNull = rs.wasNull();
return retValue;
}
/**
* @see java.sql.CallableStatement#getClob(int)
*/
public synchronized Clob getClob(int parameterIndex) throws SQLException {
ResultSetInternalMethods rs = getOutputParameters(parameterIndex);
Clob retValue = rs
.getClob(mapOutputParameterIndexToRsIndex(parameterIndex));
this.outputParamWasNull = rs.wasNull();
return retValue;
}
/**
* @see java.sql.CallableStatement#getClob(java.lang.String)
*/
public synchronized Clob getClob(String parameterName) throws SQLException {
ResultSetInternalMethods rs = getOutputParameters(0); // definitely not going to be
// from ?=
Clob retValue = rs.getClob(fixParameterName(parameterName));
this.outputParamWasNull = rs.wasNull();
return retValue;
}
/**
* @see java.sql.CallableStatement#getDate(int)
*/
public synchronized Date getDate(int parameterIndex) throws SQLException {
ResultSetInternalMethods rs = getOutputParameters(parameterIndex);
Date retValue = rs
.getDate(mapOutputParameterIndexToRsIndex(parameterIndex));
this.outputParamWasNull = rs.wasNull();
return retValue;
}
/**
* @see java.sql.CallableStatement#getDate(int, java.util.Calendar)
*/
public synchronized Date getDate(int parameterIndex, Calendar cal)
throws SQLException {
ResultSetInternalMethods rs = getOutputParameters(parameterIndex);
Date retValue = rs.getDate(
mapOutputParameterIndexToRsIndex(parameterIndex), cal);
this.outputParamWasNull = rs.wasNull();
return retValue;
}
/**
* @see java.sql.CallableStatement#getDate(java.lang.String)
*/
public synchronized Date getDate(String parameterName) throws SQLException {
ResultSetInternalMethods rs = getOutputParameters(0); // definitely not going to be
// from ?=
Date retValue = rs.getDate(fixParameterName(parameterName));
this.outputParamWasNull = rs.wasNull();
return retValue;
}
/**
* @see java.sql.CallableStatement#getDate(java.lang.String,
* java.util.Calendar)
*/
public synchronized Date getDate(String parameterName, Calendar cal)
throws SQLException {
ResultSetInternalMethods rs = getOutputParameters(0); // definitely not going to be
// from ?=
Date retValue = rs.getDate(fixParameterName(parameterName), cal);
this.outputParamWasNull = rs.wasNull();
return retValue;
}
/**
* @see java.sql.CallableStatement#getDouble(int)
*/
public synchronized double getDouble(int parameterIndex)
throws SQLException {
ResultSetInternalMethods rs = getOutputParameters(parameterIndex);
double retValue = rs
.getDouble(mapOutputParameterIndexToRsIndex(parameterIndex));
this.outputParamWasNull = rs.wasNull();
return retValue;
}
/**
* @see java.sql.CallableStatement#getDouble(java.lang.String)
*/
public synchronized double getDouble(String parameterName)
throws SQLException {
ResultSetInternalMethods rs = getOutputParameters(0); // definitely not going to be
// from ?=
double retValue = rs.getDouble(fixParameterName(parameterName));
this.outputParamWasNull = rs.wasNull();
return retValue;
}
/**
* @see java.sql.CallableStatement#getFloat(int)
*/
public synchronized float getFloat(int parameterIndex) throws SQLException {
ResultSetInternalMethods rs = getOutputParameters(parameterIndex);
float retValue = rs
.getFloat(mapOutputParameterIndexToRsIndex(parameterIndex));
this.outputParamWasNull = rs.wasNull();
return retValue;
}
/**
* @see java.sql.CallableStatement#getFloat(java.lang.String)
*/
public synchronized float getFloat(String parameterName)
throws SQLException {
ResultSetInternalMethods rs = getOutputParameters(0); // definitely not going to be
// from ?=
float retValue = rs.getFloat(fixParameterName(parameterName));
this.outputParamWasNull = rs.wasNull();
return retValue;
}
/**
* @see java.sql.CallableStatement#getInt(int)
*/
public synchronized int getInt(int parameterIndex) throws SQLException {
ResultSetInternalMethods rs = getOutputParameters(parameterIndex);
int retValue = rs
.getInt(mapOutputParameterIndexToRsIndex(parameterIndex));
this.outputParamWasNull = rs.wasNull();
return retValue;
}
/**
* @see java.sql.CallableStatement#getInt(java.lang.String)
*/
public synchronized int getInt(String parameterName) throws SQLException {
ResultSetInternalMethods rs = getOutputParameters(0); // definitely not going to be
// from ?=
int retValue = rs.getInt(fixParameterName(parameterName));
this.outputParamWasNull = rs.wasNull();
return retValue;
}
/**
* @see java.sql.CallableStatement#getLong(int)
*/
public synchronized long getLong(int parameterIndex) throws SQLException {
ResultSetInternalMethods rs = getOutputParameters(parameterIndex);
long retValue = rs
.getLong(mapOutputParameterIndexToRsIndex(parameterIndex));
this.outputParamWasNull = rs.wasNull();
return retValue;
}
/**
* @see java.sql.CallableStatement#getLong(java.lang.String)
*/
public synchronized long getLong(String parameterName) throws SQLException {
ResultSetInternalMethods rs = getOutputParameters(0); // definitely not going to be
// from ?=
long retValue = rs.getLong(fixParameterName(parameterName));
this.outputParamWasNull = rs.wasNull();
return retValue;
}
protected int getNamedParamIndex(String paramName, boolean forOut)
throws SQLException {
if (this.connection.getNoAccessToProcedureBodies()) {
throw SQLError.createSQLException("No access to parameters by name when connection has been configured not to access procedure bodies",
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, getExceptionInterceptor());
}
if ((paramName == null) || (paramName.length() == 0)) {
throw SQLError.createSQLException(Messages.getString("CallableStatement.2"), //$NON-NLS-1$
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, getExceptionInterceptor());
}
if (this.paramInfo == null) {
throw SQLError.createSQLException(
Messages.getString("CallableStatement.3") + paramName + Messages.getString("CallableStatement.4"), //$NON-NLS-1$ //$NON-NLS-2$
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, getExceptionInterceptor());
}
CallableStatementParam namedParamInfo = this.paramInfo
.getParameter(paramName);
if (forOut && !namedParamInfo.isOut) {
throw SQLError.createSQLException(
Messages.getString("CallableStatement.5") + paramName //$NON-NLS-1$
+ Messages.getString("CallableStatement.6"), //$NON-NLS-1$
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, getExceptionInterceptor());
}
if (this.placeholderToParameterIndexMap == null) {
return namedParamInfo.index + 1; // JDBC indices are 1-based
}
for (int i = 0; i < this.placeholderToParameterIndexMap.length; i++) {
if (this.placeholderToParameterIndexMap[i] == namedParamInfo.index) {
return i + 1;
}
}
throw SQLError.createSQLException("Can't find local placeholder mapping for parameter named \"" +
paramName + "\".", SQLError.SQL_STATE_ILLEGAL_ARGUMENT, getExceptionInterceptor());
}
/**
* @see java.sql.CallableStatement#getObject(int)
*/
public synchronized Object getObject(int parameterIndex)
throws SQLException {
CallableStatementParam paramDescriptor = checkIsOutputParam(parameterIndex);
ResultSetInternalMethods rs = getOutputParameters(parameterIndex);
Object retVal = rs.getObjectStoredProc(
mapOutputParameterIndexToRsIndex(parameterIndex),
paramDescriptor.desiredJdbcType);
this.outputParamWasNull = rs.wasNull();
return retVal;
}
/**
* @see java.sql.CallableStatement#getObject(int, java.util.Map)
*/
public synchronized Object getObject(int parameterIndex, Map map)
throws SQLException {
ResultSetInternalMethods rs = getOutputParameters(parameterIndex);
Object retVal = rs.getObject(
mapOutputParameterIndexToRsIndex(parameterIndex), map);
this.outputParamWasNull = rs.wasNull();
return retVal;
}
/**
* @see java.sql.CallableStatement#getObject(java.lang.String)
*/
public synchronized Object getObject(String parameterName)
throws SQLException {
ResultSetInternalMethods rs = getOutputParameters(0); // definitely not going to be
// from ?=
Object retValue = rs.getObject(fixParameterName(parameterName));
this.outputParamWasNull = rs.wasNull();
return retValue;
}
/**
* @see java.sql.CallableStatement#getObject(java.lang.String,
* java.util.Map)
*/
public synchronized Object getObject(String parameterName, Map map)
throws SQLException {
ResultSetInternalMethods rs = getOutputParameters(0); // definitely not going to be
// from ?=
Object retValue = rs.getObject(fixParameterName(parameterName), map);
this.outputParamWasNull = rs.wasNull();
return retValue;
}
/**
* Returns the ResultSet that holds the output parameters, or throws an
* appropriate exception if none exist, or they weren't returned.
*
* @return the ResultSet that holds the output parameters
*
* @throws SQLException
* if no output parameters were defined, or if no output
* parameters were returned.
*/
protected ResultSetInternalMethods getOutputParameters(int paramIndex) throws SQLException {
this.outputParamWasNull = false;
if (paramIndex == 1 && this.callingStoredFunction
&& this.returnValueParam != null) {
return this.functionReturnValueResults;
}
if (this.outputParameterResults == null) {
if (this.paramInfo.numberOfParameters() == 0) {
throw SQLError.createSQLException(Messages
.getString("CallableStatement.7"), //$NON-NLS-1$
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, getExceptionInterceptor());
}
throw SQLError.createSQLException(Messages.getString("CallableStatement.8"), //$NON-NLS-1$
SQLError.SQL_STATE_GENERAL_ERROR, getExceptionInterceptor());
}
return this.outputParameterResults;
}
public synchronized ParameterMetaData getParameterMetaData()
throws SQLException {
if (this.placeholderToParameterIndexMap == null) {
return (CallableStatementParamInfoJDBC3) this.paramInfo;
} else {
return new CallableStatementParamInfoJDBC3(this.paramInfo);
}
}
/**
* @see java.sql.CallableStatement#getRef(int)
*/
public synchronized Ref getRef(int parameterIndex) throws SQLException {
ResultSetInternalMethods rs = getOutputParameters(parameterIndex);
Ref retValue = rs
.getRef(mapOutputParameterIndexToRsIndex(parameterIndex));
this.outputParamWasNull = rs.wasNull();
return retValue;
}
/**
* @see java.sql.CallableStatement#getRef(java.lang.String)
*/
public synchronized Ref getRef(String parameterName) throws SQLException {
ResultSetInternalMethods rs = getOutputParameters(0); // definitely not going to be
// from ?=
Ref retValue = rs.getRef(fixParameterName(parameterName));
this.outputParamWasNull = rs.wasNull();
return retValue;
}
/**
* @see java.sql.CallableStatement#getShort(int)
*/
public synchronized short getShort(int parameterIndex) throws SQLException {
ResultSetInternalMethods rs = getOutputParameters(parameterIndex);
short retValue = rs
.getShort(mapOutputParameterIndexToRsIndex(parameterIndex));
this.outputParamWasNull = rs.wasNull();
return retValue;
}
/**
* @see java.sql.CallableStatement#getShort(java.lang.String)
*/
public synchronized short getShort(String parameterName)
throws SQLException {
ResultSetInternalMethods rs = getOutputParameters(0); // definitely not going to be
// from ?=
short retValue = rs.getShort(fixParameterName(parameterName));
this.outputParamWasNull = rs.wasNull();
return retValue;
}
/**
* @see java.sql.CallableStatement#getString(int)
*/
public synchronized String getString(int parameterIndex)
throws SQLException {
ResultSetInternalMethods rs = getOutputParameters(parameterIndex);
String retValue = rs
.getString(mapOutputParameterIndexToRsIndex(parameterIndex));
this.outputParamWasNull = rs.wasNull();
return retValue;
}
/**
* @see java.sql.CallableStatement#getString(java.lang.String)
*/
public synchronized String getString(String parameterName)
throws SQLException {
ResultSetInternalMethods rs = getOutputParameters(0); // definitely not going to be
// from ?=
String retValue = rs.getString(fixParameterName(parameterName));
this.outputParamWasNull = rs.wasNull();
return retValue;
}
/**
* @see java.sql.CallableStatement#getTime(int)
*/
public synchronized Time getTime(int parameterIndex) throws SQLException {
ResultSetInternalMethods rs = getOutputParameters(parameterIndex);
Time retValue = rs
.getTime(mapOutputParameterIndexToRsIndex(parameterIndex));
this.outputParamWasNull = rs.wasNull();
return retValue;
}
/**
* @see java.sql.CallableStatement#getTime(int, java.util.Calendar)
*/
public synchronized Time getTime(int parameterIndex, Calendar cal)
throws SQLException {
ResultSetInternalMethods rs = getOutputParameters(parameterIndex);
Time retValue = rs.getTime(
mapOutputParameterIndexToRsIndex(parameterIndex), cal);
this.outputParamWasNull = rs.wasNull();
return retValue;
}
/**
* @see java.sql.CallableStatement#getTime(java.lang.String)
*/
public synchronized Time getTime(String parameterName) throws SQLException {
ResultSetInternalMethods rs = getOutputParameters(0); // definitely not going to be
// from ?=
Time retValue = rs.getTime(fixParameterName(parameterName));
this.outputParamWasNull = rs.wasNull();
return retValue;
}
/**
* @see java.sql.CallableStatement#getTime(java.lang.String,
* java.util.Calendar)
*/
public synchronized Time getTime(String parameterName, Calendar cal)
throws SQLException {
ResultSetInternalMethods rs = getOutputParameters(0); // definitely not going to be
// from ?=
Time retValue = rs.getTime(fixParameterName(parameterName), cal);
this.outputParamWasNull = rs.wasNull();
return retValue;
}
/**
* @see java.sql.CallableStatement#getTimestamp(int)
*/
public synchronized Timestamp getTimestamp(int parameterIndex)
throws SQLException {
ResultSetInternalMethods rs = getOutputParameters(parameterIndex);
Timestamp retValue = rs
.getTimestamp(mapOutputParameterIndexToRsIndex(parameterIndex));
this.outputParamWasNull = rs.wasNull();
return retValue;
}
/**
* @see java.sql.CallableStatement#getTimestamp(int, java.util.Calendar)
*/
public synchronized Timestamp getTimestamp(int parameterIndex, Calendar cal)
throws SQLException {
ResultSetInternalMethods rs = getOutputParameters(parameterIndex);
Timestamp retValue = rs.getTimestamp(
mapOutputParameterIndexToRsIndex(parameterIndex), cal);
this.outputParamWasNull = rs.wasNull();
return retValue;
}
/**
* @see java.sql.CallableStatement#getTimestamp(java.lang.String)
*/
public synchronized Timestamp getTimestamp(String parameterName)
throws SQLException {
ResultSetInternalMethods rs = getOutputParameters(0); // definitely not going to be
// from ?=
Timestamp retValue = rs.getTimestamp(fixParameterName(parameterName));
this.outputParamWasNull = rs.wasNull();
return retValue;
}
/**
* @see java.sql.CallableStatement#getTimestamp(java.lang.String,
* java.util.Calendar)
*/
public synchronized Timestamp getTimestamp(String parameterName,
Calendar cal) throws SQLException {
ResultSetInternalMethods rs = getOutputParameters(0); // definitely not going to be
// from ?=
Timestamp retValue = rs.getTimestamp(fixParameterName(parameterName),
cal);
this.outputParamWasNull = rs.wasNull();
return retValue;
}
/**
* @see java.sql.CallableStatement#getURL(int)
*/
public synchronized URL getURL(int parameterIndex) throws SQLException {
ResultSetInternalMethods rs = getOutputParameters(parameterIndex);
URL retValue = rs
.getURL(mapOutputParameterIndexToRsIndex(parameterIndex));
this.outputParamWasNull = rs.wasNull();
return retValue;
}
/**
* @see java.sql.CallableStatement#getURL(java.lang.String)
*/
public synchronized URL getURL(String parameterName) throws SQLException {
ResultSetInternalMethods rs = getOutputParameters(0); // definitely not going to be
// from ?=
URL retValue = rs.getURL(fixParameterName(parameterName));
this.outputParamWasNull = rs.wasNull();
return retValue;
}
protected int mapOutputParameterIndexToRsIndex(int paramIndex)
throws SQLException {
if (this.returnValueParam != null && paramIndex == 1) {
return 1;
}
checkParameterIndexBounds(paramIndex);
int localParamIndex = paramIndex - 1;
if (this.placeholderToParameterIndexMap != null) {
localParamIndex = this.placeholderToParameterIndexMap[localParamIndex];
}
int rsIndex = this.parameterIndexToRsIndex[localParamIndex];
if (rsIndex == NOT_OUTPUT_PARAMETER_INDICATOR) {
throw SQLError.createSQLException(
Messages.getString("CallableStatement.21") + paramIndex //$NON-NLS-1$
+ Messages.getString("CallableStatement.22"), //$NON-NLS-1$
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, getExceptionInterceptor());
}
return rsIndex + 1;
}
/**
* @see java.sql.CallableStatement#registerOutParameter(int, int)
*/
public void registerOutParameter(int parameterIndex, int sqlType)
throws SQLException {
CallableStatementParam paramDescriptor = checkIsOutputParam(parameterIndex);
paramDescriptor.desiredJdbcType = sqlType;
}
/**
* @see java.sql.CallableStatement#registerOutParameter(int, int, int)
*/
public void registerOutParameter(int parameterIndex, int sqlType, int scale)
throws SQLException {
registerOutParameter(parameterIndex, sqlType);
}
/**
* @see java.sql.CallableStatement#registerOutParameter(int, int,
* java.lang.String)
*/
public void registerOutParameter(int parameterIndex, int sqlType,
String typeName) throws SQLException {
checkIsOutputParam(parameterIndex);
}
/**
* @see java.sql.CallableStatement#registerOutParameter(java.lang.String,
* int)
*/
public synchronized void registerOutParameter(String parameterName,
int sqlType) throws SQLException {
registerOutParameter(getNamedParamIndex(parameterName, true), sqlType);
}
/**
* @see java.sql.CallableStatement#registerOutParameter(java.lang.String,
* int, int)
*/
public void registerOutParameter(String parameterName, int sqlType,
int scale) throws SQLException {
registerOutParameter(getNamedParamIndex(parameterName, true), sqlType);
}
/**
* @see java.sql.CallableStatement#registerOutParameter(java.lang.String,
* int, java.lang.String)
*/
public void registerOutParameter(String parameterName, int sqlType,
String typeName) throws SQLException {
registerOutParameter(getNamedParamIndex(parameterName, true), sqlType,
typeName);
}
/**
* Issues a second query to retrieve all output parameters.
*
* @throws SQLException
* if an error occurs.
*/
private void retrieveOutParams() throws SQLException {
int numParameters = this.paramInfo.numberOfParameters();
this.parameterIndexToRsIndex = new int[numParameters];
for (int i = 0; i < numParameters; i++) {
this.parameterIndexToRsIndex[i] = NOT_OUTPUT_PARAMETER_INDICATOR;
}
int localParamIndex = 0;
if (numParameters > 0) {
StringBuffer outParameterQuery = new StringBuffer("SELECT "); //$NON-NLS-1$
boolean firstParam = true;
boolean hadOutputParams = false;
for (Iterator paramIter = this.paramInfo.iterator(); paramIter
.hasNext();) {
CallableStatementParam retrParamInfo = (CallableStatementParam) paramIter
.next();
if (retrParamInfo.isOut) {
hadOutputParams = true;
this.parameterIndexToRsIndex[retrParamInfo.index] = localParamIndex++;
String outParameterName = mangleParameterName(retrParamInfo.paramName);
if (!firstParam) {
outParameterQuery.append(","); //$NON-NLS-1$
} else {
firstParam = false;
}
if (!outParameterName.startsWith("@")) { //$NON-NLS-1$
outParameterQuery.append('@');
}
outParameterQuery.append(outParameterName);
}
}
if (hadOutputParams) {
// We can't use 'ourself' to execute this query, or any
// pending result sets would be overwritten
java.sql.Statement outParameterStmt = null;
java.sql.ResultSet outParamRs = null;
try {
outParameterStmt = this.connection.createStatement();
outParamRs = outParameterStmt
.executeQuery(outParameterQuery.toString());
this.outputParameterResults = ((com.mysql.jdbc.ResultSetInternalMethods) outParamRs)
.copy();
if (!this.outputParameterResults.next()) {
this.outputParameterResults.close();
this.outputParameterResults = null;
}
} finally {
if (outParameterStmt != null) {
outParameterStmt.close();
}
}
} else {
this.outputParameterResults = null;
}
} else {
this.outputParameterResults = null;
}
}
/**
* @see java.sql.CallableStatement#setAsciiStream(java.lang.String,
* java.io.InputStream, int)
*/
public void setAsciiStream(String parameterName, InputStream x, int length)
throws SQLException {
setAsciiStream(getNamedParamIndex(parameterName, false), x, length);
}
/**
* @see java.sql.CallableStatement#setBigDecimal(java.lang.String,
* java.math.BigDecimal)
*/
public void setBigDecimal(String parameterName, BigDecimal x)
throws SQLException {
setBigDecimal(getNamedParamIndex(parameterName, false), x);
}
/**
* @see java.sql.CallableStatement#setBinaryStream(java.lang.String,
* java.io.InputStream, int)
*/
public void setBinaryStream(String parameterName, InputStream x, int length)
throws SQLException {
setBinaryStream(getNamedParamIndex(parameterName, false), x, length);
}
/**
* @see java.sql.CallableStatement#setBoolean(java.lang.String, boolean)
*/
public void setBoolean(String parameterName, boolean x) throws SQLException {
setBoolean(getNamedParamIndex(parameterName, false), x);
}
/**
* @see java.sql.CallableStatement#setByte(java.lang.String, byte)
*/
public void setByte(String parameterName, byte x) throws SQLException {
setByte(getNamedParamIndex(parameterName, false), x);
}
/**
* @see java.sql.CallableStatement#setBytes(java.lang.String, byte[])
*/
public void setBytes(String parameterName, byte[] x) throws SQLException {
setBytes(getNamedParamIndex(parameterName, false), x);
}
/**
* @see java.sql.CallableStatement#setCharacterStream(java.lang.String,
* java.io.Reader, int)
*/
public void setCharacterStream(String parameterName, Reader reader,
int length) throws SQLException {
setCharacterStream(getNamedParamIndex(parameterName, false), reader,
length);
}
/**
* @see java.sql.CallableStatement#setDate(java.lang.String, java.sql.Date)
*/
public void setDate(String parameterName, Date x) throws SQLException {
setDate(getNamedParamIndex(parameterName, false), x);
}
/**
* @see java.sql.CallableStatement#setDate(java.lang.String, java.sql.Date,
* java.util.Calendar)
*/
public void setDate(String parameterName, Date x, Calendar cal)
throws SQLException {
setDate(getNamedParamIndex(parameterName, false), x, cal);
}
/**
* @see java.sql.CallableStatement#setDouble(java.lang.String, double)
*/
public void setDouble(String parameterName, double x) throws SQLException {
setDouble(getNamedParamIndex(parameterName, false), x);
}
/**
* @see java.sql.CallableStatement#setFloat(java.lang.String, float)
*/
public void setFloat(String parameterName, float x) throws SQLException {
setFloat(getNamedParamIndex(parameterName, false), x);
}
/**
*
*/
private void setInOutParamsOnServer() throws SQLException {
if (this.paramInfo.numParameters > 0) {
int parameterIndex = 0;
for (Iterator paramIter = this.paramInfo.iterator(); paramIter
.hasNext();) {
CallableStatementParam inParamInfo = (CallableStatementParam) paramIter
.next();
if (inParamInfo.isOut && inParamInfo.isIn) {
String inOutParameterName = mangleParameterName(inParamInfo.paramName);
StringBuffer queryBuf = new StringBuffer(
4 + inOutParameterName.length() + 1 + 1);
queryBuf.append("SET "); //$NON-NLS-1$
queryBuf.append(inOutParameterName);
queryBuf.append("=?"); //$NON-NLS-1$
PreparedStatement setPstmt = null;
try {
setPstmt = (PreparedStatement) this.connection
.clientPrepareStatement(queryBuf.toString());
byte[] parameterAsBytes = getBytesRepresentation(
inParamInfo.index);
if (parameterAsBytes != null) {
if (parameterAsBytes.length > 8
&& parameterAsBytes[0] == '_'
&& parameterAsBytes[1] == 'b'
&& parameterAsBytes[2] == 'i'
&& parameterAsBytes[3] == 'n'
&& parameterAsBytes[4] == 'a'
&& parameterAsBytes[5] == 'r'
&& parameterAsBytes[6] == 'y'
&& parameterAsBytes[7] == '\'') {
setPstmt.setBytesNoEscapeNoQuotes(1,
parameterAsBytes);
} else {
int sqlType = inParamInfo.desiredJdbcType;
switch (sqlType) {
case Types.BIT:
case Types.BINARY:
case Types.BLOB:
case Types.JAVA_OBJECT:
case Types.LONGVARBINARY:
case Types.VARBINARY:
setPstmt.setBytes(1, parameterAsBytes);
break;
default:
// the inherited PreparedStatement methods
// have already escaped and quoted these parameters
setPstmt.setBytesNoEscape(1, parameterAsBytes);
}
}
} else {
setPstmt.setNull(1, Types.NULL);
}
setPstmt.executeUpdate();
} finally {
if (setPstmt != null) {
setPstmt.close();
}
}
}
parameterIndex++;
}
}
}
/**
* @see java.sql.CallableStatement#setInt(java.lang.String, int)
*/
public void setInt(String parameterName, int x) throws SQLException {
setInt(getNamedParamIndex(parameterName, false), x);
}
/**
* @see java.sql.CallableStatement#setLong(java.lang.String, long)
*/
public void setLong(String parameterName, long x) throws SQLException {
setLong(getNamedParamIndex(parameterName, false), x);
}
/**
* @see java.sql.CallableStatement#setNull(java.lang.String, int)
*/
public void setNull(String parameterName, int sqlType) throws SQLException {
setNull(getNamedParamIndex(parameterName, false), sqlType);
}
/**
* @see java.sql.CallableStatement#setNull(java.lang.String, int,
* java.lang.String)
*/
public void setNull(String parameterName, int sqlType, String typeName)
throws SQLException {
setNull(getNamedParamIndex(parameterName, false), sqlType, typeName);
}
/**
* @see java.sql.CallableStatement#setObject(java.lang.String,
* java.lang.Object)
*/
public void setObject(String parameterName, Object x) throws SQLException {
setObject(getNamedParamIndex(parameterName, false), x);
}
/**
* @see java.sql.CallableStatement#setObject(java.lang.String,
* java.lang.Object, int)
*/
public void setObject(String parameterName, Object x, int targetSqlType)
throws SQLException {
setObject(getNamedParamIndex(parameterName, false), x, targetSqlType);
}
/**
* @see java.sql.CallableStatement#setObject(java.lang.String,
* java.lang.Object, int, int)
*/
public void setObject(String parameterName, Object x, int targetSqlType,
int scale) throws SQLException {
}
private void setOutParams() throws SQLException {
if (this.paramInfo.numParameters > 0) {
for (Iterator paramIter = this.paramInfo.iterator(); paramIter
.hasNext();) {
CallableStatementParam outParamInfo = (CallableStatementParam) paramIter
.next();
if (!this.callingStoredFunction && outParamInfo.isOut) {
String outParameterName = mangleParameterName(outParamInfo.paramName);
int outParamIndex;
if (this.placeholderToParameterIndexMap == null) {
outParamIndex = outParamInfo.index + 1;
} else {
outParamIndex = this.placeholderToParameterIndexMap[outParamInfo.index - 1 /* JDBC is 1-based */];
}
this.setBytesNoEscapeNoQuotes(outParamIndex,
StringUtils.getBytes(outParameterName,
this.charConverter, this.charEncoding,
this.connection
.getServerCharacterEncoding(),
this.connection.parserKnowsUnicode(), getExceptionInterceptor()));
}
}
}
}
/**
* @see java.sql.CallableStatement#setShort(java.lang.String, short)
*/
public void setShort(String parameterName, short x) throws SQLException {
setShort(getNamedParamIndex(parameterName, false), x);
}
/**
* @see java.sql.CallableStatement#setString(java.lang.String,
* java.lang.String)
*/
public void setString(String parameterName, String x) throws SQLException {
setString(getNamedParamIndex(parameterName, false), x);
}
/**
* @see java.sql.CallableStatement#setTime(java.lang.String, java.sql.Time)
*/
public void setTime(String parameterName, Time x) throws SQLException {
setTime(getNamedParamIndex(parameterName, false), x);
}
/**
* @see java.sql.CallableStatement#setTime(java.lang.String, java.sql.Time,
* java.util.Calendar)
*/
public void setTime(String parameterName, Time x, Calendar cal)
throws SQLException {
setTime(getNamedParamIndex(parameterName, false), x, cal);
}
/**
* @see java.sql.CallableStatement#setTimestamp(java.lang.String,
* java.sql.Timestamp)
*/
public void setTimestamp(String parameterName, Timestamp x)
throws SQLException {
setTimestamp(getNamedParamIndex(parameterName, false), x);
}
/**
* @see java.sql.CallableStatement#setTimestamp(java.lang.String,
* java.sql.Timestamp, java.util.Calendar)
*/
public void setTimestamp(String parameterName, Timestamp x, Calendar cal)
throws SQLException {
setTimestamp(getNamedParamIndex(parameterName, false), x, cal);
}
/**
* @see java.sql.CallableStatement#setURL(java.lang.String, java.net.URL)
*/
public void setURL(String parameterName, URL val) throws SQLException {
setURL(getNamedParamIndex(parameterName, false), val);
}
/**
* @see java.sql.CallableStatement#wasNull()
*/
public synchronized boolean wasNull() throws SQLException {
return this.outputParamWasNull;
}
public int[] executeBatch() throws SQLException {
if (this.hasOutputParams) {
throw SQLError.createSQLException("Can't call executeBatch() on CallableStatement with OUTPUT parameters",
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, getExceptionInterceptor());
}
return super.executeBatch();
}
protected int getParameterIndexOffset() {
if (this.callingStoredFunction) {
return -1;
}
return super.getParameterIndexOffset();
}
public void setAsciiStream(String parameterName, InputStream x) throws SQLException {
setAsciiStream(getNamedParamIndex(parameterName, false), x);
}
public void setAsciiStream(String parameterName, InputStream x, long length) throws SQLException {
setAsciiStream(getNamedParamIndex(parameterName, false), x, length);
}
public void setBinaryStream(String parameterName, InputStream x) throws SQLException {
setBinaryStream(getNamedParamIndex(parameterName, false), x);
}
public void setBinaryStream(String parameterName, InputStream x, long length) throws SQLException {
setBinaryStream(getNamedParamIndex(parameterName, false), x, length);
}
public void setBlob(String parameterName, Blob x) throws SQLException {
setBlob(getNamedParamIndex(parameterName, false), x);
}
public void setBlob(String parameterName, InputStream inputStream) throws SQLException {
setBlob(getNamedParamIndex(parameterName, false), inputStream);
}
public void setBlob(String parameterName, InputStream inputStream, long length) throws SQLException {
setBlob(getNamedParamIndex(parameterName, false), inputStream, length);
}
public void setCharacterStream(String parameterName, Reader reader) throws SQLException {
setCharacterStream(getNamedParamIndex(parameterName, false), reader);
}
public void setCharacterStream(String parameterName, Reader reader, long length) throws SQLException {
setCharacterStream(getNamedParamIndex(parameterName, false), reader, length);
}
public void setClob(String parameterName, Clob x) throws SQLException {
setClob(getNamedParamIndex(parameterName, false), x);
}
public void setClob(String parameterName, Reader reader) throws SQLException {
setClob(getNamedParamIndex(parameterName, false), reader);
}
public void setClob(String parameterName, Reader reader, long length) throws SQLException {
setClob(getNamedParamIndex(parameterName, false), reader, length);
}
public void setNCharacterStream(String parameterName, Reader value) throws SQLException {
setNCharacterStream(getNamedParamIndex(parameterName, false), value);
}
public void setNCharacterStream(String parameterName, Reader value, long length) throws SQLException {
setNCharacterStream(getNamedParamIndex(parameterName, false), value, length);
}
/**
* Check whether the stored procedure alters any data or is safe for read-only usage.
*
* @return true if procedure does not alter data
* @throws SQLException
*/
private boolean checkReadOnlyProcedure() throws SQLException {
if (this.connection.getNoAccessToProcedureBodies()) {
return false;
}
synchronized (this.paramInfo) {
if (this.paramInfo.isReadOnlySafeChecked) {
return this.paramInfo.isReadOnlySafeProcedure;
}
ResultSet rs = null;
PreparedStatement ps = null;
try {
String procName = extractProcedureName();
String catalog = this.currentCatalog;
if (procName.indexOf(".") != -1) {
catalog = procName.substring(0, procName.indexOf("."));
procName = procName.substring(procName.indexOf(".") + 1);
procName = new String(StringUtils.stripEnclosure(procName
.getBytes(), "`", "`"));
}
ps = ((DatabaseMetaData) this.connection
.getMetaData())
.prepareMetaDataSafeStatement("SELECT SQL_DATA_ACCESS FROM "
+ " information_schema.routines "
+ " WHERE routine_schema = ? "
+ " AND routine_name = ?");
ps.setString(1, catalog);
ps.setString(2, procName);
rs = ps.executeQuery();
if (rs.next()) {
String sqlDataAccess = rs.getString(1);
if ("READS SQL DATA".equalsIgnoreCase(sqlDataAccess)
|| "NO SQL".equalsIgnoreCase(sqlDataAccess)) {
synchronized (this.paramInfo) {
this.paramInfo.isReadOnlySafeChecked = true;
this.paramInfo.isReadOnlySafeProcedure = true;
}
return true;
}
}
} catch (SQLException e) {
// swallow the Exception
} finally {
if(rs != null){
rs.close();
}
if(ps != null){
ps.close();
}
}
this.paramInfo.isReadOnlySafeChecked = false;
this.paramInfo.isReadOnlySafeProcedure = false;
}
return false;
}
protected boolean checkReadOnlySafeStatement() throws SQLException {
return (super.checkReadOnlySafeStatement() || this.checkReadOnlyProcedure());
}
}
|