Search in sources :

Example 1 with SpecifiedColumn

use of org.dbflute.cbean.dream.SpecifiedColumn in project dbflute-core by dbflute.

the class AbstractSqlClause method processSelectClauseLocal.

protected int processSelectClauseLocal(StringBuilder sb) {
    final String basePointAliasName = getBasePointAliasName();
    final DBMeta dbmeta = getDBMeta();
    final Map<String, SpecifiedColumn> localSpecifiedMap;
    if (_specifiedSelectColumnMap != null) {
        localSpecifiedMap = _specifiedSelectColumnMap.get(basePointAliasName);
    } else {
        localSpecifiedMap = null;
    }
    final List<ColumnInfo> columnInfoList;
    final boolean validSpecifiedLocal;
    final boolean selectClauseTypeUniqueScalar = isSelectClauseTypeUniqueScalar();
    // unique scalar, has PK, specified scalar, specified column
    ColumnInfo specifiedUniqueScalarColumnInfo = null;
    if (selectClauseTypeUniqueScalar) {
        // it always has union-query because it's handled before this process
        if (dbmeta.hasPrimaryKey()) {
            columnInfoList = new ArrayList<ColumnInfo>();
            columnInfoList.addAll(dbmeta.getPrimaryInfo().getPrimaryColumnList());
            if (isSelectClauseTypeSpecifiedScalar()) {
                final ColumnInfo specifiedColumn = getSpecifiedColumnInfoAsOne();
                if (specifiedColumn != null && !specifiedColumn.isPrimary()) {
                    specifiedUniqueScalarColumnInfo = specifiedColumn;
                    columnInfoList.add(specifiedColumn);
                }
            // derivingSubQuery is handled after this process
            }
        } else {
            // all columns are target if no-PK and unique-scalar and union-query
            columnInfoList = dbmeta.getColumnInfoList();
        }
        // because specified columns are fixed here
        validSpecifiedLocal = false;
    } else {
        columnInfoList = dbmeta.getColumnInfoList();
        validSpecifiedLocal = localSpecifiedMap != null && !localSpecifiedMap.isEmpty();
    }
    // because 1 origin in JDBC
    int selectIndex = 0;
    boolean needsDelimiter = false;
    for (ColumnInfo columnInfo : columnInfoList) {
        final String columnDbName = columnInfo.getColumnDbName();
        final ColumnSqlName columnSqlName = columnInfo.getColumnSqlName();
        if (_pkOnlySelectForcedlyEnabled && !columnInfo.isPrimary()) {
            continue;
        }
        if (validSpecifiedLocal && !localSpecifiedMap.containsKey(columnDbName)) {
            // a case for scalar-select has been already resolved here
            continue;
        }
        if (canBeNullObjectSpecifiedColumn(columnInfo)) {
            registerColumnNullObject(basePointAliasName, columnInfo);
            continue;
        }
        if (needsDelimiter) {
            sb.append(", ");
        } else {
            sb.append("select");
            appendSelectHint(sb);
            sb.append(" ");
            needsDelimiter = true;
        }
        final String realAliasName;
        if (selectClauseTypeUniqueScalar) {
            if (specifiedUniqueScalarColumnInfo != null && columnInfo.equals(specifiedUniqueScalarColumnInfo)) {
                // has PK, specified column in unique scalar
                // might be relation column
                realAliasName = getSpecifiedColumnTableAliasNameAsOne();
            } else {
                // has no PK
                realAliasName = basePointAliasName;
            }
        } else {
            realAliasName = basePointAliasName;
        }
        final String realColumnName = realAliasName + "." + columnSqlName;
        final String onQueryName;
        ++selectIndex;
        if (_useSelectIndex) {
            final String entityNo = BASE_POINT_HANDLING_ENTITY_NO;
            onQueryName = buildSelectIndexAlias(columnSqlName, null, selectIndex, entityNo);
            registerSelectIndex(entityNo, columnDbName, onQueryName, selectIndex);
        } else {
            onQueryName = columnSqlName.toString();
        }
        sb.append(decryptSelectColumnIfNeeds(columnInfo, realColumnName)).append(" as ").append(onQueryName);
        getSelectClauseRealColumnAliasMap().put(realColumnName, onQueryName);
        if (validSpecifiedLocal && localSpecifiedMap.containsKey(columnDbName)) {
            final SpecifiedColumn specifiedColumn = localSpecifiedMap.get(columnDbName);
            // basically for queryInsert()
            specifiedColumn.setOnQueryName(onQueryName);
        }
    }
    return selectIndex;
}
Also used : ColumnSqlName(org.dbflute.dbmeta.name.ColumnSqlName) DBMeta(org.dbflute.dbmeta.DBMeta) ColumnInfo(org.dbflute.dbmeta.info.ColumnInfo) SpecifiedColumn(org.dbflute.cbean.dream.SpecifiedColumn)

