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 394b7dd..c857212 100644 --- a/src/main/java/fr/pandacube/java/util/orm/SQLElement.java +++ b/src/main/java/fr/pandacube/java/util/orm/SQLElement.java @@ -171,14 +171,24 @@ public abstract class SQLElement> { + " does not exist or is not set"); } - public > F getForeignKeyTarget(SQLFKField field) throws ORMException { + /** + * @param the type of the specified field + * @param

the table class of the primary key targeted by the specified foreign key field + * @return the element in the table P that his primary key correspond to the foreign key value of this element. + */ + public > P getReferencedEntry(SQLFKField 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); } - public > SQLElementList getForeignKeySources(SQLFKField field, SQLOrderBy orderBy, Integer limit, Integer offset) throws ORMException { + /** + * @param the type of the specified field + * @param 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 > SQLElementList getReferencingForeignEntries(SQLFKField field, SQLOrderBy orderBy, Integer limit, Integer offset) throws ORMException { T value = get(field.getPrimaryField()); if (value == null) return new SQLElementList<>(); return ORM.getAll(field.getSQLElementType(), diff --git a/src/main/java/fr/pandacube/java/util/orm/SQLElementList.java b/src/main/java/fr/pandacube/java/util/orm/SQLElementList.java index b54351f..a82aaa9 100644 --- a/src/main/java/fr/pandacube/java/util/orm/SQLElementList.java +++ b/src/main/java/fr/pandacube/java/util/orm/SQLElementList.java @@ -161,7 +161,7 @@ public class SQLElementList> extends ArrayList { - public > Map getAllForeign(SQLFKField foreignKey) throws ORMException { + public > SQLElementList

getReferencedEntries(SQLFKField foreignKey, SQLOrderBy orderBy) throws ORMException { Set values = new HashSet<>(); forEach(v -> { T val = v.get(foreignKey); @@ -170,20 +170,61 @@ public class SQLElementList> extends ArrayList { }); if (values.isEmpty()) { - return new HashMap<>(); + return new SQLElementList<>(); } SQLWhereChain where = new SQLWhereChain(SQLBoolOp.OR); values.forEach(v -> where.add(new SQLWhereComp(foreignKey.getPrimaryField(), SQLComparator.EQ, v))); - SQLElementList foreignElemts = ORM.getAll(foreignKey.getForeignElementClass(), where, null, null, null); + return ORM.getAll(foreignKey.getForeignElementClass(), where, orderBy, null, null); - Map ret = new HashMap<>(); + } + + + public > Map getReferencedEntriesInGroups(SQLFKField foreignKey) throws ORMException { + SQLElementList

foreignElemts = getReferencedEntries(foreignKey, null); + + Map ret = new HashMap<>(); foreignElemts.forEach(foreignVal -> ret.put(foreignVal.get(foreignKey.getPrimaryField()), foreignVal)); return ret; } + + + public > SQLElementList getReferencingForeignEntries(SQLFKField foreignKey, SQLOrderBy orderBy, Integer limit, Integer offset) throws ORMException { + Set values = new HashSet<>(); + forEach(v -> { + T val = v.get(foreignKey.getPrimaryField()); + if (val != null) + values.add(val); + }); + + if (values.isEmpty()) { + return new SQLElementList<>(); + } + + SQLWhereChain where = new SQLWhereChain(SQLBoolOp.OR); + values.forEach(v -> where.add(new SQLWhereComp(foreignKey, SQLComparator.EQ, v))); + + return ORM.getAll(foreignKey.getSQLElementType(), where, orderBy, limit, offset); + + } + + + public > Map> getReferencingForeignEntriesInGroups(SQLFKField foreignKey, SQLOrderBy orderBy, Integer limit, Integer offset) throws ORMException { + SQLElementList foreignElements = getReferencingForeignEntries(foreignKey, orderBy, limit, offset); + + Map> map = new HashMap<>(); + foreignElements.forEach(foreignVal -> { + SQLElementList subList = map.getOrDefault(foreignVal.get(foreignKey), new SQLElementList<>()); + subList.add(foreignVal); + map.put(foreignVal.get(foreignKey), subList); + }); + + return map; + } + diff --git a/src/main/java/fr/pandacube/java/util/orm/SQLFKField.java b/src/main/java/fr/pandacube/java/util/orm/SQLFKField.java index ae4ac97..09289de 100644 --- a/src/main/java/fr/pandacube/java/util/orm/SQLFKField.java +++ b/src/main/java/fr/pandacube/java/util/orm/SQLFKField.java @@ -2,12 +2,20 @@ package fr.pandacube.java.util.orm; import fr.pandacube.java.util.Log; -public class SQLFKField, T, F extends SQLElement> extends SQLField { +/** + * + * @author Marc + * + * @param the table class of this current foreign key field + * @param the Java type of this field + * @param

the table class of the targeted primary key + */ +public class SQLFKField, T, P extends SQLElement

> extends SQLField { - private SQLField sqlPrimaryKeyField; - private Class sqlForeignKeyElemClass; + private SQLField sqlPrimaryKeyField; + private Class

sqlForeignKeyElemClass; - protected SQLFKField(SQLType t, boolean nul, T deflt, Class fkEl, SQLField fkF) { + protected SQLFKField(SQLType t, boolean nul, T deflt, Class

fkEl, SQLField fkF) { super(t, nul, deflt); construct(fkEl, fkF); } @@ -36,7 +44,7 @@ public class SQLFKField, T, F extends SQLElement> ext return new SQLFKField<>(fkF.type, nul, deflt, fkEl, fkF); } - private void construct(Class fkEl, SQLField fkF) { + private void construct(Class

fkEl, SQLField fkF) { if (fkF == null) throw new IllegalArgumentException("foreignKeyField can't be null"); try { ORM.initTable(fkEl); @@ -51,11 +59,11 @@ public class SQLFKField, T, F extends SQLElement> ext sqlForeignKeyElemClass = fkEl; } - public SQLField getPrimaryField() { + public SQLField getPrimaryField() { return sqlPrimaryKeyField; } - public Class getForeignElementClass() { + public Class

getForeignElementClass() { return sqlForeignKeyElemClass; }