de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.relations.RelationManyToManyOrdered Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of FROST-Server.SQLjooq Show documentation
Show all versions of FROST-Server.SQLjooq Show documentation
SQL bindings for the FROST-Server.
/*
* Copyright (C) 2024 Fraunhofer Institut IOSB, Fraunhoferstr. 1, D 76131
* Karlsruhe, Germany.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see .
*/
package de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.relations;
import de.fraunhofer.iosb.ilt.frostserver.model.core.Entity;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.JooqPersistenceManager;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.QueryBuilder;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.tables.StaMainTable;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.tables.StaTable;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.utils.QueryState;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.utils.TableRef;
import de.fraunhofer.iosb.ilt.frostserver.property.NavigationPropertyMain;
import de.fraunhofer.iosb.ilt.frostserver.query.OrderBy;
import org.jooq.DSLContext;
import org.jooq.Field;
import org.jooq.Record1;
import org.jooq.SelectConditionStep;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A relation from a source table to a target table using a link table ordered
* by a long value.
*
* @author hylke
* @param The source table.
* @param The link table linking source and target entities.
* @param The target table.
*/
public class RelationManyToManyOrdered, L extends StaTable, T extends StaMainTable> extends RelationManyToMany {
private static final Logger LOGGER = LoggerFactory.getLogger(RelationManyToManyOrdered.class.getName());
/**
* The field used for the ordering.
*/
private FieldAccessor orderFieldAcc;
private boolean alwaysDistinct = false;
private boolean orderOnSource = true;
public RelationManyToManyOrdered(NavigationPropertyMain navProp, S source, L linkTable, T target, boolean orderOnSource) {
super(navProp, source, linkTable, target);
this.orderOnSource = orderOnSource;
}
public RelationManyToManyOrdered setOrderFieldAcc(FieldAccessor orderFieldAcc) {
this.orderFieldAcc = orderFieldAcc;
return this;
}
public RelationManyToManyOrdered setAlwaysDistinct(boolean alwaysDistinct) {
this.alwaysDistinct = alwaysDistinct;
return this;
}
@Override
public TableRef join(S source, QueryState> queryState, TableRef sourceRef) {
T targetAliased = (T) getTarget().asSecure(queryState.getNextAlias(), queryState.getPersistenceManager());
L linkTableAliased = (L) getLinkTable().as(queryState.getNextAlias());
Field> sourceField = getSourceFieldAcc().getField(source);
Field> sourceLinkField = getSourceLinkFieldAcc().getField(linkTableAliased);
Field> targetLinkField = getTargetLinkFieldAcc().getField(linkTableAliased);
Field> targetField = getTargetFieldAcc().getField(targetAliased);
queryState.setSqlFrom(queryState.getSqlFrom().leftJoin(linkTableAliased).on(((Field) sourceLinkField).eq(sourceField)));
queryState.setSqlFrom(queryState.getSqlFrom().leftJoin(targetAliased).on(((Field) targetField).eq(targetLinkField)));
if (alwaysDistinct || queryState.isFilter()) {
queryState.setDistinctRequired(true);
} else {
Field orderField = orderFieldAcc.getField(linkTableAliased);
queryState.getSqlSortFields().add(orderField, OrderBy.OrderType.ASCENDING);
}
return QueryBuilder.createJoinedRef(sourceRef, getNavProp(), targetAliased);
}
@Override
protected void link(JooqPersistenceManager pm, Object sourceId, Object targetId) {
final DSLContext dslContext = pm.getDslContext();
final L linkTable = getLinkTable();
final Field
© 2015 - 2025 Weber Informatics LLC | Privacy Policy