Example 2 with SpecifiedColumn

use of org.dbflute.cbean.dream.SpecifiedColumn in project dbflute-core by dbflute.

the class AbstractSqlClause method processSelectClauseRelation.

protected int processSelectClauseRelation(StringBuilder sb, int selectIndex) {
    if (_pkOnlySelectForcedlyEnabled) {
        return selectIndex;
    }
    for (Entry<String, Map<String, SelectedRelationColumn>> entry : getSelectedRelationColumnMap().entrySet()) {
        final String tableAliasName = entry.getKey();
        final Map<String, SelectedRelationColumn> relationColumnMap = entry.getValue();
        Map<String, SpecifiedColumn> foreginSpecifiedMap = null;
        if (_specifiedSelectColumnMap != null) {
            foreginSpecifiedMap = _specifiedSelectColumnMap.get(tableAliasName);
        }
        final boolean validSpecifiedForeign = foreginSpecifiedMap != null && !foreginSpecifiedMap.isEmpty();
        boolean finishedForeignIndent = false;
        for (SelectedRelationColumn selectColumnInfo : relationColumnMap.values()) {
            final ColumnInfo columnInfo = selectColumnInfo.getColumnInfo();
            final String columnDbName = columnInfo.getColumnDbName();
            if (validSpecifiedForeign && !foreginSpecifiedMap.containsKey(columnDbName)) {
                continue;
            }
            if (canBeNullObjectSpecifiedColumn(columnInfo)) {
                registerColumnNullObject(tableAliasName, columnInfo);
                continue;
            }
            final String realColumnName = selectColumnInfo.buildRealColumnSqlName();
            final String columnAliasName = selectColumnInfo.buildColumnAliasName();
            final String relationNoSuffix = selectColumnInfo.getRelationNoSuffix();
            final String onQueryName;
            ++selectIndex;
            if (_useSelectIndex) {
                final ColumnSqlName columnSqlName = columnInfo.getColumnSqlName();
                onQueryName = buildSelectIndexAlias(columnSqlName, columnAliasName, selectIndex, relationNoSuffix);
                registerSelectIndex(relationNoSuffix, columnAliasName, onQueryName, selectIndex);
            } else {
                onQueryName = columnAliasName;
            }
            if (!finishedForeignIndent) {
                sb.append(ln()).append("     ");
                finishedForeignIndent = true;
            }
            sb.append(", ");
            sb.append(decryptSelectColumnIfNeeds(columnInfo, realColumnName)).append(" as ").append(onQueryName);
            getSelectClauseRealColumnAliasMap().put(realColumnName, onQueryName);
            if (validSpecifiedForeign && foreginSpecifiedMap.containsKey(columnDbName)) {
                final SpecifiedColumn specifiedColumn = foreginSpecifiedMap.get(columnDbName);
                // basically for queryInsert()
                specifiedColumn.setOnQueryName(onQueryName);
            }
        }
    }
    return selectIndex;
}
Also used : ColumnSqlName(org.dbflute.dbmeta.name.ColumnSqlName) SelectedRelationColumn(org.dbflute.cbean.sqlclause.select.SelectedRelationColumn) ColumnInfo(org.dbflute.dbmeta.info.ColumnInfo) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) StringKeyMap(org.dbflute.helper.StringKeyMap) SpecifiedColumn(org.dbflute.cbean.dream.SpecifiedColumn)

Example 3 with SpecifiedColumn

use of org.dbflute.cbean.dream.SpecifiedColumn in project dbflute-core by dbflute.

the class AbstractSqlClause method getClauseQueryInsert.

