Search in sources :

Example 31 with DatabaseQuery

use of org.eclipse.persistence.queries.DatabaseQuery in project eclipselink by eclipse-ee4j.

the class DescriptorQueryManager method getQuery.

/**
 * PUBLIC:
 * Return the query from the set of pre-defined queries with the given name and argument types.
 * This allows for common queries to be pre-defined, reused and executed by name.
 * This method should be used if the Session has multiple queries with the same name but
 * different arguments.
 * If only one query exists, it will be returned regardless of the arguments.
 * If multiple queries exist, the first query that has corresponding argument types will be returned
 *
 * @see #getQuery(String)
 */
public DatabaseQuery getQuery(String name, Vector arguments) {
    DatabaseQuery query = getLocalQuery(name, arguments);
    // found in parents, return null.
    if (query == null) {
        DatabaseQuery parentQuery = getQueryFromParent(name, arguments);
        if ((parentQuery != null) && parentQuery.isReadQuery()) {
            parentQuery = (DatabaseQuery) parentQuery.clone();
            ((ObjectLevelReadQuery) parentQuery).setReferenceClass(this.descriptor.getJavaClass());
            addQuery(name, parentQuery);
        }
        return parentQuery;
    }
    return query;
}
Also used : ObjectLevelReadQuery(org.eclipse.persistence.queries.ObjectLevelReadQuery) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery)

Example 32 with DatabaseQuery

use of org.eclipse.persistence.queries.DatabaseQuery in project eclipselink by eclipse-ee4j.

the class DescriptorQueryManager method clone.

/**
 * INTERNAL:
 * Clone the query manager
 */
@Override
public Object clone() {
    DescriptorQueryManager manager = null;
    try {
        manager = (DescriptorQueryManager) super.clone();
    } catch (Exception exception) {
        throw new AssertionError(exception);
    }
    // Bug 3037701 - clone the queries
    // bug5677655
    manager.setQueries(new LinkedHashMap<>(getQueries().size()));
    Iterator<List<DatabaseQuery>> iterator = queries.values().iterator();
    while (iterator.hasNext()) {
        Iterator<DatabaseQuery> queriesForKey = iterator.next().iterator();
        while (queriesForKey.hasNext()) {
            DatabaseQuery initialQuery = queriesForKey.next();
            DatabaseQuery clonedQuery = (DatabaseQuery) initialQuery.clone();
            clonedQuery.setDescriptor(manager.getDescriptor());
            manager.addQuery(clonedQuery);
        }
    }
    manager.setDoesExistQuery((DoesExistQuery) getDoesExistQuery().clone());
    if (getReadAllQuery() != null) {
        manager.setReadAllQuery((ReadAllQuery) getReadAllQuery().clone());
    }
    if (getReadObjectQuery() != null) {
        manager.setReadObjectQuery((ReadObjectQuery) getReadObjectQuery().clone());
    }
    if (getUpdateQuery() != null) {
        manager.setUpdateQuery((UpdateObjectQuery) getUpdateQuery().clone());
    }
    if (getInsertQuery() != null) {
        manager.setInsertQuery((InsertObjectQuery) getInsertQuery().clone());
    }
    if (getDeleteQuery() != null) {
        manager.setDeleteQuery((DeleteObjectQuery) getDeleteQuery().clone());
    }
    return manager;
}
Also used : DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) ArrayList(java.util.ArrayList) List(java.util.List) DescriptorException(org.eclipse.persistence.exceptions.DescriptorException) ConversionException(org.eclipse.persistence.exceptions.ConversionException)

Example 33 with DatabaseQuery

use of org.eclipse.persistence.queries.DatabaseQuery in project eclipselink by eclipse-ee4j.

the class RemoteSessionController method executeNamedQuery.

/**
 * A named query after serialization is executed locally.
 */
public Transporter executeNamedQuery(Transporter nameTransporter, Transporter classTransporter, Transporter argumentsTransporter) {
    Transporter transporter = new Transporter();
    try {
        Object result;
        DatabaseQuery query;
        // Clear the unit of work, as the client unit of work may have been cleared.
        this.unitOfWork = null;
        AbstractSession executionSession = getExecutionSession();
        if (classTransporter.getObject() == null) {
            result = executionSession.executeQuery((String) nameTransporter.getObject(), (Vector) argumentsTransporter.getObject());
            query = executionSession.getQuery((String) nameTransporter.getObject());
        } else {
            result = executionSession.executeQuery((String) nameTransporter.getObject(), (Class) classTransporter.getObject(), (Vector) argumentsTransporter.getObject());
            query = executionSession.getDescriptor((Class) classTransporter.getObject()).getQueryManager().getQuery((String) nameTransporter.getObject());
        }
        transporter.setQuery(query);
        transporter.setObjectDescriptors(query.replaceValueHoldersIn(result, this));
        transporter.setObject(result);
    } catch (RuntimeException exception) {
        transporter.setException(exception);
    }
    transporter.prepare(this.session);
    return transporter;
}
Also used : DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) Vector(java.util.Vector) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 34 with DatabaseQuery

