Search in sources :

Example 71 with ColumnInfo

use of org.dbflute.dbmeta.info.ColumnInfo 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 72 with ColumnInfo

use of org.dbflute.dbmeta.info.ColumnInfo in project dbflute-core by dbflute.

the class TnQueryInsertDynamicCommand method buildQueryInsertTwoWaySql.

// ===================================================================================
// Build SQL
// =========
/**
 * @param entity The entity for fixed values. (NotNull)
 * @param intoCB The condition-bean for insert into. (NotNull)
 * @param resourceCB The condition-bean for resource. (NotNull)
 * @param option The option of insert. (NullAllowed)
 * @param boundPropTypeList The type list of bound property. (NotNull, Empty)
 * @return The two-way SQL of query-insert. (NotNull)
 */
protected String buildQueryInsertTwoWaySql(Entity entity, ConditionBean intoCB, ConditionBean resourceCB, InsertOption<ConditionBean> option, List<TnPropertyType> boundPropTypeList) {
    final StringKeyMap<String> fixedValueQueryExpMap = StringKeyMap.createAsFlexibleOrdered();
    final Set<String> modifiedProperties = entity.mymodifiedProperties();
    final DBMeta dbmeta = entity.asDBMeta();
    final List<ColumnInfo> columnInfoList = dbmeta.getColumnInfoList();
    for (ColumnInfo columnInfo : columnInfoList) {
        final String propertyName = columnInfo.getPropertyName();
        if (!modifiedProperties.contains(propertyName)) {
            continue;
        }
        final Object value = columnInfo.read(entity);
        final String fixedValueQueryExp = (value != null ? "/*entity." + propertyName + "*/null" : null);
        fixedValueQueryExpMap.put(columnInfo.getColumnDbName(), fixedValueQueryExp);
    }
    return intoCB.getSqlClause().getClauseQueryInsert(fixedValueQueryExpMap, resourceCB.getSqlClause());
}
Also used : DBMeta(org.dbflute.dbmeta.DBMeta) ColumnInfo(org.dbflute.dbmeta.info.ColumnInfo)

Example 73 with ColumnInfo

use of org.dbflute.dbmeta.info.ColumnInfo in project dbflute-core by dbflute.

the class TnDBMetaBeanAnnotationReader method getRelationKey.

public String getRelationKey(DfPropertyDesc pd) {
    if (_simpleType) {
        return null;
    }
    if (_fieldBeanAnnotationReader != null) {
        return _fieldBeanAnnotationReader.getRelationKey(pd);
    }
    if (!_dbmeta.hasForeign(pd.getPropertyName())) {
        return null;
    }
    final ForeignInfo foreignInfo = _dbmeta.findForeignInfo(pd.getPropertyName());
    final Map<ColumnInfo, ColumnInfo> localForeignColumnInfoMap = foreignInfo.getLocalForeignColumnInfoMap();
    final Set<ColumnInfo> keySet = localForeignColumnInfoMap.keySet();
    final StringBuilder sb = new StringBuilder();
    for (ColumnInfo localColumnInfo : keySet) {
        final ColumnInfo foreignColumnInfo = localForeignColumnInfoMap.get(localColumnInfo);
        if (sb.length() > 0) {
            sb.append(", ");
        }
        sb.append(localColumnInfo.getColumnDbName());
        sb.append(":").append(foreignColumnInfo.getColumnDbName());
    }
    return sb.toString();
}
Also used : ForeignInfo(org.dbflute.dbmeta.info.ForeignInfo) ColumnInfo(org.dbflute.dbmeta.info.ColumnInfo)

Aggregations

ColumnInfo (org.dbflute.dbmeta.info.ColumnInfo)73 DBMeta (org.dbflute.dbmeta.DBMeta)27 ColumnSqlName (org.dbflute.dbmeta.name.ColumnSqlName)14 Entity (org.dbflute.Entity)8 SpecifiedColumn (org.dbflute.cbean.dream.SpecifiedColumn)7 SqlClause (org.dbflute.cbean.sqlclause.SqlClause)7 ArrayList (java.util.ArrayList)6 LinkedHashMap (java.util.LinkedHashMap)5 PrimaryInfo (org.dbflute.dbmeta.info.PrimaryInfo)5 ColumnRealName (org.dbflute.dbmeta.name.ColumnRealName)5 List (java.util.List)4 ForeignInfo (org.dbflute.dbmeta.info.ForeignInfo)4 HashSet (java.util.HashSet)3 ColumnFunctionCipher (org.dbflute.cbean.cipher.ColumnFunctionCipher)3 IllegalConditionBeanOperationException (org.dbflute.exception.IllegalConditionBeanOperationException)3 Method (java.lang.reflect.Method)2 LocalDate (java.time.LocalDate)2 LocalDateTime (java.time.LocalDateTime)2 Collection (java.util.Collection)2 Date (java.util.Date)2