Revision 1956 trunk/libraries/libGDBMS/src/com/hardcode/gdbms/engine/instruction/SumExprAdapter.java
SumExprAdapter.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.engine.values.ValueFactory; |
|
6 |
import com.hardcode.gdbms.parser.SimpleNode; |
|
5 | 7 |
|
6 | 8 |
|
7 | 9 |
/** |
... | ... | |
11 | 13 |
* @author Fernando Gonz?lez Cort?s |
12 | 14 |
*/ |
13 | 15 |
public class SumExprAdapter extends AbstractExpression implements Expression { |
14 |
/** |
|
15 |
* Evalua expresi?n invocando el m?todo adecuado en funci?n del tipo de |
|
16 |
* expresion (suma, producto, ...) de los objetos Value de la expresion, |
|
17 |
* de las subexpresiones y de los objetos Field |
|
18 |
* |
|
19 |
* @param row Fila en la que se eval?a la expresi?n, en este caso no es |
|
20 |
* necesario, pero las subexpresiones sobre las que se opera pueden |
|
21 |
* ser campos de una tabla, en cuyo caso si es necesario |
|
22 |
* |
|
23 |
* @return Objeto Value resultado de la operaci?n propia de la expresi?n |
|
24 |
* representada por el nodo sobre el cual ?ste objeto es adaptador |
|
25 |
* |
|
26 |
* @throws SemanticException Si se produce un error sem?ntico |
|
27 |
* @throws DriverException Si se produce un error de I/O |
|
28 |
*/ |
|
29 |
public Value evaluate(long row) throws SemanticException, DriverException { |
|
30 |
Value ret = null; |
|
16 |
private final static int UNDEFINED = -1; |
|
17 |
private final static int SUMA = 0; |
|
18 |
private final static int RESTA = 1; |
|
19 |
private int operator = UNDEFINED; |
|
31 | 20 |
|
32 |
Adapter[] expr = (Adapter[]) getChilds(); |
|
21 |
/** |
|
22 |
* DOCUMENT ME! |
|
23 |
* |
|
24 |
* @param expr DOCUMENT ME! |
|
25 |
* |
|
26 |
* @return DOCUMENT ME! |
|
27 |
*/ |
|
28 |
private int getOperator(SimpleNode expr) { |
|
29 |
if (operator == UNDEFINED) { |
|
30 |
SimpleNode sn1 = (SimpleNode) expr.jjtGetChild(0); |
|
31 |
SimpleNode sn2 = (SimpleNode) expr.jjtGetChild(1); |
|
32 |
int pos1 = sn1.last_token.endColumn; |
|
33 |
int pos2 = sn2.first_token.beginColumn; |
|
34 |
String text = getInstructionContext().getSql(); |
|
35 |
text = text.substring(pos1, pos2 - 1); |
|
33 | 36 |
|
34 |
if (expr.length > 0) { |
|
35 |
ret = ((Expression) expr[0]).evaluateExpression(row); |
|
37 |
if (text.indexOf('+') != -1) { |
|
38 |
operator = SUMA; |
|
39 |
} |
|
36 | 40 |
|
37 |
for (int i = 1; i < expr.length; i++) {
|
|
38 |
ret = ret.suma(((Expression) expr[i]).evaluateExpression(row));
|
|
39 |
}
|
|
40 |
}
|
|
41 |
if (text.indexOf('-') != -1) {
|
|
42 |
operator = RESTA;
|
|
43 |
}
|
|
44 |
}
|
|
41 | 45 |
|
42 |
return ret;
|
|
43 |
}
|
|
46 |
return operator;
|
|
47 |
}
|
|
44 | 48 |
|
45 |
/** |
|
46 |
* @see com.hardcode.gdbms.engine.instruction.Expression#getFieldName() |
|
47 |
*/ |
|
48 |
public String getFieldName() { |
|
49 |
Adapter[] expr = (Adapter[]) getChilds(); |
|
49 |
/** |
|
50 |
* Evalua expresi?n invocando el m?todo adecuado en funci?n del tipo de |
|
51 |
* expresion (suma, producto, ...) de los objetos Value de la expresion, |
|
52 |
* de las subexpresiones y de los objetos Field |
|
53 |
* |
|
54 |
* @param row Fila en la que se eval?a la expresi?n, en este caso no es |
|
55 |
* necesario, pero las subexpresiones sobre las que se opera pueden |
|
56 |
* ser campos de una tabla, en cuyo caso si es necesario |
|
57 |
* |
|
58 |
* @return Objeto Value resultado de la operaci?n propia de la expresi?n |
|
59 |
* representada por el nodo sobre el cual ?ste objeto es adaptador |
|
60 |
* |
|
61 |
* @throws SemanticException Si se produce un error sem?ntico |
|
62 |
* @throws DriverException Si se produce un error de I/O |
|
63 |
*/ |
|
64 |
public Value evaluate(long row) throws SemanticException, DriverException { |
|
65 |
Value ret = null; |
|
50 | 66 |
|
51 |
if (expr.length != 1) { |
|
52 |
return null; |
|
53 |
} else { |
|
54 |
return ((Expression) expr[0]).getFieldName(); |
|
55 |
} |
|
56 |
} |
|
67 |
Adapter[] expr = (Adapter[]) getChilds(); |
|
57 | 68 |
|
58 |
/** |
|
59 |
* @see com.hardcode.gdbms.engine.instruction.Expression#isLiteral() |
|
60 |
*/ |
|
61 |
public boolean isLiteral() { |
|
62 |
return literal; |
|
63 |
} |
|
69 |
if (expr.length > 0) { |
|
70 |
ret = ((Expression) expr[0]).evaluateExpression(row); |
|
64 | 71 |
|
65 |
/** |
|
66 |
* @see com.hardcode.gdbms.engine.instruction.Expression#simplify() |
|
67 |
*/ |
|
68 |
public void simplify() { |
|
69 |
Adapter[] childs = getChilds(); |
|
72 |
if (expr.length == 2) { |
|
73 |
if (getOperator(this.getEntity()) == SUMA) { |
|
74 |
ret = ret.suma(((Expression) expr[1]).evaluateExpression( |
|
75 |
row)); |
|
76 |
} else if (getOperator(this.getEntity()) == RESTA) { |
|
77 |
ret = ret.suma(ValueFactory.createValue(-1).producto(((Expression) expr[1]).evaluateExpression( |
|
78 |
row))); |
|
79 |
} |
|
80 |
} |
|
81 |
} |
|
70 | 82 |
|
71 |
if (childs.length == 1) { |
|
72 |
getParent().replaceChild(this, childs[0]); |
|
73 |
} |
|
74 |
} |
|
83 |
return ret; |
|
84 |
} |
|
75 | 85 |
|
76 | 86 |
/** |
87 |
* @see com.hardcode.gdbms.engine.instruction.Expression#getFieldName() |
|
88 |
*/ |
|
89 |
public String getFieldName() { |
|
90 |
Adapter[] expr = (Adapter[]) getChilds(); |
|
91 |
|
|
92 |
if (expr.length != 1) { |
|
93 |
return null; |
|
94 |
} else { |
|
95 |
return ((Expression) expr[0]).getFieldName(); |
|
96 |
} |
|
97 |
} |
|
98 |
|
|
99 |
/** |
|
100 |
* @see com.hardcode.gdbms.engine.instruction.Expression#isLiteral() |
|
101 |
*/ |
|
102 |
public boolean isLiteral() { |
|
103 |
return literal; |
|
104 |
} |
|
105 |
|
|
106 |
/** |
|
107 |
* @see com.hardcode.gdbms.engine.instruction.Expression#simplify() |
|
108 |
*/ |
|
109 |
public void simplify() { |
|
110 |
Adapter[] childs = getChilds(); |
|
111 |
|
|
112 |
if (childs.length == 1) { |
|
113 |
getParent().replaceChild(this, childs[0]); |
|
114 |
} |
|
115 |
} |
|
116 |
|
|
117 |
/** |
|
77 | 118 |
* @see com.hardcode.gdbms.engine.instruction.Expression#calculateLiteralCondition() |
78 | 119 |
*/ |
79 | 120 |
public void calculateLiteralCondition() { |
80 |
literal = Utilities.checkExpressions(getChilds());
|
|
121 |
literal = Utilities.checkExpressions(getChilds());
|
|
81 | 122 |
} |
82 | 123 |
} |
Also available in: Unified diff