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());
}
}
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;
}
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;
}
Aggregations