1 21 package net.sf.hajdbc.sql; 22 23 import java.sql.SQLException ; 24 import java.sql.SQLWarning ; 25 import java.util.Map ; 26 27 import net.sf.hajdbc.Database; 28 import net.sf.hajdbc.Operation; 29 30 36 public class Statement<T extends java.sql.Statement > extends SQLObject<T, java.sql.Connection > implements java.sql.Statement 37 { 38 44 public Statement(Connection<?> connection, Operation<java.sql.Connection , T> operation) throws SQLException 45 { 46 super(connection, operation, connection.getDatabaseCluster().getNonTransactionalExecutor(), null); 47 } 48 49 52 @Override 53 public void handleExceptions(Map <Database, SQLException > exceptionMap) throws SQLException 54 { 55 if (this.getAutoCommit()) 56 { 57 super.handleExceptions(exceptionMap); 58 } 59 else 60 { 61 SQLException exception = null; 63 64 for (Map.Entry <Database, SQLException > exceptionMapEntry: exceptionMap.entrySet()) 65 { 66 Database database = exceptionMapEntry.getKey(); 67 SQLException cause = exceptionMapEntry.getValue(); 68 69 try 70 { 71 this.getDatabaseCluster().handleFailure(database, cause); 72 } 73 catch (SQLException e) 74 { 75 if (exception == null) 76 { 77 exception = e; 78 } 79 else 80 { 81 exception.setNextException(e); 82 } 83 } 84 } 85 86 if (exception != null) 87 { 88 throw exception; 89 } 90 } 91 } 92 93 96 public void addBatch(final String sql) throws SQLException 97 { 98 Operation<T, Void > operation = new Operation<T, Void >() 99 { 100 public Void execute(Database database, T statement) throws SQLException 101 { 102 statement.addBatch(sql); 103 104 return null; 105 } 106 }; 107 108 this.executeWriteToDriver(operation); 109 } 110 111 114 public void cancel() throws SQLException 115 { 116 Operation<T, Void > operation = new Operation<T, Void >() 117 { 118 public Void execute(Database database, T statement) throws SQLException 119 { 120 statement.cancel(); 121 122 return null; 123 } 124 }; 125 126 this.executeNonTransactionalWriteToDatabase(operation); 127 } 128 129 132 public void clearBatch() throws SQLException 133 { 134 Operation<T, Void > operation = new Operation<T, Void >() 135 { 136 public Void execute(Database database, T statement) throws SQLException 137 { 138 statement.clearBatch(); 139 140 return null; 141 } 142 }; 143 144 this.executeWriteToDriver(operation); 145 } 146 147 150 public void clearWarnings() throws SQLException 151 { 152 Operation<T, Void > operation = new Operation<T, Void >() 153 { 154 public Void execute(Database database, T statement) throws SQLException 155 { 156 statement.clearWarnings(); 157 158 return null; 159 } 160 }; 161 162 this.executeWriteToDriver(operation); 163 } 164 165 168 public void close() throws SQLException 169 { 170 Operation<T, Void > operation = new Operation<T, Void >() 171 { 172 public Void execute(Database database, T statement) throws SQLException 173 { 174 statement.close(); 175 176 return null; 177 } 178 }; 179 180 this.executeNonTransactionalWriteToDatabase(operation); 181 } 182 183 186 public boolean execute(final String sql) throws SQLException 187 { 188 Operation<T, Boolean > operation = new Operation<T, Boolean >() 189 { 190 public Boolean execute(Database database, T statement) throws SQLException 191 { 192 return statement.execute(sql); 193 } 194 }; 195 196 return this.firstValue(this.executeTransactionalWriteToDatabase(operation)); 197 } 198 199 202 public boolean execute(final String sql, final int autoGeneratedKeys) throws SQLException 203 { 204 Operation<T, Boolean > operation = new Operation<T, Boolean >() 205 { 206 public Boolean execute(Database database, T statement) throws SQLException 207 { 208 return statement.execute(sql, autoGeneratedKeys); 209 } 210 }; 211 212 return this.firstValue(this.executeTransactionalWriteToDatabase(operation)); 213 } 214 215 218 public boolean execute(final String sql, final int[] columnIndexes) throws SQLException 219 { 220 Operation<T, Boolean > operation = new Operation<T, Boolean >() 221 { 222 public Boolean execute(Database database, T statement) throws SQLException 223 { 224 return statement.execute(sql, columnIndexes); 225 } 226 }; 227 228 return this.firstValue(this.executeTransactionalWriteToDatabase(operation)); 229 } 230 231 234 public boolean execute(final String sql, final String [] columnNames) throws SQLException 235 { 236 Operation<T, Boolean > operation = new Operation<T, Boolean >() 237 { 238 public Boolean execute(Database database, T statement) throws SQLException 239 { 240 return statement.execute(sql, columnNames); 241 } 242 }; 243 244 return this.firstValue(this.executeTransactionalWriteToDatabase(operation)); 245 } 246 247 250 public int[] executeBatch() throws SQLException 251 { 252 Operation<T, int[]> operation = new Operation<T, int[]>() 253 { 254 public int[] execute(Database database, T statement) throws SQLException 255 { 256 return statement.executeBatch(); 257 } 258 }; 259 260 return this.firstValue(this.executeTransactionalWriteToDatabase(operation)); 261 } 262 263 266 public java.sql.ResultSet executeQuery(final String sql) throws SQLException 267 { 268 Operation<T, java.sql.ResultSet > operation = new Operation<T, java.sql.ResultSet >() 269 { 270 public java.sql.ResultSet execute(Database database, T statement) throws SQLException 271 { 272 return statement.executeQuery(sql); 273 } 274 }; 275 276 return ((this.getResultSetConcurrency() == java.sql.ResultSet.CONCUR_READ_ONLY) && !this.isSelectForUpdate(sql)) ? this.executeReadFromDatabase(operation) : new ResultSet<T>(this, operation); 277 } 278 279 282 public int executeUpdate(final String sql) throws SQLException 283 { 284 Operation<T, Integer > operation = new Operation<T, Integer >() 285 { 286 public Integer execute(Database database, T statement) throws SQLException 287 { 288 return statement.executeUpdate(sql); 289 } 290 }; 291 292 return this.firstValue(this.executeTransactionalWriteToDatabase(operation)); 293 } 294 295 298 public int executeUpdate(final String sql, final int autoGeneratedKeys) throws SQLException 299 { 300 Operation<T, Integer > operation = new Operation<T, Integer >() 301 { 302 public Integer execute(Database database, T statement) throws SQLException 303 { 304 return statement.executeUpdate(sql, autoGeneratedKeys); 305 } 306 }; 307 308 return this.firstValue(this.executeTransactionalWriteToDatabase(operation)); 309 } 310 311 314 public int executeUpdate(final String sql, final int[] columnIndexes) throws SQLException 315 { 316 Operation<T, Integer > operation = new Operation<T, Integer >() 317 { 318 public Integer execute(Database database, T statement) throws SQLException 319 { 320 return statement.executeUpdate(sql, columnIndexes); 321 } 322 }; 323 324 return this.firstValue(this.executeTransactionalWriteToDatabase(operation)); 325 } 326 327 330 public int executeUpdate(final String sql, final String [] columnNames) throws SQLException 331 { 332 Operation<T, Integer > operation = new Operation<T, Integer >() 333 { 334 public Integer execute(Database database, T statement) throws SQLException 335 { 336 return statement.executeUpdate(sql, columnNames); 337 } 338 }; 339 340 return this.firstValue(this.executeTransactionalWriteToDatabase(operation)); 341 } 342 343 346 public java.sql.Connection getConnection() 347 { 348 return Connection.class.cast(this.parent); 349 } 350 351 354 public int getFetchDirection() throws SQLException 355 { 356 Operation<T, Integer > operation = new Operation<T, Integer >() 357 { 358 public Integer execute(Database database, T statement) throws SQLException 359 { 360 return statement.getFetchDirection(); 361 } 362 }; 363 364 return this.executeReadFromDriver(operation); 365 } 366 367 370 public int getFetchSize() throws SQLException 371 { 372 Operation<T, Integer > operation = new Operation<T, Integer >() 373 { 374 public Integer execute(Database database, T statement) throws SQLException 375 { 376 return statement.getFetchSize(); 377 } 378 }; 379 380 return this.executeReadFromDriver(operation); 381 } 382 383 386 public java.sql.ResultSet getGeneratedKeys() throws SQLException 387 { 388 Operation<T, java.sql.ResultSet > operation = new Operation<T, java.sql.ResultSet >() 389 { 390 public java.sql.ResultSet execute(Database database, T statement) throws SQLException 391 { 392 return statement.getGeneratedKeys(); 393 } 394 }; 395 396 return this.executeReadFromDriver(operation); 397 } 398 399 402 public int getMaxFieldSize() throws SQLException 403 { 404 Operation<T, Integer > operation = new Operation<T, Integer >() 405 { 406 public Integer execute(Database database, T statement) throws SQLException 407 { 408 return statement.getMaxFieldSize(); 409 } 410 }; 411 412 return this.executeReadFromDriver(operation); 413 } 414 415 418 public int getMaxRows() throws SQLException 419 { 420 Operation<T, Integer > operation = new Operation<T, Integer >() 421 { 422 public Integer execute(Database database, T statement) throws SQLException 423 { 424 return statement.getMaxRows(); 425 } 426 }; 427 428 return this.executeReadFromDriver(operation); 429 } 430 431 434 public boolean getMoreResults() throws SQLException 435 { 436 Operation<T, Boolean > operation = new Operation<T, Boolean >() 437 { 438 public Boolean execute(Database database, T statement) throws SQLException 439 { 440 return statement.getMoreResults(); 441 } 442 }; 443 444 return this.firstValue(this.executeNonTransactionalWriteToDatabase(operation)); 445 } 446 447 450 public boolean getMoreResults(final int current) throws SQLException 451 { 452 Operation<T, Boolean > operation = new Operation<T, Boolean >() 453 { 454 public Boolean execute(Database database, T statement) throws SQLException 455 { 456 return statement.getMoreResults(current); 457 } 458 }; 459 460 return this.firstValue((current == KEEP_CURRENT_RESULT) ? this.executeWriteToDriver(operation) : this.executeNonTransactionalWriteToDatabase(operation)); 461 } 462 463 466 public int getQueryTimeout() throws SQLException 467 { 468 Operation<T, Integer > operation = new Operation<T, Integer >() 469 { 470 public Integer execute(Database database, T statement) throws SQLException 471 { 472 return statement.getQueryTimeout(); 473 } 474 }; 475 476 return this.executeReadFromDriver(operation); 477 } 478 479 482 public java.sql.ResultSet getResultSet() throws SQLException 483 { 484 Operation<T, java.sql.ResultSet > operation = new Operation<T, java.sql.ResultSet >() 485 { 486 public java.sql.ResultSet execute(Database database, T statement) throws SQLException 487 { 488 return statement.getResultSet(); 489 } 490 }; 491 492 return (this.getResultSetConcurrency() == java.sql.ResultSet.CONCUR_READ_ONLY) ? this.executeReadFromDriver(operation) : new ResultSet<T>(this, operation); 493 } 494 495 498 public int getResultSetConcurrency() throws SQLException 499 { 500 Operation<T, Integer > operation = new Operation<T, Integer >() 501 { 502 public Integer execute(Database database, T statement) throws SQLException 503 { 504 return statement.getResultSetConcurrency(); 505 } 506 }; 507 508 return this.executeReadFromDriver(operation); 509 } 510 511 514 public int getResultSetHoldability() throws SQLException 515 { 516 Operation<T, Integer > operation = new Operation<T, Integer >() 517 { 518 public Integer execute(Database database, T statement) throws SQLException 519 { 520 return statement.getResultSetHoldability(); 521 } 522 }; 523 524 return this.executeReadFromDriver(operation); 525 } 526 527 530 public int getResultSetType() throws SQLException 531 { 532 Operation<T, Integer > operation = new Operation<T, Integer >() 533 { 534 public Integer execute(Database database, T statement) throws SQLException 535 { 536 return statement.getResultSetType(); 537 } 538 }; 539 540 return this.executeReadFromDriver(operation); 541 } 542 543 546 public int getUpdateCount() throws SQLException 547 { 548 Operation<T, Integer > operation = new Operation<T, Integer >() 549 { 550 public Integer execute(Database database, T statement) throws SQLException 551 { 552 return statement.getUpdateCount(); 553 } 554 }; 555 556 return this.executeReadFromDriver(operation); 557 } 558 559 562 public SQLWarning getWarnings() throws SQLException 563 { 564 Operation<T, SQLWarning > operation = new Operation<T, SQLWarning >() 565 { 566 public SQLWarning execute(Database database, T statement) throws SQLException 567 { 568 return statement.getWarnings(); 569 } 570 }; 571 572 return this.executeReadFromDriver(operation); 573 } 574 575 578 public void setCursorName(final String name) throws SQLException 579 { 580 Operation<T, Void > operation = new Operation<T, Void >() 581 { 582 public Void execute(Database database, T statement) throws SQLException 583 { 584 statement.setCursorName(name); 585 586 return null; 587 } 588 }; 589 590 this.executeNonTransactionalWriteToDatabase(operation); 591 592 this.record(operation); 593 } 594 595 598 public void setEscapeProcessing(final boolean enable) throws SQLException 599 { 600 Operation<T, Void > operation = new Operation<T, Void >() 601 { 602 public Void execute(Database database, T statement) throws SQLException 603 { 604 statement.setEscapeProcessing(enable); 605 606 return null; 607 } 608 }; 609 610 this.executeWriteToDriver(operation); 611 } 612 613 616 public void setFetchDirection(final int direction) throws SQLException 617 { 618 Operation<T, Void > operation = new Operation<T, Void >() 619 { 620 public Void execute(Database database, T statement) throws SQLException 621 { 622 statement.setFetchDirection(direction); 623 624 return null; 625 } 626 }; 627 628 this.executeWriteToDriver(operation); 629 } 630 631 634 public void setFetchSize(final int size) throws SQLException 635 { 636 Operation<T, Void > operation = new Operation<T, Void >() 637 { 638 public Void execute(Database database, T statement) throws SQLException 639 { 640 statement.setFetchSize(size); 641 642 return null; 643 } 644 }; 645 646 this.executeWriteToDriver(operation); 647 } 648 649 652 public void setMaxFieldSize(final int size) throws SQLException 653 { 654 Operation<T, Void > operation = new Operation<T, Void >() 655 { 656 public Void execute(Database database, T statement) throws SQLException 657 { 658 statement.setMaxFieldSize(size); 659 660 return null; 661 } 662 }; 663 664 this.executeWriteToDriver(operation); 665 } 666 667 670 public void setMaxRows(final int rows) throws SQLException 671 { 672 Operation<T, Void > operation = new Operation<T, Void >() 673 { 674 public Void execute(Database database, T statement) throws SQLException 675 { 676 statement.setMaxRows(rows); 677 678 return null; 679 } 680 }; 681 682 this.executeNonTransactionalWriteToDatabase(operation); 683 684 this.record(operation); 685 } 686 687 690 public void setQueryTimeout(final int seconds) throws SQLException 691 { 692 Operation<T, Void > operation = new Operation<T, Void >() 693 { 694 public Void execute(Database database, T statement) throws SQLException 695 { 696 statement.setQueryTimeout(seconds); 697 698 return null; 699 } 700 }; 701 702 this.executeWriteToDriver(operation); 703 } 704 705 private boolean getAutoCommit() 706 { 707 try 708 { 709 return this.getConnection().getAutoCommit(); 710 } 711 catch (SQLException e) 712 { 713 return true; 714 } 715 } 716 717 protected boolean isSelectForUpdate(String sql) throws SQLException 718 { 719 return this.getDatabaseCluster().getDialect().isSelectForUpdate(this.getConnection().getMetaData(), sql); 720 } 721 722 725 @Override 726 protected void close(T statement) throws SQLException 727 { 728 statement.close(); 729 } 730 } 731 | Popular Tags |