use of com.blazebit.persistence.impl.query.QuerySpecification in project blaze-persistence by Blazebit.
the class AbstractModificationCriteriaBuilder method getExecuteWithReturningQuery.
protected <R> TypedQuery<ReturningResult<R>> getExecuteWithReturningQuery(TypedQuery<Object[]> exampleQuery, Query baseQuery, String[] returningColumns, ReturningObjectBuilder<R> objectBuilder) {
Set<String> parameterListNames = parameterManager.getParameterListNames(baseQuery);
boolean shouldRenderCteNodes = renderCteNodes(false);
List<CTENode> ctes = shouldRenderCteNodes ? getCteNodes(false) : Collections.EMPTY_LIST;
QuerySpecification querySpecification = new ModificationQuerySpecification(this, baseQuery, exampleQuery, parameterManager.getParameterImpls(), parameterListNames, mainQuery.cteManager.isRecursive(), ctes, shouldRenderCteNodes, false, returningColumns, objectBuilder, null, returningAttributeBindingMap, mainQuery.getQueryConfiguration().isQueryPlanCacheEnabled());
CustomReturningSQLTypedQuery query = new CustomReturningSQLTypedQuery<R>(querySpecification, exampleQuery, parameterManager.getCriteriaNameMapping(), parameterManager.getTransformers(), parameterManager.getValuesParameters(), parameterManager.getValuesBinders());
query.setFirstResult(firstResult);
query.setMaxResults(maxResults);
parameterManager.parameterizeQuery(query);
return query;
}
use of com.blazebit.persistence.impl.query.QuerySpecification in project blaze-persistence by Blazebit.
the class AbstractUpdateCollectionCriteriaBuilder method getExecuteWithReturningQuery.
@Override
protected <R> TypedQuery<ReturningResult<R>> getExecuteWithReturningQuery(TypedQuery<Object[]> exampleQuery, Query baseQuery, String[] returningColumns, ReturningObjectBuilder<R> objectBuilder) {
if (collectionAttribute.getJoinTable() == null) {
return super.getExecuteWithReturningQuery(exampleQuery, baseQuery, returningColumns, objectBuilder);
} else {
QuerySpecification querySpecification = getQuerySpecification(baseQuery, exampleQuery, returningColumns, objectBuilder, null);
CustomReturningSQLTypedQuery query = new CustomReturningSQLTypedQuery<R>(querySpecification, exampleQuery, parameterManager.getCriteriaNameMapping(), parameterManager.getTransformers(), parameterManager.getValuesParameters(), parameterManager.getValuesBinders());
parameterManager.parameterizeQuery(query);
return query;
}
}
use of com.blazebit.persistence.impl.query.QuerySpecification in project blaze-persistence by Blazebit.
the class AbstractCTECriteriaBuilder method getQuery.
protected Query getQuery(String baseQueryString) {
Set<JoinNode> keyRestrictedLeftJoins = getKeyRestrictedLeftJoins();
Query query;
if (hasLimit() || joinManager.hasEntityFunctions() || !keyRestrictedLeftJoins.isEmpty()) {
// We need to change the underlying sql when doing a limit
query = em.createQuery(baseQueryString);
Set<String> parameterListNames = parameterManager.getParameterListNames(query);
String limit = null;
String offset = null;
// The main query will handle that separately
if (!isMainQuery) {
if (firstResult != 0) {
query.setFirstResult(firstResult);
offset = Integer.toString(firstResult);
}
if (maxResults != Integer.MAX_VALUE) {
query.setMaxResults(maxResults);
limit = Integer.toString(maxResults);
}
}
List<String> keyRestrictedLeftJoinAliases = getKeyRestrictedLeftJoinAliases(query, keyRestrictedLeftJoins, Collections.<ClauseType>emptySet());
List<EntityFunctionNode> entityFunctionNodes = getEntityFunctionNodes(query);
QuerySpecification querySpecification = new CTEQuerySpecification(this, query, parameterManager.getParameterImpls(), parameterListNames, limit, offset, keyRestrictedLeftJoinAliases, entityFunctionNodes);
query = new CustomSQLQuery(querySpecification, query, parameterManager.getCriteriaNameMapping(), parameterManager.getTransformers(), parameterManager.getValuesParameters(), parameterManager.getValuesBinders());
} else {
query = em.createQuery(baseQueryString);
if (parameterManager.getCriteriaNameMapping() != null) {
query = new QueryWrapper(query, parameterManager.getCriteriaNameMapping());
}
}
parameterManager.parameterizeQuery(query);
return query;
}
use of com.blazebit.persistence.impl.query.QuerySpecification in project blaze-persistence by Blazebit.
the class BaseFinalSetOperationBuilderImpl method getTypedQuery.
@Override
@SuppressWarnings("unchecked")
protected TypedQuery<T> getTypedQuery(StringBuilder lateralSb, JoinNode lateralJoinNode) {
if (lateralSb != null) {
throw new IllegalStateException("Lateral join with set operations is not yet supported!");
}
Set<String> parameterListNames = new HashSet<String>();
Query leftMostQuery = setOperationManager.getStartQueryBuilder().getTypedQueryForFinalOperationBuilder();
Query baseQuery;
parameterManager.collectParameterListNames(leftMostQuery, parameterListNames);
Query q = leftMostQuery;
if (leftMostQuery instanceof TypedQueryWrapper<?>) {
q = ((TypedQueryWrapper<?>) leftMostQuery).getDelegate();
}
if (q instanceof AbstractCustomQuery<?>) {
AbstractCustomQuery<?> customQuery = (AbstractCustomQuery<?>) q;
List<Query> customQueryParticipants = customQuery.getParticipatingQueries();
baseQuery = customQueryParticipants.get(0);
} else {
baseQuery = q;
}
List<Query> setOperands = new ArrayList<Query>();
for (AbstractCommonQueryBuilder<?, ?, ?, ?, ?> setOperand : setOperationManager.getSetOperations()) {
q = setOperand.getQuery();
setOperands.add(q);
parameterManager.collectParameterListNames(q, parameterListNames);
}
String limit = null;
String offset = null;
// Main query will get the limit applied by the native mechanism
if (!isMainQuery) {
if (firstResult != 0) {
offset = Integer.toString(firstResult);
}
if (maxResults != Integer.MAX_VALUE) {
limit = Integer.toString(maxResults);
}
}
// Since this builder has no query of it's own, there can be no joins
List<String> keyRestrictedLeftJoinAliases = Collections.emptyList();
List<EntityFunctionNode> entityFunctionNodes = getEntityFunctionNodes(baseQuery);
boolean shouldRenderCteNodes = renderCteNodes(false);
List<CTENode> ctes = shouldRenderCteNodes ? getCteNodes(false) : Collections.EMPTY_LIST;
QuerySpecification querySpecification = new SetOperationQuerySpecification(this, leftMostQuery, baseQuery, setOperands, setOperationManager.getOperator(), getOrderByElements(), setOperationManager.isNested(), parameterManager.getParameterImpls(), parameterListNames, limit, offset, keyRestrictedLeftJoinAliases, entityFunctionNodes, mainQuery.cteManager.isRecursive(), ctes, shouldRenderCteNodes, mainQuery.getQueryConfiguration().isQueryPlanCacheEnabled());
// Unfortunately we need this little adapter here
@SuppressWarnings("rawtypes") TypedQuery<T> query = new CustomSQLTypedQuery<T>(querySpecification, baseQuery, parameterManager.getCriteriaNameMapping(), parameterManager.getTransformers(), parameterManager.getValuesParameters(), parameterManager.getValuesBinders());
// The main query will use the native mechanism for limit/offset
if (isMainQuery) {
if (firstResult != 0) {
query.setFirstResult(firstResult);
}
if (maxResults != Integer.MAX_VALUE) {
query.setMaxResults(maxResults);
}
}
parameterManager.parameterizeQuery(query);
return applyObjectBuilder(query);
}
use of com.blazebit.persistence.impl.query.QuerySpecification in project blaze-persistence by Blazebit.
the class BaseUpdateCriteriaBuilderImpl method getQuery.
@Override
protected Query getQuery(Map<DbmsModificationState, String> includedModificationStates) {
prepareAndCheck();
JoinNode rootNode = joinManager.getRoots().get(0);
if (joinManager.getRoots().size() > 1 || rootNode.hasChildNodes()) {
// Prefer an exists subquery instead of MERGE
if (mainQuery.dbmsDialect.getUpdateJoinStyle() == UpdateJoinStyle.NONE) {
return super.getQuery(includedModificationStates);
}
Query baseQuery = em.createQuery(getBaseQueryStringWithCheck(null, null));
QuerySpecification querySpecification = getQuerySpecification(baseQuery, getCountExampleQuery(), getReturningColumns(), null, includedModificationStates);
CustomSQLQuery query = new CustomSQLQuery(querySpecification, baseQuery, parameterManager.getCriteriaNameMapping(), parameterManager.getTransformers(), parameterManager.getValuesParameters(), parameterManager.getValuesBinders());
parameterManager.parameterizeQuery(query);
return query;
} else {
return super.getQuery(includedModificationStates);
}
}
Aggregations