// ===================================================================================
// Query Update
// ============
// -----------------------------------------------------
// Query Insert
// ------------
public String getClauseQueryInsert(Map<String, String> fixedValueQueryExpMap, SqlClause resourceSqlClause) {
    // at first, this should be called (before on-query name handling)
    // because an on-query name of mapped info are set in this process
    final String resourceViewClause = resourceSqlClause.getClause();
    if (_specifiedSelectColumnMap == null) {
        String msg = "The specified columns for query-insert are required.";
        throw new IllegalConditionBeanOperationException(msg);
    }
    final Map<String, SpecifiedColumn> elementMap = _specifiedSelectColumnMap.get(getBasePointAliasName());
    if (elementMap == null || elementMap.isEmpty()) {
        String msg = "The specified columns of inserted table for query-insert are required.";
        throw new IllegalConditionBeanOperationException(msg);
    }
    final DBMeta dbmeta = getDBMeta();
    final StringBuilder intoSb = new StringBuilder();
    final StringBuilder selectSb = new StringBuilder();
    final String resourceAlias = "dfres";
    int index = 0;
    final List<ColumnInfo> columnInfoList = dbmeta.getColumnInfoList();
    for (ColumnInfo columnInfo : columnInfoList) {
        final String columnDbName = columnInfo.getColumnDbName();
        final SpecifiedColumn specifiedColumn = elementMap.get(columnDbName);
        final String onQueryName;
        if (specifiedColumn != null) {
            onQueryName = specifiedColumn.getValidMappedOnQueryName();
        } else if (fixedValueQueryExpMap.containsKey(columnDbName)) {
            final String fixedValueQueryExp = fixedValueQueryExpMap.get(columnDbName);
            if (fixedValueQueryExp != null) {
                onQueryName = encryptIfNeeds(columnInfo, fixedValueQueryExp);
            } else {
                // it uses null literal on query
                // because the SQL analyzer blocks null parameters
                // (the analyzer should do it for condition-bean)
                onQueryName = "null";
            }
        } else {
            continue;
        }
        if (onQueryName == null || onQueryName.trim().length() == 0) {
            // no way
            String msg = "The on-query name for query-insert is required: " + specifiedColumn;
            throw new IllegalConditionBeanOperationException(msg);
        }
        final ColumnSqlName columnSqlName = columnInfo.getColumnSqlName();
        if (index > 0) {
            intoSb.append(", ");
            selectSb.append(", ");
        }
        intoSb.append(columnSqlName);
        if (specifiedColumn != null) {
            selectSb.append(resourceAlias).append(".");
        }
        selectSb.append(onQueryName);
        ++index;
    }
    final String subQueryIdentity = "queryInsertResource";
    final String subQueryBeginMark = resolveSubQueryBeginMark(subQueryIdentity);
    final String subQueryEndMark = resolveSubQueryEndMark(subQueryIdentity);
    final StringBuilder mainSb = new StringBuilder();
    mainSb.append("insert into ").append(dbmeta.getTableSqlName());
    mainSb.append(" (").append(intoSb).append(")").append(ln());
    mainSb.append("select ").append(selectSb).append(ln());
    mainSb.append("  from (").append(subQueryBeginMark).append(ln());
    mainSb.append(resourceViewClause).append(ln());
    mainSb.append("       ) ").append(resourceAlias).append(subQueryEndMark);
    final String sql = mainSb.toString();
    return processSubQueryIndent(sql);
}
Also used : ColumnSqlName(org.dbflute.dbmeta.name.ColumnSqlName) DBMeta(org.dbflute.dbmeta.DBMeta) IllegalConditionBeanOperationException(org.dbflute.exception.IllegalConditionBeanOperationException) ColumnInfo(org.dbflute.dbmeta.info.ColumnInfo) SpecifiedColumn(org.dbflute.cbean.dream.SpecifiedColumn)

Example 4 with SpecifiedColumn

use of org.dbflute.cbean.dream.SpecifiedColumn in project dbflute-core by dbflute.

the class AbstractConditionQuery method doRegisterLikeSearchQueryCompoundOptimized.

