@SuppressWarnings("unchecked") protected <D> Expression<D> createAlias(Expression<?> expr, Path<D> alias) { assertRoot(alias); return ExpressionUtils.as((Expression) expr, alias); }
/** * Create an alias expression with the given source and alias * * @param <D> type of expression * @param source source * @param alias alias * @return source as alias */ public static <D> Expression<D> as(Expression<D> source, String alias) { return as(source, path(source.getType(), alias)); }
@SuppressWarnings("unchecked") public Q on(Predicate... conditions) { queryMixin.addJoin(JoinType.JOIN, ExpressionUtils.as((Path) ref, target)); queryMixin.on(conditions); return queryMixin.getSelf(); }
/** * Add the given join conditions. * * @param conditions must not be {@literal null}. * @return the target {@link QueryMixin}. * @see QueryMixin#on(Predicate) */ @SuppressWarnings("unchecked") public Q on(Predicate... conditions) { queryMixin.addJoin(JoinType.JOIN, ExpressionUtils.as((Path) ref, target)); queryMixin.on(conditions); return queryMixin.getSelf(); } }
@Test public void with_nested_factoryExpression2() { QBean<Entity> beanProjection = new QBean<Entity>(Entity.class, age, ExpressionUtils.as(new Concatenation(name, name2), "name")); FactoryExpression<Entity> wrappedProjection = FactoryExpressionUtils.wrap(beanProjection); Entity bean = wrappedProjection.newInstance(30, "Fri","tz"); assertEquals("Fritz", bean.getName()); }
/** * Add the given join conditions. * * @param conditions must not be {@literal null}. * @return the target {@link QueryMixin}. * @see QueryMixin#on(Predicate) */ @SuppressWarnings("unchecked") public Q on(Predicate... conditions) { queryMixin.addJoin(JoinType.JOIN, ExpressionUtils.as((Path) ref, target)); queryMixin.on(conditions); return queryMixin.getSelf(); } }
@Override public Expression<T> as(Path<T> alias) { return ExpressionUtils.as(this, alias); }
@Override public Expression<T> as(String alias) { return ExpressionUtils.as(this, alias); }
@Override @SuppressWarnings({ "unchecked", "rawtypes" }) public Q from(SubQueryExpression<?> subQuery, Path<?> alias) { return queryMixin.from(ExpressionUtils.as((Expression) subQuery, alias)); }
@SuppressWarnings("unchecked") protected <D> Expression<D> createAlias(Expression<?> expr, Path<D> alias) { assertRoot(alias); return ExpressionUtils.as((Expression) expr, alias); }
public static <T> Expression<T> union(List<SubQueryExpression<T>> union, Path<T> alias, boolean unionAll) { final Expression<T> rv = union(union, unionAll); return ExpressionUtils.as(rv, alias); }
public final SQLSerializer handleSelect(final String sep, final List<? extends Expression<?>> expressions) { if (inSubquery) { Set<String> names = Sets.newHashSet(); List<Expression<?>> replacements = Lists.newArrayList(); for (Expression<?> expr : expressions) { if (expr instanceof Path) { String name = ColumnMetadata.getName((Path<?>) expr); if (!names.add(name.toLowerCase())) { expr = ExpressionUtils.as(expr, "col__" + name + replacements.size()); } } replacements.add(expr); } return handle(sep, replacements); } else { return handle(sep, expressions); } }
/** * Create an alias expression with the given source and alias * * @param <D> type of expression * @param source source * @param alias alias * @return source as alias */ public static <D> Expression<D> as(Expression<D> source, String alias) { return as(source, path(source.getType(), alias)); }
rv.add(ExpressionUtils.as(column, "col" + (++counter)));
@SuppressWarnings("unchecked") protected SQLSerializer serialize(boolean forCountRow) { SQLSerializer serializer = createSerializer(); if (union != null) { if (queryMixin.getMetadata().getProjection() == null || expandProjection(queryMixin.getMetadata().getProjection()) .equals(expandProjection(firstUnionSubQuery.getMetadata().getProjection()))) { serializer.serializeUnion(union, queryMixin.getMetadata(), unionAll); } else { QueryMixin<Q> mixin2 = new QueryMixin<Q>(queryMixin.getMetadata().clone()); Set<Path<?>> paths = getRootPaths(expandProjection(mixin2.getMetadata().getProjection())); if (paths.isEmpty()) { mixin2.from(ExpressionUtils.as((Expression) union, defaultQueryAlias)); } else if (paths.size() == 1) { mixin2.from(ExpressionUtils.as((Expression) union, paths.iterator().next())); } else { throw new IllegalStateException("Unable to create serialize union"); } serializer.serialize(mixin2.getMetadata(), forCountRow); } } else { serializer.serialize(queryMixin.getMetadata(), forCountRow); } return serializer; }
@SuppressWarnings("unchecked") @Override public Expression<?> visit(Path<?> expr, @Nullable Void context) { expr = (Path<?>) super.visit(expr, null); PathMetadata pathMetadata = expr.getMetadata(); if (pathMetadata.getPathType() == PathType.LISTVALUE || pathMetadata.getPathType() == PathType.LISTVALUE_CONSTANT) { Path<?> replacement = replacements.get(expr); if (replacement == null) { // join parent as path123 on index(path123) = ... Path parent = shorten(pathMetadata.getParent(), true); replacement = ExpressionUtils.path(expr.getType(), ExpressionUtils.createRootVariable(parent, replacements.size())); metadata.addJoin(JoinType.LEFTJOIN, ExpressionUtils.as(parent, replacement)); metadata.addJoinCondition(ExpressionUtils.eq( (Expression) Expressions.operation(Integer.class, JPQLOps.INDEX, replacement), ExpressionUtils.toExpression(pathMetadata.getElement()))); replacements.put(expr, replacement); } return replacement; } else { return super.visit(expr, context); } }
@SuppressWarnings("unchecked") @Override public Expression<?> visit(Path<?> expr, @Nullable Void context) { expr = (Path<?>) super.visit(expr, null); PathMetadata pathMetadata = expr.getMetadata(); if (pathMetadata.getPathType() == PathType.MAPVALUE || pathMetadata.getPathType() == PathType.MAPVALUE_CONSTANT) { Path<?> replacement = replacements.get(expr); if (replacement == null) { // join parent as path123 on key(path123) = ... Path parent = shorten(pathMetadata.getParent(), true); ParameterizedExpression parExpr = (ParameterizedExpression) pathMetadata.getParent(); replacement = ExpressionUtils.path(parExpr.getParameter(1), ExpressionUtils.createRootVariable(parent, replacements.size())); metadata.addJoin(JoinType.LEFTJOIN, ExpressionUtils.as(parent, replacement)); metadata.addJoinCondition(ExpressionUtils.eq( Expressions.operation(parExpr.getParameter(0), JPQLOps.KEY, replacement), ExpressionUtils.toExpression(pathMetadata.getElement()))); replacements.put(expr, replacement); } return replacement; } else { return super.visit(expr, context); } }
@SuppressWarnings("unchecked") @Override public Expression<?> visit(Operation<?> expr, @Nullable Void context) { if (expr.getOperator() == Ops.CONTAINS_KEY) { ParameterizedExpression map = (ParameterizedExpression<?>) expr.getArg(0); Expression key = expr.getArg(1); Path replacement = ExpressionUtils.path(map.getParameter(1), ExpressionUtils.createRootVariable((Path<?>) map, Math.abs(expr.hashCode()))); metadata.addJoin(JoinType.LEFTJOIN, ExpressionUtils.as(map, replacement)); metadata.addJoinCondition(ExpressionUtils.eq( Expressions.operation(map.getParameter(0), JPQLOps.KEY, replacement), key)); return ExpressionUtils.isNotNull(replacement); } else if (expr.getOperator() == Ops.CONTAINS_VALUE) { ParameterizedExpression<?> map = (ParameterizedExpression<?>) expr.getArg(0); Expression<?> value = expr.getArg(1); return Expressions.predicate(JPQLOps.MEMBER_OF, value, map); } else { return super.visit(expr, context); } }
/** * Shorten the parent path to a length of max 2 elements */ private Path<?> shorten(Path<?> path, boolean outer) { if (aliases.containsKey(path)) { return aliases.get(path); } else if (path.getMetadata().isRoot()) { return path; } else if (path.getMetadata().getParent().getMetadata().isRoot() && outer) { return path; } else { Class<?> type = JPAQueryMixin.getElementTypeOrType(path); Path<?> parent = shorten(path.getMetadata().getParent(), false); Path oldPath = ExpressionUtils.path(path.getType(), new PathMetadata(parent, path.getMetadata().getElement(), path.getMetadata().getPathType())); if (oldPath.getMetadata().getParent().getMetadata().isRoot() && outer) { return oldPath; } else { Path newPath = ExpressionUtils.path(type, ExpressionUtils.createRootVariable(oldPath)); aliases.put(path, newPath); metadata.addJoin(JoinType.LEFTJOIN, ExpressionUtils.as(oldPath, newPath)); return newPath; } } }
/** * Shorten the parent path to a length of max 2 elements */ private Path<?> shorten(Path<?> path, boolean outer) { if (aliases.containsKey(path)) { return aliases.get(path); } else if (path.getMetadata().isRoot()) { return path; } else if (path.getMetadata().getParent().getMetadata().isRoot() && outer) { return path; } else { Class<?> type = JPAQueryMixin.getElementTypeOrType(path); Path<?> parent = shorten(path.getMetadata().getParent(), false); Path oldPath = ExpressionUtils.path(path.getType(), new PathMetadata(parent, path.getMetadata().getElement(), path.getMetadata().getPathType())); if (oldPath.getMetadata().getParent().getMetadata().isRoot() && outer) { return oldPath; } else { Path newPath = ExpressionUtils.path(type, ExpressionUtils.createRootVariable(oldPath)); aliases.put(path, newPath); metadata.addJoin(JoinType.LEFTJOIN, ExpressionUtils.as(oldPath, newPath)); return newPath; } } }