Skip to content
Snippets Groups Projects

Add complex join type to SQL generator for composite key joins

2 files
+ 110
32
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -5,12 +5,10 @@ import java.util.List;
@@ -5,12 +5,10 @@ import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.Pattern;
import javax.persistence.criteria.JoinType;
import org.apache.commons.lang3.NotImplementedException;
import org.apache.commons.lang3.NotImplementedException;
import org.eclipsefoundation.persistence.model.ParameterizedSQLStatement.Clause;
import org.eclipsefoundation.persistence.model.ParameterizedSQLStatement.Clause;
import org.eclipsefoundation.persistence.model.ParameterizedSQLStatement.GroupBy;
import org.eclipsefoundation.persistence.model.ParameterizedSQLStatement.GroupBy;
import org.eclipsefoundation.persistence.model.ParameterizedSQLStatement.Join;
import org.eclipsefoundation.persistence.model.ParameterizedSQLStatement.IJoin;
public class HQLGenerator implements SQLGenerator {
public class HQLGenerator implements SQLGenerator {
private static final Pattern ORDINAL_PARAMETER_PATTERN = Pattern.compile("\\?(?!\\d)");
private static final Pattern ORDINAL_PARAMETER_PATTERN = Pattern.compile("\\?(?!\\d)");
@@ -68,17 +66,13 @@ public class HQLGenerator implements SQLGenerator {
@@ -68,17 +66,13 @@ public class HQLGenerator implements SQLGenerator {
}
}
private String getJoinClause(List<Join> joins, DtoTable base) {
private String getJoinClause(List<IJoin> joins, DtoTable base) {
StringBuilder sb = new StringBuilder();
StringBuilder sb = new StringBuilder();
List<DtoTable> selectedTables = new ArrayList<>();
List<DtoTable> selectedTables = new ArrayList<>();
joins.stream().filter(j -> base != j.getForeignTable() && !selectedTables.contains(j.getForeignTable()))
joins.stream().filter(j -> base != j.getForeignTable() && !selectedTables.contains(j.getForeignTable()))
.forEach(j -> {
.forEach(j -> {
selectedTables.add(j.getLocalTable());
selectedTables.add(j.getLocalTable());
sb.append(' ').append(j.getJoinType() != null ? j.getJoinType().name() : JoinType.LEFT.name());
sb.append(' ').append(j.toSQL());
sb.append(" JOIN ").append(j.getForeignTable().getType().getSimpleName());
sb.append(" AS ").append(j.getForeignTable().getAlias());
sb.append(" ON ").append(j.getLocalTable().getAlias()).append('.').append(j.getLocalField());
sb.append(" = ").append(j.getForeignTable().getAlias()).append('.').append(j.getForeignField());
});
});
return sb.toString();
return sb.toString();
}
}
Loading