Search in sources :

Example 16 with ColumnSqlName

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

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

the class AbstractSqlClause method buildQueryUpdateInScopeClause.

protected void buildQueryUpdateInScopeClause(Map<String, Object> columnParameterMap, DBMeta dbmeta, StringBuilder sb) {
    if (columnParameterMap != null) {
        buildQueryUpdateSetClause(columnParameterMap, dbmeta, sb, null);
    }
    final ColumnSqlName primaryKeyName = dbmeta.getPrimaryInfo().getFirstColumn().getColumnSqlName();
    final String selectClause = "select " + getBasePointAliasName() + "." + primaryKeyName;
    String fromWhereClause = getClauseFromWhereWithUnionTemplate();
    // Replace template marks. These are very important!
    fromWhereClause = replace(fromWhereClause, getUnionSelectClauseMark(), selectClause);
    fromWhereClause = replace(fromWhereClause, getUnionWhereClauseMark(), "");
    fromWhereClause = replace(fromWhereClause, getUnionWhereFirstConditionMark(), "");
    final String subQuery = processSubQueryIndent(selectClause + " " + fromWhereClause);
    sb.append(ln());
    sb.append(" where ").append(primaryKeyName);
    sb.append(" in (").append(ln()).append(subQuery);
    if (!subQuery.endsWith(ln())) {
        sb.append(ln());
    }
    sb.append(")");
}
Also used : ColumnSqlName(org.dbflute.dbmeta.name.ColumnSqlName)

Example 18 with ColumnSqlName

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

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

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

the class DerivedReferrer method setupUnionMainForFixedCondition.

protected void setupUnionMainForFixedCondition(StringBuilder keySb, String tableAliasName, ColumnSqlName derivedColumnSqlName) {
    // all columns because fixed condition might contain them
    final List<ColumnInfo> columnInfoList = _subQueryDBMeta.getColumnInfoList();
    for (ColumnInfo columnInfo : columnInfoList) {
        final ColumnSqlName sqlName = columnInfo.getColumnSqlName();
        if (sqlName.equals(derivedColumnSqlName)) {
            // to suppress same columns selected
            continue;
        }
        final ColumnRealName realName = ColumnRealName.create(tableAliasName, columnInfo.getColumnSqlName());
        keySb.append(keySb.length() > 0 ? ", " : "");
        keySb.append(realName);
    }
}
Also used : ColumnSqlName(org.dbflute.dbmeta.name.ColumnSqlName) ColumnInfo(org.dbflute.dbmeta.info.ColumnInfo) ColumnRealName(org.dbflute.dbmeta.name.ColumnRealName)

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