Make SQLWhere and SQLOrderBy generic
This commit is contained in:
parent
f510099068
commit
70ee5a6b39
@ -5,6 +5,7 @@ import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@ -14,7 +15,6 @@ import java.util.function.Consumer;
|
||||
import org.javatuples.Pair;
|
||||
|
||||
import fr.pandacube.util.Log;
|
||||
import fr.pandacube.util.orm.SQLWhereComp.SQLComparator;
|
||||
|
||||
/**
|
||||
* <b>ORM = Object-Relational Mapping</b>
|
||||
@ -102,39 +102,35 @@ public final class ORM {
|
||||
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)
|
||||
throws ORMException {
|
||||
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 {
|
||||
return getByIds(elemClass, Arrays.asList(ids));
|
||||
}
|
||||
|
||||
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 SQLWhereOr();
|
||||
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>> SQLElementList<E> getByIds(Class<E> elemClass, Collection<Integer> ids)
|
||||
throws ORMException {
|
||||
return getAll(elemClass, getSQLIdField(elemClass).in(ids), SQLOrderBy.asc(getSQLIdField(elemClass)), 1, null);
|
||||
}
|
||||
|
||||
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 {
|
||||
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 {
|
||||
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 {
|
||||
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 {
|
||||
SQLElementList<E> elts = getAll(elemClass, where, orderBy, 1, offset);
|
||||
return (elts.size() == 0) ? null : elts.get(0);
|
||||
@ -144,22 +140,22 @@ public final class ORM {
|
||||
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);
|
||||
}
|
||||
|
||||
public static <E extends SQLElement<E>> SQLElementList<E> getAll(Class<E> elemClass, SQLWhere where,
|
||||
SQLOrderBy orderBy) throws ORMException {
|
||||
public static <E extends SQLElement<E>> SQLElementList<E> getAll(Class<E> elemClass, SQLWhere<E> where,
|
||||
SQLOrderBy<E> orderBy) throws ORMException {
|
||||
return getAll(elemClass, where, orderBy, null, null);
|
||||
}
|
||||
|
||||
public static <E extends SQLElement<E>> SQLElementList<E> getAll(Class<E> elemClass, SQLWhere where,
|
||||
SQLOrderBy orderBy, Integer limit) throws ORMException {
|
||||
public static <E extends SQLElement<E>> SQLElementList<E> getAll(Class<E> elemClass, SQLWhere<E> where,
|
||||
SQLOrderBy<E> orderBy, Integer limit) throws ORMException {
|
||||
return getAll(elemClass, where, orderBy, limit, null);
|
||||
}
|
||||
|
||||
public static <E extends SQLElement<E>> SQLElementList<E> getAll(Class<E> elemClass, SQLWhere where,
|
||||
SQLOrderBy orderBy, Integer limit, Integer offset) throws ORMException {
|
||||
public static <E extends SQLElement<E>> SQLElementList<E> getAll(Class<E> elemClass, SQLWhere<E> where,
|
||||
SQLOrderBy<E> orderBy, Integer limit, Integer offset) throws ORMException {
|
||||
SQLElementList<E> elmts = new SQLElementList<>();
|
||||
forEach(elemClass, where, orderBy, limit, offset, elmts::add);
|
||||
return elmts;
|
||||
@ -169,23 +165,23 @@ public final class ORM {
|
||||
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 {
|
||||
forEach(elemClass, where, null, null, null, action);
|
||||
}
|
||||
|
||||
public static <E extends SQLElement<E>> void forEach(Class<E> elemClass, SQLWhere where,
|
||||
SQLOrderBy orderBy, Consumer<E> action) throws ORMException {
|
||||
public static <E extends SQLElement<E>> void forEach(Class<E> elemClass, SQLWhere<E> where,
|
||||
SQLOrderBy<E> orderBy, Consumer<E> action) throws ORMException {
|
||||
forEach(elemClass, where, orderBy, null, null, action);
|
||||
}
|
||||
|
||||
public static <E extends SQLElement<E>> void forEach(Class<E> elemClass, SQLWhere where,
|
||||
SQLOrderBy orderBy, Integer limit, Consumer<E> action) throws ORMException {
|
||||
public static <E extends SQLElement<E>> void forEach(Class<E> elemClass, SQLWhere<E> where,
|
||||
SQLOrderBy<E> orderBy, Integer limit, Consumer<E> action) throws ORMException {
|
||||
forEach(elemClass, where, orderBy, limit, null, action);
|
||||
}
|
||||
|
||||
public static <E extends SQLElement<E>> void forEach(Class<E> elemClass, SQLWhere where,
|
||||
SQLOrderBy orderBy, Integer limit, Integer offset, Consumer<E> action) throws ORMException {
|
||||
public static <E extends SQLElement<E>> void forEach(Class<E> elemClass, SQLWhere<E> where,
|
||||
SQLOrderBy<E> orderBy, Integer limit, Integer offset, Consumer<E> action) throws ORMException {
|
||||
initTable(elemClass);
|
||||
|
||||
try {
|
||||
@ -221,7 +217,7 @@ public final class ORM {
|
||||
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);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
/* 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();
|
||||
}
|
||||
|
||||
@ -292,7 +288,7 @@ public final class ORM {
|
||||
* @return The return value of {@link PreparedStatement#executeUpdate()}, for an SQL query {@code DELETE}.
|
||||
* @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);
|
||||
|
||||
if (where == null) {
|
||||
|
@ -21,7 +21,6 @@ import com.google.gson.Gson;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import fr.pandacube.util.Log;
|
||||
import fr.pandacube.util.orm.SQLWhereComp.SQLComparator;
|
||||
|
||||
public abstract class SQLElement<E extends SQLElement<E>> {
|
||||
/** 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 {
|
||||
T fkValue = get(field);
|
||||
if (fkValue == null) return null;
|
||||
return ORM.getFirst(field.getForeignElementClass(),
|
||||
new SQLWhereComp(field.getPrimaryField(), SQLComparator.EQ, fkValue), null);
|
||||
return ORM.getFirst(field.getForeignElementClass(), field.getPrimaryField().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.
|
||||
* @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());
|
||||
if (value == null) return new SQLElementList<>();
|
||||
return ORM.getAll(field.getSQLElementType(),
|
||||
new SQLWhereComp(field, SQLComparator.EQ, value), orderBy, limit, offset);
|
||||
return ORM.getAll(field.getSQLElementType(), field.eq(value), orderBy, limit, offset);
|
||||
}
|
||||
|
||||
public boolean isValidForSave() {
|
||||
@ -241,7 +238,7 @@ public abstract class SQLElement<E extends SQLElement<E>> {
|
||||
|
||||
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
|
||||
|
||||
|
@ -14,7 +14,6 @@ import java.util.stream.Collectors;
|
||||
import com.google.gson.JsonArray;
|
||||
|
||||
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();
|
||||
|
||||
int ret = ORM.update(classEl,
|
||||
new SQLWhereIn(storedEl.get(0).getFieldId(),
|
||||
storedEl.stream().map(SQLElement::getId).collect(Collectors.toList())
|
||||
storedEl.get(0).getFieldId().in(storedEl.stream().map(SQLElement::getId).collect(Collectors.toList())
|
||||
),
|
||||
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<>();
|
||||
forEach(v -> {
|
||||
T val = v.get(foreignKey);
|
||||
@ -152,8 +150,8 @@ public class SQLElementList<E extends SQLElement<E>> extends ArrayList<E> {
|
||||
return new SQLElementList<>();
|
||||
}
|
||||
|
||||
SQLWhereChain where = SQLWhereChain.or();
|
||||
values.forEach(v -> where.or(new SQLWhereComp(foreignKey.getPrimaryField(), SQLComparator.EQ, v)));
|
||||
SQLWhereOr<P> where = SQLWhere.or();
|
||||
values.forEach(v -> where.or(foreignKey.getPrimaryField().eq(v)));
|
||||
|
||||
|
||||
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<>();
|
||||
forEach(v -> {
|
||||
T val = v.get(foreignKey.getPrimaryField());
|
||||
@ -183,15 +181,15 @@ public class SQLElementList<E extends SQLElement<E>> extends ArrayList<E> {
|
||||
return new SQLElementList<>();
|
||||
}
|
||||
|
||||
SQLWhereChain where = SQLWhere.or();
|
||||
values.forEach(v -> where.or(new SQLWhereComp(foreignKey, SQLComparator.EQ, v)));
|
||||
SQLWhereOr<F> where = SQLWhere.or();
|
||||
values.forEach(v -> where.or(foreignKey.eq(v)));
|
||||
|
||||
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);
|
||||
|
||||
Map<T, SQLElementList<F>> map = new HashMap<>();
|
||||
|
@ -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);
|
||||
}
|
||||
public SQLWhere geq(T r) {
|
||||
public SQLWhere<E> geq(T r) {
|
||||
return comp(SQLComparator.GEQ, r);
|
||||
}
|
||||
public SQLWhere gt(T r) {
|
||||
public SQLWhere<E> gt(T r) {
|
||||
return comp(SQLComparator.GT, r);
|
||||
}
|
||||
public SQLWhere leq(T r) {
|
||||
public SQLWhere<E> leq(T r) {
|
||||
return comp(SQLComparator.LEQ, r);
|
||||
}
|
||||
public SQLWhere lt(T r) {
|
||||
public SQLWhere<E> lt(T r) {
|
||||
return comp(SQLComparator.LT, r);
|
||||
}
|
||||
public SQLWhere neq(T r) {
|
||||
public SQLWhere<E> neq(T r) {
|
||||
return comp(SQLComparator.NEQ, r);
|
||||
}
|
||||
|
||||
private SQLWhere comp(SQLComparator c, T r) {
|
||||
return new SQLWhereComp(this, c, r);
|
||||
private SQLWhere<E> comp(SQLComparator c, T r) {
|
||||
return new SQLWhereComp<>(this, c, r);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public SQLWhere in(Collection<T> v) {
|
||||
return new SQLWhereIn(this, v);
|
||||
public SQLWhere<E> in(Collection<T> v) {
|
||||
return new SQLWhereIn<>(this, v);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public SQLWhere isNull() {
|
||||
return new SQLWhereNull(this, true);
|
||||
public SQLWhere<E> isNull() {
|
||||
return new SQLWhereNull<>(this, true);
|
||||
}
|
||||
|
||||
public SQLWhere isNotNull() {
|
||||
return new SQLWhereNull(this, false);
|
||||
public SQLWhere<E> isNotNull() {
|
||||
return new SQLWhereNull<>(this, false);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ package fr.pandacube.util.orm;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class SQLOrderBy {
|
||||
public class SQLOrderBy<E extends SQLElement<E>> {
|
||||
|
||||
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)
|
||||
* @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));
|
||||
return this;
|
||||
}
|
||||
@ -30,7 +30,7 @@ public class SQLOrderBy {
|
||||
* @param field le champ SQL à ordonner
|
||||
* @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);
|
||||
}
|
||||
|
||||
@ -40,7 +40,7 @@ public class SQLOrderBy {
|
||||
* @param field le champ SQL à ordonner
|
||||
* @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);
|
||||
}
|
||||
|
||||
@ -61,10 +61,10 @@ public class SQLOrderBy {
|
||||
}
|
||||
|
||||
private class OBField {
|
||||
public final SQLField<?, ?> field;
|
||||
public final SQLField<E, ?> field;
|
||||
public final Direction direction;
|
||||
|
||||
public OBField(SQLField<?, ?> f, Direction d) {
|
||||
public OBField(SQLField<E, ?> f, Direction d) {
|
||||
field = f;
|
||||
direction = d;
|
||||
}
|
||||
@ -83,12 +83,12 @@ public class SQLOrderBy {
|
||||
|
||||
|
||||
|
||||
public static SQLOrderBy asc(SQLField<?, ?> field) {
|
||||
return new SQLOrderBy().thenAsc(field);
|
||||
public static <E extends SQLElement<E>> SQLOrderBy<E> asc(SQLField<E, ?> field) {
|
||||
return new SQLOrderBy<E>().thenAsc(field);
|
||||
}
|
||||
|
||||
public static SQLOrderBy desc(SQLField<?, ?> field) {
|
||||
return new SQLOrderBy().thenDesc(field);
|
||||
public static <E extends SQLElement<E>> SQLOrderBy<E> desc(SQLField<E, ?> field) {
|
||||
return new SQLOrderBy<E>().thenDesc(field);
|
||||
}
|
||||
|
||||
|
||||
|
@ -12,16 +12,16 @@ import fr.pandacube.util.Log;
|
||||
public class SQLUpdate<E extends SQLElement<E>> {
|
||||
|
||||
private final Class<E> elemClass;
|
||||
private final SQLWhere where;
|
||||
private final SQLWhere<E> where;
|
||||
private final Map<SQLField<E, ?>, Object> values;
|
||||
|
||||
/* package */ SQLUpdate(Class<E> el, SQLWhere w) {
|
||||
/* package */ SQLUpdate(Class<E> el, SQLWhere<E> w) {
|
||||
elemClass = el;
|
||||
where = w;
|
||||
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;
|
||||
where = w;
|
||||
values = v;
|
||||
|
@ -6,7 +6,7 @@ import org.javatuples.Pair;
|
||||
|
||||
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;
|
||||
|
||||
@ -20,26 +20,26 @@ public abstract class SQLWhere {
|
||||
}
|
||||
}
|
||||
|
||||
public SQLWhereAnd and(SQLWhere other) {
|
||||
return and().and(this).and(other);
|
||||
public SQLWhereAnd<E> and(SQLWhere<E> other) {
|
||||
return new SQLWhereAnd<E>().and(this).and(other);
|
||||
}
|
||||
|
||||
public SQLWhereOr or(SQLWhere other) {
|
||||
return or().or(this).or(other);
|
||||
public SQLWhereOr<E> or(SQLWhere<E> other) {
|
||||
return new SQLWhereOr<E>().or(this).or(other);
|
||||
}
|
||||
|
||||
public static SQLWhereAnd and() {
|
||||
return new SQLWhereAnd();
|
||||
public static <E extends SQLElement<E>> SQLWhereAnd<E> and() {
|
||||
return new SQLWhereAnd<>();
|
||||
}
|
||||
|
||||
public static SQLWhereOr or() {
|
||||
return new SQLWhereOr();
|
||||
public static <E extends SQLElement<E>> SQLWhereOr<E> or() {
|
||||
return new SQLWhereOr<>();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static SQLWhere like(SQLField<?, String> f, String like) {
|
||||
return new SQLWhereLike(f, like);
|
||||
public static <E extends SQLElement<E>> SQLWhere<E> like(SQLField<E, String> f, String like) {
|
||||
return new SQLWhereLike<E>(f, like);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,14 +1,15 @@
|
||||
package fr.pandacube.util.orm;
|
||||
|
||||
public class SQLWhereAnd extends SQLWhereChain {
|
||||
public class SQLWhereAnd<E extends SQLElement<E>> extends SQLWhereChain<E> {
|
||||
|
||||
/* package */ SQLWhereAnd() {
|
||||
super(SQLBoolOp.AND);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SQLWhereAnd and(SQLWhere other) {
|
||||
return (SQLWhereAnd) add(other);
|
||||
public SQLWhereAnd<E> and(SQLWhere<E> other) {
|
||||
add(other);
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -5,20 +5,19 @@ import java.util.List;
|
||||
|
||||
import org.javatuples.Pair;
|
||||
|
||||
public abstract class SQLWhereChain extends SQLWhere {
|
||||
public abstract class SQLWhereChain<E extends SQLElement<E>> extends SQLWhere<E> {
|
||||
|
||||
private SQLBoolOp operator;
|
||||
protected List<SQLWhere> conditions = new ArrayList<>();
|
||||
protected List<SQLWhere<E>> conditions = new ArrayList<>();
|
||||
|
||||
/* package */ SQLWhereChain(SQLBoolOp op) {
|
||||
if (op == null) throw new IllegalArgumentException("op can't be null");
|
||||
operator = op;
|
||||
}
|
||||
|
||||
protected SQLWhereChain add(SQLWhere sqlWhere) {
|
||||
protected void add(SQLWhere<E> sqlWhere) {
|
||||
if (sqlWhere == null) throw new IllegalArgumentException("sqlWhere can't be null");
|
||||
conditions.add(sqlWhere);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -31,7 +30,7 @@ public abstract class SQLWhereChain extends SQLWhere {
|
||||
List<Object> params = new ArrayList<>();
|
||||
boolean first = true;
|
||||
|
||||
for (SQLWhere w : conditions) {
|
||||
for (SQLWhere<E> w : conditions) {
|
||||
if (!first) sql += " " + operator.sql + " ";
|
||||
first = false;
|
||||
|
||||
|
@ -5,7 +5,7 @@ import java.util.List;
|
||||
|
||||
import org.javatuples.Pair;
|
||||
|
||||
/* package */ class SQLWhereComp extends SQLWhere {
|
||||
/* package */ class SQLWhereComp<E extends SQLElement<E>> extends SQLWhere<E> {
|
||||
|
||||
private SQLField<?, ?> left;
|
||||
private SQLComparator comp;
|
||||
|
@ -6,12 +6,12 @@ import java.util.List;
|
||||
|
||||
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;
|
||||
|
||||
/* package */ <T> SQLWhereIn(SQLField<?, T> f, Collection<T> v) {
|
||||
/* package */ <T> SQLWhereIn(SQLField<E, T> f, Collection<T> v) {
|
||||
if (f == null || v == null)
|
||||
throw new IllegalArgumentException("All arguments for SQLWhereIn constructor can't be null");
|
||||
field = f;
|
||||
|
@ -5,9 +5,9 @@ import java.util.List;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
@ -16,7 +16,7 @@ import org.javatuples.Pair;
|
||||
* @param f the field at left of the LIKE keyword. Can't be null
|
||||
* @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)
|
||||
throw new IllegalArgumentException("All arguments for SQLWhereLike constructor can't be null");
|
||||
field = f;
|
||||
|
@ -8,9 +8,9 @@ import org.javatuples.Pair;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
@ -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
|
||||
* "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.canBeNull) Log.getLogger().log(Level.WARNING,
|
||||
"Useless : Trying to check IS [NOT] NULL on the field " + field.getSQLElementType().getName() + "#"
|
||||
|
@ -1,14 +1,15 @@
|
||||
package fr.pandacube.util.orm;
|
||||
|
||||
public class SQLWhereOr extends SQLWhereChain {
|
||||
public class SQLWhereOr<E extends SQLElement<E>> extends SQLWhereChain<E> {
|
||||
|
||||
/* package */ SQLWhereOr() {
|
||||
super(SQLBoolOp.OR);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SQLWhereOr or(SQLWhere other) {
|
||||
return (SQLWhereOr) add(other);
|
||||
public SQLWhereOr<E> or(SQLWhere<E> other) {
|
||||
add(other);
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user