Search in sources :

Example 1 with TnRelationSelector

use of org.dbflute.s2dao.rowcreator.TnRelationSelector in project dbflute-core by dbflute.

the class TnBeanListResultSetHandler method mappingBean.

protected void mappingBean(ResultSet rs, BeanRowHandler handler) throws SQLException {
    // lazy initialization because if the result is zero, the resources are unused
    Map<String, String> selectColumnMap = null;
    Map<String, TnPropertyMapping> propertyCache = null;
    // key is relationNoSuffix, columnName
    Map<String, Map<String, TnPropertyMapping>> relPropCache = null;
    TnRelationRowCache relRowCache = null;
    TnRelationSelector relSelector = null;
    final TnBeanMetaData basePointBmd = getBeanMetaData();
    // condition-bean info (variable for minimum thread local access)
    final boolean hasCB = hasConditionBean();
    final ConditionBean cb = hasCB ? getConditionBean() : null;
    // outsideSql info (also variable for minimum thread local access)
    final boolean hasOql = hasOutsideSqlContext();
    final OutsideSqlContext oqlCtx = OutsideSqlContext.getOutsideSqlContextOnThread();
    final boolean checkNonSp = checkNonSpecifiedColumnAccess(hasCB, cb, hasOql, oqlCtx);
    final boolean colNullObj = isUseColumnNullObjectHandling(hasCB, cb);
    final boolean skipRelationLoop;
    {
        final boolean emptyRelationCB = hasCB && isSelectedRelationEmpty(cb);
        final boolean specifiedOutsideSql = hasOql && isSpecifiedOutsideSql(oqlCtx);
        // if it has condition-bean that has no relation to get
        // or it has outside SQL context that is specified outside-SQL,
        // they are unnecessary to do relation loop
        skipRelationLoop = emptyRelationCB || specifiedOutsideSql;
    }
    // null allowed
    final Map<String, Map<String, Integer>> selectIndexMap = ResourceContext.getSelectIndexMap();
    while (rs.next()) {
        if (selectColumnMap == null) {
            selectColumnMap = createSelectColumnMap(rs);
        }
        if (propertyCache == null) {
            propertyCache = createPropertyCache(selectColumnMap, selectIndexMap);
        }
        // create row instance of base table by row property cache
        final Object row = createRow(rs, selectIndexMap, propertyCache, cb);
        if (skipRelationLoop) {
            adjustCreatedRow(row, checkNonSp, colNullObj, basePointBmd, cb);
            final boolean continueToNext = handler.handle(row);
            if (!continueToNext) {
                // skip rear records (basically for cursor select)
                break;
            }
            continue;
        }
        if (relSelector == null) {
            relSelector = createRelationSelector(hasCB, cb);
        }
        if (relPropCache == null) {
            relPropCache = createRelationPropertyCache(selectColumnMap, selectIndexMap, relSelector);
        }
        if (relRowCache == null) {
            relRowCache = createRelationRowCache(hasCB, cb);
        }
        final List<TnRelationPropertyType> rptList = basePointBmd.getRelationPropertyTypeList();
        for (TnRelationPropertyType rpt : rptList) {
            if (relSelector.isNonSelectedRelation(rpt.getRelationNoSuffixPart())) {
                continue;
            }
            mappingFirstRelation(rs, row, rpt, selectColumnMap, selectIndexMap, relPropCache, relRowCache, relSelector);
        }
        adjustCreatedRow(row, checkNonSp, colNullObj, basePointBmd, cb);
        final boolean continueToNext = handler.handle(row);
        if (!continueToNext) {
            // skip rear records (basically for cursor select)
            break;
        }
    }
}
Also used : TnRelationPropertyType(org.dbflute.s2dao.metadata.TnRelationPropertyType) ConditionBean(org.dbflute.cbean.ConditionBean) TnPropertyMapping(org.dbflute.s2dao.metadata.TnPropertyMapping) TnRelationRowCache(org.dbflute.s2dao.rowcreator.TnRelationRowCache) OutsideSqlContext(org.dbflute.outsidesql.OutsideSqlContext) TnBeanMetaData(org.dbflute.s2dao.metadata.TnBeanMetaData) Map(java.util.Map) TnRelationSelector(org.dbflute.s2dao.rowcreator.TnRelationSelector)

