Update ORM API (makes usage less verbose)

This commit is contained in:
Marc Baloup 2020-02-27 02:06:38 +01:00
parent 0c3e1425fd
commit f510099068
12 changed files with 162 additions and 33 deletions

View File

@ -14,7 +14,6 @@ import java.util.function.Consumer;
import org.javatuples.Pair;
import fr.pandacube.util.Log;
import fr.pandacube.util.orm.SQLWhereChain.SQLBoolOp;
import fr.pandacube.util.orm.SQLWhereComp.SQLComparator;
/**
@ -110,10 +109,10 @@ public final class ORM {
public static <E extends SQLElement<E>> SQLElementList<E> getByIds(Class<E> elemClass, Integer... ids) throws ORMException {
SQLField<E, Integer> idField = getSQLIdField(elemClass);
SQLWhereChain where = new SQLWhereChain(SQLBoolOp.OR);
SQLWhereChain where = new SQLWhereOr();
for (Integer id : ids)
if (id != null) where.add(new SQLWhereComp(idField, SQLComparator.EQ, id));
return getAll(elemClass, where, new SQLOrderBy().add(idField), 1, null);
if (id != null) where.or(new SQLWhereComp(idField, SQLComparator.EQ, id));
return getAll(elemClass, where, SQLOrderBy.asc(idField), 1, null);
}
public static <E extends SQLElement<E>> E getById(Class<E> elemClass, int id) throws ORMException {
@ -125,6 +124,11 @@ public final class ORM {
return getFirst(elemClass, where, null, null);
}
public static <E extends SQLElement<E>> E getFirst(Class<E> elemClass, SQLOrderBy orderBy)
throws ORMException {
return getFirst(elemClass, null, orderBy, null);
}
public static <E extends SQLElement<E>> E getFirst(Class<E> elemClass, SQLWhere where, SQLOrderBy orderBy)
throws ORMException {
return getFirst(elemClass, where, orderBy, null);

View File

@ -14,7 +14,6 @@ import java.util.stream.Collectors;
import com.google.gson.JsonArray;
import fr.pandacube.util.Log;
import fr.pandacube.util.orm.SQLWhereChain.SQLBoolOp;
import fr.pandacube.util.orm.SQLWhereComp.SQLComparator;
/**
@ -153,8 +152,8 @@ public class SQLElementList<E extends SQLElement<E>> extends ArrayList<E> {
return new SQLElementList<>();
}
SQLWhereChain where = new SQLWhereChain(SQLBoolOp.OR);
values.forEach(v -> where.add(new SQLWhereComp(foreignKey.getPrimaryField(), SQLComparator.EQ, v)));
SQLWhereChain where = SQLWhereChain.or();
values.forEach(v -> where.or(new SQLWhereComp(foreignKey.getPrimaryField(), SQLComparator.EQ, v)));
return ORM.getAll(foreignKey.getForeignElementClass(), where, orderBy, null, null);
@ -184,8 +183,8 @@ public class SQLElementList<E extends SQLElement<E>> extends ArrayList<E> {
return new SQLElementList<>();
}
SQLWhereChain where = new SQLWhereChain(SQLBoolOp.OR);
values.forEach(v -> where.add(new SQLWhereComp(foreignKey, SQLComparator.EQ, v)));
SQLWhereChain where = SQLWhere.or();
values.forEach(v -> where.or(new SQLWhereComp(foreignKey, SQLComparator.EQ, v)));
return ORM.getAll(foreignKey.getSQLElementType(), where, orderBy, limit, offset);

View File

@ -1,10 +1,13 @@
package fr.pandacube.util.orm;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.javatuples.Pair;
import fr.pandacube.util.orm.SQLWhereComp.SQLComparator;
public class SQLField<E extends SQLElement<E>, T> {
private Class<E> sqlElemClass;
@ -83,5 +86,49 @@ public class SQLField<E extends SQLElement<E>, T> {
public int hashCode() {
return getName().hashCode() + sqlElemClass.hashCode();
}
public SQLWhere eq(T r) {
return comp(SQLComparator.EQ, r);
}
public SQLWhere geq(T r) {
return comp(SQLComparator.GEQ, r);
}
public SQLWhere gt(T r) {
return comp(SQLComparator.GT, r);
}
public SQLWhere leq(T r) {
return comp(SQLComparator.LEQ, r);
}
public SQLWhere lt(T r) {
return comp(SQLComparator.LT, r);
}
public SQLWhere neq(T r) {
return comp(SQLComparator.NEQ, r);
}
private SQLWhere comp(SQLComparator c, T r) {
return new SQLWhereComp(this, c, r);
}
public SQLWhere in(Collection<T> v) {
return new SQLWhereIn(this, v);
}
public SQLWhere isNull() {
return new SQLWhereNull(this, true);
}
public SQLWhere isNotNull() {
return new SQLWhereNull(this, false);
}
}

View File

@ -10,7 +10,7 @@ public class SQLOrderBy {
/**
* Construit une nouvelle clause ORDER BY
*/
public SQLOrderBy() {}
private SQLOrderBy() {}
/**
* Ajoute un champ dans la clause ORDER BY en construction
@ -19,22 +19,31 @@ public class SQLOrderBy {
* @param d le sens de tri (croissant ASC ou décroissant DESC)
* @return l'objet courant (permet de chainer les ajouts de champs)
*/
public SQLOrderBy add(SQLField<?, ?> field, Direction d) {
private SQLOrderBy add(SQLField<?, ?> field, Direction d) {
orderByFields.add(new OBField(field, d));
return this;
}
/**
* Ajoute un champ dans la clause ORDER BY en construction,
* avec comme ordre de tri croissant ASC par défaut
* Ajoute un champ dans la clause ORDER BY en construction avec pour direction ASC
*
* @param field le champ SQL à ordonner dans l'ordre croissant ASC
* @param field le champ SQL à ordonner
* @return l'objet courant (permet de chainer les ajouts de champs)
*/
public SQLOrderBy add(SQLField<?, ?> field) {
public SQLOrderBy thenAsc(SQLField<?, ?> field) {
return add(field, Direction.ASC);
}
/**
* Ajoute un champ dans la clause ORDER BY en construction avec pour direction DESC
*
* @param field le champ SQL à ordonner
* @return l'objet courant (permet de chainer les ajouts de champs)
*/
public SQLOrderBy thenDesc(SQLField<?, ?> field) {
return add(field, Direction.DESC);
}
/* package */ String toSQL() {
String ret = "";
boolean first = true;
@ -62,7 +71,27 @@ public class SQLOrderBy {
}
public enum Direction {
private enum Direction {
ASC, DESC;
}
public static SQLOrderBy asc(SQLField<?, ?> field) {
return new SQLOrderBy().thenAsc(field);
}
public static SQLOrderBy desc(SQLField<?, ?> field) {
return new SQLOrderBy().thenDesc(field);
}
}

View File

@ -19,5 +19,27 @@ public abstract class SQLWhere {
return "[SQLWhere.toString() error (see logs)]";
}
}
public SQLWhereAnd and(SQLWhere other) {
return and().and(this).and(other);
}
public SQLWhereOr or(SQLWhere other) {
return or().or(this).or(other);
}
public static SQLWhereAnd and() {
return new SQLWhereAnd();
}
public static SQLWhereOr or() {
return new SQLWhereOr();
}
public static SQLWhere like(SQLField<?, String> f, String like) {
return new SQLWhereLike(f, like);
}
}

View File

@ -0,0 +1,14 @@
package fr.pandacube.util.orm;
public class SQLWhereAnd extends SQLWhereChain {
/* package */ SQLWhereAnd() {
super(SQLBoolOp.AND);
}
@Override
public SQLWhereAnd and(SQLWhere other) {
return (SQLWhereAnd) add(other);
}
}

View File

@ -5,22 +5,22 @@ import java.util.List;
import org.javatuples.Pair;
public class SQLWhereChain extends SQLWhere {
public abstract class SQLWhereChain extends SQLWhere {
private SQLBoolOp operator;
private List<SQLWhere> conditions = new ArrayList<>();
protected List<SQLWhere> conditions = new ArrayList<>();
public SQLWhereChain(SQLBoolOp op) {
/* package */ SQLWhereChain(SQLBoolOp op) {
if (op == null) throw new IllegalArgumentException("op can't be null");
operator = op;
}
public SQLWhereChain add(SQLWhere sqlWhere) {
protected SQLWhereChain add(SQLWhere sqlWhere) {
if (sqlWhere == null) throw new IllegalArgumentException("sqlWhere can't be null");
conditions.add(sqlWhere);
return this;
}
@Override
public Pair<String, List<Object>> toSQL() throws ORMException {
if (conditions.isEmpty()) {
@ -43,12 +43,12 @@ public class SQLWhereChain extends SQLWhere {
return new Pair<>(sql, params);
}
public enum SQLBoolOp {
/* package */ enum SQLBoolOp {
/** Equivalent to SQL "<code>AND</code>" */
AND("AND"),
/** Equivalent to SQL "<code>OR</code>" */
OR("OR");
public final String sql;
/* package */ final String sql;
private SQLBoolOp(String s) {
sql = s;

View File

@ -5,7 +5,7 @@ import java.util.List;
import org.javatuples.Pair;
public class SQLWhereComp extends SQLWhere {
/* package */ class SQLWhereComp extends SQLWhere {
private SQLField<?, ?> left;
private SQLComparator comp;
@ -18,7 +18,7 @@ public class SQLWhereComp extends SQLWhere {
* @param c the comparison operator, can't be null
* @param r the value at right of the comparison operator. Can't be null
*/
public <T> SQLWhereComp(SQLField<?, T> l, SQLComparator c, T r) {
/* package */ <T> SQLWhereComp(SQLField<?, T> l, SQLComparator c, T r) {
if (l == null || r == null || c == null)
throw new IllegalArgumentException("All arguments for SQLWhereComp constructor can't be null");
left = l;
@ -33,7 +33,7 @@ public class SQLWhereComp extends SQLWhere {
return new Pair<>("`" + left.getName() + "` " + comp.sql + " ? ", params);
}
public enum SQLComparator {
/* package */ enum SQLComparator {
/** Equivalent to SQL "<code>=</code>" */
EQ("="),
/** Equivalent to SQL "<code>></code>" */
@ -47,7 +47,7 @@ public class SQLWhereComp extends SQLWhere {
/** Equivalent to SQL "<code>!=</code>" */
NEQ("!=");
public final String sql;
/* package */ final String sql;
private SQLComparator(String s) {
sql = s;

View File

@ -6,12 +6,12 @@ import java.util.List;
import org.javatuples.Pair;
public class SQLWhereIn extends SQLWhere {
/* package */ class SQLWhereIn extends SQLWhere {
private SQLField<?, ?> field;
private Collection<?> values;
public <T> SQLWhereIn(SQLField<?, T> f, Collection<T> v) {
/* package */ <T> SQLWhereIn(SQLField<?, T> f, Collection<T> v) {
if (f == null || v == null)
throw new IllegalArgumentException("All arguments for SQLWhereIn constructor can't be null");
field = f;

View File

@ -5,7 +5,7 @@ import java.util.List;
import org.javatuples.Pair;
public class SQLWhereLike extends SQLWhere {
/* package */ class SQLWhereLike extends SQLWhere {
private SQLField<?, String> field;
private String likeExpr;
@ -16,7 +16,7 @@ public class SQLWhereLike extends SQLWhere {
* @param f the field at left of the LIKE keyword. Can't be null
* @param like the like expression.
*/
public SQLWhereLike(SQLField<?, String> f, String like) {
/* package */ SQLWhereLike(SQLField<?, String> f, String like) {
if (f == null || like == null)
throw new IllegalArgumentException("All arguments for SQLWhereLike constructor can't be null");
field = f;

View File

@ -8,7 +8,7 @@ import org.javatuples.Pair;
import fr.pandacube.util.Log;
public class SQLWhereNull extends SQLWhere {
/* package */ class SQLWhereNull extends SQLWhere {
private SQLField<?, ?> fild;
private boolean nulll;
@ -20,7 +20,7 @@ public class SQLWhereNull extends SQLWhere {
* @param isNull true if we want to ckeck if "IS NULL", or false to check if
* "IS NOT NULL"
*/
public SQLWhereNull(SQLField<?, ?> field, boolean isNull) {
/* package */ SQLWhereNull(SQLField<?, ?> field, boolean isNull) {
if (field == null) throw new IllegalArgumentException("field can't be null");
if (!field.canBeNull) Log.getLogger().log(Level.WARNING,
"Useless : Trying to check IS [NOT] NULL on the field " + field.getSQLElementType().getName() + "#"

View File

@ -0,0 +1,14 @@
package fr.pandacube.util.orm;
public class SQLWhereOr extends SQLWhereChain {
/* package */ SQLWhereOr() {
super(SQLBoolOp.OR);
}
@Override
public SQLWhereOr or(SQLWhere other) {
return (SQLWhereOr) add(other);
}
}