View Javadoc

1   package org.apache.torque.sql;
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 org.apache.commons.lang.builder.EqualsBuilder;
23  import org.apache.commons.lang.builder.HashCodeBuilder;
24  import org.apache.torque.criteria.SqlEnum;
25  
26  /**
27   * The raw values for a part of the where clause of a SQL statement,
28   * either of the form lValue operator rValue, e.g. author.author_id = 1,
29   * or in form of a custom sql query with sql and replacement values.
30   *
31   * @version $Id: WhereClauseExpression.java 1347879 2012-06-08 04:47:18Z tfischer $
32   */
33  public class WhereClauseExpression
34  {
35      /**
36       * The value on the left hand side of the operator, not null.
37       */
38      private Object lValue;
39  
40      /**
41       * The operator.
42       */
43      private SqlEnum operator;
44  
45      /**
46       * The value on the right hand side of the operator.
47       */
48      private Object rValue;
49  
50      /** A verbatim SQL for this Criterion. */
51      private String sql;
52  
53      /**
54       * Replacements for the placeholders in the verbatim SQL.
55       * Is only used if sql is not null.
56       */
57      private Object[] preparedStatementReplacements;
58  
59      /**
60       * Constructor.
61       *
62       * @param lValue The value on the left hand side of the operator of the
63       *        expression. The value represents the name of a database column.
64       * @param operator the operator. Either this parameter or sql must be
65       *        not null.
66       * @param rValue The value on the right hand side of the operator of the
67       *        expression. The value represents the name of a database column.
68       * @param sql a verbatim sql condition. Either this parameter or
69       *        operator must be not null.
70       * @param preparedStatementReplacements Values for the placeholders
71       *        in the verbatim sql condition.
72       */
73      public WhereClauseExpression(
74              Object lValue,
75              SqlEnum operator,
76              Object rValue,
77              String sql,
78              Object[] preparedStatementReplacements)
79      {
80          if (operator != null
81                  && (sql != null || preparedStatementReplacements != null))
82          {
83              throw new IllegalArgumentException("Either operator or "
84                      + "some of (sql, preparedStatementReplacements) "
85                      + "can be not null, not both");
86          }
87          if ((lValue == null || operator == null)
88                  && (sql == null))
89          {
90              throw new IllegalArgumentException("Either the values"
91                      + "(lValue, comparison) or "
92                      + "sql must be not null");
93          }
94          this.lValue = lValue;
95          this.operator = operator;
96          this.rValue = rValue;
97          this.sql = sql;
98          this.preparedStatementReplacements = preparedStatementReplacements;
99      }
100 
101     /**
102      * Returns the value on the left hand side of the operator of the
103      * expression.
104      *
105      * @return the lValue.
106      */
107     public Object getLValue()
108     {
109         return lValue;
110     }
111 
112     /**
113      * Sets the value on the left hand side of the operator of the
114      * expression. The value represents the name of a database column.
115      *
116      * @param lValue the value to set, not null or empty.
117      *
118      * @throws IllegalArgumentException if lValue is null or empty.
119      */
120     public void setLValue(String lValue)
121     {
122         this.lValue = lValue;
123     }
124 
125     /**
126      * Returns the value on the operator of the expression.
127      *
128      * @return the operator, or null if this Expression represents a verbatim
129      *         sql expression.
130      */
131     public SqlEnum getOperator()
132     {
133         return operator;
134     }
135 
136     /**
137      * Sets the value on the operator of the expression.
138      *
139      * @param operator the value to set, or null fo no operator.
140      */
141     public void setOperator(SqlEnum operator)
142     {
143         this.operator = operator;
144     }
145 
146     /**
147      * Returns the value on the right hand side of the operator of the
148      * expression.
149      *
150      * @return the rValue, or null.
151      */
152     public Object getRValue()
153     {
154         return rValue;
155     }
156 
157     /**
158      * Sets the value on the right hand side of the operator of the
159      * expression.
160      *
161      * @param rValue the value to set, or null for the empty String.
162      */
163     public void setRValue(Object rValue)
164     {
165         this.rValue = rValue;
166     }
167 
168     /**
169      * Returns the verbatim sql for this expression, if any.
170      *
171      * @return the verbatim sql for this expression, or null if not given.
172      */
173     public String getSql()
174     {
175         return sql;
176     }
177 
178     /**
179      * Returns the values for the placeholders in the verbatim sql condition.
180      *
181      * @return the placeholder values, or null.
182      */
183     public Object[] getPreparedStatementReplacements()
184     {
185         return preparedStatementReplacements;
186     }
187 
188     /**
189      * Returns whether this expression represents a verbatim sql condition.
190      *
191      * @return true if  this Criterion represents a verbatim sql condition,
192      *         false if the sql is computed from lValue, comparison and rValue.
193      */
194     public boolean isVerbatimSqlCondition()
195     {
196         return (sql != null);
197     }
198 
199     @Override
200     public int hashCode()
201     {
202         HashCodeBuilder hashCodeBuilder = new HashCodeBuilder()
203             .append(lValue)
204             .append(operator)
205             .append(rValue)
206             .append(sql)
207             .append(preparedStatementReplacements);
208         return hashCodeBuilder.toHashCode();
209     }
210 
211     @Override
212     public boolean equals(Object obj)
213     {
214         if (this == obj)
215         {
216             return true;
217         }
218         if (obj == null)
219         {
220             return false;
221         }
222         if (getClass() != obj.getClass())
223         {
224             return false;
225         }
226         WhereClauseExpression other = (WhereClauseExpression) obj;
227         EqualsBuilder equalsBuilder = new EqualsBuilder()
228                 .append(lValue, other.lValue)
229                 .append(operator, other.operator)
230                 .append(rValue, other.rValue)
231                 .append(sql, other.sql)
232                 .append(
233                     preparedStatementReplacements,
234                     other.preparedStatementReplacements);
235         return equalsBuilder.isEquals();
236     }
237 
238     @Override
239     public String toString()
240     {
241         StringBuilder result = new StringBuilder(lValue.toString());
242         if (operator != null)
243         {
244             result.append(operator);
245         }
246         result.append(rValue);
247         return result.toString();
248     }
249 }