com.jpattern.orm.query.clause.OrmFrom Maven / Gradle / Ivy
package com.jpattern.orm.query.clause;
import java.util.ArrayList;
import java.util.List;
import com.jpattern.orm.classtool.IOrmClassToolMap;
import com.jpattern.orm.query.INameSolver;
import com.jpattern.orm.query.IQueryRoot;
import com.jpattern.orm.query.NullNameSolver;
import com.jpattern.orm.query.from.JoinElement;
import com.jpattern.orm.query.from.FullOuterJoinElement;
import com.jpattern.orm.query.from.FromElement;
import com.jpattern.orm.query.from.InnerJoinElement;
import com.jpattern.orm.query.from.LeftOuterJoinElement;
import com.jpattern.orm.query.from.NaturalJoinElement;
import com.jpattern.orm.query.from.RightOuterJoinElement;
/**
*
* @author Francesco Cina
*
* 27/giu/2011
*/
public class OrmFrom implements From {
private INameSolver nameSolver = new NullNameSolver();
private final List joinElements = new ArrayList();
private final IOrmClassToolMap ormClassToolMap;
private final T query;
public OrmFrom (IOrmClassToolMap ormClassToolMap, T query) {
this.ormClassToolMap = ormClassToolMap;
this.query = query;
}
@Override
public From join(Class> joinClass) {
return join(joinClass, joinClass.getSimpleName());
}
@Override
public From join(Class> joinClass, String joinClassAlias) {
final FromElement joinElement = new JoinElement(ormClassToolMap, joinClass);
nameSolver.register(joinClass, joinClassAlias);
joinElement.setNameSolver(nameSolver);
joinElements.add(joinElement);
return this;
}
@Override
public From naturalJoin(Class> joinClass) {
return naturalJoin(joinClass, joinClass.getSimpleName());
}
@Override
public From naturalJoin(Class> joinClass, String joinClassAlias) {
final FromElement joinElement = new NaturalJoinElement(ormClassToolMap, joinClass);
nameSolver.register(joinClass, joinClassAlias);
joinElement.setNameSolver(nameSolver);
joinElements.add(joinElement);
return this;
}
@Override
public From innerJoin(Class> joinClass) {
return innerJoin(joinClass, joinClass.getSimpleName());
}
@Override
public From innerJoin(Class> joinClass, String joinClassAlias) {
final FromElement joinElement = new InnerJoinElement(ormClassToolMap, joinClass);
nameSolver.register(joinClass, joinClassAlias);
joinElement.setNameSolver(nameSolver);
joinElements.add(joinElement);
return this;
}
@Override
public From innerJoin(Class> joinClass, String onLeftProperty, String onRigthProperty) {
return innerJoin(joinClass, joinClass.getSimpleName(), onLeftProperty, onRigthProperty);
}
@Override
public From innerJoin(Class> joinClass, String joinClassAlias, String onLeftProperty, String onRigthProperty) {
final FromElement joinElement = new InnerJoinElement(ormClassToolMap, joinClass, onLeftProperty, onRigthProperty);
nameSolver.register(joinClass, joinClassAlias);
joinElement.setNameSolver(nameSolver);
joinElements.add(joinElement);
return this;
}
@Override
public From leftOuterJoin(Class> joinClass) {
return leftOuterJoin(joinClass, joinClass.getSimpleName());
}
@Override
public From leftOuterJoin(Class> joinClass, String joinClassAlias) {
final FromElement joinElement = new LeftOuterJoinElement(ormClassToolMap, joinClass);
nameSolver.register(joinClass, joinClassAlias);
joinElement.setNameSolver(nameSolver);
joinElements.add(joinElement);
return this;
}
@Override
public From leftOuterJoin(Class> joinClass, String onLeftProperty, String onRigthProperty) {
return leftOuterJoin(joinClass, joinClass.getSimpleName(), onLeftProperty, onRigthProperty);
}
@Override
public From leftOuterJoin(Class> joinClass, String joinClassAlias, String onLeftProperty, String onRigthProperty) {
final FromElement joinElement = new LeftOuterJoinElement(ormClassToolMap, joinClass, onLeftProperty, onRigthProperty);
nameSolver.register(joinClass, joinClassAlias);
joinElement.setNameSolver(nameSolver);
joinElements.add(joinElement);
return this;
}
@Override
public From rightOuterJoin(Class> joinClass) {
return rightOuterJoin(joinClass, joinClass.getSimpleName());
}
@Override
public From rightOuterJoin(Class> joinClass, String joinClassAlias) {
final FromElement joinElement = new RightOuterJoinElement(ormClassToolMap, joinClass);
nameSolver.register(joinClass, joinClassAlias);
joinElement.setNameSolver(nameSolver);
joinElements.add(joinElement);
return this;
}
@Override
public From rightOuterJoin(Class> joinClass, String onLeftProperty, String onRigthProperty) {
return rightOuterJoin(joinClass, joinClass.getSimpleName(), onLeftProperty, onRigthProperty);
}
@Override
public From rightOuterJoin(Class> joinClass, String joinClassAlias, String onLeftProperty, String onRigthProperty) {
final FromElement joinElement = new RightOuterJoinElement(ormClassToolMap, joinClass, onLeftProperty, onRigthProperty);
nameSolver.register(joinClass, joinClassAlias);
joinElement.setNameSolver(nameSolver);
joinElements.add(joinElement);
return this;
}
@Override
public From fullOuterJoin(Class> joinClass) {
return fullOuterJoin(joinClass, joinClass.getSimpleName());
}
@Override
public From fullOuterJoin(Class> joinClass, String joinClassAlias) {
final FromElement joinElement = new FullOuterJoinElement(ormClassToolMap, joinClass);
nameSolver.register(joinClass, joinClassAlias);
joinElement.setNameSolver(nameSolver);
joinElements.add(joinElement);
return this;
}
@Override
public From fullOuterJoin(Class> joinClass, String onLeftProperty, String onRigthProperty) {
return fullOuterJoin(joinClass, joinClass.getSimpleName(), onLeftProperty, onRigthProperty);
}
@Override
public From fullOuterJoin(Class> joinClass, String joinClassAlias, String onLeftProperty, String onRigthProperty) {
final FromElement joinElement = new FullOuterJoinElement(ormClassToolMap, joinClass, onLeftProperty, onRigthProperty);
nameSolver.register(joinClass, joinClassAlias);
joinElement.setNameSolver(nameSolver);
joinElements.add(joinElement);
return this;
}
@Override
public void setNameSolver(INameSolver nameSolver) {
this.nameSolver = nameSolver;
}
@Override
public String renderSql() {
final StringBuilder query = new StringBuilder();
renderSql(query);
return query.toString();
}
@Override
public void renderSql(StringBuilder StringBuilder) {
for (final FromElement joinElement : joinElements) {
joinElement.renderSql(StringBuilder);
}
}
@Override
public T query() {
return query;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy