001: /**********************************************************************
002: Copyright (c) 2002 Mike Martin (TJDO) and others. All rights reserved.
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014:
015: Contributors:
016: 2003 Andy Jefferson - commented
017: ...
018: **********************************************************************/package org.jpox.store.expression;
019:
020: import java.math.BigInteger;
021: import java.util.ArrayList;
022: import java.util.List;
023:
024: import org.jpox.exceptions.JPOXUserException;
025: import org.jpox.store.mapping.JavaTypeMapping;
026:
027: /**
028: * Representation of a Character expression in a Query
029: *
030: * @version $Revision: 1.16 $
031: **/
032: public class CharacterExpression extends ScalarExpression {
033: /** JOIN expression used in JPQL queries **/
034: /*private ScalarExpression joinExpr;*/
035:
036: /**
037: * Constructor
038: * @param qs the QueryExpression
039: */
040: protected CharacterExpression(QueryExpression qs) {
041: super (qs);
042: }
043:
044: /**
045: *
046: * @param qs the QueryExpression
047: * @param mapping the mapping associated to this expression
048: * @param te the TableExpression where this expression refers to
049: */
050: public CharacterExpression(QueryExpression qs,
051: JavaTypeMapping mapping, LogicSetExpression te) {
052: super (qs, mapping, te);
053: }
054:
055: /**
056: * Generates statement as e.g. FUNCTION_NAME(arg[,argN]). The function returns a character value
057: * @param functionName
058: * @param args ScalarExpression list
059: */
060: public CharacterExpression(String functionName, List args) {
061: super (functionName, args);
062: }
063:
064: public BooleanExpression eq(ScalarExpression expr) {
065: if (expr instanceof NullLiteral) {
066: return expr.eq(this );
067: } else if (expr instanceof CharacterLiteral) {
068: return new BooleanExpression(this , OP_EQ, expr);
069: } else if (expr instanceof CharacterExpression) {
070: return new BooleanExpression(this , OP_EQ, expr);
071: } else if (expr instanceof StringLiteral) {
072: Object value = ((StringLiteral) expr).getValue();
073: if (value instanceof String
074: && ((String) value).length() > 1) {
075: // Can't compare a character with a String of more than 1 character
076: throw new JPOXUserException(
077: "Can't perform equality comparison between a character and a String of more than 1 character ("
078: + value + ") !");
079: }
080: return new BooleanExpression(this , OP_EQ, expr);
081: } else if (expr instanceof StringExpression) {
082: return new BooleanExpression(this , OP_EQ, expr);
083: } else if (expr instanceof NumericExpression) {
084: return qs.getStoreManager().getDatastoreAdapter()
085: .toNumericExpression(this ).eq(expr);
086: } else {
087: return super .eq(expr);
088: }
089: }
090:
091: public BooleanExpression noteq(ScalarExpression expr) {
092: if (expr instanceof NullLiteral) {
093: return expr.noteq(this );
094: } else if (expr instanceof CharacterLiteral) {
095: return new BooleanExpression(this , OP_NOTEQ, expr);
096: } else if (expr instanceof CharacterExpression) {
097: return new BooleanExpression(this , OP_NOTEQ, expr);
098: } else if (expr instanceof StringLiteral) {
099: return new BooleanExpression(this , OP_NOTEQ, expr);
100: } else if (expr instanceof StringExpression) {
101: return new BooleanExpression(this , OP_NOTEQ, expr);
102: } else if (expr instanceof NumericExpression) {
103: return qs.getStoreManager().getDatastoreAdapter()
104: .toNumericExpression(this ).noteq(expr);
105: } else {
106: return super .noteq(expr);
107: }
108: }
109:
110: public BooleanExpression lt(ScalarExpression expr) {
111: if (expr instanceof NullLiteral) {
112: return expr.lt(this );
113: } else if (expr instanceof CharacterLiteral) {
114: return new BooleanExpression(this , OP_LT, expr);
115: } else if (expr instanceof CharacterExpression) {
116: return new BooleanExpression(this , OP_LT, expr);
117: } else if (expr instanceof StringLiteral) {
118: return new BooleanExpression(this , OP_LT, expr);
119: } else if (expr instanceof StringExpression) {
120: return new BooleanExpression(this , OP_LT, expr);
121: } else if (expr instanceof NumericExpression) {
122: return qs.getStoreManager().getDatastoreAdapter()
123: .toNumericExpression(this ).lt(expr);
124: } else {
125: return super .lt(expr);
126: }
127: }
128:
129: public BooleanExpression lteq(ScalarExpression expr) {
130: if (expr instanceof NullLiteral) {
131: return expr.lteq(this );
132: } else if (expr instanceof CharacterLiteral) {
133: return new BooleanExpression(this , OP_LTEQ, expr);
134: } else if (expr instanceof CharacterExpression) {
135: return new BooleanExpression(this , OP_LTEQ, expr);
136: } else if (expr instanceof StringLiteral) {
137: return new BooleanExpression(this , OP_LTEQ, expr);
138: } else if (expr instanceof StringExpression) {
139: return new BooleanExpression(this , OP_LTEQ, expr);
140: } else if (expr instanceof NumericExpression) {
141: return qs.getStoreManager().getDatastoreAdapter()
142: .toNumericExpression(this ).lteq(expr);
143: } else {
144: return super .lteq(expr);
145: }
146: }
147:
148: public BooleanExpression gt(ScalarExpression expr) {
149: if (expr instanceof NullLiteral) {
150: return expr.gt(this );
151: } else if (expr instanceof CharacterLiteral) {
152: return new BooleanExpression(this , OP_GT, expr);
153: } else if (expr instanceof CharacterExpression) {
154: return new BooleanExpression(this , OP_GT, expr);
155: } else if (expr instanceof StringLiteral) {
156: return new BooleanExpression(this , OP_GT, expr);
157: } else if (expr instanceof StringExpression) {
158: return new BooleanExpression(this , OP_GT, expr);
159: } else if (expr instanceof NumericExpression) {
160: return qs.getStoreManager().getDatastoreAdapter()
161: .toNumericExpression(this ).gt(expr);
162: } else {
163: return super .gt(expr);
164: }
165: }
166:
167: public BooleanExpression gteq(ScalarExpression expr) {
168: if (expr instanceof NullLiteral) {
169: return expr.gteq(this );
170: } else if (expr instanceof CharacterLiteral) {
171: return new BooleanExpression(this , OP_GTEQ, expr);
172: } else if (expr instanceof CharacterExpression) {
173: return new BooleanExpression(this , OP_GTEQ, expr);
174: } else if (expr instanceof StringLiteral) {
175: return new BooleanExpression(this , OP_GTEQ, expr);
176: } else if (expr instanceof StringExpression) {
177: return new BooleanExpression(this , OP_GTEQ, expr);
178: } else if (expr instanceof NumericExpression) {
179: return qs.getStoreManager().getDatastoreAdapter()
180: .toNumericExpression(this ).gteq(expr);
181: } else {
182: return super .gteq(expr);
183: }
184: }
185:
186: public ScalarExpression add(ScalarExpression expr) {
187: if (expr instanceof CharacterExpression) {
188: return new NumericExpression(qs.getStoreManager()
189: .getDatastoreAdapter().toNumericExpression(this ),
190: OP_ADD, qs.getStoreManager().getDatastoreAdapter()
191: .toNumericExpression(
192: (CharacterExpression) expr));
193: } else if (expr instanceof NumericExpression) {
194: return new NumericExpression(qs.getStoreManager()
195: .getDatastoreAdapter().toNumericExpression(this ),
196: OP_ADD, expr);
197: } else {
198: return super .add(expr);
199: }
200: }
201:
202: public ScalarExpression sub(ScalarExpression expr) {
203: if (expr instanceof CharacterExpression) {
204: return new NumericExpression(qs.getStoreManager()
205: .getDatastoreAdapter().toNumericExpression(this ),
206: OP_SUB, qs.getStoreManager().getDatastoreAdapter()
207: .toNumericExpression(
208: (CharacterExpression) expr));
209: } else if (expr instanceof NumericExpression) {
210: return new NumericExpression(qs.getStoreManager()
211: .getDatastoreAdapter().toNumericExpression(this ),
212: OP_SUB, expr);
213: } else {
214: return super .sub(expr);
215: }
216: }
217:
218: public BooleanExpression in(ScalarExpression expr) {
219: return new BooleanExpression(this , OP_IN, expr);
220: }
221:
222: /**
223: * Converts the Character Expression to lower case
224: * @return the lowercased Character Expression
225: */
226: public CharacterExpression toLowerCaseMethod() {
227: ArrayList args = new ArrayList();
228: args.add(this );
229:
230: return new CharacterExpression("LOWER", args);
231: }
232:
233: /**
234: * Converts the Character Expression to upper case
235: * @return the uppercased Character Expression
236: */
237: public CharacterExpression toUpperCaseMethod() {
238: ArrayList args = new ArrayList();
239: args.add(this );
240:
241: return new CharacterExpression("UPPER", args);
242: }
243:
244: public ScalarExpression mul(ScalarExpression expr) {
245: if (expr instanceof NumericExpression) {
246: return new NumericExpression(qs.getStoreManager()
247: .getDatastoreAdapter().toNumericExpression(this ),
248: OP_MUL, expr);
249: } else if (expr instanceof CharacterExpression) {
250: return new NumericExpression(qs.getStoreManager()
251: .getDatastoreAdapter().toNumericExpression(this ),
252: OP_MUL, qs.getStoreManager().getDatastoreAdapter()
253: .toNumericExpression(
254: (CharacterExpression) expr));
255: } else if (expr instanceof NumericExpression) {
256: return qs.getStoreManager().getDatastoreAdapter()
257: .toNumericExpression(this ).mul(expr);
258: } else {
259: return super .mul(expr);
260: }
261: }
262:
263: public ScalarExpression div(ScalarExpression expr) {
264: if (expr instanceof NumericExpression) {
265: return new NumericExpression(qs.getStoreManager()
266: .getDatastoreAdapter().toNumericExpression(this ),
267: OP_DIV, expr);
268: } else if (expr instanceof CharacterExpression) {
269: return new NumericExpression(qs.getStoreManager()
270: .getDatastoreAdapter().toNumericExpression(this ),
271: OP_DIV, qs.getStoreManager().getDatastoreAdapter()
272: .toNumericExpression(
273: (CharacterExpression) expr));
274: } else if (expr instanceof NumericExpression) {
275: return qs.getStoreManager().getDatastoreAdapter()
276: .toNumericExpression(this ).div(expr);
277: } else {
278: return super .div(expr);
279: }
280: }
281:
282: /**
283: * Method to return a modulus expression.
284: * @param expr The expression to modulus against
285: * @return The modulus expression
286: */
287: public ScalarExpression mod(ScalarExpression expr) {
288: if (expr instanceof CharacterExpression) {
289: return qs
290: .getStoreManager()
291: .getDatastoreAdapter()
292: .modOperator(
293: qs.getStoreManager().getDatastoreAdapter()
294: .toNumericExpression(this ),
295: qs.getStoreManager().getDatastoreAdapter()
296: .toNumericExpression(
297: (CharacterExpression) expr));
298: } else if (expr instanceof NumericExpression) {
299: return qs.getStoreManager().getDatastoreAdapter()
300: .modOperator(
301: qs.getStoreManager().getDatastoreAdapter()
302: .toNumericExpression(this ), expr);
303: } else {
304: return super .sub(expr);
305: }
306: }
307:
308: public ScalarExpression neg() {
309: return new NumericExpression(OP_NEG, qs.getStoreManager()
310: .getDatastoreAdapter().toNumericExpression(this ));
311: }
312:
313: public ScalarExpression com() {
314: return qs.getStoreManager().getDatastoreAdapter()
315: .toNumericExpression(this ).neg()
316: .sub(new IntegerLiteral(qs, mapping, BigInteger.ONE));
317: }
318:
319: public ScalarExpression join(ScalarExpression expr) {
320: /*this.joinExpr = expr;*/
321: return this;
322: }
323: }
|