1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
|
/*
* Copyright (c) 1998, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2022 IBM Corporation. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/
// Contributors:
// Oracle - initial API and implementation from Oracle TopLink
// 07/13/2012-2.5 Guy Pelletier
// - 350487: JPA 2.1 Specification defined support for Stored Procedure Calls
// 08/24/2012-2.5 Guy Pelletier
// - 350487: JPA 2.1 Specification defined support for Stored Procedure Calls
// 01/31/2017-2.6 Will Dazey
// - 511426: Adding cloning support
// 04/11/2018 - Will Dazey
// - 533148 : Add the eclipselink.jpa.sql-call-deferral property
package org.eclipse.persistence.internal.queries;
import java.util.Collection;
import java.util.Enumeration;
import java.util.List;
import java.util.Vector;
import org.eclipse.persistence.descriptors.ClassDescriptor;
import org.eclipse.persistence.exceptions.DatabaseException;
import org.eclipse.persistence.exceptions.QueryException;
import org.eclipse.persistence.internal.databaseaccess.Accessor;
import org.eclipse.persistence.internal.databaseaccess.DatabaseCall;
import org.eclipse.persistence.internal.databaseaccess.DatasourceCall;
import org.eclipse.persistence.internal.helper.DatabaseField;
import org.eclipse.persistence.internal.helper.DatabaseTable;
import org.eclipse.persistence.internal.helper.Helper;
import org.eclipse.persistence.internal.sessions.AbstractRecord;
import org.eclipse.persistence.internal.sessions.AbstractSession;
import org.eclipse.persistence.mappings.DatabaseMapping.WriteType;
import org.eclipse.persistence.queries.ConstructorReportItem;
import org.eclipse.persistence.queries.DatabaseQuery;
import org.eclipse.persistence.queries.DeleteAllQuery;
import org.eclipse.persistence.queries.ReportQuery;
import org.eclipse.persistence.queries.UpdateAllQuery;
import org.eclipse.persistence.queries.WriteObjectQuery;
/**
* <p><b>Purpose</b>:
* Mechanism used for call queries.
* <p>
* <p><b>Responsibilities</b>:
* Executes the appropriate call.
*
* @author James Sutherland
* @since OracleAS TopLink 10<i>g</i> (10.0.3)
*/
public class DatasourceCallQueryMechanism extends DatabaseQueryMechanism {
protected DatasourceCall call;
/** Normally only a single call is used, however multiple table may require multiple calls on write. */
protected Vector calls;
public DatasourceCallQueryMechanism() {
}
/**
* Initialize the state of the query
* @param query - owner of mechanism
*/
public DatasourceCallQueryMechanism(DatabaseQuery query) {
super(query);
}
/**
* Initialize the state of the query
* @param query - owner of mechanism
*/
public DatasourceCallQueryMechanism(DatabaseQuery query, DatasourceCall call) {
super(query);
this.call = call;
call.setQuery(query);
}
/**
* Add the call.
*/
public void addCall(DatasourceCall call) {
getCalls().addElement(call);
call.setQuery(getQuery());
}
/**
* Clone the DatasourceCall and Vector<DatasourceCall>.
*/
public DatabaseQueryMechanism clone(DatabaseQuery queryClone) {
DatasourceCallQueryMechanism clone = (DatasourceCallQueryMechanism)super.clone(queryClone);
if(this.call != null) {
DatasourceCall callclone = (DatasourceCall)this.call.clone();
clone.setCall(callclone);
}
if(this.calls != null) {
Vector callsclone = (Vector)this.calls.clone();
clone.setCalls(callsclone);
}
return clone;
}
/**
* Read all rows from the database using a cursored stream.
* @exception DatabaseException - an error has occurred on the database
*/
public DatabaseCall cursorSelectAllRows() throws DatabaseException {
try {
return (DatabaseCall)executeCall();
} catch (java.lang.ClassCastException e) {
throw QueryException.mustUseCursorStreamPolicy();
}
}
/**
* Read all rows from the database, return ResultSet
* @exception DatabaseException - an error has occurred on the database
*/
public DatabaseCall selectResultSet() throws DatabaseException {
try {
// For CR 2923 must move to session we will execute call on now
// so correct DatasourcePlatform used by translate.
AbstractSession sessionToUse = this.query.getExecutionSession();
DatabaseCall clonedCall = (DatabaseCall)this.call.clone();
clonedCall.setQuery(this.query);
clonedCall.translate(this.query.getTranslationRow(), getModifyRow(), sessionToUse);
clonedCall.returnCursor();
return (DatabaseCall)sessionToUse.executeCall(clonedCall, this.query.getTranslationRow(), this.query);
} catch (java.lang.ClassCastException e) {
throw QueryException.invalidDatabaseCall(this.call);
}
}
/**
* INTERNAL:
* Delete a collection of objects. Assume call is correct.
* @exception DatabaseException - an error has occurred on the database
*/
public Integer deleteAll() throws DatabaseException {
if(((DeleteAllQuery)this.query).isPreparedUsingTempStorage()) {
return deleteAllUsingTempTables();
} else {
if (hasMultipleCalls()) {
Integer returnedRowCount = null;
// Deletion must occur in reverse order.
for (int index = getCalls().size() - 1; index >= 0; index--) {
DatasourceCall databseCall = (DatasourceCall)getCalls().elementAt(index);
returnedRowCount = (Integer)executeCall(databseCall);
}
// returns the number of rows removed from the first table in insert order
return returnedRowCount;
} else {
return (Integer)executeCall();
}
}
}
/**
* Execute deleteAll using temp tables
* @exception DatabaseException - an error has occurred on the database.
* @return the row count.
*/
public Integer deleteAllUsingTempTables() throws DatabaseException {
DatabaseException ex = null;
Integer returnedRowCount = null;
// Deletion must occur in reverse order.
// first call - crete temp table.
// may fail in case global temp table already exists.
try {
DatasourceCall databseCall = (DatasourceCall)getCalls().elementAt(getCalls().size() - 1);
executeCall(databseCall);
} catch (DatabaseException databaseEx) {
// ignore
}
// second call - populate temp table.
// if that fails save the exception and untill cleanup
try {
DatasourceCall databseCall = (DatasourceCall)getCalls().elementAt(getCalls().size() - 2);
executeCall(databseCall);
} catch (DatabaseException databaseEx) {
ex = databaseEx;
}
// third (a call per table) - delete from original tables calls.
// if that fails save the exception untill cleanup
for (int index = getCalls().size() - 3; index >= 1 && ex == null; index--) {
DatasourceCall databseCall = (DatasourceCall)getCalls().elementAt(index);
try {
// returns the number of rows removed from the first table in insert order
returnedRowCount = (Integer)executeCall(databseCall);
} catch (DatabaseException databaseEx) {
ex = databaseEx;
}
}
// last call - cleanup temp table.
// ignore exceptions here.
try {
DatasourceCall databseCall = (DatasourceCall)getCalls().elementAt(0);
executeCall(databseCall);
} catch (DatabaseException databaseEx) {
// ignore
}
if(ex != null) {
throw ex;
}
return returnedRowCount;
}
/**
* INTERNAL:
* Delete an object. Assume call is correct
* @exception DatabaseException - an error has occurred on the database
*/
public Integer deleteObject() throws DatabaseException {
if (hasMultipleCalls()) {
Integer returnedRowCount = null;
// Deletion must occur in reverse order.
for (int index = getCalls().size() - 1; index >= 0; index--) {
DatasourceCall databseCall = (DatasourceCall)getCalls().elementAt(index);
Integer rowCount = (Integer)executeCall(databseCall);
if ((index == (getCalls().size() - 1)) || (rowCount.intValue() <= 0)) {// Row count returned must be from first table or zero if any are zero.
returnedRowCount = rowCount;
}
}
return returnedRowCount;
} else {
return (Integer)executeCall();
}
}
/**
* INTERNAL:
* Execute a call.
* @exception DatabaseException - an error has occurred on the database
*/
public Object execute() throws DatabaseException {
return executeCall();
}
/**
* Execute the call. It is assumed the call has been fully prepared.
* @exception DatabaseException - an error has occurred on the database.
*/
protected Object executeCall() throws DatabaseException {
return executeCall(this.call);
}
/**
* Execute the call. It is assumed the call has been fully prepared.
* @exception DatabaseException - an error has occurred on the database.
*/
protected Object executeCall(DatasourceCall databaseCall) throws DatabaseException {
// For CR 2923 must move to session we will execute call on now
// so correct DatasourcePlatform used by translate.
AbstractSession sessionToUse = this.query.getExecutionSession();
DatasourceCall clonedCall = (DatasourceCall)databaseCall.clone();
clonedCall.setQuery(this.query);
clonedCall.translate(this.query.getTranslationRow(), getModifyRow(), sessionToUse);
return sessionToUse.executeCall(clonedCall, this.query.getTranslationRow(), this.query);
}
/**
* Execute a non selecting call.
* @exception DatabaseException - an error has occurred on the database.
* @return Returns either a {@link DatabaseCall} or Integer value,
* depending on if this INSERT call needs to return generated keys
*/
public Object executeNoSelect() throws DatabaseException {
if(((DatabaseCall)this.call).shouldReturnGeneratedKeys()) {
return generateKeysExecuteNoSelect();
}
return executeNoSelectCall();
}
/**
* Execute a non selecting call.
* @exception DatabaseException - an error has occurred on the database.
* @return the row count.
*/
public DatabaseCall generateKeysExecuteNoSelect() throws DatabaseException {
return (DatabaseCall)executeCall();
}
/**
* Execute a non selecting call.
* @exception DatabaseException - an error has occurred on the database.
* @return the row count.
*/
public Integer executeNoSelectCall() throws DatabaseException {
if (hasMultipleCalls()) {
Integer returnedRowCount = null;
for (int index = 0; index < getCalls().size(); index++) {
DatasourceCall databseCall = (DatasourceCall)getCalls().elementAt(index);
Integer rowCount = (Integer)executeCall(databseCall);
if ((index == 0) || (rowCount.intValue() <= 0)) {// Row count returned must be from first table or zero if any are zero.
returnedRowCount = rowCount;
}
}
return returnedRowCount;
} else {
return (Integer)executeCall();
}
}
/**
* INTERNAL:
* Execute a selecting call.
* @exception DatabaseException - an error has occurred on the database
*/
public Vector executeSelect() throws DatabaseException {
return executeSelectCall();
}
/**
* INTERNAL:
* Execute a selecting call.
* @exception DatabaseException - an error has occurred on the database
*/
public Vector executeSelectCall() throws DatabaseException {
if (hasMultipleCalls()) {
Vector results = new Vector();
for (Enumeration callsEnum = getCalls().elements(); callsEnum.hasMoreElements();) {
DatasourceCall databseCall = (DatasourceCall)callsEnum.nextElement();
Helper.addAllToVector(results, (Vector)executeCall(databseCall));
}
return results;
} else {
return (Vector)executeCall();
}
}
/**
* Return the call.
*/
public DatasourceCall getCall() {
return call;
}
/**
* Normally only a single call is used, however multiple table may require multiple calls on write.
* This is lazy initialised to conserve space.
*/
public Vector getCalls() {
if (calls == null) {
calls = org.eclipse.persistence.internal.helper.NonSynchronizedVector.newInstance(3);
}
return calls;
}
/**
* Normally only a single call is used, however multiple table may require multiple calls on write.
* This is lazy initialised to conserve space.
*/
public boolean hasMultipleCalls() {
return (this.calls != null) && (!this.calls.isEmpty());
}
/**
* Insert the object. Assume the call is correct.
* @exception DatabaseException - an error has occurred on the database
*/
public void insertObject() throws DatabaseException {
ClassDescriptor descriptor = getDescriptor();
boolean usesSequencing = descriptor.usesSequenceNumbers();
boolean shouldAcquireValueAfterInsert = false;
if (usesSequencing) {
shouldAcquireValueAfterInsert = descriptor.getSequence().shouldAcquireValueAfterInsert();
}
Collection returnFields = null;
if (descriptor.getReturnFieldsToMergeInsert() != null) {
returnFields = descriptor.getReturnFieldsToMergeInsert();
}
// Check to see if sequence number should be retrieved after insert
if (usesSequencing && !shouldAcquireValueAfterInsert) {
// PERF: Unit of work always assigns sequence, so only need to check it here for non unit of work/change set query.
if (getWriteObjectQuery().getObjectChangeSet() == null) {
// This is the normal case. Update object with sequence number before insert.
updateObjectAndRowWithSequenceNumber();
}
}
if (hasMultipleCalls()) {
int size = this.calls.size();
for (int index = 0; index < size; index++) {
DatasourceCall databseCall = (DatasourceCall)this.calls.get(index);
if ((index > 0) && isExpressionQueryMechanism()
&& this.query.shouldCascadeOnlyDependentParts() && !descriptor.hasMultipleTableConstraintDependecy()
&& this.query.getSession().getProject().allowSQLDeferral()) {
DatabaseTable table = descriptor.getMultipleTableInsertOrder().get(index);
this.query.getSession().getCommitManager().addDeferredCall(table, databseCall, this);
} else {
Object result = executeCall(databseCall);
// Set the return row if one was returned (Postgres).
if (result instanceof AbstractRecord) {
this.query.setProperty("output", result);
}
if (returnFields != null) {
updateObjectAndRowWithReturnRow(returnFields, index == 0);
}
if ((index == 0) && usesSequencing && shouldAcquireValueAfterInsert) {
if(result instanceof DatabaseCall) {
updateObjectAndRowWithSequenceNumber((DatabaseCall) result);
} else {
updateObjectAndRowWithSequenceNumber();
}
}
}
}
} else {
Object result = executeCall();
// Set the return row if one was returned (Postgres).
if (result instanceof AbstractRecord) {
this.query.setProperty("output", result);
}
if (returnFields != null) {
updateObjectAndRowWithReturnRow(returnFields, true);
}
if (usesSequencing && shouldAcquireValueAfterInsert) {
if(result instanceof DatabaseCall) {
updateObjectAndRowWithSequenceNumber((DatabaseCall) result);
} else {
updateObjectAndRowWithSequenceNumber();
}
}
}
// Bug 3110860: RETURNINGPOLICY-OBTAINED PK CAUSES LOB TO BE INSERTED INCORRECTLY
// The deferred locator SELECT calls should be generated and executed after ReturningPolicy
// merges PK obtained from the db into the object held by the query.
//
//Oracle thin driver handles LOB differently. During the insert, empty lob would be
//insert first, and then the LOb locator is retrieved and LOB data are written through
//the locator.
//
// Bug 2804663 - LOBValueWriter is no longer a singleton, so we execute any deferred
// select calls through the DatabaseAccessor which holds the writer instance
AbstractSession executionSession = this.query.getExecutionSession();
for (Accessor accessor : executionSession.getAccessors()) {
accessor.flushSelectCalls(executionSession);
}
}
/**
* Execute the call that was deferred to the commit manager.
* This is used to allow multiple table batching and deadlock avoidance.
*/
public void executeDeferredCall(DatasourceCall call) {
Object result = executeCall(call);
// Set the return row if one was returned (Postgres).
if (result instanceof AbstractRecord) {
this.query.setProperty("output", result);
}
Collection returnFields = null;
if (this.query.getDescriptor().hasReturningPolicy()) {
returnFields = this.query.getDescriptor().getReturningPolicy().getFieldsToMergeInsert();
}
if (returnFields != null) {
updateObjectAndRowWithReturnRow(returnFields, false);
}
}
/**
* Return true if this is a call query mechanism
*/
public boolean isCallQueryMechanism() {
return true;
}
/**
* INTERNAL:
* This is different from 'prepareForExecution' in that this is called on the original query,
* and the other is called on the copy of the query.
* This query is copied for concurrency so this prepare can only setup things that
* will apply to any future execution of this query.
*/
public void prepare() {
if ((!hasMultipleCalls()) && (getCall() == null)) {
throw QueryException.sqlStatementNotSetProperly(getQuery());
}
}
/**
* INTERNAL:
* This is different from 'prepareForExecution' in that this is called on the original query,
* and the other is called on the copy of the query.
* This query is copied for concurrency so this prepare can only setup things that
* will apply to any future execution of this query.
*/
public void prepareCall() throws QueryException {
DatabaseQuery query = getQuery();
AbstractSession executionSession = query.getExecutionSession();
if (hasMultipleCalls()) {
for (DatasourceCall call : (List<DatasourceCall>)getCalls()) {
call.prepare(executionSession);
}
} else if (getCall() != null) {
getCall().prepare(executionSession);
}
}
/**
* Pre-build configure the call.
*/
public void prepareCursorSelectAllRows() throws QueryException {
getCall().returnCursor();
prepareCall();
}
/**
* Pre-build configure the call.
*/
public void prepareDeleteAll() {
if (hasMultipleCalls()) {
for (Enumeration callsEnum = getCalls().elements(); callsEnum.hasMoreElements();) {
DatasourceCall call = (DatasourceCall)callsEnum.nextElement();
call.returnNothing();
}
} else {
getCall().returnNothing();
}
prepareCall();
}
/**
* Pre-build configure the call.
*/
public void prepareDeleteObject() {
if (hasMultipleCalls()) {
for (Enumeration callsEnum = getCalls().elements(); callsEnum.hasMoreElements();) {
DatasourceCall call = (DatasourceCall)callsEnum.nextElement();
call.returnNothing();
}
} else {
getCall().returnNothing();
}
prepareCall();
}
/**
* Pre-build configure the call.
*/
public void prepareDoesExist(DatabaseField field) {
if (hasMultipleCalls()) {
for (Enumeration callsEnum = getCalls().elements(); callsEnum.hasMoreElements();) {
((DatasourceCall)callsEnum.nextElement()).returnOneRow();
}
} else {
getCall().returnOneRow();
}
prepareCall();
}
/**
* Pre-build configure the call.
*/
public void prepareExecuteNoSelect() {
if (hasMultipleCalls()) {
for (Enumeration callsEnum = getCalls().elements(); callsEnum.hasMoreElements();) {
((DatasourceCall)callsEnum.nextElement()).returnNothing();
}
} else {
getCall().returnNothing();
}
prepareCall();
}
/**
* Pre-build configure the call. This method assumes the query was built
* using a stored procedure query which is a single call.
*
* The return type on the call will already be set and
*/
public void prepareExecute() {
getCall().setExecuteUpdate();
prepareCall();
}
/**
* Pre-build configure the call.
*/
public void prepareExecuteSelect() {
if (hasMultipleCalls()) {
for (Enumeration callsEnum = getCalls().elements(); callsEnum.hasMoreElements();) {
DatasourceCall databseCall = (DatasourceCall)callsEnum.nextElement();
databseCall.returnManyRows();
}
} else {
getCall().returnManyRows();
}
prepareCall();
}
/**
* Pre-build configure the call.
*/
public void prepareInsertObject() {
if (hasMultipleCalls()) {
int size = this.calls.size();
for (int index = 0; index < size; index++) {
DatabaseCall call = (DatabaseCall)this.calls.get(index);
if (!call.isReturnSet()) {
call.returnNothing();
}
}
} else {
if (!this.call.isReturnSet()) {
this.call.returnNothing();
}
}
prepareCall();
}
/**
* Prepare the report items.
* Indexes of results need to be calculated.
*/
protected void prepareReportQueryItems(){
//calculate indexes after normalize to insure expressions are set up correctly
//take into account any field expressions added to the ReportQuery
ReportQuery query = (ReportQuery)getQuery();
computeAndSetItemOffset(query, query.getItems(), query.getQueryExpressions().size());
}
/**
* calculate indexes for given items, given the current Offset
*/
protected int computeAndSetItemOffset(ReportQuery query, List<ReportItem> items, int itemOffset) {
for(ReportItem item : items) {
if (item.isConstructorItem()) {
List<ReportItem> reportItems = ((ConstructorReportItem) item).getReportItems();
itemOffset = computeAndSetItemOffset(query, reportItems, itemOffset);
} else {
//Don't set the offset on the ConstructorItem
item.setResultIndex(itemOffset);
if (item.getAttributeExpression() != null) {
if (item.hasJoining()){
itemOffset = item.getJoinedAttributeManager().computeJoiningMappingIndexes(true, getSession(), itemOffset);
} else {
if (item.getDescriptor() != null) {
itemOffset += item.getDescriptor().getAllSelectionFields(query).size();
} else {
if (item.getMapping() != null && item.getMapping().isAggregateObjectMapping()) {
itemOffset += item.getMapping().getFields().size(); // Aggregate object may consist out of 1..n fields
} else {
++itemOffset; //only a single attribute can be selected
}
}
}
}
}
}
return itemOffset;
}
/**
* Pre-build configure the call.
*/
public void prepareReportQuerySelectAllRows() {
prepareReportQueryItems();
prepareExecuteSelect();
}
/**
* Prepare for a sub select using a call.
*/
public void prepareReportQuerySubSelect() {
prepareReportQueryItems();
prepareCall();
}
/**
* Pre-build configure the call.
*/
public void prepareSelectAllRows() {
if (hasMultipleCalls()) {
for (Enumeration callsEnum = getCalls().elements(); callsEnum.hasMoreElements();) {
DatasourceCall databseCall = (DatasourceCall)callsEnum.nextElement();
databseCall.returnManyRows();
}
} else {
getCall().returnManyRows();
}
prepareCall();
}
/**
* Pre-build configure the call.
*/
public void prepareSelectOneRow() {
if (hasMultipleCalls()) {
for (Enumeration callsEnum = getCalls().elements(); callsEnum.hasMoreElements();) {
DatasourceCall databseCall = (DatasourceCall)callsEnum.nextElement();
databseCall.returnOneRow();
}
} else {
getCall().returnOneRow();
}
prepareCall();
}
/**
* Pre-build configure the call.
*/
public void prepareUpdateObject() {
if (hasMultipleCalls()) {
int size = this.calls.size();
for (int index = 0; index < size; index++) {
DatabaseCall call = (DatabaseCall)this.calls.get(index);
if (!call.isReturnSet()) {
call.returnNothing();
}
}
} else if (getCall() != null) {
if (!call.isReturnSet()) {
this.call.returnNothing();
}
}
prepareCall();
}
/**
* Pre-build configure the call.
*/
public void prepareUpdateAll() {
if (getCall() != null) {
getCall().returnNothing();
}
prepareCall();
}
/**
* Read all rows from the database. Assume call is correct returns the required fields.
* @return Vector containing the database rows
* @exception DatabaseException - an error has occurred on the database
*/
public Vector selectAllReportQueryRows() throws DatabaseException {
return executeSelect();
}
/**
* Read all rows from the database. Assume call is correct returns the required fields.
* @return Vector containing the database rows
* @exception DatabaseException - an error has occurred on the database
*/
public Vector selectAllRows() throws DatabaseException {
return executeSelectCall();
}
/**
* Read a single row from the database. Assume call is correct.
* @return row containing data
* @exception DatabaseException - an error has occurred on the database
*/
public AbstractRecord selectOneRow() throws DatabaseException {
if (hasMultipleCalls()) {
for (Enumeration callsEnum = getCalls().elements(); callsEnum.hasMoreElements();) {
DatasourceCall databaseCall = (DatasourceCall)callsEnum.nextElement();
AbstractRecord result = (AbstractRecord)executeCall(databaseCall);
if (result != null) {
return result;
}
}
return null;
} else {
return (AbstractRecord)executeCall();
}
}
/**
* Perform a does exist check
* @param field - the field used for does exist check
* @return the associated row from the database
* @exception DatabaseException - an error has occurred on the database
*/
public AbstractRecord selectRowForDoesExist(DatabaseField field) throws DatabaseException {
if (hasMultipleCalls()) {
for (Enumeration callsEnum = getCalls().elements(); callsEnum.hasMoreElements();) {
DatasourceCall databaseCall = (DatasourceCall)callsEnum.nextElement();
AbstractRecord result = (AbstractRecord)executeCall(databaseCall);
if (result != null) {
return result;
}
}
return null;
} else {
return (AbstractRecord)executeCall();
}
}
/**
* Set the call.
*/
public void setCall(DatasourceCall call) {
this.call = call;
if (call != null) {
call.setQuery(getQuery());
}
}
/**
* Normally only a single call is used, however multiple table may require multiple calls on write.
* This is lazy initialised to conserve space.
*/
protected void setCalls(Vector calls) {
this.calls = calls;
}
/**
* Update the object. Assume the call is correct.
* @exception DatabaseException - an error has occurred on the database.
* @return the row count.
*/
public Integer updateObject() throws DatabaseException {
ClassDescriptor descriptor = getDescriptor();
Collection returnFields = null;
if (descriptor.getReturnFieldsToMergeUpdate() != null) {
returnFields = descriptor.getReturnFieldsToMergeUpdate();
}
Integer returnedRowCount = null;
if (hasMultipleCalls()) {
int size = this.calls.size();
for (int index = 0; index < size; index++) {
DatasourceCall databseCall = (DatasourceCall)this.calls.get(index);
if ((index > 0) && isExpressionQueryMechanism()
&& this.query.shouldCascadeOnlyDependentParts() && !descriptor.hasMultipleTableConstraintDependecy()
&& this.query.getSession().getProject().allowSQLDeferral()) {
DatabaseTable table = descriptor.getMultipleTableInsertOrder().get(index);
this.query.getSession().getCommitManager().addDeferredCall(table, databseCall, this);
} else {
Object result = executeCall(databseCall);
// Set the return row if one was returned (Postgres).
Integer rowCount;
if (result instanceof AbstractRecord) {
this.query.setProperty("output", result);
rowCount = Integer.valueOf(1);
} else {
rowCount = (Integer)result;
}
if ((index == 0) || (rowCount.intValue() <= 0)) {// Row count returned must be from first table or zero if any are zero.
returnedRowCount = rowCount;
}
if (returnFields != null) {
updateObjectAndRowWithReturnRow(returnFields, false);
}
}
}
} else {
Object result = executeCall();
// Set the return row if one was returned (Postgres).
if (result instanceof AbstractRecord) {
this.query.setProperty("output", result);
returnedRowCount = Integer.valueOf(1);
} else {
returnedRowCount = (Integer)result;
}
if (returnFields != null) {
updateObjectAndRowWithReturnRow(returnFields, false);
}
}
//Oracle thin driver handles LOB differently. During the insert, empty lob would be
//insert first, and then the LOb locator is retrieved and LOB data are written through
//the locator.
//
// Bug 2804663 - LOBValueWriter is no longer a singleton, so we execute any deferred
// select calls through the DatabaseAccessor which holds the writer instance
//
// Building of SELECT statements is no longer done in DatabaseAccessor.basicExecuteCall
// because DatabaseCall.isUpdateCall() can't recognize update in case StoredProcedureCall
// is used.
AbstractSession executionSession = this.query.getExecutionSession();
for (Accessor accessor : executionSession.getAccessors()) {
accessor.flushSelectCalls(executionSession);
}
return returnedRowCount;
}
/**
* Update the rows on the database. Assume the call is correct.
* @exception DatabaseException - an error has occurred on the database.
*/
public Integer updateAll() throws DatabaseException {
if(((UpdateAllQuery)this.query).isPreparedUsingTempStorage() && getExecutionSession().getPlatform().supportsTempTables()) {
return updateAllUsingTempTables();
} else {
Integer rowCount = executeNoSelectCall();
if(((UpdateAllQuery)this.query).isPreparedUsingTempStorage()) {
// the query was prepared using Oracle anonymous block
AbstractRecord outputRow = (AbstractRecord)this.query.getProperty("output");
rowCount = (Integer)outputRow.get("ROW_COUNT");
}
return rowCount;
}
}
/**
* Execute updateAll using temp tables
* @exception DatabaseException - an error has occurred on the database.
* @return the row count.
*/
public Integer updateAllUsingTempTables() throws DatabaseException {
int nTables = getCalls().size() / 4;
DatabaseException ex = null;
Integer returnedRowCount = null;
// first quarter - crete temp tables calls.
// may fail in case global temp table already exists.
for (int index = 0; index < nTables; index++) {
try {
DatasourceCall databseCall = (DatasourceCall)getCalls().elementAt(index);
executeCall(databseCall);
} catch (DatabaseException databaseEx) {
// ignore
}
}
// second quarter - populate temp tables calls.
// if that fails save the exception and until cleanup
for (int index = nTables; index < nTables*2 && ex == null; index++) {
try {
DatasourceCall databseCall = (DatasourceCall)getCalls().elementAt(index);
executeCall(databseCall);
} catch (DatabaseException databaseEx) {
ex = databaseEx;
}
}
// third quarter - update original tables calls.
// if that fails save the exception and until cleanup
for (int index = nTables*2; index < nTables*3 && ex == null; index++) {
try {
DatasourceCall databseCall = (DatasourceCall)getCalls().elementAt(index);
Integer rowCount = (Integer)executeCall(databseCall);
if ((index == nTables*2) || (rowCount.intValue() <= 0)) {// Row count returned must be from first table or zero if any are zero.
returnedRowCount = rowCount;
}
} catch (DatabaseException databaseEx) {
ex = databaseEx;
}
}
// last quarter - cleanup temp tables calls.
// ignore exceptions here.
for (int index = nTables*3; index < nTables*4; index++) {
try {
DatasourceCall databseCall = (DatasourceCall)getCalls().elementAt(index);
executeCall(databseCall);
} catch (DatabaseException databaseEx) {
// ignore
}
}
if(ex != null) {
throw ex;
}
return returnedRowCount;
}
/**
* Update the foreign key fields when resolving a bi-directional reference in a UOW.
* This is rare to occur for non-relational, however if it does each of the calls must be re-executed.
*/
protected void updateForeignKeyFieldAfterInsert(WriteObjectQuery writeQuery) {
writeQuery.setModifyRow(this.getDescriptor().getObjectBuilder().buildRow(writeQuery.getObject(), this.getSession(), WriteType.INSERT));
// For CR 2923 must move to session we will execute call on now
// so correct DatasourcePlatform used by translate.
AbstractSession sessionToUse = this.query.getExecutionSession();
// yes - this is a bit ugly...
Vector calls = ((DatasourceCallQueryMechanism)this.getDescriptor().getQueryManager().getUpdateQuery().getQueryMechanism()).getCalls();
for (Enumeration stream = calls.elements(); stream.hasMoreElements();) {
DatasourceCall call = (DatasourceCall)((DatasourceCall)stream.nextElement()).clone();
call.setQuery(writeQuery);
sessionToUse.executeCall(call, this.getTranslationRow(), writeQuery);
}
}
}
|