| 1 21 package net.sf.hajdbc.sql; 22 23 import java.io.File ; 24 import java.io.InputStream ; 25 import java.io.Reader ; 26 import java.math.BigDecimal ; 27 import java.net.URL ; 28 import java.sql.Array ; 29 import java.sql.Blob ; 30 import java.sql.Clob ; 31 import java.sql.Date ; 32 import java.sql.ParameterMetaData ; 33 import java.sql.Ref ; 34 import java.sql.ResultSetMetaData ; 35 import java.sql.SQLException ; 36 import java.sql.Time ; 37 import java.sql.Timestamp ; 38 import java.util.Calendar ; 39 40 import net.sf.hajdbc.Database; 41 import net.sf.hajdbc.Operation; 42 43 49 public class PreparedStatement<T extends java.sql.PreparedStatement > extends Statement<T> implements java.sql.PreparedStatement  50 { 51 private String sql; 52 53 60 public PreparedStatement(Connection<?> connection, Operation<java.sql.Connection , T> operation, String sql) throws java.sql.SQLException  61 { 62 super(connection, operation); 63 64 this.sql = sql; 65 } 66 67 70 public void addBatch() throws SQLException  71 { 72 Operation<T, Void > operation = new Operation<T, Void >() 73 { 74 public Void execute(Database database, T statement) throws SQLException  75 { 76 statement.addBatch(); 77 78 return null; 79 } 80 }; 81 82 this.executeWriteToDriver(operation); 83 } 84 85 88 public void clearParameters() throws SQLException  89 { 90 Operation<T, Void > operation = new Operation<T, Void >() 91 { 92 public Void execute(Database database, T statement) throws SQLException  93 { 94 statement.clearParameters(); 95 96 return null; 97 } 98 }; 99 100 this.executeWriteToDriver(operation); 101 } 102 103 106 public boolean execute() throws SQLException  107 { 108 Operation<T, Boolean > operation = new Operation<T, Boolean >() 109 { 110 public Boolean execute(Database database, T statement) throws SQLException  111 { 112 return statement.execute(); 113 } 114 }; 115 116 return this.firstValue(this.executeTransactionalWriteToDatabase(operation)); 117 } 118 119 122 public java.sql.ResultSet executeQuery() throws SQLException  123 { 124 Operation<T, java.sql.ResultSet > operation = new Operation<T, java.sql.ResultSet >() 125 { 126 public java.sql.ResultSet execute(Database database, T statement) throws SQLException  127 { 128 return statement.executeQuery(); 129 } 130 }; 131 132 return ((this.getResultSetConcurrency() == java.sql.ResultSet.CONCUR_READ_ONLY) && !this.isSelectForUpdate(this.sql)) ? this.executeReadFromDatabase(operation) : new ResultSet<T>(this, operation); 133 } 134 135 138 public int executeUpdate() throws SQLException  139 { 140 Operation<T, Integer > operation = new Operation<T, Integer >() 141 { 142 public Integer execute(Database database, T statement) throws SQLException  143 { 144 return statement.executeUpdate(); 145 } 146 }; 147 148 return this.firstValue(this.executeTransactionalWriteToDatabase(operation)); 149 } 150 151 154 public ResultSetMetaData getMetaData() throws SQLException  155 { 156 Operation<T, ResultSetMetaData > operation = new Operation<T, ResultSetMetaData >() 157 { 158 public ResultSetMetaData execute(Database database, T statement) throws SQLException  159 { 160 return statement.getMetaData(); 161 } 162 }; 163 164 return this.executeReadFromDatabase(operation); 165 } 166 167 170 public ParameterMetaData getParameterMetaData() throws SQLException  171 { 172 Operation<T, ParameterMetaData > operation = new Operation<T, ParameterMetaData >() 173 { 174 public ParameterMetaData execute(Database database, T statement) throws SQLException  175 { 176 return statement.getParameterMetaData(); 177 } 178 }; 179 180 return this.executeReadFromDatabase(operation); 181 } 182 183 186 public void setArray(final int index, final Array value) throws SQLException  187 { 188 Operation<T, Void > operation = new Operation<T, Void >() 189 { 190 public Void execute(Database database, T statement) throws SQLException  191 { 192 statement.setArray(index, value); 193 194 return null; 195 } 196 }; 197 198 this.executeWriteToDriver(operation); 199 } 200 201 204 public void setAsciiStream(final int index, InputStream inputStream, final int length) throws SQLException  205 { 206 final FileSupport fileSupport = this.getFileSupport(); 207 final File file = fileSupport.createFile(inputStream); 208 209 Operation<T, Void > operation = new Operation<T, Void >() 210 { 211 public Void execute(Database database, T statement) throws SQLException  212 { 213 statement.setAsciiStream(index, fileSupport.getInputStream(file), length); 214 215 return null; 216 } 217 }; 218 219 this.executeWriteToDriver(operation); 220 } 221 222 225 public void setBigDecimal(final int index, final BigDecimal value) throws SQLException  226 { 227 Operation<T, Void > operation = new Operation<T, Void >() 228 { 229 public Void execute(Database database, T statement) throws SQLException  230 { 231 statement.setBigDecimal(index, value); 232 233 return null; 234 } 235 }; 236 237 this.executeWriteToDriver(operation); 238 } 239 240 243 public void setBinaryStream(final int index, InputStream inputStream, final int length) throws SQLException  244 { 245 final FileSupport fileSupport = this.getFileSupport(); 246 final File file = fileSupport.createFile(inputStream); 247 248 Operation<T, Void > operation = new Operation<T, Void >() 249 { 250 public Void execute(Database database, T statement) throws SQLException  251 { 252 statement.setBinaryStream(index, fileSupport.getInputStream(file), length); 253 254 return null; 255 } 256 }; 257 258 this.executeWriteToDriver(operation); 259 } 260 261 264 public void setBlob(final int index, final Blob value) throws SQLException  265 { 266 final FileSupport fileSupport = this.getFileSupport(); 267 final File file = fileSupport.createFile(value); 268 269 Operation<T, Void > operation = new Operation<T, Void >() 270 { 271 public Void execute(Database database, T statement) throws SQLException  272 { 273 statement.setBlob(index, fileSupport.getBlob(file)); 274 275 return null; 276 } 277 }; 278 279 this.executeWriteToDriver(operation); 280 } 281 282 285 public void setBoolean(final int index, final boolean value) throws SQLException  286 { 287 Operation<T, Void > operation = new Operation<T, Void >() 288 { 289 public Void execute(Database database, T statement) throws SQLException  290 { 291 statement.setBoolean(index, value); 292 293 return null; 294 } 295 }; 296 297 this.executeWriteToDriver(operation); 298 } 299 300 303 public void setByte(final int index, final byte value) throws SQLException  304 { 305 Operation<T, Void > operation = new Operation<T, Void >() 306 { 307 public Void execute(Database database, T statement) throws SQLException  308 { 309 statement.setByte(index, value); 310 311 return null; 312 } 313 }; 314 315 this.executeWriteToDriver(operation); 316 } 317 318 321 public void setBytes(final int index, final byte[] value) throws SQLException  322 { 323 Operation<T, Void > operation = new Operation<T, Void >() 324 { 325 public Void execute(Database database, T statement) throws SQLException  326 { 327 statement.setBytes(index, value); 328 329 return null; 330 } 331 }; 332 333 this.executeWriteToDriver(operation); 334 } 335 336 339 public void setCharacterStream(final int index, Reader reader, final int length) throws SQLException  340 { 341 final FileSupport fileSupport = this.getFileSupport(); 342 final File file = fileSupport.createFile(reader); 343 344 Operation<T, Void > operation = new Operation<T, Void >() 345 { 346 public Void execute(Database database, T statement) throws SQLException  347 { 348 statement.setCharacterStream(index, fileSupport.getReader(file), length); 349 350 return null; 351 } 352 }; 353 354 this.executeWriteToDriver(operation); 355 } 356 357 360 public void setClob(final int index, final Clob value) throws SQLException  361 { 362 final FileSupport fileSupport = this.getFileSupport(); 363 final File file = fileSupport.createFile(value); 364 365 Operation<T, Void > operation = new Operation<T, Void >() 366 { 367 public Void execute(Database database, T statement) throws SQLException  368 { 369 statement.setClob(index, fileSupport.getClob(file)); 370 371 return null; 372 } 373 }; 374 375 this.executeWriteToDriver(operation); 376 } 377 378 381 public void setDate(final int index, final Date value) throws SQLException  382 { 383 Operation<T, Void > operation = new Operation<T, Void >() 384 { 385 public Void execute(Database database, T statement) throws SQLException  386 { 387 statement.setDate(index, value); 388 389 return null; 390 } 391 }; 392 393 this.executeWriteToDriver(operation); 394 } 395 396 399 public void setDate(final int index, final Date value, final Calendar calendar) throws SQLException  400 { 401 Operation<T, Void > operation = new Operation<T, Void >() 402 { 403 public Void execute(Database database, T statement) throws SQLException  404 { 405 statement.setDate(index, value, calendar); 406 407 return null; 408 } 409 }; 410 411 this.executeWriteToDriver(operation); 412 } 413 414 417 public void setDouble(final int index, final double value) throws SQLException  418 { 419 Operation<T, Void > operation = new Operation<T, Void >() 420 { 421 public Void execute(Database database, T statement) throws SQLException  422 { 423 statement.setDouble(index, value); 424 425 return null; 426 } 427 }; 428 429 this.executeWriteToDriver(operation); 430 } 431 432 435 public void setFloat(final int index, final float value) throws SQLException  436 { 437 Operation<T, Void > operation = new Operation<T, Void >() 438 { 439 public Void execute(Database database, T statement) throws SQLException  440 { 441 statement.setFloat(index, value); 442 443 return null; 444 } 445 }; 446 447 this.executeWriteToDriver(operation); 448 } 449 450 453 public void setInt(final int index, final int value) throws SQLException  454 { 455 Operation<T, Void > operation = new Operation<T, Void >() 456 { 457 public Void execute(Database database, T statement) throws SQLException  458 { 459 statement.setInt(index, value); 460 461 return null; 462 } 463 }; 464 465 this.executeWriteToDriver(operation); 466 } 467 468 471 public void setLong(final int index, final long value) throws SQLException  472 { 473 Operation<T, Void > operation = new Operation<T, Void >() 474 { 475 public Void execute(Database database, T statement) throws SQLException  476 { 477 statement.setLong(index, value); 478 479 return null; 480 } 481 }; 482 483 this.executeWriteToDriver(operation); 484 } 485 486 489 public void setNull(final int index, final int sqlType) throws SQLException  490 { 491 Operation<T, Void > operation = new Operation<T, Void >() 492 { 493 public Void execute(Database database, T statement) throws SQLException  494 { 495 statement.setNull(index, sqlType); 496 497 return null; 498 } 499 }; 500 501 this.executeWriteToDriver(operation); 502 } 503 504 507 public void setNull(final int index, final int sqlType, final String typeName) throws SQLException  508 { 509 Operation<T, Void > operation = new Operation<T, Void >() 510 { 511 public Void execute(Database database, T statement) throws SQLException  512 { 513 statement.setNull(index, sqlType, typeName); 514 515 return null; 516 } 517 }; 518 519 this.executeWriteToDriver(operation); 520 } 521 522 525 public void setObject(final int index, final Object value) throws SQLException  526 { 527 Operation<T, Void > operation = new Operation<T, Void >() 528 { 529 public Void execute(Database database, T statement) throws SQLException  530 { 531 statement.setObject(index, value); 532 533 return null; 534 } 535 }; 536 537 this.executeWriteToDriver(operation); 538 } 539 540 543 public void setObject(final int index, final Object value, final int sqlType) throws SQLException  544 { 545 Operation<T, Void > operation = new Operation<T, Void >() 546 { 547 public Void execute(Database database, T statement) throws SQLException  548 { 549 statement.setObject(index, value, sqlType); 550 551 return null; 552 } 553 }; 554 555 this.executeWriteToDriver(operation); 556 } 557 558 561 public void setObject(final int index, final Object value, final int sqlType, final int scale) throws SQLException  562 { 563 Operation<T, Void > operation = new Operation<T, Void >() 564 { 565 public Void execute(Database database, T statement) throws SQLException  566 { 567 statement.setObject(index, value, sqlType, scale); 568 569 return null; 570 } 571 }; 572 573 this.executeWriteToDriver(operation); 574 } 575 576 579 public void setRef(final int index, final Ref value) throws SQLException  580 { 581 Operation<T, Void > operation = new Operation<T, Void >() 582 { 583 public Void execute(Database database, T statement) throws SQLException  584 { 585 statement.setRef(index, value); 586 587 return null; 588 } 589 }; 590 591 this.executeWriteToDriver(operation); 592 } 593 594 597 public void setShort(final int index, final short value) throws SQLException  598 { 599 Operation<T, Void > operation = new Operation<T, Void >() 600 { 601 public Void execute(Database database, T statement) throws SQLException  602 { 603 statement.setShort(index, value); 604 605 return null; 606 } 607 }; 608 609 this.executeWriteToDriver(operation); 610 } 611 612 615 public void setString(final int index, final String value) throws SQLException  616 { 617 Operation<T, Void > operation = new Operation<T, Void >() 618 { 619 public Void execute(Database database, T statement) throws SQLException  620 { 621 statement.setString(index, value); 622 623 return null; 624 } 625 }; 626 627 this.executeWriteToDriver(operation); 628 } 629 630 633 public void setTime(final int index, final Time value) throws SQLException  634 { 635 Operation<T, Void > operation = new Operation<T, Void >() 636 { 637 public Void execute(Database database, T statement) throws SQLException  638 { 639 statement.setTime(index, value); 640 641 return null; 642 } 643 }; 644 645 this.executeWriteToDriver(operation); 646 } 647 648 651 public void setTime(final int index, final Time value, final Calendar calendar) throws SQLException  652 { 653 Operation<T, Void > operation = new Operation<T, Void >() 654 { 655 public Void execute(Database database, T statement) throws SQLException  656 { 657 statement.setTime(index, value, calendar); 658 659 return null; 660 } 661 }; 662 663 this.executeWriteToDriver(operation); 664 } 665 666 669 public void setTimestamp(final int index, final Timestamp value) throws SQLException  670 { 671 Operation<T, Void > operation = new Operation<T, Void >() 672 { 673 public Void execute(Database database, T statement) throws SQLException  674 { 675 statement.setTimestamp(index, value); 676 677 return null; 678 } 679 }; 680 681 this.executeWriteToDriver(operation); 682 } 683 684 687 public void setTimestamp(final int index, final Timestamp value, final Calendar calendar) throws SQLException  688 { 689 Operation<T, Void > operation = new Operation<T, Void >() 690 { 691 public Void execute(Database database, T statement) throws SQLException  692 { 693 statement.setTimestamp(index, value, calendar); 694 695 return null; 696 } 697 }; 698 699 this.executeWriteToDriver(operation); 700 } 701 702 706 @Deprecated  707 public void setUnicodeStream(final int index, InputStream inputStream, final int length) throws SQLException  708 { 709 final FileSupport fileSupport = this.getFileSupport(); 710 final File file = fileSupport.createFile(inputStream); 711 712 Operation<T, Void > operation = new Operation<T, Void >() 713 { 714 public Void execute(Database database, T statement) throws SQLException  715 { 716 statement.setUnicodeStream(index, fileSupport.getInputStream(file), length); 717 718 return null; 719 } 720 }; 721 722 this.executeWriteToDriver(operation); 723 } 724 725 728 public void setURL(final int index, final URL value) throws SQLException  729 { 730 Operation<T, Void > operation = new Operation<T, Void >() 731 { 732 public Void execute(Database database, T statement) throws SQLException  733 { 734 statement.setURL(index, value); 735 736 return null; 737 } 738 }; 739 740 this.executeWriteToDriver(operation); 741 } 742 743 protected FileSupport getFileSupport() 744 { 745 Connection connection = (Connection) this.getConnection(); 746 747 return connection.getFileSupport(); 748 } 749 } 750 | Popular Tags |