| 1 30 31 32 package org.hsqldb.jdbc; 33 34 import java.io.IOException ; 35 import java.io.Serializable ; 36 import java.math.BigDecimal ; 37 import java.sql.Date ; 38 import java.sql.PreparedStatement ; 39 import java.sql.ResultSet ; 40 import java.sql.ResultSetMetaData ; 41 import java.sql.SQLException ; 42 import java.sql.Time ; 43 import java.sql.Timestamp ; 44 import java.util.Calendar ; 45 46 import java.sql.Array ; 48 import java.sql.Blob ; 49 import java.sql.Clob ; 50 import java.sql.Ref ; 51 52 import java.sql.ParameterMetaData ; 55 56 import org.hsqldb.Column; 58 import org.hsqldb.HsqlDateTime; 59 import org.hsqldb.HsqlException; 60 import org.hsqldb.Result; 61 import org.hsqldb.ResultConstants; 62 import org.hsqldb.Trace; 63 import org.hsqldb.Types; 64 import org.hsqldb.lib.ArrayUtil; 65 import org.hsqldb.lib.HsqlByteArrayOutputStream; 66 import org.hsqldb.lib.Iterator; 67 import org.hsqldb.lib.StringConverter; 68 import org.hsqldb.types.Binary; 69 import org.hsqldb.types.JavaObject; 70 71 87 203 public class jdbcPreparedStatement extends jdbcStatement 204 implements PreparedStatement { 205 206 207 protected Object [] parameterValues; 208 209 210 protected boolean[] parameterSet; 211 212 213 protected boolean[] parameterStream; 214 215 216 protected int[] parameterTypes; 217 218 219 protected int[] parameterModes; 220 221 222 protected int[] streamLengths; 223 224 225 protected boolean hasStreams; 226 227 230 protected Result rsmdDescriptor; 231 232 233 protected Result pmdDescriptor; 234 235 236 protected jdbcResultSetMetaData rsmd; 237 238 240 241 protected Object pmd; 242 243 244 protected String sql; 245 246 253 protected int statementID; 254 255 259 protected boolean isRowCount; 260 261 271 292 public void setEscapeProcessing(boolean enable) throws SQLException { 293 checkClosed(); 294 } 295 296 333 public boolean execute() throws SQLException { 334 335 checkClosed(); 336 connection.clearWarningsNoCheck(); 337 338 resultIn = null; 339 340 try { 341 resultOut.setMaxRows(maxRows); 342 resultOut.setParameterData(parameterValues); 343 344 resultIn = connection.sessionProxy.execute(resultOut); 345 } catch (HsqlException e) { 346 throw Util.sqlException(e); 347 } 348 349 if (resultIn.isError()) { 350 Util.throwError(resultIn); 351 } 352 353 return resultIn.isData(); 354 } 355 356 367 public ResultSet executeQuery() throws SQLException { 368 369 checkClosed(); 370 connection.clearWarningsNoCheck(); 371 checkIsRowCount(false); 372 checkParametersSet(); 373 374 resultIn = null; 375 376 try { 377 resultOut.setMaxRows(maxRows); 378 resultOut.setParameterData(parameterValues); 379 380 resultIn = connection.sessionProxy.execute(resultOut); 381 } catch (HsqlException e) { 382 throw Util.sqlException(e); 383 } 384 385 if (resultIn.isError()) { 386 Util.throwError(resultIn); 387 } else if (!resultIn.isData()) { 388 String msg = "Expected but did not recieve a result set"; 389 390 throw Util.sqlException(Trace.UNEXPECTED_EXCEPTION, msg); 391 } 392 393 return new jdbcResultSet(this, resultIn, connection.connProperties, 394 connection.isNetConn); 395 } 396 397 412 public int executeUpdate() throws SQLException { 413 414 checkClosed(); 415 connection.clearWarningsNoCheck(); 416 checkIsRowCount(true); 417 checkParametersSet(); 418 419 resultIn = null; 420 421 try { 422 resultOut.setParameterData(parameterValues); 423 424 resultIn = connection.sessionProxy.execute(resultOut); 425 } catch (HsqlException e) { 426 throw Util.sqlException(e); 427 } 428 429 if (resultIn.isError()) { 430 Util.throwError(resultIn); 431 } else if (resultIn.mode != ResultConstants.UPDATECOUNT) { 432 String msg = "Expected but did not recieve a row update count"; 433 434 throw Util.sqlException(Trace.UNEXPECTED_EXCEPTION, msg); 435 } 436 437 return resultIn.getUpdateCount(); 438 } 439 440 511 public int[] executeBatch() throws SQLException { 512 513 if (batchResultOut == null) { 514 batchResultOut = new Result(ResultConstants.BATCHEXECUTE, 515 parameterTypes, statementID); 516 } 517 518 return super.executeBatch(); 519 } 520 521 540 public void setNull(int paramIndex, int sqlType) throws SQLException { 541 setParameter(paramIndex, null); 542 } 543 544 564 public void setBoolean(int parameterIndex, 565 boolean x) throws SQLException { 566 567 Boolean b = x ? Boolean.TRUE 568 : Boolean.FALSE; 569 570 setParameter(parameterIndex, b); 571 } 572 573 584 public void setByte(int parameterIndex, byte x) throws SQLException { 585 setIntParameter(parameterIndex, x); 586 } 587 588 599 public void setShort(int parameterIndex, short x) throws SQLException { 600 setIntParameter(parameterIndex, x); 601 } 602 603 614 public void setInt(int parameterIndex, int x) throws SQLException { 615 setIntParameter(parameterIndex, x); 616 } 617 618 629 public void setLong(int parameterIndex, long x) throws SQLException { 630 setLongParameter(parameterIndex, x); 631 } 632 633 655 public void setFloat(int parameterIndex, float x) throws SQLException { 656 setDouble(parameterIndex, (double) x); 657 } 658 659 681 public void setDouble(int parameterIndex, double x) throws SQLException { 682 683 Double d = new Double (x); 684 685 setParameter(parameterIndex, d); 686 } 687 688 700 public void setBigDecimal(int parameterIndex, 701 BigDecimal x) throws SQLException { 702 setParameter(parameterIndex, x); 703 } 704 705 729 public void setString(int parameterIndex, String x) throws SQLException { 730 setParameter(parameterIndex, x); 731 } 732 733 756 public void setBytes(int paramIndex, byte[] x) throws SQLException { 757 setParameter(paramIndex, x); 758 } 759 760 771 public void setDate(int parameterIndex, Date x) throws SQLException { 772 setParameter(parameterIndex, x); 773 } 774 775 786 public void setTime(int parameterIndex, Time x) throws SQLException { 787 setParameter(parameterIndex, x); 788 } 789 790 802 public void setTimestamp(int parameterIndex, 803 Timestamp x) throws SQLException { 804 setParameter(parameterIndex, x); 805 } 806 807 839 public void setAsciiStream(int parameterIndex, java.io.InputStream x, 840 int length) throws SQLException { 841 842 checkSetParameterIndex(parameterIndex, true); 843 844 String s; 845 846 if (x == null) { 847 s = "input stream is null"; 848 849 throw Util.sqlException(Trace.INVALID_JDBC_ARGUMENT, s); 850 } 851 852 try { 853 s = StringConverter.inputStreamToString(x, length); 854 855 setParameter(parameterIndex, s); 856 } catch (IOException e) { 857 throw Util.sqlException(Trace.INVALID_CHARACTER_ENCODING); 858 } 859 } 860 861 896 897 public void setUnicodeStream(int parameterIndex, java.io.InputStream x, 899 int length) throws SQLException { 900 901 checkSetParameterIndex(parameterIndex, true); 902 903 String msg = null; 904 905 if (x == null) { 906 msg = "input stream is null"; 907 } else if (length % 2 != 0) { 908 msg = "odd length argument"; 909 } 910 911 if (msg != null) { 912 throw Util.sqlException(Trace.INVALID_JDBC_ARGUMENT, msg); 913 } 914 915 int chlen = length / 2; 916 int chread = 0; 917 StringBuffer sb = new StringBuffer (); 918 int hi; 919 int lo; 920 921 try { 922 for (; chread < chlen; chread++) { 923 hi = x.read(); 924 925 if (hi == -1) { 926 break; 927 } 928 929 lo = x.read(); 930 931 if (lo == -1) { 932 break; 933 } 934 935 sb.append((char) (hi << 8 | lo)); 936 } 937 } catch (IOException e) { 938 throw Util.sqlException(Trace.TRANSFER_CORRUPTED); 939 } 940 941 setParameter(parameterIndex, sb.toString()); 942 } 943 944 946 973 public void setBinaryStream(int parameterIndex, java.io.InputStream x, 974 int length) throws SQLException { 975 976 checkSetParameterIndex(parameterIndex, true); 977 978 if (x == null) { 979 throw Util.sqlException(Trace.error(Trace.INVALID_JDBC_ARGUMENT, 980
|