Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 1998, 2021 Oracle and/or its affiliates. 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
// 04/01/2011-2.3 Guy Pelletier
// - 337323: Multi-tenant with shared schema support (part 2)
// 09/09/2011-2.3.1 Guy Pelletier
// - 356197: Add new VPD type to MultitenantType
package org.eclipse.persistence.queries;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import org.eclipse.persistence.exceptions.DatabaseException;
import org.eclipse.persistence.exceptions.QueryException;
import org.eclipse.persistence.expressions.Expression;
import org.eclipse.persistence.expressions.ExpressionBuilder;
import org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor;
import org.eclipse.persistence.internal.databaseaccess.DatabaseCall;
import org.eclipse.persistence.internal.databaseaccess.DatabasePlatform;
import org.eclipse.persistence.internal.descriptors.ObjectBuilder;
import org.eclipse.persistence.internal.expressions.QueryKeyExpression;
import org.eclipse.persistence.internal.helper.ClassConstants;
import org.eclipse.persistence.internal.helper.DatabaseField;
import org.eclipse.persistence.internal.helper.InvalidObject;
import org.eclipse.persistence.internal.helper.ThreadCursoredList;
import org.eclipse.persistence.internal.queries.ContainerPolicy;
import org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism;
import org.eclipse.persistence.internal.sessions.AbstractRecord;
import org.eclipse.persistence.internal.sessions.AbstractSession;
import org.eclipse.persistence.internal.sessions.ResultSetRecord;
import org.eclipse.persistence.internal.sessions.SimpleResultSetRecord;
import org.eclipse.persistence.internal.sessions.UnitOfWorkImpl;
import org.eclipse.persistence.internal.sessions.remote.RemoteSessionController;
import org.eclipse.persistence.internal.sessions.remote.Transporter;
import org.eclipse.persistence.mappings.DatabaseMapping;
import org.eclipse.persistence.mappings.OneToManyMapping;
import org.eclipse.persistence.sessions.DatabaseRecord;
import org.eclipse.persistence.sessions.SessionProfiler;
import org.eclipse.persistence.sessions.remote.DistributedSession;
import org.eclipse.persistence.tools.profiler.QueryMonitor;
/**
*
Purpose:
* Concrete class for all read queries involving a collection of objects.
*
*
Responsibilities:
* Return a container of the objects generated by the query.
* Implements the inheritance feature when dealing with abstract descriptors
*
* @author Yvon Lavoie
* @since TOPLink/Java 1.0
*/
public class ReadAllQuery extends ObjectLevelReadQuery {
/** Used for collection and stream support. */
protected ContainerPolicy containerPolicy;
/** Used for Oracle HierarchicalQuery support */
protected Expression startWithExpression;
protected Expression connectByExpression;
protected List orderSiblingsByExpressions;
protected Direction direction;
/**
* Specifies the direction in which the hierarchy is traversed in a
* hierarchical query.
*/
public enum Direction {
/**
* Hierarchy will be traversed from parent to child - PRIOR keyword is
* generated on the left side of the equation
*/
PARENT_TO_CHILD,
/**
* Hierarchy will be traversed from child to parent - PRIOR keyword is
* generated on the right side of the equation
*/
CHILD_TO_PARENT;
/**
* PUBLIC: Returns the default hierarchy traversal direction for the
* specified mapping.
* For OneToOne mappings, source in parent object goes to target in
* child object, collections are the opposite way.
*
* @param mapping
* The mapping for which to return default hierarchy
* traversal direction
* @return The default hierarchy traversal direction for the mapping
* passed
*/
public static Direction getDefault(DatabaseMapping mapping) {
return mapping != null && mapping.isOneToOneMapping() ? CHILD_TO_PARENT : PARENT_TO_CHILD;
}
}
/**
* PUBLIC:
* Return a new read all query.
* A reference class must be specified before execution.
* It is better to provide the class and expression builder on construction to ensure a single expression builder is used.
* If no selection criteria is specified this will read all objects of the class from the database.
*/
public ReadAllQuery() {
super();
setContainerPolicy(ContainerPolicy.buildDefaultPolicy());
}
/**
* PUBLIC:
* Return a new read all query.
* It is better to provide the class and expression builder on construction to ensure a single expression builder is used.
* If no selection criteria is specified this will read all objects of the class from the database.
*/
public ReadAllQuery(Class> classToRead) {
this();
this.referenceClass = classToRead;
}
/**
* PUBLIC:
* Return a new read all query for the class and the selection criteria.
*/
public ReadAllQuery(Class> classToRead, Expression selectionCriteria) {
this();
this.referenceClass = classToRead;
setSelectionCriteria(selectionCriteria);
}
/**
* PUBLIC:
* Return a new read all query for the class.
* The expression builder must be used for all associated expressions used with the query.
*/
public ReadAllQuery(Class> classToRead, ExpressionBuilder builder) {
this();
this.defaultBuilder = builder;
this.referenceClass = classToRead;
}
/**
* PUBLIC:
* Return a new read all query.
* The call represents a database interaction such as SQL, Stored Procedure.
*/
public ReadAllQuery(Class> classToRead, Call call) {
this();
this.referenceClass = classToRead;
setCall(call);
}
/**
* PUBLIC:
* Return a query by example query to find all objects matching the attributes of the example object.
*/
public ReadAllQuery(Object exampleObject, QueryByExamplePolicy policy) {
this();
setExampleObject(exampleObject);
setQueryByExamplePolicy(policy);
}
/**
* PUBLIC:
* The expression builder should be provide on creation to ensure only one is used.
*/
public ReadAllQuery(ExpressionBuilder builder) {
this();
this.defaultBuilder = builder;
}
/**
* PUBLIC:
* Create a read all query with the database call.
*/
public ReadAllQuery(Call call) {
this();
setCall(call);
}
/**
* PUBLIC:
* Order the query results by the object's attribute or query key name.
*/
public void addAscendingOrdering(String queryKeyName) {
addOrdering(getExpressionBuilder().get(queryKeyName).ascending());
}
/**
* INTERNAL:
*
This method is called by the object builder when building an original.
* It will cause the original to be cached in the query results if the query
* is set to do so.
*/
@Override
public void cacheResult(Object unwrappedOriginal) {
Collection container = (Collection)getTemporaryCachedQueryResults();
if (container == null) {
container = (Collection)getContainerPolicy().containerInstance();
setTemporaryCachedQueryResults(container);
}
getContainerPolicy().addInto(unwrappedOriginal, container, getSession());
}
/**
* INTERNAL:
* The cache check is done before the prepare as a hit will not require the work to be done.
*/
@Override
protected Object checkEarlyReturnLocal(AbstractSession session, AbstractRecord translationRow) {
// Check for in-memory only query.
if (shouldCheckCacheOnly()) {
// assert !isReportQuery();
if (shouldUseWrapperPolicy()) {
getContainerPolicy().setElementDescriptor(this.descriptor);
}
// PERF: Fixed to not query each unit of work cache (is not conforming),
// avoid hashtable and primary key indexing.
// At some point we may need to support some kind of in-memory with conforming option,
// but we do not currently allow this.
AbstractSession rootSession = session;
while (rootSession.isUnitOfWork()) {
rootSession = rootSession.getParent();
}
Vector allCachedVector = rootSession.getIdentityMapAccessor().getAllFromIdentityMap(getSelectionCriteria(), getReferenceClass(), translationRow, getInMemoryQueryIndirectionPolicyState(), false);
// Must ensure that all of the objects returned are correctly registered in the unit of work.
if (session.isUnitOfWork()) {
allCachedVector = ((UnitOfWorkImpl)session).registerAllObjects(allCachedVector);
}
this.isCacheCheckComplete = true;
return getContainerPolicy().buildContainerFromVector(allCachedVector, session);
} else {
return null;
}
}
/**
* INTERNAL:
* Check and return custom query flag. Custom query flag value is initialized when stored value is {@code null}.
* Called from {@link #checkForCustomQuery(AbstractSession, AbstractRecord)} to retrieve custom query flag.
* @param session Current session (not used).
* @param translationRow Database record (not used).
* @return Current custom query flag. Value will never be {@code null}.
*/
@Override
protected Boolean checkCustomQueryFlag(final AbstractSession session, final AbstractRecord translationRow) {
// #436871 - Use local copy to avoid NPE from concurrent modification.
final Boolean useCustomQuery = isCustomQueryUsed;
if (useCustomQuery != null) {
return useCustomQuery;
// Initialize custom query flag.
} else {
final boolean useCustomQueryValue =
!isUserDefined() && isExpressionQuery() && getSelectionCriteria() == null
&& isDefaultPropertiesQuery() && (!hasOrderByExpressions())
&& descriptor.getQueryManager().hasReadAllQuery();
setIsCustomQueryUsed(useCustomQueryValue);
return useCustomQueryValue;
}
}
/**
* INTERNAL:
* Get custom all read query from query manager.
* Called from {@link #checkForCustomQuery(AbstractSession, AbstractRecord)} to retrieve custom read query.
* @return Custom all read query from query manager.
*/
@Override
protected ObjectLevelReadQuery getReadQuery() {
return descriptor.getQueryManager().getReadAllQuery();
}
/**
* INTERNAL:
* Creates and returns a copy of this query.
* @return A clone of this instance.
*/
@Override
public Object clone() {
final ReadAllQuery cloneQuery = (ReadAllQuery)super.clone();
// Don't use setters as that will trigger unprepare.
cloneQuery.containerPolicy = containerPolicy.clone(cloneQuery);
return cloneQuery;
}
/**
* INTERNAL:
* Conform the result if specified.
*/
protected Object conformResult(Object result, UnitOfWorkImpl unitOfWork, AbstractRecord arguments, boolean buildDirectlyFromRows) {
Expression selectionCriteria = getSelectionCriteria();
if (selectionCriteria != null) {
ExpressionBuilder builder = getSelectionCriteria().getBuilder();
builder.setSession(unitOfWork.getRootSession(null));
builder.setQueryClass(getReferenceClass());
if (getQueryMechanism().isExpressionQueryMechanism() && selectionCriteria.isLogicalExpression()) {
// bug #526546
if (builder.derivedExpressions != null) {
for (Expression e : builder.derivedExpressions) {
if (e.isQueryKeyExpression() && ((QueryKeyExpression) e).shouldQueryToManyRelationship()) {
DatabaseMapping mapping = ((QueryKeyExpression) e).getMapping();
if (mapping.isOneToManyMapping()) {
OneToManyMapping otm = (OneToManyMapping) mapping;
Expression join = otm.buildSelectionCriteria();
selectionCriteria = selectionCriteria.and(join);
}
}
}
}
}
}
// If the query is redirected then the collection returned might no longer
// correspond to the original container policy. CR#2342-S.M.
ContainerPolicy cp;
if (getRedirector() != null) {
cp = ContainerPolicy.buildPolicyFor(result.getClass());
} else {
cp = getContainerPolicy();
}
// This code is now a great deal different... For one, registration is done
// as part of conforming. Also, this should only be called if one actually
// is conforming.
// First scan the UnitOfWork for conforming instances.
// This will walk through the entire cache of registered objects.
// Let p be objects from result not in the cache.
// Let c be objects from cache.
// Presently p intersect c = empty set, but later p subset c.
// By checking cache now doesConform will be called p fewer times.
Map