KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > framework > server > store > WhereClause


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package sync4j.framework.server.store;
20
21 import org.apache.commons.lang.builder.ToStringBuilder;
22 import java.io.Serializable JavaDoc;
23
24 /**
25  * This class represents selection clause of a query.
26  *
27  * @author Luigia Fassina @ Funambol
28  * @version $Id: WhereClause.java,v 1.8 2005/03/02 20:57:38 harrie Exp $
29  *
30  */

31 public class WhereClause
32 extends Clause
33 implements Serializable JavaDoc {
34
35     // -------------------------------------------------------- Public constants
36
public static final String JavaDoc OPT_START_WITH = "START_WITH";
37     public static final String JavaDoc OPT_END_WITH = "END_WITH" ;
38     public static final String JavaDoc OPT_CONTAINS = "CONTAINS" ;
39     public static final String JavaDoc OPT_EQ = "EQ" ;
40     public static final String JavaDoc OPT_GT = "GT" ;
41     public static final String JavaDoc OPT_LT = "LT" ;
42     public static final String JavaDoc OPT_BETWEEN = "BETWEEN" ;
43     public static final String JavaDoc OPT_GE = "GE" ;
44     public static final String JavaDoc OPT_LE = "LE" ;
45
46     // ------------------------------------------------------------ Private data
47
private static final String JavaDoc OPT_UPPER = "upper(" ;
48
49     private String JavaDoc property;
50     private String JavaDoc[] value;
51     private String JavaDoc operator;
52     private boolean caseSensitive;
53
54     /**
55      * The default constructor is not intended to be used.
56      */

57     public WhereClause() {
58         this(null, null, null, false);
59     }
60
61     public WhereClause(String JavaDoc property, String JavaDoc[] value, String JavaDoc operator, boolean caseSensitive) {
62         this.property = property;
63         this.value = value;
64         this.operator = operator;
65         this.caseSensitive = caseSensitive;
66     }
67
68     /** Getter for property property.
69      * @return Value of property property.
70      *
71      */

72     public String JavaDoc getProperty() {
73         return property;
74     }
75
76     /** Setter for property property.
77      * @param property New value of property property.
78      *
79      */

80     public void setProperty(String JavaDoc property) {
81         this.property = property;
82     }
83
84     /** Getter for property value.
85      * @return Value of property value.
86      *
87      */

88     public String JavaDoc[] getvalue() {
89         return value;
90     }
91
92     /** Setter for property value.
93      * @param value New value of property value.
94      *
95      */

96     public void setValue(String JavaDoc[] value) {
97         this.value = value;
98     }
99
100     /** Setter for property parameter.
101      * @param value New value of property value.
102      *
103      */

104     public void setParameter(String JavaDoc[] value) {
105         this.value = value;
106     }
107
108     /** Getter for property operator.
109      * @return Value of property operator.
110      *
111      */

112     public String JavaDoc getOperator() {
113         return operator;
114     }
115
116     /** Setter for property operator.
117      * @param operator New value of property operator.
118      *
119      */

120     public void setOperator(String JavaDoc operator) {
121         this.operator = operator;
122     }
123
124     /** Getter for property caseSensitive.
125      * @return Value of property caseSensitive.
126      *
127      */

128     public boolean isCaseSensitive() {
129         return caseSensitive;
130     }
131
132     /** Setter for property caseSensitive.
133      * @param caseSensitive New value of property caseSensitive.
134      *
135      */

136     public void setCaseSensitive(boolean caseSensitive) {
137         this.caseSensitive = caseSensitive;
138     }
139
140     // ------------------------------------------------ Implementation of Clause
141

142     public PreparedWhere getPreparedWhere() {
143         String JavaDoc property = getProperty();
144         String JavaDoc operator = getOperator();
145         String JavaDoc[] values = getvalue();
146         boolean caseSensitive = isCaseSensitive();
147
148         StringBuffer JavaDoc query = new StringBuffer JavaDoc();
149         
150         assert (values != null);
151         
152         PreparedWhere where = new PreparedWhere();
153         where.parameters = new Object JavaDoc[values.length];
154         
155         String JavaDoc uprOpen = "" ;
156         String JavaDoc uprClose = "" ;
157         String JavaDoc uprProperty = property;
158         
159         if (!caseSensitive) {
160             uprOpen = OPT_UPPER;
161             uprClose = ")";
162             uprProperty = " UPPER("+property+")";
163         }
164
165         if (OPT_START_WITH.equalsIgnoreCase(operator)) {
166             query.append(uprProperty).append(" LIKE ").append(uprOpen).append('?').append(uprClose);
167             where.sql = query.toString();
168             where.parameters[0] = values[0] + '%';
169         } else if (OPT_END_WITH.equalsIgnoreCase(operator)) {
170             query.append(uprProperty).append(" LIKE ").append(uprOpen).append('?').append(uprClose);
171             where.sql = query.toString();
172             where.parameters[0] = '%' + values[0];
173         } else if (OPT_CONTAINS.equalsIgnoreCase(operator)) {
174             query.append(uprProperty).append(" LIKE ").append(uprOpen).append('?').append(uprClose);
175             where.sql = query.toString();
176             where.parameters[0] = '%' + values[0] + '%';
177         } else if (OPT_EQ.equalsIgnoreCase(operator)) {
178             query.append(uprProperty).append(" = ").append(uprOpen).append('?').append(uprClose);
179             where.sql = query.toString();
180             where.parameters[0] = values[0];
181         } else if (OPT_GT.equalsIgnoreCase(operator)) {
182             query.append(uprProperty).append(" > ").append(uprOpen).append('?').append(uprClose);
183             where.sql = query.toString();
184             where.parameters[0] = values[0];
185         } else if (OPT_LT.equalsIgnoreCase(operator)) {
186             query.append(uprProperty).append(" < ").append(uprOpen).append('?').append(uprClose);
187             where.sql = query.toString();
188             where.parameters[0] = values[0];
189         } else if (OPT_BETWEEN.equalsIgnoreCase(operator)) {
190             query.append(uprProperty).append(' ').append(OPT_BETWEEN).append(' ').append(uprOpen).append('?').append(uprClose)
191                 .append(" AND ").append(uprOpen).append('?').append(uprClose);
192             where.sql = query.toString();
193             where.parameters[0] = values[0];
194             where.parameters[1] = values[1];
195         } else if (OPT_GE.equalsIgnoreCase(operator)) {
196             query.append(uprProperty).append(" >= ").append(uprOpen).append('?').append(uprClose);
197             where.sql = query.toString();
198             where.parameters[0] = values[0];
199         } else if (OPT_LE.equalsIgnoreCase(operator)) {
200             query.append(uprProperty).append(" <= ").append(uprOpen).append('?').append(uprClose);
201             where.sql = query.toString();
202             where.parameters[0] = values[0];
203         }
204         
205         where.sql = '(' + where.sql + ')';
206         return where;
207     }
208     
209     // -------------------------------------------------------------------------
210

211     public String JavaDoc toString() {
212         ToStringBuilder sb = new ToStringBuilder(this);
213         sb.append("operator:", operator);
214         sb.append("property:", property);
215         for (int i=0; i<value.length; i++) {
216             sb.append("value:", value[i]);
217         }
218         sb.append("caseSensitive:", caseSensitive);
219
220         return sb.toString();
221     }
222 }
223
Popular Tags