| 1 | package org.sqlorm.querybuilder; |
| 2 | |
| 3 | /** |
| 4 | * Internal helper class for query generation. Generic class to represent inner joins, outer joins, cross joins, left |
| 5 | * joins, right joins, ... |
| 6 | * |
| 7 | * @author kasper graversen |
| 8 | */ |
| 9 | public class MultiJoinOnExpr implements IPositionSelectFrom { |
| 10 | enum JoinType { |
| 11 | INNER_JOIN, OUTER_JOIN, CROSS_JOIN, LEFT_JOIN, RIGHT_JOIN |
| 12 | } |
| 13 | |
| 14 | TableIdentifierExpr table; |
| 15 | AndOrList constraints = new AndOrList(); |
| 16 | |
| 17 | JoinType joinType;; |
| 18 | |
| 19 | MultiJoinOnExpr(final JoinType joinType, final String table) { |
| 20 | this.joinType = joinType; |
| 21 | this.table = new TableIdentifierExpr(table); |
| 22 | constraints.internal_setIsOutermostList(); |
| 23 | } |
| 24 | |
| 25 | MultiJoinOnExpr(final JoinType joinType, final String table, final String alias) { |
| 26 | this.joinType = joinType; |
| 27 | this.table = new TableIdentifierExpr(table, alias); |
| 28 | constraints.internal_setIsOutermostList(); |
| 29 | } |
| 30 | |
| 31 | @Override |
| 32 | public boolean equals(final Object other) { |
| 33 | if(other instanceof MultiJoinOnExpr == false) { |
| 34 | return false; |
| 35 | } |
| 36 | return table.equals(((MultiJoinOnExpr) other).table); |
| 37 | } |
| 38 | |
| 39 | public ItfAndOrList on() { |
| 40 | return constraints; |
| 41 | } |
| 42 | |
| 43 | public void toSql(final StringBuilder sb, final String indent) { |
| 44 | sb.append("\n"); |
| 45 | sb.append(indent); |
| 46 | |
| 47 | switch(joinType) { |
| 48 | case INNER_JOIN: |
| 49 | sb.append("INNER JOIN "); |
| 50 | break; |
| 51 | case CROSS_JOIN: |
| 52 | sb.append("CROSS JOIN"); |
| 53 | break; |
| 54 | case LEFT_JOIN: |
| 55 | sb.append("LEFT JOIN"); |
| 56 | break; |
| 57 | case RIGHT_JOIN: |
| 58 | sb.append("RIGHT JOIN"); |
| 59 | break; |
| 60 | case OUTER_JOIN: |
| 61 | sb.append("OUTER JOIN"); |
| 62 | break; |
| 63 | default: |
| 64 | throw new IllegalStateException("Cannot generate sql for join type " + joinType); |
| 65 | } |
| 66 | table.toSql(sb, indent); |
| 67 | sb.append(" ON "); |
| 68 | constraints.toSql(sb, indent); |
| 69 | } |
| 70 | |
| 71 | } |