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 /**
23 * A typesafe enum of SQL string fragments. Used by Criteria and SqlExpression
24 * to build queries. Criteria also makes most of the constants available
25 * in order to specify a criterion.
26 *
27 * @author <a href="mailto:jmcnally@collab.net"></a>
28 * @author <a href="mailto:fischer@seitenbau.de">Thomas Fischer</a>
29 * @version $Id: SqlEnum.java 1397355 2012-10-11 22:52:33Z tfischer $
30 * @since 3.0
31 */
32 public final class SqlEnum implements java.io.Serializable
33 {
34 /**
35 * Serial version
36 */
37 private static final long serialVersionUID = 5963149836513364800L;
38
39 /** The SQL expression. */
40 private final String s;
41
42 /**
43 * The number of operands, if the SqlEnum is a comparison operator.
44 * -1 if the SqlEnum is no comparison operator.
45 * A Set operand (e.g. in IN) is counted as one operand.
46 */
47 private final int numberOfCompareOperands;
48
49 private SqlEnum(String s, int numberOfCompareOperands)
50 {
51 this.s = s;
52 this.numberOfCompareOperands = numberOfCompareOperands;
53 }
54
55 @Override
56 public String toString()
57 {
58 return s;
59 }
60
61 /**
62 * Returns the number of operands, if the SqlEnum is a comparison operator.
63 * A Set operand (e.g. in IN) is counted as one operand.
64 *
65 * @return the number of compare operands, or -1 if the SqlEnum
66 * is no comparison operator.
67 */
68 public int getNumberOfCompareOperands()
69 {
70 return numberOfCompareOperands;
71 }
72
73 /** SQL Expression "=". */
74 public static final SqlEnum EQUAL =
75 new SqlEnum("=", 2);
76 /** SQL Expression "<>". */
77 public static final SqlEnum NOT_EQUAL =
78 new SqlEnum("<>", 2);
79 /** SQL Expression "!=". */
80 public static final SqlEnum ALT_NOT_EQUAL =
81 new SqlEnum("!=", 2);
82 /** SQL Expression ">". */
83 public static final SqlEnum GREATER_THAN =
84 new SqlEnum(">", 2);
85 /** SQL Expression "<". */
86 public static final SqlEnum LESS_THAN =
87 new SqlEnum("<", 2);
88 /** SQL Expression ">=". */
89 public static final SqlEnum GREATER_EQUAL =
90 new SqlEnum(">=", 2);
91 /** SQL Expression "<=". */
92 public static final SqlEnum LESS_EQUAL =
93 new SqlEnum("<=", 2);
94 /** SQL Expression " LIKE ". */
95 public static final SqlEnum LIKE =
96 new SqlEnum(" LIKE ", 2);
97 /** SQL Expression " NOT LIKE ". */
98 public static final SqlEnum NOT_LIKE =
99 new SqlEnum(" NOT LIKE ", 2);
100 /** SQL Expression " ILIKE ". */
101 public static final SqlEnum ILIKE =
102 new SqlEnum(" ILIKE ", 2);
103 /** SQL Expression " NOT ILIKE ". */
104 public static final SqlEnum NOT_ILIKE =
105 new SqlEnum(" NOT ILIKE ", 2);
106 /** SQL Expression " IN ". */
107 public static final SqlEnum IN =
108 new SqlEnum(" IN ", 2);
109 /** SQL Expression " NOT IN ". */
110 public static final SqlEnum NOT_IN =
111 new SqlEnum(" NOT IN ", 2);
112 /**
113 * Constant for "CUSTOM".
114 *
115 * @deprecated use the methods Criteria.whereVerbatimSql,
116 * Criteria.andVerbatimSql, Criteria.orVerbatimSql
117 * or the Constructor
118 * Criterion(null, null, null, String, Object[])
119 * instead of using SqlEnum.CUSTOM.
120 * This constant will be removed in Torque 4.1.
121 */
122 @Deprecated
123 public static final SqlEnum CUSTOM =
124 new SqlEnum("CUSTOM", -1);
125 /** SQL Expression "JOIN". */
126 public static final SqlEnum JOIN =
127 new SqlEnum("JOIN", -1);
128 /** SQL Expression "DISTINCT ". */
129 public static final SqlEnum DISTINCT =
130 new SqlEnum("DISTINCT ", -1);
131 /** SQL Expression "ALL ". */
132 public static final SqlEnum ALL =
133 new SqlEnum("ALL ", -1);
134 /** SQL Expression "ASC". */
135 public static final SqlEnum ASC =
136 new SqlEnum("ASC", -1);
137 /** SQL Expression "DESC". */
138 public static final SqlEnum DESC =
139 new SqlEnum("DESC", -1);
140 /** SQL Expression " IS NULL ". */
141 public static final SqlEnum ISNULL =
142 new SqlEnum(" IS NULL", 1);
143 /** SQL Expression " IS NOT NULL ". */
144 public static final SqlEnum ISNOTNULL =
145 new SqlEnum(" IS NOT NULL", 1);
146 /** SQL Expression "CURRENT_DATE". */
147 public static final SqlEnum CURRENT_DATE =
148 new SqlEnum("CURRENT_DATE", -1);
149 /** SQL Expression "CURRENT_TIME". */
150 public static final SqlEnum CURRENT_TIME =
151 new SqlEnum("CURRENT_TIME", -1);
152 /** SQL Expression "CURRENT_TIMESTAMP". */
153 public static final SqlEnum CURRENT_TIMESTAMP =
154 new SqlEnum("CURRENT_TIMESTAMP", -1);
155 /** SQL Expression " ON ". */
156 public static final SqlEnum ON =
157 new SqlEnum(" ON ", -1);
158 /** SQL Expression " AS ". */
159 public static final SqlEnum AS =
160 new SqlEnum(" AS ", -1);
161 /** SQL Expression " ESCAPE ". */
162 public static final SqlEnum ESCAPE =
163 new SqlEnum(" ESCAPE ", -1);
164
165 /**
166 * returns whether o is the same SqlEnum as this object.
167 * Two SqlEnums are considered equal if they contain the same String.
168 * @param o the object to compare the SqlEnum with.
169 */
170 @Override
171 public boolean equals(Object o)
172 {
173 if (o == null)
174 {
175 return false;
176 }
177
178 if (!(o instanceof SqlEnum))
179 {
180 return false;
181 }
182
183 SqlEnum otherEnum = (SqlEnum) o;
184
185
186 // both null: true
187 // other null, this not null: false
188 // else compare
189 return (otherEnum.s == null)
190 ? (s == null)
191 : otherEnum.s.equals(s);
192 }
193
194 /**
195 * returns a hashcode for this object which is consistent with equals()
196 */
197 @Override
198 public int hashCode()
199 {
200 return (s == null)
201 ? 0
202 : s.hashCode();
203 }
204 }