Search in sources :

Example 1 with CalculationType

use of org.dbflute.cbean.chelper.HpCalcElement.CalculationType in project dbflute-core by dbflute.

the class HpCalcSpecification method buildCalculationExp.

/**
 * @param targetExp The expression of target column already handled cipher. (NotNull)
 * @param columnAliasMap The map of column alias. (NullAllowed)
 * @param calculation The element of calculation. (NotNull)
 * @param removeCalcAlias Does it remove alias of calculation column.
 * @return The expression of calculation statement. (NotNull)
 */
protected String buildCalculationExp(String targetExp, Map<String, String> columnAliasMap, HpCalcElement calculation, boolean removeCalcAlias) {
    final CalculationType calculationType = calculation.getCalculationType();
    if (calculationType.equals(CalculationType.CONV)) {
        // convert
        final ColumnConversionOption columnConversionOption = calculation.getColumnConversionOption();
        return columnConversionOption.filterFunction(targetExp);
    }
    // number value or number column here
    final Object calcValueExp;
    if (calculation.hasCalculationValue()) {
        // number value
        calcValueExp = calculation.getCalculationValue();
    } else if (calculation.hasCalculationColumn()) {
        // number column
        final SpecifiedColumn calculationColumn = calculation.getCalculationColumn();
        final String columnExp;
        if (removeCalcAlias) {
            // means e.g. plain update
            final String basePointAliasName = _baseCB.getSqlClause().getBasePointAliasName();
            if (!basePointAliasName.equals(calculationColumn.getTableAliasName())) {
                // may be relation column
                throwCalculationColumnRelationUnresolvedException(targetExp, calculationColumn);
            }
            columnExp = calculationColumn.toColumnSqlName().toString();
        } else {
            columnExp = calculationColumn.toColumnRealName().toString();
        }
        final Object baseExp;
        if (columnAliasMap != null) {
            // e.g. ManualOrder on union
            final String mappedAlias = columnAliasMap.get(columnExp);
            baseExp = mappedAlias != null ? mappedAlias : columnExp;
        } else {
            // e.g. ColumnQuery, UpdateOption, non-union ManualOrder, DerivedReferrer
            final ColumnInfo columnInfo = calculationColumn.getColumnInfo();
            baseExp = !calculationColumn.isDerived() ? decryptIfNeeds(columnInfo, columnExp) : columnExp;
        }
        calcValueExp = filterNestedCalculation(baseExp, calculationColumn);
    } else {
        throwCalculationElementIllegalStateException(targetExp);
        // unreachable
        return null;
    }
    return targetExp + " " + calculationType.operand() + " " + calcValueExp;
}
Also used : ColumnConversionOption(org.dbflute.cbean.coption.ColumnConversionOption) CalculationType(org.dbflute.cbean.chelper.HpCalcElement.CalculationType) ColumnInfo(org.dbflute.dbmeta.info.ColumnInfo) SpecifiedColumn(org.dbflute.cbean.dream.SpecifiedColumn)

Aggregations

CalculationType (org.dbflute.cbean.chelper.HpCalcElement.CalculationType)1 ColumnConversionOption (org.dbflute.cbean.coption.ColumnConversionOption)1 SpecifiedColumn (org.dbflute.cbean.dream.SpecifiedColumn)1 ColumnInfo (org.dbflute.dbmeta.info.ColumnInfo)1