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

Add Expression.getUnenclosedSQL(StringBuilder)

上级 d0ecd064
...@@ -20,7 +20,6 @@ import org.h2.result.RowList; ...@@ -20,7 +20,6 @@ import org.h2.result.RowList;
import org.h2.table.PlanItem; import org.h2.table.PlanItem;
import org.h2.table.Table; import org.h2.table.Table;
import org.h2.table.TableFilter; import org.h2.table.TableFilter;
import org.h2.util.StringUtils;
import org.h2.value.Value; import org.h2.value.Value;
import org.h2.value.ValueNull; import org.h2.value.ValueNull;
...@@ -140,12 +139,12 @@ public class Delete extends Prepared { ...@@ -140,12 +139,12 @@ public class Delete extends Prepared {
buff.append("DELETE "); buff.append("DELETE ");
buff.append("FROM ").append(targetTableFilter.getPlanSQL(false)); buff.append("FROM ").append(targetTableFilter.getPlanSQL(false));
if (condition != null) { if (condition != null) {
buff.append("\nWHERE ").append(StringUtils.unEnclose( buff.append("\nWHERE ");
condition.getSQL())); condition.getUnenclosedSQL(buff);
} }
if (limitExpr != null) { if (limitExpr != null) {
buff.append("\nLIMIT (").append(StringUtils.unEnclose( buff.append("\nLIMIT (");
limitExpr.getSQL())).append(')'); limitExpr.getUnenclosedSQL(buff).append(')');
} }
return buff.toString(); return buff.toString();
} }
......
...@@ -1360,8 +1360,8 @@ public class Select extends Query { ...@@ -1360,8 +1360,8 @@ public class Select extends Query {
} }
} }
if (condition != null) { if (condition != null) {
buff.append("\nWHERE ").append( buff.append("\nWHERE ");
StringUtils.unEnclose(condition.getSQL())); condition.getUnenclosedSQL(buff.builder());
} }
if (groupIndex != null) { if (groupIndex != null) {
buff.append("\nGROUP BY "); buff.append("\nGROUP BY ");
...@@ -1370,7 +1370,7 @@ public class Select extends Query { ...@@ -1370,7 +1370,7 @@ public class Select extends Query {
Expression g = exprList[gi]; Expression g = exprList[gi];
g = g.getNonAliasExpression(); g = g.getNonAliasExpression();
buff.appendExceptFirst(", "); buff.appendExceptFirst(", ");
buff.append(StringUtils.unEnclose(g.getSQL())); g.getUnenclosedSQL(buff.builder());
} }
} }
if (group != null) { if (group != null) {
...@@ -1378,7 +1378,7 @@ public class Select extends Query { ...@@ -1378,7 +1378,7 @@ public class Select extends Query {
buff.resetCount(); buff.resetCount();
for (Expression g : group) { for (Expression g : group) {
buff.appendExceptFirst(", "); buff.appendExceptFirst(", ");
buff.append(StringUtils.unEnclose(g.getSQL())); g.getUnenclosedSQL(buff.builder());
} }
} }
if (having != null) { if (having != null) {
...@@ -1386,12 +1386,12 @@ public class Select extends Query { ...@@ -1386,12 +1386,12 @@ public class Select extends Query {
// in this case the query is not run directly, just getPlanSQL is // in this case the query is not run directly, just getPlanSQL is
// called // called
Expression h = having; Expression h = having;
buff.append("\nHAVING ").append( buff.append("\nHAVING ");
StringUtils.unEnclose(h.getSQL())); h.getUnenclosedSQL(buff.builder());
} else if (havingIndex >= 0) { } else if (havingIndex >= 0) {
Expression h = exprList[havingIndex]; Expression h = exprList[havingIndex];
buff.append("\nHAVING ").append( buff.append("\nHAVING ");
StringUtils.unEnclose(h.getSQL())); h.getUnenclosedSQL(buff.builder());
} }
if (sort != null) { if (sort != null) {
buff.append("\nORDER BY ").append( buff.append("\nORDER BY ").append(
...@@ -1407,8 +1407,8 @@ public class Select extends Query { ...@@ -1407,8 +1407,8 @@ public class Select extends Query {
} }
appendLimitToSQL(buff.builder()); appendLimitToSQL(buff.builder());
if (sampleSizeExpr != null) { if (sampleSizeExpr != null) {
buff.append("\nSAMPLE_SIZE ").append( buff.append("\nSAMPLE_SIZE ");
StringUtils.unEnclose(sampleSizeExpr.getSQL())); sampleSizeExpr.getUnenclosedSQL(buff.builder());
} }
if (isForUpdate) { if (isForUpdate) {
buff.append("\nFOR UPDATE"); buff.append("\nFOR UPDATE");
......
...@@ -28,7 +28,6 @@ import org.h2.table.ColumnResolver; ...@@ -28,7 +28,6 @@ import org.h2.table.ColumnResolver;
import org.h2.table.Table; import org.h2.table.Table;
import org.h2.table.TableFilter; import org.h2.table.TableFilter;
import org.h2.util.ColumnNamer; import org.h2.util.ColumnNamer;
import org.h2.util.StringUtils;
import org.h2.value.Value; import org.h2.value.Value;
import org.h2.value.ValueInt; import org.h2.value.ValueInt;
import org.h2.value.ValueNull; import org.h2.value.ValueNull;
...@@ -429,8 +428,8 @@ public class SelectUnion extends Query { ...@@ -429,8 +428,8 @@ public class SelectUnion extends Query {
} }
appendLimitToSQL(buff); appendLimitToSQL(buff);
if (sampleSizeExpr != null) { if (sampleSizeExpr != null) {
buff.append("\nSAMPLE_SIZE ").append( buff.append("\nSAMPLE_SIZE ");
StringUtils.unEnclose(sampleSizeExpr.getSQL())); sampleSizeExpr.getUnenclosedSQL(buff);
} }
if (isForUpdate) { if (isForUpdate) {
buff.append("\nFOR UPDATE"); buff.append("\nFOR UPDATE");
......
...@@ -28,7 +28,6 @@ import org.h2.table.PlanItem; ...@@ -28,7 +28,6 @@ import org.h2.table.PlanItem;
import org.h2.table.Table; import org.h2.table.Table;
import org.h2.table.TableFilter; import org.h2.table.TableFilter;
import org.h2.util.StatementBuilder; import org.h2.util.StatementBuilder;
import org.h2.util.StringUtils;
import org.h2.util.Utils; import org.h2.util.Utils;
import org.h2.value.Value; import org.h2.value.Value;
import org.h2.value.ValueNull; import org.h2.value.ValueNull;
...@@ -226,11 +225,12 @@ public class Update extends Prepared { ...@@ -226,11 +225,12 @@ public class Update extends Prepared {
e.getSQL(buff.builder()); e.getSQL(buff.builder());
} }
if (condition != null) { if (condition != null) {
buff.append("\nWHERE ").append(StringUtils.unEnclose(condition.getSQL())); buff.append("\nWHERE ");
condition.getUnenclosedSQL(buff.builder());
} }
if (limitExpr != null) { if (limitExpr != null) {
buff.append("\nLIMIT ").append( buff.append("\nLIMIT ");
StringUtils.unEnclose(limitExpr.getSQL())); limitExpr.getUnenclosedSQL(buff.builder());
} }
return buff.toString(); return buff.toString();
} }
......
...@@ -11,7 +11,6 @@ import org.h2.result.ResultInterface; ...@@ -11,7 +11,6 @@ import org.h2.result.ResultInterface;
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.StringUtils;
import org.h2.value.Value; import org.h2.value.Value;
import org.h2.value.ValueArray; import org.h2.value.ValueArray;
...@@ -125,6 +124,25 @@ public abstract class Expression { ...@@ -125,6 +124,25 @@ public abstract class Expression {
*/ */
public abstract StringBuilder getSQL(StringBuilder builder); public abstract StringBuilder getSQL(StringBuilder builder);
/**
* Appends the SQL statement of this expression to the specified builder.
* This may not always be the original SQL statement, specially after
* optimization. Enclosing '(' and ')' are removed.
*
* @param builder
* string builder
* @return the specified string builder
*/
public StringBuilder getUnenclosedSQL(StringBuilder builder) {
int first = builder.length();
int last = getSQL(builder).length() - 1;
if (last > first && builder.charAt(first) == '(' && builder.charAt(last) == ')') {
builder.setLength(last);
builder.deleteCharAt(first);
}
return builder;
}
/** /**
* Update an aggregate value. This method is called at statement execution * Update an aggregate value. This method is called at statement execution
* time. It is usually called once for each row, but if the expression is * time. It is usually called once for each row, but if the expression is
...@@ -281,7 +299,7 @@ public abstract class Expression { ...@@ -281,7 +299,7 @@ public abstract class Expression {
* @return the alias name * @return the alias name
*/ */
public String getAlias() { public String getAlias() {
return StringUtils.unEnclose(getSQL()); return getUnenclosedSQL(new StringBuilder()).toString();
} }
/** /**
......
...@@ -15,7 +15,6 @@ import org.h2.message.DbException; ...@@ -15,7 +15,6 @@ import org.h2.message.DbException;
import org.h2.result.SortOrder; import org.h2.result.SortOrder;
import org.h2.table.ColumnResolver; import org.h2.table.ColumnResolver;
import org.h2.table.TableFilter; import org.h2.table.TableFilter;
import org.h2.util.StringUtils;
import org.h2.value.Value; import org.h2.value.Value;
import org.h2.value.ValueArray; import org.h2.value.ValueArray;
...@@ -220,7 +219,7 @@ public final class Window { ...@@ -220,7 +219,7 @@ public final class Window {
if (i > 0) { if (i > 0) {
builder.append(", "); builder.append(", ");
} }
builder.append(StringUtils.unEnclose(partitionBy.get(i).getSQL())); partitionBy.get(i).getUnenclosedSQL(builder);
} }
} }
appendOrderBy(builder, orderBy); appendOrderBy(builder, orderBy);
......
...@@ -12,8 +12,6 @@ import org.h2.expression.Expression; ...@@ -12,8 +12,6 @@ import org.h2.expression.Expression;
import org.h2.expression.ExpressionColumn; import org.h2.expression.ExpressionColumn;
import org.h2.table.Column; import org.h2.table.Column;
import org.h2.table.TableFilter; import org.h2.table.TableFilter;
import org.h2.util.StatementBuilder;
import org.h2.util.StringUtils;
import org.h2.util.Utils; import org.h2.util.Utils;
import org.h2.value.Value; import org.h2.value.Value;
import org.h2.value.ValueNull; import org.h2.value.ValueNull;
...@@ -118,18 +116,21 @@ public class SortOrder implements Comparator<Value[]> { ...@@ -118,18 +116,21 @@ public class SortOrder implements Comparator<Value[]> {
* @return the SQL snippet * @return the SQL snippet
*/ */
public String getSQL(Expression[] list, int visible) { public String getSQL(Expression[] list, int visible) {
StatementBuilder buff = new StatementBuilder(); StringBuilder builder = new StringBuilder();
int i = 0; int i = 0;
for (int idx : queryColumnIndexes) { for (int idx : queryColumnIndexes) {
buff.appendExceptFirst(", "); if (i > 0) {
builder.append(", ");
}
if (idx < visible) { if (idx < visible) {
buff.append(idx + 1); builder.append(idx + 1);
} else { } else {
buff.append('=').append(StringUtils.unEnclose(list[idx].getSQL())); builder.append('=');
list[idx].getUnenclosedSQL(builder);
} }
typeToString(buff.builder(), sortTypes[i++]); typeToString(builder, sortTypes[i++]);
} }
return buff.toString(); return builder.toString();
} }
/** /**
......
...@@ -785,7 +785,7 @@ public class TableFilter implements ColumnResolver { ...@@ -785,7 +785,7 @@ public class TableFilter implements ColumnResolver {
// otherwise the nesting is unclear // otherwise the nesting is unclear
buff.append("1=1"); buff.append("1=1");
} else { } else {
buff.append(StringUtils.unEnclose(joinCondition.getSQL())); joinCondition.getUnenclosedSQL(buff);
} }
} }
return buff.toString(); return buff.toString();
...@@ -851,7 +851,7 @@ public class TableFilter implements ColumnResolver { ...@@ -851,7 +851,7 @@ public class TableFilter implements ColumnResolver {
// unclear // unclear
buff.append("1=1"); buff.append("1=1");
} else { } else {
buff.append(StringUtils.unEnclose(joinCondition.getSQL())); joinCondition.getUnenclosedSQL(buff);
} }
} }
if (filterCondition != null) { if (filterCondition != null) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论