Search in sources :

Example 1 with TableSqlName

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

the class HpFixedConditionQueryResolver method resolveFixedInlineView.

// ===================================================================================
// Resolve Fixed InlineView
// ========================
public String resolveFixedInlineView(String foreignTableSqlName, boolean treatedAsInnerJoin) {
    // so it can uses bridge variables here
    if (_inlineViewResourceMap == null || _inlineViewResourceMap.isEmpty()) {
        // not uses InlineView
        return foreignTableSqlName;
    }
    // alias is required because foreignTableSqlName may be (normal) InlineView
    final String baseAlias = "dffixedbase";
    final String baseIndent;
    if (treatedAsInnerJoin) {
        // ----------"    inner join "
        baseIndent = "               ";
    } else {
        // ----------"    left outer join "
        baseIndent = "                    ";
    }
    final StringBuilder joinSb = new StringBuilder();
    final Map<ForeignInfo, String> relationMap = new HashMap<ForeignInfo, String>();
    final List<String> additionalRealColumnList = new ArrayList<String>();
    // basically not null
    final String resolvedFixedCondition = _resolvedFixedCondition;
    String optimizedCondition = _inlineViewOptimizedCondition;
    int groupIndex = 0;
    for (InlineViewResource resource : _inlineViewResourceMap.values()) {
        final List<ForeignInfo> joinInfoList = resource.getJoinInfoList();
        final String aliasBase = "dffixedjoin";
        String preForeignAlias = null;
        String foreignAlias = null;
        int joinIndex = 0;
        final Map<ForeignInfo, String> foreignAliasMap = new HashMap<ForeignInfo, String>(joinInfoList.size());
        for (ForeignInfo joinInfo : joinInfoList) {
            if (relationMap.containsKey(joinInfo)) {
                // already joined
                // update previous alias
                preForeignAlias = relationMap.get(joinInfo);
                continue;
            }
            final TableSqlName foreignTable;
            final String localAlias;
            {
                final DBMeta foreignDBMeta = joinInfo.getForeignDBMeta();
                foreignTable = foreignDBMeta.getTableSqlName();
                localAlias = (preForeignAlias != null ? preForeignAlias : baseAlias);
                foreignAlias = aliasBase + "_" + groupIndex + "_" + joinIndex;
                preForeignAlias = foreignAlias;
            }
            joinSb.append(ln()).append(baseIndent);
            joinSb.append("     left outer join ").append(foreignTable).append(" ").append(foreignAlias);
            joinSb.append(" on ");
            final Map<ColumnInfo, ColumnInfo> columnInfoMap = joinInfo.getLocalForeignColumnInfoMap();
            int columnIndex = 0;
            for (Entry<ColumnInfo, ColumnInfo> localForeignEntry : columnInfoMap.entrySet()) {
                final ColumnInfo localColumnInfo = localForeignEntry.getKey();
                final ColumnInfo foreignColumninfo = localForeignEntry.getValue();
                if (columnIndex > 0) {
                    joinSb.append(" and ");
                }
                joinSb.append(localAlias).append(".").append(localColumnInfo.getColumnSqlName());
                joinSb.append(" = ").append(foreignAlias).append(".").append(foreignColumninfo.getColumnSqlName());
                ++columnIndex;
            }
            foreignAliasMap.put(joinInfo, foreignAlias);
            relationMap.put(joinInfo, foreignAlias);
            ++joinIndex;
        }
        if (optimizedCondition != null) {
            optimizedCondition = resolvedOptimizedCondition(optimizedCondition, resource, foreignAliasMap);
        }
        collectAdditionalRealColumnList(additionalRealColumnList, resolvedFixedCondition, resource, foreignAlias);
        ++groupIndex;
    }
    if (optimizedCondition != null) {
        // foreign alias for in-line view is resolved here
        optimizedCondition = replaceString(optimizedCondition, getForeignAliasMark(), baseAlias);
        optimizedCondition = filterSubQueryIndentMark(optimizedCondition, false, true);
    }
    final StringBuilder sqlSb = new StringBuilder();
    sqlSb.append("(select ").append(baseAlias).append(".*");
    for (String columnName : additionalRealColumnList) {
        sqlSb.append(", ").append(columnName);
    }
    sqlSb.append(ln()).append(baseIndent);
    sqlSb.append("   from ").append(foreignTableSqlName).append(" ").append(baseAlias);
    sqlSb.append(joinSb);
    if (optimizedCondition != null) {
        buildOptimizedInlineWhereClause(optimizedCondition, baseIndent, sqlSb);
    }
    sqlSb.append(ln()).append(baseIndent);
    sqlSb.append(")");
    return sqlSb.toString();
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) ColumnInfo(org.dbflute.dbmeta.info.ColumnInfo) ForeignInfo(org.dbflute.dbmeta.info.ForeignInfo) DBMeta(org.dbflute.dbmeta.DBMeta) TableSqlName(org.dbflute.dbmeta.name.TableSqlName)

