View Javadoc

1   package org.apache.torque.criteria;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.Serializable;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.commons.lang.builder.EqualsBuilder;
27  import org.apache.commons.lang.builder.HashCodeBuilder;
28  
29  /**
30   * This class describes an Element in the From-part of a SQL clause.
31   * It must contain the name of the database table.
32   * It might contain an alias for the table name, a join type
33   * a join condition and prepared statement replacements.
34   * The class is immutable.
35   */
36  public class FromElement implements Serializable
37  {
38      /** serial Version UID. */
39      private static final long serialVersionUID = 1L;
40  
41      /**
42       * The fromExpression, e.g. a simple table name or a table name
43       * or a subquery with an appended alias name.
44       */
45      private String fromExpression = null;
46  
47      /** The type of the join, e.g. JoinType.LEFT_JOIN */
48      private JoinType joinType = null;
49  
50      /** The join condition, e.g. table_a.id = table_b.a_id */
51      private String joinCondition = null;
52  
53      /** The replacements which might occur in the fromExpression. */
54      private final List<Object> preparedStatementReplacements
55              = new ArrayList<Object>();
56  
57      /**
58       * Constructor with join type null and joinCondition null.
59       *
60       * @param tableName the table name, might contain an appended alias name
61       *        e.g. <br />
62       *        table_1<br />
63       *        table_1 alias_for_table_1
64       */
65      public FromElement(String tableName)
66      {
67          this(tableName, null, null, null);
68      }
69  
70      /**
71       * Constructor.
72       *
73       * @param fromExpression the expression to add to the from clause,
74       *        e.g. a simple table name or a table name with an alias,
75       *        e.g. <br />
76       *        table_1<br />
77       *        table_1 alias_for_table_1
78       * @param joinType the type of the join, e.g. JoinType.LEFT_JOIN,
79       *        or null if no excplicit join is wanted
80       * @param joinCondition the join condition,
81       *        e.g. table_a.id = table_b.a_id,
82       *        or null if no explicit join is wanted
83       *        (In this case, the join condition is appended to the
84       *         whereClause instead)
85       */
86      public FromElement(
87              String fromExpression,
88              JoinType joinType,
89              String joinCondition)
90      {
91          this(fromExpression, joinType, joinCondition, null);
92      }
93  
94      /**
95       * Constructor.
96       *
97       * @param fromExpression the expression to add to the from clause,
98       *        e.g. a simple table name or a table name with an alias,
99       *        e.g. <br />
100      *        table_1<br />
101      *        table_1 alias_for_table_1
102      * @param joinType the type of the join, e.g. JoinType.LEFT_JOIN,
103      *        or null if no explicit join is wanted
104      * @param joinCondition the join condition,
105      *        e.g. table_a.id = table_b.a_id,
106      *        or null if no explicit join is wanted
107      *        (In this case, the join condition is appended to the
108      *         whereClause instead)
109      * @param preparedStatementReplacements the replacements for placeholders
110      *        which might occur in the fromExpression, may be null.
111      */
112     public FromElement(
113             String fromExpression,
114             JoinType joinType,
115             String joinCondition,
116             List<Object> preparedStatementReplacements)
117     {
118         this.fromExpression = fromExpression;
119         this.joinType = joinType;
120         this.joinCondition = joinCondition;
121         if (preparedStatementReplacements != null)
122         {
123             this.preparedStatementReplacements.addAll(
124                     preparedStatementReplacements);
125         }
126     }
127 
128     /**
129      * Constructor.
130      *
131      * @param fromExpression the expression to add to the from clause,
132      *        e.g. a simple table name or a table name with an alias,
133      *        e.g. <br />
134      *        table_1<br />
135      *        table_1 alias_for_table_1
136      * @param joinType the type of the join, e.g. JoinType.LEFT_JOIN,
137      *        or null if no explicit join is wanted
138      * @param joinCondition the join condition,
139      *        e.g. table_a.id = table_b.a_id, not null.
140      */
141     public FromElement(
142             String fromExpression,
143             JoinType joinType,
144             PreparedStatementPart joinCondition)
145     {
146         this(fromExpression,
147                 joinType,
148                 joinCondition.getSql().toString(),
149                 joinCondition.getPreparedStatementReplacements());
150     }
151 
152 
153     /**
154      * Returns the join condition.
155      *
156      * @return the join condition, e.g. table_a.id = table_b.a_id,
157      *         or null if the join is not an explicit join
158      */
159     public String getJoinCondition()
160     {
161         return joinCondition;
162     }
163 
164     /**
165      * Returns the join type.
166      *
167      * @return the type of the join, e.g. JoinType.LEFT_JOIN,
168      *         or null if the join is not an explicit join
169      */
170     public JoinType getJoinType()
171     {
172         return joinType;
173     }
174 
175     /**
176      * Returns the fromExpression, which might e.g. be a simple table name or
177      *         a table name or a subquery with an alias appended.
178      *
179      * @return the tablename, might contain an appended alias name,
180      *         e.g. <br />
181      *         table_1<br />
182      *         table_1 alias_for_table_1
183      *
184      */
185     public String getFromExpression()
186     {
187         return fromExpression;
188     }
189 
190     /**
191      * Returns the replacements for prepared statement placeholders in the
192      * fromExpression.
193      *
194      * @return the replacements, not null.
195 
196      */
197     public List<Object> getPreparedStatementReplacements()
198     {
199         return preparedStatementReplacements;
200     }
201 
202     /**
203      * Returns a SQL representation of the element.
204      *
205      * @return a SQL representation of the element
206      */
207     @Override
208     public String toString()
209     {
210         StringBuffer result = new StringBuffer();
211         if (joinType != null)
212         {
213             result.append(joinType);
214         }
215         result.append(fromExpression);
216         if (joinCondition != null)
217         {
218             result.append(SqlEnum.ON);
219             result.append(joinCondition);
220         }
221         return result.toString();
222     }
223 
224 
225     @Override
226     public int hashCode()
227     {
228         return new HashCodeBuilder()
229                 .append(joinCondition)
230                 .append(joinType)
231                 .append(fromExpression)
232                 .append(preparedStatementReplacements)
233                 .toHashCode();
234     }
235 
236 
237     @Override
238     public boolean equals(Object obj)
239     {
240         if (this == obj)
241         {
242             return true;
243         }
244         if (obj == null)
245         {
246             return false;
247         }
248         if (getClass() != obj.getClass())
249         {
250             return false;
251         }
252         FromElement other = (FromElement) obj;
253         return new EqualsBuilder()
254             .append(joinCondition, other.joinCondition)
255             .append(joinType, other.joinType)
256             .append(fromExpression, other.fromExpression)
257             .append(preparedStatementReplacements,
258                     other.preparedStatementReplacements)
259             .isEquals();
260     }
261 }