Search in sources :

Example 46 with ColumnInfo

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

the class AbstractSqlClause method buildSelectClauseSpecifiedScalar.

protected String buildSelectClauseSpecifiedScalar(String aliasName, String function) {
    final String columnAlias = getScalarSelectColumnAlias();
    final ColumnSqlName columnSqlName = getSpecifiedColumnSqlNameAsOne();
    if (columnSqlName != null) {
        // normal column
        final String valueExp = aliasName + "." + columnSqlName;
        if (hasUnionQuery() && hasSpecifyCalculation(getSpecifiedColumnAsOne())) {
            throwScalarSelectUnionQuerySpecifyCalculationUnsupportedException();
        }
        // not null here
        final ColumnInfo columnInfo = getSpecifiedColumnInfoAsOne();
        // calculation resolved
        final String functionExp = doBuildFunctionExp(function, columnInfo, valueExp);
        return "select " + functionExp + " as " + columnAlias;
    }
    final String subQuery = getSpecifiedDerivingSubQueryAsOne();
    if (subQuery != null) {
        // SpecifyCalculation is already resolved in DerivedReferrer process
        if (hasUnionQuery()) {
            final String valueExp = aliasName + "." + columnAlias;
            // not null here
            final ColumnInfo columnInfo = getSpecifiedDerivingColumnInfoAsOne();
            return "select " + doBuildFunctionExp(function, decryptSelectColumnIfNeeds(columnInfo, valueExp));
        } else {
            // adjusts alias definition target (move to function's scope)
            final String aliasDef = " as " + columnAlias;
            final StringBuilder sb = new StringBuilder();
            final String pureSubQuery = Srl.substringLastFront(subQuery, aliasDef);
            // just in case
            final String aliasDefRear = Srl.substringLastRear(subQuery, aliasDef);
            final String functionExp = doBuildFunctionExp(function, pureSubQuery);
            sb.append("select ").append(functionExp).append(aliasDef).append(aliasDefRear);
            return sb.toString();
        }
    }
    String msg = "Not found specifed column for scalar: function=" + function;
    // basically no way (checked before)
    throw new IllegalStateException(msg);
}
Also used : ColumnSqlName(org.dbflute.dbmeta.name.ColumnSqlName) ColumnInfo(org.dbflute.dbmeta.info.ColumnInfo)

Example 47 with ColumnInfo

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

the class AbstractSqlClause method buildQueryUpdateSetClause.

protected void buildQueryUpdateSetClause(Map<String, Object> columnParameterMap, DBMeta dbmeta, StringBuilder sb, String aliasName) {
    if (columnParameterMap == null) {
        String msg = "The argument 'columnParameterMap' should not be null.";
        throw new IllegalArgumentException(msg);
    }
    sb.append(ln());
    int index = 0;
    final int mapSize = columnParameterMap.size();
    for (Entry<String, Object> entry : columnParameterMap.entrySet()) {
        final String columnName = entry.getKey();
        final Object parameter = entry.getValue();
        final ColumnInfo columnInfo = dbmeta.findColumnInfo(columnName);
        final ColumnSqlName columnSqlName = columnInfo.getColumnSqlName();
        if (index == 0) {
            sb.append("   set ");
        } else {
            sb.append("     , ");
        }
        if (aliasName != null) {
            sb.append(aliasName);
        }
        sb.append(columnSqlName).append(" = ");
        final String valueExp;
        if (parameter instanceof QueryUpdateSetCalculationHandler) {
            final QueryUpdateSetCalculationHandler handler = (QueryUpdateSetCalculationHandler) parameter;
            valueExp = handler.buildStatement(aliasName);
        } else {
            valueExp = parameter.toString();
        }
        sb.append(encryptIfNeeds(columnInfo, valueExp));
        if (mapSize - 1 > index) {
            // before last loop
            sb.append(ln());
        }
        ++index;
    }
}
Also used : ColumnSqlName(org.dbflute.dbmeta.name.ColumnSqlName) ColumnInfo(org.dbflute.dbmeta.info.ColumnInfo)

Example 48 with ColumnInfo

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

the class ScalarCondition method buildScalarCondition.

// ===================================================================================
// Build Clause
// ============
public String buildScalarCondition(String function, ScalarConditionOption option) {
    setupOptionAttribute(option);
    // get the specified column before it disappears at sub-query making.
    final ColumnRealName columnRealName;
    {
        final String columnDbName = _subQuerySqlClause.getSpecifiedColumnDbNameAsOne();
        if (columnDbName == null || columnDbName.trim().length() == 0) {
            throwScalarConditionInvalidColumnSpecificationException(function);
        }
        columnRealName = _localRealNameProvider.provide(columnDbName);
    }
    final String subQueryClause = buildSubQueryClause(function, option);
    final String beginMark = resolveSubQueryBeginMark(_subQueryIdentity) + ln();
    final String endMark = resolveSubQueryEndMark(_subQueryIdentity);
    final String endIndent = "       ";
    final ColumnInfo columnInfo = _subQuerySqlClause.getSpecifiedColumnInfoAsOne();
    final String specifiedExp = decrypt(columnInfo, columnRealName.toString());
    return // left and operand
    specifiedExp + " " + _operand + " (" + beginMark + subQueryClause + ln() + endIndent + ") " + // right
    endMark;
}
Also used : ColumnInfo(org.dbflute.dbmeta.info.ColumnInfo) ColumnRealName(org.dbflute.dbmeta.name.ColumnRealName)

Example 49 with ColumnInfo

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

the class ScalarCondition method buildUnionMainPartClause.

// almost same as derived-referrer's logic
protected String buildUnionMainPartClause(ColumnSqlName relatedColumnSqlName, String tableAliasName, ColumnRealName derivedColumnRealName, ColumnSqlName derivedColumnSqlName) {
    // derivedColumnSqlName : e.g. PURCHASE_PRICE
    // derivedRealSqlName   : might be sub-query
    final ColumnSqlName derivedRealSqlName = derivedColumnRealName.getColumnSqlName();
    final StringBuilder keySb = new StringBuilder();
    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 != null && !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 50 with ColumnInfo

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

the class ScalarCondition method setupOptionAttribute.

protected void setupOptionAttribute(ScalarConditionOption option) {
    ColumnInfo columnInfo = _subQuerySqlClause.getSpecifiedColumnInfoAsOne();
    if (columnInfo == null) {
        columnInfo = _subQuerySqlClause.getSpecifiedDerivingColumnInfoAsOne();
    }
    // basically not null (checked before)
    option.xsetTargetColumnInfo(columnInfo);
    option.xjudgeDatabase(_subQuerySqlClause);
}
Also used : 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