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 org.javatuples.Pair;
import fr.pandacube.util.Log; import fr.pandacube.util.Log;
import fr.pandacube.util.orm.SQLWhereChain.SQLBoolOp;
import fr.pandacube.util.orm.SQLWhereComp.SQLComparator; 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 { public static <E extends SQLElement<E>> SQLElementList<E> getByIds(Class<E> elemClass, Integer... ids) throws ORMException {
SQLField<E, Integer> idField = getSQLIdField(elemClass); SQLField<E, Integer> idField = getSQLIdField(elemClass);
SQLWhereChain where = new SQLWhereChain(SQLBoolOp.OR); SQLWhereChain where = new SQLWhereOr();
for (Integer id : ids) for (Integer id : ids)
if (id != null) where.add(new SQLWhereComp(idField, SQLComparator.EQ, id)); if (id != null) where.or(new SQLWhereComp(idField, SQLComparator.EQ, id));
return getAll(elemClass, where, new SQLOrderBy().add(idField), 1, null); return getAll(elemClass, where, SQLOrderBy.asc(idField), 1, null);
} }
public static <E extends SQLElement<E>> E getById(Class<E> elemClass, int id) throws ORMException { 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); 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) public static <E extends SQLElement<E>> E getFirst(Class<E> elemClass, SQLWhere where, SQLOrderBy orderBy)
throws ORMException { throws ORMException {
return getFirst(elemClass, where, orderBy, null); return getFirst(elemClass, where, orderBy, null);

View File

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

View File

@ -1,10 +1,13 @@
package fr.pandacube.util.orm; package fr.pandacube.util.orm;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import java.util.List;
import org.javatuples.Pair; import org.javatuples.Pair;
import fr.pandacube.util.orm.SQLWhereComp.SQLComparator;
public class SQLField<E extends SQLElement<E>, T> { public class SQLField<E extends SQLElement<E>, T> {
private Class<E> sqlElemClass; private Class<E> sqlElemClass;
@ -83,5 +86,49 @@ public class SQLField<E extends SQLElement<E>, T> {
public int hashCode() { public int hashCode() {
return getName().hashCode() + sqlElemClass.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 * Construit une nouvelle clause ORDER BY
*/ */
public SQLOrderBy() {} private SQLOrderBy() {}
/** /**
* Ajoute un champ dans la clause ORDER BY en construction * 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) * @param d le sens de tri (croissant ASC ou décroissant DESC)
* @return l'objet courant (permet de chainer les ajouts de champs) * @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)); orderByFields.add(new OBField(field, d));
return this; return this;
} }
/** /**
* Ajoute un champ dans la clause ORDER BY en construction, * Ajoute un champ dans la clause ORDER BY en construction avec pour direction ASC
* avec comme ordre de tri croissant ASC par défaut
* *
* @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) * @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); 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() { /* package */ String toSQL() {
String ret = ""; String ret = "";
boolean first = true; boolean first = true;
@ -62,7 +71,27 @@ public class SQLOrderBy {
} }
public enum Direction { private enum Direction {
ASC, DESC; 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)]"; 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; import org.javatuples.Pair;
public class SQLWhereChain extends SQLWhere { public abstract class SQLWhereChain extends SQLWhere {
private SQLBoolOp operator; 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"); if (op == null) throw new IllegalArgumentException("op can't be null");
operator = op; operator = op;
} }
public SQLWhereChain add(SQLWhere sqlWhere) { protected SQLWhereChain add(SQLWhere sqlWhere) {
if (sqlWhere == null) throw new IllegalArgumentException("sqlWhere can't be null"); if (sqlWhere == null) throw new IllegalArgumentException("sqlWhere can't be null");
conditions.add(sqlWhere); conditions.add(sqlWhere);
return this; return this;
} }
@Override @Override
public Pair<String, List<Object>> toSQL() throws ORMException { public Pair<String, List<Object>> toSQL() throws ORMException {
if (conditions.isEmpty()) { if (conditions.isEmpty()) {
@ -43,12 +43,12 @@ public class SQLWhereChain extends SQLWhere {
return new Pair<>(sql, params); return new Pair<>(sql, params);
} }
public enum SQLBoolOp { /* package */ enum SQLBoolOp {
/** Equivalent to SQL "<code>AND</code>" */ /** Equivalent to SQL "<code>AND</code>" */
AND("AND"), AND("AND"),
/** Equivalent to SQL "<code>OR</code>" */ /** Equivalent to SQL "<code>OR</code>" */
OR("OR"); OR("OR");
public final String sql; /* package */ final String sql;
private SQLBoolOp(String s) { private SQLBoolOp(String s) {
sql = s; sql = s;

View File

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

View File

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

View File

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

View File

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