1 21 package oracle.toplink.essentials.internal.queryframework; 23 24 import java.util.*; 25 import oracle.toplink.essentials.internal.helper.*; 26 import oracle.toplink.essentials.internal.databaseaccess.DatasourceCall; 27 import oracle.toplink.essentials.internal.databaseaccess.DatabaseCall; 28 import oracle.toplink.essentials.exceptions.*; 29 import oracle.toplink.essentials.queryframework.*; 30 import oracle.toplink.essentials.internal.sessions.AbstractRecord; 31 import oracle.toplink.essentials.internal.sessions.AbstractSession; 32 33 43 public class DatasourceCallQueryMechanism extends DatabaseQueryMechanism { 44 protected DatasourceCall call; 45 46 47 protected Vector calls; 48 49 53 public DatasourceCallQueryMechanism(DatabaseQuery query) { 54 super(query); 55 } 56 57 61 public DatasourceCallQueryMechanism(DatabaseQuery query, DatasourceCall call) { 62 super(query); 63 this.call = call; 64 call.setQuery(query); 65 } 66 67 70 public void addCall(DatasourceCall call) { 71 getCalls().addElement(call); 72 call.setQuery(getQuery()); 73 } 74 75 79 public DatabaseCall cursorSelectAllRows() throws DatabaseException { 80 try { 81 return (DatabaseCall)executeCall(); 82 } catch (java.lang.ClassCastException e) { 83 throw QueryException.mustUseCursorStreamPolicy(); 84 } 85 } 86 87 92 public Integer deleteAll() throws DatabaseException { 93 if(((DeleteAllQuery)getQuery()).isPreparedUsingTempStorage()) { 94 return deleteAllUsingTempTables(); 95 } else { 96 if (hasMultipleCalls()) { 97 Integer returnedRowCount = null; 98 99 for (int index = getCalls().size() - 1; index >= 0; index--) { 101 DatasourceCall databseCall = (DatasourceCall)getCalls().elementAt(index); 102 returnedRowCount = (Integer )executeCall(databseCall); 103 } 104 return returnedRowCount; 106 } else { 107 return (Integer )executeCall(); 108 } 109 } 110 } 111 112 117 public Integer deleteAllUsingTempTables() throws DatabaseException { 118 DatabaseException ex = null; 119 Integer returnedRowCount = null; 120 121 123 try { 126 DatasourceCall databseCall = (DatasourceCall)getCalls().elementAt(getCalls().size() - 1); 127 executeCall(databseCall); 128 } catch (DatabaseException databaseEx) { 129 } 131 132 if(ex == null) { 135 try { 136 DatasourceCall databseCall = (DatasourceCall)getCalls().elementAt(getCalls().size() - 2); 137 executeCall(databseCall); 138 } catch (DatabaseException databaseEx) { 139 ex = databaseEx; 140 } 141 } 142 143 for (int index = getCalls().size() - 3; index >= 1 && ex == null; index--) { 146 DatasourceCall databseCall = (DatasourceCall)getCalls().elementAt(index); 147 try { 148 returnedRowCount = (Integer )executeCall(databseCall); 150 } catch (DatabaseException databaseEx) { 151 ex = databaseEx; 152 } 153 } 154 155 try { 158 DatasourceCall databseCall = (DatasourceCall)getCalls().elementAt(0); 159 executeCall(databseCall); 160 } catch (DatabaseException databaseEx) { 161 } 163 164 if(ex != null) { 165 throw ex; 166 } 167 168 return returnedRowCount; 169 } 170 171 176 public Integer deleteObject() throws DatabaseException { 177 if (hasMultipleCalls()) { 178 Integer returnedRowCount = null; 179 180 for (int index = getCalls().size() - 1; index >= 0; index--) { 182 DatasourceCall databseCall = (DatasourceCall)getCalls().elementAt(index); 183 Integer rowCount = (Integer )executeCall(databseCall); 184 if ((index == (getCalls().size() - 1)) || (rowCount.intValue() <= 0)) { returnedRowCount = rowCount; 186 } 187 } 188 return returnedRowCount; 189 } else { 190 return (Integer )executeCall(); 191 } 192 } 193 194 198 protected Object executeCall() throws DatabaseException { 199 return executeCall(getCall()); 200 } 201 202 206 protected Object executeCall(DatasourceCall databaseCall) throws DatabaseException { 207 AbstractSession sessionToUse = getSession().getExecutionSession(getQuery()); 210 DatasourceCall clonedCall = (DatasourceCall)databaseCall.clone(); 211 clonedCall.setQuery(getQuery()); 212 clonedCall.translate(getTranslationRow(), getModifyRow(), sessionToUse); 213 return sessionToUse.executeCall(clonedCall, getTranslationRow(), getQuery()); 214 } 215 216 221 public Integer executeNoSelect() throws DatabaseException { 222 return executeNoSelectCall(); 223 } 224 225 230 public Integer executeNoSelectCall() throws DatabaseException { 231 if (hasMultipleCalls()) { 232 Integer returnedRowCount = null; 233 for (int index = 0; index < getCalls().size(); index++) { 234 DatasourceCall databseCall = (DatasourceCall)getCalls().elementAt(index); 235 Integer rowCount = (Integer )executeCall(databseCall); 236 if ((index == 0) || (rowCount.intValue() <= 0)) { returnedRowCount = rowCount; 238 } 239 } 240 return returnedRowCount; 241 } else { 242 return (Integer )executeCall(); 243 } 244 } 245 246 251 public Vector executeSelect() throws DatabaseException { 252 return executeSelectCall(); 253 } 254 255 260 public Vector executeSelectCall() throws DatabaseException { 261 if (hasMultipleCalls()) { 262 Vector results = new Vector(); 263 for (Enumeration callsEnum = getCalls().elements(); callsEnum.hasMoreElements();) { 264 DatasourceCall databseCall = (DatasourceCall)callsEnum.nextElement(); 265 Helper.addAllToVector(results, (Vector)executeCall(databseCall)); 266 } 267 268 return results; 269 } else { 270 return (Vector)executeCall(); 271 } 272 } 273 274 277 public DatasourceCall getCall() { 278 return call; 279 } 280 281 285 public Vector getCalls() { 286 if (calls == null) { 287 calls = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(3); 288 } 289 return calls; 290 } 291 292 296 public boolean hasMultipleCalls() { 297 return (calls != null) && (!calls.isEmpty()); 298 } 299 300 304 public void insertObject() throws DatabaseException { 305 Class cls = ((DatabaseQuery)getQuery()).getReferenceClass(); 306 boolean usesSequencing = getDescriptor().usesSequenceNumbers(); 307 boolean shouldAcquireValueAfterInsert = false; 308 if (usesSequencing) { 309 shouldAcquireValueAfterInsert = getSession().getSequencing().shouldAcquireValueAfterInsert(cls); 310 } 311 Collection returnFields = null; 312 313 if (usesSequencing && !shouldAcquireValueAfterInsert) { 315 updateObjectAndRowWithSequenceNumber(); 317 } 318 319 if (hasMultipleCalls()) { 320 for (int index = 0; index < getCalls().size(); index++) { 321 DatasourceCall databseCall = (DatasourceCall)getCalls().elementAt(index); 322 executeCall(databseCall); 323 if (returnFields != null) { 324 updateObjectAndRowWithReturnRow(returnFields, index == 0); 325 } 326 if ((index == 0) && usesSequencing && shouldAcquireValueAfterInsert) { 327 updateObjectAndRowWithSequenceNumber(); 328 } 329 } 330 } else { 331 executeCall(); 332 if (returnFields != null) { 333 updateObjectAndRowWithReturnRow(returnFields, true); 334 } 335 if (usesSequencing && shouldAcquireValueAfterInsert) { 336 updateObjectAndRowWithSequenceNumber(); 337 } 338 } 339 340 AbstractSession executionSession = getSession().getExecutionSession(getQuery()); 351 executionSession.getAccessor().flushSelectCalls(executionSession); 352 } 353 354 357 public boolean isCallQueryMechanism() { 358 return true; 359 } 360 361 368 public void prepare() { 369 if ((!hasMultipleCalls()) && (getCall() == null)) { 370 throw QueryException.sqlStatementNotSetProperly(getQuery()); 371 } 372 } 373 374 381 public void prepareCall() throws QueryException { 382 DatabaseQuery query = getQuery(); 383 AbstractSession executionSession = getSession().getExecutionSession(query); 384 if (hasMultipleCalls()) { 385 for (Enumeration callsEnum = getCalls().elements(); callsEnum.hasMoreElements();) { 386 DatasourceCall call = (DatasourceCall)callsEnum.nextElement(); 387 call.prepare(executionSession); 388 } 389 } else if (getCall() != null) { 390 getCall().prepare(executionSession); 391 } 392 } 393 394 397 public void prepareCursorSelectAllRows() throws QueryException { 398 getCall().returnCursor(); 399 prepareCall(); 400 } 401 402 405 public void prepareDeleteAll() { 406 if (hasMultipleCalls()) { 407 for (Enumeration callsEnum = getCalls().elements(); callsEnum.hasMoreElements();) { 408 DatasourceCall call = (DatasourceCall)callsEnum.nextElement(); 409 call.returnNothing(); 410 } 411 } else { 412 getCall().returnNothing(); 413 } 414 prepareCall(); 415 } 416 417 420 public void prepareDeleteObject() { 421 if (hasMultipleCalls()) { 422 for (Enumeration callsEnum = getCalls().elements(); callsEnum.hasMoreElements();) { 423 DatasourceCall call = (DatasourceCall)callsEnum.nextElement(); 424 call.returnNothing(); 425 } 426 } else { 427 getCall().returnNothing(); 428 } 429 prepareCall(); 430 } 431 432 435 public void prepareDoesExist(DatabaseField field) { 436 if (hasMultipleCalls()) { 437 for (Enumeration callsEnum = getCalls().elements(); callsEnum.hasMoreElements();) { 438 ((DatasourceCall)callsEnum.nextElement()).returnOneRow(); 439 } 440 } else { 441 getCall().returnOneRow(); 442 } 443 prepareCall(); 444 } 445 446 449 public void prepareExecuteNoSelect() { 450 if (hasMultipleCalls()) { 451 for (Enumeration callsEnum = getCalls().elements(); callsEnum.hasMoreElements();) { 452 ((DatasourceCall)callsEnum.nextElement()).returnNothing(); 453 } 454 } else { 455 getCall().returnNothing(); 456 } 457 prepareCall(); 458 } 459 460 463 public void prepareExecuteSelect() { 464 if (hasMultipleCalls()) { 465 for (Enumeration callsEnum = getCalls().elements(); callsEnum.hasMoreElements();) { 466 DatasourceCall databseCall = (DatasourceCall)callsEnum.nextElement(); 467 databseCall.returnManyRows(); 468 } 469 } else { 470 getCall().returnManyRows(); 471 } 472 prepareCall(); 473 } 474 475 478 public void prepareInsertObject() { 479 if (hasMultipleCalls()) { 480 for (Enumeration callsEnum = getCalls().elements(); callsEnum.hasMoreElements();) { 481 ((DatasourceCall)callsEnum.nextElement()).returnNothing(); 482 } 483 } else { 484 getCall().returnNothing(); 485 } 486 prepareCall(); 487 } 488 489 492 protected void prepareReportQueryItems(){ 493 int itemOffset = 0; 495 for (Iterator items = ((ReportQuery)getQuery()).getItems().iterator(); items.hasNext();){ 496 ReportItem item = (ReportItem) items.next(); 497 item.setResultIndex(itemOffset); 498 if (item.getAttributeExpression() != null){ 499 JoinedAttributeManager joinManager = item.getJoinedAttributeManager(); 500 if (joinManager.hasJoinedExpressions()){ 501 itemOffset = joinManager.computeJoiningMappingIndexes(true, getSession(),itemOffset); 502 }else{ 503 if (item.getDescriptor() != null){ 504 itemOffset += item.getDescriptor().getAllFields().size(); 505 }else { 506 ++itemOffset; } 508 } 509 } 510 } 511 512 } 513 516 public void prepareReportQuerySelectAllRows() { 517 prepareReportQueryItems(); 518 prepareExecuteSelect(); 519 } 520 521 524 public void prepareReportQuerySubSelect() { 525 prepareReportQueryItems(); 526 prepareCall(); 527 } 528 529 532 public void prepareSelectAllRows() { 533 if (hasMultipleCalls()) { 534 for (Enumeration callsEnum = getCalls().elements(); callsEnum.hasMoreElements();) { 535 DatasourceCall databseCall = (DatasourceCall)callsEnum.nextElement(); 536 databseCall.returnManyRows(); 537 } 538 } else { 539 getCall().returnManyRows(); 540 } 541 prepareCall(); 542 } 543 544 547 public void prepareSelectOneRow() { 548 if (hasMultipleCalls()) { 549 for (Enumeration callsEnum = getCalls().elements(); callsEnum.hasMoreElements();) { 550 DatasourceCall databseCall = (DatasourceCall)callsEnum.nextElement(); 551 databseCall.returnOneRow(); 552 } 553 } else { 554 getCall().returnOneRow(); 555 } 556 prepareCall(); 557 } 558 559 562 public void prepareUpdateObject() { 563 if (hasMultipleCalls()) { 564 for (Enumeration callsEnum = getCalls().elements(); callsEnum.hasMoreElements();) { 565 DatasourceCall call = (DatasourceCall)callsEnum.nextElement(); 566 call.returnNothing(); 567 } 568 } else if (getCall() != null) { 569 getCall().returnNothing(); 570 } 571 prepareCall(); 572 } 573 574 577 public void prepareUpdateAll() { 578 if (getCall() != null) { 579 getCall().returnNothing(); 580 } 581 582 prepareCall(); 583 } 584 585 590 public Vector selectAllReportQueryRows() throws DatabaseException { 591 return executeSelect(); 592 } 593 594 599 public Vector selectAllRows() throws DatabaseException { 600 return executeSelectCall(); 601 } 602 603 608 public AbstractRecord selectOneRow() throws DatabaseException { 609 if (hasMultipleCalls()) { 610 for (Enumeration callsEnum = getCalls().elements(); callsEnum.hasMoreElements();) { 611 DatasourceCall databaseCall = (DatasourceCall)callsEnum.nextElement(); 612 AbstractRecord result = (AbstractRecord)executeCall(databaseCall); 613 if (result != null) { 614 return result; 615 } 616 } 617 618 return null; 619 } else { 620 return (AbstractRecord)executeCall(); 621 } 622 } 623 624 630 public AbstractRecord selectRowForDoesExist(DatabaseField field) throws DatabaseException { 631 if (hasMultipleCalls()) { 632 for (Enumeration callsEnum = getCalls().elements(); callsEnum.hasMoreElements();) { 633 DatasourceCall databaseCall = (DatasourceCall)callsEnum.nextElement(); 634 AbstractRecord result = (AbstractRecord)executeCall(databaseCall); 635 if (result != null) { 636 return result; 637 } 638 } 639 640 return null; 641 } else { 642 return (AbstractRecord)executeCall(); 643 } 644 } 645 646 649 public void setCall(DatasourceCall call) { 650 this.call = call; 651 if (call != null) { 652 call.setQuery(getQuery()); 653 } 654 } 655 656 660 protected void setCalls(Vector calls) { 661 this.calls = calls; 662 } 663 664 669 public Integer updateObject() throws DatabaseException { 670 Collection returnFields = null; 671 Integer returnedRowCount = null; 672 if (hasMultipleCalls()) { 673 for (int index = 0; index < getCalls().size(); index++) { 674 DatasourceCall databseCall = (DatasourceCall)getCalls().elementAt(index); 675 Integer rowCount = (Integer )executeCall(databseCall); 676 if ((index == 0) || (rowCount.intValue() <= 0)) { returnedRowCount = rowCount; 678 } 679 if (returnFields != null) { 680 updateObjectAndRowWithReturnRow(returnFields, false); 681 } 682 } 683 } else { 684 returnedRowCount = (Integer )executeCall(); 685 if (returnFields != null) { 686 updateObjectAndRowWithReturnRow(returnFields, false); 687 } 688 } 689 690 AbstractSession executionSession = getSession().getExecutionSession(getQuery()); 701 executionSession.getAccessor().flushSelectCalls(executionSession); 702 return returnedRowCount; 703 } 704 705 709 public Integer updateAll() throws DatabaseException { 710 if(((UpdateAllQuery)getQuery()).isPreparedUsingTempStorage() && getSession().getPlatform().supportsTempTables()) { 711 return updateAllUsingTempTables(); 712 } else { 713 Integer rowCount = executeNoSelectCall(); 714 if(((UpdateAllQuery)getQuery()).isPreparedUsingTempStorage()) { 715 AbstractRecord outputRow = (AbstractRecord)getQuery().getProperty("output"); 717 rowCount = (Integer )outputRow.get("ROW_COUNT"); 718 } 719 return rowCount; 720 } 721 } 722 723 728 public Integer updateAllUsingTempTables() throws DatabaseException { 729 int nTables = getCalls().size() / 4; 730 DatabaseException ex = null; 731 Integer returnedRowCount = null; 732 733 for (int index = 0; index < nTables; index++) { 736 try { 737 DatasourceCall databseCall = (DatasourceCall)getCalls().elementAt(index); 738 executeCall(databseCall); 739 } catch (DatabaseException databaseEx) { 740 } 742 } 743 744 for (int index = nTables; index < nTables*2 && ex == null; index++) { 747 try { 748 DatasourceCall databseCall = (DatasourceCall)getCalls().elementAt(index); 749 executeCall(databseCall); 750 } catch (DatabaseException databaseEx) { 751 ex = databaseEx; 752 } 753 } 754 755 for (int index = nTables*2; index < nTables*3 && ex == null; index++) { 758 try { 759 DatasourceCall databseCall = (DatasourceCall)getCalls().elementAt(index); 760 Integer rowCount = (Integer )executeCall(databseCall); 761 if ((index == nTables*2) || (rowCount.intValue() <= 0)) { returnedRowCount = rowCount; 763 } 764 } catch (DatabaseException databaseEx) { 765 ex = databaseEx; 766 } 767 } 768 769 for (int index = nTables*3; index < nTables*4; index++) { 772 try { 773 DatasourceCall databseCall = (DatasourceCall)getCalls().elementAt(index); 774 executeCall(databseCall); 775 } catch (DatabaseException databaseEx) { 776 } 778 } 779 780 if(ex != null) { 781 throw ex; 782 } 783 784 return returnedRowCount; 785 } 786 787 791 protected void updateForeignKeyFieldAfterInsert(WriteObjectQuery writeQuery) { 792 writeQuery.setModifyRow(this.getDescriptor().getObjectBuilder().buildRow(writeQuery.getObject(), this.getSession())); 793 794 AbstractSession sessionToUse = getSession().getExecutionSession(getQuery()); 797 798 Vector calls = ((DatasourceCallQueryMechanism)this.getDescriptor().getQueryManager().getUpdateQuery().getQueryMechanism()).getCalls(); 800 for (Enumeration stream = calls.elements(); stream.hasMoreElements();) { 801 DatasourceCall call = (DatasourceCall)((DatasourceCall)stream.nextElement()).clone(); 802 call.setQuery(writeQuery); 803 sessionToUse.executeCall(call, this.getTranslationRow(), writeQuery); 804 } 805 } 806 } 807 | Popular Tags |