Statistics
| Revision:

root / trunk / libraries / libGDBMS / src / main / java / com / hardcode / gdbms / engine / instruction / SumExprAdapter.java @ 10627

History | View | Annotate | Download (3.91 KB)

1
package com.hardcode.gdbms.engine.instruction;
2

    
3
import com.hardcode.gdbms.engine.values.Value;
4
import com.hardcode.gdbms.engine.values.ValueFactory;
5
import com.hardcode.gdbms.parser.SimpleNode;
6

    
7

    
8
/**
9
 * Adaptador sobre los nodos de expresi?n de suma del arbol sint?ctico de
10
 * entrada
11
 *
12
 * @author Fernando Gonz?lez Cort?s
13
 */
14
public class SumExprAdapter extends AbstractExpression implements Expression {
15
    private final static int UNDEFINED = -1;
16
    private final static int SUMA = 0;
17
    private final static int RESTA = 1;
18
    private int operator = UNDEFINED;
19

    
20
    /**
21
     * DOCUMENT ME!
22
     *
23
     * @param expr DOCUMENT ME!
24
     *
25
     * @return DOCUMENT ME!
26
     */
27
    private int getOperator(SimpleNode expr) {
28
        if (operator == UNDEFINED) {
29
            SimpleNode sn1 = (SimpleNode) expr.jjtGetChild(0);
30
            SimpleNode sn2 = (SimpleNode) expr.jjtGetChild(1);
31
            int pos1 = sn1.last_token.endColumn;
32
            int pos2 = sn2.first_token.beginColumn;
33
            String text = getInstructionContext().getSql();
34
            text = text.substring(pos1, pos2 - 1);
35

    
36
            if (text.indexOf('+') != -1) {
37
                operator = SUMA;
38
            }
39

    
40
            if (text.indexOf('-') != -1) {
41
                operator = RESTA;
42
            }
43
        }
44

    
45
        return operator;
46
    }
47

    
48
    /**
49
     * Evalua expresi?n invocando el m?todo adecuado en funci?n del tipo de
50
     * expresion (suma, producto, ...) de los objetos Value de la expresion,
51
     * de las subexpresiones y de los objetos Field
52
     *
53
     * @param row Fila en la que se eval?a la expresi?n, en este caso no es
54
     *        necesario, pero las subexpresiones sobre las que se opera pueden
55
     *        ser campos de una tabla, en cuyo caso si es necesario
56
     *
57
     * @return Objeto Value resultado de la operaci?n propia de la expresi?n
58
     *         representada por el nodo sobre el cual ?ste objeto es adaptador
59
     *
60
     * @throws EvaluationException Si se produce un error sem?ntico
61
     */
62
    public Value evaluate(long row) throws EvaluationException {
63
        Value ret = null;
64

    
65
        Adapter[] expr = getChilds();
66

    
67
        if (expr.length > 0) {
68
            ret = ((Expression) expr[0]).evaluateExpression(row);
69

    
70
            if (expr.length == 2) {
71
                try {
72
                    if (getOperator(this.getEntity()) == SUMA) {
73
                        ret = ret.suma(((Expression) expr[1]).evaluateExpression(
74
                                    row));
75
                    } else if (getOperator(this.getEntity()) == RESTA) {
76
                        ret = ret.suma(ValueFactory.createValue(-1).producto(((Expression) expr[1]).evaluateExpression(
77
                                        row)));
78
                    }
79
                } catch (IncompatibleTypesException e) {
80
                    throw new EvaluationException();
81
                }
82
            }
83
        }
84

    
85
        return ret;
86
    }
87

    
88
    /**
89
     * @see com.hardcode.gdbms.engine.instruction.Expression#getFieldName()
90
     */
91
    public String getFieldName() {
92
        Adapter[] expr = (Adapter[]) getChilds();
93

    
94
        if (expr.length != 1) {
95
            return null;
96
        } else {
97
            return ((Expression) expr[0]).getFieldName();
98
        }
99
    }
100

    
101
    /**
102
     * @see com.hardcode.gdbms.engine.instruction.Expression#isLiteral()
103
     */
104
    public boolean isLiteral() {
105
        return Utilities.checkExpressions(getChilds());
106
    }
107

    
108
    /**
109
     * @see com.hardcode.gdbms.engine.instruction.Expression#simplify()
110
     */
111
    public void simplify() {
112
        Adapter[] childs = getChilds();
113

    
114
        if (childs.length == 1) {
115
            getParent().replaceChild(this, childs[0]);
116
        }
117
    }
118

    
119
    /**
120
     * @see com.hardcode.gdbms.engine.instruction.Expression#isAggregated()
121
     */
122
    public boolean isAggregated() {
123
                Adapter[] expr = (Adapter[]) getChilds();
124

    
125
                if (expr.length != 1) {
126
                        return false;
127
                } else {
128
                        return ((Expression) expr[0]).isAggregated();
129
                }
130
    }
131
}