提交 a7b650a5 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Extract Expression.writeExpressions() helper methods

上级 ecb98c32
...@@ -418,14 +418,9 @@ public abstract class Prepared { ...@@ -418,14 +418,9 @@ public abstract class Prepared {
* @return the SQL snippet * @return the SQL snippet
*/ */
protected static String getSQL(Expression[] list) { protected static String getSQL(Expression[] list) {
StatementBuilder buff = new StatementBuilder(); StringBuilder builder = new StringBuilder();
for (Expression e : list) { Expression.writeExpressions(builder, list);
buff.appendExceptFirst(", "); return builder.toString();
if (e != null) {
e.getSQL(buff.builder());
}
}
return buff.toString();
} }
/** /**
......
...@@ -154,12 +154,7 @@ public class ConditionIn extends Condition { ...@@ -154,12 +154,7 @@ public class ConditionIn extends Condition {
public StringBuilder getSQL(StringBuilder builder) { public StringBuilder getSQL(StringBuilder builder) {
builder.append('('); builder.append('(');
left.getSQL(builder).append(" IN("); left.getSQL(builder).append(" IN(");
for (int i = 0; i < valueList.size(); i++) { writeExpressions(builder, valueList);
if (i > 0) {
builder.append(", ");
}
valueList.get(i).getSQL(builder);
}
return builder.append("))"); return builder.append("))");
} }
......
...@@ -116,12 +116,7 @@ public class ConditionInConstantSet extends Condition { ...@@ -116,12 +116,7 @@ public class ConditionInConstantSet extends Condition {
public StringBuilder getSQL(StringBuilder builder) { public StringBuilder getSQL(StringBuilder builder) {
builder.append('('); builder.append('(');
left.getSQL(builder).append(" IN("); left.getSQL(builder).append(" IN(");
for (int i = 0, s = valueList.size(); i < s; i++) { writeExpressions(builder, valueList);
if (i > 0) {
builder.append(", ");
}
valueList.get(i).getSQL(builder);
}
return builder.append("))"); return builder.append("))");
} }
......
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
*/ */
package org.h2.expression; package org.h2.expression;
import java.util.List;
import org.h2.engine.Database; import org.h2.engine.Database;
import org.h2.engine.Session; import org.h2.engine.Session;
import org.h2.result.ResultInterface; import org.h2.result.ResultInterface;
...@@ -38,6 +40,24 @@ public abstract class Expression { ...@@ -38,6 +40,24 @@ public abstract class Expression {
private boolean addedToFilter; private boolean addedToFilter;
public static void writeExpressions(StringBuilder builder, List<? extends Expression> expressions) {
for (int i = 0, length = expressions.size(); i < length; i++) {
if (i > 0) {
builder.append(", ");
}
expressions.get(i).getSQL(builder);
}
}
public static void writeExpressions(StringBuilder builder, Expression[] expressions) {
for (int i = 0, length = expressions.length; i < length; i++) {
if (i > 0) {
builder.append(", ");
}
expressions[i].getSQL(builder);
}
}
/** /**
* Return the resulting value for the current row. * Return the resulting value for the current row.
* *
......
...@@ -86,12 +86,7 @@ public class ExpressionList extends Expression { ...@@ -86,12 +86,7 @@ public class ExpressionList extends Expression {
@Override @Override
public StringBuilder getSQL(StringBuilder builder) { public StringBuilder getSQL(StringBuilder builder) {
builder.append('('); builder.append('(');
for (int i = 0; i < list.length; i++) { writeExpressions(builder, list);
if (i > 0) {
builder.append(", ");
}
list[i].getSQL(builder);
}
if (list.length == 1) { if (list.length == 1) {
builder.append(','); builder.append(',');
} }
......
...@@ -2627,14 +2627,8 @@ public class Function extends Expression implements FunctionCall { ...@@ -2627,14 +2627,8 @@ public class Function extends Expression implements FunctionCall {
args[1].getSQL(builder); args[1].getSQL(builder);
break; break;
} }
default: { default:
for (int i = 0; i < args.length; i++) { writeExpressions(builder, args);
if (i > 0) {
builder.append(", ");
}
args[i].getSQL(builder);
}
}
} }
return builder.append(')'); return builder.append(')');
} }
......
...@@ -95,12 +95,7 @@ public class JavaFunction extends Expression implements FunctionCall { ...@@ -95,12 +95,7 @@ public class JavaFunction extends Expression implements FunctionCall {
Parser.quoteIdentifier(builder, functionAlias.getSchema().getName()).append('.'); Parser.quoteIdentifier(builder, functionAlias.getSchema().getName()).append('.');
} }
Parser.quoteIdentifier(builder, functionAlias.getName()).append('('); Parser.quoteIdentifier(builder, functionAlias.getName()).append('(');
for (int i = 0; i < args.length; i++) { writeExpressions(builder, this.args);
if (i > 0) {
builder.append(", ");
}
args[i].getSQL(builder);
}
return builder.append(')'); return builder.append(')');
} }
......
...@@ -123,12 +123,7 @@ public class Wildcard extends Expression { ...@@ -123,12 +123,7 @@ public class Wildcard extends Expression {
builder.append('*'); builder.append('*');
if (exceptColumns != null) { if (exceptColumns != null) {
builder.append(" EXCEPT ("); builder.append(" EXCEPT (");
for (int i = 0; i < exceptColumns.size(); i++) { writeExpressions(builder, exceptColumns);
if (i > 0) {
builder.append(", ");
}
exceptColumns.get(i).getSQL(builder);
}
builder.append(')'); builder.append(')');
} }
return builder; return builder;
......
...@@ -69,12 +69,7 @@ public class JavaAggregate extends AbstractAggregate { ...@@ -69,12 +69,7 @@ public class JavaAggregate extends AbstractAggregate {
@Override @Override
public StringBuilder getSQL(StringBuilder builder) { public StringBuilder getSQL(StringBuilder builder) {
Parser.quoteIdentifier(builder, userAggregate.getName()).append('('); Parser.quoteIdentifier(builder, userAggregate.getName()).append('(');
for (int i = 0; i < args.length; i++) { writeExpressions(builder, args);
if (i > 0) {
builder.append(", ");
}
args[i].getSQL(builder);
}
builder.append(')'); builder.append(')');
return appendTailConditions(builder); return appendTailConditions(builder);
} }
......
...@@ -519,12 +519,7 @@ public class WindowFunction extends DataAnalysisOperation { ...@@ -519,12 +519,7 @@ public class WindowFunction extends DataAnalysisOperation {
String name = type.getSQL(); String name = type.getSQL();
builder.append(name).append('('); builder.append(name).append('(');
if (args != null) { if (args != null) {
for (int i = 0, numArgs = args.length; i < numArgs; i++) { writeExpressions(builder, args);
if (i > 0) {
builder.append(", ");
}
args[i].getSQL(builder);
}
} }
builder.append(')'); builder.append(')');
if (fromLast && type == WindowFunctionType.NTH_VALUE) { if (fromLast && type == WindowFunctionType.NTH_VALUE) {
......
...@@ -177,50 +177,47 @@ public class IndexCondition { ...@@ -177,50 +177,47 @@ public class IndexCondition {
if (compareType == Comparison.FALSE) { if (compareType == Comparison.FALSE) {
return "FALSE"; return "FALSE";
} }
StatementBuilder buff = new StatementBuilder(); StringBuilder builder = new StringBuilder();
buff.append(column.getSQL()); builder.append(column.getSQL());
switch (compareType) { switch (compareType) {
case Comparison.EQUAL: case Comparison.EQUAL:
buff.append(" = "); builder.append(" = ");
break; break;
case Comparison.EQUAL_NULL_SAFE: case Comparison.EQUAL_NULL_SAFE:
buff.append(" IS "); builder.append(" IS ");
break; break;
case Comparison.BIGGER_EQUAL: case Comparison.BIGGER_EQUAL:
buff.append(" >= "); builder.append(" >= ");
break; break;
case Comparison.BIGGER: case Comparison.BIGGER:
buff.append(" > "); builder.append(" > ");
break; break;
case Comparison.SMALLER_EQUAL: case Comparison.SMALLER_EQUAL:
buff.append(" <= "); builder.append(" <= ");
break; break;
case Comparison.SMALLER: case Comparison.SMALLER:
buff.append(" < "); builder.append(" < ");
break; break;
case Comparison.IN_LIST: case Comparison.IN_LIST:
buff.append(" IN("); builder.append(" IN(");
for (Expression e : expressionList) { Expression.writeExpressions(builder, expressionList);
buff.appendExceptFirst(", "); builder.append(')');
e.getSQL(buff.builder());
}
buff.append(')');
break; break;
case Comparison.IN_QUERY: case Comparison.IN_QUERY:
buff.append(" IN("); builder.append(" IN(");
buff.append(expressionQuery.getPlanSQL()); builder.append(expressionQuery.getPlanSQL());
buff.append(')'); builder.append(')');
break; break;
case Comparison.SPATIAL_INTERSECTS: case Comparison.SPATIAL_INTERSECTS:
buff.append(" && "); builder.append(" && ");
break; break;
default: default:
DbException.throwInternalError("type=" + compareType); DbException.throwInternalError("type=" + compareType);
} }
if (expression != null) { if (expression != null) {
expression.getSQL(buff.builder()); expression.getSQL(builder);
} }
return buff.toString(); return builder.toString();
} }
/** /**
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论