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);
}
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);
}
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;
}
}
}
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;
}
}
Aggregations