01: package org.obe.sql;
02:
03: import java.io.IOException;
04: import java.io.Writer;
05: import java.util.HashMap;
06: import java.util.Map;
07:
08: /**
09: * @author Adrian Price
10: */
11: public class SQLCompareOp extends SQLArithmeticExpr {
12: protected static final int EQ = 0;
13: protected static final int NE = 1;
14: protected static final int NE2 = 2;
15: protected static final int GT = 3;
16: protected static final int GE = 4;
17: protected static final int LT = 5;
18: protected static final int LE = 6;
19: protected static final String[] _operators = { "=", "!=", "<>",
20: ">", ">=", "<", "<=" };
21: protected static final Map _operatorMap = new HashMap();
22:
23: private int _opCode;
24:
25: static {
26: for (int i = 0; i < _operators.length; i++)
27: _operatorMap.put(_operators[i], new Integer(i));
28: }
29:
30: public SQLCompareOp(int id) {
31: super (id);
32: }
33:
34: public void setName(String name) {
35: _opCode = ((Integer) _operatorMap.get(name)).intValue();
36: }
37:
38: public void write(Writer out) throws IOException {
39: out.write(_operators[_opCode]);
40: }
41:
42: public Object execute(Object context, Object lhs, Object rhs) {
43: int comparison = compare(lhs, rhs);
44: boolean result;
45: switch (_opCode) {
46: case EQ:
47: result = comparison == 0;
48: break;
49: case NE:
50: case NE2:
51: result = comparison != 0;
52: break;
53: case GT:
54: result = comparison > 0;
55: break;
56: case GE:
57: result = comparison >= 0;
58: break;
59: case LT:
60: result = comparison < 0;
61: break;
62: case LE:
63: result = comparison <= 0;
64: break;
65: default:
66: // (Can't happen)
67: result = false;
68: }
69: return result ? Boolean.TRUE : Boolean.FALSE;
70: }
71: }
|