mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-30 21:08:30 +08:00
124 lines
3.5 KiB
Java
124 lines
3.5 KiB
Java
/*
|
|
* Copyright (c) 2009 Mysema Ltd.
|
|
* All rights reserved.
|
|
*
|
|
*/
|
|
package com.mysema.query.hql;
|
|
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
import com.mysema.query.support.QueryBaseWithProjection;
|
|
import com.mysema.query.types.CascadingBoolean;
|
|
import com.mysema.query.types.expr.EBoolean;
|
|
import com.mysema.query.types.expr.EEntity;
|
|
import com.mysema.query.types.expr.Expr;
|
|
import com.mysema.query.types.operation.OSimple;
|
|
import com.mysema.query.types.path.PEntity;
|
|
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<SubType extends HQLQueryBase<SubType>>
|
|
extends QueryBaseWithProjection<HQLJoinMeta, SubType> {
|
|
|
|
private List<Object> constants;
|
|
|
|
private String countRowsString, queryString;
|
|
|
|
private final HQLPatterns ops;
|
|
|
|
public HQLQueryBase(HQLPatterns ops) {
|
|
this.ops = ops;
|
|
}
|
|
|
|
private String buildQueryString(boolean forCountRow) {
|
|
if (getMetadata().getJoins().isEmpty()) {
|
|
throw new IllegalArgumentException("No joins given");
|
|
}
|
|
HQLSerializer serializer = new HQLSerializer(ops);
|
|
serializer.serialize(getMetadata(), forCountRow);
|
|
constants = serializer.getConstants();
|
|
return serializer.toString();
|
|
}
|
|
|
|
@Override
|
|
protected void clear() {
|
|
super.clear();
|
|
queryString = null;
|
|
countRowsString = null;
|
|
}
|
|
|
|
@SuppressWarnings("unchecked")
|
|
private <D> Expr<D> createAlias(EEntity<?> target, PEntity<D> alias){
|
|
return new OSimple<Object,D>((Class<D>)alias.getType(), HQLPatterns.ALIAS, target, alias);
|
|
}
|
|
|
|
protected EBoolean createQBECondition(PEntity<?> entity,
|
|
Map<String, Object> map) {
|
|
CascadingBoolean expr = new CascadingBoolean();
|
|
for (Map.Entry<String, Object> entry : map.entrySet()) {
|
|
PathMetadata<String> md = PathMetadata.forProperty(entity, entry
|
|
.getKey());
|
|
PSimple<Object> path = new PSimple<Object>(Object.class, md);
|
|
if (entry.getValue() != null) {
|
|
expr.and(path.eq(entry.getValue()));
|
|
} else {
|
|
expr.and(path.isNull());
|
|
}
|
|
}
|
|
return expr.create();
|
|
}
|
|
|
|
public SubType from(PEntity<?>... o) {
|
|
super.from(o);
|
|
return _this;
|
|
}
|
|
|
|
|
|
public SubType fullJoin(EEntity<?> target, PEntity<?> alias) {
|
|
super.fullJoin(createAlias(target,alias));
|
|
return _this;
|
|
}
|
|
|
|
protected List<Object> getConstants() {
|
|
return constants;
|
|
}
|
|
|
|
public SubType innerJoin(EEntity<?> target, PEntity<?> alias) {
|
|
super.innerJoin(createAlias(target,alias));
|
|
return _this;
|
|
}
|
|
|
|
public SubType join(EEntity<?> target, PEntity<?> alias) {
|
|
super.join(createAlias(target,alias));
|
|
return _this;
|
|
}
|
|
|
|
public SubType leftJoin(EEntity<?> target, PEntity<?> alias) {
|
|
super.leftJoin(createAlias(target,alias));
|
|
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;
|
|
}
|
|
|
|
}
|