Search in sources :

Example 1 with TnBeanMetaData

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

the class TnAbstractEntityHandler method setupInsertBindVariables.

protected void setupInsertBindVariables(Object bean) {
    final List<Object> varList = new ArrayList<Object>();
    final List<ValueType> varValueTypeList = new ArrayList<ValueType>();
    final TnBeanMetaData bmd = getBeanMetaData();
    final String timestampPropertyName = bmd.getTimestampPropertyName();
    final String versionNoPropertyName = bmd.getVersionNoPropertyName();
    for (int i = 0; i < _boundPropTypes.length; ++i) {
        final TnPropertyType pt = _boundPropTypes[i];
        if (pt.getPropertyName().equalsIgnoreCase(timestampPropertyName)) {
            final Timestamp timestamp = ResourceContext.getAccessTimestamp();
            addNewTimestamp(timestamp);
            varList.add(timestamp);
        } else if (pt.getPropertyName().equalsIgnoreCase(versionNoPropertyName)) {
            final Long firstNo = InsertOption.VERSION_NO_FIRST_VALUE;
            addNewVersionNo(firstNo);
            varList.add(firstNo);
        } else {
            varList.add(pt.getPropertyDesc().getValue(bean));
        }
        varValueTypeList.add(pt.getValueType());
    }
    _bindVariables = varList.toArray();
    _bindVariableValueTypes = (ValueType[]) varValueTypeList.toArray(new ValueType[varValueTypeList.size()]);
}
Also used : ValueType(org.dbflute.jdbc.ValueType) TnBeanMetaData(org.dbflute.s2dao.metadata.TnBeanMetaData) ArrayList(java.util.ArrayList) Timestamp(java.sql.Timestamp) TnPropertyType(org.dbflute.s2dao.metadata.TnPropertyType)

Example 2 with TnBeanMetaData

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

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

the class TnAbstractEntityHandler method setupUpdateBindVariables.

protected void setupUpdateBindVariables(Object bean) {
    final List<Object> varList = new ArrayList<Object>();
    final List<ValueType> varValueTypeList = new ArrayList<ValueType>();
    // might be null
    final Set<String> uniqueDrivenPropSet = extractUniqueDrivenPropSet(bean);
    final TnBeanMetaData bmd = getBeanMetaData();
    final String timestampPropertyName = bmd.getTimestampPropertyName();
    final String versionNoPropertyName = bmd.getVersionNoPropertyName();
    for (int i = 0; i < _boundPropTypes.length; ++i) {
        final TnPropertyType pt = _boundPropTypes[i];
        final String propertyName = pt.getPropertyName();
        if (uniqueDrivenPropSet != null && uniqueDrivenPropSet.contains(propertyName)) {
            continue;
        }
        if (propertyName.equalsIgnoreCase(timestampPropertyName)) {
            final Timestamp timestamp = ResourceContext.getAccessTimestamp();
            addNewTimestamp(timestamp);
            varList.add(timestamp);
        } else if (propertyName.equalsIgnoreCase(versionNoPropertyName)) {
            if (!_versionNoAutoIncrementOnMemory) {
                // because of 'VERSION_NO = VERSION_NO + 1'
                continue;
            }
            // already null-checked
            final Object value = pt.getPropertyDesc().getValue(bean);
            final long longValue = DfTypeUtil.toPrimitiveLong(value) + 1L;
            final Long versionNo = Long.valueOf(longValue);
            addNewVersionNo(versionNo);
            varList.add(versionNo);
        } else if (_updateOption != null && _updateOption.hasStatement(pt.getColumnDbName())) {
            // because of 'FOO_COUNT = FOO_COUNT + 1'
            continue;
        } else {
            varList.add(pt.getPropertyDesc().getValue(bean));
        }
        varValueTypeList.add(pt.getValueType());
    }
    doSetupUpdateWhereBindVariables(varList, varValueTypeList, bean, uniqueDrivenPropSet);
    _bindVariables = varList.toArray();
    _bindVariableValueTypes = (ValueType[]) varValueTypeList.toArray(new ValueType[varValueTypeList.size()]);
}
Also used : ValueType(org.dbflute.jdbc.ValueType) TnBeanMetaData(org.dbflute.s2dao.metadata.TnBeanMetaData) ArrayList(java.util.ArrayList) Timestamp(java.sql.Timestamp) TnPropertyType(org.dbflute.s2dao.metadata.TnPropertyType)

Example 4 with TnBeanMetaData

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

the class TnBeanMetaDataFactoryExtension method createBeanMetaDataImpl.

