package fr.pandacube.util.orm; import java.util.ArrayList; import java.util.List; import org.javatuples.Pair; public abstract class SQLWhereChain> extends SQLWhere { private SQLBoolOp operator; protected List> conditions = new ArrayList<>(); /* package */ SQLWhereChain(SQLBoolOp op) { if (op == null) throw new IllegalArgumentException("op can't be null"); operator = op; } protected void add(SQLWhere sqlWhere) { if (sqlWhere == null) throw new IllegalArgumentException("sqlWhere can't be null"); conditions.add(sqlWhere); } @Override public Pair> toSQL() throws ORMException { if (conditions.isEmpty()) { throw new ORMException("SQLWhereChain needs at least one element inside !"); } String sql = ""; List params = new ArrayList<>(); boolean first = true; for (SQLWhere w : conditions) { if (!first) sql += " " + operator.sql + " "; first = false; Pair> ret = w.toSQL(); sql += "(" + ret.getValue0() + ")"; params.addAll(ret.getValue1()); } return new Pair<>(sql, params); } /* package */ enum SQLBoolOp { /** Equivalent to SQL "AND" */ AND("AND"), /** Equivalent to SQL "OR" */ OR("OR"); /* package */ final String sql; private SQLBoolOp(String s) { sql = s; } } }