1 24 25 package org.objectweb.cjdbc.driver; 26 27 import java.io.ByteArrayInputStream ; 28 import java.io.ByteArrayOutputStream ; 29 import java.io.IOException ; 30 import java.io.InputStream ; 31 import java.io.ObjectInputStream ; 32 import java.io.ObjectOutputStream ; 33 import java.io.Serializable ; 34 import java.math.BigDecimal ; 35 import java.net.MalformedURLException ; 36 import java.net.URL ; 37 import java.sql.Array ; 38 import java.sql.BatchUpdateException ; 39 import java.sql.Date ; 40 import java.sql.ParameterMetaData ; 41 import java.sql.Ref ; 42 import java.sql.SQLException ; 43 import java.sql.Time ; 44 import java.sql.Timestamp ; 45 import java.sql.Types ; 46 import java.text.ParseException ; 47 import java.text.SimpleDateFormat ; 48 import java.util.ArrayList ; 49 import java.util.Vector ; 50 51 import org.objectweb.cjdbc.common.exceptions.NotImplementedException; 52 import org.objectweb.cjdbc.common.sql.filters.HexaBlobFilter; 53 import org.objectweb.cjdbc.common.util.Strings; 54 55 82 public class PreparedStatement extends Statement 83 implements 84 java.sql.PreparedStatement 85 { 86 87 88 92 93 public static final String BYTE_TAG = "b|"; 94 95 public static final String BYTES_TAG = "B|"; 96 97 public static final String BLOB_TAG = "c|"; 98 99 public static final String CLOB_TAG = "C|"; 100 101 public static final String BOOLEAN_TAG = "0|"; 102 103 public static final String BIG_DECIMAL_TAG = "1|"; 104 105 public static final String DATE_TAG = "d|"; 106 107 public static final String DOUBLE_TAG = "D|"; 108 109 public static final String FLOAT_TAG = "F|"; 110 111 public static final String INTEGER_TAG = "I|"; 112 113 public static final String LONG_TAG = "L|"; 114 115 public static final String NULL_TAG = "N|"; 116 117 public static final String NULL_STRING_TAG = "n|"; 118 119 public static final String OBJECT_TAG = "O|"; 120 121 public static final String REF_TAG = "R|"; 122 123 public static final String SHORT_TAG = "s|"; 124 125 public static final String STRING_TAG = "S|"; 126 127 public static final String TIME_TAG = "t|"; 128 129 public static final String TIMESTAMP_TAG = "T|"; 130 131 public static final String URL_TAG = "U|"; 132 133 134 public static final String TAG_MARKER = "!%"; 135 136 public static final String TAG_MARKER_ESCAPE = TAG_MARKER + ";"; 137 138 public static final String START_PARAM_TAG = "<" + TAG_MARKER; 139 140 public static final String END_PARAM_TAG = "|" + TAG_MARKER + ">"; 141 142 143 protected String sql; 144 145 private String [] inStrings; 146 147 private String [] templateStrings; 148 149 private StringBuffer sbuf = new StringBuffer (); 151 152 162 public PreparedStatement(Connection connection, String sqlStatement) 163 throws SQLException 164 { 165 super(connection); 166 167 168 ArrayList segs = new ArrayList (); 169 int lastParmEnd = 0; 170 171 177 boolean inString = false; 178 boolean inMetaString = false; 179 180 this.sql = sqlStatement.trim(); 181 this.connection = connection; 182 for (int i = 0; i < sql.length(); ++i) 183 { 184 if (sql.charAt(i) == '\'') 185 inString = !inString; 186 if (sql.charAt(i) == '"') 187 inMetaString = !inMetaString; 188 if ((sql.charAt(i) == '?') && (!(inString || inMetaString))) 189 { 190 segs.add(sql.substring(lastParmEnd, i)); 191 lastParmEnd = i + 1; 192 } 193 } 194 segs.add(sql.substring(lastParmEnd, sql.length())); 195 196 int size = segs.size(); 197 templateStrings = new String [size]; 198 inStrings = new String [size - 1]; 199 clearParameters(); 200 201 for (int i = 0; i < size; ++i) 202 templateStrings[i] = (String ) segs.get(i); 203 } 204 205 210 public void close() throws SQLException 211 { 212 sql = null; 213 templateStrings = null; 214 inStrings = null; 215 216 super.close(); 217 } 218 219 227 public java.sql.ResultSet executeQuery() throws SQLException 228 { 229 return super.executeQuery(sql, compileQuery()); } 231 232 241 public int executeUpdate() throws SQLException 242 { 243 return super.executeUpdateWithSkeleton(sql, compileQuery()); 244 } 246 247 255 protected synchronized String compileQuery() throws SQLException 256 { 257 sbuf.setLength(0); 258 int i; 259 260 for (i = 0; i < inStrings.length; ++i) 261 { 262 if (inStrings[i] == null) 263 throw new SQLException ("Parameter " + (i + 1) + " is incorrect"); 264 sbuf.append(templateStrings[i]).append(inStrings[i]); 265 } 266 sbuf.append(templateStrings[inStrings.length]); 267 return sbuf.toString(); 268 } 269 270 280 protected String doEscapeProcessing(String x) 281 { 282 synchronized (sbuf) 285 { 286 sbuf.setLength(0); 287 int i; 288 sbuf.append(connection.getEscapeChar()); 289 for (i = 0; i < x.length(); ++i) 290 { 291 char c = x.charAt(i); 292 if ((c == '\'' && connection.isEscapeSingleQuote()) 293 || (c == '\\' && connection.isEscapeBackslash())) 294 sbuf.append(c); 295 sbuf.append(c); 296 } 297 sbuf.append(connection.getEscapeChar()); 298 } 299 return sbuf.toString(); 300 } 301 302 311 public void setNull(int parameterIndex, int sqlType) throws SQLException 312 { 313 if (connection.isDriverProcessed()) 314 set(parameterIndex, "null"); 315 else 316 setWithTag(parameterIndex, NULL_TAG, String.valueOf(sqlType)); 317 } 318 319 327 public void setBoolean(int parameterIndex, boolean x) throws SQLException 328 { 329 if (connection.isDriverProcessed()) 330 { 331 set(parameterIndex, x 332 ? connection.getPreparedStatementBooleanTrue() 333 : connection.getPreparedStatementBooleanFalse()); 334 } 335 else 336 { 337 setWithTag(parameterIndex, BOOLEAN_TAG, String.valueOf(x)); 338 } 339 } 340 341 348 public void setByte(int parameterIndex, byte x) throws SQLException 349 { 350 if (connection.isDriverProcessed()) 351 { 352 set(parameterIndex, Integer.toString(x)); 353 } 354 else 355 { 356 setWithTag(parameterIndex, BYTE_TAG, Integer.toString(x)); 357 } 358 } 359 360 368 public void setShort(int parameterIndex, short x) throws SQLException 369 { 370 if (connection.isDriverProcessed()) 371 { 372 set(parameterIndex, Integer.toString(x)); 373 } 374 else 375 { 376 setWithTag(parameterIndex, SHORT_TAG, Integer.toString(x)); 377 } 378 } 379 380 388 public void setInt(int parameterIndex, int x) throws SQLException 389 { 390 if (connection.isDriverProcessed()) 391 { 392 set(parameterIndex, Integer.toString(x)); 393 } 394 else 395 { 396 setWithTag(parameterIndex, INTEGER_TAG, Integer.toString(x)); 397 } 398 } 399 400 408 public void setLong(int parameterIndex, long x) throws SQLException 409 { 410 if (connection.isDriverProcessed()) 411 { 412 set(parameterIndex, Long.toString(x)); 413 } 414 else 415 { 416 setWithTag(parameterIndex, LONG_TAG, Long.toString(x)); 417 } 418 } 419 420 428 public void setFloat(int parameterIndex, float x) throws SQLException 429 { 430 if (connection.isDriverProcessed()) 431 { 432 set(parameterIndex, Float.toString(x)); 433 } 434 else 435 { 436 setWithTag(parameterIndex, FLOAT_TAG, Float.toString(x)); 437 } 438 } 439 440 448 public void setDouble(int parameterIndex, double x) throws SQLException 449 { 450 if (connection.isDriverProcessed()) 451 { 452 set(parameterIndex, Double.toString(x)); 453 } 454 else 455 { 456 setWithTag(parameterIndex, DOUBLE_TAG, Double.toString(x)); 457 } 458 } 459 460 468 public void setBigDecimal(int parameterIndex, BigDecimal x) 469 throws SQLException 470 { 471 if (connection.isDriverProcessed()) 472 { 473 if (x == null) 474 setNull(parameterIndex, Types.DECIMAL); 475 else 476 set(parameterIndex, x.toString()); 477 } 478 else 479 { 480 if (x == null) 481 setWithTag(parameterIndex, BIG_DECIMAL_TAG, NULL_TAG); 482 else 483 setWithTag(parameterIndex, BIG_DECIMAL_TAG, x.toString()); 484 } 485 } 486 487 496 public void setString(int parameterIndex, String x) throws SQLException 497 { 498 if (connection.isDriverProcessed()) 499 { 500 if (x == null) 501 setNull(parameterIndex, Types.VARCHAR); 503 else 504 { 505 if (escapeProcessing 506 && (connection.isEscapeBackslash() || connection 507 .isEscapeSingleQuote())) 508 set(parameterIndex, doEscapeProcessing(x)); 509 else 510 set(parameterIndex, x); 512 } 513 } 514 else 515 { 516 if (x == null) 517 setWithTag(parameterIndex, STRING_TAG, NULL_TAG); 518 else 519 { 520 if (NULL_TAG.equals(x)) 521 { setWithTag(parameterIndex, NULL_STRING_TAG, x); 524 } 525 else 526 { setWithTag(parameterIndex, STRING_TAG, x); 529 } 530 } 531 } 532 } 533 534 542 public void setBytes(int parameterIndex, byte[] x) throws SQLException 543 { 544 String blob; 545 try 546 { 547 synchronized (sbuf) 548 { 549 if (connection.isDriverProcessed()) 550 { 551 552 blob = connection.getBlobFilter().encode(x); 553 sbuf.setLength(0); 554 sbuf.append(connection.escapeChar); 555 sbuf.append(blob); 556 sbuf.append(connection.escapeChar); 557 set(parameterIndex, sbuf.toString()); 558 } 559 else 560 { 561 566 blob = new HexaBlobFilter().encode(x); 567 setWithTag(parameterIndex, BYTES_TAG, blob); 568 } 569 } 570 } 571 catch (OutOfMemoryError oome) 572 { 573 blob = null; 574 sbuf = null; 575 System.gc(); 576 throw new SQLException ("Out of memory"); 577 } 578 } 579 580 588 public void setDate(int parameterIndex, java.sql.Date x) throws SQLException 589 { 590 if (connection.isDriverProcessed()) 591 { 592 if (x == null) 593 setNull(parameterIndex, Types.DATE); 594 else 595 set(parameterIndex, "'" + new java.sql.Date (x.getTime()).toString() 596 + "'"); 597 } 598 else 599 { 600 if (x == null) 601 setWithTag(parameterIndex, DATE_TAG, NULL_TAG); 602 else 603 setWithTag(parameterIndex, DATE_TAG, new java.sql.Date (x.getTime()) 604 .toString()); 605 } 606 } 607 608 616 public void setTime(int parameterIndex, Time x) throws SQLException 617 { 618 if (connection.isDriverProcessed()) 619 { 620 if (x == null) 621 setNull(parameterIndex, Types.TIME); 622 else 623 set(parameterIndex, "{t '" + x.toString() + "'}"); 624 } 625 else 626 { 627 if (x == null) 628 setWithTag(parameterIndex, TIME_TAG, NULL_TAG); 629 else 630 setWithTag(parameterIndex, TIME_TAG, x.toString()); 631 } 632 } 633 634 642 public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException 643 { 644 if (connection.isDriverProcessed()) 645 { 646 if (x == null) 647 setNull(parameterIndex, Types.TIMESTAMP); 648 else 649 { 650 if (x.getClass().equals(Timestamp .class)) 653 set(parameterIndex, "'" + x.toString() + "'"); 654 else 655 set(parameterIndex, "'" + new Timestamp (x.getTime()).toString() + "'"); 656 } 657 } 658 else 659 { 660 if (x == null) 661 setWithTag(parameterIndex, TIMESTAMP_TAG, NULL_TAG); 662 else 663 { 664 if (x.getClass().equals(Timestamp .class)) 665 setWithTag(parameterIndex, TIMESTAMP_TAG, x.toString()); 666 else 667 setWithTag(parameterIndex, TIMESTAMP_TAG, new Timestamp (x.getTime()) 668 .toString()); 669 } 670 } 671 } 672 673 688 public void setAsciiStream(int parameterIndex, InputStream x, int length) 689 throws SQLException 690 { 691 setBinaryStream(parameterIndex, x, length); 692 } 693 694 711 public void setUnicodeStream(int parameterIndex, InputStream x, int length) 712 throws SQLException 713 { 714 setBinaryStream(parameterIndex, x, length); 715 } 716 717 734 public void setBinaryStream(int parameterIndex, InputStream inStreamArg, 735 int length) throws SQLException 736 { 737 byte[] data = new byte[length]; 738 try 739 { 740 inStreamArg.read(data, 0, length); 741 } 742 catch (Exception ioe) 743 { 744 throw new SQLException ("Problem with streaming of data"); 745 } 746 setBytes(parameterIndex, data); 747 } 748 749 758 public void clearParameters() throws SQLException 759 { 760 int i; 761 762 for (i = 0; i < inStrings.length; i++) 763 inStrings[i] = null; 764 } 765 766 788 public void setObject(int parameterIndex, Object x, int targetSqlType, 789 int scale) throws SQLException 790 { 791 if (x == null) 792 { 793 setNull(parameterIndex, targetSqlType); 794 return; 795 } 796 797 try 798 { 799 boolean failed = false; 800 switch (targetSqlType) 801 { 802 808 case Types.TINYINT : 810 case Types.SMALLINT : 811 case Types.INTEGER : 812 if (x instanceof Number ) 813 setInt(parameterIndex, ((Number ) x).intValue()); 814 else if (x instanceof Boolean ) 815 setInt(parameterIndex, ((Boolean ) x).booleanValue() ? 1 : 0); 816 else if (x instanceof String ) 817 setInt(parameterIndex, Integer.parseInt((String ) x)); 818 else 819 failed = true; 820 break; 821 case Types.BIGINT : 822 if (x instanceof Number ) 823 setLong(parameterIndex, ((Number ) x).longValue()); 824 else if (x instanceof String ) 825 setLong(parameterIndex, Long.parseLong((String ) x)); 826 else if (x instanceof Boolean ) 827 setLong(parameterIndex, ((Boolean ) x).booleanValue() ? 1 : 0); 828 else 829 failed = true; 830 break; 831 case Types.REAL : 832 case Types.FLOAT : 833 case Types.DOUBLE : 834 case Types.DECIMAL : 835 case Types.NUMERIC : 836 if (connection.isDriverProcessed()) 839 set(parameterIndex, x.toString()); 840 else 841 setWithTag(parameterIndex, STRING_TAG, x.toString()); 842 break; 843 case Types.BIT : 844 case Types.BOOLEAN : 845 if (x instanceof Number ) 846 setBoolean(parameterIndex, 0 != ((Number ) x).longValue()); 847 else if (x instanceof Boolean ) 848 setBoolean(parameterIndex, ((Boolean ) x).booleanValue()); 849 else if (x instanceof String ) 850 setBoolean(parameterIndex, Boolean.valueOf((String ) x) 851 .booleanValue()); 852 else 853 failed = true; 854 break; 855 case Types.CHAR : 856 case Types.VARCHAR : 857 case Types.LONGVARCHAR : 858 setString(parameterIndex, x.toString()); 859 break; 860 case Types.BINARY : 861 case Types.VARBINARY : 862 case Types.LONGVARBINARY : 863 if (x instanceof byte[]) 864 setBytes(parameterIndex, (byte[]) x); 865 else if (x instanceof Blob) 866 setBlob(parameterIndex, (Blob) x); 867 else if (x instanceof Serializable ) 868 setObject(parameterIndex, x); 870 else 871 failed = true; 872 break; 873 case Types.DATE : 874 if (x instanceof String ) 875 setDate(parameterIndex, java.sql.Date.valueOf((String ) x)); 876 else if (x instanceof java.sql.Date ) 877 setDate(parameterIndex, (java.sql.Date ) x); 878 else if (x instanceof Timestamp ) 879 setDate(parameterIndex, 880 new java.sql.Date (((Timestamp ) x).getTime())); 881 else 882 failed = true; 883 break; 884 case Types.TIME : 885 if (x instanceof String ) 886 setTime(parameterIndex, Time.valueOf((String ) x)); 887 else if (x instanceof Time ) 888 setTime(parameterIndex, (Time ) x); 889 else if (x instanceof Timestamp ) 890 setTime(parameterIndex, new Time (((Timestamp ) x).getTime())); 891 else 892 failed = true; 893 break; 894 case Types.TIMESTAMP : 895 if (x instanceof String ) 896 setTimestamp(parameterIndex, Timestamp.valueOf((String ) x)); 897 else if (x instanceof Date ) 898 setTimestamp(parameterIndex, new Timestamp (((Date ) x).getTime())); 899 else if (x instanceof Timestamp ) 900 setTimestamp(parameterIndex, (Timestamp ) x); 901 else 902 failed = true; 903 break; 904 case Types.BLOB : 905 if (x instanceof Blob) 906 setBlob(parameterIndex, (Blob) x); 907 else 908 failed = true; 909 break; 910 case Types.DATALINK : 911 if (x instanceof java.net.URL ) 912 setURL(parameterIndex, (java.net.URL ) x); 913 else 914 setURL(parameterIndex, new java.net.URL (x.toString())); 915 break; 916 case Types.JAVA_OBJECT : 917 case Types.OTHER : 918 setObject(parameterIndex, x); 919 break; 920 default : 921 throw new SQLException ("Unsupported type value"); 922 } 923 if (true == failed) 924 throw new IllegalArgumentException ( 925 "Attempt to perform an illegal conversion"); 926 } 927 catch (Exception e) 928 { 929 SQLException outE = new SQLException ("Exception while converting type " 930 + x.getClass() + " to SQL type " + targetSqlType); 931 outE.initCause(e); 932 throw outE; 933 } 934 } 935 936 939 public void setObject(int parameterIndex, Object x, int targetSqlType) 940 throws SQLException 941 { 942 setObject(parameterIndex, x, targetSqlType, 0); 943 } 944 945 952 public void setObject(int parameterIndex, Object x) throws SQLException 953 { 954 if (x == null) 955 { 956 if (connection.isDriverProcessed()) 957 setNull(parameterIndex, Types.JAVA_OBJECT); 958 else 959 setWithTag(parameterIndex, OBJECT_TAG, NULL_TAG); 960 } 961 else 962 { 963 if (x instanceof String ) 964 setString(parameterIndex, (String ) x); 965 else if (x instanceof BigDecimal ) 966 setBigDecimal(parameterIndex, (BigDecimal ) x); 967 else if (x instanceof Short ) 968 setShort(parameterIndex, ((Short ) x).shortValue()); 969 else if (x instanceof Integer ) 970 setInt(parameterIndex, ((Integer ) x).intValue()); 971 else if (x instanceof Long ) 972 setLong(parameterIndex, ((Long ) x).longValue()); 973 else if (x instanceof Float ) 974 setFloat(parameterIndex, ((Float ) x).floatValue()); 975 else if (x instanceof Double ) 976 setDouble(parameterIndex, ((Double ) x).doubleValue()); 977 else if (x instanceof byte[]) 978 setBytes(parameterIndex, (byte[]) x); 979 else if (x instanceof java.sql.Date ) 980 setDate(parameterIndex, (java.sql.Date ) x); 981 else if (x instanceof Time ) 982 setTime(parameterIndex, (Time ) x); 983 else if (x instanceof Timestamp ) 984 setTimestamp(parameterIndex, (Timestamp ) x); 985 else if (x instanceof Boolean ) 986 setBoolean(parameterIndex, ((Boolean ) x).booleanValue()); 987 else if (x instanceof Blob) 988 setBlob(parameterIndex, (Blob) x); 989 else if (x instanceof java.net.URL ) 990 setURL(parameterIndex, (java.net.URL ) x); 991 else if (x instanceof Serializable ) 992 { 993 ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream (); 994 try 995 { 996 ObjectOutputStream objectOutputStream = new ObjectOutputStream ( 998 byteOutputStream); 999 objectOutputStream.writeObject(x); 1000 objectOutputStream.flush(); 1001 objectOutputStream.close(); 1002 if (connection.isDriverProcessed()) 1003 setBytes(parameterIndex, byteOutputStream.toByteArray()); 1004 else 1005 synchronized (sbuf) 1006 { 1007 sbuf.setLength(0); 1008 sbuf.append(byteOutputStream); 1009 setWithTag(parameterIndex, OBJECT_TAG, sbuf.toString()); 1010 } 1011 } 1012 catch (IOException e) 1013 { 1014 throw new SQLException ("Failed to serialize object: " + e); 1015 } 1016 } 1017 else 1018 throw new SQLException ("Objects of type " + x.getClass() 1019 + " are not supported."); 1020 } 1021 } 1022 1023 1033 public boolean execute() throws SQLException 1034 { 1035 int start = 0; 1036 try 1037 { 1038 while (sql.charAt(start) == '(') 1040 start++; 1041 } 1042 catch (IndexOutOfBoundsException e) 1043 { 1044 start = 0; 1047 } 1048 1049 if (sql.regionMatches(true, start, "select", 0, 6) 1050 || (sql.regionMatches(true, start, "{call", 0, 5))) 1051 { 1052 result = executeQuery(sql, compileQuery()); 1053 return true; 1054 } 1055 else 1056 { 1057 updateCount = executeUpdateWithSkeleton(sql, compileQuery()); 1058 return false; 1059 } 1060 } 1061 1062 1071 public String toString() 1072 { 1073 synchronized (sbuf) 1074 { 1075 sbuf.setLength(0); 1076 int i; 1077 1078 for (i = 0; i < inStrings.length; ++i) 1079 { 1080 if (inStrings[i] == null) 1081 sbuf.append('?'); 1082 else 1083 sbuf.append(templateStrings[i]); 1084 sbuf.append(inStrings[i]); 1085 } 1086 sbuf.append(templateStrings[inStrings.length]); 1087 return sbuf.toString(); 1088 } 1089 } 1090 1091 1093 1098 public synchronized void addBatch() throws SQLException 1099 { 1100 if (batch == null) 1101 batch = new Vector (); 1102 batch.addElement(new BatchElement(sql, compileQuery())); 1103 } 1104 1105 1115 public int[] executeBatch() throws BatchUpdateException 1116 { 1117 if (batch == null || batch.isEmpty()) 1118 return new int[0]; 1119 1120 int size = batch.size(); 1121 int[] nbsRowsUpdated = new int[size]; 1122 int i = 0; 1123 1124 try 1125 { 1126 for (i = 0; i < size; i++) 1127 { 1128 BatchElement be = (BatchElement) batch.elementAt(i); 1129 nbsRowsUpdated[i] = this.executeUpdateWithSkeleton(be.getSqlTemplate(), 1130 be.getCompiledSql()); 1131 } 1132 return nbsRowsUpdated; 1133 } 1134 catch (SQLException e) 1135 { 1136 String message = "Batch failed for request " + i + ": " 1137 + ((BatchElement) batch.elementAt(i)).getCompiledSql() + " (" + e 1138 + ")"; 1139 1140 int[] updateCounts = new int[i]; 1142 System.arraycopy(nbsRowsUpdated, 0, updateCounts, 0, i); 1143 1144 throw new BatchUpdateException (message, updateCounts); 1145 } 1146 finally 1147 { 1148 batch.removeAllElements(); 1149 } 1150 } 1151 1152 1159 public java.sql.ResultSetMetaData getMetaData() throws SQLException 1160 { 1161 java.sql.ResultSet rs = getResultSet(); 1162 if (rs != null) 1163 return rs.getMetaData(); 1164 1165 return null; 1167 } 1168 1169 1172 public void setArray(int i, Array x) throws SQLException 1173 { 1174 throw new NotImplementedException("setArray()"); 1175 } 1176 1177 1180 public void setBlob(int paramIndex, java.sql.Blob sqlBlobParam) 1181 throws SQLException 1182 { 1183 if (sqlBlobParam == null) 1184 { 1185 if (connection.isDriverProcessed()) 1186 setNull(paramIndex, Types.BLOB); 1187 else 1188 setWithTag(paramIndex, BLOB_TAG, NULL_TAG); 1189 return; 1190 } 1191 1192 InputStream blobBinStream = sqlBlobParam.getBinaryStream(); 1195 1196 if (connection.isDriverProcessed()) 1197 setBinaryStream(paramIndex, blobBinStream, (int) sqlBlobParam.length()); 1198 else 1199 { 1200 byte[] data = new byte[(int) sqlBlobParam.length()]; 1201 try 1202 { 1203 blobBinStream.read(data, 0, (int) sqlBlobParam.length()); 1204 } 1205 catch (Exception ioe) 1206 { 1207 throw new SQLException ("Problem with data streaming"); 1208 } 1209 try 1210 { 1211 synchronized (this.sbuf) 1212 { 1213 this.sbuf.setLength(0); 1214 1219 this.sbuf.append(new HexaBlobFilter().encode(data)); 1220 setWithTag(paramIndex, BLOB_TAG, this.sbuf.toString()); 1221 } 1222 } 1223 catch (OutOfMemoryError oome) 1224 { 1225 this.sbuf = null; 1226 System.gc(); 1227 throw new SQLException ("Out of memory"); 1228 } 1229 1230 } 1231 } 1232 1233 1237 public void setCharacterStream(int i, java.io.Reader x, int length) 1238 throws SQLException 1239 { 1240 char[] data = new char[length]; 1241 try 1242 { 1243 x.read(data, 0, length); 1244 } 1245 catch (Exception ioe) 1246 { 1247 throw new SQLException ("Problem with streaming of data"); 1248 } 1249 setString(i, new String (data)); 1250 } 1251 1252 1255 public void setClob(int i, java.sql.Clob clobArg) throws SQLException 1256 { 1257 if (clobArg == null) 1258 { 1259 if (connection.isDriverProcessed()) 1260 setNull(i, Types.CLOB); 1261 else 1262 setWithTag(i, CLOB_TAG, NULL_TAG); 1263 return; 1264 } 1265 if (connection.isDriverProcessed()) 1266 setString(i, clobArg.getSubString(0, (int) clobArg.length())); 1267 else 1268 setWithTag(i, CLOB_TAG, clobArg.getSubString(0, (int) clobArg.length())); 1269 } 1270 1271 1274 public void setNull(int i, int t, String s) throws SQLException 1275 { 1276 setNull(i, t); 1277 } 1278 1279 1282 public void setRef(int i, Ref x) throws SQLException 1283 { 1284 if (connection.isDriverProcessed()) 1285 { 1286 if (x == null) 1287 setNull(i, Types.REF); 1288 else 1289 set(i, x.toString()); 1290 } 1291 else 1292 { 1293 if (x == null) 1294 setWithTag(i, REF_TAG, NULL_TAG); 1295 else 1296 setWithTag(i, REF_TAG, x.toString()); 1297 } 1298 } 1299 1300 1304 public void setDate(int i, java.sql.Date d, java.util.Calendar cal) 1305 throws SQLException 1306 { 1307 if (d == null) 1308 { 1309 if (connection.isDriverProcessed()) 1310 setNull(i, Types.DATE); 1311 else 1312 setWithTag(i, DATE_TAG, NULL_TAG); 1313 return; 1314 } 1315 else 1316 { 1317 if (cal == null) 1318 setDate(i, d); 1319 else 1320 { 1321 cal.setTime(d); 1322 setDate(i, new java.sql.Date (cal.getTime().getTime())); 1323 } 1324 } 1325 } 1326 1327 1331 public void setTime(int i, Time t, java.util.Calendar cal) 1332 throws SQLException 1333 { 1334 if (t == null) 1335 { 1336 if (connection.isDriverProcessed()) 1337 setNull(i, Types.TIME); 1338 else 1339 setWithTag(i, TIME_TAG, NULL_TAG); 1340 return; 1341 } 1342 else 1343 { 1344 if (cal == null) 1345 setTime(i, t); 1346 else 1347 { 1348 cal.setTime(t); 1349 setTime(i, new java.sql.Time (cal.getTime().getTime())); 1350 } 1351 } 1352 } 1353 1354 1358 public void setTimestamp(int i, Timestamp t, java.util.Calendar cal) 1359 throws SQLException 1360 { 1361 if (t == null) 1362 { 1363 if (connection.isDriverProcessed()) 1364 setNull(i, Types.TIMESTAMP); 1365 else 1366 setWithTag(i, TIMESTAMP_TAG, NULL_TAG); 1367 return; 1368 } 1369 else 1370 { 1371 if (cal == null) 1372 setTimestamp(i, t); 1373 else 1374 { 1375 cal.setTime(t); 1376 setTimestamp(i, new java.sql.Timestamp (cal.getTime().getTime())); 1377 } 1378 } 1379 } 1380 1381 1383 1393 public void setURL(int parameterIndex, java.net.URL x) throws SQLException 1394 { 1395 if (connection.isDriverProcessed()) 1396 { 1397 if (x == null) 1398 setNull(parameterIndex, Types.OTHER); 1399 else 1400 set(parameterIndex, x.toString()); 1401 } 1402 else 1403 { 1404 if (x == null) 1405 setWithTag(parameterIndex, URL_TAG, NULL_TAG); 1406 else 1407 setWithTag(parameterIndex, URL_TAG, x.toString()); 1408 } 1409 } 1410 1411 1422 public ParameterMetaData getParameterMetaData() throws SQLException 1423 { 1424 throw new NotImplementedException("getParameterMetaData"); 1425 } 1426 1427 1431 1439 private void set(int paramIndex, String s) throws SQLException 1440 { 1441 if (paramIndex < 1 || paramIndex > inStrings.length) 1442 throw new SQLException ("Parameter index out of range."); 1443 inStrings[paramIndex - 1] = s; 1444 } 1445 1446 1461 private void setWithTag(int paramIndex, String typeTag, String param) 1462 throws SQLException 1463 { 1464 1468 set(paramIndex, START_PARAM_TAG + typeTag 1469 + Strings.replace(param, TAG_MARKER, TAG_MARKER_ESCAPE) + END_PARAM_TAG); 1470 } 1471 1472 1479 protected void setGeneratedKeysFlag(int autoGeneratedKeys) 1480 { 1481 generatedKeysFlag = autoGeneratedKeys; 1482 } 1483 1484 1495 public static void setPreparedStatement(String quotedRequest, 1496 java.sql.PreparedStatement backendPS) throws SQLException 1497 { 1498 int i = 0; 1499 int paramIdx = 0; 1500 1501 while ((i = quotedRequest.indexOf(START_PARAM_TAG, i)) > -1) 1503 { 1504 paramIdx++; 1505 1506 int typeStart = i + START_PARAM_TAG.length(); 1507 1508 String paramType = quotedRequest.substring(typeStart, typeStart 1510 + BOOLEAN_TAG.length()); 1511 String paramValue = quotedRequest.substring(typeStart 1512 + BOOLEAN_TAG.length(), quotedRequest.indexOf(END_PARAM_TAG, i)); 1513 paramValue = Strings.replace(paramValue, TAG_MARKER_ESCAPE, TAG_MARKER); 1514 1515 if (paramType.equals(BIG_DECIMAL_TAG)) 1517 { 1518 if (paramValue.equals(NULL_TAG)) 1519 backendPS.setBigDecimal(paramIdx, null); 1520 else 1521 { 1522 BigDecimal t = new BigDecimal (paramValue); 1523 backendPS.setBigDecimal(paramIdx, t); 1524 } 1525 } 1526 else if (paramType.equals(BOOLEAN_TAG)) 1527 backendPS.setBoolean(paramIdx, Boolean.valueOf(paramValue) 1528 .booleanValue()); 1529 else if (paramType.equals(BYTE_TAG)) 1530 { 1531 byte t = new Integer (paramValue).byteValue(); 1532 backendPS.setByte(paramIdx, t); 1533 } 1534 else if (paramType.equals(BYTES_TAG)) 1535 { 1536 1540 byte[] t = new HexaBlobFilter().decode(paramValue); 1541 backendPS.setBytes(paramIdx, t); 1542 } 1543 else if (paramType.equals(BLOB_TAG)) 1544 { 1545 if (paramValue.equals(NULL_TAG)) 1546 backendPS.setBlob(paramIdx, null); 1547 else 1548 { 1549 1552 Blob b = new Blob(new HexaBlobFilter().decode(paramValue)); 1553 backendPS.setBlob(paramIdx, b); 1554 } 1555 } 1556 else if (paramType.equals(CLOB_TAG)) 1557 { 1558 if (paramValue.equals(NULL_TAG)) 1559 backendPS.setClob(paramIdx, null); 1560 else 1561 { 1562 Clob c = new Clob(paramValue); 1563 backendPS.setClob(paramIdx, c); 1564 } 1565 } 1566 else if (paramType.equals(DATE_TAG)) 1567 { 1568 if (paramValue.equals(NULL_TAG)) 1569 backendPS.setDate(paramIdx, null); 1570 else 1571 try 1572 { 1573 SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd"); 1574 Date t = new Date (sdf.parse(paramValue).getTime()); 1575 backendPS.setDate(paramIdx, t); 1576 } 1577 catch (ParseException p) 1578 { 1579 backendPS.setDate(paramIdx, null); 1580 throw new SQLException ("Couldn't format date!!!"); 1581 } 1582 } 1583 else if (paramType.equals(DOUBLE_TAG)) 1584 backendPS.setDouble(paramIdx, Double.valueOf(paramValue).doubleValue()); 1585 else if (paramType.equals(FLOAT_TAG)) 1586 backendPS.setFloat(paramIdx, Float.valueOf(paramValue).floatValue()); 1587 else if (paramType.equals(INTEGER_TAG)) 1588 backendPS.setInt(paramIdx, Integer.valueOf(paramValue).intValue()); 1589 else if (paramType.equals(LONG_TAG)) 1590 backendPS.setLong(paramIdx, Long.valueOf(paramValue).longValue()); 1591 else if (paramType.equals(NULL_TAG)) 1592 backendPS.setNull(paramIdx, Integer.valueOf(paramValue).intValue()); 1593 else if (paramType.equals(OBJECT_TAG)) 1594 { 1595 if (paramValue.equals(NULL_TAG)) 1596 backendPS.setObject(paramIdx, null); 1597 else 1598 { 1599 try 1600 { 1601 ObjectInputStream in = new ObjectInputStream ( 1602 new ByteArrayInputStream (paramValue.getBytes())); 1603 backendPS.setObject(paramIdx, in.readObject()); 1604 in.close(); 1605 } 1606 catch (Exception e) 1607 { 1608 throw new SQLException ("Failed to rebuild object from stream " + e); 1609 } 1610 } 1611 } 1612 else if (paramType.equals(REF_TAG)) 1613 { 1614 if (paramValue.equals(NULL_TAG)) 1615 backendPS.setRef(paramIdx, null); 1616 else 1617 throw new SQLException ("Ref type not supported"); 1618 } 1619 else if (paramType.equals(SHORT_TAG)) 1620 { 1621 short t = new Integer (paramValue).shortValue(); 1622 backendPS.setShort(paramIdx, t); 1623 } 1624 else if (paramType.equals(STRING_TAG)) 1625 { backendPS.setString(paramIdx, paramValue); 1627 } 1628 else if (paramType.equals(NULL_STRING_TAG)) 1629 { 1630 backendPS.setString(paramIdx, null); 1631 } 1632 else if (paramType.equals(TIME_TAG)) 1633 { 1634 if (paramValue.equals(NULL_TAG)) 1635 backendPS.setTime(paramIdx, null); 1636 else 1637 try 1638 { 1639 SimpleDateFormat sdf = new SimpleDateFormat ("HH:mm:ss"); 1640 Time t = new Time (sdf.parse(paramValue).getTime()); 1641 backendPS.setTime(paramIdx, t); 1642 } 1643 catch (ParseException p) 1644 { 1645 backendPS.setTime(paramIdx, null); 1646 throw new SQLException ("Couldn't format time!!!"); 1647 } 1648 } 1649 else if (paramType.equals(TIMESTAMP_TAG)) 1650 { 1651 if (paramValue.equals(NULL_TAG)) 1652 backendPS.setTimestamp(paramIdx, null); 1653 else 1654 try 1655 { 1656 SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss.S"); 1657 Timestamp t = new Timestamp (sdf.parse(paramValue).getTime()); 1658 backendPS.setTimestamp(paramIdx, t); 1659 } 1660 catch (ParseException p) 1661 { 1662 backendPS.setTimestamp(paramIdx, null); 1663 throw new SQLException ("Couldn't format timestamp!!!"); 1664 } 1665 } 1666 else if (paramType.equals(URL_TAG)) 1667 { 1668 if (paramValue.equals(NULL_TAG)) 1669 backendPS.setURL(paramIdx, null); 1670 else 1671 try 1672 { 1673 backendPS.setURL(paramIdx, new URL (paramValue)); 1674 } 1675 catch (MalformedURLException e) 1676 { 1677 throw new SQLException ("Unable to create URL " + paramValue + " (" 1678 + e + ")"); 1679 } 1680 } 1681 else 1682 { 1683 paramIdx--; 1686 } 1687 i = typeStart; 1688 } 1689 } 1690 1691 1699 private class BatchElement 1700 { 1701 private String sqlTemplate; 1702 private String compiledSql; 1703 1704 1710 public BatchElement(String sqlTemplate, String compiledSql) 1711 { 1712 this.sqlTemplate = sqlTemplate; 1713 this.compiledSql = compiledSql; 1714 } 1715 1716 1721 public String getCompiledSql() 1722 { 1723 return compiledSql; 1724 } 1725 1726 1731 public String getSqlTemplate() 1732 { 1733 return sqlTemplate; 1734 } 1735 } 1736} 1737 | Popular Tags |