00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
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 import org.objectweb.cjdbc.common.sql.filters.HexaBlobFilter;
00049 import org.objectweb.cjdbc.common.util.Strings;
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075 public class PreparedStatement extends Statement
00076 implements
00077 java.sql.PreparedStatement
00078 {
00079
00080 public static final String BYTE_TAG = "b|";
00081
00082 public static final String BYTES_TAG = "B|";
00083
00084 public static final String BLOB_TAG = "c|";
00085
00086 public static final String CLOB_TAG = "C|";
00087
00088 public static final String BOOLEAN_TAG = "0|";
00089
00090 public static final String BIG_DECIMAL_TAG = "1|";
00091
00092 public static final String DATE_TAG = "d|";
00093
00094 public static final String DOUBLE_TAG = "D|";
00095
00096 public static final String FLOAT_TAG = "F|";
00097
00098 public static final String INTEGER_TAG = "I|";
00099
00100 public static final String LONG_TAG = "L|";
00101
00102 public static final String NULL_TAG = "N|";
00103
00104 public static final String OBJECT_TAG = "O|";
00105
00106 public static final String SHORT_TAG = "s|";
00107
00108 public static final String STRING_TAG = "S|";
00109
00110 public static final String TIME_TAG = "t|";
00111
00112 public static final String TIMESTAMP_TAG = "T|";
00113
00114 protected String sql;
00115 private String[] templateStrings;
00116 private String[] inStrings;
00117
00118
00119 private StringBuffer sbuf = new StringBuffer();
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130 public PreparedStatement(Connection connection, String sqlStatement)
00131 throws SQLException
00132 {
00133 super(connection);
00134
00135 ArrayList v = new ArrayList();
00136 int lastParmEnd = 0;
00137
00138
00139
00140
00141
00142
00143
00144 boolean inString = false;
00145 boolean inMetaString = false;
00146
00147 this.sql = sqlStatement.trim();
00148 this.connection = connection;
00149 for (int i = 0; i < sql.length(); ++i)
00150 {
00151 if (sql.charAt(i) == '\'')
00152 inString = !inString;
00153 if (sql.charAt(i) == '"')
00154 inMetaString = !inMetaString;
00155 if ((sql.charAt(i) == '?') && (!(inString || inMetaString)))
00156 {
00157 v.add(sql.substring(lastParmEnd, i));
00158 lastParmEnd = i + 1;
00159 }
00160 }
00161 v.add(sql.substring(lastParmEnd, sql.length()));
00162
00163 int size = v.size();
00164 templateStrings = new String[size];
00165 inStrings = new String[size - 1];
00166 clearParameters();
00167
00168 for (int i = 0; i < size; ++i)
00169 templateStrings[i] = (String) v.get(i);
00170 }
00171
00172
00173
00174
00175
00176
00177 public void close() throws SQLException
00178 {
00179 sql = null;
00180 templateStrings = null;
00181 inStrings = null;
00182
00183 super.close();
00184 }
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194 public java.sql.ResultSet executeQuery() throws SQLException
00195 {
00196 return super.executeQuery(sql, compileQuery());
00197 }
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208 public int executeUpdate() throws SQLException
00209 {
00210 return super.executeUpdateWithSkeleton(sql, compileQuery());
00211
00212 }
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222 protected synchronized String compileQuery() throws SQLException
00223 {
00224 sbuf.setLength(0);
00225 int i;
00226
00227 for (i = 0; i < inStrings.length; ++i)
00228 {
00229 if (inStrings[i] == null)
00230 throw new SQLException("Parameter " + (i + 1) + " is incorrect");
00231 sbuf.append(templateStrings[i]).append(inStrings[i]);
00232 }
00233 sbuf.append(templateStrings[inStrings.length]);
00234 return sbuf.toString();
00235 }
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247 protected String doEscapeProcessing(String x)
00248 {
00249
00250
00251 synchronized (sbuf)
00252 {
00253 sbuf.setLength(0);
00254 int i;
00255 sbuf.append(connection.getEscapeChar());
00256 for (i = 0; i < x.length(); ++i)
00257 {
00258 char c = x.charAt(i);
00259 if ((c == '\'' && connection.isEscapeSingleQuote())
00260 || (c == '\\' && connection.isEscapeBackslash()))
00261 sbuf.append(c);
00262 sbuf.append(c);
00263 }
00264 sbuf.append(connection.getEscapeChar());
00265 }
00266 return sbuf.toString();
00267 }
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278 public void setNull(int parameterIndex, int sqlType) throws SQLException
00279 {
00280 if (connection.isDriverProcessed())
00281 set(parameterIndex, "null");
00282 else
00283 setWithTag(parameterIndex, NULL_TAG, String.valueOf(sqlType));
00284 }
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294 public void setBoolean(int parameterIndex, boolean x) throws SQLException
00295 {
00296 if (connection.isDriverProcessed())
00297 {
00298 set(parameterIndex, x
00299 ? connection.getPreparedStatementBooleanTrue()
00300 : connection.getPreparedStatementBooleanFalse());
00301 }
00302 else
00303 {
00304 setWithTag(parameterIndex, BOOLEAN_TAG, String.valueOf(x));
00305 }
00306 }
00307
00308
00309
00310
00311
00312
00313
00314
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 setWithTag(parameterIndex, BYTE_TAG, Integer.toString(x));
00324 }
00325 }
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335 public void setShort(int parameterIndex, short x) throws SQLException
00336 {
00337 if (connection.isDriverProcessed())
00338 {
00339 set(parameterIndex, Integer.toString(x));
00340 }
00341 else
00342 {
00343 setWithTag(parameterIndex, SHORT_TAG, Integer.toString(x));
00344 }
00345 }
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355 public void setInt(int parameterIndex, int x) throws SQLException
00356 {
00357 if (connection.isDriverProcessed())
00358 {
00359 set(parameterIndex, Integer.toString(x));
00360 }
00361 else
00362 {
00363 setWithTag(parameterIndex, INTEGER_TAG, Integer.toString(x));
00364 }
00365 }
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375 public void setLong(int parameterIndex, long x) throws SQLException
00376 {
00377 if (connection.isDriverProcessed())
00378 {
00379 set(parameterIndex, Long.toString(x));
00380 }
00381 else
00382 {
00383 setWithTag(parameterIndex, LONG_TAG, Long.toString(x));
00384 }
00385 }
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395 public void setFloat(int parameterIndex, float x) throws SQLException
00396 {
00397 if (connection.isDriverProcessed())
00398 {
00399 set(parameterIndex, Float.toString(x));
00400 }
00401 else
00402 {
00403 setWithTag(parameterIndex, FLOAT_TAG, Float.toString(x));
00404 }
00405 }
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415 public void setDouble(int parameterIndex, double x) throws SQLException
00416 {
00417 if (connection.isDriverProcessed())
00418 {
00419 set(parameterIndex, Double.toString(x));
00420 }
00421 else
00422 {
00423 setWithTag(parameterIndex, DOUBLE_TAG, Double.toString(x));
00424 }
00425 }
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435 public void setBigDecimal(int parameterIndex, BigDecimal x)
00436 throws SQLException
00437 {
00438 if (connection.isDriverProcessed())
00439 {
00440 if (x == null)
00441 setNull(parameterIndex, Types.DECIMAL);
00442 else
00443 set(parameterIndex, x.toString());
00444 }
00445 else
00446 {
00447 if (x == null)
00448 setWithTag(parameterIndex, BIG_DECIMAL_TAG, NULL_TAG);
00449 else
00450 setWithTag(parameterIndex, BIG_DECIMAL_TAG, x.toString());
00451 }
00452 }
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463 public void setString(int parameterIndex, String x) throws SQLException
00464 {
00465 if (connection.isDriverProcessed())
00466 {
00467 if (x == null)
00468
00469 setNull(parameterIndex, Types.VARCHAR);
00470 else
00471 {
00472 if (escapeProcessing
00473 && (connection.isEscapeBackslash() || connection
00474 .isEscapeSingleQuote()))
00475 set(parameterIndex, doEscapeProcessing(x));
00476 else
00477
00478 set(parameterIndex, x);
00479 }
00480 }
00481 else
00482 {
00483 if (x == null)
00484 setWithTag(parameterIndex, STRING_TAG, NULL_TAG);
00485 else
00486
00487
00488 setWithTag(parameterIndex, STRING_TAG, x);
00489 }
00490 }
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504 public void setBytes(int parameterIndex, byte x[]) throws SQLException
00505 {
00506 String blob;
00507 try
00508 {
00509 synchronized (sbuf)
00510 {
00511 if (connection.isDriverProcessed())
00512 {
00513 blob = connection.getBlobFilter().encode(x);
00514 sbuf.setLength(0);
00515 sbuf.append(connection.escapeChar);
00516 sbuf.append(blob);
00517 sbuf.append(connection.escapeChar);
00518 set(parameterIndex, sbuf.toString());
00519 }
00520 else
00521 {
00522 blob = new HexaBlobFilter().encode(x);
00523 setWithTag(parameterIndex, BYTES_TAG, blob);
00524 }
00525 }
00526 }
00527 catch (OutOfMemoryError oome)
00528 {
00529 blob = null;
00530 sbuf = null;
00531 System.gc();
00532 throw new SQLException("Out of memory");
00533 }
00534 }
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544 public void setDate(int parameterIndex, java.sql.Date x) throws SQLException
00545 {
00546 if (connection.isDriverProcessed())
00547 {
00548 if (x == null)
00549 setNull(parameterIndex, Types.DATE);
00550 else
00551 set(parameterIndex, "'" + new java.sql.Date(x.getTime()).toString()
00552 + "'");
00553 }
00554 else
00555 {
00556 if (x == null)
00557 setWithTag(parameterIndex, DATE_TAG, NULL_TAG);
00558 else
00559 setWithTag(parameterIndex, DATE_TAG, new java.sql.Date(x.getTime())
00560 .toString());
00561 }
00562 }
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572 public void setTime(int parameterIndex, Time x) throws SQLException
00573 {
00574 if (connection.isDriverProcessed())
00575 {
00576 if (x == null)
00577 setNull(parameterIndex, Types.TIME);
00578 else
00579 set(parameterIndex, "{t '" + x.toString() + "'}");
00580 }
00581 else
00582 {
00583 if (x == null)
00584 setWithTag(parameterIndex, TIME_TAG, NULL_TAG);
00585 else
00586 setWithTag(parameterIndex, TIME_TAG, x.toString());
00587 }
00588 }
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598 public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException
00599 {
00600 if (connection.isDriverProcessed())
00601 {
00602 if (x == null)
00603 setNull(parameterIndex, Types.TIMESTAMP);
00604 else
00605 {
00606
00607
00608 if (x.getClass().equals(Timestamp.class))
00609 set(parameterIndex, "'" + x.toString() + "'");
00610 else
00611 set(parameterIndex, "'" + new Timestamp(x.getTime()).toString() + "'");
00612 }
00613 }
00614 else
00615 {
00616 if (x == null)
00617 setWithTag(parameterIndex, TIMESTAMP_TAG, NULL_TAG);
00618 else
00619 {
00620 if (x.getClass().equals(Timestamp.class))
00621 setWithTag(parameterIndex, TIMESTAMP_TAG, x.toString());
00622 else
00623 setWithTag(parameterIndex, TIMESTAMP_TAG, new Timestamp(x.getTime())
00624 .toString());
00625 }
00626 }
00627 }
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644 public void setAsciiStream(int parameterIndex, InputStream x, int length)
00645 throws SQLException
00646 {
00647 setBinaryStream(parameterIndex, x, length);
00648 }
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667 public void setUnicodeStream(int parameterIndex, InputStream x, int length)
00668 throws SQLException
00669 {
00670 setBinaryStream(parameterIndex, x, length);
00671 }
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686 public void setBinaryStream(int parameterIndex, InputStream x, int length)
00687 throws SQLException
00688 {
00689 byte[] data = new byte[length];
00690 try
00691 {
00692 x.read(data, 0, length);
00693 }
00694 catch (Exception ioe)
00695 {
00696 throw new SQLException("Problem with streaming of data");
00697 }
00698 setBytes(parameterIndex, data);
00699 }
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710 public void clearParameters() throws SQLException
00711 {
00712 int i;
00713
00714 for (i = 0; i < inStrings.length; i++)
00715 inStrings[i] = null;
00716 }
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738 public void setObject(int parameterIndex, Object x, int targetSqlType,
00739 int scale) throws SQLException
00740 {
00741 if (x == null)
00742 {
00743 setNull(parameterIndex, targetSqlType);
00744 }
00745 else
00746 {
00747 switch (targetSqlType)
00748 {
00749 case Types.TINYINT :
00750 case Types.SMALLINT :
00751 case Types.INTEGER :
00752 setInt(parameterIndex, ((Number) x).intValue());
00753 break;
00754 case Types.BIGINT :
00755 setLong(parameterIndex, ((Number) x).longValue());
00756 break;
00757 case Types.REAL :
00758 case Types.FLOAT :
00759 case Types.DOUBLE :
00760 case Types.DECIMAL :
00761 case Types.NUMERIC :
00762
00763 if (connection.isDriverProcessed())
00764 set(parameterIndex, x.toString());
00765 else
00766 setWithTag(parameterIndex, STRING_TAG, x.toString());
00767 break;
00768 case Types.BIT :
00769 case Types.BOOLEAN :
00770 setBoolean(parameterIndex, ((Boolean) x).booleanValue());
00771 break;
00772 case Types.CHAR :
00773 case Types.VARCHAR :
00774 case Types.LONGVARCHAR :
00775 setString(parameterIndex, (String) x);
00776 break;
00777 case Types.BINARY :
00778 case Types.VARBINARY :
00779 case Types.LONGVARBINARY :
00780 setBytes(parameterIndex, (byte[]) x);
00781 break;
00782 case Types.DATE :
00783 setDate(parameterIndex, (java.sql.Date) x);
00784 break;
00785 case Types.TIME :
00786 setTime(parameterIndex, (Time) x);
00787 break;
00788 case Types.TIMESTAMP :
00789 setTimestamp(parameterIndex, (Timestamp) x);
00790 break;
00791 case Types.BLOB :
00792 setBlob(parameterIndex, (Blob) x);
00793 break;
00794 case Types.DATALINK :
00795 setURL(parameterIndex, (java.net.URL) x);
00796 break;
00797 case Types.JAVA_OBJECT :
00798 case Types.OTHER :
00799 setObject(parameterIndex, x);
00800 break;
00801 default :
00802 throw new SQLException("Unsupported type value");
00803 }
00804 }
00805 }
00806
00807
00808
00809
00810 public void setObject(int parameterIndex, Object x, int targetSqlType)
00811 throws SQLException
00812 {
00813 setObject(parameterIndex, x, targetSqlType, 0);
00814 }
00815
00816
00817
00818
00819
00820
00821
00822
00823 public void setObject(int parameterIndex, Object x) throws SQLException
00824 {
00825 if (x == null)
00826 {
00827 if (connection.isDriverProcessed())
00828 setNull(parameterIndex, Types.JAVA_OBJECT);
00829 else
00830 setWithTag(parameterIndex, OBJECT_TAG, NULL_TAG);
00831 }
00832 else
00833 {
00834 if (x instanceof String)
00835 setString(parameterIndex, (String) x);
00836 else if (x instanceof BigDecimal)
00837 setBigDecimal(parameterIndex, (BigDecimal) x);
00838 else if (x instanceof Short)
00839 setShort(parameterIndex, ((Short) x).shortValue());
00840 else if (x instanceof Integer)
00841 setInt(parameterIndex, ((Integer) x).intValue());
00842 else if (x instanceof Long)
00843 setLong(parameterIndex, ((Long) x).longValue());
00844 else if (x instanceof Float)
00845 setFloat(parameterIndex, ((Float) x).floatValue());
00846 else if (x instanceof Double)
00847 setDouble(parameterIndex, ((Double) x).doubleValue());
00848 else if (x instanceof byte[])
00849 setBytes(parameterIndex, (byte[]) x);
00850 else if (x instanceof java.sql.Date)
00851 setDate(parameterIndex, (java.sql.Date) x);
00852 else if (x instanceof Time)
00853 setTime(parameterIndex, (Time) x);
00854 else if (x instanceof Timestamp)
00855 setTimestamp(parameterIndex, (Timestamp) x);
00856 else if (x instanceof Boolean)
00857 setBoolean(parameterIndex, ((Boolean) x).booleanValue());
00858 else if (x instanceof Blob)
00859 setBlob(parameterIndex, (Blob) x);
00860 else if (x instanceof java.net.URL)
00861 setURL(parameterIndex, (java.net.URL) x);
00862 else if (x instanceof Serializable)
00863 {
00864 ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
00865 try
00866 {
00867
00868 ObjectOutputStream objectOutputStream = new ObjectOutputStream(
00869 byteOutputStream);
00870 objectOutputStream.writeObject(x);
00871 objectOutputStream.flush();
00872 objectOutputStream.close();
00873 if (connection.isDriverProcessed())
00874 setBytes(parameterIndex, byteOutputStream.toByteArray());
00875 else
00876 synchronized (sbuf)
00877 {
00878 sbuf.setLength(0);
00879 sbuf.append(byteOutputStream);
00880 setWithTag(parameterIndex, OBJECT_TAG, sbuf.toString());
00881 }
00882 }
00883 catch (IOException e)
00884 {
00885 throw new SQLException("Failed to serialize object: " + e);
00886 }
00887 }
00888 else
00889 throw new SQLException("Objects of type " + x.getClass()
00890 + " are not supported.");
00891 }
00892 }
00893
00894
00895
00896
00897
00898
00899
00900
00901
00902
00903
00904 public boolean execute() throws SQLException
00905 {
00906 int start = 0;
00907 try
00908 {
00909
00910 while (sql.charAt(start) == '(')
00911 start++;
00912 }
00913 catch (IndexOutOfBoundsException e)
00914 {
00915
00916
00917 start = 0;
00918 }
00919
00920 if (sql.regionMatches(true, start, "select", 0, 6)
00921 || (sql.regionMatches(true, start, "{call", 0, 5)))
00922 {
00923 result = executeQuery(sql, compileQuery());
00924 return true;
00925 }
00926 else
00927 {
00928 updateCount = executeUpdateWithSkeleton(sql, compileQuery());
00929 return false;
00930 }
00931 }
00932
00933
00934
00935
00936
00937
00938
00939
00940
00941
00942 public String toString()
00943 {
00944 synchronized (sbuf)
00945 {
00946 sbuf.setLength(0);
00947 int i;
00948
00949 for (i = 0; i < inStrings.length; ++i)
00950 {
00951 if (inStrings[i] == null)
00952 sbuf.append('?');
00953 else
00954 sbuf.append(templateStrings[i]);
00955 sbuf.append(inStrings[i]);
00956 }
00957 sbuf.append(templateStrings[inStrings.length]);
00958 return sbuf.toString();
00959 }
00960 }
00961
00962
00963
00964
00965
00966
00967
00968
00969 public void addBatch() throws SQLException
00970 {
00971 super.addBatch(compileQuery());
00972 }
00973
00974
00975
00976
00977
00978
00979
00980
00981 public java.sql.ResultSetMetaData getMetaData() throws SQLException
00982 {
00983 java.sql.ResultSet rs = getResultSet();
00984 if (rs != null)
00985 return rs.getMetaData();
00986
00987
00988 return null;
00989 }
00990
00991
00992
00993
00994 public void setArray(int i, Array x) throws SQLException
00995 {
00996 throw new NotImplementedException("setArray()");
00997 }
00998
00999
01000
01001
01002 public void setBlob(int i, java.sql.Blob x) throws SQLException
01003 {
01004 if (x == null)
01005 {
01006 if (connection.isDriverProcessed())
01007 setNull(i, Types.BLOB);
01008 else
01009 setWithTag(i, BLOB_TAG, NULL_TAG);
01010 return;
01011 }
01012
01013 if (connection.isDriverProcessed())
01014 setBinaryStream(i, x.getBinaryStream(), (int) x.length());
01015 else
01016 {
01017 byte[] data = new byte[(int) x.length()];
01018 InputStream binStream = x.getBinaryStream();
01019 try
01020 {
01021 binStream.read(data, 0, (int) x.length());
01022 }
01023 catch (Exception ioe)
01024 {
01025 throw new SQLException("Problem with data streaming");
01026 }
01027 try
01028 {
01029 synchronized (sbuf)
01030 {
01031 sbuf.setLength(0);
01032 sbuf.append(new HexaBlobFilter().encode(data));
01033 setWithTag(i, BLOB_TAG, sbuf.toString());
01034 }
01035 }
01036 catch (OutOfMemoryError oome)
01037 {
01038 sbuf = null;
01039 System.gc();
01040 throw new SQLException("Out of memory");
01041 }
01042
01043 }
01044 }
01045
01046
01047
01048
01049
01050 public void setCharacterStream(int i, java.io.Reader x, int length)
01051 throws SQLException
01052 {
01053 char[] data = new char[length];
01054 try
01055 {
01056 x.read(data, 0, length);
01057 }
01058 catch (Exception ioe)
01059 {
01060 throw new SQLException("Problem with streaming of data");
01061 }
01062 setString(i, new String(data));
01063 }
01064
01065
01066
01067
01068 public void setClob(int i, java.sql.Clob x) throws SQLException
01069 {
01070 if (x == null)
01071 {
01072 if (connection.isDriverProcessed())
01073 setNull(i, Types.CLOB);
01074 else
01075 setWithTag(i, CLOB_TAG, NULL_TAG);
01076 return;
01077 }
01078 if (connection.isDriverProcessed())
01079 setString(i, x.getSubString(0, (int) x.length()));
01080 else
01081 setWithTag(i, CLOB_TAG, x.getSubString(0, (int) x.length()));
01082 }
01083
01084
01085
01086
01087 public void setNull(int i, int t, String s) throws SQLException
01088 {
01089 setNull(i, t);
01090 }
01091
01092
01093
01094
01095 public void setRef(int i, Ref x) throws SQLException
01096 {
01097 throw new NotImplementedException("setRef()");
01098 }
01099
01100
01101
01102
01103
01104 public void setDate(int i, java.sql.Date d, java.util.Calendar cal)
01105 throws SQLException
01106 {
01107 if (d == null)
01108 {
01109 if (connection.isDriverProcessed())
01110 setNull(i, Types.DATE);
01111 else
01112 setWithTag(i, DATE_TAG, NULL_TAG);
01113 return;
01114 }
01115 else
01116 {
01117 if (cal == null)
01118 setDate(i, d);
01119 else
01120 {
01121 cal.setTime(d);
01122 setDate(i, new java.sql.Date(cal.getTime().getTime()));
01123 }
01124 }
01125 }
01126
01127
01128
01129
01130
01131 public void setTime(int i, Time t, java.util.Calendar cal)
01132 throws SQLException
01133 {
01134 if (t == null)
01135 {
01136 if (connection.isDriverProcessed())
01137 setNull(i, Types.TIME);
01138 else
01139 setWithTag(i, TIME_TAG, NULL_TAG);
01140 return;
01141 }
01142 else
01143 {
01144 if (cal == null)
01145 setTime(i, t);
01146 else
01147 {
01148 cal.setTime(t);
01149 setTime(i, new java.sql.Time(cal.getTime().getTime()));
01150 }
01151 }
01152 }
01153
01154
01155
01156
01157
01158 public void setTimestamp(int i, Timestamp t, java.util.Calendar cal)
01159 throws SQLException
01160 {
01161 if (t == null)
01162 {
01163 if (connection.isDriverProcessed())
01164 setNull(i, Types.TIMESTAMP);
01165 else
01166 setWithTag(i, TIMESTAMP_TAG, NULL_TAG);
01167 return;
01168 }
01169 else
01170 {
01171 if (cal == null)
01172 setTimestamp(i, t);
01173 else
01174 {
01175 cal.setTime(t);
01176 setTimestamp(i, new java.sql.Timestamp(cal.getTime().getTime()));
01177 }
01178 }
01179 }
01180
01181
01182
01183
01184
01185
01186
01187
01188
01189
01190
01191
01192
01193 public void setURL(int parameterIndex, java.net.URL x) throws SQLException
01194 {
01195 throw new NotImplementedException("setURL");
01196 }
01197
01198
01199
01200
01201
01202
01203
01204
01205
01206
01207
01208
01209 public ParameterMetaData getParameterMetaData() throws SQLException
01210 {
01211 throw new NotImplementedException("getParameterMetaData");
01212 }
01213
01214
01215
01216
01217
01218
01219
01220
01221
01222
01223
01224
01225
01226 private void set(int paramIndex, String s) throws SQLException
01227 {
01228 if (paramIndex < 1 || paramIndex > inStrings.length)
01229 throw new SQLException("Parameter index out of range.");
01230 inStrings[paramIndex - 1] = s;
01231 }
01232
01233
01234
01235
01236
01237
01238
01239
01240
01241 private void setWithTag(int paramIndex, String tag, String s)
01242 throws SQLException
01243 {
01244
01245 if (!connection.isDriverProcessed() && NULL_TAG.equals(s))
01246 {
01247
01248
01249
01250 throw new SQLException("driverProcessed=false does not support value "
01251 + NULL_TAG);
01252 }
01253
01254 set(paramIndex, Connection.START_PARAM_TAG
01255 + tag
01256 + Strings.replace(s, Connection.TAG_MARKER,
01257 Connection.TAG_MARKER_ESCAPE) + Connection.END_PARAM_TAG);
01258 }
01259
01260
01261
01262
01263
01264
01265
01266
01267 protected void setGeneratedKeysFlag(int autoGeneratedKeys)
01268 {
01269 generatedKeysFlag = autoGeneratedKeys;
01270 }
01271
01272
01273
01274
01275
01276
01277
01278
01279
01280 public static void setPreparedStatement(String sql,
01281 java.sql.PreparedStatement ps) throws SQLException
01282 {
01283 int i = 0;
01284 int currentParameter = 0;
01285
01286
01287 while ((i = sql.indexOf(Connection.START_PARAM_TAG, i)) > -1)
01288 {
01289 currentParameter++;
01290
01291 int typeStart = i + Connection.START_PARAM_TAG.length();
01292
01293
01294 String tag = sql.substring(typeStart, typeStart + BOOLEAN_TAG.length());
01295 String value = sql.substring(typeStart + BOOLEAN_TAG.length(), sql
01296 .indexOf(Connection.END_PARAM_TAG, i));
01297 value = Strings.replace(value, Connection.TAG_MARKER_ESCAPE,
01298 Connection.TAG_MARKER);
01299
01300
01301 if (tag.equals(BIG_DECIMAL_TAG))
01302 {
01303 if (value.equals(NULL_TAG))
01304 ps.setBigDecimal(currentParameter, null);
01305 else
01306 {
01307 BigDecimal t = new BigDecimal(value);
01308 ps.setBigDecimal(currentParameter, t);
01309 }
01310 }
01311 else if (tag.equals(BOOLEAN_TAG))
01312 ps.setBoolean(currentParameter, Boolean.valueOf(value).booleanValue());
01313 else if (tag.equals(BYTE_TAG))
01314 {
01315 byte t = new Integer(value).byteValue();
01316 ps.setByte(currentParameter, t);
01317 }
01318 else if (tag.equals(BYTES_TAG))
01319 {
01320 byte[] t = new HexaBlobFilter().decode(value);
01321 ps.setBytes(currentParameter, t);
01322 }
01323 else if (tag.equals(BLOB_TAG))
01324 {
01325 if (value.equals(NULL_TAG))
01326 ps.setBlob(currentParameter, null);
01327 else
01328 {
01329 Blob b = new Blob(new HexaBlobFilter().decode(value));
01330 ps.setBlob(currentParameter, b);
01331 }
01332 }
01333 else if (tag.equals(CLOB_TAG))
01334 {
01335 if (value.equals(NULL_TAG))
01336 ps.setClob(currentParameter, null);
01337 else
01338 {
01339 Clob c = new Clob(value);
01340 ps.setClob(currentParameter, c);
01341 }
01342 }
01343 else if (tag.equals(DATE_TAG))
01344 {
01345 if (value.equals(NULL_TAG))
01346 ps.setDate(currentParameter, null);
01347 else
01348 try
01349 {
01350 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
01351 Date t = new Date(sdf.parse(value).getTime());
01352 ps.setDate(currentParameter, t);
01353 }
01354 catch (ParseException p)
01355 {
01356 ps.setDate(currentParameter, null);
01357 throw new SQLException("Couldn't format date!!!");
01358 }
01359 }
01360 else if (tag.equals(DOUBLE_TAG))
01361 ps.setDouble(currentParameter, Double.valueOf(value).doubleValue());
01362 else if (tag.equals(FLOAT_TAG))
01363 ps.setFloat(currentParameter, Float.valueOf(value).floatValue());
01364 else if (tag.equals(INTEGER_TAG))
01365 ps.setInt(currentParameter, Integer.valueOf(value).intValue());
01366 else if (tag.equals(LONG_TAG))
01367 ps.setLong(currentParameter, Long.valueOf(value).longValue());
01368 else if (tag.equals(NULL_TAG))
01369 ps.setNull(currentParameter, Integer.valueOf(value).intValue());
01370 else if (tag.equals(OBJECT_TAG))
01371 {
01372 if (value.equals(NULL_TAG))
01373 ps.setObject(currentParameter, null);
01374 else
01375 {
01376 try
01377 {
01378 ObjectInputStream in = new ObjectInputStream(
01379 new ByteArrayInputStream(value.getBytes()));
01380 ps.setObject(currentParameter, in.readObject());
01381 in.close();
01382 }
01383 catch (Exception e)
01384 {
01385 throw new SQLException("Failed to rebuild object from stream " + e);
01386 }
01387 }
01388 }
01389 else if (tag.equals(SHORT_TAG))
01390 {
01391 short t = new Integer(value).shortValue();
01392 ps.setShort(currentParameter, t);
01393 }
01394 else if (tag.equals(STRING_TAG))
01395 {
01396 if (value.equals(NULL_TAG))
01397 ps.setString(currentParameter, null);
01398 else
01399 ps.setString(currentParameter, value);
01400 }
01401 else if (tag.equals(TIME_TAG))
01402 {
01403 if (value.equals(NULL_TAG))
01404 ps.setTime(currentParameter, null);
01405 else
01406 try
01407 {
01408 SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
01409 Time t = new Time(sdf.parse(value).getTime());
01410 ps.setTime(currentParameter, t);
01411 }
01412 catch (ParseException p)
01413 {
01414 ps.setTime(currentParameter, null);
01415 throw new SQLException("Couldn't format time!!!");
01416 }
01417 }
01418 else if (tag.equals(TIMESTAMP_TAG))
01419 {
01420 if (value.equals(NULL_TAG))
01421 ps.setTimestamp(currentParameter, null);
01422 else
01423 try
01424 {
01425 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
01426 Timestamp t = new Timestamp(sdf.parse(value).getTime());
01427 ps.setTimestamp(currentParameter, t);
01428 }
01429 catch (ParseException p)
01430 {
01431 ps.setTimestamp(currentParameter, null);
01432 throw new SQLException("Couldn't format timestamp!!!");
01433 }
01434 }
01435 else
01436 {
01437
01438
01439 currentParameter--;
01440 }
01441 i = typeStart;
01442 }
01443 }
01444
01445 }