Search in sources :

Example 11 with ForeignInfo

use of org.dbflute.dbmeta.info.ForeignInfo in project dbflute-core by dbflute.

the class HpFixedConditionQueryResolver method resolveOptimizedForeignAlias.

protected String resolveOptimizedForeignAlias(String optimizedCondition, InlineViewResource resource, Map<ForeignInfo, String> foreignAliasMap) {
    if (optimizedCondition == null) {
        return null;
    }
    final Map<String, ForeignInfo> optimizedVariableMap = resource.getOptimizedVariableMap();
    if (optimizedVariableMap == null) {
        return optimizedCondition;
    }
    for (Entry<String, ForeignInfo> entry : optimizedVariableMap.entrySet()) {
        final String relationVariable = entry.getKey();
        final ForeignInfo foreignInfo = entry.getValue();
        final String inlineAlias = foreignAliasMap.get(foreignInfo);
        optimizedCondition = replaceString(optimizedCondition, relationVariable, inlineAlias);
    }
    return optimizedCondition;
}
Also used : ForeignInfo(org.dbflute.dbmeta.info.ForeignInfo)

Example 12 with ForeignInfo

use of org.dbflute.dbmeta.info.ForeignInfo in project dbflute-core by dbflute.

the class AbstractBehaviorReadable method xdoBuildReferrerCorrelatedFixedCondition.

protected String xdoBuildReferrerCorrelatedFixedCondition(ConditionBean cb, ReferrerInfo referrerInfo) {
    final RelationInfo reverseRelation = referrerInfo.getReverseRelation();
    if (reverseRelation == null) {
        return null;
    }
    if (!(reverseRelation instanceof ForeignInfo)) {
        String msg = "The reverse relation (referrer's reverse) should be foreign info: " + referrerInfo;
        throw new IllegalStateException(msg);
    }
    final ForeignInfo foreignInfo = (ForeignInfo) reverseRelation;
    final String fixedCondition = foreignInfo.getFixedCondition();
    if (fixedCondition == null || fixedCondition.trim().length() == 0) {
        return null;
    }
    final String localAliasMark = HpFixedConditionQueryResolver.LOCAL_ALIAS_MARK;
    final String basePointAliasName = cb.getSqlClause().getBasePointAliasName();
    return Srl.replace(fixedCondition, localAliasMark, basePointAliasName);
}
Also used : ForeignInfo(org.dbflute.dbmeta.info.ForeignInfo) RelationInfo(org.dbflute.dbmeta.info.RelationInfo)

Example 13 with ForeignInfo

use of org.dbflute.dbmeta.info.ForeignInfo in project dbflute-core by dbflute.

the class AbstractBehaviorReadable method helpPulloutInternally.

// ===================================================================================
// Pull out Relation
// =================
protected <LOCAL_ENTITY extends Entity, FOREIGN_ENTITY extends Entity> List<FOREIGN_ENTITY> helpPulloutInternally(List<LOCAL_ENTITY> localEntityList, String foreignPropertyName) {
    assertObjectNotNull("localEntityList", localEntityList);
    assertObjectNotNull("foreignPropertyName", foreignPropertyName);
    final DBMeta dbmeta = asDBMeta();
    final ForeignInfo foreignInfo = dbmeta.findForeignInfo(foreignPropertyName);
    final RelationInfo reverseInfo = foreignInfo.getReverseRelation();
    final boolean existsReferrer = reverseInfo != null;
    final RelationOptionalFactory optionalFactory = xgetROpFactory();
    final Map<Integer, FOREIGN_ENTITY> foreignBasicMap = new LinkedHashMap<Integer, FOREIGN_ENTITY>();
    // lazy-loaded
    Map<Integer, List<FOREIGN_ENTITY>> foreignCollisionMap = null;
    final Map<FOREIGN_ENTITY, List<LOCAL_ENTITY>> foreignReferrerMap = new LinkedHashMap<FOREIGN_ENTITY, List<LOCAL_ENTITY>>();
    for (LOCAL_ENTITY localEntity : localEntityList) {
        final FOREIGN_ENTITY foreignEntity = xextractPulloutForeignEntity(foreignInfo, reverseInfo, optionalFactory, localEntity);
        if (foreignEntity == null) {
            continue;
        }
        // _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
        // basically mapped foreign entities are unique instances
        // but according to circumstances, same PK different instance (rare case)
        // 
        // e.g. normal pattern (no problem)
        // A - M - X
        // B - M - X
        // C - N - X
        // D - O - Y
        // 
        // e.g. when OverRelation, might be following pattern
        // A - M - X
        // B - M - Y *same relation but different nested relation
        // C - N - X
        // D - O - Y
        // 
        // when the latter pattern, the instance of A's M is different from the one of B's M
        // so it need to handle the unique as instance (don't use overridden equals() of entity)
        // _/_/_/_/_/_/_/_/_/_/
        foreignCollisionMap = xsavePulloutForeignEntity(foreignBasicMap, foreignCollisionMap, foreignEntity);
        if (existsReferrer) {
            if (!foreignReferrerMap.containsKey(foreignEntity)) {
                foreignReferrerMap.put(foreignEntity, new ArrayList<LOCAL_ENTITY>());
            }
            foreignReferrerMap.get(foreignEntity).add(localEntity);
        }
    }
    if (existsReferrer) {
        for (Entry<FOREIGN_ENTITY, List<LOCAL_ENTITY>> entry : foreignReferrerMap.entrySet()) {
            final FOREIGN_ENTITY foreignEntity = entry.getKey();
            final List<LOCAL_ENTITY> mappedLocalList = entry.getValue();
            final Object writtenObj = xextractPulloutReverseWrittenObject(foreignInfo, reverseInfo, optionalFactory, mappedLocalList);
            reverseInfo.write(foreignEntity, writtenObj);
        }
    }
    return xpreparePulloutResultList(foreignBasicMap, foreignCollisionMap);
}
Also used : RelationOptionalFactory(org.dbflute.optional.RelationOptionalFactory) LinkedHashMap(java.util.LinkedHashMap) ForeignInfo(org.dbflute.dbmeta.info.ForeignInfo) DBMeta(org.dbflute.dbmeta.DBMeta) RelationInfo(org.dbflute.dbmeta.info.RelationInfo) List(java.util.List) ArrayList(java.util.ArrayList)

