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();
}
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);
}
}
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());
}
Aggregations