Make SQLWhere and SQLOrderBy generic

This commit is contained in:
Marc Baloup 2020-03-05 20:04:34 +01:00
parent f510099068
commit 70ee5a6b39
14 changed files with 101 additions and 109 deletions

View File

@ -5,6 +5,7 @@ import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -14,7 +15,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.SQLWhereComp.SQLComparator;
/** /**
* <b>ORM = Object-Relational Mapping</b> * <b>ORM = Object-Relational Mapping</b>
@ -102,39 +102,35 @@ public final class ORM {
return (SQLField<E, Integer>) SQLElement.fieldsCache.get(elemClass).get("id"); return (SQLField<E, Integer>) SQLElement.fieldsCache.get(elemClass).get("id");
} }
public static <E extends SQLElement<E>> SQLElementList<E> getByIds(Class<E> elemClass, Collection<Integer> ids) public static <E extends SQLElement<E>> SQLElementList<E> getByIds(Class<E> elemClass, Integer... ids) throws ORMException {
throws ORMException { return getByIds(elemClass, Arrays.asList(ids));
return getByIds(elemClass, ids.toArray(new Integer[ids.size()]));
} }
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, Collection<Integer> ids)
SQLField<E, Integer> idField = getSQLIdField(elemClass); throws ORMException {
SQLWhereChain where = new SQLWhereOr(); return getAll(elemClass, getSQLIdField(elemClass).in(ids), SQLOrderBy.asc(getSQLIdField(elemClass)), 1, null);
for (Integer id : ids)
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 { public static <E extends SQLElement<E>> E getById(Class<E> elemClass, int id) throws ORMException {
return getFirst(elemClass, new SQLWhereComp(getSQLIdField(elemClass), SQLComparator.EQ, id)); return getFirst(elemClass, getSQLIdField(elemClass).eq(id));
} }
public static <E extends SQLElement<E>> E getFirst(Class<E> elemClass, SQLWhere where) public static <E extends SQLElement<E>> E getFirst(Class<E> elemClass, SQLWhere<E> where)
throws ORMException { throws ORMException {
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) public static <E extends SQLElement<E>> E getFirst(Class<E> elemClass, SQLOrderBy<E> orderBy)
throws ORMException { throws ORMException {
return getFirst(elemClass, null, orderBy, null); 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<E> where, SQLOrderBy<E> orderBy)
throws ORMException { throws ORMException {
return getFirst(elemClass, where, orderBy, null); return getFirst(elemClass, where, orderBy, null);
} }
public static <E extends SQLElement<E>> E getFirst(Class<E> elemClass, SQLWhere where, SQLOrderBy orderBy, Integer offset) public static <E extends SQLElement<E>> E getFirst(Class<E> elemClass, SQLWhere<E> where, SQLOrderBy<E> orderBy, Integer offset)
throws ORMException { throws ORMException {
SQLElementList<E> elts = getAll(elemClass, where, orderBy, 1, offset); SQLElementList<E> elts = getAll(elemClass, where, orderBy, 1, offset);
return (elts.size() == 0) ? null : elts.get(0); return (elts.size() == 0) ? null : elts.get(0);
@ -144,22 +140,22 @@ public final class ORM {
return getAll(elemClass, null, null, null, null); return getAll(elemClass, null, null, null, null);
} }
public static <E extends SQLElement<E>> SQLElementList<E> getAll(Class<E> elemClass, SQLWhere where) throws ORMException { public static <E extends SQLElement<E>> SQLElementList<E> getAll(Class<E> elemClass, SQLWhere<E> where) throws ORMException {
return getAll(elemClass, where, null, null, null); return getAll(elemClass, where, null, null, null);
} }
public static <E extends SQLElement<E>> SQLElementList<E> getAll(Class<E> elemClass, SQLWhere where, public static <E extends SQLElement<E>> SQLElementList<E> getAll(Class<E> elemClass, SQLWhere<E> where,
SQLOrderBy orderBy) throws ORMException { SQLOrderBy<E> orderBy) throws ORMException {
return getAll(elemClass, where, orderBy, null, null); return getAll(elemClass, where, orderBy, null, null);
} }
public static <E extends SQLElement<E>> SQLElementList<E> getAll(Class<E> elemClass, SQLWhere where, public static <E extends SQLElement<E>> SQLElementList<E> getAll(Class<E> elemClass, SQLWhere<E> where,
SQLOrderBy orderBy, Integer limit) throws ORMException { SQLOrderBy<E> orderBy, Integer limit) throws ORMException {
return getAll(elemClass, where, orderBy, limit, null); return getAll(elemClass, where, orderBy, limit, null);
} }
public static <E extends SQLElement<E>> SQLElementList<E> getAll(Class<E> elemClass, SQLWhere where, public static <E extends SQLElement<E>> SQLElementList<E> getAll(Class<E> elemClass, SQLWhere<E> where,
SQLOrderBy orderBy, Integer limit, Integer offset) throws ORMException { SQLOrderBy<E> orderBy, Integer limit, Integer offset) throws ORMException {
SQLElementList<E> elmts = new SQLElementList<>(); SQLElementList<E> elmts = new SQLElementList<>();
forEach(elemClass, where, orderBy, limit, offset, elmts::add); forEach(elemClass, where, orderBy, limit, offset, elmts::add);
return elmts; return elmts;
@ -169,23 +165,23 @@ public final class ORM {
forEach(elemClass, null, null, null, null, action); forEach(elemClass, null, null, null, null, action);
} }
public static <E extends SQLElement<E>> void forEach(Class<E> elemClass, SQLWhere where, public static <E extends SQLElement<E>> void forEach(Class<E> elemClass, SQLWhere<E> where,
Consumer<E> action) throws ORMException { Consumer<E> action) throws ORMException {
forEach(elemClass, where, null, null, null, action); forEach(elemClass, where, null, null, null, action);
} }
public static <E extends SQLElement<E>> void forEach(Class<E> elemClass, SQLWhere where, public static <E extends SQLElement<E>> void forEach(Class<E> elemClass, SQLWhere<E> where,
SQLOrderBy orderBy, Consumer<E> action) throws ORMException { SQLOrderBy<E> orderBy, Consumer<E> action) throws ORMException {
forEach(elemClass, where, orderBy, null, null, action); forEach(elemClass, where, orderBy, null, null, action);
} }
public static <E extends SQLElement<E>> void forEach(Class<E> elemClass, SQLWhere where, public static <E extends SQLElement<E>> void forEach(Class<E> elemClass, SQLWhere<E> where,
SQLOrderBy orderBy, Integer limit, Consumer<E> action) throws ORMException { SQLOrderBy<E> orderBy, Integer limit, Consumer<E> action) throws ORMException {
forEach(elemClass, where, orderBy, limit, null, action); forEach(elemClass, where, orderBy, limit, null, action);
} }
public static <E extends SQLElement<E>> void forEach(Class<E> elemClass, SQLWhere where, public static <E extends SQLElement<E>> void forEach(Class<E> elemClass, SQLWhere<E> where,
SQLOrderBy orderBy, Integer limit, Integer offset, Consumer<E> action) throws ORMException { SQLOrderBy<E> orderBy, Integer limit, Integer offset, Consumer<E> action) throws ORMException {
initTable(elemClass); initTable(elemClass);
try { try {
@ -221,7 +217,7 @@ public final class ORM {
return count(elemClass, null); return count(elemClass, null);
} }
public static <E extends SQLElement<E>> long count(Class<E> elemClass, SQLWhere where) throws ORMException { public static <E extends SQLElement<E>> long count(Class<E> elemClass, SQLWhere<E> where) throws ORMException {
initTable(elemClass); initTable(elemClass);
try { try {
@ -276,11 +272,11 @@ public final class ORM {
public static <E extends SQLElement<E>> SQLUpdate<E> update(Class<E> elemClass, SQLWhere where) throws ORMException { public static <E extends SQLElement<E>> SQLUpdate<E> update(Class<E> elemClass, SQLWhere<E> where) throws ORMException {
return new SQLUpdate<>(elemClass, where); return new SQLUpdate<>(elemClass, where);
} }
/* package */ static <E extends SQLElement<E>> int update(Class<E> elemClass, SQLWhere where, Map<SQLField<E, ?>, Object> values) throws ORMException { /* package */ static <E extends SQLElement<E>> int update(Class<E> elemClass, SQLWhere<E> where, Map<SQLField<E, ?>, Object> values) throws ORMException {
return new SQLUpdate<>(elemClass, where, values).execute(); return new SQLUpdate<>(elemClass, where, values).execute();
} }
@ -292,7 +288,7 @@ public final class ORM {
* @return The return value of {@link PreparedStatement#executeUpdate()}, for an SQL query {@code DELETE}. * @return The return value of {@link PreparedStatement#executeUpdate()}, for an SQL query {@code DELETE}.
* @throws ORMException * @throws ORMException
*/ */
public static <E extends SQLElement<E>> int delete(Class<E> elemClass, SQLWhere where) throws ORMException { public static <E extends SQLElement<E>> int delete(Class<E> elemClass, SQLWhere<E> where) throws ORMException {
initTable(elemClass); initTable(elemClass);
if (where == null) { if (where == null) {

View File

@ -21,7 +21,6 @@ import com.google.gson.Gson;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import fr.pandacube.util.Log; import fr.pandacube.util.Log;
import fr.pandacube.util.orm.SQLWhereComp.SQLComparator;
public abstract class SQLElement<E extends SQLElement<E>> { public abstract class SQLElement<E extends SQLElement<E>> {
/** cache for fields for each subclass of SQLElement */ /** cache for fields for each subclass of SQLElement */
@ -190,8 +189,7 @@ public abstract class SQLElement<E extends SQLElement<E>> {
public <T, P extends SQLElement<P>> P getReferencedEntry(SQLFKField<E, T, P> field) throws ORMException { public <T, P extends SQLElement<P>> P getReferencedEntry(SQLFKField<E, T, P> field) throws ORMException {
T fkValue = get(field); T fkValue = get(field);
if (fkValue == null) return null; if (fkValue == null) return null;
return ORM.getFirst(field.getForeignElementClass(), return ORM.getFirst(field.getForeignElementClass(), field.getPrimaryField().eq(fkValue), null);
new SQLWhereComp(field.getPrimaryField(), SQLComparator.EQ, fkValue), null);
} }
/** /**
@ -199,11 +197,10 @@ public abstract class SQLElement<E extends SQLElement<E>> {
* @param <F> the table class of the foreign key that reference a primary key of this element. * @param <F> the table class of the foreign key that reference a primary key of this element.
* @return all elements in the table F for which the specified foreign key value correspond to the primary key of this element. * @return all elements in the table F for which the specified foreign key value correspond to the primary key of this element.
*/ */
public <T, F extends SQLElement<F>> SQLElementList<F> getReferencingForeignEntries(SQLFKField<F, T, E> field, SQLOrderBy orderBy, Integer limit, Integer offset) throws ORMException { public <T, F extends SQLElement<F>> SQLElementList<F> getReferencingForeignEntries(SQLFKField<F, T, E> field, SQLOrderBy<F> orderBy, Integer limit, Integer offset) throws ORMException {
T value = get(field.getPrimaryField()); T value = get(field.getPrimaryField());
if (value == null) return new SQLElementList<>(); if (value == null) return new SQLElementList<>();
return ORM.getAll(field.getSQLElementType(), return ORM.getAll(field.getSQLElementType(), field.eq(value), orderBy, limit, offset);
new SQLWhereComp(field, SQLComparator.EQ, value), orderBy, limit, offset);
} }
public boolean isValidForSave() { public boolean isValidForSave() {
@ -241,7 +238,7 @@ public abstract class SQLElement<E extends SQLElement<E>> {
if (modifiedValues.isEmpty()) return; if (modifiedValues.isEmpty()) return;
ORM.update((Class<E>)getClass(), new SQLWhereComp(getFieldId(), SQLComparator.EQ, getId()), modifiedValues); ORM.update((Class<E>)getClass(), getFieldId().eq(getId()), modifiedValues);
} }
else { // ajouter dans la base else { // ajouter dans la base

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.SQLWhereComp.SQLComparator;
/** /**
* *
@ -85,8 +84,7 @@ public class SQLElementList<E extends SQLElement<E>> extends ArrayList<E> {
Class<E> classEl = (Class<E>)storedEl.get(0).getClass(); Class<E> classEl = (Class<E>)storedEl.get(0).getClass();
int ret = ORM.update(classEl, int ret = ORM.update(classEl,
new SQLWhereIn(storedEl.get(0).getFieldId(), storedEl.get(0).getFieldId().in(storedEl.stream().map(SQLElement::getId).collect(Collectors.toList())
storedEl.stream().map(SQLElement::getId).collect(Collectors.toList())
), ),
modifiedValues); modifiedValues);
@ -140,7 +138,7 @@ public class SQLElementList<E extends SQLElement<E>> extends ArrayList<E> {
public <T, P extends SQLElement<P>> SQLElementList<P> getReferencedEntries(SQLFKField<E, T, P> foreignKey, SQLOrderBy orderBy) throws ORMException { public <T, P extends SQLElement<P>> SQLElementList<P> getReferencedEntries(SQLFKField<E, T, P> foreignKey, SQLOrderBy<P> orderBy) throws ORMException {
Set<T> values = new HashSet<>(); Set<T> values = new HashSet<>();
forEach(v -> { forEach(v -> {
T val = v.get(foreignKey); T val = v.get(foreignKey);
@ -152,8 +150,8 @@ public class SQLElementList<E extends SQLElement<E>> extends ArrayList<E> {
return new SQLElementList<>(); return new SQLElementList<>();
} }
SQLWhereChain where = SQLWhereChain.or(); SQLWhereOr<P> where = SQLWhere.or();
values.forEach(v -> where.or(new SQLWhereComp(foreignKey.getPrimaryField(), SQLComparator.EQ, v))); values.forEach(v -> where.or(foreignKey.getPrimaryField().eq(v)));
return ORM.getAll(foreignKey.getForeignElementClass(), where, orderBy, null, null); return ORM.getAll(foreignKey.getForeignElementClass(), where, orderBy, null, null);
@ -171,7 +169,7 @@ public class SQLElementList<E extends SQLElement<E>> extends ArrayList<E> {
public <T, F extends SQLElement<F>> SQLElementList<F> getReferencingForeignEntries(SQLFKField<F, T, E> foreignKey, SQLOrderBy orderBy, Integer limit, Integer offset) throws ORMException { public <T, F extends SQLElement<F>> SQLElementList<F> getReferencingForeignEntries(SQLFKField<F, T, E> foreignKey, SQLOrderBy<F> orderBy, Integer limit, Integer offset) throws ORMException {
Set<T> values = new HashSet<>(); Set<T> values = new HashSet<>();
forEach(v -> { forEach(v -> {
T val = v.get(foreignKey.getPrimaryField()); T val = v.get(foreignKey.getPrimaryField());
@ -183,15 +181,15 @@ public class SQLElementList<E extends SQLElement<E>> extends ArrayList<E> {
return new SQLElementList<>(); return new SQLElementList<>();
} }
SQLWhereChain where = SQLWhere.or(); SQLWhereOr<F> where = SQLWhere.or();
values.forEach(v -> where.or(new SQLWhereComp(foreignKey, SQLComparator.EQ, v))); values.forEach(v -> where.or(foreignKey.eq(v)));
return ORM.getAll(foreignKey.getSQLElementType(), where, orderBy, limit, offset); return ORM.getAll(foreignKey.getSQLElementType(), where, orderBy, limit, offset);
} }
public <T, F extends SQLElement<F>> Map<T, SQLElementList<F>> getReferencingForeignEntriesInGroups(SQLFKField<F, T, E> foreignKey, SQLOrderBy orderBy, Integer limit, Integer offset) throws ORMException { public <T, F extends SQLElement<F>> Map<T, SQLElementList<F>> getReferencingForeignEntriesInGroups(SQLFKField<F, T, E> foreignKey, SQLOrderBy<F> orderBy, Integer limit, Integer offset) throws ORMException {
SQLElementList<F> foreignElements = getReferencingForeignEntries(foreignKey, orderBy, limit, offset); SQLElementList<F> foreignElements = getReferencingForeignEntries(foreignKey, orderBy, limit, offset);
Map<T, SQLElementList<F>> map = new HashMap<>(); Map<T, SQLElementList<F>> map = new HashMap<>();

View File

@ -92,43 +92,43 @@ public class SQLField<E extends SQLElement<E>, T> {
public SQLWhere eq(T r) { public SQLWhere<E> eq(T r) {
return comp(SQLComparator.EQ, r); return comp(SQLComparator.EQ, r);
} }
public SQLWhere geq(T r) { public SQLWhere<E> geq(T r) {
return comp(SQLComparator.GEQ, r); return comp(SQLComparator.GEQ, r);
} }
public SQLWhere gt(T r) { public SQLWhere<E> gt(T r) {
return comp(SQLComparator.GT, r); return comp(SQLComparator.GT, r);
} }
public SQLWhere leq(T r) { public SQLWhere<E> leq(T r) {
return comp(SQLComparator.LEQ, r); return comp(SQLComparator.LEQ, r);
} }
public SQLWhere lt(T r) { public SQLWhere<E> lt(T r) {
return comp(SQLComparator.LT, r); return comp(SQLComparator.LT, r);
} }
public SQLWhere neq(T r) { public SQLWhere<E> neq(T r) {
return comp(SQLComparator.NEQ, r); return comp(SQLComparator.NEQ, r);
} }
private SQLWhere comp(SQLComparator c, T r) { private SQLWhere<E> comp(SQLComparator c, T r) {
return new SQLWhereComp(this, c, r); return new SQLWhereComp<>(this, c, r);
} }
public SQLWhere in(Collection<T> v) { public SQLWhere<E> in(Collection<T> v) {
return new SQLWhereIn(this, v); return new SQLWhereIn<>(this, v);
} }
public SQLWhere isNull() { public SQLWhere<E> isNull() {
return new SQLWhereNull(this, true); return new SQLWhereNull<>(this, true);
} }
public SQLWhere isNotNull() { public SQLWhere<E> isNotNull() {
return new SQLWhereNull(this, false); return new SQLWhereNull<>(this, false);
} }
} }

View File

@ -3,7 +3,7 @@ package fr.pandacube.util.orm;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class SQLOrderBy { public class SQLOrderBy<E extends SQLElement<E>> {
private List<OBField> orderByFields = new ArrayList<>(); private List<OBField> orderByFields = new ArrayList<>();
@ -19,7 +19,7 @@ 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)
*/ */
private SQLOrderBy add(SQLField<?, ?> field, Direction d) { private SQLOrderBy<E> add(SQLField<E, ?> field, Direction d) {
orderByFields.add(new OBField(field, d)); orderByFields.add(new OBField(field, d));
return this; return this;
} }
@ -30,7 +30,7 @@ public class SQLOrderBy {
* @param field le champ SQL à ordonner * @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 thenAsc(SQLField<?, ?> field) { public SQLOrderBy<E> thenAsc(SQLField<E, ?> field) {
return add(field, Direction.ASC); return add(field, Direction.ASC);
} }
@ -40,7 +40,7 @@ public class SQLOrderBy {
* @param field le champ SQL à ordonner * @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 thenDesc(SQLField<?, ?> field) { public SQLOrderBy<E> thenDesc(SQLField<E, ?> field) {
return add(field, Direction.DESC); return add(field, Direction.DESC);
} }
@ -61,10 +61,10 @@ public class SQLOrderBy {
} }
private class OBField { private class OBField {
public final SQLField<?, ?> field; public final SQLField<E, ?> field;
public final Direction direction; public final Direction direction;
public OBField(SQLField<?, ?> f, Direction d) { public OBField(SQLField<E, ?> f, Direction d) {
field = f; field = f;
direction = d; direction = d;
} }
@ -83,12 +83,12 @@ public class SQLOrderBy {
public static SQLOrderBy asc(SQLField<?, ?> field) { public static <E extends SQLElement<E>> SQLOrderBy<E> asc(SQLField<E, ?> field) {
return new SQLOrderBy().thenAsc(field); return new SQLOrderBy<E>().thenAsc(field);
} }
public static SQLOrderBy desc(SQLField<?, ?> field) { public static <E extends SQLElement<E>> SQLOrderBy<E> desc(SQLField<E, ?> field) {
return new SQLOrderBy().thenDesc(field); return new SQLOrderBy<E>().thenDesc(field);
} }

View File

@ -12,16 +12,16 @@ import fr.pandacube.util.Log;
public class SQLUpdate<E extends SQLElement<E>> { public class SQLUpdate<E extends SQLElement<E>> {
private final Class<E> elemClass; private final Class<E> elemClass;
private final SQLWhere where; private final SQLWhere<E> where;
private final Map<SQLField<E, ?>, Object> values; private final Map<SQLField<E, ?>, Object> values;
/* package */ SQLUpdate(Class<E> el, SQLWhere w) { /* package */ SQLUpdate(Class<E> el, SQLWhere<E> w) {
elemClass = el; elemClass = el;
where = w; where = w;
values = new HashMap<>(); values = new HashMap<>();
} }
/* package */ SQLUpdate(Class<E> el, SQLWhere w, Map<SQLField<E, ?>, Object> v) { /* package */ SQLUpdate(Class<E> el, SQLWhere<E> w, Map<SQLField<E, ?>, Object> v) {
elemClass = el; elemClass = el;
where = w; where = w;
values = v; values = v;

View File

@ -6,7 +6,7 @@ import org.javatuples.Pair;
import fr.pandacube.util.Log; import fr.pandacube.util.Log;
public abstract class SQLWhere { public abstract class SQLWhere<E extends SQLElement<E>> {
public abstract Pair<String, List<Object>> toSQL() throws ORMException; public abstract Pair<String, List<Object>> toSQL() throws ORMException;
@ -20,26 +20,26 @@ public abstract class SQLWhere {
} }
} }
public SQLWhereAnd and(SQLWhere other) { public SQLWhereAnd<E> and(SQLWhere<E> other) {
return and().and(this).and(other); return new SQLWhereAnd<E>().and(this).and(other);
} }
public SQLWhereOr or(SQLWhere other) { public SQLWhereOr<E> or(SQLWhere<E> other) {
return or().or(this).or(other); return new SQLWhereOr<E>().or(this).or(other);
} }
public static SQLWhereAnd and() { public static <E extends SQLElement<E>> SQLWhereAnd<E> and() {
return new SQLWhereAnd(); return new SQLWhereAnd<>();
} }
public static SQLWhereOr or() { public static <E extends SQLElement<E>> SQLWhereOr<E> or() {
return new SQLWhereOr(); return new SQLWhereOr<>();
} }
public static SQLWhere like(SQLField<?, String> f, String like) { public static <E extends SQLElement<E>> SQLWhere<E> like(SQLField<E, String> f, String like) {
return new SQLWhereLike(f, like); return new SQLWhereLike<E>(f, like);
} }
} }

View File

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

View File

@ -5,20 +5,19 @@ import java.util.List;
import org.javatuples.Pair; import org.javatuples.Pair;
public abstract class SQLWhereChain extends SQLWhere { public abstract class SQLWhereChain<E extends SQLElement<E>> extends SQLWhere<E> {
private SQLBoolOp operator; private SQLBoolOp operator;
protected List<SQLWhere> conditions = new ArrayList<>(); protected List<SQLWhere<E>> conditions = new ArrayList<>();
/* package */ 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;
} }
protected SQLWhereChain add(SQLWhere sqlWhere) { protected void add(SQLWhere<E> 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;
} }
@Override @Override
@ -31,7 +30,7 @@ public abstract class SQLWhereChain extends SQLWhere {
List<Object> params = new ArrayList<>(); List<Object> params = new ArrayList<>();
boolean first = true; boolean first = true;
for (SQLWhere w : conditions) { for (SQLWhere<E> w : conditions) {
if (!first) sql += " " + operator.sql + " "; if (!first) sql += " " + operator.sql + " ";
first = false; first = false;

View File

@ -5,7 +5,7 @@ import java.util.List;
import org.javatuples.Pair; import org.javatuples.Pair;
/* package */ class SQLWhereComp extends SQLWhere { /* package */ class SQLWhereComp<E extends SQLElement<E>> extends SQLWhere<E> {
private SQLField<?, ?> left; private SQLField<?, ?> left;
private SQLComparator comp; private SQLComparator comp;

View File

@ -6,12 +6,12 @@ import java.util.List;
import org.javatuples.Pair; import org.javatuples.Pair;
/* package */ class SQLWhereIn extends SQLWhere { /* package */ class SQLWhereIn<E extends SQLElement<E>> extends SQLWhere<E> {
private SQLField<?, ?> field; private SQLField<E, ?> field;
private Collection<?> values; private Collection<?> values;
/* package */ <T> SQLWhereIn(SQLField<?, T> f, Collection<T> v) { /* package */ <T> SQLWhereIn(SQLField<E, 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,9 +5,9 @@ import java.util.List;
import org.javatuples.Pair; import org.javatuples.Pair;
/* package */ class SQLWhereLike extends SQLWhere { /* package */ class SQLWhereLike<E extends SQLElement<E>> extends SQLWhere<E> {
private SQLField<?, String> field; private SQLField<E, String> field;
private String likeExpr; private String likeExpr;
/** /**
@ -16,7 +16,7 @@ import org.javatuples.Pair;
* @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.
*/ */
/* package */ SQLWhereLike(SQLField<?, String> f, String like) { /* package */ SQLWhereLike(SQLField<E, 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,9 +8,9 @@ import org.javatuples.Pair;
import fr.pandacube.util.Log; import fr.pandacube.util.Log;
/* package */ class SQLWhereNull extends SQLWhere { /* package */ class SQLWhereNull<E extends SQLElement<E>> extends SQLWhere<E> {
private SQLField<?, ?> fild; private SQLField<E, ?> fild;
private boolean nulll; private boolean nulll;
/** /**
@ -20,7 +20,7 @@ import fr.pandacube.util.Log;
* @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"
*/ */
/* package */ SQLWhereNull(SQLField<?, ?> field, boolean isNull) { /* package */ SQLWhereNull(SQLField<E, ?> 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

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