Search in sources :

Example 6 with TnPropertyType

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

the class TnQueryInsertDynamicCommand method execute.

// ===================================================================================
// Execute
// =======
public Object execute(Object[] args) {
    // analyze arguments
    final Entity entity = extractEntityWithCheck(args);
    final ConditionBean intoCB = extractIntoConditionBeanWithCheck(args);
    final ConditionBean resourceCB = extractResourceConditionBeanWithCheck(args);
    final InsertOption<ConditionBean> option = extractInsertOptionWithCheck(args);
    prepareStatementConfigOnThreadIfExists(option);
    // arguments for execution (not contains an option)
    final String[] argNames = new String[] { "entity", "pmb" };
    final Class<?>[] argTypes = new Class<?>[] { entity.getClass(), resourceCB.getClass() };
    final Object[] realArgs = new Object[] { entity, resourceCB };
    // prepare context
    final List<TnPropertyType> boundPropTypeList = new ArrayList<TnPropertyType>();
    final CommandContext context;
    {
        final String twoWaySql = buildQueryInsertTwoWaySql(entity, intoCB, resourceCB, option, boundPropTypeList);
        context = createCommandContext(twoWaySql, argNames, argTypes, realArgs);
    }
    // execute
    final TnCommandContextHandler handler = createCommandContextHandler(context);
    handler.setExceptionMessageSqlArgs(context.getBindVariables());
    final boolean identityDisabled = option != null && option.isPrimaryKeyIdentityDisabled();
    if (identityDisabled) {
        disableIdentityGeneration(entity);
    }
    final int rows;
    RuntimeException sqlEx = null;
    try {
        rows = handler.execute(realArgs);
    } catch (RuntimeException e) {
        sqlEx = e;
        throw e;
    } finally {
        if (identityDisabled) {
            try {
                enableIdentityGeneration(entity);
            } catch (RuntimeException e) {
                if (sqlEx == null) {
                    throw e;
                }
            // ignore the exception when main SQL fails
            // not to close the main exception
            }
        }
    }
    return Integer.valueOf(rows);
}
Also used : Entity(org.dbflute.Entity) CommandContext(org.dbflute.twowaysql.context.CommandContext) ArrayList(java.util.ArrayList) TnCommandContextHandler(org.dbflute.s2dao.sqlhandler.TnCommandContextHandler) ConditionBean(org.dbflute.cbean.ConditionBean) TnPropertyType(org.dbflute.s2dao.metadata.TnPropertyType)

Example 7 with TnPropertyType

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

the class TnUpdateEntityDynamicCommand method createUpdatePropertyTypes.

// ===================================================================================
// Update Column
// =============
protected TnPropertyType[] createUpdatePropertyTypes(Object bean, UpdateOption<ConditionBean> option) {
    final Set<String> modifiedSet = getModifiedPropertyNames(bean);
    final List<TnPropertyType> typeList = new ArrayList<TnPropertyType>();
    final String timestampProp = _beanMetaData.getTimestampPropertyName();
    final String versionNoProp = _beanMetaData.getVersionNoPropertyName();
    final String[] propertyNames = _propertyNames;
    for (int i = 0; i < propertyNames.length; ++i) {
        final TnPropertyType pt = _beanMetaData.getPropertyType(propertyNames[i]);
        if (pt.isPrimaryKey()) {
            continue;
        }
        if (// optimistic lock
        isOptimisticLockProperty(timestampProp, versionNoProp, pt) || // statement
        isStatementProperty(option, pt) || isSpecifiedProperty(option, modifiedSet, pt)) {
            // specified
            typeList.add(pt);
        }
    }
    return typeList.toArray(new TnPropertyType[typeList.size()]);
}
Also used : ArrayList(java.util.ArrayList) TnPropertyType(org.dbflute.s2dao.metadata.TnPropertyType)

Example 8 with TnPropertyType

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

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

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

the class AbstractCountableUpdateCommand method createNonOrderedPropertyNames.

private String[] createNonOrderedPropertyNames(TnBeanMetaData bmd) {
    final List<String> propertyNameList = new ArrayList<String>();
    final List<TnPropertyType> ptList = bmd.getPropertyTypeList();
    for (TnPropertyType pt : ptList) {
        if (pt.isPersistent()) {
            propertyNameList.add(pt.getPropertyName());
        }
    }
    return propertyNameList.toArray(new String[propertyNameList.size()]);
}
Also used : ArrayList(java.util.ArrayList) TnPropertyType(org.dbflute.s2dao.metadata.TnPropertyType)

Aggregations

TnPropertyType (org.dbflute.s2dao.metadata.TnPropertyType)25 ArrayList (java.util.ArrayList)12 TnBeanMetaData (org.dbflute.s2dao.metadata.TnBeanMetaData)7 ConditionBean (org.dbflute.cbean.ConditionBean)3 DBMeta (org.dbflute.dbmeta.DBMeta)3 ColumnSqlName (org.dbflute.dbmeta.name.ColumnSqlName)3 ValueType (org.dbflute.jdbc.ValueType)3 Timestamp (java.sql.Timestamp)2 Entity (org.dbflute.Entity)2 ColumnInfo (org.dbflute.dbmeta.info.ColumnInfo)2 DfPropertyDesc (org.dbflute.helper.beans.DfPropertyDesc)2 TnIdentifierGenerator (org.dbflute.s2dao.identity.TnIdentifierGenerator)2 TnCommandContextHandler (org.dbflute.s2dao.sqlhandler.TnCommandContextHandler)2 CommandContext (org.dbflute.twowaysql.context.CommandContext)2 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 SqlClause (org.dbflute.cbean.sqlclause.SqlClause)1