Search in sources :

Example 21 with TnPropertyType

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

the class TnQueryUpdateDynamicCommand method execute.

// ===================================================================================
// Execute
// =======
public Object execute(Object[] args) {
    // analyze arguments
    final Entity entity = extractEntityWithCheck(args);
    final ConditionBean cb = extractConditionBeanWithCheck(args);
    final UpdateOption<ConditionBean> option = extractUpdateOptionWithCheck(args);
    prepareStatementConfigOnThreadIfExists(option);
    // arguments for execution (not contains an option)
    final String[] argNames = new String[] { "entity", "pmb" };
    final Class<?>[] argTypes = new Class<?>[] { entity.getClass(), cb.getClass() };
    final Object[] realArgs = new Object[] { entity, cb };
    // prepare context
    final List<TnPropertyType> boundPropTypeList = new ArrayList<TnPropertyType>();
    final CommandContext context;
    {
        final String twoWaySql = buildQueryUpdateTwoWaySql(entity, cb, option, boundPropTypeList);
        if (twoWaySql == null) {
            // non execute
            return 0;
        }
        context = createCommandContext(twoWaySql, argNames, argTypes, realArgs);
    }
    // execute
    final TnCommandContextHandler handler = createCommandContextHandler(context);
    handler.setExceptionMessageSqlArgs(context.getBindVariables());
    handler.setFirstBoundPropTypeList(boundPropTypeList);
    final int rows = handler.execute(realArgs);
    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 22 with TnPropertyType

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

the class TnQueryUpdateDynamicCommand method buildQueryUpdateTwoWaySql.

// ===================================================================================
// Build SQL
// =========
/**
 * @param entity The entity for update. (NotNull)
 * @param cb The condition-beanĀ for query. (NotNull)
 * @param option The option of update. (NullAllowed)
 * @param boundPropTypeList The type list of bound property. (NotNull, AlwaysEmpty)
 * @return The two-way SQL of query update. (NullAllowed: if non-modification, return null)
 */
protected String buildQueryUpdateTwoWaySql(Entity entity, ConditionBean cb, final UpdateOption<ConditionBean> option, List<TnPropertyType> boundPropTypeList) {
    final Map<String, Object> columnParameterMap = new LinkedHashMap<String, Object>();
    final DBMeta dbmeta = entity.asDBMeta();
    final Set<String> modifiedSet = entity.mymodifiedProperties();
    final List<ColumnInfo> columnInfoList = dbmeta.getColumnInfoList();
    for (final ColumnInfo columnInfo : columnInfoList) {
        if (columnInfo.isOptimisticLock()) {
            // exclusive control columns are processed after here
            continue;
        }
        final String columnDbName = columnInfo.getColumnDbName();
        if (option != null && option.hasStatement(columnDbName)) {
            // prior to specified
            columnParameterMap.put(columnDbName, new SqlClause.QueryUpdateSetCalculationHandler() {

                public String buildStatement(String aliasName) {
                    return option.buildStatement(columnDbName, aliasName);
                }
            });
            continue;
        }
        if (isSpecifiedProperty(option, modifiedSet, columnInfo)) {
            final String propertyName = columnInfo.getPropertyName();
            final Object value = columnInfo.read(entity);
            if (value != null) {
                columnParameterMap.put(columnDbName, "/*entity." + propertyName + "*/null");
                // add bound property type
                final TnPropertyType propertyType = _beanMetaData.getPropertyType(propertyName);
                boundPropTypeList.add(propertyType);
            } else {
                // it uses null literal on query
                // because the SQL analyzer blocks null parameters
                // (the analyzer should do it for condition-bean)
                columnParameterMap.put(columnDbName, "null");
            }
            continue;
        }
    }
    if (columnParameterMap.isEmpty()) {
        return null;
    }
    if (dbmeta.hasVersionNo()) {
        final ColumnInfo columnInfo = dbmeta.getVersionNoColumnInfo();
        final String columnDbName = columnInfo.getColumnDbName();
        columnParameterMap.put(columnDbName, new SqlClause.QueryUpdateSetCalculationHandler() {

            public String buildStatement(String aliasName) {
                // cipher for versionNo is unsupported
                final ColumnSqlName columnSqlName = columnInfo.getColumnSqlName();
                return (aliasName != null ? aliasName : "") + columnSqlName + " + 1";
            }
        });
    }
    if (dbmeta.hasUpdateDate()) {
        ColumnInfo columnInfo = dbmeta.getUpdateDateColumnInfo();
        columnInfo.write(entity, ResourceContext.getAccessTimestamp());
        final String columnDbName = columnInfo.getColumnDbName();
        final String propertyName = columnInfo.getPropertyName();
        columnParameterMap.put(columnDbName, "/*entity." + propertyName + "*/null");
        // add bound property type
        boundPropTypeList.add(_beanMetaData.getPropertyType(propertyName));
    }
    if (option != null && option.isQueryUpdateForcedDirectAllowed()) {
        cb.getSqlClause().enableQueryUpdateForcedDirect();
    }
    return cb.getSqlClause().getClauseQueryUpdate(columnParameterMap);
}
Also used : ColumnSqlName(org.dbflute.dbmeta.name.ColumnSqlName) DBMeta(org.dbflute.dbmeta.DBMeta) SqlClause(org.dbflute.cbean.sqlclause.SqlClause) ColumnInfo(org.dbflute.dbmeta.info.ColumnInfo) TnPropertyType(org.dbflute.s2dao.metadata.TnPropertyType) LinkedHashMap(java.util.LinkedHashMap)

Example 23 with TnPropertyType

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

the class TnUpdateEntityDynamicCommand method createUpdateSql.

// ===================================================================================
// Update SQL
// ==========
/**
 * Create update SQL. The update is by the primary keys or unique keys.
 * @param bean The bean of the entity to update. (NotNull)
 * @param propertyTypes The types of property for update. (NotNull)
 * @param option An option of update. (NullAllowed)
 * @return The update SQL. (NotNull)
 */
protected String createUpdateSql(Object bean, TnPropertyType[] propertyTypes, UpdateOption<ConditionBean> option) {
    checkPrimaryKey();
    final String tableDbName = _targetDBMeta.getTableDbName();
    final Set<String> uniqueDrivenPropSet = extractUniqueDrivenPropSet(bean);
    final StringBuilder sb = new StringBuilder(96);
    sb.append("update ").append(_targetDBMeta.getTableSqlName()).append(" set ");
    final String versionNoPropertyName = _beanMetaData.getVersionNoPropertyName();
    int columnCount = 0;
    for (TnPropertyType pt : propertyTypes) {
        final String columnDbName = pt.getColumnDbName();
        final ColumnSqlName columnSqlName = pt.getColumnSqlName();
        final String propertyName = pt.getPropertyName();
        if (uniqueDrivenPropSet != null && uniqueDrivenPropSet.contains(propertyName)) {
            if (option != null && option.hasStatement(columnDbName)) {
                throwUniqueDrivenColumnUpdateStatementException(tableDbName, columnDbName, uniqueDrivenPropSet);
            }
            continue;
        }
        if (columnCount > 0) {
            sb.append(", ");
        }
        ++columnCount;
        if (propertyName.equalsIgnoreCase(versionNoPropertyName)) {
            if (!isVersionNoAutoIncrementOnMemory()) {
                setupVersionNoAutoIncrementOnQuery(sb, columnSqlName);
                continue;
            }
        }
        sb.append(columnSqlName).append(" = ");
        final String valueExp;
        if (option != null && option.hasStatement(columnDbName)) {
            // prior to specified
            final String statement = option.buildStatement(columnDbName);
            valueExp = encryptIfNeeds(tableDbName, columnDbName, statement);
        } else {
            valueExp = encryptIfNeeds(tableDbName, columnDbName, "?");
        }
        sb.append(valueExp);
    }
    sb.append(ln());
    setupUpdateWhere(sb, uniqueDrivenPropSet, _optimisticLockHandling);
    return sb.toString();
}
Also used : ColumnSqlName(org.dbflute.dbmeta.name.ColumnSqlName) TnPropertyType(org.dbflute.s2dao.metadata.TnPropertyType)

Example 24 with TnPropertyType

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

the class TnAbstractEntityHandler method doSetupUpdateWhereBindVariables.

protected void doSetupUpdateWhereBindVariables(List<Object> varList, List<ValueType> varValueTypeList, Object bean, Set<String> uniqueDrivenPropSet) {
    final TnBeanMetaData bmd = getBeanMetaData();
    if (uniqueDrivenPropSet != null && !uniqueDrivenPropSet.isEmpty()) {
        for (String uniqueProp : uniqueDrivenPropSet) {
            final TnPropertyType pt = bmd.getPropertyType(uniqueProp);
            doRegisterUpdateWhereBindVariable(varList, varValueTypeList, bean, pt);
        }
    } else {
        for (int i = 0; i < bmd.getPrimaryKeySize(); ++i) {
            final TnPropertyType pt = bmd.getPropertyTypeByColumnName(bmd.getPrimaryKeyDbName(i));
            doRegisterUpdateWhereBindVariable(varList, varValueTypeList, bean, pt);
        }
    }
    if (_optimisticLockHandling && bmd.hasVersionNoPropertyType()) {
        final TnPropertyType pt = bmd.getVersionNoPropertyType();
        doRegisterUpdateWhereBindVariable(varList, varValueTypeList, bean, pt);
    }
    if (_optimisticLockHandling && bmd.hasTimestampPropertyType()) {
        final TnPropertyType pt = bmd.getTimestampPropertyType();
        doRegisterUpdateWhereBindVariable(varList, varValueTypeList, bean, pt);
    }
}
Also used : TnBeanMetaData(org.dbflute.s2dao.metadata.TnBeanMetaData) TnPropertyType(org.dbflute.s2dao.metadata.TnPropertyType)

Example 25 with TnPropertyType

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

the class TnCommandContextHandler method bindFirstScope.

// ===================================================================================
// Bind Scope
// ==========
protected int bindFirstScope(Connection conn, PreparedStatement ps, Object[] bindVariables, Class<?>[] bindVariableTypes) {
    final List<Object> firstVariableList = new ArrayList<Object>();
    final List<ValueType> firstValueTypeList = new ArrayList<ValueType>();
    int index = 0;
    for (TnPropertyType propertyType : _firstBoundPropTypeList) {
        firstVariableList.add(bindVariables[index]);
        firstValueTypeList.add(propertyType.getValueType());
        ++index;
    }
    bindArgs(conn, ps, firstVariableList.toArray(), firstValueTypeList.toArray(new ValueType[0]));
    return index;
}
Also used : ValueType(org.dbflute.jdbc.ValueType) 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