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

Add Expression.getUnenclosedSQL(StringBuilder)

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