提交 974ad323 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Extract Operation.optimizeDateTime()

上级 ce2f4d82
......@@ -217,129 +217,7 @@ public class Operation extends Expression {
dataType = Value.DECIMAL;
}
} else if (DataType.isDateTimeType(l) || DataType.isDateTimeType(r)) {
switch (opType) {
case PLUS:
if (r != Value.getHigherOrder(l, r)) {
// order left and right: INT < TIME < DATE < TIMESTAMP
swap();
int t = l;
l = r;
r = t;
}
switch (l) {
case Value.INT: {
// Oracle date add
Function f = Function.getFunction(session.getDatabase(), "DATEADD");
f.setParameter(0, ValueExpression.get(ValueString.get("DAY")));
f.setParameter(1, left);
f.setParameter(2, right);
f.doneWithParameters();
return f.optimize(session);
}
case Value.DECIMAL:
case Value.FLOAT:
case Value.DOUBLE: {
// Oracle date add
Function f = Function.getFunction(session.getDatabase(), "DATEADD");
f.setParameter(0, ValueExpression.get(ValueString.get("SECOND")));
left = new Operation(OpType.MULTIPLY, ValueExpression.get(ValueInt
.get(60 * 60 * 24)), left);
f.setParameter(1, left);
f.setParameter(2, right);
f.doneWithParameters();
return f.optimize(session);
}
case Value.TIME:
if (r == Value.TIME || r == Value.TIMESTAMP_TZ) {
dataType = r;
return this;
} else { // DATE, TIMESTAMP
dataType = Value.TIMESTAMP;
return this;
}
}
break;
case MINUS:
switch (l) {
case Value.DATE:
case Value.TIMESTAMP:
case Value.TIMESTAMP_TZ:
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;
return this;
case Value.DATE:
case Value.TIMESTAMP:
case Value.TIMESTAMP_TZ: {
// Oracle date subtract
Function f = Function.getFunction(session.getDatabase(), "DATEDIFF");
f.setParameter(0, ValueExpression.get(ValueString.get("DAY")));
f.setParameter(1, right);
f.setParameter(2, left);
f.doneWithParameters();
return f.optimize(session);
}
}
break;
case Value.TIME:
if (r == Value.TIME) {
dataType = Value.TIME;
return this;
}
break;
}
break;
case MULTIPLY:
if (l == Value.TIME) {
dataType = Value.TIME;
convertRight = false;
return this;
} else if (r == Value.TIME) {
swap();
dataType = Value.TIME;
convertRight = false;
return this;
}
break;
case DIVIDE:
if (l == Value.TIME) {
dataType = Value.TIME;
convertRight = false;
return this;
}
break;
default:
}
throw DbException.getUnsupportedException(
DataType.getDataType(l).name + " " +
getOperationToken() + " " +
DataType.getDataType(r).name);
return optimizeDateTime(session, l, r);
} else {
dataType = Value.getHigherOrder(l, r);
if (dataType == Value.ENUM) {
......@@ -359,6 +237,132 @@ public class Operation extends Expression {
return this;
}
private Expression optimizeDateTime(Session session, int l, int r) {
switch (opType) {
case PLUS:
if (r != Value.getHigherOrder(l, r)) {
// order left and right: INT < TIME < DATE < TIMESTAMP
swap();
int t = l;
l = r;
r = t;
}
switch (l) {
case Value.INT: {
// Oracle date add
Function f = Function.getFunction(session.getDatabase(), "DATEADD");
f.setParameter(0, ValueExpression.get(ValueString.get("DAY")));
f.setParameter(1, left);
f.setParameter(2, right);
f.doneWithParameters();
return f.optimize(session);
}
case Value.DECIMAL:
case Value.FLOAT:
case Value.DOUBLE: {
// Oracle date add
Function f = Function.getFunction(session.getDatabase(), "DATEADD");
f.setParameter(0, ValueExpression.get(ValueString.get("SECOND")));
left = new Operation(OpType.MULTIPLY, ValueExpression.get(ValueInt
.get(60 * 60 * 24)), left);
f.setParameter(1, left);
f.setParameter(2, right);
f.doneWithParameters();
return f.optimize(session);
}
case Value.TIME:
if (r == Value.TIME || r == Value.TIMESTAMP_TZ) {
dataType = r;
return this;
} else { // DATE, TIMESTAMP
dataType = Value.TIMESTAMP;
return this;
}
}
break;
case MINUS:
switch (l) {
case Value.DATE:
case Value.TIMESTAMP:
case Value.TIMESTAMP_TZ:
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;
return this;
case Value.DATE:
case Value.TIMESTAMP:
case Value.TIMESTAMP_TZ: {
// Oracle date subtract
Function f = Function.getFunction(session.getDatabase(), "DATEDIFF");
f.setParameter(0, ValueExpression.get(ValueString.get("DAY")));
f.setParameter(1, right);
f.setParameter(2, left);
f.doneWithParameters();
return f.optimize(session);
}
}
break;
case Value.TIME:
if (r == Value.TIME) {
dataType = Value.TIME;
return this;
}
break;
}
break;
case MULTIPLY:
if (l == Value.TIME) {
dataType = Value.TIME;
convertRight = false;
return this;
} else if (r == Value.TIME) {
swap();
dataType = Value.TIME;
convertRight = false;
return this;
}
break;
case DIVIDE:
if (l == Value.TIME) {
dataType = Value.TIME;
convertRight = false;
return this;
}
break;
default:
}
throw DbException.getUnsupportedException(
DataType.getDataType(l).name + " " +
getOperationToken() + " " +
DataType.getDataType(r).name);
}
private void swap() {
Expression temp = left;
left = right;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论