Search in sources :

Example 61 with DBMeta

use of org.dbflute.dbmeta.DBMeta in project dbflute-core by dbflute.

the class UpdateOption method xcheckSpecifiedUpdateColumnPrimaryKey.

// -----------------------------------------------------
// Update Process
// --------------
public void xcheckSpecifiedUpdateColumnPrimaryKey() {
    // checked later by process if it needs
    if (_updateColumnSpecification == null) {
        return;
    }
    assertUpdateColumnSpecifiedCB();
    final CB cb = _updateColumnSpecifiedCB;
    final String basePointAliasName = cb.getSqlClause().getBasePointAliasName();
    final DBMeta dbmeta = cb.asDBMeta();
    if (dbmeta.hasPrimaryKey()) {
        final PrimaryInfo pkInfo = dbmeta.getPrimaryInfo();
        final List<ColumnInfo> pkList = pkInfo.getPrimaryColumnList();
        for (ColumnInfo pk : pkList) {
            final String columnDbName = pk.getColumnDbName();
            if (cb.getSqlClause().hasSpecifiedSelectColumn(basePointAliasName, columnDbName)) {
                String msg = "PK columns should not be allowed to specify as update columns: " + columnDbName;
                throw new SpecifyUpdateColumnInvalidException(msg);
            }
        }
    }
}
Also used : DBMeta(org.dbflute.dbmeta.DBMeta) ColumnInfo(org.dbflute.dbmeta.info.ColumnInfo) SpecifyUpdateColumnInvalidException(org.dbflute.exception.SpecifyUpdateColumnInvalidException) PrimaryInfo(org.dbflute.dbmeta.info.PrimaryInfo)

Example 62 with DBMeta

use of org.dbflute.dbmeta.DBMeta in project dbflute-core by dbflute.

the class UpdateOption method xacceptUpdateColumnModifiedPropertiesIfNeeds.

// -----------------------------------------------------
// Modified Properties
// -------------------
// for BatchUpdate
public void xacceptUpdateColumnModifiedPropertiesIfNeeds(List<? extends Entity> entityList) {
    // internal
    if (entityList == null) {
        throw new IllegalArgumentException("The argument 'entityList' should not be null.");
    }
    if (_updateColumnSpecification != null) {
        // already specified
        return;
    }
    if (entityList.isEmpty()) {
        // do nothing
        return;
    }
    if (xisCompatibleBatchUpdateDefaultEveryColumn()) {
        // every column for compatible
        return;
    }
    final Entity firstEntity = entityList.get(0);
    final Set<String> targetProps = xgatherUpdateColumnModifiedProperties(entityList, firstEntity);
    final DBMeta dbmeta = firstEntity.asDBMeta();
    specify(new SpecifyQuery<CB>() {

        public void specify(CB cb) {
            // you don't need to specify primary key because primary key has special handling
            for (String prop : targetProps) {
                final ColumnInfo info = dbmeta.findColumnInfo(prop);
                if (!info.isPrimary()) {
                    // except PK
                    cb.localSp().xspecifyColumn(info.getColumnDbName());
                }
            }
        }
    });
}
Also used : Entity(org.dbflute.Entity) DBMeta(org.dbflute.dbmeta.DBMeta) ColumnInfo(org.dbflute.dbmeta.info.ColumnInfo)

Example 63 with DBMeta

use of org.dbflute.dbmeta.DBMeta 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 64 with DBMeta

use of org.dbflute.dbmeta.DBMeta 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)

Aggregations

DBMeta (org.dbflute.dbmeta.DBMeta)64 ColumnInfo (org.dbflute.dbmeta.info.ColumnInfo)32 ArrayList (java.util.ArrayList)13 ForeignInfo (org.dbflute.dbmeta.info.ForeignInfo)13 List (java.util.List)12 LinkedHashMap (java.util.LinkedHashMap)11 ColumnSqlName (org.dbflute.dbmeta.name.ColumnSqlName)10 Entity (org.dbflute.Entity)9 PrimaryInfo (org.dbflute.dbmeta.info.PrimaryInfo)9 ReferrerInfo (org.dbflute.dbmeta.info.ReferrerInfo)9 Method (java.lang.reflect.Method)8 ReflectionFailureException (org.dbflute.util.DfReflectionUtil.ReflectionFailureException)8 Map (java.util.Map)7 ColumnFunctionCipher (org.dbflute.cbean.cipher.ColumnFunctionCipher)7 RelationInfo (org.dbflute.dbmeta.info.RelationInfo)7 LocalDate (java.time.LocalDate)6 LocalDateTime (java.time.LocalDateTime)6 Collection (java.util.Collection)6 Date (java.util.Date)6 HashMap (java.util.HashMap)6