Search in sources :

Example 1 with JPAQueryBuilder

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

the class DescriptorQueryManager method postInitialize.

/**
 * INTERNAL:
 * Post initializations after mappings are initialized.
 */
public void postInitialize(AbstractSession session) throws DescriptorException {
    // have been fully initialized.
    if (additionalCriteria != null) {
        if (getDescriptor().hasInheritance() && getDescriptor().getInheritancePolicy().hasView()) {
            throw DescriptorException.additionalCriteriaNotSupportedWithInheritanceViews(getDescriptor());
        }
        JPAQueryBuilder queryBuilder = session.getQueryBuilder();
        Expression selectionCriteria = queryBuilder.buildSelectionCriteria(getDescriptor().getAlias(), additionalCriteria, session);
        updatePropertyParameterExpression(selectionCriteria);
        additionalJoinExpression = selectionCriteria.and(additionalJoinExpression);
    }
    if (additionalJoinExpression != null) {
        // The make sure the additional join expression has the correct
        // context, rebuild the additional join expression on a new
        // expression builder.
        additionalJoinExpression = additionalJoinExpression.rebuildOn(new ExpressionBuilder());
    }
}
Also used : SubSelectExpression(org.eclipse.persistence.internal.expressions.SubSelectExpression) ParameterExpression(org.eclipse.persistence.internal.expressions.ParameterExpression) CompoundExpression(org.eclipse.persistence.internal.expressions.CompoundExpression) FunctionExpression(org.eclipse.persistence.internal.expressions.FunctionExpression) Expression(org.eclipse.persistence.expressions.Expression) JPAQueryBuilder(org.eclipse.persistence.queries.JPAQueryBuilder) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder)

Example 2 with JPAQueryBuilder

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

the class AbstractSession method buildDefaultQueryBuilder.

/**
 * INTERNAL
 * Build the JPQL builder based on session properties.
 */
protected JPAQueryBuilder buildDefaultQueryBuilder() {
    String queryBuilderClassName = (String) getProperty(PersistenceUnitProperties.JPQL_PARSER);
    if (queryBuilderClassName == null) {
        queryBuilderClassName = "org.eclipse.persistence.internal.jpa.jpql.HermesParser";
    // queryBuilderClassName = "org.eclipse.persistence.queries.ANTLRQueryBuilder";
    }
    String validation = (String) getProperty(PersistenceUnitProperties.JPQL_VALIDATION);
    JPAQueryBuilder builder = null;
    try {
        Class<? extends JPAQueryBuilder> parserClass = null;
        // Use Class.forName not thread class loader to avoid class loader issues.
        if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
            parserClass = AccessController.doPrivileged(new PrivilegedClassForName<>(queryBuilderClassName));
            builder = AccessController.doPrivileged(new PrivilegedNewInstanceFromClass<>(parserClass));
        } else {
            parserClass = PrivilegedAccessHelper.getClassForName(queryBuilderClassName);
            builder = PrivilegedAccessHelper.newInstanceFromClass(parserClass);
        }
    } catch (Exception e) {
        throw new IllegalStateException("Could not load the JPQL parser class.", /* TODO: Localize string */
        e);
    }
    if (validation != null) {
        builder.setValidationLevel(validation);
    }
    return builder;
}
Also used : PrivilegedNewInstanceFromClass(org.eclipse.persistence.internal.security.PrivilegedNewInstanceFromClass) JPAQueryBuilder(org.eclipse.persistence.queries.JPAQueryBuilder) DatabaseException(org.eclipse.persistence.exceptions.DatabaseException) IntegrityException(org.eclipse.persistence.exceptions.IntegrityException) ValidationException(org.eclipse.persistence.exceptions.ValidationException) EclipseLinkException(org.eclipse.persistence.exceptions.EclipseLinkException) OptimisticLockException(org.eclipse.persistence.exceptions.OptimisticLockException) QueryException(org.eclipse.persistence.exceptions.QueryException) ConcurrencyException(org.eclipse.persistence.exceptions.ConcurrencyException) PrivilegedClassForName(org.eclipse.persistence.internal.security.PrivilegedClassForName)

Example 3 with JPAQueryBuilder

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

the class EJBQueryImpl method buildEJBQLDatabaseQuery.

/**
 * Build a DatabaseQuery from an JPQL string.
 *
 * @param jpqlQuery
 *            the JPQL string.
 * @param session
 *            the session to get the descriptors for this query for.
 * @param hints
 *            a list of hints to be applied to the query.
 * @return a DatabaseQuery representing the given jpql.
 */