@Override
protected TnBeanMetaDataImpl createBeanMetaDataImpl(Class<?> beanClass) {
    // /= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
    // for ConditionBean and insert() and update() and delete() and so on...
    // = = = = = = = = = =/
    final DBMeta dbmeta = provideDBMeta(beanClass);
    return new TnBeanMetaDataImpl(beanClass, dbmeta) {

        /**
         * The internal list of identifier generator. Elements of this list should be added when initializing.
         */
        protected final List<TnIdentifierGenerator> _internalIdentifierGeneratorList = new ArrayList<TnIdentifierGenerator>();

        /**
         * The internal map of identifier generator by property name.
         */
        protected final Map<String, TnIdentifierGenerator> _internalIdentifierGeneratorsByPropertyName = newConcurrentHashMap();

        // /= = = = = = =
        // for cache
        // = = = = =/
        @Override
        public void initialize() {
            // non thread safe so this is called immediately after creation
            final Class<?> myBeanClass = getBeanClass();
            if (isDBFluteEntity(myBeanClass)) {
                final TnBeanMetaData cachedMeta = getMetaFromCache(myBeanClass);
                if (cachedMeta == null) {
                    if (isInternalDebugEnabled()) {
                        _log.debug("...Caching the bean: " + DfTypeUtil.toClassTitle(myBeanClass));
                    }
                    _metaMap.put(myBeanClass, this);
                }
            }
            super.initialize();
        }

        // /= = = = = = =
        // for insert()
        // = = = = =/
        // The attributes 'identifierGenerators' and 'identifierGeneratorsByPropertyName'
        // of super class are unused. It prepares original attributes here.
        @Override
        protected void setupIdentifierGenerator(TnPropertyType propertyType) {
            // only called in the initialize() process
            final DfPropertyDesc pd = propertyType.getPropertyDesc();
            final String propertyName = propertyType.getPropertyName();
            final String idType = _beanAnnotationReader.getId(pd);
            final TnIdentifierGenerator generator = createInternalIdentifierGenerator(propertyType, idType);
            _internalIdentifierGeneratorList.add(generator);
            _internalIdentifierGeneratorsByPropertyName.put(propertyName, generator);
        }

        protected TnIdentifierGenerator createInternalIdentifierGenerator(TnPropertyType propertyType, String idType) {
            return TnIdentifierGeneratorFactory.createIdentifierGenerator(propertyType, idType);
        }

        @Override
        public TnIdentifierGenerator getIdentifierGenerator(int index) {
            return _internalIdentifierGeneratorList.get(index);
        }

        @Override
        public int getIdentifierGeneratorSize() {
            return _internalIdentifierGeneratorList.size();
        }

        @Override
        public TnIdentifierGenerator getIdentifierGenerator(String propertyName) {
            return _internalIdentifierGeneratorsByPropertyName.get(propertyName);
        }
    };
}
Also used : DfPropertyDesc(org.dbflute.helper.beans.DfPropertyDesc) DBMeta(org.dbflute.dbmeta.DBMeta) TnBeanMetaData(org.dbflute.s2dao.metadata.TnBeanMetaData) ArrayList(java.util.ArrayList) List(java.util.List) TnBeanMetaDataImpl(org.dbflute.s2dao.metadata.impl.TnBeanMetaDataImpl) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TnPropertyType(org.dbflute.s2dao.metadata.TnPropertyType) TnIdentifierGenerator(org.dbflute.s2dao.identity.TnIdentifierGenerator)

Example 5 with TnBeanMetaData

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

the class TnRelationPropertyTypeFactoryImpl method createRelationPropertyType.

protected TnRelationPropertyType createRelationPropertyType(DfPropertyDesc propertyDesc) {
    final String[] myKeys;
    final String[] yourKeys;
    final int relno = _beanAnnotationReader.getRelationNo(propertyDesc);
    final String relkeys = _beanAnnotationReader.getRelationKey(propertyDesc);
    if (relkeys != null) {
        final List<String> tokenList = Srl.splitListTrimmed(relkeys, ",");
        final List<String> myKeyList = new ArrayList<String>(tokenList.size());
        final List<String> yourKeyList = new ArrayList<String>(tokenList.size());
        for (String token : tokenList) {
            final int index = token.indexOf(':');
            if (index > 0) {
                myKeyList.add(token.substring(0, index));
                yourKeyList.add(token.substring(index + 1));
            } else {
                myKeyList.add(token);
                yourKeyList.add(token);
            }
        }
        myKeys = (String[]) myKeyList.toArray(new String[myKeyList.size()]);
        yourKeys = (String[]) yourKeyList.toArray(new String[yourKeyList.size()]);
    } else {
        // basically no way at least on DBFlute
        myKeys = new String[0];
        yourKeys = new String[0];
    }
    final Class<?> propertyType = chooseAnalyzedPropertyType(propertyDesc);
    final TnBeanMetaData relationBeanMetaData = createRelationBeanMetaData(propertyType);
    return createRelationPropertyType(propertyDesc, myKeys, yourKeys, relno, relationBeanMetaData);
}
Also used : TnBeanMetaData(org.dbflute.s2dao.metadata.TnBeanMetaData) ArrayList(java.util.ArrayList)

Aggregations

TnBeanMetaData (org.dbflute.s2dao.metadata.TnBeanMetaData)11 TnPropertyType (org.dbflute.s2dao.metadata.TnPropertyType)7 ArrayList (java.util.ArrayList)4 TnRelationPropertyType (org.dbflute.s2dao.metadata.TnRelationPropertyType)3 Timestamp (java.sql.Timestamp)2 Map (java.util.Map)2 ConditionBean (org.dbflute.cbean.ConditionBean)2 DBMeta (org.dbflute.dbmeta.DBMeta)2 ValueType (org.dbflute.jdbc.ValueType)2 List (java.util.List)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 DfPropertyDesc (org.dbflute.helper.beans.DfPropertyDesc)1 OutsideSqlContext (org.dbflute.outsidesql.OutsideSqlContext)1 TnIdentifierGenerator (org.dbflute.s2dao.identity.TnIdentifierGenerator)1 TnPropertyMapping (org.dbflute.s2dao.metadata.TnPropertyMapping)1 TnBeanMetaDataImpl (org.dbflute.s2dao.metadata.impl.TnBeanMetaDataImpl)1 TnRelationRowCache (org.dbflute.s2dao.rowcreator.TnRelationRowCache)1 TnRelationSelector (org.dbflute.s2dao.rowcreator.TnRelationSelector)1