refactoring foreigh key method and field

This commit is contained in:
Marc Baloup 2019-01-16 11:15:42 +01:00
parent 9f339c357f
commit 65d173fa04
3 changed files with 8 additions and 8 deletions

View File

@ -175,11 +175,11 @@ public abstract class SQLElement<E extends SQLElement<E>> {
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.getPrimaryField(), 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 { 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()); 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(),
new SQLWhereComp(field, SQLComparator.EQ, value), orderBy, limit, offset); new SQLWhereComp(field, SQLComparator.EQ, value), orderBy, limit, offset);

View File

@ -174,13 +174,13 @@ public class SQLElementList<E extends SQLElement<E>> extends ArrayList<E> {
} }
SQLWhereChain where = new SQLWhereChain(SQLBoolOp.OR); SQLWhereChain where = new SQLWhereChain(SQLBoolOp.OR);
values.forEach(v -> where.add(new SQLWhereComp(foreignKey.getForeignField(), SQLComparator.EQ, v))); values.forEach(v -> where.add(new SQLWhereComp(foreignKey.getPrimaryField(), SQLComparator.EQ, v)));
SQLElementList<F> foreignElemts = ORM.getAll(foreignKey.getForeignElementClass(), where, null, null, null); SQLElementList<F> foreignElemts = ORM.getAll(foreignKey.getForeignElementClass(), where, null, null, null);
Map<T, F> ret = new HashMap<>(); Map<T, F> ret = new HashMap<>();
foreignElemts.forEach(foreignVal -> ret.put(foreignVal.get(foreignKey.getForeignField()), foreignVal)); foreignElemts.forEach(foreignVal -> ret.put(foreignVal.get(foreignKey.getPrimaryField()), foreignVal));
return ret; return ret;
} }

View File

@ -4,7 +4,7 @@ import fr.pandacube.java.util.Log;
public class SQLFKField<E extends SQLElement<E>, T, F extends SQLElement<F>> extends SQLField<E, T> { public class SQLFKField<E extends SQLElement<E>, T, F extends SQLElement<F>> extends SQLField<E, T> {
private SQLField<F, T> sqlForeignKeyField; private SQLField<F, T> sqlPrimaryKeyField;
private Class<F> sqlForeignKeyElemClass; private Class<F> sqlForeignKeyElemClass;
protected SQLFKField(SQLType<T> t, boolean nul, T deflt, Class<F> fkEl, SQLField<F, T> fkF) { protected SQLFKField(SQLType<T> t, boolean nul, T deflt, Class<F> fkEl, SQLField<F, T> fkF) {
@ -47,12 +47,12 @@ public class SQLFKField<E extends SQLElement<E>, T, F extends SQLElement<F>> ext
throw new IllegalArgumentException("foreignKeyField must be from supplied foreignKeyElement"); throw new IllegalArgumentException("foreignKeyField must be from supplied foreignKeyElement");
if (!type.equals(fkF.type)) if (!type.equals(fkF.type))
throw new IllegalArgumentException("foreignKeyField and current Field must have the same SQLType"); throw new IllegalArgumentException("foreignKeyField and current Field must have the same SQLType");
sqlForeignKeyField = fkF; sqlPrimaryKeyField = fkF;
sqlForeignKeyElemClass = fkEl; sqlForeignKeyElemClass = fkEl;
} }
public SQLField<F, T> getForeignField() { public SQLField<F, T> getPrimaryField() {
return sqlForeignKeyField; return sqlPrimaryKeyField;
} }
public Class<F> getForeignElementClass() { public Class<F> getForeignElementClass() {