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

Extract Expression.writeExpressions() helper methods

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