From d0d53ac472704328cd05e9d2603787f3b68d0757 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Fri, 15 Sep 2017 01:33:45 +0200 Subject: [PATCH] SQL ORM Improvement --- src/main/java/fr/pandacube/java/util/orm/ORM.java | 2 +- .../java/fr/pandacube/java/util/orm/SQLCustomType.java | 2 +- src/main/java/fr/pandacube/java/util/orm/SQLElement.java | 9 ++++++++- src/main/java/fr/pandacube/java/util/orm/SQLOrderBy.java | 6 +++--- 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/main/java/fr/pandacube/java/util/orm/ORM.java b/src/main/java/fr/pandacube/java/util/orm/ORM.java index 0bc4479..da34a10 100644 --- a/src/main/java/fr/pandacube/java/util/orm/ORM.java +++ b/src/main/java/fr/pandacube/java/util/orm/ORM.java @@ -108,7 +108,7 @@ public final class ORM { SQLWhereChain where = new SQLWhereChain(SQLBoolOp.OR); for (Integer id : ids) 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 getById(Class elemClass, int id) throws ORMException { diff --git a/src/main/java/fr/pandacube/java/util/orm/SQLCustomType.java b/src/main/java/fr/pandacube/java/util/orm/SQLCustomType.java index f3e2589..a076def 100644 --- a/src/main/java/fr/pandacube/java/util/orm/SQLCustomType.java +++ b/src/main/java/fr/pandacube/java/util/orm/SQLCustomType.java @@ -27,6 +27,6 @@ public class SQLCustomType extends SQLType { } - // tester en local + // TODO tester en local } diff --git a/src/main/java/fr/pandacube/java/util/orm/SQLElement.java b/src/main/java/fr/pandacube/java/util/orm/SQLElement.java index cf05f66..a371af3 100644 --- a/src/main/java/fr/pandacube/java/util/orm/SQLElement.java +++ b/src/main/java/fr/pandacube/java/util/orm/SQLElement.java @@ -172,13 +172,20 @@ public abstract class SQLElement> { + " does not exist or is not set"); } - public > F getForeign(SQLFKField field) throws ORMException { + public > F getForeignKeyTarget(SQLFKField field) throws ORMException { T fkValue = get(field); if (fkValue == null) return null; return ORM.getFirst(field.getForeignElementClass(), new SQLWhereComp(field.getForeignField(), SQLComparator.EQ, fkValue), null); } + public > SQLElementList getForeignKeySources(SQLFKField 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() { return values.keySet().containsAll(fields.values()); } diff --git a/src/main/java/fr/pandacube/java/util/orm/SQLOrderBy.java b/src/main/java/fr/pandacube/java/util/orm/SQLOrderBy.java index f67d8c9..cb85026 100644 --- a/src/main/java/fr/pandacube/java/util/orm/SQLOrderBy.java +++ b/src/main/java/fr/pandacube/java/util/orm/SQLOrderBy.java @@ -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) */ - public SQLOrderBy addField(SQLField field, Direction d) { + public SQLOrderBy add(SQLField field, Direction d) { orderByFields.add(new OBField(field, d)); return this; } @@ -31,8 +31,8 @@ public class SQLOrderBy { * @param field le champ SQL à ordonner dans l'ordre croissant ASC * @return l'objet courant (permet de chainer les ajouts de champs) */ - public SQLOrderBy addField(SQLField field) { - return addField(field, Direction.ASC); + public SQLOrderBy add(SQLField field) { + return add(field, Direction.ASC); } /* package */ String toSQL() {