Example 14 with ForeignInfo

use of org.dbflute.dbmeta.info.ForeignInfo in project dbflute-core by dbflute.

the class AbstractSqlClause method resolveRelationNo.

/**
 * {@inheritDoc}
 */
public int resolveRelationNo(String localTableName, String foreignPropertyName) {
    final DBMeta dbmeta = findDBMeta(localTableName);
    final ForeignInfo foreignInfo = dbmeta.findForeignInfo(foreignPropertyName);
    return foreignInfo.getRelationNo();
}
Also used : ForeignInfo(org.dbflute.dbmeta.info.ForeignInfo) DBMeta(org.dbflute.dbmeta.DBMeta)

Example 15 with ForeignInfo

use of org.dbflute.dbmeta.info.ForeignInfo in project dbflute-core by dbflute.

the class AbstractDBMeta method findForeignInfo.

/**
 * {@inheritDoc}
 */
public ForeignInfo findForeignInfo(int relationNo) {
    final Map<Integer, ForeignInfo> relationNoKeyMap = getForeignInfoRelationNoKeyMap();
    final ForeignInfo foreignInfo = relationNoKeyMap.get(relationNo);
    if (foreignInfo == null) {
        final String notice = "The foreign info was not found.";
        final String keyName = "Relation No";
        throwDBMetaNotFoundException(notice, keyName, relationNo, relationNoKeyMap.keySet());
    }
    return foreignInfo;
}
Also used : ForeignInfo(org.dbflute.dbmeta.info.ForeignInfo)

Aggregations

ForeignInfo (org.dbflute.dbmeta.info.ForeignInfo)20 DBMeta (org.dbflute.dbmeta.DBMeta)8 LinkedHashMap (java.util.LinkedHashMap)4 ColumnInfo (org.dbflute.dbmeta.info.ColumnInfo)4 ArrayList (java.util.ArrayList)3 RelationInfo (org.dbflute.dbmeta.info.RelationInfo)3 List (java.util.List)2 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 Entity (org.dbflute.Entity)1 ConditionQuery (org.dbflute.cbean.ConditionQuery)1 FixedConditionResolver (org.dbflute.cbean.sqlclause.join.FixedConditionResolver)1 SelectedRelationColumn (org.dbflute.cbean.sqlclause.select.SelectedRelationColumn)1 TableSqlName (org.dbflute.dbmeta.name.TableSqlName)1 PropertyMethodFinder (org.dbflute.dbmeta.property.PropertyMethodFinder)1 DBMetaNotFoundException (org.dbflute.exception.DBMetaNotFoundException)1 DfPropertyAccessor (org.dbflute.helper.beans.DfPropertyAccessor)1 RelationOptionalFactory (org.dbflute.optional.RelationOptionalFactory)1 IndexOfInfo (org.dbflute.util.Srl.IndexOfInfo)1