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.Serializable ; 31 import java.math.BigDecimal ; 32 import java.sql.Array ; 33 import java.sql.BatchUpdateException ; 34 import java.sql.Blob ; 35 import java.sql.Date ; 36 import java.sql.ParameterMetaData ; 37 import java.sql.Ref ; 38 import java.sql.SQLException ; 39 import java.sql.Time ; 40 import java.sql.Timestamp ; 41 import java.sql.Types ; 42 import java.util.LinkedList ; 43 import java.util.Vector ; 44 45 import org.continuent.sequoia.common.exceptions.NotImplementedException; 46 import org.continuent.sequoia.common.protocol.PreparedStatementSerializationConstants; 47 import org.continuent.sequoia.common.sql.filters.AbstractBlobFilter; 48 import org.continuent.sequoia.common.util.Strings; 49 50 77 public class PreparedStatement extends Statement 78 implements 79 java.sql.PreparedStatement 80 { 81 82 protected String sql; 83 84 private String [] inStrings; 85 86 protected StringBuffer sbuf = new StringBuffer (); 88 89 97 PreparedStatement(Connection connection, String sqlStatement, Driver driver) 98 { 99 super(connection, driver); 100 101 107 boolean inString = false; 108 boolean inMetaString = false; 109 int nbParam = 0; 110 111 this.sql = sqlStatement.trim(); 112 this.connection = connection; 113 114 for (int i = 0; i < sql.length(); ++i) 116 { 117 if (sql.charAt(i) == '\'') 118 if (i > 0 && sql.charAt(i - 1) != '\\') 119 inString = !inString; 120 if (sql.charAt(i) == '"') 121 if (i > 0 && sql.charAt(i - 1) != '\\') 122 inMetaString = !inMetaString; 123 if ((sql.charAt(i) == '?') && (!(inString || inMetaString))) 124 nbParam++; 125 } 126 127 inStrings = new String [nbParam]; 128 129 if (connection.isAlwaysGettingGeneratedKeys()) 130 generatedKeysFlag = RETURN_GENERATED_KEYS; 131 } 132 133 136 PreparedStatement(Connection connection, String sqlStatement, Driver driver, 137 int autoGeneratedKeysArg) 138 { 139 this(connection, sqlStatement, driver); 140 if (!connection.isAlwaysGettingGeneratedKeys()) 141 generatedKeysFlag = autoGeneratedKeysArg; 142 } 143 144 149 public void close() throws SQLException 150 { 151 sql = null; 152 inStrings = null; 153 154 super.close(); 155 } 156 157 165 public java.sql.ResultSet executeQuery() throws SQLException 166 { 167 return super.executeQuery(sql, compileParameters(false)); 169 } 170 171 180 public int executeUpdate() throws SQLException 181 { 182 return super.executeUpdateWithSkeleton(sql, compileParameters(false)); 184 } 185 186 196 protected synchronized String compileParameters( 197 boolean fillEmptyParametersWithCSParamTag) throws SQLException 198 { 199 if (inStrings.length == 0) 200 { 201 return ""; 202 } 203 204 sbuf.setLength(0); 205 for (int i = 0; i < inStrings.length; ++i) 206 { 207 if (inStrings[i] == null) 208 { 209 if (!fillEmptyParametersWithCSParamTag) 210 throw new SQLException ("Parameter " + (i + 1) + " is not set"); 211 setParameterWithTag(i + 1, 212 PreparedStatementSerializationConstants.CS_PARAM_TAG, ""); 213 } 214 sbuf.append(inStrings[i]); 215 } 216 return sbuf.toString(); 217 } 218 219 229 protected String doEscapeProcessing(String x) 230 { 231 synchronized (sbuf) 234 { 235 sbuf.setLength(0); 236 int i; 237 sbuf.append(connection.getEscapeChar()); 238 for (i = 0; i < x.length(); ++i) 239 { 240 char c = x.charAt(i); 241 if ((c == '\'' && connection.isEscapeSingleQuote()) 242 || (c == '\\' && connection.isEscapeBackslash())) 243 sbuf.append(c); 244 sbuf.append(c); 245 } 246 sbuf.append(connection.getEscapeChar()); 247 } 248 return sbuf.toString(); 249 } 250 251 258 public void setNull(int parameterIndex, int sqlType) throws SQLException 259 { 260 setParameterWithTag(parameterIndex, 263 PreparedStatementSerializationConstants.NULL_VALUE, String 264 .valueOf(sqlType)); 265 } 266 267 275 public void setBoolean(int parameterIndex, boolean x) throws SQLException 276 { 277 setParameterWithTag(parameterIndex, 278 PreparedStatementSerializationConstants.BOOLEAN_TAG, String.valueOf(x)); 279 } 280 281 288 public void setByte(int parameterIndex, byte x) throws SQLException 289 { 290 setParameterWithTag(parameterIndex, 291 PreparedStatementSerializationConstants.BYTE_TAG, Integer.toString(x)); 292 } 293 294 302 public void setShort(int parameterIndex, short x) throws SQLException 303 { 304 setParameterWithTag(parameterIndex, 305 PreparedStatementSerializationConstants.SHORT_TAG, Integer.toString(x)); 306 } 307 308 316 public void setInt(int parameterIndex, int x) throws SQLException 317 { 318 setParameterWithTag(parameterIndex, 319 PreparedStatementSerializationConstants.INTEGER_TAG, Integer 320 .toString(x)); 321 } 322 323 331 public void setLong(int parameterIndex, long x) throws SQLException 332 { 333 setParameterWithTag(parameterIndex, 334 PreparedStatementSerializationConstants.LONG_TAG, Long.toString(x)); 335 } 336 337 345 public void setFloat(int parameterIndex, float x) throws SQLException 346 { 347 setParameterWithTag(parameterIndex, 348 PreparedStatementSerializationConstants.FLOAT_TAG, Float.toString(x)); 349 } 350 351 359 public void setDouble(int parameterIndex, double x) throws SQLException 360 { 361 setParameterWithTag(parameterIndex, 362 PreparedStatementSerializationConstants.DOUBLE_TAG, Double.toString(x)); 363 } 364 365 373 public void setBigDecimal(int parameterIndex, BigDecimal x) 374 throws SQLException 375 { 376 String serializedParam = (x == null ? null : x.toString()); 377 378 setParameterWithTag(parameterIndex, 379 PreparedStatementSerializationConstants.BIG_DECIMAL_TAG, 380 serializedParam); 381 } 382 383 392 public void setString(int parameterIndex, String x) throws SQLException 393 { 394 if (PreparedStatementSerializationConstants.NULL_VALUE.equals(x)) 395 setParameterWithTag(parameterIndex, 398 PreparedStatementSerializationConstants.NULL_STRING_TAG, x); 399 else 400 setParameterWithTag(parameterIndex, 401 PreparedStatementSerializationConstants.STRING_TAG, x); 402 } 403 404 412 public void setBytes(int parameterIndex, byte[] x) throws SQLException 413 { 414 try 415 { 416 421 String encodedString = AbstractBlobFilter.getDefaultBlobFilter() 422 .encode(x); 423 setParameterWithTag(parameterIndex, 424 PreparedStatementSerializationConstants.BYTES_TAG, encodedString); 425 } 426 catch (OutOfMemoryError oome) 427 { 428 System.gc(); 429 throw new SQLException ("Out of memory while encoding bytes"); 430 } 431 } 432 433 441 public void setDate(int parameterIndex, java.sql.Date x) throws SQLException 442 { 443 String serializedParam = (x == null ? null : new java.sql.Date (x.getTime()) 444 .toString()); 445 446 setParameterWithTag(parameterIndex, 447 PreparedStatementSerializationConstants.DATE_TAG, serializedParam); 448 } 449 450 458 public void setTime(int parameterIndex, Time x) throws SQLException 459 { 460 String serializedParam = (x == null ? null : x.toString()); 461 462 setParameterWithTag(parameterIndex, 463 PreparedStatementSerializationConstants.TIME_TAG, serializedParam); 464 } 465 466 474 public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException 475 { 476 if (x == null) 477 setParameterWithTag(parameterIndex, 478 PreparedStatementSerializationConstants.TIMESTAMP_TAG, null); 479 else 480 { 481 if (x.getClass().equals(Timestamp .class)) 482 setParameterWithTag(parameterIndex, 483 PreparedStatementSerializationConstants.TIMESTAMP_TAG, x.toString()); 484 else 485 setParameterWithTag(parameterIndex, 486 PreparedStatementSerializationConstants.TIMESTAMP_TAG, 487 new Timestamp (x.getTime()).toString()); 488 } 489 } 490 491 506 public void setAsciiStream(int parameterIndex, InputStream x, int length) 507 throws SQLException 508 { 509 setBinaryStream(parameterIndex, x, length); 510 } 511 512 529 public void setUnicodeStream(int parameterIndex, InputStream x, int length) 530 throws SQLException 531 { 532 setBinaryStream(parameterIndex, x, length); 533 } 534 535 552 public void setBinaryStream(int parameterIndex, InputStream inStreamArg, 553 int length) throws SQLException 554 { 555 byte[] data = new byte[length]; 556 try 557 { 558 inStreamArg.read(data, 0, length); 559 } 560 catch (Exception ioe) 561 { 562 throw new SQLException ("Problem with streaming of data"); 563 } 564 setBytes(parameterIndex, data); 567 } 568 569 578 public void clearParameters() throws SQLException 579 { 580 int i; 581 582 for (i = 0; i < inStrings.length; i++) 583 inStrings[i] = null; 584 } 585 586 608 public void setObject(int parameterIndex, Object x, int targetSqlType, 609 int scale) throws SQLException 610 { 611 if (x == null) 612 { 613 setNull(parameterIndex, targetSqlType); 614 return; 615 } 616 617 try 618 { 619 boolean failed = false; 620 switch (targetSqlType) 621 { 622 628 629 case Types.TINYINT : 634 case Types.SMALLINT : 635 if (x instanceof Number ) 636 setShort(parameterIndex, ((Number ) x).shortValue()); 637 else if (x instanceof Boolean ) 638 setShort(parameterIndex, ((Boolean ) x).booleanValue() 639 ? (short) 1 640 : (short) 0); 641 else if (x instanceof String ) 642 setShort(parameterIndex, Short.parseShort((String ) x)); 643 else 644 failed = true; 645 break; 646 case Types.INTEGER : 648 if (x instanceof Number ) 649 setInt(parameterIndex, ((Number ) x).intValue()); 650 else if (x instanceof Boolean ) 651 setInt(parameterIndex, ((Boolean ) x).booleanValue() ? 1 : 0); 652 else if (x instanceof String ) 653 setInt(parameterIndex, Integer.parseInt((String ) x)); 654 else 655 failed = true; 656 break; 657 case Types.BIGINT : 659 if (x instanceof Number ) 660 setLong(parameterIndex, ((Number ) x).longValue()); 661 else if (x instanceof String ) 662 setLong(parameterIndex, Long.parseLong((String ) x)); 663 else if (x instanceof Boolean ) 664 setLong(parameterIndex, ((Boolean ) x).booleanValue() ? 1 : 0); 665 else 666 failed = true; 667 break; 668 case Types.REAL : 670 case Types.FLOAT : 671 case Types.DOUBLE : 672 if (x instanceof Number ) 673 setDouble(parameterIndex, ((Number ) x).doubleValue()); 674 else if (x instanceof String ) 675 setDouble(parameterIndex, Double.parseDouble((String ) x)); 676 else if (x instanceof Boolean ) 677 setDouble(parameterIndex, ((Boolean ) x).booleanValue() ? 1 : 0); 678 else 679 failed = true; 680 break; 681 case Types.DECIMAL : 683 case Types.NUMERIC : 684 BigDecimal bd; 685 if (x instanceof Boolean ) 686 bd = new BigDecimal (((Boolean ) x).booleanValue() ? 1d : 0d); 687 else if (x instanceof Number ) 688 bd = new BigDecimal (((Number ) x).toString()); 689 else if (x instanceof String ) 690 bd = new BigDecimal ((String ) x); 691 else 692 { 693 failed = true; 694 break; 695 } 696 bd = bd.setScale(scale, BigDecimal.ROUND_HALF_UP); 697 setBigDecimal(parameterIndex, bd); 698 break; 699 case Types.BIT : 701 case Types.BOOLEAN : 702 if (x instanceof Number ) 703 setBoolean(parameterIndex, 0 != ((Number ) x).longValue()); 704 else if (x instanceof Boolean ) 705 setBoolean(parameterIndex, ((Boolean ) x).booleanValue()); 706 else if (x instanceof String ) 707 setBoolean(parameterIndex, Boolean.valueOf((String ) x) 708 .booleanValue()); 709 else 710 failed = true; 711 break; 712 case Types.CHAR : 714 case Types.VARCHAR : 715 case Types.LONGVARCHAR : 716 setString(parameterIndex, x.toString()); 717 break; 718 case Types.BINARY : 720 case Types.VARBINARY : 721 case Types.LONGVARBINARY : 722 if (x instanceof byte[]) 723 setBytes(parameterIndex, (byte[]) x); 724 else if (x instanceof Blob ) 725 setBlob(parameterIndex, (Blob ) x); 726 else if (x instanceof Serializable ) 727 setObject(parameterIndex, x); 729 else 730 failed = true; 731 break; 732 case Types.DATE : 734 if (x instanceof String ) 735 setDate(parameterIndex, java.sql.Date.valueOf((String ) x)); 736 else if (x instanceof java.sql.Date ) 737 setDate(parameterIndex, (java.sql.Date ) x); 738 else if (x instanceof Timestamp ) 739 setDate(parameterIndex, 740 new java.sql.Date (((Timestamp ) x).getTime())); 741 else 742 failed = true; 743 break; 744 case Types.TIME : 746 if (x instanceof String ) 747 setTime(parameterIndex, Time.valueOf((String ) x)); 748 else if (x instanceof Time ) 749 setTime(parameterIndex, (Time ) x); 750 else if (x instanceof Timestamp ) 751 setTime(parameterIndex, new Time (((Timestamp ) x).getTime())); 752 else 753 failed = true; 754 break; 755 case Types.TIMESTAMP : 757 if (x instanceof String ) 758 setTimestamp(parameterIndex, Timestamp.valueOf((String ) x)); 759 else if (x instanceof Date ) 760 setTimestamp(parameterIndex, new Timestamp (((Date ) x).getTime())); 761 else if (x instanceof Timestamp ) 762 setTimestamp(parameterIndex, (Timestamp ) x); 763 else 764 failed = true; 765 break; 766 case Types.BLOB : 768 if (x instanceof Blob ) 769 setBlob(parameterIndex, (Blob ) x); 770 else 771 failed = true; 772 break; 773 case Types.DATALINK : 775 if (x instanceof java.net.URL ) 776 setURL(parameterIndex, (java.net.URL ) x); 777 else 778 setURL(parameterIndex, new java.net.URL (x.toString())); 779 break; 780 case Types.JAVA_OBJECT : 781 case Types.OTHER : 782 setObject(parameterIndex, x); 784 break; 785 default : 786 throw new SQLException ("Unsupported type value"); 787 } 788 if (true == failed) 789 throw new IllegalArgumentException ( 790 "Attempt to perform an illegal conversion"); 791 } 792 catch (Exception e) 793 { 794 SQLException outE = new SQLException ("Exception while converting type " 795 + x.getClass() + " to SQL type " + targetSqlType); 796 outE.initCause(e); 797 throw outE; 798 } 799 } 800 801 804 public void setObject(int parameterIndex, Object x, int targetSqlType) 805 throws SQLException 806 { 807 setObject(parameterIndex, x, targetSqlType, 0); 808 } 809 810 817 public void setObject(int parameterIndex, Object x) throws SQLException 818 { 819 if (x == null) 820 { 821 setParameterWithTag(parameterIndex, 822 PreparedStatementSerializationConstants.OBJECT_TAG, null); 823 } 824 else 825 { 835 if (x instanceof String ) 836 setString(parameterIndex, (String ) x); 837 else if (x instanceof BigDecimal ) 838 setBigDecimal(parameterIndex, (BigDecimal ) x); 839 else if (x instanceof Boolean ) 840 setBoolean(parameterIndex, ((Boolean ) x).booleanValue()); 841 else if (x instanceof Short ) 842 setShort(parameterIndex, ((Short ) x).shortValue()); 843 else if (x instanceof Integer ) 844 setInt(parameterIndex, ((Integer ) x).intValue()); 845 else if (x instanceof Long ) 846 setLong(parameterIndex, ((Long ) x).longValue()); 847 else if (x instanceof Float ) 848 setFloat(parameterIndex, ((Float ) x).floatValue()); 849 else if (x instanceof Double ) 850 setDouble(parameterIndex, ((Double ) x).doubleValue()); 851 else if (x instanceof byte[]) 852 setBytes(parameterIndex, (byte[]) x); 853 else if (x instanceof java.sql.Date ) 854 setDate(parameterIndex, (java.sql.Date ) x); 855 else if (x instanceof Time ) 856 setTime(parameterIndex, (Time ) x); 857 else if (x instanceof Timestamp ) 858 setTimestamp(parameterIndex, (Timestamp ) x); 859 else if (x instanceof Blob ) 860 setBlob(parameterIndex, (Blob ) x); 861 else if (x instanceof java.net.URL ) 862 setURL(parameterIndex, (java.net.URL ) x); 863 else if (x instanceof Serializable ) 864 { 865 ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream (); 866 try 867 { 868 ObjectOutputStream objectOutputStream = new ObjectOutputStream ( 870 byteOutputStream); 871 objectOutputStream.writeObject(x); 872 objectOutputStream.close(); 873 synchronized (this.sbuf) 874 { 875 this.sbuf.setLength(0); 876 880 this.sbuf.append(AbstractBlobFilter.getDefaultBlobFilter().encode( 881 byteOutputStream.toByteArray())); 882 setParameterWithTag(parameterIndex, 883 PreparedStatementSerializationConstants.OBJECT_TAG, this.sbuf 884 .toString()); 885 } 886 } 887 catch (IOException e) 888 { 889 throw new SQLException ("Failed to serialize object: " + e); 890 } 891 } 892 else 893 throw new SQLException ("Objects of type " + x.getClass() 894 + " are not supported."); 895 } 896 } 897 898 908 public boolean execute() throws SQLException 909 { 910 if (this.generatedKeysFlag == RETURN_GENERATED_KEYS) 911 { 912 if (connection.isAlwaysGettingGeneratedKeys() 913 && !sql.toLowerCase().trim().startsWith("insert")) 914 { 915 return super.execute(sql, compileParameters(false)); 916 } 917 int result = executeUpdate(); 918 resultList = new LinkedList (); 919 resultList.add(new Integer (result)); 920 resultListIterator = resultList.iterator(); 921 return getMoreResults(); 922 } 923 return super.execute(sql, compileParameters(false)); 924 } 925 926 935 public String toString() 936 { 937 synchronized (sbuf) 938 { 939 sbuf.setLength(0); 940 sbuf.append(sql); 941 int i; 942 943 if (inStrings == null) 944 return sbuf.toString(); 945 946 for (i = 0; i < inStrings.length; ++i) 947 { 948 if (inStrings[i] == null) 949 sbuf.append('?'); 950 else 951 sbuf.append(inStrings[i]); 952 } 953 return sbuf.toString(); 954 } 955 } 956 957 959 964 public synchronized void addBatch() throws SQLException 965 { 966 if (batch == null) 967 batch = new Vector (); 968 batch.addElement(new BatchElement(sql, compileParameters(false))); 969 } 970 971 981 public int[] executeBatch() throws BatchUpdateException 982 { 983 if (batch == null || batch.isEmpty()) 984 return new int[0]; 985 986 int size = batch.size(); 987 int[] nbsRowsUpdated = new int[size]; 988 int i = 0; 989 990 try 991 { 992 for (i = 0; i < size; i++) 993 { 994 BatchElement be = (BatchElement) batch.elementAt(i); 995 nbsRowsUpdated[i] = this.executeUpdateWithSkeleton(be.getSqlTemplate(), 996 be.getParameters()); 997 } 998 return nbsRowsUpdated; 999 } 1000 catch (SQLException e) 1001 { 1002 String message = "Batch failed for request " + i + ": " 1003 + ((BatchElement) batch.elementAt(i)).getSqlTemplate() + " (" + e 1004 + ")"; 1005 1006 int[] updateCounts = new int[i]; 1008 System.arraycopy(nbsRowsUpdated, 0, updateCounts, 0, i); 1009 1010 throw new BatchUpdateException (message, updateCounts); 1011 } 1012 finally 1013 { 1014 batch.removeAllElements(); 1015 } 1016 } 1017 1018 1025 public java.sql.ResultSetMetaData getMetaData() throws SQLException 1026 { 1027 java.sql.ResultSet rs = getResultSet(); 1028 if (rs != null) 1029 return rs.getMetaData(); 1030 else 1031 return connection.preparedStatementGetMetaData(sql); 1032 } 1033 1034 1037 public void setArray(int i, Array x) throws SQLException 1038 { 1039 throw new NotImplementedException("setArray()"); 1040 } 1041 1042 1045 public void setBlob(int paramIndex, Blob sqlBlobParam) throws SQLException 1046 { 1047 if (sqlBlobParam == null) 1048 { 1049 setParameterWithTag(paramIndex, 1050 PreparedStatementSerializationConstants.BLOB_TAG, null); 1051 return; 1052 } 1053 1054 InputStream blobBinStream = sqlBlobParam.getBinaryStream(); 1057 1058 byte[] data = new byte[(int) sqlBlobParam.length()]; 1059 try 1060 { 1061 blobBinStream.read(data, 0, (int) sqlBlobParam.length()); 1062 } 1063 catch (Exception ioe) 1064 { 1065 throw new SQLException ("Problem with data streaming"); 1066 } 1067 try 1068 { 1069 synchronized (this.sbuf) 1070 { 1071 this.sbuf.setLength(0); 1072 1077 this.sbuf 1078 .append(AbstractBlobFilter.getDefaultBlobFilter().encode(data)); 1079 setParameterWithTag(paramIndex, 1080 PreparedStatementSerializationConstants.BLOB_TAG, this.sbuf 1081 .toString()); 1082 } 1083 } 1084 catch (OutOfMemoryError oome) 1085 { 1086 this.sbuf = null; 1087 System.gc(); 1088 throw new SQLException ("Out of memory"); 1089 } 1090 } 1091 1092 1096 public void setCharacterStream(int i, java.io.Reader x, int length) 1097 throws SQLException 1098 { 1099 char[] data = new char[length]; 1100 try 1101 { 1102 x.read(data, 0, length); 1103 } 1104 catch (Exception ioe) 1105 { 1106 throw new SQLException ("Problem with streaming of data"); 1107 } 1108 setString(i, new String (data)); 1109 } 1110 1111 1114 public void setClob(int i, java.sql.Clob clobArg) throws SQLException 1115 { 1116 String serializedParam = (clobArg == null ? null : clobArg.getSubString(0, 1117 (int) clobArg.length())); 1118 1119 setParameterWithTag(i, PreparedStatementSerializationConstants.CLOB_TAG, 1120 serializedParam); 1121 } 1122 1123 1126 public void setNull(int i, int t, String s) throws SQLException 1127 { 1128 setNull(i, t); 1129 } 1130 1131 1134 public void setRef(int i, Ref x) throws SQLException 1135 { 1136 String serializedParam = (x == null ? null : x.toString()); 1137 1138 setParameterWithTag(i, PreparedStatementSerializationConstants.REF_TAG, 1139 serializedParam); 1140 } 1141 1142 1146 public void setDate(int i, java.sql.Date d, java.util.Calendar cal) 1147 throws SQLException 1148 { 1149 if (d == null) 1150 setParameterWithTag(i, PreparedStatementSerializationConstants.DATE_TAG, 1151 null); 1152 else 1153 { 1154 if (cal == null) 1155 setDate(i, d); 1156 else 1157 { 1158 cal.setTime(d); 1159 setDate(i, new java.sql.Date (cal.getTime().getTime())); 1160 } 1161 } 1162 } 1163 1164 1168 public void setTime(int i, Time t, java.util.Calendar cal) 1169 throws SQLException 1170 { 1171 if (t == null) 1172 setParameterWithTag(i, PreparedStatementSerializationConstants.TIME_TAG, 1173 null); 1174 else 1175 { 1176 if (cal == null) 1177 setTime(i, t); 1178 else 1179 { 1180 cal.setTime(t); 1181 setTime(i, new java.sql.Time (cal.getTime().getTime())); 1182 } 1183 } 1184 } 1185 1186 1190 public void setTimestamp(int i, Timestamp t, java.util.Calendar cal) 1191 throws SQLException 1192 { 1193 if (t == null) 1194 setParameterWithTag(i, 1195 PreparedStatementSerializationConstants.TIMESTAMP_TAG, null); 1196 else 1197 { 1198 if (cal == null) 1199 setTimestamp(i, t); 1200 else 1201 { 1202 cal.setTime(t); 1203 setTimestamp(i, new java.sql.Timestamp (cal.getTime().getTime())); 1204 } 1205 } 1206 } 1207 1208 1210 1220 public void setURL(int parameterIndex, java.net.URL x) throws SQLException 1221 { 1222 String serializedParam = (x == null ? null : x.toString()); 1223 1224 setParameterWithTag(parameterIndex, 1225 PreparedStatementSerializationConstants.URL_TAG, serializedParam); 1226 } 1227 1228 1239 public ParameterMetaData getParameterMetaData() throws SQLException 1240 { 1241 throw new NotImplementedException("getParameterMetaData"); 1242 } 1243 1244 1248 1256 protected void setParameter(int paramIndex, String s) throws SQLException 1257 { 1258 if (paramIndex < 1 || paramIndex > inStrings.length) 1259 throw new SQLException ("Parameter index out of range."); 1260 inStrings[paramIndex - 1] = s; 1261 } 1262 1263 1270 protected String [] getParameterTagAndValue(int paramIndex) 1271 throws SQLException 1272 { 1273 if (paramIndex < 1 || paramIndex > inStrings.length) 1274 throw new SQLException ("Parameter index out of range."); 1275 int typeStart = PreparedStatementSerializationConstants.START_PARAM_TAG 1276 .length(); 1277 1278 String tagString = inStrings[paramIndex - 1]; 1280 String paramType = tagString.substring(typeStart, typeStart 1281 + PreparedStatementSerializationConstants.BOOLEAN_TAG.length()); 1282 String paramValue = tagString.substring(typeStart 1283 + PreparedStatementSerializationConstants.BOOLEAN_TAG.length(), 1284 tagString 1285 .indexOf(PreparedStatementSerializationConstants.END_PARAM_TAG)); 1286 paramValue = Strings.replace(paramValue, 1287 PreparedStatementSerializationConstants.TAG_MARKER_ESCAPE, 1288 PreparedStatementSerializationConstants.TAG_MARKER); 1289 return new String []{paramType, paramValue}; 1290 } 1291 1292 1305 void setParameterWithTag(int paramIndex, String typeTag, String param) 1306 throws SQLException 1307 { 1308 if (isClosed()) 1309 throw new SQLException ("Unable to set a parameter on a closed statement"); 1310 1311 1315 if (param == null) 1316 param = PreparedStatementSerializationConstants.NULL_VALUE; 1317 else 1318 param = Strings.replace(param, 1320 PreparedStatementSerializationConstants.TAG_MARKER, 1321 PreparedStatementSerializationConstants.TAG_MARKER_ESCAPE); 1322 1323 setParameter(paramIndex, 1324 PreparedStatementSerializationConstants.START_PARAM_TAG + typeTag 1325 + param + PreparedStatementSerializationConstants.END_PARAM_TAG); 1326 } 1327 1328 1336 protected class BatchElement 1337 { 1338 private String sqlTemplate; 1339 private String parameters; 1340 1341 1347 public BatchElement(String sqlTemplate, String parameters) 1348 { 1349 this.sqlTemplate = sqlTemplate; 1350 this.parameters = parameters; 1351 } 1352 1353 1358 public String getParameters() 1359 { 1360 return parameters; 1361 } 1362 1363 1368 public String getSqlTemplate() 1369 { 1370 return sqlTemplate; 1371 } 1372 } 1373} 1374 | Popular Tags |