use of org.dbflute.s2dao.rowcreator.TnRelationRowCache 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;
}
}
}
use of org.dbflute.s2dao.rowcreator.TnRelationRowCache in project dbflute-core by dbflute.
the class TnRelationRowCreatorExtension method mappingNextRelation.
/**
* Do mapping next relation row. <br>
* This logic is similar to first relation mapping in {@link TnBeanListResultSetHandler}. <br>
* So you should check it when this logic has modification.
* @param res The resource of relation row creation. (NotNull)
* @param row The base point row, which is previous relation row. (NotNull)
* @throws SQLException When it fails to handle the SQL.
*/
protected void mappingNextRelation(TnRelationRowCreationResource res, Object row) throws SQLException {
if (res.isStopCurrentRelationMapping()) {
return;
}
// also saves it in resource
final TnRelationKey relKey = res.prepareRelationKey();
final TnRelationPropertyType rpt = res.getRelationPropertyType();
Object relationRow = null;
if (relKey != null) {
final String relationNoSuffix = res.getRelationNoSuffix();
final boolean canUseRelationCache = res.canUseRelationCache();
TnRelationRowCache relRowCache = null;
if (canUseRelationCache) {
relRowCache = res.getRelRowCache();
relationRow = relRowCache.getRelationRow(relationNoSuffix, relKey);
}
if (relationRow == null) {
// when no cache
relationRow = createRelationRow(res);
if (relationRow != null) {
// is new created relation row
adjustCreatedRelationRow(relationRow, res.getRelationNoSuffix(), res.getRelationSelector(), rpt);
if (canUseRelationCache) {
relRowCache.addRelationRow(relationNoSuffix, relKey, relationRow);
}
}
}
}
// if exists, optional or plain value
// if null, empty optional or nothing
relationRow = filterOptionalRelationRowIfNeeds(row, rpt, relationRow);
if (relationRow != null) {
// exists or empty optional
rpt.getPropertyAccessor().setValue(row, relationRow);
}
}
Aggregations