Revision 1956 trunk/libraries/libGDBMS/src/com/hardcode/gdbms/engine/instruction/ProductExprAdapter.java

View differences:

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

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

  
6 7

  
7 8
/**
......
10 11
 * @author Fernando Gonz?lez Cort?s
11 12
 */
12 13
public class ProductExprAdapter extends AbstractExpression implements Expression {
13
    /**
14
     * Evalua expresi?n invocando el m?todo adecuado en funci?n del tipo de
15
     * expresion (suma, producto, ...) de los objetos Value de la expresion,
16
     * de las subexpresiones y de los objetos Field
17
     *
18
     * @param row Fila en la que se eval?a la expresi?n, en este caso no es
19
     *        necesario, pero las subexpresiones sobre las que se opera pueden
20
     *        ser campos de una tabla, en cuyo caso si es necesario
21
     *
22
     * @return Objeto Value resultado de la operaci?n propia de la expresi?n
23
     *         representada por el nodo sobre el cual ?ste objeto es adaptador
24
     *
25
     * @throws SemanticException Si se produce un error sem?ntico
26
     * @throws DriverException Si se produce un error de I/O
27
     */
28
    public Value evaluate(long row) throws SemanticException, DriverException {
29
        Value ret = null;
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;
30 18

  
31
        Adapter[] expr = (Adapter[]) getChilds();
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;
32 36

  
33
        if (expr.length > 0) {
34
            ret = ((Expression) expr[0]).evaluateExpression(row);
37
		Adapter[] expr = (Adapter[]) getChilds();
35 38

  
36
            for (int i = 1; i < expr.length; i++) {
37
                ret = ret.producto(((Expression) expr[i]).evaluateExpression(
38
                            row));
39
            }
40
        }
39
		if (expr.length > 0) {
40
			ret = ((Expression) expr[0]).evaluateExpression(row);
41 41

  
42
        return ret;
43
    }
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
		}
44 52

  
45
    /**
46
     * @see com.hardcode.gdbms.engine.instruction.Expression#getFieldName()
47
     */
48
    public String getFieldName() {
49
        Adapter[] expr = (Adapter[]) getChilds();
53
		return ret;
54
	}
50 55

  
51
        if (expr.length != 1) {
52
            return null;
53
        } else {
54
            return ((Expression) expr[0]).getFieldName();
55
        }
56
    }
56
	/**
57
	 * @see com.hardcode.gdbms.engine.instruction.Expression#getFieldName()
58
	 */
59
	public String getFieldName() {
60
		Adapter[] expr = (Adapter[]) getChilds();
57 61

  
58
    /**
59
     * @see com.hardcode.gdbms.engine.instruction.Expression#isLiteral()
60
     */
61
    public boolean isLiteral() {
62
        return literal;
63
    }
62
		if (expr.length != 1) {
63
			return null;
64
		} else {
65
			return ((Expression) expr[0]).getFieldName();
66
		}
67
	}
64 68

  
65
    /**
66
     * @see com.hardcode.gdbms.engine.instruction.Expression#simplify()
67
     */
68
    public void simplify() {
69
        Adapter[] childs = getChilds();
69
	/**
70
	 * @see com.hardcode.gdbms.engine.instruction.Expression#isLiteral()
71
	 */
72
	public boolean isLiteral() {
73
		return literal;
74
	}
70 75

  
71
        if (childs.length == 1) {
72
            getParent().replaceChild(this, childs[0]);
73
        }
74
    }
76
	/**
77
	 * @see com.hardcode.gdbms.engine.instruction.Expression#simplify()
78
	 */
79
	public void simplify() {
80
		Adapter[] childs = getChilds();
75 81

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

  
76 87
	/**
77 88
	 * @see com.hardcode.gdbms.engine.instruction.Expression#calculateLiteralCondition()
78 89
	 */
79 90
	public void calculateLiteralCondition() {
80
        literal = Utilities.checkExpressions(getChilds());
91
		literal = Utilities.checkExpressions(getChilds());
81 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
	}
82 121
}

Also available in: Unified diff