1 package in.co.daffodil.db.jdbc; 2 3 import java.math.*; 4 import java.sql.*; 5 import java.sql.Date ; 6 import java.util.*; 7 import com.daffodilwoods.daffodildb.client.*; 8 import com.daffodilwoods.daffodildb.server.serversystem.*; 9 import com.daffodilwoods.daffodildb.server.sql99.utils.*; 10 import com.daffodilwoods.database.resource.*; 11 import in.co.daffodil.db.general.*; 12 import com.daffodilwoods.daffodildb.server.sql99.dql.listenerevents.*; 13 14 51 public class DaffodilDBCallableStatement 52 extends DaffodilDBPreparedStatement 53 implements CallableStatement { 54 55 58 private HashMap parameterMap; 59 60 63 private HashMap outParametersTypeMap; 64 65 69 private Object [] outParameter; 70 71 76 private HashMap outParameterMap; 77 78 81 private Object outParameters; 82 83 84 85 private Object lastOutParameterRetrieved; 86 private int outParametersCount; 87 88 boolean flagIndexParameterUsed = false; 89 boolean flagNamedParameterUsed = false; 90 91 public DaffodilDBCallableStatement(DaffodilDBConnection connection, 92 String query) throws SQLException { 93 this(connection, query, ResultSet.TYPE_FORWARD_ONLY, 94 ResultSet.CONCUR_READ_ONLY); 95 } 96 97 public DaffodilDBCallableStatement(DaffodilDBConnection connection, 98 String query, int resultSetType, 99 int resultSetConcurrency) throws 100 SQLException { 101 this(connection, query, ResultSet.TYPE_FORWARD_ONLY, 102 ResultSet.CONCUR_READ_ONLY, 103 1 ); 104 } 105 106 public DaffodilDBCallableStatement(DaffodilDBConnection connection, 107 String query, int resultSetType, 108 int resultSetConcurrency, 109 int resultSetHoldability) throws 110 SQLException { 111 super(connection, query, resultSetType, resultSetConcurrency, 112 resultSetHoldability); 113 outParametersCount = 0; 114 } 115 116 142 143 public synchronized void registerOutParameter(int parameterIndex, int sqlType) throws 144 SQLException { 145 registerOutParameter(parameterIndex, new SQLType(sqlType)); 146 } 147 148 170 171 public synchronized void registerOutParameter(int parameterIndex, int sqlType, int scale) throws SQLException { 172 registerOutParameter(parameterIndex, new SQLType(sqlType, scale)); 173 } 174 175 188 public synchronized boolean wasNull() throws SQLException { 189 return lastOutParameterRetrieved == null; 190 } 191 192 212 213 public synchronized String getString(int parameterIndex) throws SQLException { 214 isValidOUTParameterIndex(parameterIndex); 215 checkTypeEqual(parameterIndex, "String"); 216 return outParameter[parameterIndex - 1] == null ? null : 217 outParameter[parameterIndex - 1].toString(); 218 } 219 220 231 232 public synchronized boolean getBoolean(int parameterIndex) throws SQLException { 233 isValidOUTParameterIndex(parameterIndex); 234 checkTypeEqual(parameterIndex, Types.BIT, -1); 235 try { 236 Boolean bool = (Boolean ) Utilities.convertObject(outParameter[ 237 parameterIndex - 1], Types.BIT); 238 return bool == null ? false : bool.booleanValue(); 239 } 240 catch (DException dxe) { 241 throw dxe.getSqlException(connection.getLocale()); 242 } 243 } 244 245 256 257 public synchronized byte getByte(int parameterIndex) throws SQLException { 258 isValidOUTParameterIndex(parameterIndex); 259 checkTypeEqual(parameterIndex, Types.TINYINT, -1); 260 try { 261 Integer bool = (Integer ) Utilities.convertObject(outParameter[ 262 parameterIndex - 1], Types.TINYINT); 263 return bool == null ? 0 : bool.byteValue(); 264 } 265 catch (DException dxe) { 266 throw dxe.getSqlException(connection.getLocale()); 267 } 268 } 269 270 281 282 public synchronized short getShort(int parameterIndex) throws SQLException { 283 isValidOUTParameterIndex(parameterIndex); 284 checkTypeEqual(parameterIndex, Types.SMALLINT, -1); 285 try { 286 Integer bool = (Integer ) Utilities.convertObject(outParameter[ 287 parameterIndex - 1], Types.SMALLINT); 288 return bool == null ? 0 : bool.shortValue(); 289 } 290 catch (DException dxe) { 291 throw dxe.getSqlException(connection.getLocale()); 292 } 293 } 294 295 306 307 public synchronized int getInt(int parameterIndex) throws SQLException { 308 isValidOUTParameterIndex(parameterIndex); 309 checkTypeEqual(parameterIndex, Types.INTEGER, -1); 310 try { 311 Integer bool = (Integer ) Utilities.convertObject(outParameter[ 312 parameterIndex - 1], Types.INTEGER); 313 return bool == null ? 0 : bool.intValue(); 314 } 315 catch (DException dxe) { 316 throw dxe.getSqlException(connection.getLocale()); 317 } 318 } 319 320 331 public synchronized long getLong(int parameterIndex) throws SQLException { 332 isValidOUTParameterIndex(parameterIndex); 333 checkTypeEqual(parameterIndex, Types.BIGINT, -1); 334 try { 335 Long bool = (Long ) Utilities.convertObject(outParameter[parameterIndex - 336 1], Types.BIGINT); 337 return bool == null ? 0 : bool.longValue(); 338 } 339 catch (DException dxe) { 340 throw dxe.getSqlException(connection.getLocale()); 341 } 342 } 343 344 355 356 public synchronized float getFloat(int parameterIndex) throws SQLException { 357 isValidOUTParameterIndex(parameterIndex); 358 checkTypeEqual(parameterIndex, Types.FLOAT, -1); 359 try { 360 Double value = (Double ) Utilities.convertObject(outParameter[ 361 parameterIndex - 1], Types.FLOAT); 362 return value == null ? 0 : value.floatValue(); 363 } 364 catch (DException dxe) { 365 throw dxe.getSqlException(connection.getLocale()); 366 } 367 } 368 369 379 public synchronized double getDouble(int parameterIndex) throws SQLException { 380 isValidOUTParameterIndex(parameterIndex); 381 checkTypeEqual(parameterIndex, Types.DOUBLE, -1); 382 try { 383 Double bool = (Double ) Utilities.convertObject(outParameter[ 384 parameterIndex - 1], Types.DOUBLE); 385 return bool == null ? 0 : bool.doubleValue(); 386 } 387 catch (DException dxe) { 388 throw dxe.getSqlException(connection.getLocale()); 389 } 390 } 391 392 406 public synchronized BigDecimal getBigDecimal(int parameterIndex, int scale) throws 407 SQLException { 408 isValidOUTParameterIndex(parameterIndex); 409 SQLType sqlType = checkTypeEqual(parameterIndex, Types.DECIMAL, scale); 410 scale = sqlType.getScale(); 411 try { 412 BigDecimal bd = (BigDecimal) Utilities.convertObject(outParameter[ 413 parameterIndex - 1], Types.DECIMAL); 414 return (scale == -1 || bd==null) ? bd : bd.setScale(scale, bd.ROUND_DOWN); 415 } 416 catch (DException dxe) { 417 throw dxe.getSqlException(connection.getLocale()); 418 } 419 } 420 421 432 433 public synchronized byte[] getBytes(int parameterIndex) throws SQLException { 434 isValidOUTParameterIndex(parameterIndex); 435 checkTypeEqual(parameterIndex, "Binary"); 436 try { 437 return (byte[]) Utilities.convertObject(outParameter[parameterIndex - 1], 438 Types.BINARY); 439 } 440 catch (DException dxe) { 441 throw dxe.getSqlException(connection.getLocale()); 442 } 443 } 444 445 455 456 public synchronized java.sql.Date getDate(int parameterIndex) throws SQLException { 457 return getDate(parameterIndex, null); 458 } 459 460 471 472 public synchronized java.sql.Time getTime(int parameterIndex) throws SQLException { 473 return getTime(parameterIndex, null); 474 } 475 476 487 488 public synchronized java.sql.Timestamp getTimestamp(int parameterIndex) throws 489 SQLException { 490 return getTimestamp(parameterIndex, null); 491 } 492 493 494 512 513 public synchronized Object getObject(int parameterIndex) throws SQLException { 514 return getObject(parameterIndex, null); 515 } 516 517 518 530 531 public synchronized BigDecimal getBigDecimal(int parameterIndex) throws SQLException { 532 return getBigDecimal(parameterIndex,-1 ); 533 } 534 535 552 553 public synchronized Object getObject(int parameterIndex, java.util.Map map) throws 554 SQLException { 555 isValidOUTParameterIndex(parameterIndex); 556 SQLType sqlType = checkTypeEqual(parameterIndex, Types.OTHER, -1); 557 558 try { 559 return Utilities.convertObject(outParameter[parameterIndex - 1], 560 sqlType.getType() ); 561 } 562 catch (DException dxe) { 563 throw dxe.getSqlException(connection.getLocale()); 564 } 565 } 566 567 580 581 public synchronized Ref getRef(int parameterIndex) throws SQLException { 582 DException dex = new DException("DSE16", new Object [] {"getRef"}); 583 throw dex.getSqlException(connection.getLocale()); 584 } 585 586 596 597 public synchronized Blob getBlob(int parameterIndex) throws SQLException { 598 isValidOUTParameterIndex(parameterIndex); 599 checkTypeEqual(parameterIndex, Types.BLOB, -1); 600 try { 601 return (Blob) Utilities.convertObject(outParameter[parameterIndex - 1], 602 Types.BLOB); 603 } 604 catch (DException dxe) { 605 throw dxe.getSqlException(connection.getLocale()); 606 } 607 } 608 609 620 621 public synchronized Clob getClob(int parameterIndex) throws SQLException { 622 isValidOUTParameterIndex(parameterIndex); 623 checkTypeEqual(parameterIndex, Types.CLOB, -1); 624 try { 625 return (Clob) Utilities.convertObject(outParameter[parameterIndex - 1], 626 Types.CLOB); 627 } 628 catch (DException dxe) { 629 throw dxe.getSqlException(connection.getLocale()); 630 } 631 } 632 633 645 646 public synchronized Array getArray(int parameterIndex) throws SQLException { 647 DException dex = new DException("DSE22", new Object [] {"User-defined type"}); 648 throw dex.getSqlException(connection.getLocale()); 649 } 650 651 671 672 public synchronized java.sql.Date getDate(int parameterIndex, Calendar cal) throws 673 SQLException { 674 isValidOUTParameterIndex(parameterIndex); 675 checkTypeEqual(parameterIndex, Types.DATE, -1); 676 try { 677 java.sql.Date date = (Date ) Utilities.convertObject(outParameter[ 678 parameterIndex - 1], Types.DATE); 679 if (date == null || cal == null) 680 return date; 681 cal.setTime(date); 682 return new com.daffodilwoods.daffodildb.utils.DBDate(cal.getTime().getTime() 683 ); 684 } 685 catch (DException dxe) { 686 throw dxe.getSqlException(connection.getLocale()); 687 } 688 } 689 690 710 711 public synchronized java.sql.Time getTime(int parameterIndex, Calendar cal) throws 712 SQLException { 713 try { 714 isValidOUTParameterIndex(parameterIndex); 715 checkTypeEqual(parameterIndex, Types.TIME, -1); 716 Time tmObj = (Time) Utilities.convertObject(outParameter[parameterIndex - 717 1], Types.TIME); 718 if (tmObj == null) 719 return null; 720 Date timeObject = new com.daffodilwoods.daffodildb.utils.DBDate(tmObj.getTime()); 721 if (cal == null) { 722 Time time = new Time(timeObject.getTime()); 723 return time; 724 } 725 cal.setTime(timeObject); 726 Time calTime = new Time(cal.getTime().getTime() 727 ); 729 return calTime; 730 } 731 catch (Exception e) { 732 DaffodilDBExceptionHandler.handle("error in getTime ", "", e, 733 connection.getLocale()); 734 } 735 return null; 736 } 737 738 759 760 public synchronized java.sql.Timestamp getTimestamp(int parameterIndex, Calendar cal) throws 761 SQLException { 762 try { 763 isValidOUTParameterIndex(parameterIndex); 764 checkTypeEqual(parameterIndex, Types.TIME, -1); 765 Timestamp tmObj = (Timestamp) Utilities.convertObject(outParameter[ 766 parameterIndex - 1], Types.TIMESTAMP); 767 if (tmObj == null) 768 return null; 769 Date timeObject = new com.daffodilwoods.daffodildb.utils.DBDate(tmObj.getTime()); 770 if (cal == null) { 771 Timestamp time = new Timestamp(timeObject.getTime()); 772 return time; 773 } 774 cal.setTime(timeObject); 775 Timestamp calTime = new Timestamp(cal.getTime().getTime()); 776 return calTime; 777 } 778 catch (Exception e) { 779 DaffodilDBExceptionHandler.handle("error in getPropertyInfo", "", e, 780 connection.getLocale()); 781 return null; 782 } 783 } 784 785 821 822 public synchronized void registerOutParameter(int paramIndex, int sqlType, String typeName) throws 823 SQLException { 824 DException dex = new DException("DSE16", 825 new Object [] {"registerOutParameter"}); 826 throw dex.getSqlException(connection.getLocale()); 827 } 828 829 830 854 855 public synchronized void registerOutParameter(String parameterName, int sqlType) throws 856 SQLException { 857 registerOutParameter(parameterName, new SQLType(sqlType)); 858 } 859 860 881 882 public synchronized void registerOutParameter(String parameterName, int sqlType, int scale) throws 883 SQLException { 884 registerOutParameter(parameterName, new SQLType(sqlType, scale)); 885 } 886 887 923 924 public synchronized void registerOutParameter(String parameterName, int sqlType, 925 String typeName) throws SQLException { 926 DException dex = new DException("DSE16", 927 new Object [] {"registerOutParameter"}); 928 throw dex.getSqlException(connection.getLocale()); 929 } 930 931 946 public synchronized java.net.URL getURL(int parameterIndex) throws SQLException { 947 try { 948 isValidOUTParameterIndex(parameterIndex); 949 checkTypeEqual(parameterIndex, 70 , -1); 950 String url = (String ) Utilities.convertObject(outParameter[parameterIndex - 951 1], Types.CHAR); 952 if (url == null) 953 return null; 954 return new java.net.URL (url); 955 } 956 catch (Exception E) { 957 throw new DException("DSE538",null).getSqlException(connection.getLocale()); 958 } 959 } 960 961 975 public synchronized void setURL(String parameterName, java.net.URL val) throws 976 SQLException { 977 DException dex = new DException("DSE16", new Object [] {"setURL"}); 978 throw dex.getSqlException(connection.getLocale()); 979 } 980 981 991 992 public synchronized void setNull(String parameterName, int sqlType) throws SQLException { 993 setObject(parameterName, null, sqlType); 994 } 995 996 1025 1026 public synchronized void setNull(String parameterName, int sqlType, String typeName) throws SQLException { 1027 setObject(parameterName, null, sqlType); 1028 } 1029 1030 1041 1042 public synchronized void setBoolean(String parameterName, boolean x) throws SQLException { 1043 setObject(parameterName, Utilities.getBooleanValue(x)); 1044 } 1045 1046 1057 public synchronized void setByte(String parameterName, byte x) throws SQLException { 1058 setObject(parameterName, new Byte (x)); 1059 } 1060 1061 1072 1073 public synchronized void setShort(String parameterName, short x) throws SQLException { 1074 setObject(parameterName, new Short (x)); 1075 } 1076 1077 1088 1089 public synchronized void setInt(String parameterName, int x) throws SQLException { 1090 setObject(parameterName, new Integer (x)); 1091 } 1092 1093 1104 1105 public synchronized void setLong(String parameterName, long x) throws SQLException { 1106 setObject(parameterName, new Long (x)); 1107 } 1108 1109 1120 1121 public synchronized void setFloat(String parameterName, float x) throws SQLException { 1122 setObject(parameterName, new Float (x)); 1123 } 1124 1125 1136 1137 public synchronized void setDouble(String parameterName, double x) throws SQLException { 1138 setObject(parameterName, new Double (x)); 1139 } 1140 1141 1153 1154 public synchronized void setBigDecimal(String parameterName, BigDecimal x) throws 1155 SQLException { 1156 setObject(parameterName, x); 1157 } 1158 1159 1173 1174 public synchronized void setString(String parameterName, String x) throws SQLException { 1175 setObject(parameterName, x); 1176 } 1177 1178 1193 1194 public synchronized void setBytes(String parameterName, byte x[]) throws SQLException { 1195 setObject(parameterName, x); 1196 } 1197 1198 1209 1210 public synchronized void setDate(String parameterName, Date x) throws SQLException { 1211 setObject(parameterName, x); 1212 } 1213 1214 1225 1226 public synchronized void setTime(String parameterName, java.sql.Time x) throws 1227 SQLException { 1228 setObject(parameterName, x); 1229 } 1230 1231 1243 1244 public synchronized void setTimestamp(String parameterName, java.sql.Timestamp x) throws 1245 SQLException { 1246 setObject(parameterName, x); 1247 } 1248 1249 1268 1269 public synchronized void setAsciiStream(String parameterName, java.io.InputStream x, 1270 int length) throws SQLException { 1271 byte[] bytes = new byte[length]; 1272 try { 1273 x.read(bytes, 0, length); 1274 } 1275 catch (java.io.IOException E) { 1276 DException dex = new DException("DSE356", null); 1277 throw dex.getSqlException(connection.getLocale()); 1278 } 1279 setObject(parameterName, bytes); 1280 } 1281 1282 1300 1301 public synchronized void setBinaryStream(String parameterName, java.io.InputStream x, 1302 int length) throws SQLException { 1303 byte[] bytes = new byte[length]; 1304 try { 1305 x.read(bytes, 0, length); 1306 } 1307 catch (java.io.IOException E) { 1308 DException dex = new DException("DSE356", null); 1309 throw dex.getSqlException(connection.getLocale()); 1310 } 1311 setObject(parameterName, bytes); 1312 } 1313 1314 1346 1347 public synchronized void setObject(String parameterName, Object x, int targetSqlType, 1348 int scale) throws SQLException { 1349 try { 1350 setObject(parameterName, 1351 Utilities.convertObject(x, new SQLType(targetSqlType, scale))); 1352 } 1353 catch (DException e) { 1354 throw e.getSqlException(connection.getLocale()); 1355 } 1356 } 1357 1358 1371 public void setObject(String parameterName, Object x, int targetSqlType) throws 1372 SQLException { 1373 try { 1374 setObject(parameterName, 1375 Utilities.convertObject(x, new SQLType(targetSqlType))); 1376 } 1377 catch (DException e) { 1378 throw e.getSqlException(connection.getLocale()); 1379 } 1380 } 1381 1382 1414 1415 public synchronized void setObject(String parameterName, Object x) throws SQLException { 1416 if (flagIndexParameterUsed) { 1417 DException dex = new DException("DSE212", null); 1418 throw dex.getSqlException(connection.getLocale()); 1419 } 1420 if (parameterMap == null) 1421 parameterMap = new HashMap(parametersCount); 1422 parameterMap.put(parameterName, x); 1423 flagNamedParameterUsed = true; 1424 } 1425 1426 1446 1447 public synchronized void setCharacterStream(String parameterName, java.io.Reader reader, 1448 int length) throws SQLException { 1449 char[] bytes = new char[length]; 1450 try { 1451 reader.read(bytes, 0, length); 1452 setObject(parameterName, Utilities.convertCharsToBytes(bytes)); 1453 } 1454 catch (Exception E) { 1455 DException dex = new DException("DSE356", null); 1456 throw dex.getSqlException(connection.getLocale()); 1457 } 1458 } 1459 1460 1478 1479 public synchronized void setDate(String parameterName, java.sql.Date x, Calendar cal) throws 1480 SQLException { 1481 cal.setTime(x); 1482 java.sql.Date date = new com.daffodilwoods.daffodildb.utils.DBDate(cal.getTime().getTime()); 1483 setObject(parameterName, date); 1484 } 1485 1486 1504 1505 public synchronized void setTime(String parameterName, java.sql.Time x, Calendar cal) throws 1506 SQLException { 1507 cal.setTime(x); 1508 Time time = new Time(cal.getTime().getTime()); 1509 setObject(parameterName, time); 1510 } 1511 1512 1530 1531 public synchronized void setTimestamp(String parameterName, java.sql.Timestamp x, 1532 Calendar cal) throws SQLException { 1533 cal.setTime(x); 1534 Timestamp timeStamp = new Timestamp(cal.getTime().getTime()); 1535 setObject(parameterName, timeStamp); 1536 } 1537 1538 1555 1556 public synchronized String getString(String parameterName) throws SQLException { 1557 isValidOUTParameterName(parameterName); 1558 checkTypeEqual(parameterName, "String"); 1559 return parameterMap.get(parameterName) == null ? null : 1560 parameterMap.get(parameterName).toString(); 1561 } 1562 1563 1573 1574 public synchronized boolean getBoolean(String parameterName) throws SQLException { 1575 isValidOUTParameterName(parameterName); 1576 checkTypeEqual(parameterName, Types.BIT, -1); 1577 try { 1578 Boolean bool = (Boolean ) Utilities.convertObject(parameterMap.get( 1579 parameterName), Types.BIT); 1580 return bool == null ? false : bool.booleanValue(); 1581 } 1582 catch (DException e) { 1583 throw e.getSqlException(connection.getLocale()); 1584 } 1585 } 1586 1587 1597 1598 public synchronized byte getByte(String parameterName) throws SQLException { 1599 isValidOUTParameterName(parameterName); 1600 checkTypeEqual(parameterName, Types.TINYINT, -1); 1601 try { 1602 Integer bool = (Integer ) Utilities.convertObject(parameterMap.get( 1603 parameterName), Types.TINYINT); 1604 return bool == null ? 0 : bool.byteValue(); 1605 } 1606 catch (DException e) { 1607 throw e.getSqlException(connection.getLocale()); 1608 } 1609 } 1610 1611 1621 1622 public synchronized short getShort(String parameterName) throws SQLException { 1623 isValidOUTParameterName(parameterName); 1624 checkTypeEqual(parameterName, Types.SMALLINT, -1); 1625 try { 1626 Integer bool = (Integer ) Utilities.convertObject(parameterMap.get( 1627 parameterName), Types.SMALLINT); 1628 return bool == null ? 0 : bool.shortValue(); 1629 } 1630 catch (DException e) { 1631 throw e.getSqlException(connection.getLocale()); 1632 } 1633 } 1634 1635 1646 1647 public synchronized int getInt(String parameterName) throws SQLException { 1648 isValidOUTParameterName(parameterName); 1649 checkTypeEqual(parameterName, Types.INTEGER, -1); 1650 try { 1651 Integer bool = (Integer ) Utilities.convertObject(parameterMap.get( 1652 parameterName), Types.INTEGER); 1653 return bool == null ? 0 : bool.intValue(); 1654 } 1655 catch (DException e) { 1656 throw e.getSqlException(connection.getLocale()); 1657 } 1658 } 1659 1660 1671 1672 public synchronized long getLong(String parameterName) throws SQLException { 1673 isValidOUTParameterName(parameterName); 1674 checkTypeEqual(parameterName, Types.BIGINT, -1); 1675 try { 1676 Long bool = (Long ) Utilities.convertObject(parameterMap.get(parameterName), 1677 Types.BIGINT); 1678 return bool == null ? 0 : bool.longValue(); 1679 } 1680 catch (DException e) { 1681 throw e.getSqlException(connection.getLocale()); 1682 } 1683 } 1684 1685 1695 1696 public synchronized float getFloat(String parameterName) throws SQLException { 1697 isValidOUTParameterName(parameterName); 1698 checkTypeEqual(parameterName, Types.FLOAT, -1); 1699 try { 1700 Float bool = (Float ) Utilities.convertObject(parameterMap.get( 1701 parameterName), Types.FLOAT); 1702 return bool == null ? 0 : bool.floatValue(); 1703 } 1704 catch (DException e) { 1705 throw e.getSqlException(connection.getLocale()); 1706 } 1707 } 1708 1709 1719 1720 public synchronized double getDouble(String parameterName) throws SQLException { 1721 isValidOUTParameterName(parameterName); 1722 checkTypeEqual(parameterName, Types.DOUBLE, -1); 1723 try { 1724 Double bool = (Double ) Utilities.convertObject(parameterMap.get( 1725 parameterName), Types.DOUBLE); 1726 return bool == null ? 0 : bool.doubleValue(); 1727 } 1728 catch (DException e) { 1729 throw e.getSqlException(connection.getLocale()); 1730 } 1731 } 1732 1733 1744 1745 public synchronized byte[] getBytes(String parameterName) throws SQLException { 1746 isValidOUTParameterName(parameterName); 1747 checkTypeEqual(parameterName, "Binary"); 1748 try { 1749 return (byte[]) Utilities.convertObject(parameterMap.get(parameterName), 1750 Types.BINARY); 1751 } 1752 catch (DException e) { 1753 throw e.getSqlException(connection.getLocale()); 1754 } 1755 } 1756 1757 1767 1768 public synchronized java.sql.Date getDate(String parameterName) throws SQLException { 1769 return getDate(parameterName, null); 1770 } 1771 1772 1782 1783 public synchronized java.sql.Time getTime(String parameterName) throws SQLException { 1784 return getTime(parameterName, null); 1785 } 1786 1787 1797 1798 public synchronized java.sql.Timestamp getTimestamp(String parameterName) throws 1799 SQLException { 1800 return getTimestamp(parameterName, null); 1801 } 1802 1803 1820 1821 public synchronized Object getObject(String parameterName) throws SQLException { 1822 return getObject(parameterName, null); 1823 } 1824 1825 1836 1837 public synchronized BigDecimal getBigDecimal(String parameterName) throws SQLException { 1838 isValidOUTParameterName(parameterName); 1839 checkTypeEqual(parameterName, Types.DECIMAL, -1); 1840 try { 1841 return (BigDecimal) Utilities.convertObject(parameterMap.get( 1842 parameterName), Types.DECIMAL); 1843 } 1844 catch (DException e) { 1845 throw e.getSqlException(connection.getLocale()); 1846 } 1847 } 1848 1849 1866 public synchronized Object getObject(String parameterName, java.util.Map map) throws 1867 SQLException { 1868 isValidOUTParameterName(parameterName); 1869 SQLType sqlType = checkTypeEqual(parameterName, Types.OTHER, -1); 1870 1871 try { 1872 return Utilities.convertObject(parameterMap.get(parameterName), 1873 sqlType.getType()); 1874 } 1875 catch (DException e) { 1876 throw e.getSqlException(connection.getLocale()); 1877 } 1878 } 1879 1880 1893 1894 public synchronized Ref getRef(String parameterName) throws SQLException { 1895 DException dex = new DException("DSE16", new Object [] {"getRef"}); 1896 throw dex.getSqlException(connection.getLocale()); 1897 } 1898 1899 1910 1911 public synchronized Blob getBlob(String parameterName) throws SQLException { 1912 isValidOUTParameterName(parameterName); 1913 checkTypeEqual(parameterName, Types.BLOB, -1); 1914 try { 1915 return (Blob) Utilities.convertObject(parameterMap.get(parameterName), 1916 Types.BLOB); 1917 } 1918 catch (DException e) { 1919 throw e.getSqlException(connection.getLocale()); 1920 } 1921 } 1922 1923 1933 1934 public synchronized Clob getClob(String parameterName) throws SQLException { 1935 isValidOUTParameterName(parameterName); 1936 checkTypeEqual(parameterName, Types.CLOB, -1); 1937 try { 1938 return (Clob) Utilities.convertObject(parameterMap.get(parameterName), 1939 Types.CLOB); 1940 } 1941 catch (DException e) { 1942 throw e.getSqlException(connection.getLocale()); 1943 } 1944 } 1945 1946 1957 1958 public synchronized Array getArray(String parameterName) throws SQLException { 1959 DException dex = new DException("DSE16", new Object [] {"getArray"}); 1960 throw dex.getSqlException(connection.getLocale()); 1961 } 1962 1963 1982 1983 public synchronized java.sql.Date getDate(String parameterName, Calendar cal) throws 1984 SQLException { 1985 isValidOUTParameterName(parameterName); 1986 checkTypeEqual(parameterName, Types.DATE, -1); 1987 try { 1988 java.sql.Date date = (Date ) Utilities.convertObject(parameterMap.get( 1989 parameterName), Types.DATE); 1990 if (date == null || cal == null) 1991 return date; 1992 cal.setTime(date); 1993 return new com.daffodilwoods.daffodildb.utils.DBDate(cal.getTime().getTime() 1994 ); 1995 } 1996 catch (DException e) { 1997 throw e.getSqlException(connection.getLocale()); 1998 } 1999 } 2000 2001 2020 2021 public synchronized java.sql.Time getTime(String parameterName, Calendar cal) throws 2022 SQLException { 2023 try { 2024 isValidOUTParameterName(parameterName); 2025 checkTypeEqual(parameterName, Types.TIME, -1); 2026 Time tmObj = (Time) Utilities.convertObject(parameterMap.get( 2027 parameterName), Types.TIME); 2028 if (tmObj == null) 2029 return null; 2030 Date timeObject = new com.daffodilwoods.daffodildb.utils.DBDate(tmObj.getTime()); 2031 if (cal == null) { 2032 Time time = new Time(timeObject.getTime()); 2033 return time; 2034 } 2035 cal.setTime(timeObject); 2036 Time calTime = new Time(cal.getTime().getTime() 2037 ); 2039 return calTime; 2040 } 2041 catch (Exception e) { 2042 DaffodilDBExceptionHandler.handle("", "", e, connection.getLocale()); 2043 return null; 2044 } 2045 } 2046 2047 2068 2069 public synchronized java.sql.Timestamp getTimestamp(String parameterName, Calendar cal) throws 2070 SQLException { 2071 try { 2072 isValidOUTParameterName(parameterName); 2073 checkTypeEqual(parameterName, Types.TIMESTAMP, -1); 2074 Timestamp tmObj = (Timestamp) Utilities.convertObject(parameterMap.get( 2075 parameterName), Types.TIMESTAMP); 2076 if (tmObj == null) 2077 return null; 2078 Date timeStamp = new com.daffodilwoods.daffodildb.utils.DBDate(tmObj.getTime()); 2079 if (cal == null) 2080 return new Timestamp(timeStamp.getTime()); 2081 cal.setTime(timeStamp); 2082 Timestamp calTimeStamp = new Timestamp(cal.getTime().getTime()); 2083 return calTimeStamp; 2084 } 2085 catch (Exception e) { 2086 DaffodilDBExceptionHandler.handle("", "", e, connection.getLocale()); 2087 return null; 2088 } 2089 } 2090 2091 2104 2105 public synchronized java.net.URL getURL(String parameterName) throws SQLException { 2106 DException dex = new DException("DSE16", new Object [] {"getURL"}); 2107 throw dex.getSqlException(connection.getLocale()); 2108 } 2109 2110 2114 2115 public synchronized void addBatch() throws SQLException { 2116 if(serverPreparedStatement.getQueryType() != _PreparedStatement.callstatement) { 2117 super.addBatch(); 2118 return; 2119 } 2120 checkConnection(); 2121 checkIfAllParametersSet(); 2122 if (parameterBatch == null) 2123 parameterBatch = new ArrayList(4); 2124 if (flagNamedParameterUsed) { 2125 parameterBatch.add(parameterMap); 2126 parameterMap = new HashMap(); 2127 } 2128 else{ 2129 parameterBatch.add(parameters); 2130 parameters = new Object [parametersCount]; 2131 parametersSet = new boolean[parametersCount]; 2132 Arrays.fill(parametersSet, false); 2133 } 2134 } 2135 2136 public synchronized int[] executeBatch() throws SQLException { 2137 if(serverPreparedStatement.getQueryType() != _PreparedStatement.callstatement) { 2138 return super.executeBatch(); 2139 } 2140 checkConnection(); 2141 if (parameterBatch == null) 2142 return new int[0]; 2143 createEnvironmentToExecute(false); 2144 outParameters = null; 2145 outParameter = null; 2146 outParameterMap = null; 2147 int size = parameterBatch.size(); 2148 int[] abc = new int[size]; 2149 java.util.Arrays.fill(abc, -3); 2150 Object obj = null; 2151 synchronized (connection){ 2152 2153 for (int i = 0; i < size; i++) { 2154 try { 2155 Object o = parameterBatch.get(i); 2156 if (o instanceof HashMap) { 2157 obj = serverPreparedStatement.execute( (HashMap) o, queryTimeOut); 2158 } 2159 else 2160 obj = serverPreparedStatement.execute( (Object []) parameterBatch. 2161 get( 2162 i), queryTimeOut); 2163 CallResult callResult = (CallResult) obj; 2164 if (callResult.getResult()instanceof _SelectIterator) { 2165 DException dex = new DException("DSE401", null); 2166 throw dex.getSqlException(connection.getLocale()); 2167 } 2168 abc[i] = callResult.getResult().hashCode(); 2169 abc[i] = abc[i] == Integer.MIN_VALUE ? 0 : abc[i]; 2170 } 2171 catch (DException E) { 2172 clearBatch(); 2173 throw new BatchUpdateException(E.getMessage(), abc); 2174 } 2175 } 2176 } 2177 clearBatch(); 2178 return abc; 2179 } 2180 2181 public synchronized ResultSet executeQuery() throws SQLException { 2182 if(serverPreparedStatement.getQueryType() != _PreparedStatement.callstatement) { 2183 return super.executeQuery(); 2184 } 2185 execute(); 2186 return resultSet; 2187 } 2188 2189 public synchronized int executeUpdate() throws SQLException { 2190 if(serverPreparedStatement.getQueryType() != _PreparedStatement.callstatement) { 2191 return super.executeUpdate(); 2192 } 2193 execute(); 2194 return updateCount; 2195 } 2196 2197 2201 public synchronized boolean execute() throws SQLException { 2202 if(serverPreparedStatement.getQueryType() != _PreparedStatement.callstatement ) { 2203 return super.execute(); 2204 } 2205 2206 checkConnection(); 2207 writeToLog(query); 2208 createEnvironmentToExecute(true); 2209 checkIfAllParametersSet(); 2210 if(userDefinedFunction && outParametersTypeMap == null ){ 2211 DException dex = new DException("DSE1157", null); 2212 throw dex.getSqlException(connection.getLocale()); 2213 } 2214 2215 outParameter = null; 2216 outParameters = null; 2217 outParameterMap = null; 2218 updateCount = -1; 2219 currentResultIndex = 0; 2220 resultSet = null; 2221 2222 Object result; 2223 try { 2224 synchronized (connection) { 2225 if (flagNamedParameterUsed) { 2226 2230 result = serverPreparedStatement.execute(parameterMap, queryTimeOut); 2231 } 2232 else { 2233 if(userDefinedFunction){ 2234 Object paramObj[] =new Object [parameters.length -1 ]; 2235 System.arraycopy(parameters,1,paramObj,0,parameters.length -1 ); 2236 result = serverPreparedStatement.execute(paramObj, queryTimeOut); 2237 2238 } 2239 else{ 2240 result = serverPreparedStatement.execute(parameters, queryTimeOut); 2241 } 2242 2243 } 2244 } 2245 } 2246 catch (DException e) { 2247 throw e.getSqlException(connection.getLocale()); 2248 } 2249 if (result == null) { 2250 executeResult = null; 2251 return false; 2252 } 2253 try { 2254 CallResult callResult = (CallResult) result; 2255 outParameters = callResult.getOUTParameters(); 2256 if (outParameters instanceof HashMap) 2257 outParameterMap = (HashMap) outParameters; 2258 else 2259 outParameter = (Object []) outParameters; 2260 2261 if(userDefinedFunction) { 2262 outParameter = new Object [1]; 2263 outParameter[0] = callResult.getResult(); 2264 return true; 2265 } 2266 2267 if (callResult.getResult() == null) { 2268 executeResult = null; 2269 return false; 2270 } 2271 2272 executeResult = (Object []) callResult.getResult(); 2273 if (executeResult == null || executeResult[0] == null) { 2274 currentResultIndex = -2; 2275 updateCount = -1; 2276 resultSet = null; 2277 return false; 2278 } 2279 Object [] reqObject = DaffodilDBStatement.getRequiredObjectOfexecute(executeResult[0], resultSetConcurrency,maxRows); 2280 executeResult[0] = reqObject[0]; 2281 if (executeResult[0] instanceof _RecordSetBuffer) { 2282 _RecordSetBuffer recordSetBuffer = (_RecordSetBuffer) executeResult[0]; 2283 resultSet = new DaffodilDBResultSet(this); 2284 resultSet.setRecordSetBuffer(recordSetBuffer); 2285 int oldResultSetCon = resultSetConcurrency; 2286 resultSetConcurrency = reqObject[1].hashCode(); 2287 if(oldResultSetCon != resultSetConcurrency ) 2288 addWarning("the resultSet Concurency is set ResultSet.CONCUR_READ_ONLY " ) ; 2289 setResultSetAttributes(); 2290 setRecordSetBufferAttributes(recordSetBuffer); 2291 resultSetConcurrency = oldResultSetCon; 2292 return true; 2293 } 2294 updateCount = executeResult[0].hashCode(); 2295 updateCount = updateCount == Integer.MIN_VALUE ? 0 : updateCount; 2296 return false; 2297 } 2298 catch (DException dxe) { 2299 throw dxe.getSqlException(connection.getLocale()); 2300 } 2301 } 2302 2303 2304 2305 private void isValidOUTParameterName(String name) throws SQLException { 2306 checkStatementType(); 2307 if (flagNamedParameterUsed == false || outParametersTypeMap.get(name) == null || 2308 outParameterMap == null || ! (outParameterMap.containsKey(name))) { 2309 DException dex = new DException("DSE740", new Object [] {name, 2310 outParameterMap}); 2311 throw dex.getSqlException(connection.getLocale()); 2312 } 2313 } 2314 2315 private void isValidOUTParameterIndex(int index) throws SQLException { 2316 checkStatementType(); 2317 checkValidParameterIndex(index); 2318 if (flagIndexParameterUsed == false || outParametersTypeMap == null|| 2319 outParametersTypeMap.get(new Integer (index)) == null || 2320 outParameter == null) { 2321 DException dex = new DException("DSE740", new Object [] {new Integer (index), 2322 outParameterMap}); 2323 throw dex.getSqlException(connection.getLocale()); 2324 } 2325 } 2326 2327 private void checkStatementType() throws SQLException { 2328 if (serverPreparedStatement.getQueryType() != 2329 _PreparedStatement.callstatement) { 2330 DException ex = new DException("DSE8042", null); 2331 throw ex.getSqlException(connection.getLocale()); 2332 } 2333 } 2334 2335 private void registerOutParameter(int parameterIndex, SQLType sqlType) throws 2336 SQLException { 2337 checkValidParameterIndex(parameterIndex); 2338 if (flagNamedParameterUsed == true) { 2339 DException dex = new DException("DSE212", null); 2340 throw dex.getSqlException(connection.getLocale()); 2341 } 2342 if(outParametersTypeMap == null) 2343 outParametersTypeMap = new HashMap(); 2344 outParametersTypeMap.put(new Integer (parameterIndex), sqlType); 2345 if(parametersSet[parameterIndex - 1]==false) { 2346 setNull(parameterIndex, sqlType.getType()); 2347 } 2348 } 2349 2350 private void registerOutParameter(String parameterName, SQLType sqlType) throws 2351 SQLException { 2352 checkStatementType(); 2353 if (flagIndexParameterUsed == true) { 2354 DException dex = new DException("DSE212", null); 2355 throw dex.getSqlException(connection.getLocale()); 2356 } 2357 if (outParametersTypeMap == null) 2358 outParametersTypeMap = new HashMap(); 2359 outParametersTypeMap.put(parameterName, sqlType); 2360 if(parameterMap.containsKey(parameterName)==false) { 2361 setNull(parameterName, sqlType.getType()); 2362 } 2363 } 2364 2365 private SQLType checkTypeEqual(int parameterIndex, int type, int scale) throws SQLException { 2366 SQLType sqlType = (SQLType) outParametersTypeMap.get(new Integer ( 2367 parameterIndex)); 2368 return checkTypeEqual(sqlType, type, scale); 2369 } 2370 2371 private SQLType checkTypeEqual(String parameterName, int type, int scale) throws SQLException { 2372 SQLType sqlType = (SQLType) outParametersTypeMap.get(parameterName); 2373 return checkTypeEqual(sqlType, type, scale); 2374 } 2375 2376 private SQLType checkTypeEqual(SQLType sqlType, int type, int scale) throws SQLException { 2377 if (sqlType.isCompatibleTo(type, scale)) 2378 return sqlType; 2379 DException dex = new DException("DSE1022", null); 2380 throw dex.getSqlException(connection.getLocale()); 2381 } 2382 2383 private SQLType checkTypeEqual(int parameterIndex, String str) throws SQLException { 2384 SQLType sqlType = (SQLType) outParametersTypeMap.get(new Integer ( 2385 parameterIndex)); 2386 return checkTypeEqual(sqlType, str); 2387 } 2388 2389 private SQLType checkTypeEqual(String parameterName, String str) throws SQLException { 2390 SQLType sqlType = (SQLType) outParametersTypeMap.get(new Integer ( 2391 parameterName)); 2392 return checkTypeEqual(sqlType, str); 2393 } 2394 2395 private SQLType checkTypeEqual(SQLType sqlType, String str) throws SQLException { 2396 if (str.equalsIgnoreCase("string")) { 2397 if (sqlType.getType() == Types.CHAR || sqlType.getType() == Types.VARCHAR || 2398 sqlType.getType() == Types.LONGVARCHAR) 2399 return sqlType; 2400 } 2401 else if (str.equalsIgnoreCase("binary")) { 2402 if (sqlType.getType() == Types.BINARY || 2403 sqlType.getType() == Types.VARBINARY || 2404 sqlType.getType() == Types.LONGVARBINARY) 2405 return sqlType; 2406 } 2407 DException dex = new DException("DSE1022", null); 2408 throw dex.getSqlException(connection.getLocale()); 2409 } 2410 2411 public synchronized void setObject(int parameterIndex, Object x) throws SQLException { 2412 if (flagNamedParameterUsed) { 2413 DException dex = new DException("DSE212", null); 2414 throw dex.getSqlException(connection.getLocale()); 2415 } 2416 super.setObject(parameterIndex, x); 2417 flagIndexParameterUsed = true; 2418 } 2419 2420 protected void checkIfAllParametersSet() throws SQLException{ 2421 if(flagNamedParameterUsed && parameterMap.size()!=parametersCount) { 2422 DException dex = new DException("DSE1157", null); 2423 throw dex.getSqlException(connection.getLocale()); 2424 } 2425 if(flagIndexParameterUsed) { 2426 boolean flag = true; 2427 for (int i = 0; flag && i < parametersCount; i++) 2428 flag = flag && parametersSet[i]; 2429 if (flag == false) { 2430 DException dex = new DException("DSE1157", null); 2431 throw dex.getSqlException(connection.getLocale()); 2432 } 2433 } 2434 } 2435 2436 2437protected void setPreparedStatement() throws SQLException { 2438 String queryForDB = changeQueryForDB(query).trim().toLowerCase(); 2439 if(queryForDB.startsWith("?")) { 2440 int callIndex = queryForDB.indexOf("="); 2441 queryForDB = queryForDB.substring(callIndex +1); 2442 outParametersCount = 1; 2443 query = queryForDB; 2444 userDefinedFunction = true; 2445 } 2446 2447 try { 2448 if(autoGeneratedKeyType == _Connection.NOAUTOGENERATEDKEYS){ 2449 serverPreparedStatement = connection.getServerConnection(). 2450 getPreparedStatement(queryForDB, 2451 resultSetConcurrency == 2452 ResultSet.CONCUR_UPDATABLE ? 2453 IteratorConstants.UPDATABLE : 2454 IteratorConstants.NONSCROLLABLE); 2455 } 2456 2457 else{ 2458 serverPreparedStatement = connection.getServerConnection(). 2459 getPreparedStatement(queryForDB, 2460 resultSetConcurrency == 2461 ResultSet.CONCUR_UPDATABLE ? 2462 IteratorConstants.UPDATABLE : 2463 IteratorConstants.NONSCROLLABLE,autoGeneratedKeyType,autoGeneratedKeyParams); 2464 } 2465 parametersCount = serverPreparedStatement.getParameterMetaData().getParameterCount(); 2466 2467 }catch (DException dxe) { 2468 throw dxe.getSqlException(connection.getLocale()); 2469 } 2470 parametersSet = new boolean[parametersCount]; 2471 parameters = new Object [parametersCount]; 2472 Arrays.fill(parametersSet, false); 2473 } 2474} 2475 | Popular Tags |