/* * Copyright (c) 2009 Mysema Ltd. * All rights reserved. * */ package com.mysema.query.hql; import java.util.List; import java.util.Map; import javax.annotation.Nullable; import com.mysema.query.BooleanBuilder; import com.mysema.query.JoinExpression; import com.mysema.query.JoinType; import com.mysema.query.QueryMetadata; import com.mysema.query.support.QueryBaseWithProjection; import com.mysema.query.types.expr.EBoolean; import com.mysema.query.types.expr.Expr; import com.mysema.query.types.operation.OSimple; import com.mysema.query.types.operation.Ops; import com.mysema.query.types.path.PEntity; import com.mysema.query.types.path.PEntityCollection; import com.mysema.query.types.path.PEntityMap; import com.mysema.query.types.path.PSimple; import com.mysema.query.types.path.PathMetadata; /** * HQLQueryBase is a base Query class for HQL * * @author tiwe * @version $Id$ */ public abstract class HQLQueryBase> extends QueryBaseWithProjection { private Map constants; @Nullable private String countRowsString, queryString; private final HQLTemplates templates; public HQLQueryBase(QueryMetadata md, HQLTemplates templates) { super(md); this.templates = templates; } private String buildQueryString(boolean forCountRow) { if (getMetadata().getJoins().isEmpty()) { throw new IllegalArgumentException("No joins given"); } HQLSerializer serializer = new HQLSerializer(templates); serializer.serialize(getMetadata(), forCountRow); constants = serializer.getConstantToLabel(); return serializer.toString(); } @SuppressWarnings("unchecked") private Expr createAlias(PEntity target, PEntity alias){ return OSimple.create((Class)alias.getType(), Ops.ALIAS, target, alias); } @SuppressWarnings("unchecked") private Expr createAlias(PEntityCollection target, PEntity alias){ return OSimple.create((Class)alias.getType(), Ops.ALIAS, target.asExpr(), alias); } @SuppressWarnings("unchecked") private Expr createAlias(PEntityMap target, PEntity alias){ return OSimple.create((Class)alias.getType(), Ops.ALIAS, target, alias); } protected EBoolean createQBECondition(PEntity entity, Map map) { BooleanBuilder expr = new BooleanBuilder(); for (Map.Entry entry : map.entrySet()) { PathMetadata md = PathMetadata.forProperty(entity, entry .getKey()); PSimple path = new PSimple(Object.class, md); if (entry.getValue() != null) { expr.and(path.eq(entry.getValue())); } else { expr.and(path.isNull()); } } return expr; } public SubType from(PEntity... args) { getMetadata().addFrom(args); return _this; } public SubType fetch(){ List joins = getMetadata().getJoins(); joins.get(joins.size()-1).setFetch(true); return _this; } public

SubType fullJoin(PEntity

target) { getMetadata().addJoin(JoinType.FULLJOIN, target); return _this; } public

SubType fullJoin(PEntity

target, PEntity

alias) { getMetadata().addJoin(JoinType.FULLJOIN, createAlias(target, alias)); return _this; } public

SubType fullJoin(PEntityCollection

target) { getMetadata().addJoin(JoinType.FULLJOIN, target); return _this; } public

SubType fullJoin(PEntityCollection

target, PEntity

alias) { getMetadata().addJoin(JoinType.FULLJOIN, createAlias(target, alias)); return _this; } public

SubType fullJoin(PEntityMap target) { getMetadata().addJoin(JoinType.FULLJOIN, target); return _this; } public

SubType fullJoin(PEntityMap target, PEntity

alias) { getMetadata().addJoin(JoinType.FULLJOIN, createAlias(target, alias)); return _this; } protected Map getConstants() { return constants; } public

SubType innerJoin(PEntity

target) { getMetadata().addJoin(JoinType.INNERJOIN, target); return _this; } public

SubType innerJoin(PEntity

target, PEntity

alias) { getMetadata().addJoin(JoinType.INNERJOIN, createAlias(target, alias)); return _this; } public

SubType innerJoin(PEntityCollection

target) { getMetadata().addJoin(JoinType.INNERJOIN, target); return _this; } public

SubType innerJoin(PEntityCollection

target, PEntity

alias) { getMetadata().addJoin(JoinType.INNERJOIN, createAlias(target, alias)); return _this; } public

SubType innerJoin(PEntityMap target) { getMetadata().addJoin(JoinType.INNERJOIN, target); return _this; } public

SubType innerJoin(PEntityMap target, PEntity

alias) { getMetadata().addJoin(JoinType.INNERJOIN, createAlias(target, alias)); return _this; } public

SubType join(PEntity

target) { getMetadata().addJoin(JoinType.JOIN, target); return _this; } public

SubType join(PEntity

target, PEntity

alias) { getMetadata().addJoin(JoinType.JOIN, createAlias(target, alias)); return _this; } public

SubType join(PEntityCollection

target) { getMetadata().addJoin(JoinType.JOIN, target); return _this; } public

SubType join(PEntityCollection

target, PEntity

alias) { getMetadata().addJoin(JoinType.JOIN, createAlias(target, alias)); return _this; } public

SubType join(PEntityMap target) { getMetadata().addJoin(JoinType.JOIN, target); return _this; } public

SubType join(PEntityMap target, PEntity

alias) { getMetadata().addJoin(JoinType.JOIN, createAlias(target, alias)); return _this; } public

SubType leftJoin(PEntity

target) { getMetadata().addJoin(JoinType.LEFTJOIN, target); return _this; } public

SubType leftJoin(PEntity

target, PEntity

alias) { getMetadata().addJoin(JoinType.LEFTJOIN, createAlias(target, alias)); return _this; } public

SubType leftJoin(PEntityCollection

target) { getMetadata().addJoin(JoinType.LEFTJOIN, target); return _this; } public

SubType leftJoin(PEntityCollection

target, PEntity

alias) { getMetadata().addJoin(JoinType.LEFTJOIN, createAlias(target, alias)); return _this; } public

SubType leftJoin(PEntityMap target, PEntity

alias) { getMetadata().addJoin(JoinType.LEFTJOIN, createAlias(target, alias)); return _this; } public

SubType leftJoin(PEntityMap target) { getMetadata().addJoin(JoinType.LEFTJOIN, target); return _this; } public SubType with(EBoolean... conditions){ for (EBoolean condition : conditions){ getMetadata().addJoinCondition(condition); } return _this; } public String toCountRowsString() { if (countRowsString == null) { countRowsString = buildQueryString(true); } return countRowsString; } @Override public String toString() { if (queryString == null) { queryString = buildQueryString(false); } return queryString; } }