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 SQLSumExpr extends SQLArithmeticExpr {
12: private static final int ADD = 0;
13: private static final int SUBTRACT = 1;
14: private static final int CONCAT = 2;
15: private static final String[] _operators = { "+", "-", "||" };
16: private static final Map operatorMap = new HashMap();
17:
18: static {
19: operatorMap.put("+", new Integer(ADD));
20: operatorMap.put("-", new Integer(SUBTRACT));
21: operatorMap.put("||", new Integer(CONCAT));
22: }
23:
24: public SQLSumExpr(int id) {
25: super (id);
26: }
27:
28: public void addOperator(String op) {
29: operators.add(operatorMap.get(op));
30: }
31:
32: public void write(Writer out) throws IOException {
33: for (int i = 0; i < children.length; i++) {
34: children[i].write(out);
35: if (i > 0) {
36: int opCode = ((Integer) operators.get(i - 1))
37: .intValue();
38: out.write(' ');
39: out.write(_operators[opCode]);
40: out.write(' ');
41: }
42: }
43: }
44:
45: public Object execute(Object context) {
46: Object[] args = { children[0].execute(context), null };
47: for (int i = 1; i < children.length; i++) {
48: args[1] = children[i].execute(context);
49: switch (((Integer) operators.get(i - 1)).intValue()) {
50: case ADD:
51: case CONCAT:
52: args[0] = add(args);
53: break;
54: case SUBTRACT:
55: args[0] = subtract(args);
56: break;
57: }
58: }
59: return args[0];
60: }
61: }
|