1 23 24 package org.continuent.sequoia.driver; 25 26 import java.io.ByteArrayOutputStream ; 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.io.ObjectOutputStream ; 30 import java.io.Reader ; 31 import java.io.Serializable ; 32 import java.math.BigDecimal ; 33 import java.net.URL ; 34 import java.sql.Array ; 35 import java.sql.BatchUpdateException ; 36 import java.sql.Blob ; 37 import java.sql.Clob ; 38 import java.sql.Date ; 39 import java.sql.Ref ; 40 import java.sql.ResultSet ; 41 import java.sql.SQLException ; 42 import java.sql.SQLWarning ; 43 import java.sql.Time ; 44 import java.sql.Timestamp ; 45 import java.sql.Types ; 46 import java.util.Calendar ; 47 import java.util.HashMap ; 48 import java.util.Iterator ; 49 import java.util.LinkedList ; 50 import java.util.List ; 51 import java.util.Map ; 52 53 import org.continuent.sequoia.common.exceptions.NotImplementedException; 54 import org.continuent.sequoia.common.protocol.PreparedStatementSerializationConstants; 55 import org.continuent.sequoia.common.sql.Request; 56 import org.continuent.sequoia.common.sql.RequestWithResultSetParameters; 57 import org.continuent.sequoia.common.sql.filters.AbstractBlobFilter; 58 import org.continuent.sequoia.common.util.Strings; 59 60 92 public class CallableStatement extends PreparedStatement 93 implements 94 java.sql.CallableStatement 95 { 96 private HashMap outParameters = null; 98 private HashMap outAndNamedParameterTypes = null; 100 private HashMap namedParameterValues = null; 102 private int lastOutParameterIndex = -1; 103 104 112 public CallableStatement(Connection connection, String sql, Driver driver) 113 { 114 super(connection, sql, driver); 115 } 116 117 122 protected synchronized String compileParameters() throws SQLException 123 { 124 String parameters = super.compileParameters(true); 126 127 if (outAndNamedParameterTypes == null) 128 return parameters; 129 130 for (Iterator iter = outAndNamedParameterTypes.values().iterator(); iter 133 .hasNext();) 134 { 135 String namedParam = (String ) iter.next(); 136 sbuf.append(namedParam); 137 } 138 return sbuf.toString(); 139 } 140 141 144 public boolean execute() throws SQLException 145 { 146 if (isClosed()) 147 { 148 throw new SQLException ("Unable to execute query on a closed statement"); 149 } 150 RequestWithResultSetParameters proc = new RequestWithResultSetParameters( 151 sql, compileParameters(), escapeProcessing, timeout); 152 setReadRequestParameters(proc); 153 ResultAndWarnings result = connection.callableStatementExecute(proc); 154 warnings = result.getStatementWarnings(); 155 List resultWithParameters = result.getResultList(); 156 resultList = (LinkedList ) resultWithParameters.get(0); 157 resultListIterator = resultList.iterator(); 158 outParameters = (HashMap ) resultWithParameters.get(1); 159 namedParameterValues = (HashMap ) resultWithParameters.get(2); 160 161 return getMoreResults(); 162 } 163 164 174 public int[] executeBatch() throws BatchUpdateException 175 { 176 if (batch == null || batch.isEmpty()) 177 return new int[0]; 178 179 int size = batch.size(); 180 int[] nbsRowsUpdated = new int[size]; 181 int i = 0; 182 SQLWarning allWarnings = null; 185 try 186 { 187 for (i = 0; i < size; i++) 188 { 189 BatchElement be = (BatchElement) batch.elementAt(i); 190 Request proc = new Request(be.getSqlTemplate(), be.getParameters(), 191 escapeProcessing, timeout); 192 193 ResultAndWarnings result = connection 194 .callableStatementExecuteUpdate(proc); 195 if (result.getStatementWarnings() != null) 196 addWarningTo(result.getStatementWarnings(), allWarnings); 197 List resultWithParameters = result.getResultList(); 198 nbsRowsUpdated[i] = ((Integer ) resultWithParameters.get(0)).intValue(); 199 outParameters = (HashMap ) resultWithParameters.get(1); 203 namedParameterValues = (HashMap ) resultWithParameters.get(2); 204 } 205 warnings = allWarnings; 207 return nbsRowsUpdated; 208 } 209 catch (SQLException e) 210 { 211 String message = "Batch failed for request " + i + ": " 212 + ((BatchElement) batch.elementAt(i)).getSqlTemplate() + " (" + e 213 + ")"; 214 215 int[] updateCounts = new int[i]; 217 System.arraycopy(nbsRowsUpdated, 0, updateCounts, 0, i); 218 219 throw new BatchUpdateException (message, updateCounts); 220 } 221 finally 222 { 223 batch.removeAllElements(); 224 } 225 } 226 227 230 public ResultSet executeQuery() throws SQLException 231 { 232 if (isClosed()) 233 { 234 throw new SQLException ("Unable to execute query on a closed statement"); 235 } 236 updateCount = -1; if (result != null) 238 { result.close(); 240 result = null; 241 } 242 243 RequestWithResultSetParameters proc = new RequestWithResultSetParameters( 244 sql, compileParameters(), escapeProcessing, timeout); 245 setReadRequestParameters(proc); 246 ResultAndWarnings res = connection.callableStatementExecuteQuery(proc); 247 warnings = res.getStatementWarnings(); 248 List resultWithParameters = res.getResultList(); 249 result = (ResultSet ) resultWithParameters.get(0); 250 outParameters = (HashMap ) resultWithParameters.get(1); 251 namedParameterValues = (HashMap ) resultWithParameters.get(2); 252 if (result instanceof DriverResultSet) 253 ((DriverResultSet) result).setStatement(this); 254 return result; 255 } 256 257 260 public int executeUpdate() throws SQLException 261 { 262 if (isClosed()) 263 { 264 throw new SQLException ("Unable to execute query on a closed statement"); 265 } 266 if (result != null) 267 { result.close(); 269 result = null; 270 } 271 Request proc = new Request(sql, compileParameters(), escapeProcessing, 272 timeout); 273 ResultAndWarnings result = connection.callableStatementExecuteUpdate(proc); 274 this.warnings = result.getStatementWarnings(); 275 List resultWithParameters = result.getResultList(); 276 updateCount = ((Integer ) resultWithParameters.get(0)).intValue(); 277 outParameters = (HashMap ) resultWithParameters.get(1); 278 namedParameterValues = (HashMap ) resultWithParameters.get(2); 279 280 return updateCount; 281 } 282 283 306 public void registerOutParameter(int parameterIndex, int sqlType) 307 throws SQLException 308 { 309 setOutParameterWithTag(String.valueOf(parameterIndex), 310 PreparedStatementSerializationConstants.REGISTER_OUT_PARAMETER, String 311 .valueOf(sqlType)); 312 } 313 314 333 public void registerOutParameter(int parameterIndex, int sqlType, int scale) 334 throws SQLException 335 { 336 setOutParameterWithTag( 337 String.valueOf(parameterIndex), 338 PreparedStatementSerializationConstants.REGISTER_OUT_PARAMETER_WITH_SCALE, 339 sqlType + "," + scale); 340 } 341 342 375 public void registerOutParameter(int paramIndex, int sqlType, String typeName) 376 throws SQLException 377 { 378 setOutParameterWithTag( 379 String.valueOf(paramIndex), 380 PreparedStatementSerializationConstants.REGISTER_OUT_PARAMETER_WITH_NAME, 381 sqlType + "," + typeName); 382 } 383 384 393 private int getOutParameterType(int parameterIndex) throws SQLException 394 { 395 lastOutParameterIndex = -1; 396 397 if (outParameters == null) 398 throw new SQLException ("No OUT parameter has been returned"); 399 400 lastOutParameterIndex = parameterIndex; 401 402 String type = (String ) outAndNamedParameterTypes.get(Integer 403 .toString(parameterIndex)); 404 if (type != null) 405 { 406 int typeStart = PreparedStatementSerializationConstants.START_PARAM_TAG 407 .length(); 408 String paramType = type.substring(typeStart, typeStart 409 + PreparedStatementSerializationConstants.BOOLEAN_TAG.length()); 410 String paramValue = type.substring(typeStart 411 + PreparedStatementSerializationConstants.BOOLEAN_TAG.length(), type 412 .indexOf(PreparedStatementSerializationConstants.END_PARAM_TAG)); 413 414 416 int comma = paramValue.indexOf(","); 420 paramValue = paramValue.substring(comma + 1); 421 422 if (paramType 423 .startsWith(PreparedStatementSerializationConstants.REGISTER_OUT_PARAMETER)) 424 return Integer.parseInt(paramValue); 425 if (paramType 426 .startsWith(PreparedStatementSerializationConstants.REGISTER_OUT_PARAMETER_WITH_NAME)) 427 return Integer.parseInt(paramValue 428 .substring(0, paramValue.indexOf(','))); 429 if (paramType 430 .startsWith(PreparedStatementSerializationConstants.REGISTER_OUT_PARAMETER_WITH_SCALE)) 431 return Integer.parseInt(paramValue 432 .substring(0, paramValue.indexOf(','))); 433 } 434 throw new SQLException ("Parameter " + parameterIndex 435 + " is not a registered OUT parameter"); 436 } 437 438 448 public boolean wasNull() throws SQLException 449 { 450 if (lastOutParameterIndex == -1) 451 return false; 452 return (outParameters.get(new Integer (lastOutParameterIndex)) == null); 453 } 454 455 471 public String getString(int parameterIndex) throws SQLException 472 { 473 int type = getOutParameterType(parameterIndex); 474 switch (type) 475 { 476 case Types.CHAR : 477 case Types.VARCHAR : 478 case Types.LONGVARCHAR : 479 return (String ) outParameters.get(new Integer (parameterIndex)); 480 default : 481 throw new SQLException ("Cannot convert OUT parameter of type " + type 482 + " to the requested type."); 483 } 484 } 485 486 496 public boolean getBoolean(int parameterIndex) throws SQLException 497 { 498 int type = getOutParameterType(parameterIndex); 499 switch (type) 500 { 501 case Types.BIT : 502 Boolean b = (Boolean ) outParameters.get(new Integer (parameterIndex)); 503 if (b == null) 504 return false; 505 else 506 return b.booleanValue(); 507 default : 508 throw new SQLException ("Cannot convert OUT parameter of type " + type 509 + " to the requested type."); 510 } 511 } 512 513 523 public byte getByte(int parameterIndex) throws SQLException 524 { 525 int type = getOutParameterType(parameterIndex); 526 switch (type) 527 { 528 case Types.TINYINT : 529 Number b = (Number ) outParameters.get(new Integer (parameterIndex)); 530 if (b == null) 531 return 0; 532 else 533 return b.byteValue(); 534 default : 535 throw new SQLException ("Cannot convert OUT parameter of type " + type 536 + " to the requested type."); 537 } 538 } 539 540 550 public short getShort(int parameterIndex) throws SQLException 551 { 552 int type = getOutParameterType(parameterIndex); 553 switch (type) 554 { 555 case Types.SMALLINT : 556 Number s = (Number ) outParameters.get(new Integer (parameterIndex)); 557 if (s == null) 558 return 0; 559 else 560 return s.shortValue(); 561 default : 562 throw new SQLException ("Cannot convert OUT parameter of type " + type 563 + " to the requested type."); 564 } 565 } 566 567 577 public int getInt(int parameterIndex) throws SQLException 578 { 579 int type = getOutParameterType(parameterIndex); 580 switch (type) 581 { 582 case Types.INTEGER : 583 Integer i = (Integer ) outParameters.get(new Integer (parameterIndex)); 584 if (i == null) 585 return 0; 586 else 587 return i.intValue(); 588 default : 589 throw new SQLException ("Cannot convert OUT parameter of type " + type 590 + " to the requested type."); 591 } 592 } 593 594 604 public long getLong(int parameterIndex) throws SQLException 605 { 606 int type = getOutParameterType(parameterIndex); 607 switch (type) 608 { 609 case Types.BIGINT : 610 Long l = (Long ) outParameters.get(new Integer (parameterIndex)); 611 if (l == null) 612 return 0; 613 else 614 return l.longValue(); 615 default : 616 throw new SQLException ("Cannot convert OUT parameter of type " + type 617 + " to the requested type."); 618 } 619 } 620 621 631 public float getFloat(int parameterIndex) throws SQLException 632 { 633 int type = getOutParameterType(parameterIndex); 634 switch (type) 635 { 636 case Types.FLOAT : 637 case Types.REAL : 638 Float f = (Float ) outParameters.get(new Integer (parameterIndex)); 639 if (f == null) 640 return 0; 641 else 642 return f.floatValue(); 643 default : 644 throw new SQLException ("Cannot convert OUT parameter of type " + type 645 + " to the requested type."); 646 } 647 } 648 649 659 public double getDouble(int parameterIndex) throws SQLException 660 { 661 int type = getOutParameterType(parameterIndex); 662 switch (type) 663 { 664 case Types.DOUBLE : 665 case Types.FLOAT : 666 Double d = (Double ) outParameters.get(new Integer (parameterIndex)); 667 if (d == null) 668 return 0; 669 else 670 return d.doubleValue(); 671 default : 672 throw new SQLException ("Cannot convert OUT parameter of type " + type 673 + " to the requested type."); 674 } 675 } 676 677 691 public BigDecimal getBigDecimal(int parameterIndex, int scale) 692 throws SQLException 693 { 694 return getBigDecimal(parameterIndex); 695 } 696 697 708 public byte[] getBytes(int parameterIndex) throws SQLException 709 { 710 int type = getOutParameterType(parameterIndex); 711 switch (type) 712 { 713 case Types.BINARY : 714 return (byte[]) outParameters.get(new Integer (parameterIndex)); 715 default : 716 throw new SQLException ("Cannot convert OUT parameter of type " + type 717 + " to the requested type."); 718 } 719 720 } 721 722 732 public Date getDate(int parameterIndex) throws SQLException 733 { 734 int type = getOutParameterType(parameterIndex); 735 switch (type) 736 { 737 case Types.DATE : 738 return (Date ) outParameters.get(new Integer (parameterIndex)); 739 default : 740 throw new SQLException ("Cannot convert OUT parameter of type " + type 741 + " to the requested type."); 742 } 743 } 744 745 755 public Time getTime(int parameterIndex) throws SQLException 756 { 757 int type = getOutParameterType(parameterIndex); 758 switch (type) 759 { 760 case Types.TIME : 761 return (Time ) outParameters.get(new Integer (parameterIndex)); 762 default : 763 throw new SQLException ("Cannot convert OUT parameter of type " + type 764 + " to the requested type."); 765 } 766 } 767 768 778 public Timestamp getTimestamp(int parameterIndex) throws SQLException 779 { 780 int type = getOutParameterType(parameterIndex); 781 switch (type) 782 { 783 case Types.TIMESTAMP : 784 return (Timestamp ) outParameters.get(new Integer (parameterIndex)); 785 default : 786 throw new SQLException ("Cannot convert OUT parameter of type " + type 787 + " to the requested type."); 788 } 789 } 790 791 794 811 public Object getObject(int parameterIndex) throws SQLException 812 { 813 getOutParameterType(parameterIndex); 815 return outParameters.get(new Integer (parameterIndex)); 816 } 817 818 820 832 public BigDecimal getBigDecimal(int parameterIndex) throws SQLException 833 { 834 int type = getOutParameterType(parameterIndex); 835 switch (type) 836 { 837 case Types.NUMERIC : 838 case Types.DECIMAL : 839 return (BigDecimal ) outParameters.get(new Integer (parameterIndex)); 840 default : 841 throw new SQLException ("Cannot convert OUT parameter of type " + type 842 + " to the requested type."); 843 } 844 } 845 846 863 public Object getObject(int i, Map map) throws SQLException 864 { 865 throw new NotImplementedException("getObject"); 866 } 867 868 880 public Ref getRef(int parameterIndex) throws SQLException 881 { 882 int type = getOutParameterType(parameterIndex); 883 switch (type) 884 { 885 case Types.REF : 886 return (Ref ) outParameters.get(new Integer (parameterIndex)); 887 default : 888 throw new SQLException ("Cannot convert OUT parameter of type " + type 889 + " to the requested type."); 890 } 891 } 892 893 904 public Blob getBlob(int i) throws SQLException 905 { 906 int type = getOutParameterType(i); 907 switch (type) 908 { 909 case Types.BLOB : 910 return (Blob ) outParameters.get(new Integer (i)); 911 default : 912 throw new SQLException ("Cannot convert OUT parameter of type " + type 913 + " to the requested type."); 914 } 915 } 916 917 928 public Clob getClob(int i) throws SQLException 929 { 930 int type = getOutParameterType(i); 931 switch (type) 932 { 933 case Types.CLOB : 934 return (Clob ) outParameters.get(new Integer (i)); 935 default : 936 throw new SQLException ("Cannot convert OUT parameter of type " + type 937 + " to the requested type."); 938 } 939 } 940 941 952 public Array getArray(int i) throws SQLException 953 { 954 int type = getOutParameterType(i); 955 switch (type) 956 { 957 case Types.ARRAY : 958 return (Array ) outParameters.get(new Integer (i)); 959 default : 960 throw new SQLException ("Cannot convert OUT parameter of type " + type 961 + " to the requested type."); 962 } 963 } 964 965 982 public Date getDate(int parameterIndex, Calendar cal) throws SQLException 983 { 984 return getDate(parameterIndex); 985 } 986 987 1004 public Time getTime(int parameterIndex, Calendar cal) throws SQLException 1005 { 1006 return getTime(parameterIndex); 1007 } 1008 1009 1027 public Timestamp getTimestamp(int parameterIndex, Calendar cal) 1028 throws SQLException 1029 { 1030 return getTimestamp(parameterIndex); 1031 } 1032 1033 1045 public URL getURL(int parameterIndex) throws SQLException 1046 { 1047 int type = getOutParameterType(parameterIndex); 1048 switch (type) 1049 { 1050 case Types.DATALINK : 1051 return (URL ) outParameters.get(new Integer (parameterIndex)); 1052 default : 1053 throw new SQLException ("Cannot convert OUT parameter of type " + type 1054 + " to the requested type."); 1055 } 1056 } 1057 1058 1082 public void registerOutParameter(String parameterName, int sqlType) 1083 throws SQLException 1084 { 1085 setOutParameterWithTag(parameterName, 1086 PreparedStatementSerializationConstants.REGISTER_OUT_PARAMETER, String 1087 .valueOf(sqlType)); 1088 } 1089 1090 1110 public void registerOutParameter(String parameterName, int sqlType, int scale) 1111 throws SQLException 1112 { 1113 setOutParameterWithTag( 1114 parameterName, 1115 PreparedStatementSerializationConstants.REGISTER_OUT_PARAMETER_WITH_SCALE, 1116 String.valueOf(sqlType) + "," + scale); 1117 } 1118 1119 1150 public void registerOutParameter(String parameterName, int sqlType, 1151 String typeName) throws SQLException 1152 { 1153 setOutParameterWithTag( 1154 parameterName, 1155 PreparedStatementSerializationConstants.REGISTER_OUT_PARAMETER_WITH_NAME, 1156 String.valueOf(sqlType) + "," + typeName); 1157 } 1158 1159 1168 private void setOutParameterWithTag(String paramName, String typeTag, 1169 String paramValue) 1170 { 1171 if (outAndNamedParameterTypes == null) 1172 outAndNamedParameterTypes = new HashMap (); 1173 1174 outAndNamedParameterTypes.put(paramName, 1176 PreparedStatementSerializationConstants.START_PARAM_TAG + typeTag 1177 + paramName + "," + paramValue 1178 + PreparedStatementSerializationConstants.END_PARAM_TAG); 1179 } 1180 1181 1189 private void setNamedParameterWithTag(String paramName, String typeTag, 1190 String param) 1191 { 1192 if (outAndNamedParameterTypes == null) 1193 outAndNamedParameterTypes = new HashMap (); 1194 1195 outAndNamedParameterTypes.put(paramName, 1197 PreparedStatementSerializationConstants.START_PARAM_TAG 1198 + PreparedStatementSerializationConstants.NAMED_PARAMETER_TAG 1199 + paramName 1200 + "," 1201 + typeTag 1202 + Strings.replace(param, 1203 PreparedStatementSerializationConstants.TAG_MARKER, 1204 PreparedStatementSerializationConstants.TAG_MARKER_ESCAPE) 1205 + PreparedStatementSerializationConstants.END_PARAM_TAG); 1206 } 1207 1208 1220 public void setURL(String parameterName, URL url) throws SQLException 1221 { 1222 if (url == null) 1223 setNamedParameterWithTag(parameterName, 1224 PreparedStatementSerializationConstants.URL_TAG, 1225 PreparedStatementSerializationConstants.NULL_VALUE); 1226 else 1227 setNamedParameterWithTag(parameterName, 1228 PreparedStatementSerializationConstants.URL_TAG, url.toString()); 1229 } 1230 1231 1241 public void setNull(String parameterName, int sqlType) throws SQLException 1242 { 1243 setNamedParameterWithTag(parameterName, 1244 PreparedStatementSerializationConstants.NULL_VALUE, String 1245 .valueOf(sqlType)); 1246 } 1247 1248 1259 public void setBoolean(String parameterName, boolean x) throws SQLException 1260 { 1261 setNamedParameterWithTag(parameterName, 1262 PreparedStatementSerializationConstants.BOOLEAN_TAG, String.valueOf(x)); 1263 } 1264 1265 1276 public void setByte(String parameterName, byte x) throws SQLException 1277 { 1278 setNamedParameterWithTag(parameterName, 1279 PreparedStatementSerializationConstants.BYTE_TAG, Integer.toString(x)); 1280 } 1281 1282 1293 public void setShort(String parameterName, short x) throws SQLException 1294 { 1295 setNamedParameterWithTag(parameterName, 1296 PreparedStatementSerializationConstants.SHORT_TAG, Integer.toString(x)); 1297 } 1298 1299 1310 public void setInt(String parameterName, int x) throws SQLException 1311 { 1312 setNamedParameterWithTag(parameterName, 1313 PreparedStatementSerializationConstants.INTEGER_TAG, Integer 1314 .toString(x)); 1315 } 1316 1317 1328 public void setLong(String parameterName, long x) throws SQLException 1329 { 1330 setNamedParameterWithTag(parameterName, 1331 PreparedStatementSerializationConstants.LONG_TAG, Long.toString(x)); 1332 } 1333 1334 1345 public void setFloat(String parameterName, float x) throws SQLException 1346 { 1347 setNamedParameterWithTag(parameterName, 1348 PreparedStatementSerializationConstants.FLOAT_TAG, Float.toString(x)); 1349 } 1350 1351 1362 public void setDouble(String parameterName, double x) throws SQLException 1363 { 1364 setNamedParameterWithTag(parameterName, 1365 PreparedStatementSerializationConstants.DOUBLE_TAG, Double.toString(x)); 1366 } 1367 1368 1379 public void setBigDecimal(String parameterName, BigDecimal x) 1380 throws SQLException 1381 { 1382 if (x == null) 1383 setNamedParameterWithTag(parameterName, 1384 PreparedStatementSerializationConstants.BIG_DECIMAL_TAG, 1385 PreparedStatementSerializationConstants.NULL_VALUE); 1386 else 1387 setNamedParameterWithTag(parameterName, 1388 PreparedStatementSerializationConstants.BIG_DECIMAL_TAG, x.toString()); 1389 } 1390 1391 1404 public void setString(String parameterName, String x) throws SQLException 1405 { 1406 if (x == null) 1407 setNamedParameterWithTag(parameterName, 1408 PreparedStatementSerializationConstants.STRING_TAG, 1409 PreparedStatementSerializationConstants.NULL_VALUE); 1410 else 1411 { 1412 if (PreparedStatementSerializationConstants.NULL_VALUE.equals(x)) 1413 { setNamedParameterWithTag(parameterName, 1416 PreparedStatementSerializationConstants.NULL_STRING_TAG, x); 1417 } 1418 else 1419 { setNamedParameterWithTag(parameterName, 1422 PreparedStatementSerializationConstants.STRING_TAG, x); 1423 } 1424 } 1425 } 1426 1427 1440 public void setBytes(String parameterName, byte[] x) throws SQLException 1441 { 1442 try 1443 { 1444 1449 String encodedString = AbstractBlobFilter.getDefaultBlobFilter() 1450 .encode(x); 1451 setNamedParameterWithTag(parameterName, 1452 PreparedStatementSerializationConstants.BYTES_TAG, encodedString); 1453 } 1454 catch (OutOfMemoryError oome) 1455 { 1456 System.gc(); 1457 throw new SQLException ("Out of memory while encoding bytes"); 1458 } 1459 } 1460 1461 1472 public void setDate(String parameterName, Date x) throws SQLException 1473 { 1474 if (x == null) 1475 setNamedParameterWithTag(parameterName, 1476 PreparedStatementSerializationConstants.DATE_TAG, 1477 PreparedStatementSerializationConstants.NULL_VALUE); 1478 else 1479 setNamedParameterWithTag(parameterName, 1480 PreparedStatementSerializationConstants.DATE_TAG, new java.sql.Date (x 1481 .getTime()).toString()); 1482 } 1483 1484 1495 public void setTime(String parameterName, Time x) throws SQLException 1496 { 1497 if (x == null) 1498 setNamedParameterWithTag(parameterName, 1499 PreparedStatementSerializationConstants.TIME_TAG, 1500 PreparedStatementSerializationConstants.NULL_VALUE); 1501 else 1502 setNamedParameterWithTag(parameterName, 1503 PreparedStatementSerializationConstants.TIME_TAG, x.toString()); 1504 } 1505 1506 1517 public void setTimestamp(String parameterName, Timestamp x) 1518 throws SQLException 1519 { 1520 if (x == null) 1521 setNamedParameterWithTag(parameterName, 1522 PreparedStatementSerializationConstants.TIMESTAMP_TAG, 1523 PreparedStatementSerializationConstants.NULL_VALUE); 1524 else 1525 { 1526 if (x.getClass().equals(Timestamp .class)) 1527 setNamedParameterWithTag(parameterName, 1528 PreparedStatementSerializationConstants.TIMESTAMP_TAG, 1529 new Timestamp (x.getTime()).toString()); 1530 } 1531 } 1532 1533 1550 public void setAsciiStream(String parameterName, InputStream x, int length) 1551 throws SQLException 1552 { 1553 setBinaryStream(parameterName, x, length); 1554 } 1555 1556 1572 public void setBinaryStream(String parameterName, InputStream x, int length) 1573 throws SQLException 1574 { 1575 byte[] data = new byte[length]; 1576 try 1577 { 1578 x.read(data, 0, length); 1579 } 1580 catch (Exception ioe) 1581 { 1582 throw new SQLException ("Problem with streaming of data"); 1583 } 1584 setBytes(parameterName, data); 1587 } 1588 1589 1622 public void setObject(String parameterName, Object x, int targetSqlType, 1623 int scale) throws SQLException 1624 { 1625 if (x == null) 1626 { 1627 setNull(parameterName, targetSqlType); 1628 return; 1629 } 1630 1631 try 1632 { 1633 boolean failed = false; 1634 switch (targetSqlType) 1635 { 1636 1642 case Types.TINYINT : 1647 case Types.SMALLINT : 1648 if (x instanceof Number ) 1649 setShort(parameterName, ((Number ) x).shortValue()); 1650 else if (x instanceof Boolean ) 1651 setShort(parameterName, ((Boolean ) x).booleanValue() 1652 ? (short) 1 1653 : (short) 0); 1654 else if (x instanceof String ) 1655 setInt(parameterName, Short.parseShort((String ) x)); 1656 else 1657 failed = true; 1658 break; 1659 case Types.INTEGER : 1661 if (x instanceof Number ) 1662 setInt(parameterName, ((Number ) x).intValue()); 1663 else if (x instanceof Boolean ) 1664 setInt(parameterName, ((Boolean ) x).booleanValue() ? 1 : 0); 1665 else if (x instanceof String ) 1666 setInt(parameterName, Integer.parseInt((String ) x)); 1667 else 1668 failed = true; 1669 break; 1670 case Types.BIGINT : 1672 if (x instanceof Number ) 1673 setLong(parameterName, ((Number ) x).longValue()); 1674 else if (x instanceof String ) 1675 setLong(parameterName, Long.parseLong((String ) x)); 1676 else if (x instanceof Boolean ) 1677 setLong(parameterName, ((Boolean ) x).booleanValue() ? 1 : 0); 1678 else 1679 failed = true; 1680 break; 1681 case Types.REAL : 1683 case Types.FLOAT : 1684 case Types.DOUBLE : 1685 if (x instanceof Number ) 1686 setDouble(parameterName, ((Number ) x).doubleValue()); 1687 else if (x instanceof String ) 1688 setDouble(parameterName, Double.parseDouble((String ) x)); 1689 else if (x instanceof Boolean ) 1690 setDouble(parameterName, ((Boolean ) x).booleanValue() ? 1 : 0); 1691 else 1692 failed = true; 1693 break; 1694 case Types.DECIMAL : 1696 case Types.NUMERIC : 1697 BigDecimal bd; 1698 if (x instanceof Boolean ) 1699 bd = new BigDecimal (((Boolean ) x).booleanValue() ? 1d : 0d); 1700 else if (x instanceof Number ) 1701 bd = new BigDecimal (((Number ) x).toString()); 1702 else if (x instanceof String ) 1703 bd = new BigDecimal ((String ) x); 1704 else 1705 { 1706 failed = true; 1707 break; 1708 } 1709 bd = bd.setScale(scale, BigDecimal.ROUND_HALF_UP); 1710 setBigDecimal(parameterName, bd); 1711 break; 1712 case Types.BIT : 1714 case Types.BOOLEAN : 1715 if (x instanceof Number ) 1716 setBoolean(parameterName, 0 != ((Number ) x).longValue()); 1717 else if (x instanceof Boolean ) 1718 setBoolean(parameterName, ((Boolean ) x).booleanValue()); 1719 else if (x instanceof String ) 1720 setBoolean(parameterName, Boolean.valueOf((String ) x) 1721 .booleanValue()); 1722 else 1723 failed = true; 1724 break; 1725 case Types.CHAR : 1727 case Types.VARCHAR : 1728 case Types.LONGVARCHAR : 1729 setString(parameterName, x.toString()); 1730 break; 1731 case Types.BINARY : 1733 case Types.VARBINARY : 1734 case Types.LONGVARBINARY : 1735 if (x instanceof byte[]) 1736 setBytes(parameterName, (byte[]) x); 1737 else if (x instanceof Serializable ) 1738 setObject(parameterName, x); 1740 else 1741 failed = true; 1742 break; 1743 case Types.DATE : 1745 if (x instanceof String ) 1746 setDate(parameterName, java.sql.Date.valueOf((String ) x)); 1747 else if (x instanceof java.sql.Date ) 1748 setDate(parameterName, (java.sql.Date ) x); 1749 else if (x instanceof Timestamp ) 1750 setDate(parameterName, new java.sql.Date (((Timestamp ) x).getTime())); 1751 else 1752 failed = true; 1753 break; 1754 case Types.TIME : 1756 if (x instanceof String ) 1757 setTime(parameterName, Time.valueOf((String ) x)); 1758 else if (x instanceof Time ) 1759 setTime(parameterName, (Time ) x); 1760 else if (x instanceof Timestamp ) 1761 setTime(parameterName, new Time (((Timestamp ) x).getTime())); 1762 else 1763 failed = true; 1764 break; 1765 case Types.TIMESTAMP : 1767 if (x instanceof String ) 1768 setTimestamp(parameterName, Timestamp.valueOf((String ) x)); 1769 else if (x instanceof Date ) 1770 setTimestamp(parameterName, new Timestamp (((Date ) x).getTime())); 1771 else if (x instanceof Timestamp ) 1772 setTimestamp(parameterName, (Timestamp ) x); 1773 else 1774 failed = true; 1775 break; 1776 case Types.BLOB : 1778 failed = true; 1779 break; 1780 case Types.DATALINK : 1782 if (x instanceof java.net.URL ) 1783 setURL(parameterName, (java.net.URL ) x); 1784 else 1785 setURL(parameterName, new java.net.URL (x.toString())); 1786 break; 1787 case Types.JAVA_OBJECT : 1788 case Types.OTHER : 1789 setObject(parameterName, x); 1791 break; 1792 default : 1793 throw new SQLException ("Unsupported type value"); 1794 } 1795 if (true == failed) 1796 throw new IllegalArgumentException ( 1797 "Attempt to perform an illegal conversion"); 1798 } 1799 catch (Exception e) 1800 { 1801 SQLException outE = new SQLException ("Exception while converting type " 1802 + x.getClass() + " to SQL type " + targetSqlType); 1803 outE.initCause(e); 1804 throw outE; 1805 } 1806 } 1807 1808 1821 public void setObject(String parameterName, Object x, int targetSqlType) 1822 throws SQLException 1823 { 1824 setObject(parameterName, x, targetSqlType, 0); 1825 } 1826 1827 1859 public void setObject(String parameterName, Object x) throws SQLException 1860 { 1861 if (x == null) 1862 { 1863 setNamedParameterWithTag(parameterName, 1864 PreparedStatementSerializationConstants.OBJECT_TAG, 1865 PreparedStatementSerializationConstants.NULL_VALUE); 1866 } 1867 else 1868 { 1878 if (x instanceof String ) 1879 setString(parameterName, (String ) x); 1880 else if (x instanceof BigDecimal ) 1881 setBigDecimal(parameterName, (BigDecimal ) x); 1882 else if (x instanceof Boolean ) 1883 setBoolean(parameterName, ((Boolean ) x).booleanValue()); 1884 else if (x instanceof Short ) 1885 setShort(parameterName, ((Short ) x).shortValue()); 1886 else if (x instanceof Integer ) 1887 setInt(parameterName, ((Integer ) x).intValue()); 1888 else if (x instanceof Long ) 1889 setLong(parameterName, ((Long ) x).longValue()); 1890 else if (x instanceof Float ) 1891 setFloat(parameterName, ((Float ) x).floatValue()); 1892 else if (x instanceof Double ) 1893 setDouble(parameterName, ((Double ) x).doubleValue()); 1894 else if (x instanceof byte[]) 1895 setBytes(parameterName, (byte[]) x); 1896 else if (x instanceof java.sql.Date ) 1897 setDate(parameterName, (java.sql.Date ) x); 1898 else if (x instanceof Time ) 1899 setTime(parameterName, (Time ) x); 1900 else if (x instanceof Timestamp ) 1901 setTimestamp(parameterName, (Timestamp ) x); 1902 else if (x instanceof java.net.URL ) 1903 setURL(parameterName, (java.net.URL ) x); 1904 else if (x instanceof Serializable ) 1905 { 1906 ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream (); 1907 try 1908 { 1909 ObjectOutputStream objectOutputStream = new ObjectOutputStream ( 1911 byteOutputStream); 1912 objectOutputStream.writeObject(x); 1913 objectOutputStream.close(); 1914 synchronized (this.sbuf) 1915 { 1916 this.sbuf.setLength(0); 1917 1921 this.sbuf.append(AbstractBlobFilter.getDefaultBlobFilter().encode( 1922 byteOutputStream.toByteArray())); 1923 setNamedParameterWithTag(parameterName, 1924 PreparedStatementSerializationConstants.OBJECT_TAG, this.sbuf 1925 .toString()); 1926 } 1927 } 1928 catch (IOException e) 1929 { 1930 throw new SQLException ("Failed to serialize object: " + e); 1931 } 1932 } 1933 else 1934 throw new SQLException ("Objects of type " + x.getClass() 1935 + " are not supported."); 1936 } 1937 } 1938 1939 1958 public void setCharacterStream(String parameterName, Reader reader, int length) 1959 throws SQLException 1960 { 1961 char[] data = new char[length]; 1962 try 1963 { 1964 reader.read(data, 0, length); 1965 } 1966 catch (Exception ioe) 1967 { 1968 throw new SQLException ("Problem with streaming of data"); 1969 } 1970 setString(parameterName, new String (data)); 1971 } 1972 1973 1991 public void setDate(String parameterName, Date d, Calendar cal) 1992 throws SQLException 1993 { 1994 if (d == null) 1995 setNamedParameterWithTag(parameterName, 1996 PreparedStatementSerializationConstants.DATE_TAG, 1997 PreparedStatementSerializationConstants.NULL_VALUE); 1998 else 1999 { 2000 if (cal == null) 2001 setDate(parameterName, d); 2002 else 2003 { 2004 cal.setTime(d); 2005 setDate(parameterName, new java.sql.Date (cal.getTime().getTime())); 2006 } 2007 } 2008 } 2009 2010 2028 public void setTime(String parameterName, Time t, Calendar cal) 2029 throws SQLException 2030 { 2031 if (t == null) 2032 setNamedParameterWithTag(parameterName, 2033 PreparedStatementSerializationConstants.TIME_TAG, 2034 PreparedStatementSerializationConstants.NULL_VALUE); 2035 else 2036 { 2037 if (cal == null) 2038 setTime(parameterName, t); 2039 else 2040 { 2041 cal.setTime(t); 2042 setTime(parameterName, new java.sql.Time (cal.getTime().getTime())); 2043 } 2044 } 2045 } 2046 2047 2065 public void setTimestamp(String parameterName, Timestamp t, Calendar cal) 2066 throws SQLException 2067 { 2068 if (t == null) 2069 setNamedParameterWithTag(parameterName, 2070 PreparedStatementSerializationConstants.TIMESTAMP_TAG, 2071 PreparedStatementSerializationConstants.NULL_VALUE); 2072 else 2073 { 2074 if (cal == null) 2075 setTimestamp(parameterName, t); 2076 else 2077 { 2078 cal.setTime(t); 2079 setTimestamp(parameterName, new java.sql.Timestamp (cal.getTime() 2080 .getTime())); 2081 } 2082 } 2083 } 2084 2085 2110 public void setNull(String parameterName, int sqlType, String typeName) 2111 throws SQLException 2112 { 2113 setNull(parameterName, sqlType); 2114 } 2115 2116 2133 public String getString(String parameterName) throws SQLException 2134 { 2135 if (!outAndNamedParameterTypes.containsKey(parameterName)) 2136 throw new SQLException ("Invalid named parameter " + parameterName); 2137 2138 try 2139 { 2140 return (String ) namedParameterValues.get(parameterName); 2141 } 2142 catch (Exception e) 2143 { 2144 throw new SQLException ("Unable to convert named parameter " 2145 + parameterName + " to the requested type"); 2146 } 2147 } 2148 2149 2160 public boolean getBoolean(String parameterName) throws SQLException 2161 { 2162 if (!outAndNamedParameterTypes.containsKey(parameterName)) 2163 throw new SQLException ("Invalid named parameter " + parameterName); 2164 2165 try 2166 { 2167 Boolean b = (Boolean ) namedParameterValues.get(parameterName); 2168 if (b == null) 2169 return false; 2170 else 2171 return b.booleanValue(); 2172 } 2173 catch (Exception e) 2174 { 2175 throw new SQLException ("Unable to convert named parameter " 2176 + parameterName + " to the requested type"); 2177 } 2178 } 2179 2180 2191 public byte getByte(String parameterName) throws SQLException 2192 { 2193 if (!outAndNamedParameterTypes.containsKey(parameterName)) 2194 throw new SQLException ("Invalid named parameter " + parameterName); 2195 2196 try 2197 { 2198 Byte b = (Byte ) namedParameterValues.get(parameterName); 2199 if (b == null) 2200 return 0; 2201 else 2202 return b.byteValue(); 2203 } 2204 catch (Exception e) 2205 { 2206 throw new SQLException ("Unable to convert named parameter " 2207 + parameterName + " to the requested type"); 2208 } 2209 } 2210 2211 2222 public short getShort(String parameterName) throws SQLException 2223 { 2224 if (!outAndNamedParameterTypes.containsKey(parameterName)) 2225 throw new SQLException ("Invalid named parameter " + parameterName); 2226 2227 try 2228 { 2229 Short s = (Short ) namedParameterValues.get(parameterName); 2230 if (s == null) 2231 return 0; 2232 else 2233 return s.shortValue(); 2234 } 2235 catch (Exception e) 2236 { 2237 throw new SQLException ("Unable to convert named parameter " 2238 + parameterName + " to the requested type"); 2239 } 2240 } 2241 2242 2253 public int getInt(String parameterName) throws SQLException 2254 { 2255 if (!outAndNamedParameterTypes.containsKey(parameterName)) 2256 throw new SQLException ("Invalid named parameter " + parameterName); 2257 2258 try 2259 { 2260 Integer i = (Integer ) namedParameterValues.get(parameterName); 2261 if (i == null) 2262 return 0; 2263 else 2264 return i.intValue(); 2265 } 2266 catch (Exception e) 2267 { 2268 throw new SQLException ("Unable to convert named parameter " 2269 + parameterName + " to the requested type"); 2270 } 2271 } 2272 2273 2284 public long getLong(String parameterName) throws SQLException 2285 { 2286 if (!outAndNamedParameterTypes.containsKey(parameterName)) 2287 throw new SQLException ("Invalid named parameter " + parameterName); 2288 2289 try 2290 { 2291 Long l = (Long ) namedParameterValues.get(parameterName); 2292 if (l == null) 2293 return 0; 2294 else 2295 return l.longValue(); 2296 } 2297 catch (Exception e) 2298 { 2299 throw new SQLException ("Unable to convert named parameter " 2300 + parameterName + " to the requested type"); 2301 } 2302 } 2303 2304 2315 public float getFloat(String parameterName) throws SQLException 2316 { 2317 if (!outAndNamedParameterTypes.containsKey(parameterName)) 2318 throw new SQLException ("Invalid named parameter " + parameterName); 2319 2320 try 2321 { 2322 Float f = (Float ) namedParameterValues.get(parameterName); 2323 if (f == null) 2324 return 0; 2325 else 2326 return f.floatValue(); 2327 } 2328 catch (Exception e) 2329 { 2330 throw new SQLException ("Unable to convert named parameter " 2331 + parameterName + " to the requested type"); 2332 } 2333 } 2334 2335 2346 public double getDouble(String parameterName) throws SQLException 2347 { 2348 if (!outAndNamedParameterTypes.containsKey(parameterName)) 2349 throw new SQLException ("Invalid named parameter " + parameterName); 2350 2351 try 2352 { 2353 Double d = (Double ) namedParameterValues.get(parameterName); 2354 if (d == null) 2355 return 0; 2356 else 2357 return d.doubleValue(); 2358 } 2359 catch (Exception e) 2360 { 2361 throw new SQLException ("Unable to convert named parameter " 2362 + parameterName + " to the requested type"); 2363 } 2364 } 2365 2366 2378 public byte[] getBytes(String parameterName) throws SQLException 2379 { 2380 if (!outAndNamedParameterTypes.containsKey(parameterName)) 2381 throw new SQLException ("Invalid named parameter " + parameterName); 2382 2383 try 2384 { 2385 return (byte[]) namedParameterValues.get(parameterName); 2386 } 2387 catch (Exception e) 2388 { 2389 throw new SQLException ("Unable to convert named parameter " 2390 + parameterName + " to the requested type"); 2391 } 2392 } 2393 2394 2405 public Date getDate(String parameterName) throws SQLException 2406 { 2407 if (!outAndNamedParameterTypes.containsKey(parameterName)) 2408 throw new SQLException ("Invalid named parameter " + parameterName); 2409 2410 try 2411 { 2412 return (Date ) namedParameterValues.get(parameterName); 2413 } 2414 catch (Exception e) 2415 { 2416 throw new SQLException ("Unable to convert named parameter " 2417 + parameterName + " to the requested type"); 2418 } 2419 } 2420 2421 2432 public Time getTime(String parameterName) throws SQLException 2433 { 2434 if (!outAndNamedParameterTypes.containsKey(parameterName)) 2435 throw new SQLException ("Invalid named parameter " + parameterName); 2436 2437 try 2438 { 2439 return (Time ) namedParameterValues.get(parameterName); 2440 } 2441 catch (Exception e) 2442 { 2443 throw new SQLException ("Unable to convert named parameter " 2444 + parameterName + " to the requested type"); 2445 } 2446 } 2447 2448 2459 public Timestamp getTimestamp(String parameterName) throws SQLException 2460 { 2461 if (!outAndNamedParameterTypes.containsKey(parameterName)) 2462 throw new SQLException ("Invalid named parameter " + parameterName); 2463 2464 try 2465 { 2466 return (Timestamp ) namedParameterValues.get(parameterName); 2467 } 2468 catch (Exception e) 2469 { 2470 throw new SQLException ("Unable to convert named parameter " 2471 + parameterName + " to the requested type"); 2472 } 2473 } 2474 2475 2493 public Object getObject(String parameterName) throws SQLException 2494 { 2495 if (!outAndNamedParameterTypes.containsKey(parameterName)) 2496 throw new SQLException ("Invalid named parameter " + parameterName); 2497 2498 try 2499 { 2500 return namedParameterValues.get(parameterName); 2501 } 2502 catch (Exception e) 2503 { 2504 throw new SQLException ("Unable to convert named parameter " 2505 + parameterName + " to the requested type"); 2506 } 2507 } 2508 2509 2521 public BigDecimal getBigDecimal(String parameterName) throws SQLException 2522 { 2523 if (!outAndNamedParameterTypes.containsKey(parameterName)) 2524 throw new SQLException ("Invalid named parameter " + parameterName); 2525 2526 try 2527 { 2528 return (BigDecimal ) namedParameterValues.get(parameterName); 2529 } 2530 catch (Exception e) 2531 { 2532 throw new SQLException ("Unable to convert named parameter " 2533 + parameterName + " to the requested type"); 2534 } 2535 } 2536 2537 2554 public Object getObject(String parameterName, Map map) throws SQLException 2555 { 2556 throw new NotImplementedException("getObject"); 2557 } 2558 2559 2570 public Ref getRef(String parameterName) throws SQLException 2571 { 2572 if (!outAndNamedParameterTypes.containsKey(parameterName)) 2573 throw new SQLException ("Invalid named parameter " + parameterName); 2574 2575 try 2576 { 2577 return (Ref ) namedParameterValues.get(parameterName); 2578 } 2579 catch (Exception e) 2580 { 2581 throw new SQLException ("Unable to convert named parameter " 2582 + parameterName + " to the requested type"); 2583 } 2584 } 2585 2586 2597 public Blob getBlob(String parameterName) throws SQLException 2598 { 2599 if (!outAndNamedParameterTypes.containsKey(parameterName)) 2600 throw new SQLException ("Invalid named parameter " + parameterName); 2601 2602 try 2603 { 2604 return (Blob ) namedParameterValues.get(parameterName); 2605 } 2606 catch (Exception e) 2607 { 2608 throw new SQLException ("Unable to convert named parameter " 2609 + parameterName + " to the requested type"); 2610 } 2611 } 2612 2613 2624 public Clob getClob(String parameterName) throws SQLException 2625 { 2626 if (!outAndNamedParameterTypes.containsKey(parameterName)) 2627 throw new SQLException ("Invalid named parameter " + parameterName); 2628 2629 try 2630 { 2631 return (Clob ) namedParameterValues.get(parameterName); 2632 } 2633 catch (Exception e) 2634 { 2635 throw new SQLException ("Unable to convert named parameter " 2636 + parameterName + " to the requested type"); 2637 } 2638 } 2639 2640 2651 public Array getArray(String parameterName) throws SQLException 2652 { 2653 if (!outAndNamedParameterTypes.containsKey(parameterName)) 2654 throw new SQLException ("Invalid named parameter " + parameterName); 2655 2656 try 2657 { 2658 return (Array ) namedParameterValues.get(parameterName); 2659 } 2660 catch (Exception e) 2661 { 2662 throw new SQLException ("Unable to convert named parameter " 2663 + parameterName + " to the requested type"); 2664 } 2665 } 2666 2667 2684 public Date getDate(String parameterName, Calendar cal) throws SQLException 2685 { 2686 return getDate(executeUpdate()); 2687 } 2688 2689 2706 public Time getTime(String parameterName, Calendar cal) throws SQLException 2707 { 2708 return getTime(executeUpdate()); 2709 } 2710 2711 2729 public Timestamp getTimestamp(String parameterName, Calendar cal) 2730 throws SQLException 2731 { 2732 return getTimestamp(executeUpdate()); 2733 } 2734 2735 2748 public URL getURL(String parameterName) throws SQLException 2749 { 2750 if (!outAndNamedParameterTypes.containsKey(parameterName)) 2751 throw new SQLException ("Invalid named parameter " + parameterName); 2752 2753 try 2754 { 2755 return (URL ) namedParameterValues.get(parameterName); 2756 } 2757 catch (Exception e) 2758 { 2759 throw new SQLException ("Unable to convert named parameter " 2760 + parameterName + " to the requested type"); 2761 } 2762 } 2763} | Popular Tags |