Search in sources :

Example 21 with ColumnSqlName

use of org.dbflute.dbmeta.name.ColumnSqlName in project dbflute-core by dbflute.

the class DerivedReferrer method buildUnionMainPartClause.

protected String buildUnionMainPartClause(ColumnSqlName relatedColumnSqlName, String tableAliasName, ColumnRealName derivedColumnRealName, ColumnSqlName derivedColumnSqlName, String correlatedFixedCondition) {
    // derivedColumnSqlName : e.g. PURCHASE_PRICE
    // derivedRealSqlName   : might be sub-query
    final ColumnSqlName derivedRealSqlName = derivedColumnRealName.getColumnSqlName();
    final StringBuilder keySb = new StringBuilder();
    if (isFixedCondition(correlatedFixedCondition)) {
        setupUnionMainForFixedCondition(keySb, tableAliasName, derivedColumnSqlName);
    } else {
        // no fixed condition, mainly here
        final List<ColumnInfo> pkList = _subQueryDBMeta.getPrimaryInfo().getPrimaryColumnList();
        for (ColumnInfo pk : pkList) {
            final ColumnSqlName pkSqlName = pk.getColumnSqlName();
            if (pkSqlName.equals(derivedRealSqlName) || pkSqlName.equals(relatedColumnSqlName)) {
                // to suppress same columns selected
                continue;
            }
            keySb.append(keySb.length() > 0 ? ", " : "");
            keySb.append(ColumnRealName.create(tableAliasName, pk.getColumnSqlName()));
        }
        if (!relatedColumnSqlName.equals(derivedRealSqlName)) {
            // to suppress same columns selected
            keySb.append(keySb.length() > 0 ? ", " : "");
            keySb.append(ColumnRealName.create(tableAliasName, relatedColumnSqlName));
        }
    }
    setupUnionMainForDerivedColumn(keySb, derivedColumnRealName, derivedColumnSqlName, derivedRealSqlName);
    return completeUnionMainWholeClause(tableAliasName, keySb);
}
Also used : ColumnSqlName(org.dbflute.dbmeta.name.ColumnSqlName) ColumnInfo(org.dbflute.dbmeta.info.ColumnInfo)

Example 22 with ColumnSqlName

use of org.dbflute.dbmeta.name.ColumnSqlName in project dbflute-core by dbflute.

the class DerivedReferrer method buildSubQueryClause.

// ===================================================================================
// (Single PrimaryKey) SubQuery Clause
// ===================================
/**
 * Build the clause of sub-query by single primary key.
 * @param function The expression for deriving function. (NotNull)
 * @param correlatedColumnRealName The real names of correlated column that is main-query table's column. (NotNull)
 * @param relatedColumnSqlName The real names of related column that is sub-query table's column. (NotNull)
 * @param correlatedFixedCondition The fixed condition as correlated condition. (NullAllowed)
 * @param option The option of DerivedReferrer. (NotNull)
 * @return The clause of sub-query. (NotNull)
 */
protected String buildSubQueryClause(String function, ColumnRealName correlatedColumnRealName, ColumnSqlName relatedColumnSqlName, String correlatedFixedCondition, DerivedReferrerOption option) {
    final String tableAliasName = getSubQueryLocalAliasName();
    final ColumnSqlName derivedColumnSqlName = getDerivedColumnSqlName();
    if (derivedColumnSqlName == null) {
        throwDerivedReferrerInvalidColumnSpecificationException(function);
    }
    final ColumnRealName derivedColumnRealName = getDerivedColumnRealName();
    final String subQueryClause;
    if (_subQuerySqlClause.hasUnionQuery()) {
        subQueryClause = buildUnionSubQueryClause(function, correlatedColumnRealName, relatedColumnSqlName, option, tableAliasName, derivedColumnRealName, derivedColumnSqlName, correlatedFixedCondition);
    } else {
        final String selectClause = "select " + buildFunctionPart(function, derivedColumnRealName, option, false);
        final String fromWhereClause;
        if (option.isSuppressCorrelation()) {
            // e.g. myselfDerived
            fromWhereClause = buildPlainFromWhereClause(selectClause, tableAliasName, correlatedFixedCondition);
        } else {
            // basically here
            fromWhereClause = buildCorrelationFromWhereClause(selectClause, tableAliasName, correlatedColumnRealName, relatedColumnSqlName, correlatedFixedCondition);
        }
        subQueryClause = selectClause + " " + fromWhereClause;
    }
    return resolveSubQueryLevelVariable(subQueryClause);
}
Also used : ColumnSqlName(org.dbflute.dbmeta.name.ColumnSqlName) ColumnRealName(org.dbflute.dbmeta.name.ColumnRealName)

Example 23 with ColumnSqlName

use of org.dbflute.dbmeta.name.ColumnSqlName in project dbflute-core by dbflute.

the class DerivedReferrer method buildUnionMainPartClause.