use of org.eclipse.persistence.queries.DatabaseQuery in project eclipselink by eclipse-ee4j.

the class DescriptorQueryManager method getLocalQuery.

/**
 * INTENAL:
 * Return the query from the set of pre-defined queries with the given name and argument types.
 * This allows for common queries to be pre-defined, reused and executed by name.
 * Only returns those queries locally defined, not superclass's queries
 * If only one query exists, it will be returned regardless of the arguments.
 * If multiple queries exist, the first query that has corresponding argument types will be returned
 *
 * @see #getQuery(String)
 */
public DatabaseQuery getLocalQuery(String name, Vector arguments) {
    List<DatabaseQuery> queries = getQueries().get(name);
    if (queries == null) {
        return null;
    }
    // Short circuit the simple, most common case of only one query.
    if (queries.size() == 1) {
        return queries.get(0);
    }
    // CR#3754; Predrag; mar 19/2002;
    // We allow multiple named queries with the same name but
    // different argument set; we can have only one query with
    // no arguments; Vector queries is not sorted;
    // When asked for the query with no parameters the
    // old version did return the first query - wrong:
    // return (DatabaseQuery) queries.firstElement();
    int argumentTypesSize = 0;
    if (arguments != null) {
        argumentTypesSize = arguments.size();
    }
    Vector argumentTypes = org.eclipse.persistence.internal.helper.NonSynchronizedVector.newInstance(argumentTypesSize);
    for (int i = 0; i < argumentTypesSize; i++) {
        argumentTypes.addElement(arguments.elementAt(i).getClass());
    }
    return getLocalQueryByArgumentTypes(name, argumentTypes);
}
Also used : DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) Vector(java.util.Vector) NonSynchronizedVector(org.eclipse.persistence.internal.helper.NonSynchronizedVector)

Example 35 with DatabaseQuery

use of org.eclipse.persistence.queries.DatabaseQuery in project eclipselink by eclipse-ee4j.

the class DescriptorQueryManager method initialize.

/**
 * INTERNAL:
 * Post initialize the mappings
 */
public void initialize(AbstractSession session) {
    this.initializeQueryTimeout(session);
    if (getDescriptor().isAggregateDescriptor()) {
        return;
    }
    if (getMultipleTableJoinExpression() != null) {
        // Combine new multiple table expression to additional join expression
        setAdditionalJoinExpression(getMultipleTableJoinExpression().and(getAdditionalJoinExpression()));
    }
    if (getDescriptor().isAggregateCollectionDescriptor()) {
        return;
    }
    // Configure default query cache for all named queries.
    QueryResultsCachePolicy defaultQueryCachePolicy = session.getProject().getDefaultQueryResultsCachePolicy();
    if (defaultQueryCachePolicy != null && !getDescriptor().getCachePolicy().isIsolated()) {
        for (List<DatabaseQuery> queries : getQueries().values()) {
            for (DatabaseQuery query : queries) {
                if (query.isReadQuery()) {
                    ReadQuery readQuery = (ReadQuery) query;
                    if (!readQuery.shouldCacheQueryResults()) {
                        readQuery.setQueryResultsCachePolicy(defaultQueryCachePolicy.clone());
                    }
                }
            }
        }
    }
    getDescriptor().initialize(this, session);
}
Also used : QueryResultsCachePolicy(org.eclipse.persistence.queries.QueryResultsCachePolicy) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) ObjectLevelReadQuery(org.eclipse.persistence.queries.ObjectLevelReadQuery) ReadQuery(org.eclipse.persistence.queries.ReadQuery)

Aggregations

DatabaseQuery (org.eclipse.persistence.queries.DatabaseQuery)86 ArrayList (java.util.ArrayList)18 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)15 List (java.util.List)14 Vector (java.util.Vector)12 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)12 ObjectLevelReadQuery (org.eclipse.persistence.queries.ObjectLevelReadQuery)12 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)8 EntityManager (jakarta.persistence.EntityManager)6 HashMap (java.util.HashMap)6 QName (javax.xml.namespace.QName)6 EJBQueryImpl (org.eclipse.persistence.internal.jpa.EJBQueryImpl)6 PersistenceContext (org.eclipse.persistence.jpa.rs.PersistenceContext)6 DataReadQuery (org.eclipse.persistence.queries.DataReadQuery)6 Session (org.eclipse.persistence.sessions.Session)6 Test (org.junit.Test)6 NonSynchronizedVector (org.eclipse.persistence.internal.helper.NonSynchronizedVector)5 ReadQuery (org.eclipse.persistence.queries.ReadQuery)5 ReportQuery (org.eclipse.persistence.queries.ReportQuery)5 PersistenceException (jakarta.persistence.PersistenceException)4