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 - coding standards
017: 2008 Andy Jefferson - add check on incompatible types for comparison
018: 2008 Andy Jefferson - add handling of inconsistent datastore types
019: ...
020: **********************************************************************/package org.jpox.store.expression;
021:
022: import java.math.BigInteger;
023: import java.util.List;
024:
025: import org.jpox.store.mapping.JavaTypeMapping;
026:
027: /**
028: * Representation of a Numeric expression in a Query.
029: *
030: * @version $Revision: 1.20 $
031: **/
032: public class NumericExpression extends ScalarExpression {
033: /**
034: * Constructor
035: * @param qs the QueryExpression
036: */
037: protected NumericExpression(QueryExpression qs) {
038: super (qs);
039: }
040:
041: /**
042: *
043: * @param qs the QueryExpression
044: * @param mapping the mapping associated to this expression
045: * @param te the TableExpression where this expression refers to
046: */
047: public NumericExpression(QueryExpression qs,
048: JavaTypeMapping mapping, LogicSetExpression te) {
049: super (qs, mapping, te);
050: }
051:
052: /**
053: * Generates statement as e.g. FUNCTION_NAME(arg[,argN]). The function returns a numeric value
054: * @param functionName
055: * @param args ScalarExpression list
056: */
057: public NumericExpression(String functionName, List args) {
058: super (functionName, args);
059: }
060:
061: /**
062: * Generates statement as e.g. FUNCTION_NAME(arg AS type[,argN as typeN])
063: * @param functionName Name of function
064: * @param args ScalarExpression list
065: * @param types String or ScalarExpression list
066: */
067: public NumericExpression(String functionName, List args, List types) {
068: super (functionName, args, types);
069: }
070:
071: /**
072: * Perform a function <code>op</code> on <code>operand</code>
073: * @param op operator
074: * @param operand operand
075: */
076: public NumericExpression(MonadicOperator op,
077: ScalarExpression operand) {
078: super (op, operand);
079: }
080:
081: /**
082: * Performs a function on two arguments.
083: * op(operand1,operand2)
084: * operand1 op operand2
085: * @param operand1 the first expression
086: * @param op the operator between operands
087: * @param operand2 the second expression
088: */
089: public NumericExpression(ScalarExpression operand1,
090: DyadicOperator op, ScalarExpression operand2) {
091: super (operand1, op, operand2);
092:
093: // Assign a mapping to this expression otherwise it will have nothing to use for get/set operations (where needed)
094: mapping = (operand1.getMapping() != null ? operand1
095: .getMapping() : operand2.getMapping());
096: }
097:
098: public BooleanExpression eq(ScalarExpression expr) {
099: // Allow for parameter comparison
100: assertValidTypeForParameterComparison(expr,
101: NumericExpression.class);
102: expr = getConsistentTypeForParameterComparison(expr);
103:
104: if (expr instanceof NullLiteral) {
105: return expr.eq(this );
106: } else if (expr instanceof NumericExpression
107: || expr instanceof SubqueryExpression) {
108: return new BooleanExpression(this , OP_EQ, expr);
109: } else if (expr instanceof CharacterExpression) {
110: return new BooleanExpression(this , OP_EQ, qs
111: .getStoreManager().getDatastoreAdapter()
112: .toNumericExpression((CharacterExpression) expr));
113: } else {
114: return super .eq(expr);
115: }
116: }
117:
118: public BooleanExpression noteq(ScalarExpression expr) {
119: // Allow for parameter comparison
120: assertValidTypeForParameterComparison(expr,
121: NumericExpression.class);
122: expr = getConsistentTypeForParameterComparison(expr);
123:
124: if (expr instanceof NullLiteral) {
125: return expr.noteq(this );
126: } else if (expr instanceof NumericExpression
127: || expr instanceof SubqueryExpression) {
128: return new BooleanExpression(this , OP_NOTEQ, expr);
129: } else if (expr instanceof CharacterExpression) {
130: return new BooleanExpression(this , OP_NOTEQ, qs
131: .getStoreManager().getDatastoreAdapter()
132: .toNumericExpression((CharacterExpression) expr));
133: } else {
134: return super .noteq(expr);
135: }
136: }
137:
138: public BooleanExpression lt(ScalarExpression expr) {
139: // Allow for parameter comparison
140: assertValidTypeForParameterComparison(expr,
141: NumericExpression.class);
142: expr = getConsistentTypeForParameterComparison(expr);
143:
144: if (expr instanceof NumericExpression
145: || expr instanceof SubqueryExpression) {
146: return new BooleanExpression(this , OP_LT, expr);
147: } else if (expr instanceof CharacterExpression) {
148: return new BooleanExpression(this , OP_LT, qs
149: .getStoreManager().getDatastoreAdapter()
150: .toNumericExpression((CharacterExpression) expr));
151: } else {
152: return super .lt(expr);
153: }
154: }
155:
156: public BooleanExpression lteq(ScalarExpression expr) {
157: // Allow for parameter comparison
158: assertValidTypeForParameterComparison(expr,
159: NumericExpression.class);
160: expr = getConsistentTypeForParameterComparison(expr);
161:
162: if (expr instanceof NumericExpression
163: || expr instanceof SubqueryExpression) {
164: return new BooleanExpression(this , OP_LTEQ, expr);
165: } else if (expr instanceof CharacterExpression) {
166: return new BooleanExpression(this , OP_LTEQ, qs
167: .getStoreManager().getDatastoreAdapter()
168: .toNumericExpression((CharacterExpression) expr));
169: } else {
170: return super .lteq(expr);
171: }
172: }
173:
174: public BooleanExpression gt(ScalarExpression expr) {
175: // Allow for parameter comparison
176: assertValidTypeForParameterComparison(expr,
177: NumericExpression.class);
178: expr = getConsistentTypeForParameterComparison(expr);
179:
180: if (expr instanceof NumericExpression
181: || expr instanceof SubqueryExpression) {
182: return new BooleanExpression(this , OP_GT, expr);
183: } else if (expr instanceof CharacterExpression) {
184: return new BooleanExpression(this , OP_GT, qs
185: .getStoreManager().getDatastoreAdapter()
186: .toNumericExpression((CharacterExpression) expr));
187: } else {
188: return super .gt(expr);
189: }
190: }
191:
192: public BooleanExpression gteq(ScalarExpression expr) {
193: // Allow for parameter comparison
194: assertValidTypeForParameterComparison(expr,
195: NumericExpression.class);
196: expr = getConsistentTypeForParameterComparison(expr);
197:
198: if (expr instanceof NumericExpression
199: || expr instanceof SubqueryExpression) {
200: return new BooleanExpression(this , OP_GTEQ, expr);
201: } else if (expr instanceof CharacterExpression) {
202: return new BooleanExpression(this , OP_GTEQ, qs
203: .getStoreManager().getDatastoreAdapter()
204: .toNumericExpression((CharacterExpression) expr));
205: } else {
206: return super .gteq(expr);
207: }
208: }
209:
210: public BooleanExpression in(ScalarExpression expr) {
211: return new BooleanExpression(this , OP_IN, expr);
212: }
213:
214: public ScalarExpression add(ScalarExpression expr) {
215: if (expr instanceof NumericExpression) {
216: return new NumericExpression(this , OP_ADD, expr);
217: } else if (expr instanceof StringExpression) {
218: return new StringExpression(qs.getStoreManager()
219: .getDatastoreAdapter().toStringExpression(this ),
220: OP_CONCAT, expr);
221: } else if (expr instanceof CharacterExpression) {
222: return new NumericExpression(this , OP_ADD, qs
223: .getStoreManager().getDatastoreAdapter()
224: .toNumericExpression((CharacterExpression) expr));
225: } else if (expr instanceof NullLiteral) {
226: return expr;
227: } else {
228: return super .add(expr);
229: }
230: }
231:
232: public ScalarExpression sub(ScalarExpression expr) {
233: if (expr instanceof NumericExpression) {
234: return new NumericExpression(this , OP_SUB, expr);
235: } else if (expr instanceof CharacterExpression) {
236: return new NumericExpression(this , OP_SUB, qs
237: .getStoreManager().getDatastoreAdapter()
238: .toNumericExpression((CharacterExpression) expr));
239: } else {
240: return super .sub(expr);
241: }
242: }
243:
244: public ScalarExpression mul(ScalarExpression expr) {
245: if (expr instanceof NumericExpression) {
246: return new NumericExpression(this , OP_MUL, expr);
247: } else if (expr instanceof CharacterExpression) {
248: return new NumericExpression(this , OP_MUL, qs
249: .getStoreManager().getDatastoreAdapter()
250: .toNumericExpression((CharacterExpression) expr));
251: } else {
252: return super .mul(expr);
253: }
254: }
255:
256: public ScalarExpression div(ScalarExpression expr) {
257: if (expr instanceof NumericExpression) {
258: return new NumericExpression(this , OP_DIV, expr);
259: } else if (expr instanceof CharacterExpression) {
260: return new NumericExpression(this , OP_DIV, qs
261: .getStoreManager().getDatastoreAdapter()
262: .toNumericExpression((CharacterExpression) expr));
263: } else {
264: return super .div(expr);
265: }
266: }
267:
268: /**
269: * Method to return a modulus expression.
270: * @param expr The expression to modulus against
271: * @return The modulus expression
272: */
273: public ScalarExpression mod(ScalarExpression expr) {
274: if (expr instanceof NumericExpression) {
275: return qs.getStoreManager().getDatastoreAdapter()
276: .modOperator(this , expr);
277: } else if (expr instanceof CharacterExpression) {
278: return qs
279: .getStoreManager()
280: .getDatastoreAdapter()
281: .modOperator(
282: this ,
283: qs.getStoreManager().getDatastoreAdapter()
284: .toNumericExpression(
285: (CharacterExpression) expr));
286: } else {
287: return super .mod(expr);
288: }
289: }
290:
291: public ScalarExpression neg() {
292: return new NumericExpression(OP_NEG, this );
293: }
294:
295: public ScalarExpression com() {
296: return this .neg().sub(
297: new IntegerLiteral(qs, mapping, BigInteger.ONE, false));
298: }
299: }
|