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

Add Expression.getSQL(StringBuilder)

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