Search in sources :

Example 1 with TnPropertyMapping

use of org.dbflute.s2dao.metadata.TnPropertyMapping in project dbflute-core by dbflute.

the class TnRelationRowCreatorExtension method setupRelationAllValue.

// ===================================================================================
// Relation AllValue Setup
// =======================
@Override
protected void setupRelationAllValue(TnRelationRowCreationResource res) throws SQLException {
    final Map<String, TnPropertyMapping> propertyCacheElement = res.extractPropertyCacheElement();
    for (Entry<String, TnPropertyMapping> entry : propertyCacheElement.entrySet()) {
        final TnPropertyMapping pt = entry.getValue();
        res.setCurrentPropertyType(pt);
        if (!isValidRelationPerPropertyLoop(res)) {
            // no way unless the method is overridden
            res.clearRowInstance();
            return;
        }
        setupRelationProperty(res);
    }
    if (!isValidRelationAfterPropertyLoop(res)) {
        // e.g. when all values are null
        res.clearRowInstance();
        return;
    }
    res.clearValidValueCount();
    if (res.isStopNextRelationMapping()) {
        return;
    }
    setupNextRelationRow(res);
}
Also used : TnPropertyMapping(org.dbflute.s2dao.metadata.TnPropertyMapping)

Example 2 with TnPropertyMapping

use of org.dbflute.s2dao.metadata.TnPropertyMapping in project dbflute-core by dbflute.

the class TnRelationRowCreatorExtension method registerRelationValue.

protected void registerRelationValue(TnRelationRowCreationResource res, String columnName) throws SQLException {
    final TnPropertyMapping mapping = res.getCurrentPropertyMapping();
    Object value = null;
    if (res.containsRelationKeyColumn(columnName)) {
        // #RELKEY
        // if this column is relation key, it gets the value from relation key values
        // for performance and avoiding twice getting same column value
        value = res.extractRelationKeyValue(columnName);
    } else {
        final ValueType valueType = mapping.getValueType();
        final Map<String, Map<String, Integer>> selectIndexMap = res.getSelectIndexMap();
        final ResultSet rs = res.getResultSet();
        if (selectIndexMap != null) {
            final String relationNoSuffix = res.getRelationNoSuffix();
            value = ResourceContext.getRelationValue(rs, relationNoSuffix, columnName, valueType, selectIndexMap);
        } else {
            value = valueType.getValue(rs, columnName);
        }
    }
    handleRelationValueRegistration(res, mapping, value);
}
Also used : TnPropertyMapping(org.dbflute.s2dao.metadata.TnPropertyMapping) ValueType(org.dbflute.jdbc.ValueType) ResultSet(java.sql.ResultSet) Map(java.util.Map)

Example 3 with TnPropertyMapping

use of org.dbflute.s2dao.metadata.TnPropertyMapping 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 4 with TnPropertyMapping

use of org.dbflute.s2dao.metadata.TnPropertyMapping in project dbflute-core by dbflute.

the class TnRowCreatorExtension method createRow.

// ===================================================================================
// Main
// ====
/**
 * {@inheritDoc}
 */
public Object createRow(ResultSet rs, Map<String, Map<String, Integer>> selectIndexMap, Map<String, TnPropertyMapping> propertyCache, Class<?> beanClass, ConditionBean cb) throws SQLException {
    if (propertyCache.isEmpty()) {
        String msg = "The propertyCache should not be empty: bean=" + beanClass.getName();
        throw new IllegalStateException(msg);
    }
    // temporary variable, for exception message, debug message
    String columnName = null;
    TnPropertyMapping mapping = null;
    String propertyName = null;
    Object selectedValue = null;
    ColumnInfo columnInfo = null;
    final Object row;
    final DBMeta dbmeta;
    if (_fixedDBMeta != null) {
        if (_creatableByDBMeta) {
            // mainly here
            final Entity entity = _fixedDBMeta.newEntity();
            reflectConditionBeanOptionToEntity(cb, entity);
            row = entity;
        } else {
            // e.g. manual-extended entity
            row = newBean(beanClass);
        }
        dbmeta = _fixedDBMeta;
    } else {
        // e.g. manual-created bean of outsideSql
        row = newBean(beanClass);
        // find just in case
        dbmeta = findCachedDBMeta(row);
    }
    try {
        if (dbmeta != null) {
            // mainly here
            // almost always true
            final boolean isEntity = row instanceof Entity;
            final Entity entityRow = isEntity ? (Entity) row : null;
            for (Entry<String, TnPropertyMapping> entry : propertyCache.entrySet()) {
                columnName = entry.getKey();
                mapping = entry.getValue();
                propertyName = mapping.getPropertyName();
                selectedValue = getValue(rs, columnName, mapping.getValueType(), selectIndexMap);
                columnInfo = mapping.getEntityColumnInfo();
                if (columnInfo != null && isEntity) {
                    columnInfo.write(entityRow, selectedValue);
                } else {
                    mapping.getPropertyAccessor().setValue(row, selectedValue);
                }
            }
            if (canHandleDerivedMap(row)) {
                processDerivedMap(rs, selectIndexMap, propertyCache, row);
            }
        } else {
            // not DBFlute entity
            for (Entry<String, TnPropertyMapping> entry : propertyCache.entrySet()) {
                columnName = entry.getKey();
                mapping = entry.getValue();
                propertyName = mapping.getPropertyName();
                selectedValue = getValue(rs, columnName, mapping.getValueType(), selectIndexMap);
                mapping.getPropertyAccessor().setValue(row, selectedValue);
            }
        }
        return row;
    } catch (ClassCastException e) {
        throwMappingClassCastException(row, dbmeta, mapping, selectedValue, e);
        // unreachable
        return null;
    } catch (SQLException e) {
        if (_log.isDebugEnabled()) {
            String msg = "Failed to get selected values while resultSet handling:";
            msg = msg + " target=" + DfTypeUtil.toClassTitle(beanClass) + "." + propertyName;
            _log.debug(msg);
        }
        throw e;
    }
}
Also used : Entity(org.dbflute.Entity) DBMeta(org.dbflute.dbmeta.DBMeta) TnPropertyMapping(org.dbflute.s2dao.metadata.TnPropertyMapping) SQLException(java.sql.SQLException) MappingClassCastException(org.dbflute.exception.MappingClassCastException) ColumnInfo(org.dbflute.dbmeta.info.ColumnInfo)

Aggregations

TnPropertyMapping (org.dbflute.s2dao.metadata.TnPropertyMapping)4 Map (java.util.Map)2 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Entity (org.dbflute.Entity)1 ConditionBean (org.dbflute.cbean.ConditionBean)1 DBMeta (org.dbflute.dbmeta.DBMeta)1 ColumnInfo (org.dbflute.dbmeta.info.ColumnInfo)1 MappingClassCastException (org.dbflute.exception.MappingClassCastException)1 ValueType (org.dbflute.jdbc.ValueType)1 OutsideSqlContext (org.dbflute.outsidesql.OutsideSqlContext)1 TnBeanMetaData (org.dbflute.s2dao.metadata.TnBeanMetaData)1 TnRelationPropertyType (org.dbflute.s2dao.metadata.TnRelationPropertyType)1 TnRelationRowCache (org.dbflute.s2dao.rowcreator.TnRelationRowCache)1 TnRelationSelector (org.dbflute.s2dao.rowcreator.TnRelationSelector)1