protected String buildUnionMainPartClause(ColumnRealName[] correlatedColumnRealNames, ColumnSqlName[] relatedColumnSqlNames, String tableAliasName, ColumnRealName derivedColumnRealName, ColumnSqlName derivedColumnSqlName, String correlatedFixedCondition) {
    // similar to simple primary key so refer it
    final StringBuilder keySb = new StringBuilder();
    final ColumnSqlName derivedRealSqlName = derivedColumnRealName.getColumnSqlName();
    if (isFixedCondition(correlatedFixedCondition)) {
        setupUnionMainForFixedCondition(keySb, tableAliasName, derivedColumnSqlName);
    } else {
        // no fixed condition, mainly here
        final Set<ColumnSqlName> relatedColumnSqlSet = new HashSet<ColumnSqlName>();
        for (ColumnSqlName columnSqlName : relatedColumnSqlNames) {
            relatedColumnSqlSet.add(columnSqlName);
        }
        final ColumnSqlName derivedSqlName = derivedRealSqlName;
        final List<ColumnInfo> pkList = _subQueryDBMeta.getPrimaryInfo().getPrimaryColumnList();
        for (ColumnInfo pk : pkList) {
            final ColumnSqlName pkSqlName = pk.getColumnSqlName();
            if (pkSqlName.equals(derivedSqlName) || relatedColumnSqlSet.contains(pkSqlName)) {
                // to suppress same columns selected
                continue;
            }
            keySb.append(keySb.length() > 0 ? ", " : "");
            keySb.append(ColumnRealName.create(tableAliasName, pk.getColumnSqlName()));
        }
        for (ColumnSqlName relatedSqlName : relatedColumnSqlNames) {
            if (relatedSqlName.equals(derivedSqlName)) {
                // to suppress same columns selected
                continue;
            }
            keySb.append(keySb.length() > 0 ? ", " : "");
            keySb.append(ColumnRealName.create(tableAliasName, relatedSqlName));
        }
    }
    setupUnionMainForDerivedColumn(keySb, derivedColumnRealName, derivedColumnSqlName, derivedRealSqlName);
    return completeUnionMainWholeClause(tableAliasName, keySb);
}
Also used : ColumnSqlName(org.dbflute.dbmeta.name.ColumnSqlName) ColumnInfo(org.dbflute.dbmeta.info.ColumnInfo) HashSet(java.util.HashSet)

Example 24 with ColumnSqlName

use of org.dbflute.dbmeta.name.ColumnSqlName in project dbflute-core by dbflute.

the class SqlClauseOracle method lockForUpdate.

// ===================================================================================
// Lock Override
// =============
/**
 * {@inheritDoc}
 */
public void lockForUpdate() {
    final DBMeta dbmeta = findDBMeta(_tableDbName);
    final String basePointAliasName = getBasePointAliasName();
    if (dbmeta.hasPrimaryKey()) {
        final ColumnSqlName primaryKeyName = dbmeta.getPrimaryInfo().getFirstColumn().getColumnSqlName();
        _lockSqlSuffix = " for update of " + basePointAliasName + "." + primaryKeyName;
    } else {
        final ColumnSqlName randomColumnName = ((ColumnInfo) dbmeta.getColumnInfoList().get(0)).getColumnSqlName();
        _lockSqlSuffix = " for update of " + basePointAliasName + "." + randomColumnName;
    }
}
Also used : ColumnSqlName(org.dbflute.dbmeta.name.ColumnSqlName) DBMeta(org.dbflute.dbmeta.DBMeta) ColumnInfo(org.dbflute.dbmeta.info.ColumnInfo)

Example 25 with ColumnSqlName

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

Aggregations

ColumnSqlName (org.dbflute.dbmeta.name.ColumnSqlName)28 ColumnInfo (org.dbflute.dbmeta.info.ColumnInfo)14 ColumnRealName (org.dbflute.dbmeta.name.ColumnRealName)8 DBMeta (org.dbflute.dbmeta.DBMeta)5 SpecifiedColumn (org.dbflute.cbean.dream.SpecifiedColumn)4 IllegalConditionBeanOperationException (org.dbflute.exception.IllegalConditionBeanOperationException)3 TnPropertyType (org.dbflute.s2dao.metadata.TnPropertyType)3 LinkedHashMap (java.util.LinkedHashMap)2 SqlClause (org.dbflute.cbean.sqlclause.SqlClause)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 ConditionBean (org.dbflute.cbean.ConditionBean)1 ColumnFunctionCipher (org.dbflute.cbean.cipher.ColumnFunctionCipher)1 SelectedRelationColumn (org.dbflute.cbean.sqlclause.select.SelectedRelationColumn)1 OnQueryStringConnector (org.dbflute.dbway.OnQueryStringConnector)1 StringKeyMap (org.dbflute.helper.StringKeyMap)1 ValueType (org.dbflute.jdbc.ValueType)1 TnPropertyTypeImpl (org.dbflute.s2dao.metadata.impl.TnPropertyTypeImpl)1