提交 6b6db5e3 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Reorganize conditions in Operation.optimize()

上级 6ad9d310
...@@ -219,7 +219,8 @@ public class Operation extends Expression { ...@@ -219,7 +219,8 @@ public class Operation extends Expression {
} else if (l == Value.DATE || l == Value.TIMESTAMP || } else if (l == Value.DATE || l == Value.TIMESTAMP ||
l == Value.TIME || r == Value.DATE || l == Value.TIME || r == Value.DATE ||
r == Value.TIMESTAMP || r == Value.TIME) { r == Value.TIMESTAMP || r == Value.TIME) {
if (opType == OpType.PLUS) { switch (opType) {
case PLUS:
if (r != Value.getHigherOrder(l, r)) { if (r != Value.getHigherOrder(l, r)) {
// order left and right: INT < TIME < DATE < TIMESTAMP // order left and right: INT < TIME < DATE < TIMESTAMP
swap(); swap();
...@@ -227,7 +228,8 @@ public class Operation extends Expression { ...@@ -227,7 +228,8 @@ public class Operation extends Expression {
l = r; l = r;
r = t; r = t;
} }
if (l == Value.INT) { switch (l) {
case Value.INT: {
// Oracle date add // Oracle date add
Function f = Function.getFunction(session.getDatabase(), "DATEADD"); Function f = Function.getFunction(session.getDatabase(), "DATEADD");
f.setParameter(0, ValueExpression.get(ValueString.get("DAY"))); f.setParameter(0, ValueExpression.get(ValueString.get("DAY")));
...@@ -235,7 +237,10 @@ public class Operation extends Expression { ...@@ -235,7 +237,10 @@ public class Operation extends Expression {
f.setParameter(2, right); f.setParameter(2, right);
f.doneWithParameters(); f.doneWithParameters();
return f.optimize(session); return f.optimize(session);
} else if (l == Value.DECIMAL || l == Value.FLOAT || l == Value.DOUBLE) { }
case Value.DECIMAL:
case Value.FLOAT:
case Value.DOUBLE: {
// Oracle date add // Oracle date add
Function f = Function.getFunction(session.getDatabase(), "DATEADD"); Function f = Function.getFunction(session.getDatabase(), "DATEADD");
f.setParameter(0, ValueExpression.get(ValueString.get("SECOND"))); f.setParameter(0, ValueExpression.get(ValueString.get("SECOND")));
...@@ -245,42 +250,53 @@ public class Operation extends Expression { ...@@ -245,42 +250,53 @@ public class Operation extends Expression {
f.setParameter(2, right); f.setParameter(2, right);
f.doneWithParameters(); f.doneWithParameters();
return f.optimize(session); return f.optimize(session);
} else if (l == Value.TIME && r == Value.TIME) {
dataType = Value.TIME;
return this;
} else if (l == Value.TIME) {
dataType = Value.TIMESTAMP;
return this;
} }
} else if (opType == OpType.MINUS) { case Value.TIME:
if ((l == Value.DATE || l == Value.TIMESTAMP) && r == Value.INT) {
// Oracle date subtract
Function f = Function.getFunction(session.getDatabase(), "DATEADD");
f.setParameter(0, ValueExpression.get(ValueString.get("DAY")));
right = new Operation(OpType.NEGATE, right, null);
right = right.optimize(session);
f.setParameter(1, right);
f.setParameter(2, left);
f.doneWithParameters();
return f.optimize(session);
} else if ((l == Value.DATE || l == Value.TIMESTAMP) &&
(r == Value.DECIMAL || r == Value.FLOAT || r == Value.DOUBLE)) {
// Oracle date subtract
Function f = Function.getFunction(session.getDatabase(), "DATEADD");
f.setParameter(0, ValueExpression.get(ValueString.get("SECOND")));
right = new Operation(OpType.MULTIPLY, ValueExpression.get(ValueInt
.get(60 * 60 * 24)), right);
right = new Operation(OpType.NEGATE, right, null);
right = right.optimize(session);
f.setParameter(1, right);
f.setParameter(2, left);
f.doneWithParameters();
return f.optimize(session);
} else if (l == Value.DATE || l == Value.TIMESTAMP) {
if (r == Value.TIME) { if (r == Value.TIME) {
dataType = Value.TIME;
return this;
} else {
dataType = Value.TIMESTAMP;
return this;
}
}
break;
case MINUS:
switch (l) {
case Value.DATE:
case Value.TIMESTAMP:
switch (r) {
case Value.INT: {
// Oracle date subtract
Function f = Function.getFunction(session.getDatabase(), "DATEADD");
f.setParameter(0, ValueExpression.get(ValueString.get("DAY")));
right = new Operation(OpType.NEGATE, right, null);
right = right.optimize(session);
f.setParameter(1, right);
f.setParameter(2, left);
f.doneWithParameters();
return f.optimize(session);
}
case Value.DECIMAL:
case Value.FLOAT:
case Value.DOUBLE: {
// Oracle date subtract
Function f = Function.getFunction(session.getDatabase(), "DATEADD");
f.setParameter(0, ValueExpression.get(ValueString.get("SECOND")));
right = new Operation(OpType.MULTIPLY, ValueExpression.get(ValueInt
.get(60 * 60 * 24)), right);
right = new Operation(OpType.NEGATE, right, null);
right = right.optimize(session);
f.setParameter(1, right);
f.setParameter(2, left);
f.doneWithParameters();
return f.optimize(session);
}
case Value.TIME:
dataType = Value.TIMESTAMP; dataType = Value.TIMESTAMP;
return this; return this;
} else if (r == Value.DATE || r == Value.TIMESTAMP) { case Value.DATE:
case Value.TIMESTAMP: {
// Oracle date subtract // Oracle date subtract
Function f = Function.getFunction(session.getDatabase(), "DATEDIFF"); Function f = Function.getFunction(session.getDatabase(), "DATEDIFF");
f.setParameter(0, ValueExpression.get(ValueString.get("DAY"))); f.setParameter(0, ValueExpression.get(ValueString.get("DAY")));
...@@ -289,11 +305,17 @@ public class Operation extends Expression { ...@@ -289,11 +305,17 @@ public class Operation extends Expression {
f.doneWithParameters(); f.doneWithParameters();
return f.optimize(session); return f.optimize(session);
} }
} else if (l == Value.TIME && r == Value.TIME) { }
dataType = Value.TIME; break;
return this; case Value.TIME:
if (r == Value.TIME) {
dataType = Value.TIME;
return this;
}
break;
} }
} else if (opType == OpType.MULTIPLY) { break;
case MULTIPLY:
if (l == Value.TIME) { if (l == Value.TIME) {
dataType = Value.TIME; dataType = Value.TIME;
convertRight = false; convertRight = false;
...@@ -304,12 +326,15 @@ public class Operation extends Expression { ...@@ -304,12 +326,15 @@ public class Operation extends Expression {
convertRight = false; convertRight = false;
return this; return this;
} }
} else if (opType == OpType.DIVIDE) { break;
case DIVIDE:
if (l == Value.TIME) { if (l == Value.TIME) {
dataType = Value.TIME; dataType = Value.TIME;
convertRight = false; convertRight = false;
return this; return this;
} }
break;
default:
} }
throw DbException.getUnsupportedException( throw DbException.getUnsupportedException(
DataType.getDataType(l).name + " " + DataType.getDataType(l).name + " " +
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论