Example 2 with TnRelationSelector

use of org.dbflute.s2dao.rowcreator.TnRelationSelector in project dbflute-core by dbflute.

the class TnBeanListResultSetHandler method createRelationSelector.

/**
 * Create the selector of relation.
 * @param hasCB Does the select use condition-bean?
 * @param cb The condition-bean for the select. (NullAllowed: not condition-bean select)
 * @return The created selector instance. (NotNull)
 */
protected TnRelationSelector createRelationSelector(final boolean hasCB, ConditionBean cb) {
    final boolean undefClsSel = isUndefinedClassificationSelectAllowed(hasCB, cb);
    final boolean colNullObj = isUseColumnNullObjectHandling(hasCB, cb);
    return new TnRelationSelector() {

        public boolean isNonLimitMapping() {
            return hasCB;
        }

        public boolean isNonSelectedRelation(String relationNoSuffix) {
            return hasCB && !cb.getSqlClause().hasSelectedRelation(relationNoSuffix);
        }

        public boolean isNonSelectedNextConnectingRelation(String relationNoSuffix) {
            return hasCB && !cb.getSqlClause().isSelectedNextConnectingRelation(relationNoSuffix);
        }

        public boolean canUseRelationCache(String relationNoSuffix) {
            return hasCB && cb.getSqlClause().canUseRelationCache(relationNoSuffix);
        }

        public boolean isNonSpecifiedColumnAccessAllowed(String relationNoSuffix) {
            return hasCB && cb.isNonSpecifiedColumnAccessAllowed();
        }

        public boolean isUsingSpecifyColumnInRelation(String relationNoSuffix) {
            if (!hasCB) {
                return false;
            }
            final SqlClause sqlClause = cb.getSqlClause();
            final String tableAlias = sqlClause.translateSelectedRelationPathToTableAlias(relationNoSuffix);
            if (tableAlias == null) {
                // no way but just in case
                return false;
            }
            return sqlClause.hasSpecifiedSelectColumn(tableAlias);
        }

        public Set<ColumnInfo> getRelationSpecifiedNullObjectColumnSet(String relationNoSuffix) {
            if (!hasCB) {
                return DfCollectionUtil.emptySet();
            }
            return cb.getSqlClause().getRelationSpecifiedNullObjectColumnSet(relationNoSuffix);
        }

        public boolean isUndefinedClassificationSelectAllowed(String relationNoSuffix) {
            return undefClsSel;
        }

        public boolean isColumnNullObjectEnabled(String relationNoSuffix) {
            return colNullObj;
        }
    };
}
Also used : SqlClause(org.dbflute.cbean.sqlclause.SqlClause) ColumnInfo(org.dbflute.dbmeta.info.ColumnInfo) TnRelationSelector(org.dbflute.s2dao.rowcreator.TnRelationSelector)

Aggregations

TnRelationSelector (org.dbflute.s2dao.rowcreator.TnRelationSelector)2 Map (java.util.Map)1 ConditionBean (org.dbflute.cbean.ConditionBean)1 SqlClause (org.dbflute.cbean.sqlclause.SqlClause)1 ColumnInfo (org.dbflute.dbmeta.info.ColumnInfo)1 OutsideSqlContext (org.dbflute.outsidesql.OutsideSqlContext)1 TnBeanMetaData (org.dbflute.s2dao.metadata.TnBeanMetaData)1 TnPropertyMapping (org.dbflute.s2dao.metadata.TnPropertyMapping)1 TnRelationPropertyType (org.dbflute.s2dao.metadata.TnRelationPropertyType)1 TnRelationRowCache (org.dbflute.s2dao.rowcreator.TnRelationRowCache)1