Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libGDBMS / src / com / hardcode / gdbms / engine / instruction / ProductExprAdapter.java @ 1956

History | View | Annotate | Download (3.18 KB)

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

    
3
import com.hardcode.gdbms.engine.data.driver.DriverException;
4
import com.hardcode.gdbms.engine.values.Value;
5
import com.hardcode.gdbms.parser.SimpleNode;
6

    
7

    
8
/**
9
 * Adaptador sobre las expresiones producto del arbol sint?ctico
10
 *
11
 * @author Fernando Gonz?lez Cort?s
12
 */
13
public class ProductExprAdapter extends AbstractExpression implements Expression {
14
        private static final int UNDEFINED = -1;
15
        private static final int PRODUCTO = 0;
16
        private static final int DIVISION = 1;
17
        private int operator = UNDEFINED;
18

    
19
        /**
20
         * Evalua expresi?n invocando el m?todo adecuado en funci?n del tipo de
21
         * expresion (suma, producto, ...) de los objetos Value de la expresion,
22
         * de las subexpresiones y de los objetos Field
23
         *
24
         * @param row Fila en la que se eval?a la expresi?n, en este caso no es
25
         *                   necesario, pero las subexpresiones sobre las que se opera pueden
26
         *                   ser campos de una tabla, en cuyo caso si es necesario
27
         *
28
         * @return Objeto Value resultado de la operaci?n propia de la expresi?n
29
         *                    representada por el nodo sobre el cual ?ste objeto es adaptador
30
         *
31
         * @throws SemanticException Si se produce un error sem?ntico
32
         * @throws DriverException Si se produce un error de I/O
33
         */
34
        public Value evaluate(long row) throws SemanticException, DriverException {
35
                Value ret = null;
36

    
37
                Adapter[] expr = (Adapter[]) getChilds();
38

    
39
                if (expr.length > 0) {
40
                        ret = ((Expression) expr[0]).evaluateExpression(row);
41

    
42
                        if (expr.length == 2) {
43
                                if (getOperator(this.getEntity()) == PRODUCTO) {
44
                                        ret = ret.producto(((Expression) expr[1]).evaluateExpression(
45
                                                                row));
46
                                } else if (getOperator(this.getEntity()) == DIVISION) {
47
                                        ret = ret.producto(((Expression) expr[1]).evaluateExpression(
48
                                                                row).inversa());
49
                                }
50
                        }
51
                }
52

    
53
                return ret;
54
        }
55

    
56
        /**
57
         * @see com.hardcode.gdbms.engine.instruction.Expression#getFieldName()
58
         */
59
        public String getFieldName() {
60
                Adapter[] expr = (Adapter[]) getChilds();
61

    
62
                if (expr.length != 1) {
63
                        return null;
64
                } else {
65
                        return ((Expression) expr[0]).getFieldName();
66
                }
67
        }
68

    
69
        /**
70
         * @see com.hardcode.gdbms.engine.instruction.Expression#isLiteral()
71
         */
72
        public boolean isLiteral() {
73
                return literal;
74
        }
75

    
76
        /**
77
         * @see com.hardcode.gdbms.engine.instruction.Expression#simplify()
78
         */
79
        public void simplify() {
80
                Adapter[] childs = getChilds();
81

    
82
                if (childs.length == 1) {
83
                        getParent().replaceChild(this, childs[0]);
84
                }
85
        }
86

    
87
        /**
88
         * @see com.hardcode.gdbms.engine.instruction.Expression#calculateLiteralCondition()
89
         */
90
        public void calculateLiteralCondition() {
91
                literal = Utilities.checkExpressions(getChilds());
92
        }
93

    
94
        /**
95
         * DOCUMENT ME!
96
         *
97
         * @param expr DOCUMENT ME!
98
         *
99
         * @return DOCUMENT ME!
100
         */
101
        private int getOperator(SimpleNode expr) {
102
                if (operator == UNDEFINED) {
103
                        SimpleNode sn1 = (SimpleNode) expr.jjtGetChild(0);
104
                        SimpleNode sn2 = (SimpleNode) expr.jjtGetChild(1);
105
                        int pos1 = sn1.last_token.endColumn;
106
                        int pos2 = sn2.first_token.beginColumn;
107
                        String text = getInstructionContext().getSql();
108
                        text = text.substring(pos1, pos2 - 1);
109

    
110
                        if (text.indexOf('*') != -1) {
111
                                operator = PRODUCTO;
112
                        }
113

    
114
                        if (text.indexOf('/') != -1) {
115
                                operator = DIVISION;
116
                        }
117
                }
118

    
119
                return operator;
120
        }
121
}