From f510099068e6e43b09f97a7d7a1f7bc98989ff98 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Thu, 27 Feb 2020 02:06:38 +0100 Subject: [PATCH] Update ORM API (makes usage less verbose) --- src/main/java/fr/pandacube/util/orm/ORM.java | 12 +++-- .../fr/pandacube/util/orm/SQLElementList.java | 9 ++-- .../java/fr/pandacube/util/orm/SQLField.java | 47 +++++++++++++++++++ .../fr/pandacube/util/orm/SQLOrderBy.java | 43 ++++++++++++++--- .../java/fr/pandacube/util/orm/SQLWhere.java | 22 +++++++++ .../fr/pandacube/util/orm/SQLWhereAnd.java | 14 ++++++ .../fr/pandacube/util/orm/SQLWhereChain.java | 14 +++--- .../fr/pandacube/util/orm/SQLWhereComp.java | 8 ++-- .../fr/pandacube/util/orm/SQLWhereIn.java | 4 +- .../fr/pandacube/util/orm/SQLWhereLike.java | 4 +- .../fr/pandacube/util/orm/SQLWhereNull.java | 4 +- .../fr/pandacube/util/orm/SQLWhereOr.java | 14 ++++++ 12 files changed, 162 insertions(+), 33 deletions(-) create mode 100644 src/main/java/fr/pandacube/util/orm/SQLWhereAnd.java create mode 100644 src/main/java/fr/pandacube/util/orm/SQLWhereOr.java diff --git a/src/main/java/fr/pandacube/util/orm/ORM.java b/src/main/java/fr/pandacube/util/orm/ORM.java index 88f2fd0..2aa8384 100644 --- a/src/main/java/fr/pandacube/util/orm/ORM.java +++ b/src/main/java/fr/pandacube/util/orm/ORM.java @@ -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 > SQLElementList getByIds(Class elemClass, Integer... ids) throws ORMException { SQLField 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 getById(Class elemClass, int id) throws ORMException { @@ -125,6 +124,11 @@ public final class ORM { return getFirst(elemClass, where, null, null); } + public static > E getFirst(Class elemClass, SQLOrderBy orderBy) + throws ORMException { + return getFirst(elemClass, null, orderBy, null); + } + public static > E getFirst(Class elemClass, SQLWhere where, SQLOrderBy orderBy) throws ORMException { return getFirst(elemClass, where, orderBy, null); diff --git a/src/main/java/fr/pandacube/util/orm/SQLElementList.java b/src/main/java/fr/pandacube/util/orm/SQLElementList.java index da3a3cd..27806cd 100644 --- a/src/main/java/fr/pandacube/util/orm/SQLElementList.java +++ b/src/main/java/fr/pandacube/util/orm/SQLElementList.java @@ -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> extends ArrayList { 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> extends ArrayList { 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); diff --git a/src/main/java/fr/pandacube/util/orm/SQLField.java b/src/main/java/fr/pandacube/util/orm/SQLField.java index f048944..4e73199 100644 --- a/src/main/java/fr/pandacube/util/orm/SQLField.java +++ b/src/main/java/fr/pandacube/util/orm/SQLField.java @@ -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, T> { private Class sqlElemClass; @@ -83,5 +86,49 @@ public class SQLField, 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 v) { + return new SQLWhereIn(this, v); + } + + + + public SQLWhere isNull() { + return new SQLWhereNull(this, true); + } + + public SQLWhere isNotNull() { + return new SQLWhereNull(this, false); + } } diff --git a/src/main/java/fr/pandacube/util/orm/SQLOrderBy.java b/src/main/java/fr/pandacube/util/orm/SQLOrderBy.java index 2f85d79..401337a 100644 --- a/src/main/java/fr/pandacube/util/orm/SQLOrderBy.java +++ b/src/main/java/fr/pandacube/util/orm/SQLOrderBy.java @@ -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); + } + + + + } diff --git a/src/main/java/fr/pandacube/util/orm/SQLWhere.java b/src/main/java/fr/pandacube/util/orm/SQLWhere.java index c4e30a3..6819cde 100644 --- a/src/main/java/fr/pandacube/util/orm/SQLWhere.java +++ b/src/main/java/fr/pandacube/util/orm/SQLWhere.java @@ -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 f, String like) { + return new SQLWhereLike(f, like); + } } diff --git a/src/main/java/fr/pandacube/util/orm/SQLWhereAnd.java b/src/main/java/fr/pandacube/util/orm/SQLWhereAnd.java new file mode 100644 index 0000000..b71cdf2 --- /dev/null +++ b/src/main/java/fr/pandacube/util/orm/SQLWhereAnd.java @@ -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); + } + +} diff --git a/src/main/java/fr/pandacube/util/orm/SQLWhereChain.java b/src/main/java/fr/pandacube/util/orm/SQLWhereChain.java index fe81968..b482405 100644 --- a/src/main/java/fr/pandacube/util/orm/SQLWhereChain.java +++ b/src/main/java/fr/pandacube/util/orm/SQLWhereChain.java @@ -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 conditions = new ArrayList<>(); + protected List 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> 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 "AND" */ AND("AND"), /** Equivalent to SQL "OR" */ OR("OR"); - public final String sql; + /* package */ final String sql; private SQLBoolOp(String s) { sql = s; diff --git a/src/main/java/fr/pandacube/util/orm/SQLWhereComp.java b/src/main/java/fr/pandacube/util/orm/SQLWhereComp.java index 377cb31..2c719a7 100644 --- a/src/main/java/fr/pandacube/util/orm/SQLWhereComp.java +++ b/src/main/java/fr/pandacube/util/orm/SQLWhereComp.java @@ -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 SQLWhereComp(SQLField l, SQLComparator c, T r) { + /* package */ SQLWhereComp(SQLField 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 "=" */ EQ("="), /** Equivalent to SQL ">" */ @@ -47,7 +47,7 @@ public class SQLWhereComp extends SQLWhere { /** Equivalent to SQL "!=" */ NEQ("!="); - public final String sql; + /* package */ final String sql; private SQLComparator(String s) { sql = s; diff --git a/src/main/java/fr/pandacube/util/orm/SQLWhereIn.java b/src/main/java/fr/pandacube/util/orm/SQLWhereIn.java index 516f663..526746f 100644 --- a/src/main/java/fr/pandacube/util/orm/SQLWhereIn.java +++ b/src/main/java/fr/pandacube/util/orm/SQLWhereIn.java @@ -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 SQLWhereIn(SQLField f, Collection v) { + /* package */ SQLWhereIn(SQLField f, Collection v) { if (f == null || v == null) throw new IllegalArgumentException("All arguments for SQLWhereIn constructor can't be null"); field = f; diff --git a/src/main/java/fr/pandacube/util/orm/SQLWhereLike.java b/src/main/java/fr/pandacube/util/orm/SQLWhereLike.java index e8cc35c..4aa2e0c 100644 --- a/src/main/java/fr/pandacube/util/orm/SQLWhereLike.java +++ b/src/main/java/fr/pandacube/util/orm/SQLWhereLike.java @@ -5,7 +5,7 @@ import java.util.List; import org.javatuples.Pair; -public class SQLWhereLike extends SQLWhere { +/* package */ class SQLWhereLike extends SQLWhere { private SQLField 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 f, String like) { + /* package */ SQLWhereLike(SQLField f, String like) { if (f == null || like == null) throw new IllegalArgumentException("All arguments for SQLWhereLike constructor can't be null"); field = f; diff --git a/src/main/java/fr/pandacube/util/orm/SQLWhereNull.java b/src/main/java/fr/pandacube/util/orm/SQLWhereNull.java index 28d7f4c..b8cb409 100644 --- a/src/main/java/fr/pandacube/util/orm/SQLWhereNull.java +++ b/src/main/java/fr/pandacube/util/orm/SQLWhereNull.java @@ -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() + "#" diff --git a/src/main/java/fr/pandacube/util/orm/SQLWhereOr.java b/src/main/java/fr/pandacube/util/orm/SQLWhereOr.java new file mode 100644 index 0000000..d64a409 --- /dev/null +++ b/src/main/java/fr/pandacube/util/orm/SQLWhereOr.java @@ -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); + } + +}