src/org/objectweb/cjdbc/driver/PreparedStatement.java

説明を見る。
00001 00025 package org.objectweb.cjdbc.driver; 00026 00027 import java.io.ByteArrayInputStream; 00028 import java.io.ByteArrayOutputStream; 00029 import java.io.IOException; 00030 import java.io.InputStream; 00031 import java.io.ObjectInputStream; 00032 import java.io.ObjectOutputStream; 00033 import java.io.Serializable; 00034 import java.math.BigDecimal; 00035 import java.sql.Array; 00036 import java.sql.Date; 00037 import java.sql.ParameterMetaData; 00038 import java.sql.Ref; 00039 import java.sql.SQLException; 00040 import java.sql.Time; 00041 import java.sql.Timestamp; 00042 import java.sql.Types; 00043 import java.text.ParseException; 00044 import java.text.SimpleDateFormat; 00045 import java.util.ArrayList; 00046 00047 import org.objectweb.cjdbc.common.sql.NotImplementedException; 00048 00073 public class PreparedStatement extends Statement 00074 implements 00075 java.sql.PreparedStatement 00076 { 00078 public static final String BYTE_TAG = "b|"; 00080 public static final String BYTES_TAG = "B|"; 00082 public static final String BLOB_TAG = "c|"; 00084 public static final String CLOB_TAG = "C|"; 00086 public static final String BOOLEAN_TAG = "0|"; 00088 public static final String BIG_DECIMAL_TAG = "1|"; 00090 public static final String DATE_TAG = "d|"; 00092 public static final String DOUBLE_TAG = "D|"; 00094 public static final String FLOAT_TAG = "F|"; 00096 public static final String INTEGER_TAG = "I|"; 00098 public static final String LONG_TAG = "L|"; 00100 public static final String NULL_TAG = "N|"; 00102 public static final String OBJECT_TAG = "O|"; 00104 public static final String SHORT_TAG = "s|"; 00106 public static final String STRING_TAG = "S|"; 00108 public static final String TIME_TAG = "t|"; 00110 public static final String TIMESTAMP_TAG = "T|"; 00111 00112 protected String sql; 00113 private String[] templateStrings; 00114 private String[] inStrings; 00115 00116 // Some performance caches 00117 private StringBuffer sbuf = new StringBuffer(); 00118 00128 public PreparedStatement(Connection connection, String sqlStatement) 00129 throws SQLException 00130 { 00131 super(connection); 00132 00133 ArrayList v = new ArrayList(); 00134 int lastParmEnd = 0; 00135 00136 // The following two boolean switches are used to make sure we're not 00137 // counting "?" in either strings or metadata strings. For instance the 00138 // following query: 00139 // select '?' "A ? value" from dual 00140 // doesn't have any parameters. 00141 00142 boolean inString = false; 00143 boolean inMetaString = false; 00144 00145 this.sql = sqlStatement.trim(); 00146 this.connection = connection; 00147 for (int i = 0; i < sql.length(); ++i) 00148 { 00149 if (sql.charAt(i) == '\'') 00150 inString = !inString; 00151 if (sql.charAt(i) == '"') 00152 inMetaString = !inMetaString; 00153 if ((sql.charAt(i) == '?') && (!(inString || inMetaString))) 00154 { 00155 v.add(sql.substring(lastParmEnd, i)); 00156 lastParmEnd = i + 1; 00157 } 00158 } 00159 v.add(sql.substring(lastParmEnd, sql.length())); 00160 00161 int size = v.size(); 00162 templateStrings = new String[size]; 00163 inStrings = new String[size - 1]; 00164 clearParameters(); 00165 00166 for (int i = 0; i < size; ++i) 00167 templateStrings[i] = (String) v.get(i); 00168 } 00169 00175 public void close() throws SQLException 00176 { 00177 sql = null; 00178 templateStrings = null; 00179 inStrings = null; 00180 00181 super.close(); 00182 } 00183 00192 public java.sql.ResultSet executeQuery() throws SQLException 00193 { 00194 return super.executeQuery(sql, compileQuery()); // in Statement class 00195 } 00196 00206 public int executeUpdate() throws SQLException 00207 { 00208 return super.executeUpdateWithSkeleton(sql, compileQuery()); 00209 // in Statement class 00210 } 00211 00220 protected synchronized String compileQuery() throws SQLException 00221 { 00222 sbuf.setLength(0); 00223 int i; 00224 00225 for (i = 0; i < inStrings.length; ++i) 00226 { 00227 if (inStrings[i] == null) 00228 throw new SQLException("Parameter " + (i + 1) + " is incorrect"); 00229 sbuf.append(templateStrings[i]).append(inStrings[i]); 00230 } 00231 sbuf.append(templateStrings[inStrings.length]); 00232 return sbuf.toString(); 00233 } 00234 00245 protected String doEscapeProcessing(String x) 00246 { 00247 // use the shared buffer object. Should never clash but this 00248 // makes us thread safe! 00249 synchronized (sbuf) 00250 { 00251 sbuf.setLength(0); 00252 int i; 00253 sbuf.append(connection.getEscapeChar()); 00254 for (i = 0; i < x.length(); ++i) 00255 { 00256 char c = x.charAt(i); 00257 if ((c == '\'' && connection.isEscapeSingleQuote()) 00258 || (c == '\\' && connection.isEscapeBackslash())) 00259 sbuf.append("" + c); 00260 sbuf.append(c); 00261 } 00262 sbuf.append(connection.getEscapeChar()); 00263 } 00264 return sbuf.toString(); 00265 } 00266 00276 public void setNull(int parameterIndex, int sqlType) throws SQLException 00277 { 00278 if (connection.isDriverProcessed()) 00279 set(parameterIndex, "null"); 00280 else 00281 set(parameterIndex, Connection.startParamTag + NULL_TAG + sqlType 00282 + Connection.endParamTag); 00283 } 00284 00293 public void setBoolean(int parameterIndex, boolean x) throws SQLException 00294 { 00295 if (connection.isDriverProcessed()) 00296 { 00297 set(parameterIndex, x 00298 ? connection.getPreparedStatementBooleanTrue() 00299 : connection.getPreparedStatementBooleanFalse()); 00300 } 00301 else 00302 { 00303 set(parameterIndex, Connection.startParamTag + BOOLEAN_TAG + x 00304 + Connection.endParamTag); 00305 } 00306 } 00307 00315 public void setByte(int parameterIndex, byte x) throws SQLException 00316 { 00317 if (connection.isDriverProcessed()) 00318 { 00319 set(parameterIndex, Integer.toString(x)); 00320 } 00321 else 00322 { 00323 set(parameterIndex, Connection.startParamTag + BYTE_TAG 00324 + Integer.toString(x) + Connection.endParamTag); 00325 } 00326 } 00327 00336 public void setShort(int parameterIndex, short x) throws SQLException 00337 { 00338 if (connection.isDriverProcessed()) 00339 { 00340 set(parameterIndex, Integer.toString(x)); 00341 } 00342 else 00343 { 00344 set(parameterIndex, Connection.startParamTag + SHORT_TAG 00345 + Integer.toString(x) + Connection.endParamTag); 00346 } 00347 } 00348 00357 public void setInt(int parameterIndex, int x) throws SQLException 00358 { 00359 if (connection.isDriverProcessed()) 00360 { 00361 set(parameterIndex, Integer.toString(x)); 00362 } 00363 else 00364 { 00365 set(parameterIndex, Connection.startParamTag + INTEGER_TAG 00366 + Integer.toString(x) + Connection.endParamTag); 00367 } 00368 } 00369 00378 public void setLong(int parameterIndex, long x) throws SQLException 00379 { 00380 if (connection.isDriverProcessed()) 00381 { 00382 set(parameterIndex, Long.toString(x)); 00383 } 00384 else 00385 { 00386 set(parameterIndex, Connection.startParamTag + LONG_TAG 00387 + Long.toString(x) + Connection.endParamTag); 00388 } 00389 } 00390 00399 public void setFloat(int parameterIndex, float x) throws SQLException 00400 { 00401 if (connection.isDriverProcessed()) 00402 { 00403 set(parameterIndex, Float.toString(x)); 00404 } 00405 else 00406 { 00407 set(parameterIndex, Connection.startParamTag + FLOAT_TAG 00408 + Float.toString(x) + Connection.endParamTag); 00409 } 00410 } 00411 00420 public void setDouble(int parameterIndex, double x) throws SQLException 00421 { 00422 if (connection.isDriverProcessed()) 00423 { 00424 set(parameterIndex, Double.toString(x)); 00425 } 00426 else 00427 { 00428 set(parameterIndex, Connection.startParamTag + DOUBLE_TAG 00429 + Double.toString(x) + Connection.endParamTag); 00430 } 00431 } 00432 00441 public void setBigDecimal(int parameterIndex, BigDecimal x) 00442 throws SQLException 00443 { 00444 if (connection.isDriverProcessed()) 00445 { 00446 if (x == null) 00447 setNull(parameterIndex, Types.DECIMAL); 00448 else 00449 set(parameterIndex, x.toString()); 00450 } 00451 else 00452 { 00453 if (x == null) 00454 set(parameterIndex, Connection.startParamTag + BIG_DECIMAL_TAG + "null" 00455 + Connection.endParamTag); 00456 else 00457 set(parameterIndex, Connection.startParamTag + BIG_DECIMAL_TAG 00458 + x.toString() + Connection.endParamTag); 00459 } 00460 } 00461 00471 public void setString(int parameterIndex, String x) throws SQLException 00472 { 00473 if (connection.isDriverProcessed()) 00474 { 00475 if (x == null) 00476 // if the passed string is null, then set this column to null 00477 setNull(parameterIndex, Types.VARCHAR); 00478 else 00479 { 00480 if (escapeProcessing 00481 && (connection.isEscapeBackslash() || connection 00482 .isEscapeSingleQuote())) 00483 set(parameterIndex, doEscapeProcessing(x)); 00484 else 00485 // No escape processing 00486 set(parameterIndex, x); 00487 } 00488 } 00489 else 00490 { 00491 if (x == null) 00492 set(parameterIndex, Connection.startParamTag + STRING_TAG + "null" 00493 + Connection.endParamTag); 00494 else 00495 // No escape processing is needed for queries not being parsed into 00496 // statements. 00497 set(parameterIndex, Connection.startParamTag + STRING_TAG + x 00498 + Connection.endParamTag); 00499 } 00500 } 00501 00514 public void setBytes(int parameterIndex, byte x[]) throws SQLException 00515 { 00516 String blob; 00517 try 00518 { 00519 synchronized (sbuf) 00520 { 00521 if (connection.isDriverProcessed()) 00522 { 00523 blob = connection.getBlobFilter().encode(x); 00524 sbuf.setLength(0); 00525 sbuf.append(connection.escapeChar); 00526 sbuf.append(blob); 00527 sbuf.append(connection.escapeChar); 00528 set(parameterIndex, sbuf.toString()); 00529 } 00530 else 00531 { 00532 sbuf.setLength(0); 00533 sbuf.append(x); 00534 set(parameterIndex, Connection.startParamTag + BYTES_TAG 00535 + sbuf.toString() + Connection.endParamTag); 00536 } 00537 } 00538 } 00539 catch (OutOfMemoryError oome) 00540 { 00541 blob = null; 00542 sbuf = null; 00543 System.gc(); 00544 throw new SQLException("Out of memory"); 00545 } 00546 } 00547 00556 public void setDate(int parameterIndex, java.sql.Date x) throws SQLException 00557 { 00558 if (connection.isDriverProcessed()) 00559 { 00560 if (x == null) 00561 setNull(parameterIndex, Types.DATE); 00562 else 00563 set(parameterIndex, "'" + new java.sql.Date(x.getTime()).toString() 00564 + "'"); 00565 } 00566 else 00567 { 00568 if (x == null) 00569 set(parameterIndex, Connection.startParamTag + DATE_TAG + "null" 00570 + Connection.endParamTag); 00571 else 00572 set(parameterIndex, Connection.startParamTag + DATE_TAG 00573 + new java.sql.Date(x.getTime()).toString() 00574 + Connection.endParamTag); 00575 } 00576 } 00577 00586 public void setTime(int parameterIndex, Time x) throws SQLException 00587 { 00588 if (connection.isDriverProcessed()) 00589 { 00590 if (x == null) 00591 setNull(parameterIndex, Types.TIME); 00592 else 00593 set(parameterIndex, "{t '" + x.toString() + "'}"); 00594 } 00595 else 00596 { 00597 if (x == null) 00598 set(parameterIndex, Connection.startParamTag + TIME_TAG + "null" 00599 + Connection.endParamTag); 00600 else 00601 set(parameterIndex, Connection.startParamTag + TIME_TAG + x.toString() 00602 + Connection.endParamTag); 00603 } 00604 } 00605 00614 public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException 00615 { 00616 if (connection.isDriverProcessed()) 00617 { 00618 if (x == null) 00619 setNull(parameterIndex, Types.TIMESTAMP); 00620 else 00621 { 00622 // Be careful don't use instanceof here since it would match derived 00623 // classes. 00624 if (x.getClass().equals(Timestamp.class)) 00625 set(parameterIndex, "'" + x.toString() + "'"); 00626 else 00627 set(parameterIndex, "'" + new Timestamp(x.getTime()).toString() + "'"); 00628 } 00629 } 00630 else 00631 { 00632 if (x == null) 00633 set(parameterIndex, Connection.startParamTag + TIMESTAMP_TAG + "null" 00634 + Connection.endParamTag); 00635 else 00636 { 00637 if (x.getClass().equals(Timestamp.class)) 00638 set(parameterIndex, Connection.startParamTag + TIMESTAMP_TAG 00639 + x.toString() + Connection.endParamTag); 00640 else 00641 set(parameterIndex, Connection.startParamTag + TIMESTAMP_TAG 00642 + new Timestamp(x.getTime()).toString() + Connection.endParamTag); 00643 } 00644 } 00645 } 00646 00662 public void setAsciiStream(int parameterIndex, InputStream x, int length) 00663 throws SQLException 00664 { 00665 setBinaryStream(parameterIndex, x, length); 00666 } 00667 00685 public void setUnicodeStream(int parameterIndex, InputStream x, int length) 00686 throws SQLException 00687 { 00688 setBinaryStream(parameterIndex, x, length); 00689 } 00690 00704 public void setBinaryStream(int parameterIndex, InputStream x, int length) 00705 throws SQLException 00706 { 00707 byte[] data = new byte[length]; 00708 try 00709 { 00710 x.read(data, 0, length); 00711 } 00712 catch (Exception ioe) 00713 { 00714 throw new SQLException("Problem with streaming of data"); 00715 } 00716 setBytes(parameterIndex, data); 00717 } 00718 00728 public void clearParameters() throws SQLException 00729 { 00730 int i; 00731 00732 for (i = 0; i < inStrings.length; i++) 00733 inStrings[i] = null; 00734 } 00735 00756 public void setObject(int parameterIndex, Object x, int targetSqlType, 00757 int scale) throws SQLException 00758 { 00759 if (x == null) 00760 { 00761 setNull(parameterIndex, targetSqlType); 00762 } 00763 else 00764 { 00765 switch (targetSqlType) 00766 { 00767 case Types.TINYINT : 00768 case Types.SMALLINT : 00769 case Types.INTEGER : 00770 case Types.BIGINT : 00771 case Types.REAL : 00772 case Types.FLOAT : 00773 case Types.DOUBLE : 00774 case Types.DECIMAL : 00775 case Types.NUMERIC : 00776 // Cast to Number is not necessary 00777 set(parameterIndex, x.toString()); 00778 break; 00779 case Types.BIT : 00780 case Types.BOOLEAN : 00781 setBoolean(parameterIndex, ((Boolean) x).booleanValue()); 00782 break; 00783 case Types.CHAR : 00784 case Types.VARCHAR : 00785 case Types.LONGVARCHAR : 00786 setString(parameterIndex, (String) x); 00787 break; 00788 case Types.BINARY : 00789 case Types.VARBINARY : 00790 case Types.LONGVARBINARY : 00791 setBytes(parameterIndex, (byte[]) x); 00792 break; 00793 case Types.DATE : 00794 setDate(parameterIndex, (java.sql.Date) x); 00795 break; 00796 case Types.TIME : 00797 setTime(parameterIndex, (Time) x); 00798 break; 00799 case Types.TIMESTAMP : 00800 setTimestamp(parameterIndex, (Timestamp) x); 00801 break; 00802 case Types.BLOB : 00803 setBlob(parameterIndex, (Blob) x); 00804 break; 00805 case Types.DATALINK : 00806 setURL(parameterIndex, (java.net.URL) x); 00807 break; 00808 case Types.JAVA_OBJECT : 00809 case Types.OTHER : 00810 setObject(parameterIndex, x); 00811 break; 00812 default : 00813 throw new SQLException("Unsupported type value"); 00814 } 00815 } 00816 } 00817 00821 public void setObject(int parameterIndex, Object x, int targetSqlType) 00822 throws SQLException 00823 { 00824 setObject(parameterIndex, x, targetSqlType, 0); 00825 } 00826 00834 public void setObject(int parameterIndex, Object x) throws SQLException 00835 { 00836 if (x == null) 00837 { 00838 if (connection.isDriverProcessed()) 00839 setNull(parameterIndex, Types.JAVA_OBJECT); 00840 else 00841 set(parameterIndex, Connection.startParamTag + OBJECT_TAG + "null" 00842 + Connection.endParamTag); 00843 } 00844 else 00845 { 00846 if (x instanceof String) 00847 setString(parameterIndex, (String) x); 00848 else if (x instanceof BigDecimal) 00849 setBigDecimal(parameterIndex, (BigDecimal) x); 00850 else if (x instanceof Short) 00851 setShort(parameterIndex, ((Short) x).shortValue()); 00852 else if (x instanceof Integer) 00853 setInt(parameterIndex, ((Integer) x).intValue()); 00854 else if (x instanceof Long) 00855 setLong(parameterIndex, ((Long) x).longValue()); 00856 else if (x instanceof Float) 00857 setFloat(parameterIndex, ((Float) x).floatValue()); 00858 else if (x instanceof Double) 00859 setDouble(parameterIndex, ((Double) x).doubleValue()); 00860 else if (x instanceof byte[]) 00861 setBytes(parameterIndex, (byte[]) x); 00862 else if (x instanceof java.sql.Date) 00863 setDate(parameterIndex, (java.sql.Date) x); 00864 else if (x instanceof Time) 00865 setTime(parameterIndex, (Time) x); 00866 else if (x instanceof Timestamp) 00867 setTimestamp(parameterIndex, (Timestamp) x); 00868 else if (x instanceof Boolean) 00869 setBoolean(parameterIndex, ((Boolean) x).booleanValue()); 00870 else if (x instanceof Blob) 00871 setBlob(parameterIndex, (Blob) x); 00872 else if (x instanceof java.net.URL) 00873 setURL(parameterIndex, (java.net.URL) x); 00874 else if (x instanceof Serializable) 00875 { 00876 ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); 00877 try 00878 { 00879 // Serialize object to byte array 00880 ObjectOutputStream objectOutputStream = new ObjectOutputStream( 00881 byteOutputStream); 00882 objectOutputStream.writeObject(x); 00883 objectOutputStream.flush(); 00884 objectOutputStream.close(); 00885 if (connection.isDriverProcessed()) 00886 setBytes(parameterIndex, byteOutputStream.toByteArray()); 00887 else 00888 synchronized (sbuf) 00889 { 00890 sbuf.setLength(0); 00891 sbuf.append(byteOutputStream); 00892 set(parameterIndex, Connection.startParamTag + OBJECT_TAG 00893 + sbuf.toString() + Connection.endParamTag); 00894 } 00895 } 00896 catch (IOException e) 00897 { 00898 throw new SQLException("Failed to serialize object: " + e); 00899 } 00900 } 00901 else 00902 throw new SQLException("Objects of type " + x.getClass() 00903 + " are not supported."); 00904 } 00905 } 00906 00917 public boolean execute() throws SQLException 00918 { 00919 return super.execute(compileQuery()); // in Statement class 00920 } 00921 00931 public String toString() 00932 { 00933 synchronized (sbuf) 00934 { 00935 sbuf.setLength(0); 00936 int i; 00937 00938 for (i = 0; i < inStrings.length; ++i) 00939 { 00940 if (inStrings[i] == null) 00941 sbuf.append('?'); 00942 else 00943 sbuf.append(templateStrings[i]); 00944 sbuf.append(inStrings[i]); 00945 } 00946 sbuf.append(templateStrings[inStrings.length]); 00947 return sbuf.toString(); 00948 } 00949 } 00950 00951 // ** JDBC 2 Extensions ** 00952 00958 public void addBatch() throws SQLException 00959 { 00960 super.addBatch(compileQuery()); 00961 } 00962 00970 public java.sql.ResultSetMetaData getMetaData() throws SQLException 00971 { 00972 java.sql.ResultSet rs = getResultSet(); 00973 if (rs != null) 00974 return rs.getMetaData(); 00975 00976 // Does anyone really know what this method does? 00977 return null; 00978 } 00979 00983 public void setArray(int i, Array x) throws SQLException 00984 { 00985 throw new NotImplementedException("setArray()"); 00986 } 00987 00991 public void setBlob(int i, java.sql.Blob x) throws SQLException 00992 { 00993 if (x == null) 00994 { 00995 if (connection.isDriverProcessed()) 00996 setNull(i, Types.BLOB); 00997 else 00998 set(i, Connection.startParamTag + BLOB_TAG + "null" 00999 + Connection.endParamTag); 01000 return; 01001 } 01002 01003 if (connection.isDriverProcessed()) 01004 setBinaryStream(i, x.getBinaryStream(), (int) x.length()); 01005 else 01006 { 01007 byte[] data = new byte[(int) x.length()]; 01008 InputStream binStream = x.getBinaryStream(); 01009 try 01010 { 01011 binStream.read(data, 0, (int) x.length()); 01012 } 01013 catch (Exception ioe) 01014 { 01015 throw new SQLException("Problem with data streaming"); 01016 } 01017 try 01018 { 01019 synchronized (sbuf) 01020 { 01021 sbuf.setLength(0); 01022 sbuf.append(x); 01023 set(i, Connection.startParamTag + BLOB_TAG + sbuf.toString() 01024 + Connection.endParamTag); 01025 } 01026 } 01027 catch (OutOfMemoryError oome) 01028 { 01029 sbuf = null; 01030 System.gc(); 01031 throw new SQLException("Out of memory"); 01032 } 01033 01034 } 01035 } 01036 01041 public void setCharacterStream(int i, java.io.Reader x, int length) 01042 throws SQLException 01043 { 01044 char[] data = new char[length]; 01045 try 01046 { 01047 x.read(data, 0, length); 01048 } 01049 catch (Exception ioe) 01050 { 01051 throw new SQLException("Problem with streaming of data"); 01052 } 01053 setString(i, new String(data)); 01054 } 01055 01059 public void setClob(int i, java.sql.Clob x) throws SQLException 01060 { 01061 if (x == null) 01062 { 01063 if (connection.isDriverProcessed()) 01064 setNull(i, Types.CLOB); 01065 else 01066 set(i, Connection.startParamTag + CLOB_TAG + "null" 01067 + Connection.endParamTag); 01068 return; 01069 } 01070 if (connection.isDriverProcessed()) 01071 setString(i, x.getSubString(0, (int) x.length())); 01072 else 01073 set(i, Connection.startParamTag + CLOB_TAG 01074 + x.getSubString(0, (int) x.length()) + Connection.endParamTag); 01075 } 01076 01080 public void setNull(int i, int t, String s) throws SQLException 01081 { 01082 setNull(i, t); 01083 } 01084 01088 public void setRef(int i, Ref x) throws SQLException 01089 { 01090 throw new NotImplementedException("setRef()"); 01091 } 01092 01097 public void setDate(int i, java.sql.Date d, java.util.Calendar cal) 01098 throws SQLException 01099 { 01100 if (d == null) 01101 { 01102 if (connection.isDriverProcessed()) 01103 setNull(i, Types.DATE); 01104 else 01105 set(i, Connection.startParamTag + DATE_TAG + "null" 01106 + Connection.endParamTag); 01107 return; 01108 } 01109 else 01110 { 01111 if (cal == null) 01112 setDate(i, d); 01113 else 01114 { 01115 cal.setTime(d); 01116 setDate(i, new java.sql.Date(cal.getTime().getTime())); 01117 } 01118 } 01119 } 01120 01125 public void setTime(int i, Time t, java.util.Calendar cal) 01126 throws SQLException 01127 { 01128 if (t == null) 01129 { 01130 if (connection.isDriverProcessed()) 01131 setNull(i, Types.TIME); 01132 else 01133 set(i, Connection.startParamTag + TIME_TAG + "null" 01134 + Connection.endParamTag); 01135 return; 01136 } 01137 else 01138 { 01139 if (cal == null) 01140 setTime(i, t); 01141 else 01142 { 01143 cal.setTime(t); 01144 setTime(i, new java.sql.Time(cal.getTime().getTime())); 01145 } 01146 } 01147 } 01148 01153 public void setTimestamp(int i, Timestamp t, java.util.Calendar cal) 01154 throws SQLException 01155 { 01156 if (t == null) 01157 { 01158 if (connection.isDriverProcessed()) 01159 setNull(i, Types.TIMESTAMP); 01160 else 01161 set(i, Connection.startParamTag + TIMESTAMP_TAG + "null" 01162 + Connection.endParamTag); 01163 return; 01164 } 01165 else 01166 { 01167 if (cal == null) 01168 setTimestamp(i, t); 01169 else 01170 { 01171 cal.setTime(t); 01172 setTimestamp(i, new java.sql.Timestamp(cal.getTime().getTime())); 01173 } 01174 } 01175 } 01176 01177 //------------------------- JDBC 3.0 ----------------------------------- 01178 01189 public void setURL(int parameterIndex, java.net.URL x) throws SQLException 01190 { 01191 throw new NotImplementedException("setURL"); 01192 } 01193 01205 public ParameterMetaData getParameterMetaData() throws SQLException 01206 { 01207 throw new NotImplementedException("getParameterMetaData"); 01208 } 01209 01210 // ************************************************************** 01211 // END OF PUBLIC INTERFACE 01212 // ************************************************************** 01213 01222 private void set(int paramIndex, String s) throws SQLException 01223 { 01224 if (paramIndex < 1 || paramIndex > inStrings.length) 01225 throw new SQLException("Parameter index out of range."); 01226 inStrings[paramIndex - 1] = s; 01227 } 01228 01236 protected void setGeneratedKeysFlag(int autoGeneratedKeys) 01237 { 01238 generatedKeysFlag = autoGeneratedKeys; 01239 } 01240 01249 public static void setPreparedStatement(String sql, 01250 java.sql.PreparedStatement ps) throws SQLException 01251 { 01252 int i = 0; 01253 int currentParameter = 0; 01254 01255 // Set all parameters 01256 while ((i = sql.indexOf(Connection.startParamTag, i)) > -1) 01257 { 01258 currentParameter++; 01259 01260 int typeStart = i + Connection.startParamTag.length(); 01261 01262 // Here we assume that all tags have the same length as the boolean tag. 01263 String tag = sql.substring(typeStart, typeStart + BOOLEAN_TAG.length()); 01264 String value = sql.substring(typeStart + BOOLEAN_TAG.length(), sql 01265 .indexOf(Connection.endParamTag, i)); 01266 01267 // Test tags in alphabetical order (to make the code easier to read) 01268 if (tag.equals(BIG_DECIMAL_TAG)) 01269 { 01270 if (value.equals("null")) 01271 ps.setBigDecimal(currentParameter, null); 01272 else 01273 { 01274 BigDecimal t = new BigDecimal(value); 01275 ps.setBigDecimal(currentParameter, t); 01276 } 01277 } 01278 else if (tag.equals(BOOLEAN_TAG)) 01279 ps.setBoolean(currentParameter, Boolean.valueOf(value).booleanValue()); 01280 else if (tag.equals(BYTE_TAG)) 01281 { 01282 byte t = new Integer(value).byteValue(); 01283 ps.setByte(currentParameter, t); 01284 } 01285 else if (tag.equals(BYTES_TAG)) 01286 { 01287 byte[] t = value.getBytes(); 01288 ps.setBytes(currentParameter, t); 01289 } 01290 else if (tag.equals(BLOB_TAG)) 01291 { 01292 if (value.equals("null")) 01293 ps.setBlob(currentParameter, null); 01294 else 01295 { 01296 Blob b = new Blob(value.getBytes()); 01297 ps.setBlob(currentParameter, b); 01298 } 01299 } 01300 else if (tag.equals(CLOB_TAG)) 01301 { 01302 if (value.equals("null")) 01303 ps.setClob(currentParameter, null); 01304 else 01305 { 01306 Clob c = new Clob(value); 01307 ps.setClob(currentParameter, c); 01308 } 01309 } 01310 else if (tag.equals(DATE_TAG)) 01311 { 01312 if (value.equals("null")) 01313 ps.setDate(currentParameter, null); 01314 else 01315 try 01316 { 01317 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 01318 Date t = new Date(sdf.parse(value).getTime()); 01319 ps.setDate(currentParameter, t); 01320 } 01321 catch (ParseException p) 01322 { 01323 ps.setDate(currentParameter, null); 01324 throw new SQLException("Couldn't format date!!!"); 01325 } 01326 } 01327 else if (tag.equals(DOUBLE_TAG)) 01328 ps.setDouble(currentParameter, Double.valueOf(value).doubleValue()); 01329 else if (tag.equals(FLOAT_TAG)) 01330 ps.setFloat(currentParameter, Float.valueOf(value).floatValue()); 01331 else if (tag.equals(INTEGER_TAG)) 01332 ps.setInt(currentParameter, Integer.valueOf(value).intValue()); 01333 else if (tag.equals(LONG_TAG)) 01334 ps.setLong(currentParameter, Long.valueOf(value).longValue()); 01335 else if (tag.equals(NULL_TAG)) 01336 ps.setNull(currentParameter, Integer.valueOf(value).intValue()); 01337 else if (tag.equals(OBJECT_TAG)) 01338 { 01339 if (value.equals("null")) 01340 ps.setObject(currentParameter, null); 01341 else 01342 { 01343 try 01344 { 01345 ObjectInputStream in = new ObjectInputStream( 01346 new ByteArrayInputStream(value.getBytes())); 01347 ps.setObject(currentParameter, in.readObject()); 01348 in.close(); 01349 } 01350 catch (Exception e) 01351 { 01352 throw new SQLException("Failed to rebuild object from stream " + e); 01353 } 01354 } 01355 } 01356 else if (tag.equals(SHORT_TAG)) 01357 { 01358 short t = new Integer(value).shortValue(); 01359 ps.setShort(currentParameter, t); 01360 } 01361 else if (tag.equals(STRING_TAG)) 01362 { 01363 if (value.equals("null")) 01364 ps.setString(currentParameter, null); 01365 else 01366 ps.setString(currentParameter, value); 01367 } 01368 else if (tag.equals(TIME_TAG)) 01369 { 01370 if (value.equals("null")) 01371 ps.setTime(currentParameter, null); 01372 else 01373 try 01374 { 01375 SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); 01376 Time t = new Time(sdf.parse(value).getTime()); 01377 ps.setTime(currentParameter, t); 01378 } 01379 catch (ParseException p) 01380 { 01381 ps.setTime(currentParameter, null); 01382 throw new SQLException("Couldn't format time!!!"); 01383 } 01384 } 01385 else if (tag.equals(TIMESTAMP_TAG)) 01386 { 01387 if (value.equals("null")) 01388 ps.setTimestamp(currentParameter, null); 01389 else 01390 try 01391 { 01392 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S"); 01393 Timestamp t = new Timestamp(sdf.parse(value).getTime()); 01394 ps.setTimestamp(currentParameter, t); 01395 } 01396 catch (ParseException p) 01397 { 01398 ps.setTimestamp(currentParameter, null); 01399 throw new SQLException("Couldn't format timestamp!!!"); 01400 } 01401 } 01402 i = typeStart; 01403 } 01404 } 01405 01406 }

CJDBCversion1.0.4に対してTue Oct 12 15:16:05 2004に生成されました。 doxygen 1.3.8