SQL ORM Improvement

This commit is contained in:
Marc Baloup 2017-09-15 01:33:45 +02:00
parent 0391b7a9a0
commit d0d53ac472
4 changed files with 13 additions and 6 deletions

View File

@ -108,7 +108,7 @@ public final class ORM {
SQLWhereChain where = new SQLWhereChain(SQLBoolOp.OR); SQLWhereChain where = new SQLWhereChain(SQLBoolOp.OR);
for (Integer id : ids) for (Integer id : ids)
if (id != null) where.add(new SQLWhereComp(idField, SQLComparator.EQ, id)); if (id != null) where.add(new SQLWhereComp(idField, SQLComparator.EQ, id));
return getAll(elemClass, where, new SQLOrderBy().addField(idField), 1, null); return getAll(elemClass, where, new SQLOrderBy().add(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 {

View File

@ -27,6 +27,6 @@ public class SQLCustomType<IT, JT> extends SQLType<JT> {
} }
// tester en local // TODO tester en local
} }

View File

@ -172,13 +172,20 @@ public abstract class SQLElement<E extends SQLElement<E>> {
+ " does not exist or is not set"); + " does not exist or is not set");
} }
public <T, F extends SQLElement<F>> F getForeign(SQLFKField<E, T, F> field) throws ORMException { public <T, F extends SQLElement<F>> F getForeignKeyTarget(SQLFKField<E, T, F> 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(),
new SQLWhereComp(field.getForeignField(), SQLComparator.EQ, fkValue), null); new SQLWhereComp(field.getForeignField(), SQLComparator.EQ, fkValue), null);
} }
public <T, S extends SQLElement<S>> SQLElementList<S> getForeignKeySources(SQLFKField<S, T, E> field, SQLOrderBy orderBy, Integer limit, Integer offset) throws ORMException {
T value = get(field.getForeignField());
if (value == null) return new SQLElementList<>();
return ORM.getAll(field.getSQLElementType(),
new SQLWhereComp(field, SQLComparator.EQ, value), orderBy, limit, offset);
}
public boolean isValidForSave() { public boolean isValidForSave() {
return values.keySet().containsAll(fields.values()); return values.keySet().containsAll(fields.values());
} }

View File

@ -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)
*/ */
public SQLOrderBy addField(SQLField<?, ?> field, Direction d) { public SQLOrderBy add(SQLField<?, ?> field, Direction d) {
orderByFields.add(new OBField(field, d)); orderByFields.add(new OBField(field, d));
return this; return this;
} }
@ -31,8 +31,8 @@ public class SQLOrderBy {
* @param field le champ SQL à ordonner dans l'ordre croissant ASC * @param field le champ SQL à ordonner dans l'ordre croissant ASC
* @return l'objet courant (permet de chainer les ajouts de champs) * @return l'objet courant (permet de chainer les ajouts de champs)
*/ */
public SQLOrderBy addField(SQLField<?, ?> field) { public SQLOrderBy add(SQLField<?, ?> field) {
return addField(field, Direction.ASC); return add(field, Direction.ASC);
} }
/* package */ String toSQL() { /* package */ String toSQL() {