1 package com.quadcap.jdbc; 2 3 40 41 import java.io.IOException ; 42 import java.io.InputStream ; 43 import java.io.Reader ; 44 45 import java.util.Calendar ; 46 import java.util.Vector ; 47 48 import java.math.BigDecimal ; 49 50 import java.sql.Array ; 52 import java.sql.Blob ; 53 import java.sql.Clob ; 54 import java.sql.Ref ; 55 57 import java.sql.Date ; 58 import java.sql.SQLException ; 59 import java.sql.SQLWarning ; 60 import java.sql.Time ; 61 import java.sql.Timestamp ; 62 import java.sql.Types ; 63 64 import antlr.RecognitionException; 65 66 import com.quadcap.sql.InsertBlob; 67 import com.quadcap.sql.ParameterExpression; 68 import com.quadcap.sql.SQLParser; 69 import com.quadcap.sql.Stmt; 70 71 import com.quadcap.io.AsciiReader; 72 import com.quadcap.io.ReaderInputStream; 73 74 import com.quadcap.sql.file.BlockFile; 75 76 import com.quadcap.sql.types.*; 77 78 import com.quadcap.util.Debug; 79 80 90 public class PreparedStatement extends Statement 91 implements java.sql.PreparedStatement 92 { 93 Stmt stmt; 94 Vector params; 95 String sql; 97 99 102 public PreparedStatement(Connection conn, String sql) 103 throws SQLException , IOException 104 { 105 super(conn); 106 this.sql = sql; 108 if (Statement.trace.bit(0)) { 109 Debug.println("prepare(" + sql + ")"); 110 } 111 session.makeTransaction(); 113 SQLParser p = new SQLParser(session, sql, escapeProcessing); 114 String auth = qConn.getAuth(); 115 try { 116 stmt = p.statement(); 117 params = p.getParameters(); 118 } catch (RecognitionException e) { 119 throw new SQLException (e.toString(), "42000"); 120 } catch (antlr.TokenStreamException e) { 121 throw new SQLException (e.toString(), "Q0010"); 122 } finally { 123 132 qConn.setAuth(auth, null); 135 } 136 } 137 138 143 public void addBatch() throws SQLException { 144 throw new SQLException ("not implemented", "0A000"); 145 } 146 147 ParameterExpression getParam(int i) { 148 return (ParameterExpression)params.elementAt(i-1); 149 } 150 151 157 public void clearParameters() throws SQLException { 158 for (int i = 1; i <= params.size(); i++) { 159 ParameterExpression pe = getParam(i); 160 pe.setValue(ValueNull.valueNull); 161 } 162 } 163 164 173 public boolean execute() throws SQLException { 174 if (Statement.trace.bit(1)) { 176 Debug.println("PreparedStatement.execute(" + sql + ")"); 177 } 178 this.rs = null; 180 this.updateCount = -1; 181 182 if (this.qConn.isClosed()) { 183 throw new SQLException ("Connection closed"); 184 } 185 try { 186 session.doStatement(stmt); 187 this.rs = (ResultSet)session.getResultSet(this); 188 if (rs != null) { 189 return true; 190 } else { 191 this.updateCount = session.getUpdateCount(); 192 return false; 193 } 194 } catch (IOException e) { 195 try { 196 session.endStatement(true); 197 } catch (IOException e1) {} 198 throw new SQLException (e.toString(), "Q0011"); 199 } catch (SQLException e) { 200 try { 201 session.endStatement(true); 202 } catch (IOException e1) {} 203 throw e; 204 } catch (Throwable e) { 205 Debug.print(e); 206 try { 207 session.endStatement(true); 208 } catch (IOException e1) {} 209 throw new SQLException (e.toString(), "Q0013"); 210 } 211 } 212 213 222 public java.sql.ResultSet executeQuery() throws SQLException { 223 if (execute()) { 224 return this.rs; 225 } else { 226 return null; 227 } 228 } 229 230 238 public int executeUpdate() throws SQLException { 239 if (!execute()) { 240 return updateCount; 241 } else { 242 return 0; 243 } 244 } 245 246 257 public java.sql.ResultSetMetaData getMetaData() { 258 return null; 259 } 260 261 273 public void setAsciiStream(int col, InputStream in, int length) 274 throws SQLException 275 { 276 try { 277 if (in == null) { 278 setParamValue(col, ValueNull.valueNull); 279 } else { 280 ValueClob clob = new ValueClob(session.getDatabase(), 281 session.makeTransaction(), 282 new AsciiReader(in), length); 283 setParamValue(col, clob); 284 session.doStep(new InsertBlob(session, clob)); 285 clob.close(); 286 } 287 288 } catch (IOException e) { 289 Debug.print(e); 290 throw new SQLException (e.toString(), "Q001H"); 291 } 292 } 293 294 final void setParamValue(int col, Value v) throws SQLException { 295 if (Statement.trace.bit(4)) { 297 Debug.println("setValue(" + col + ", " + v + ")"); 298 } 299 getParam(col).setValue(v); 301 } 302 303 311 public void setBigDecimal(int col, BigDecimal val) throws SQLException { 312 if (val == null) { 313 setParamValue(col, ValueNull.valueNull); 314 } else { 315 setParamValue(col, new ValueScaledInteger(val)); 316 } 317 } 318 319 335 public void setBinaryStream(int col, InputStream in, int length) 336 throws SQLException 337 { 338 try { 339 if (in == null) { 340 setParamValue(col, ValueNull.valueNull); 341 } else { 342 ValueBlob blob = new ValueBlob(session.getDatabase(), 343 session.makeTransaction(), 344 in, length); 345 setParamValue(col, blob); 346 session.doStep(new InsertBlob(session, blob)); 347 blob.close(); 348 } 349 350 } catch (IOException e) { 351 Debug.print(e); 352 throw new SQLException (e.toString(), "Q001H"); 353 } 354 } 355 356 364 public void setBoolean(int col, boolean val) throws SQLException { 365 setParamValue(col, new ValueBoolean(val)); 366 } 367 368 376 public void setByte(int col, byte val) throws SQLException { 377 setParamValue(col, new ValueShort(val)); 378 } 379 380 388 public void setBytes(int col, byte[] val) throws SQLException { 389 if (val == null) { 390 setParamValue(col, ValueNull.valueNull); 391 } else { 392 setParamValue(col, new ValueOctets(val)); 393 } 394 } 395 396 406 public void setCharacterStream(int col, Reader r, int length) 407 throws SQLException 408 { 409 try { 410 if (r == null) { 411 setParamValue(col, ValueNull.valueNull); 412 } else { 413 ValueClob clob = new ValueClob(session.getDatabase(), 414 session.makeTransaction(), 415 r, length); 416 setParamValue(col, clob); 417 session.doStep(new InsertBlob(session, clob)); 418 clob.close(); 419 } 420 421 } catch (IOException e) { 422 Debug.print(e); 423 throw new SQLException (e.toString(), "Q001H"); 424 } 425 } 426 427 435 public void setDate(int col, Date val) throws SQLException { 436 if (val == null) { 437 setParamValue(col, ValueNull.valueNull); 438 } else { 439 setParamValue(col, new ValueDate(val.getTime())); 440 } 441 } 442 443 454 public void setDate(int col, Date val, Calendar cal) throws SQLException { 455 if (val == null) { 456 setParamValue(col, ValueNull.valueNull); 457 } else { 458 cal.setTime(val); 459 setParamValue(col, new ValueDate(cal.getTime().getTime())); 460 } 461 } 462 463 471 public void setDouble(int col, double val) throws SQLException { 472 setParamValue(col, new ValueDouble(val)); 473 } 474 475 483 public void setFloat(int col, float val) throws SQLException { 484 setParamValue(col, new ValueDouble(val)); 485 } 486 487 495 public void setInt(int col, int val) throws SQLException { 496 setParamValue(col, new ValueInteger(val)); 497 } 498 499 507 public void setLong(int col, long val) throws SQLException { 508 setParamValue(col, new ValueLong(val)); 509 } 510 511 520 public void setNull(int col, int type) throws SQLException { 521 setParamValue(col, ValueNull.valueNull); 522 } 523 524 536 public void setNull(int col, int type, String typename) 537 throws SQLException 538 { 539 setParamValue(col, ValueNull.valueNull); 540 } 541 542 550 public void setObject(int col, Object val) throws SQLException { 551 if (val instanceof Blob ) { 552 Blob blob = (Blob )val; 553 setBinaryStream(col, blob.getBinaryStream(), (int)blob.length()); 554 } else if (val instanceof Clob ) { 555 Clob clob = (Clob )val; 556 setCharacterStream(col, clob.getCharacterStream(), 557 (int)clob.length()); 558 } else { 559 setParamValue(col, Value.fromObject(val)); 560 } 561 } 562 563 572 public void setObject(int col, Object val, int type) throws SQLException { 573 Type t = Value.typeForJdbcType(type); 574 Value v = Value.fromObject(val); 575 setParamValue(col, t.convert(v)); 576 } 577 578 588 public void setObject(int col, Object val, int type, int scale) 589 throws SQLException 590 { 591 Value v = Value.fromObject(val); 592 Type t = null; 593 switch (type) { 594 case Types.NUMERIC: 595 case Types.DECIMAL: 596 t = new TypeDecimal(32, scale); 597 break; 598 default: 599 t = Value.typeForJdbcType(type); 600 } 601 setParamValue(col, t.convert(v)); 602 } 603 604 612 public void setShort(int col, short val) throws SQLException { 613 setParamValue(col, new ValueShort(val)); 614 } 615 616 624 public void setString(int col, String val) throws SQLException { 625 if (val == null) { 626 setParamValue(col, ValueNull.valueNull); 627 } else { 628 setParamValue(col, new ValueString(val)); 629 } 630 } 631 632 640 public void setTime(int col, Time val) throws SQLException { 641 if (val == null) { 642 setParamValue(col, ValueNull.valueNull); 643 } else { 644 setParamValue(col, new ValueTime(val.getTime())); 645 } 646 } 647 648 659 public void setTime(int col, Time val, Calendar cal) throws SQLException { 660 if (val == null) { 661 setParamValue(col, ValueNull.valueNull); 662 } else { 663 long t = val.getTime(); 664 cal.setTime(val); 665 t -= (cal.get(Calendar.ZONE_OFFSET) + 666 cal.get(Calendar.DST_OFFSET)); 667 setParamValue(col, new ValueTime(t)); 668 } 669 } 670 671 679 public void setTimestamp(int col, Timestamp val) throws SQLException { 680 if (val == null) { 681 setParamValue(col, ValueNull.valueNull); 682 } else { 683 setParamValue(col, new ValueTimestamp(val)); 684 } 685 } 686 687 698 public void setTimestamp(int col, Timestamp val, Calendar cal) 699 throws SQLException 700 { 701 if (val == null) { 702 setParamValue(col, ValueNull.valueNull); 703 } else { 704 long t = val.getTime(); 705 cal.setTime(val); 706 t -= (cal.get(Calendar.ZONE_OFFSET) + 707 cal.get(Calendar.DST_OFFSET)); 708 Timestamp r = new Timestamp (t); 709 r.setNanos(val.getNanos()); 710 setParamValue(col, new ValueTimestamp(r)); 711 } 712 } 713 714 717 public void setUnicodeStream(int col, InputStream in, int length) 718 throws SQLException 719 { 720 setBinaryStream(col, in, length); 721 } 722 723 724 727 public PreparedStatement(Connection conn, String sql, 728 int resultSetType, 729 int resultSetConcurrency) 730 throws SQLException , IOException 731 { 732 this(conn, sql); 733 this.resultSetType = resultSetType; 734 this.resultSetConcurrency = resultSetConcurrency; 735 } 736 737 748 public void setArray(int col, Array val) throws SQLException { 749 throw new SQLException ("not implemented", "0A000"); 750 } 751 752 760 public void setBlob(int col, Blob val) throws SQLException { 761 if (val == null) { 762 setParamValue(col, ValueNull.valueNull); 763 } else { 764 if (val instanceof ValueBlob) { 765 setParamValue(col, (ValueBlob)val); 766 } else { 767 setBinaryStream(col, val.getBinaryStream(), 768 (int)(val.length())); 769 } 770 } 771 } 772 773 781 public void setClob(int col, Clob val) throws SQLException { 782 if (val == null) { 783 setParamValue(col, ValueNull.valueNull); 784 } else { 785 setAsciiStream(col, val.getAsciiStream(), (int)(val.length())); 786 } 787 } 788 789 799 public void setRef(int col, Ref val) throws SQLException { 800 throw new SQLException ("not implemented", "0A000"); 801 } 802 803 805 808 821 public void setURL(int parameterIndex, java.net.URL x) 822 throws SQLException 823 { 824 setString(parameterIndex, x.toString()); 825 } 826 827 828 841 public java.sql.ParameterMetaData getParameterMetaData() 842 throws SQLException 843 { 844 throw new SQLException ("Not implemented"); 845 } 846 848 public String toString() { 849 StringBuffer sb = 850 new StringBuffer ("com.quadcap.jdbc.PreparedStatement("); 851 if (params != null) for (int i = 1; i <= params.size(); i++) { 852 if (i > 1) sb.append(","); 853 ParameterExpression e = getParam(i); 854 Value v = e.getValue(null, null); 855 sb.append(String.valueOf(v)); 856 } 857 sb.append(")"); 858 return sb.toString(); 859 } 860 861 } 862 | Popular Tags |