Example 2 with TableSqlName

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

the class AbstractSqlClause method buildJoinTableClause.

protected void buildJoinTableClause(StringBuilder sb, LeftOuterJoinInfo joinInfo, String joinExp, boolean canBeInnerJoin) {
    final String foreignTableDbName = joinInfo.getForeignTableDbName();
    // basically for in-line view indent
    final int tablePos = 3 + joinExp.length();
    final DBMeta foreignDBMeta = findDBMeta(foreignTableDbName);
    final TableSqlName foreignTableSqlName = foreignDBMeta.getTableSqlName();
    final List<QueryClause> inlineWhereClauseList = joinInfo.getInlineWhereClauseList();
    final String tableExp;
    if (inlineWhereClauseList.isEmpty()) {
        tableExp = foreignTableSqlName.toString();
    } else {
        tableExp = getInlineViewClause(foreignTableSqlName, inlineWhereClauseList, tablePos);
    }
    if (joinInfo.hasFixedCondition()) {
        sb.append(joinInfo.resolveFixedInlineView(tableExp, canBeInnerJoin));
    } else {
        sb.append(tableExp);
    }
}
Also used : DBMeta(org.dbflute.dbmeta.DBMeta) QueryClause(org.dbflute.cbean.sqlclause.query.QueryClause) OrScopeQueryAndPartQueryClause(org.dbflute.cbean.sqlclause.query.OrScopeQueryAndPartQueryClause) StringQueryClause(org.dbflute.cbean.sqlclause.query.StringQueryClause) TableSqlName(org.dbflute.dbmeta.name.TableSqlName)

Example 3 with TableSqlName

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

the class AbstractSqlClause method buildFromClause.

protected void buildFromClause(StringBuilder sb) {
    sb.append(ln()).append("  ");
    sb.append("from ");
    // basically for in-line view indent
    int tablePos = 7;
    if (isJoinInParentheses()) {
        for (int i = 0; i < getOuterJoinMap().size(); i++) {
            sb.append("(");
            ++tablePos;
        }
    }
    final TableSqlName tableSqlName = getDBMeta().getTableSqlName();
    final String basePointAliasName = getBasePointAliasName();
    if (hasBaseTableInlineWhereClause()) {
        final List<QueryClause> baseTableInlineWhereList = getBaseTableInlineWhereList();
        sb.append(getInlineViewClause(tableSqlName, baseTableInlineWhereList, tablePos));
        sb.append(" ").append(basePointAliasName);
    } else {
        sb.append(tableSqlName).append(" ").append(basePointAliasName);
    }
    sb.append(getFromBaseTableHint());
    if (_dynamicHintFromBaseTable != null) {
        sb.append(" ").append(_dynamicHintFromBaseTable);
    }
    sb.append(getLeftOuterJoinClause());
}
Also used : QueryClause(org.dbflute.cbean.sqlclause.query.QueryClause) OrScopeQueryAndPartQueryClause(org.dbflute.cbean.sqlclause.query.OrScopeQueryAndPartQueryClause) StringQueryClause(org.dbflute.cbean.sqlclause.query.StringQueryClause) TableSqlName(org.dbflute.dbmeta.name.TableSqlName)

Aggregations

TableSqlName (org.dbflute.dbmeta.name.TableSqlName)3 OrScopeQueryAndPartQueryClause (org.dbflute.cbean.sqlclause.query.OrScopeQueryAndPartQueryClause)2 QueryClause (org.dbflute.cbean.sqlclause.query.QueryClause)2 StringQueryClause (org.dbflute.cbean.sqlclause.query.StringQueryClause)2 DBMeta (org.dbflute.dbmeta.DBMeta)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 ColumnInfo (org.dbflute.dbmeta.info.ColumnInfo)1 ForeignInfo (org.dbflute.dbmeta.info.ForeignInfo)1