public static DatabaseQuery buildEJBQLDatabaseQuery(String queryName, String jpqlQuery, AbstractSession session, Enum lockMode, Map<String, Object> hints, ClassLoader classLoader) {
    // PERF: Check if the JPQL has already been parsed.
    // Only allow queries with default properties to be parse cached.
    boolean isCacheable = (queryName == null) && (hints == null);
    DatabaseQuery databaseQuery = null;
    if (isCacheable) {
        databaseQuery = (DatabaseQuery) session.getProject().getJPQLParseCache().get(jpqlQuery);
    }
    if ((databaseQuery == null) || (!databaseQuery.isPrepared())) {
        JPAQueryBuilder queryBuilder = session.getQueryBuilder();
        databaseQuery = queryBuilder.buildQuery(jpqlQuery, session);
        // filtering duplicates.
        if (databaseQuery.isReadAllQuery()) {
            ReadAllQuery readAllQuery = (ReadAllQuery) databaseQuery;
            if (readAllQuery.hasJoining() && (readAllQuery.getDistinctState() == ReadAllQuery.DONT_USE_DISTINCT)) {
                readAllQuery.setShouldFilterDuplicates(false);
            }
        } else if (databaseQuery.isModifyQuery()) {
            // By default, do not batch modify queries, as row count must be returned.
            ((ModifyQuery) databaseQuery).setIsBatchExecutionSupported(false);
        }
        ((JPQLCallQueryMechanism) databaseQuery.getQueryMechanism()).getJPQLCall().setIsParsed(true);
        // Apply the lock mode.
        if (lockMode != null && !lockMode.name().equals(ObjectLevelReadQuery.NONE)) {
            if (databaseQuery.isObjectLevelReadQuery()) {
                // set the lock mode, throw an exception.
                if (((ObjectLevelReadQuery) databaseQuery).setLockModeType(lockMode.name(), session)) {
                    throw new PersistenceException(ExceptionLocalization.buildMessage("ejb30-wrong-lock_called_without_version_locking-index", null));
                }
            } else {
                throw new IllegalArgumentException(ExceptionLocalization.buildMessage("invalid_lock_query", null));
            }
        }
        // Apply any query hints.
        databaseQuery = applyHints(hints, databaseQuery, classLoader, session);
        // If a primary key query, switch to read-object to allow cache hit.
        if (databaseQuery.isReadAllQuery() && !databaseQuery.isReportQuery() && ((ReadAllQuery) databaseQuery).shouldCheckCache()) {
            ReadAllQuery readQuery = (ReadAllQuery) databaseQuery;
            if ((readQuery.getContainerPolicy().getContainerClass() == ContainerPolicy.getDefaultContainerClass()) && (!readQuery.hasHierarchicalExpressions())) {
                databaseQuery.checkDescriptor(session);
                Expression selectionCriteria = databaseQuery.getSelectionCriteria();
                if ((selectionCriteria != null) && (databaseQuery.getDescriptor().getObjectBuilder().isPrimaryKeyExpression(true, selectionCriteria, session) || (databaseQuery.getDescriptor().getCachePolicy().isIndexableExpression(selectionCriteria, databaseQuery.getDescriptor(), session)))) {
                    ReadObjectQuery newQuery = new ReadObjectQuery();
                    newQuery.copyFromQuery(databaseQuery);
                    databaseQuery = newQuery;
                }
            }
        }
        if (isCacheable) {
            // Prepare query as hint may cause cloning (but not un-prepare
            // as in read-only).
            databaseQuery.checkPrepare(session, new DatabaseRecord());
            session.getProject().getJPQLParseCache().put(jpqlQuery, databaseQuery);
        }
    }
    return databaseQuery;
}
Also used : ObjectLevelReadQuery(org.eclipse.persistence.queries.ObjectLevelReadQuery) ReadObjectQuery(org.eclipse.persistence.queries.ReadObjectQuery) DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) Expression(org.eclipse.persistence.expressions.Expression) JPAQueryBuilder(org.eclipse.persistence.queries.JPAQueryBuilder) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) PersistenceException(jakarta.persistence.PersistenceException)

Aggregations

JPAQueryBuilder (org.eclipse.persistence.queries.JPAQueryBuilder)3 Expression (org.eclipse.persistence.expressions.Expression)2 PersistenceException (jakarta.persistence.PersistenceException)1 ConcurrencyException (org.eclipse.persistence.exceptions.ConcurrencyException)1 DatabaseException (org.eclipse.persistence.exceptions.DatabaseException)1 EclipseLinkException (org.eclipse.persistence.exceptions.EclipseLinkException)1 IntegrityException (org.eclipse.persistence.exceptions.IntegrityException)1 OptimisticLockException (org.eclipse.persistence.exceptions.OptimisticLockException)1 QueryException (org.eclipse.persistence.exceptions.QueryException)1 ValidationException (org.eclipse.persistence.exceptions.ValidationException)1 ExpressionBuilder (org.eclipse.persistence.expressions.ExpressionBuilder)1 CompoundExpression (org.eclipse.persistence.internal.expressions.CompoundExpression)1 FunctionExpression (org.eclipse.persistence.internal.expressions.FunctionExpression)1 ParameterExpression (org.eclipse.persistence.internal.expressions.ParameterExpression)1 SubSelectExpression (org.eclipse.persistence.internal.expressions.SubSelectExpression)1 PrivilegedClassForName (org.eclipse.persistence.internal.security.PrivilegedClassForName)1 PrivilegedNewInstanceFromClass (org.eclipse.persistence.internal.security.PrivilegedNewInstanceFromClass)1 DatabaseQuery (org.eclipse.persistence.queries.DatabaseQuery)1 ObjectLevelReadQuery (org.eclipse.persistence.queries.ObjectLevelReadQuery)1 ReadAllQuery (org.eclipse.persistence.queries.ReadAllQuery)1