1 | package org.sqlorm.querybuilder; |
2 | |
3 | import java.util.ArrayList; |
4 | import java.util.List; |
5 | |
6 | /** |
7 | * Internal helper class for query generation. Implements <tt>IPositionAndOr</tt> as you can say |
8 | * <tt>.and( new AndOrList().and(boo).and(bar)</tt> |
9 | * |
10 | * @author kasper graversen |
11 | */ |
12 | public class AndOrList implements ItfAndOrList { |
13 | private final List<IPositionAndOr> andOrs = new ArrayList<IPositionAndOr>(); |
14 | |
15 | /** |
16 | * if true, do not add "()" around the entries of the list |
17 | */ |
18 | private boolean isOutermostList = false; |
19 | |
20 | public ItfAndOrList and(final IPositionAndOr andOr) { |
21 | this.andOrs.add(new AndExpr(andOr)); |
22 | return this; |
23 | } |
24 | |
25 | public ItfAndOrList and(final String sqlString) { |
26 | this.andOrs.add(new AndExpr(new ExprString(QueryBuilderHelper.format(sqlString, new Object[0])))); |
27 | return this; |
28 | } |
29 | |
30 | public ItfAndOrList and(final String fmtString, final Object... args) { |
31 | this.andOrs.add(new AndExpr(new ExprString(QueryBuilderHelper.format(fmtString, args)))); |
32 | return this; |
33 | } |
34 | |
35 | public ItfAndOrList or(final IPositionAndOr andOr) { |
36 | this.andOrs.add(new OrExpr(andOr)); |
37 | return this; |
38 | } |
39 | |
40 | public ItfAndOrList or(final String sqlString) { |
41 | this.andOrs.add(new OrExpr(new ExprString(QueryBuilderHelper.format(sqlString, new Object[0])))); |
42 | return this; |
43 | } |
44 | |
45 | public ItfAndOrList or(final String fmtString, final Object... args) { |
46 | this.andOrs.add(new OrExpr(new ExprString(QueryBuilderHelper.format(fmtString, args)))); |
47 | return this; |
48 | } |
49 | |
50 | /** |
51 | * internal method! Use this method when creating InnerJoin expressions or other elements that contains lists, where |
52 | * that list may be nested in other lists. But where the outermost list must be formatted differently than the nested |
53 | * lists. |
54 | */ |
55 | public void internal_setIsOutermostList() { |
56 | this.isOutermostList = true; |
57 | } |
58 | |
59 | /** |
60 | * internal method! |
61 | */ |
62 | public int internal_size() { |
63 | return andOrs.size(); |
64 | } |
65 | |
66 | public void toSql(final StringBuilder sb, final String indent) { |
67 | if(isOutermostList == false) { |
68 | sb.append("("); |
69 | } |
70 | |
71 | boolean first = true; |
72 | for(final IPositionAndOr ao : andOrs) { |
73 | if(first) { |
74 | first = false; |
75 | ao.toSql(sb, indent); // add with no 'and' or 'or' prepended |
76 | continue; |
77 | } |
78 | |
79 | if(ao instanceof AndExpr) { |
80 | sb.append(" AND "); |
81 | ao.toSql(sb, indent); |
82 | } |
83 | else if(ao instanceof OrExpr) { |
84 | sb.append(" OR "); |
85 | ao.toSql(sb, indent); |
86 | } |
87 | else { |
88 | throw new IllegalStateException("can never reach here"); |
89 | } |
90 | } |
91 | |
92 | if(isOutermostList == false) { |
93 | sb.append(")"); |
94 | } |
95 | |
96 | } |
97 | } |