protected void doRegisterLikeSearchQueryCompoundOptimized(String value, ConditionValue cvalue, String columnDbName, LikeSearchOption option) {
    if (!option.isLikePrefix()) {
        String msg = "This optimization is only for LikePrefix: " + option;
        throw new IllegalStateException(msg);
    }
    // *char type only but no checked (cannot check)
    final List<SpecifiedColumn> compoundColumnList = option.getCompoundColumnList();
    final List<Integer> sizeList = option.getCompoundColumnSizeList();
    String currentValue = value;
    int currentLength = value.length();
    String currentColumn = columnDbName;
    final boolean needsAndPart = isOrScopeQueryDirectlyUnder();
    if (needsAndPart) {
        xgetSqlClause().beginOrScopeQueryAndPart();
    }
    try {
        boolean shortLengthBreak = false;
        final Iterator<SpecifiedColumn> compoundColumnIterator = compoundColumnList.iterator();
        for (Integer columnSize : sizeList) {
            // should be less or equal column count (checked in option)
            if (currentLength >= columnSize) {
                // can treat current condition as equal
                final String equalValue = currentValue.substring(0, columnSize);
                invokeQueryEqual(currentColumn, equalValue);
                currentValue = currentValue.substring(columnSize);
                currentLength = currentValue.length();
                final SpecifiedColumn specifiedColumn;
                if (compoundColumnIterator.hasNext()) {
                    specifiedColumn = compoundColumnIterator.next();
                    currentColumn = specifiedColumn.getColumnDbName();
                } else {
                    // means just size
                    // means end
                    currentColumn = null;
                    // though basically no need to break because of size loop end
                    break;
                }
            } else {
                // short length condition value
                shortLengthBreak = true;
                break;
            }
        }
        if (currentValue.length() > 0 && currentColumn != null) {
            // double check
            final LikeSearchOption copyOption = option.createDeepCopy();
            // also fixed sizes cleared
            copyOption.clearCompoundColumn();
            if (!shortLengthBreak) {
                while (compoundColumnIterator.hasNext()) {
                    copyOption.addCompoundColumn(compoundColumnIterator.next());
                }
            }
            invokeQueryLikeSearch(currentColumn, currentValue, copyOption);
        }
    } finally {
        if (needsAndPart) {
            xgetSqlClause().endOrScopeQueryAndPart();
        }
    }
}
Also used : LikeSearchOption(org.dbflute.cbean.coption.LikeSearchOption) SpecifiedColumn(org.dbflute.cbean.dream.SpecifiedColumn)

Example 5 with SpecifiedColumn

use of org.dbflute.cbean.dream.SpecifiedColumn in project dbflute-core by dbflute.

the class HpCalcSpecification method getResolvedSpecifiedColumnDbName.

// -----------------------------------------------------
// Column Name
// -----------
/**
 * @return The column DB name of specified resolved column. (NullAllowed)
 */
public String getResolvedSpecifiedColumnDbName() {
    // resolved plain or deriving sub-query
    checkSpecifiedCB();
    if (_specifedCB.xhasDreamCruiseTicket()) {
        final SpecifiedColumn ticket = _specifedCB.xshowDreamCruiseTicket();
        return ticket.getColumnDbName();
    }
    final ColumnInfo columnInfo = getResolvedSpecifiedColumnInfo();
    return columnInfo != null ? columnInfo.getColumnDbName() : null;
}
Also used : ColumnInfo(org.dbflute.dbmeta.info.ColumnInfo) SpecifiedColumn(org.dbflute.cbean.dream.SpecifiedColumn)

Aggregations

SpecifiedColumn (org.dbflute.cbean.dream.SpecifiedColumn)20 ColumnInfo (org.dbflute.dbmeta.info.ColumnInfo)7 ColumnSqlName (org.dbflute.dbmeta.name.ColumnSqlName)4 ConditionBean (org.dbflute.cbean.ConditionBean)3 SqlClause (org.dbflute.cbean.sqlclause.SqlClause)3 IllegalConditionBeanOperationException (org.dbflute.exception.IllegalConditionBeanOperationException)3 DBMeta (org.dbflute.dbmeta.DBMeta)2 ColumnRealName (org.dbflute.dbmeta.name.ColumnRealName)2 ExceptionMessageBuilder (org.dbflute.helper.message.ExceptionMessageBuilder)2 Method (java.lang.reflect.Method)1 LocalDate (java.time.LocalDate)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 CalculationType (org.dbflute.cbean.chelper.HpCalcElement.CalculationType)1 ColumnConversionOption (org.dbflute.cbean.coption.ColumnConversionOption)1 LikeSearchOption (org.dbflute.cbean.coption.LikeSearchOption)1 OrderByElement (org.dbflute.cbean.sqlclause.orderby.OrderByElement)1