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

Pass StringBuilder to TableFilter.getPlanSQL()

上级 e619227f
...@@ -136,8 +136,8 @@ public class Delete extends Prepared { ...@@ -136,8 +136,8 @@ public class Delete extends Prepared {
@Override @Override
public String getPlanSQL() { public String getPlanSQL() {
StringBuilder buff = new StringBuilder(); StringBuilder buff = new StringBuilder();
buff.append("DELETE "); buff.append("DELETE FROM ");
buff.append("FROM ").append(targetTableFilter.getPlanSQL(false)); targetTableFilter.getPlanSQL(buff, false);
if (condition != null) { if (condition != null) {
buff.append("\nWHERE "); buff.append("\nWHERE ");
condition.getUnenclosedSQL(buff); condition.getUnenclosedSQL(buff);
......
...@@ -1345,7 +1345,7 @@ public class Select extends Query { ...@@ -1345,7 +1345,7 @@ public class Select extends Query {
int i = 0; int i = 0;
do { do {
buff.appendExceptFirst("\n"); buff.appendExceptFirst("\n");
buff.append(filter.getPlanSQL(i++ > 0)); filter.getPlanSQL(buff.builder(), i++ > 0);
filter = filter.getJoin(); filter = filter.getJoin();
} while (filter != null); } while (filter != null);
} else { } else {
...@@ -1354,7 +1354,7 @@ public class Select extends Query { ...@@ -1354,7 +1354,7 @@ public class Select extends Query {
for (TableFilter f : topFilters) { for (TableFilter f : topFilters) {
do { do {
buff.appendExceptFirst("\n"); buff.appendExceptFirst("\n");
buff.append(f.getPlanSQL(i++ > 0)); f.getPlanSQL(buff.builder(), i++ > 0);
f = f.getJoin(); f = f.getJoin();
} while (f != null); } while (f != null);
} }
......
...@@ -27,7 +27,6 @@ import org.h2.table.Column; ...@@ -27,7 +27,6 @@ import org.h2.table.Column;
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.StatementBuilder;
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;
...@@ -216,23 +215,25 @@ public class Update extends Prepared { ...@@ -216,23 +215,25 @@ public class Update extends Prepared {
@Override @Override
public String getPlanSQL() { public String getPlanSQL() {
StatementBuilder buff = new StatementBuilder("UPDATE "); StringBuilder builder = new StringBuilder("UPDATE ");
buff.append(targetTableFilter.getPlanSQL(false)).append("\nSET\n "); targetTableFilter.getPlanSQL(builder, false).append("\nSET\n ");
for (Column c : columns) { for (int i = 0, size = columns.size(); i < size; i++) {
Expression e = expressionMap.get(c); if (i > 0) {
buff.appendExceptFirst(",\n "); builder.append(",\n ");
buff.append(c.getName()).append(" = "); }
e.getSQL(buff.builder()); Column c = columns.get(i);
builder.append(c.getName()).append(" = ");
expressionMap.get(c).getSQL(builder);
} }
if (condition != null) { if (condition != null) {
buff.append("\nWHERE "); builder.append("\nWHERE ");
condition.getUnenclosedSQL(buff.builder()); condition.getUnenclosedSQL(builder);
} }
if (limitExpr != null) { if (limitExpr != null) {
buff.append("\nLIMIT "); builder.append("\nLIMIT ");
limitExpr.getUnenclosedSQL(buff.builder()); limitExpr.getUnenclosedSQL(builder);
} }
return buff.toString(); return builder.toString();
} }
@Override @Override
......
...@@ -30,7 +30,6 @@ import org.h2.table.ColumnResolver; ...@@ -30,7 +30,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.StatementBuilder; import org.h2.util.StatementBuilder;
import org.h2.util.StringUtils;
import org.h2.util.ValueHashMap; import org.h2.util.ValueHashMap;
import org.h2.value.CompareMode; import org.h2.value.CompareMode;
import org.h2.value.DataType; import org.h2.value.DataType;
......
...@@ -747,75 +747,75 @@ public class TableFilter implements ColumnResolver { ...@@ -747,75 +747,75 @@ public class TableFilter implements ColumnResolver {
} }
/** /**
* Get the query execution plan text to use for this table filter. * Get the query execution plan text to use for this table filter and append
* it to the specified builder.
* *
* @param builder string builder to append to
* @param isJoin if this is a joined table * @param isJoin if this is a joined table
* @return the SQL statement snippet * @return the specified builder
*/ */
public String getPlanSQL(boolean isJoin) { public StringBuilder getPlanSQL(StringBuilder builder, boolean isJoin) {
StringBuilder buff = new StringBuilder();
if (isJoin) { if (isJoin) {
if (joinOuter) { if (joinOuter) {
buff.append("LEFT OUTER JOIN "); builder.append("LEFT OUTER JOIN ");
} else { } else {
buff.append("INNER JOIN "); builder.append("INNER JOIN ");
} }
} }
if (nestedJoin != null) { if (nestedJoin != null) {
StringBuilder buffNested = new StringBuilder(); StringBuilder buffNested = new StringBuilder();
TableFilter n = nestedJoin; TableFilter n = nestedJoin;
do { do {
buffNested.append(n.getPlanSQL(n != nestedJoin)); n.getPlanSQL(buffNested, n != nestedJoin).append('\n');
buffNested.append('\n');
n = n.getJoin(); n = n.getJoin();
} while (n != null); } while (n != null);
String nested = buffNested.toString(); String nested = buffNested.toString();
boolean enclose = !nested.startsWith("("); boolean enclose = !nested.startsWith("(");
if (enclose) { if (enclose) {
buff.append("(\n"); builder.append("(\n");
} }
StringUtils.indent(buff, nested, 4, false); StringUtils.indent(builder, nested, 4, false);
if (enclose) { if (enclose) {
buff.append(')'); builder.append(')');
} }
if (isJoin) { if (isJoin) {
buff.append(" ON "); builder.append(" ON ");
if (joinCondition == null) { if (joinCondition == null) {
// need to have a ON expression, // need to have a ON expression,
// otherwise the nesting is unclear // otherwise the nesting is unclear
buff.append("1=1"); builder.append("1=1");
} else { } else {
joinCondition.getUnenclosedSQL(buff); joinCondition.getUnenclosedSQL(builder);
} }
} }
return buff.toString(); return builder;
} }
if (table.isView() && ((TableView) table).isRecursive()) { if (table.isView() && ((TableView) table).isRecursive()) {
buff.append(table.getSchema().getSQL()).append('.').append(Parser.quoteIdentifier(table.getName())); builder.append(table.getSchema().getSQL()).append('.').append(Parser.quoteIdentifier(table.getName()));
} else { } else {
buff.append(table.getSQL()); builder.append(table.getSQL());
} }
if (table.isView() && ((TableView) table).isInvalid()) { if (table.isView() && ((TableView) table).isInvalid()) {
throw DbException.get(ErrorCode.VIEW_IS_INVALID_2, table.getName(), "not compiled"); throw DbException.get(ErrorCode.VIEW_IS_INVALID_2, table.getName(), "not compiled");
} }
if (alias != null) { if (alias != null) {
buff.append(' ').append(Parser.quoteIdentifier(alias)); builder.append(' ').append(Parser.quoteIdentifier(alias));
} }
if (indexHints != null) { if (indexHints != null) {
buff.append(" USE INDEX ("); builder.append(" USE INDEX (");
boolean first = true; boolean first = true;
for (String index : indexHints.getAllowedIndexes()) { for (String index : indexHints.getAllowedIndexes()) {
if (!first) { if (!first) {
buff.append(", "); builder.append(", ");
} else { } else {
first = false; first = false;
} }
buff.append(Parser.quoteIdentifier(index)); builder.append(Parser.quoteIdentifier(index));
} }
buff.append(")"); builder.append(")");
} }
if (index != null) { if (index != null) {
buff.append('\n'); builder.append('\n');
StatementBuilder planBuff = new StatementBuilder(); StatementBuilder planBuff = new StatementBuilder();
if (joinBatch != null) { if (joinBatch != null) {
IndexLookupBatch lookupBatch = joinBatch.getLookupBatch(joinFilterId); IndexLookupBatch lookupBatch = joinBatch.getLookupBatch(joinFilterId);
...@@ -842,28 +842,28 @@ public class TableFilter implements ColumnResolver { ...@@ -842,28 +842,28 @@ public class TableFilter implements ColumnResolver {
if (plan.indexOf('\n') >= 0) { if (plan.indexOf('\n') >= 0) {
plan += "\n"; plan += "\n";
} }
StringUtils.indent(buff, "/* " + plan + " */", 4, false); StringUtils.indent(builder, "/* " + plan + " */", 4, false);
} }
if (isJoin) { if (isJoin) {
buff.append("\n ON "); builder.append("\n ON ");
if (joinCondition == null) { if (joinCondition == null) {
// need to have a ON expression, otherwise the nesting is // need to have a ON expression, otherwise the nesting is
// unclear // unclear
buff.append("1=1"); builder.append("1=1");
} else { } else {
joinCondition.getUnenclosedSQL(buff); joinCondition.getUnenclosedSQL(builder);
} }
} }
if (filterCondition != null) { if (filterCondition != null) {
buff.append('\n'); builder.append('\n');
String condition = StringUtils.unEnclose(filterCondition.getSQL()); String condition = StringUtils.unEnclose(filterCondition.getSQL());
condition = "/* WHERE " + StringUtils.quoteRemarkSQL(condition) + "\n*/"; condition = "/* WHERE " + StringUtils.quoteRemarkSQL(condition) + "\n*/";
StringUtils.indent(buff, condition, 4, false); StringUtils.indent(builder, condition, 4, false);
} }
if (scanCount > 0) { if (scanCount > 0) {
buff.append("\n /* scanCount: ").append(scanCount).append(" */"); builder.append("\n /* scanCount: ").append(scanCount).append(" */");
} }
return buff.toString(); return builder;
} }
/** /**
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论