提交 07154efb authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Add Expression.getSQL(StringBuilder)

上级 274abf34
......@@ -422,7 +422,7 @@ public abstract class Prepared {
for (Expression e : list) {
buff.appendExceptFirst(", ");
if (e != null) {
buff.append(e.getSQL());
e.getSQL(buff.builder());
}
}
return buff.toString();
......
......@@ -421,7 +421,8 @@ public class Insert extends Prepared implements ResultTarget {
for (Column column : duplicateKeyAssignmentMap.keySet()) {
buff.appendExceptFirst(", ");
Expression ex = duplicateKeyAssignmentMap.get(column);
buff.append(column.getSQL()).append('=').append(ex.getSQL());
buff.append(column.getSQL()).append('=');
ex.getSQL(buff.builder());
}
buff.append(" WHERE ");
Index foundIndex = (Index) de.getSource();
......@@ -429,7 +430,7 @@ public class Insert extends Prepared implements ResultTarget {
throw DbException.getUnsupportedException(
"Unable to apply ON DUPLICATE KEY UPDATE, no index found!");
}
buff.append(prepareUpdateCondition(foundIndex, row).getSQL());
prepareUpdateCondition(foundIndex, row).getSQL(buff.builder());
String sql = buff.toString();
Update command = (Update) session.prepare(sql);
command.setUpdateToCurrentValuesReturnsZero(true);
......
......@@ -248,7 +248,7 @@ public class Merge extends Prepared {
if (e == null) {
buff.append("DEFAULT");
} else {
buff.append(e.getSQL());
e.getSQL(buff.builder());
}
}
buff.append(')');
......
......@@ -228,7 +228,7 @@ public class Replace extends Prepared {
if (e == null) {
buff.append("DEFAULT");
} else {
buff.append(e.getSQL());
e.getSQL(buff.builder());
}
}
buff.append(')');
......
......@@ -1327,7 +1327,7 @@ public class Select extends Query {
buff.append(" ON(");
for (Expression distinctExpression: distinctExpressions) {
buff.appendExceptFirst(", ");
buff.append(distinctExpression.getSQL());
distinctExpression.getSQL(buff.builder());
}
buff.append(')');
buff.resetCount();
......
......@@ -33,9 +33,10 @@ public class SelectOrderBy {
public String getSQL() {
StringBuilder buff = new StringBuilder();
if (expression != null) {
buff.append('=').append(expression.getSQL());
buff.append('=');
expression.getSQL(buff);
} else {
buff.append(columnIndexExpr.getSQL());
columnIndexExpr.getSQL(buff);
}
SortOrder.typeToString(buff, sortType);
return buff.toString();
......
......@@ -222,7 +222,8 @@ public class Update extends Prepared {
for (Column c : columns) {
Expression e = expressionMap.get(c);
buff.appendExceptFirst(",\n ");
buff.append(c.getName()).append(" = ").append(e.getSQL());
buff.append(c.getName()).append(" = ");
e.getSQL(buff.builder());
}
if (condition != null) {
buff.append("\nWHERE ").append(StringUtils.unEnclose(condition.getSQL()));
......
......@@ -64,7 +64,9 @@ public class ConstraintCheck extends Constraint {
}
private String getShortDescription() {
return getName() + ": " + expr.getSQL();
StringBuilder builder = new StringBuilder().append(getName()).append(": ");
expr.getSQL(builder);
return builder.toString();
}
@Override
......@@ -140,8 +142,11 @@ public class ConstraintCheck extends Constraint {
// don't check at startup
return;
}
String sql = "SELECT 1 FROM " + filter.getTable().getSQL() +
" WHERE NOT(" + expr.getSQL() + ")";
StringBuilder builder = new StringBuilder().append("SELECT 1 FROM ")
.append(filter.getTable().getSQL())
.append(" WHERE NOT(");
expr.getSQL(builder).append(')');
String sql = builder.toString();
ResultInterface r = session.prepare(sql).query(1);
if (r.next()) {
throw DbException.get(ErrorCode.CHECK_CONSTRAINT_VIOLATED_1, getName());
......
......@@ -78,8 +78,8 @@ public class Alias extends Expression {
}
@Override
public String getSQL() {
return expr.getSQL() + " AS " + Parser.quoteIdentifier(alias);
public StringBuilder getSQL(StringBuilder builder) {
return expr.getSQL(builder).append(" AS ").append(Parser.quoteIdentifier(alias));
}
@Override
......
......@@ -68,10 +68,12 @@ public class BinaryOperation extends Expression {
}
@Override
public String getSQL() {
public StringBuilder getSQL(StringBuilder builder) {
// don't remove the space, otherwise it might end up some thing like
// --1 which is a line remark
return '(' + left.getSQL() + ' ' + getOperationToken() + ' ' + right.getSQL() + ')';
builder.append('(');
left.getSQL(builder).append(' ').append(getOperationToken()).append(' ');
return right.getSQL(builder).append(')');
}
private String getOperationToken() {
......
......@@ -76,17 +76,20 @@ public class CompareLike extends Condition {
}
@Override
public String getSQL() {
String sql;
public StringBuilder getSQL(StringBuilder builder) {
builder.append('(');
if (regexp) {
sql = left.getSQL() + " REGEXP " + right.getSQL();
left.getSQL(builder).append(" REGEXP ");
right.getSQL(builder);
} else {
sql = left.getSQL() + " LIKE " + right.getSQL();
left.getSQL(builder).append(" LIKE ");
right.getSQL(builder);
if (escape != null) {
sql += " ESCAPE " + escape.getSQL();
builder.append(" ESCAPE ");
escape.getSQL(builder);
}
}
return "(" + sql + ")";
return builder.append(')');
}
@Override
......
......@@ -126,23 +126,25 @@ public class Comparison extends Condition {
}
@Override
public String getSQL() {
String sql;
public StringBuilder getSQL(StringBuilder builder) {
builder.append('(');
switch (compareType) {
case IS_NULL:
sql = left.getSQL() + " IS NULL";
left.getSQL(builder).append(" IS NULL");
break;
case IS_NOT_NULL:
sql = left.getSQL() + " IS NOT NULL";
left.getSQL(builder).append(" IS NOT NULL");
break;
case SPATIAL_INTERSECTS:
sql = "INTERSECTS(" + left.getSQL() + ", " + right.getSQL() + ")";
builder.append("INTERSECTS(");
left.getSQL(builder).append(", ");
right.getSQL(builder).append(')');
break;
default:
sql = left.getSQL() + " " + getCompareOperator(compareType) +
" " + right.getSQL();
left.getSQL(builder).append(' ').append(getCompareOperator(compareType)).append(' ');
right.getSQL(builder);
}
return "(" + sql + ")";
return builder.append(')');
}
/**
......
......@@ -42,19 +42,20 @@ public class ConditionAndOr extends Condition {
}
@Override
public String getSQL() {
String sql;
public StringBuilder getSQL(StringBuilder builder) {
builder.append('(');
left.getSQL(builder);
switch (andOrType) {
case AND:
sql = left.getSQL() + "\n AND " + right.getSQL();
builder.append("\n AND ");
break;
case OR:
sql = left.getSQL() + "\n OR " + right.getSQL();
builder.append("\n OR ");
break;
default:
throw DbException.throwInternalError("andOrType=" + andOrType);
}
return "(" + sql + ")";
return right.getSQL(builder).append(')');
}
@Override
......
......@@ -41,8 +41,10 @@ public class ConditionExists extends Condition {
}
@Override
public String getSQL() {
return "EXISTS(\n" + StringUtils.indent(query.getPlanSQL(), 4, false) + ")";
public StringBuilder getSQL(StringBuilder builder) {
builder.append("EXISTS(\n");
builder.append(StringUtils.indent(query.getPlanSQL(), 4, false));
return builder.append(')');
}
@Override
......
......@@ -11,7 +11,6 @@ import org.h2.engine.Session;
import org.h2.index.IndexCondition;
import org.h2.table.ColumnResolver;
import org.h2.table.TableFilter;
import org.h2.util.StatementBuilder;
import org.h2.value.Value;
import org.h2.value.ValueBoolean;
import org.h2.value.ValueNull;
......@@ -152,14 +151,16 @@ public class ConditionIn extends Condition {
}
@Override
public String getSQL() {
StatementBuilder buff = new StatementBuilder("(");
buff.append(left.getSQL()).append(" IN(");
for (Expression e : valueList) {
buff.appendExceptFirst(", ");
buff.append(e.getSQL());
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);
}
return buff.append("))").toString();
return builder.append("))");
}
@Override
......
......@@ -15,7 +15,6 @@ import org.h2.index.IndexCondition;
import org.h2.message.DbException;
import org.h2.table.ColumnResolver;
import org.h2.table.TableFilter;
import org.h2.util.StatementBuilder;
import org.h2.value.ExtTypeInfo;
import org.h2.value.Value;
import org.h2.value.ValueBoolean;
......@@ -114,14 +113,16 @@ public class ConditionInConstantSet extends Condition {
}
@Override
public String getSQL() {
StatementBuilder buff = new StatementBuilder("(");
buff.append(left.getSQL()).append(" IN(");
for (Expression e : valueList) {
buff.appendExceptFirst(", ");
buff.append(e.getSQL());
}
return buff.append("))").toString();
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);
}
return builder.append("))");
}
@Override
......
......@@ -140,8 +140,10 @@ public class ConditionInParameter extends Condition {
}
@Override
public String getSQL() {
return '(' + left.getSQL() + " = ANY(" + parameter.getSQL() + "))";
public StringBuilder getSQL(StringBuilder builder) {
builder.append('(');
left.getSQL(builder).append(" = ANY(");
return parameter.getSQL(builder).append("))");
}
@Override
......
......@@ -129,23 +129,21 @@ public class ConditionInSelect extends Condition {
}
@Override
public String getSQL() {
StringBuilder buff = new StringBuilder();
buff.append('(').append(left.getSQL()).append(' ');
public StringBuilder getSQL(StringBuilder builder) {
builder.append('(');
left.getSQL(builder).append(' ');
if (all) {
buff.append(Comparison.getCompareOperator(compareType)).
builder.append(Comparison.getCompareOperator(compareType)).
append(" ALL");
} else {
if (compareType == Comparison.EQUAL) {
buff.append("IN");
builder.append("IN");
} else {
buff.append(Comparison.getCompareOperator(compareType)).
builder.append(Comparison.getCompareOperator(compareType)).
append(" ANY");
}
}
buff.append("(\n").append(StringUtils.indent(query.getPlanSQL(), 4, false)).
append("))");
return buff.toString();
return builder.append("(\n").append(StringUtils.indent(query.getPlanSQL(), 4, false)).append("))");
}
@Override
......
......@@ -65,8 +65,9 @@ public class ConditionNot extends Condition {
}
@Override
public String getSQL() {
return "(NOT " + condition.getSQL() + ")";
public StringBuilder getSQL(StringBuilder builder) {
builder.append("(NOT ");
return condition.getSQL(builder).append(')');
}
@Override
......
......@@ -110,7 +110,22 @@ public abstract class Expression {
*
* @return the SQL statement
*/
public abstract String getSQL();
public String getSQL() {
StringBuilder builder = new StringBuilder();
getSQL(builder);
return builder.toString();
}
/**
* Appends the SQL statement of this expression to the specified builder.
* This may not always be the original SQL statement, specially after
* optimization.
*
* @param builder
* string builder
* @return the specified string builder
*/
public abstract StringBuilder getSQL(StringBuilder builder);
/**
* Update an aggregate value. This method is called at statement execution
......
......@@ -55,23 +55,22 @@ public class ExpressionColumn extends Expression {
}
@Override
public String getSQL() {
String sql;
public StringBuilder getSQL(StringBuilder builder) {
boolean quote = database.getSettings().databaseToUpper;
if (column != null) {
sql = column.getSQL();
} else {
sql = quote ? Parser.quoteIdentifier(columnName) : columnName;
if (schemaName != null) {
String s = quote ? Parser.quoteIdentifier(schemaName) : schemaName;
builder.append(s).append('.');
}
if (tableAlias != null) {
String a = quote ? Parser.quoteIdentifier(tableAlias) : tableAlias;
sql = a + "." + sql;
builder.append(a).append('.');
}
if (schemaName != null) {
String s = quote ? Parser.quoteIdentifier(schemaName) : schemaName;
sql = s + "." + sql;
if (column != null) {
builder.append(column.getSQL());
} else {
builder.append(quote ? Parser.quoteIdentifier(columnName) : columnName);
}
return sql;
return builder;
}
public TableFilter getTableFilter() {
......
......@@ -9,7 +9,6 @@ import org.h2.engine.Session;
import org.h2.table.Column;
import org.h2.table.ColumnResolver;
import org.h2.table.TableFilter;
import org.h2.util.StatementBuilder;
import org.h2.value.Value;
import org.h2.value.ValueArray;
......@@ -85,16 +84,18 @@ public class ExpressionList extends Expression {
}
@Override
public String getSQL() {
StatementBuilder buff = new StatementBuilder("(");
for (Expression e: list) {
buff.appendExceptFirst(", ");
buff.append(e.getSQL());
public StringBuilder getSQL(StringBuilder builder) {
builder.append('(');
for (int i = 0; i < list.length; i++) {
if (i > 0) {
builder.append(", ");
}
list[i].getSQL(builder);
}
if (list.length == 1) {
buff.append(',');
builder.append(',');
}
return buff.append(')').toString();
return builder.append(')');
}
@Override
......
......@@ -50,7 +50,6 @@ import org.h2.util.DateTimeUtils;
import org.h2.util.IOUtils;
import org.h2.util.JdbcUtils;
import org.h2.util.MathUtils;
import org.h2.util.StatementBuilder;
import org.h2.util.StringUtils;
import org.h2.util.ToChar;
import org.h2.util.ToDateParser;
......@@ -2582,36 +2581,41 @@ public class Function extends Expression implements FunctionCall {
}
@Override
public String getSQL() {
StatementBuilder buff = new StatementBuilder(info.name);
public StringBuilder getSQL(StringBuilder builder) {
builder.append(info.name);
if (info.type == CASE) {
if (args[0] != null) {
buff.append(' ').append(args[0].getSQL());
builder.append(' ');
args[0].getSQL(builder);
}
for (int i = 1, len = args.length - 1; i < len; i += 2) {
buff.append(" WHEN ").append(args[i].getSQL());
buff.append(" THEN ").append(args[i + 1].getSQL());
builder.append(" WHEN ");
args[i].getSQL(builder);
builder.append(" THEN ");
args[i + 1].getSQL(builder);
}
if (args.length % 2 == 0) {
buff.append(" ELSE ").append(args[args.length - 1].getSQL());
builder.append(" ELSE ");
args[args.length - 1].getSQL(builder);
}
return buff.append(" END").toString();
return builder.append(" END");
}
buff.append('(');
builder.append('(');
switch (info.type) {
case CAST: {
buff.append(args[0].getSQL()).append(" AS ").
args[0].getSQL(builder).append(" AS ").
append(new Column(null, dataType, precision,
scale, displaySize, extTypeInfo).getCreateSQL());
break;
}
case CONVERT: {
if (database.getMode().swapConvertFunctionParameters) {
buff.append(new Column(null, dataType, precision,
builder.append(new Column(null, dataType, precision,
scale, displaySize).getCreateSQL()).
append(',').append(args[0].getSQL());
append(',');
args[0].getSQL(builder);
} else {
buff.append(args[0].getSQL()).append(',').
args[0].getSQL(builder).append(',').
append(new Column(null, dataType, precision,
scale, displaySize).getCreateSQL());
}
......@@ -2619,17 +2623,20 @@ public class Function extends Expression implements FunctionCall {
}
case EXTRACT: {
ValueString v = (ValueString) ((ValueExpression) args[0]).getValue(null);
buff.append(v.getString()).append(" FROM ").append(args[1].getSQL());
builder.append(v.getString()).append(" FROM ");
args[1].getSQL(builder);
break;
}
default: {
for (Expression e : args) {
buff.appendExceptFirst(", ");
buff.append(e.getSQL());
for (int i = 0; i < args.length; i++) {
if (i > 0) {
builder.append(", ");
}
args[i].getSQL(builder);
}
}
}
return buff.append(')').toString();
return builder.append(')');
}
@Override
......
......@@ -113,8 +113,10 @@ public class IntervalOperation extends Expression {
}
@Override
public String getSQL() {
return '(' + left.getSQL() + ' ' + getOperationToken() + ' ' + right.getSQL() + ')';
public StringBuilder getSQL(StringBuilder builder) {
builder.append('(');
left.getSQL(builder).append(' ').append(getOperationToken()).append(' ');
return right.getSQL(builder).append(')');
}
private char getOperationToken() {
......
......@@ -11,7 +11,6 @@ import org.h2.engine.FunctionAlias;
import org.h2.engine.Session;
import org.h2.table.ColumnResolver;
import org.h2.table.TableFilter;
import org.h2.util.StatementBuilder;
import org.h2.value.DataType;
import org.h2.value.Value;
import org.h2.value.ValueArray;
......@@ -89,21 +88,22 @@ public class JavaFunction extends Expression implements FunctionCall {
}
@Override
public String getSQL() {
StatementBuilder buff = new StatementBuilder();
public StringBuilder getSQL(StringBuilder builder) {
// TODO always append the schema once FUNCTIONS_IN_SCHEMA is enabled
if (functionAlias.getDatabase().getSettings().functionsInSchema ||
!functionAlias.getSchema().getName().equals(Constants.SCHEMA_MAIN)) {
buff.append(
builder.append(
Parser.quoteIdentifier(functionAlias.getSchema().getName()))
.append('.');
}
buff.append(Parser.quoteIdentifier(functionAlias.getName())).append('(');
for (Expression e : args) {
buff.appendExceptFirst(", ");
buff.append(e.getSQL());
builder.append(Parser.quoteIdentifier(functionAlias.getName())).append('(');
for (int i = 0; i < args.length; i++) {
if (i > 0) {
builder.append(", ");
}
args[i].getSQL(builder);
}
return buff.append(')').toString();
return builder.append(')');
}
@Override
......
......@@ -30,8 +30,8 @@ public class Parameter extends Expression implements ParameterInterface {
}
@Override
public String getSQL() {
return "?" + (index + 1);
public StringBuilder getSQL(StringBuilder builder) {
return builder.append('?').append(index + 1);
}
@Override
......
......@@ -72,6 +72,11 @@ public class Rownum extends Expression {
return "ROWNUM()";
}
@Override
public StringBuilder getSQL(StringBuilder builder) {
return builder.append("ROWNUM()");
}
@Override
public void updateAggregate(Session session, int stage) {
// nothing to do
......
......@@ -67,8 +67,9 @@ public class SequenceValue extends Expression {
}
@Override
public String getSQL() {
return "(NEXT VALUE FOR " + sequence.getSQL() +")";
public StringBuilder getSQL(StringBuilder builder) {
builder.append("(NEXT VALUE FOR ");
return builder.append(sequence.getSQL()).append(')');
}
@Override
......
......@@ -89,8 +89,8 @@ public class Subquery extends Expression {
}
@Override
public String getSQL() {
return "(" + query.getPlanSQL() + ")";
public StringBuilder getSQL(StringBuilder builder) {
return builder.append('(').append(query.getPlanSQL()).append(')');
}
@Override
......
......@@ -52,7 +52,8 @@ public class TableFunction extends Function {
int i = 0;
for (Expression e : args) {
buff.appendExceptFirst(", ");
buff.append(columnList[i++].getCreateSQL()).append('=').append(e.getSQL());
buff.append(columnList[i++].getCreateSQL()).append('=');
e.getSQL(buff.builder());
}
return buff.append(')').toString();
}
......
......@@ -24,10 +24,11 @@ public class UnaryOperation extends Expression {
}
@Override
public String getSQL() {
public StringBuilder getSQL(StringBuilder builder) {
// don't remove the space, otherwise it might end up some thing like
// --1 which is a line remark
return "(- " + arg.getSQL() + ')';
builder.append("(- ");
return arg.getSQL(builder).append(')');
}
@Override
......
......@@ -142,6 +142,16 @@ public class ValueExpression extends Expression {
return value.getSQL();
}
@Override
public StringBuilder getSQL(StringBuilder builder) {
if (this == DEFAULT) {
builder.append("DEFAULT");
} else {
builder.append(value.getSQL());
}
return builder;
}
@Override
public void updateAggregate(Session session, int stage) {
// nothing to do
......
......@@ -41,8 +41,8 @@ public class Variable extends Expression {
}
@Override
public String getSQL() {
return "@" + Parser.quoteIdentifier(name);
public StringBuilder getSQL(StringBuilder builder) {
return builder.append('@').append(Parser.quoteIdentifier(name));
}
@Override
......
......@@ -116,8 +116,7 @@ public class Wildcard extends Expression {
}
@Override
public String getSQL() {
StringBuilder builder = new StringBuilder();
public StringBuilder getSQL(StringBuilder builder) {
if (table != null) {
builder.append(StringUtils.quoteIdentifier(table)).append('.');
}
......@@ -128,12 +127,11 @@ public class Wildcard extends Expression {
if (i > 0) {
builder.append(", ");
}
ExpressionColumn ec = exceptColumns.get(i);
builder.append(ec.getSQL());
exceptColumns.get(i).getSQL(builder);
}
builder.append(')');
}
return builder.toString();
return builder;
}
@Override
......
......@@ -159,7 +159,8 @@ public abstract class AbstractAggregate extends DataAnalysisOperation {
@Override
protected StringBuilder appendTailConditions(StringBuilder builder) {
if (filterCondition != null) {
builder.append(" FILTER (WHERE ").append(filterCondition.getSQL()).append(')');
builder.append(" FILTER (WHERE ");
filterCondition.getSQL(builder).append(')');
}
return super.appendTailConditions(builder);
}
......
......@@ -729,39 +729,40 @@ public class Aggregate extends AbstractAggregate {
return displaySize;
}
private String getSQLGroupConcat() {
StringBuilder buff = new StringBuilder("GROUP_CONCAT(");
private StringBuilder getSQLGroupConcat(StringBuilder builder) {
builder.append("GROUP_CONCAT(");
if (distinct) {
buff.append("DISTINCT ");
builder.append("DISTINCT ");
}
buff.append(on.getSQL());
Window.appendOrderBy(buff, orderByList);
on.getSQL(builder);
Window.appendOrderBy(builder, orderByList);
if (groupConcatSeparator != null) {
buff.append(" SEPARATOR ").append(groupConcatSeparator.getSQL());
builder.append(" SEPARATOR ");
groupConcatSeparator.getSQL(builder);
}
buff.append(')');
return appendTailConditions(buff).toString();
builder.append(')');
return appendTailConditions(builder);
}
private String getSQLArrayAggregate() {
StringBuilder buff = new StringBuilder("ARRAY_AGG(");
private StringBuilder getSQLArrayAggregate(StringBuilder builder) {
builder.append("ARRAY_AGG(");
if (distinct) {
buff.append("DISTINCT ");
builder.append("DISTINCT ");
}
buff.append(on.getSQL());
Window.appendOrderBy(buff, orderByList);
buff.append(')');
return appendTailConditions(buff).toString();
on.getSQL(builder);
Window.appendOrderBy(builder, orderByList);
builder.append(')');
return appendTailConditions(builder);
}
@Override
public String getSQL() {
public StringBuilder getSQL(StringBuilder builder) {
String text;
switch (type) {
case GROUP_CONCAT:
return getSQLGroupConcat();
return getSQLGroupConcat(builder);
case COUNT_ALL:
return appendTailConditions(new StringBuilder().append("COUNT(*)")).toString();
return appendTailConditions(builder.append("COUNT(*)"));
case COUNT:
text = "COUNT";
break;
......@@ -811,7 +812,7 @@ public class Aggregate extends AbstractAggregate {
text = "MEDIAN";
break;
case ARRAY_AGG:
return getSQLArrayAggregate();
return getSQLArrayAggregate(builder);
case MODE:
text = "MODE";
break;
......@@ -821,13 +822,14 @@ public class Aggregate extends AbstractAggregate {
default:
throw DbException.throwInternalError("type=" + type);
}
StringBuilder builder = new StringBuilder().append(text);
builder.append(text);
if (distinct) {
builder.append("(DISTINCT ").append(on.getSQL()).append(')');
builder.append("(DISTINCT ");
on.getSQL(builder).append(')');
} else {
builder.append(StringUtils.enclose(on.getSQL()));
}
return appendTailConditions(builder).toString();
return appendTailConditions(builder);
}
private Index getMinMaxColumnIndex() {
......
......@@ -17,7 +17,6 @@ import org.h2.expression.ExpressionVisitor;
import org.h2.message.DbException;
import org.h2.table.ColumnResolver;
import org.h2.table.TableFilter;
import org.h2.util.StatementBuilder;
import org.h2.value.DataType;
import org.h2.value.Value;
import org.h2.value.ValueArray;
......@@ -68,15 +67,16 @@ public class JavaAggregate extends AbstractAggregate {
}
@Override
public String getSQL() {
StatementBuilder buff = new StatementBuilder();
buff.append(Parser.quoteIdentifier(userAggregate.getName())).append('(');
for (Expression e : args) {
buff.appendExceptFirst(", ");
buff.append(e.getSQL());
public StringBuilder getSQL(StringBuilder builder) {
builder.append(Parser.quoteIdentifier(userAggregate.getName())).append('(');
for (int i = 0; i < args.length; i++) {
if (i > 0) {
builder.append(", ");
}
args[i].getSQL(builder);
}
buff.append(')');
return appendTailConditions(buff.builder()).toString();
builder.append(')');
return appendTailConditions(builder);
}
@Override
......
......@@ -49,7 +49,7 @@ public final class Window {
if (i > 0) {
builder.append(", ");
}
builder.append(o.expression.getSQL());
o.expression.getSQL(builder);
SortOrder.typeToString(builder, o.sortType);
}
}
......
......@@ -62,7 +62,8 @@ public class WindowFrameBound {
*/
public String getSQL(boolean following) {
if (type == WindowFrameBoundType.PRECEDING || type == WindowFrameBoundType.FOLLOWING) {
return value.getSQL() + ' ' + type.getSQL();
StringBuilder builder = new StringBuilder();
return value.getSQL(builder).append(' ').append(type.getSQL()).toString();
}
return type.getSQL();
}
......
......@@ -515,15 +515,15 @@ public class WindowFunction extends DataAnalysisOperation {
}
@Override
public String getSQL() {
public StringBuilder getSQL(StringBuilder builder) {
String name = type.getSQL();
StringBuilder builder = new StringBuilder().append(name).append('(');
builder.append(name).append('(');
if (args != null) {
for (int i = 0, numArgs = args.length; i < numArgs; i++) {
if (i > 0) {
builder.append(", ");
}
builder.append(args[i].getSQL());
args[i].getSQL(builder);
}
}
builder.append(')');
......@@ -542,7 +542,7 @@ public class WindowFunction extends DataAnalysisOperation {
default:
}
}
return appendTailConditions(builder).toString();
return appendTailConditions(builder);
}
@Override
......
......@@ -202,7 +202,7 @@ public class IndexCondition {
buff.append(" IN(");
for (Expression e : expressionList) {
buff.appendExceptFirst(", ");
buff.append(e.getSQL());
e.getSQL(buff.builder());
}
buff.append(')');
break;
......@@ -218,7 +218,7 @@ public class IndexCondition {
DbException.throwInternalError("type=" + compareType);
}
if (expression != null) {
buff.append(expression.getSQL());
expression.getSQL(buff.builder());
}
return buff.toString();
}
......
......@@ -72,11 +72,15 @@ public class RangeTable extends Table {
@Override
public String getSQL() {
String sql = NAME + "(" + min.getSQL() + ", " + max.getSQL();
StringBuilder builder = new StringBuilder();
builder.append(NAME).append('(');
min.getSQL(builder).append(", ");
max.getSQL(builder);
if (step != null) {
sql += ", " + step.getSQL();
builder.append(", ");
step.getSQL(builder);
}
return sql + ")";
return builder.append(')').toString();
}
@Override
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论