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

Pass StringBuilder to StringUtils.indent()

上级 140e8a8d
...@@ -1336,7 +1336,7 @@ public class Select extends Query { ...@@ -1336,7 +1336,7 @@ public class Select extends Query {
for (int i = 0; i < visibleColumnCount; i++) { for (int i = 0; i < visibleColumnCount; i++) {
buff.appendExceptFirst(","); buff.appendExceptFirst(",");
buff.append('\n'); buff.append('\n');
buff.append(StringUtils.indent(exprList[i].getSQL(), 4, false)); StringUtils.indent(buff.builder(), exprList[i].getSQL(), 4, false);
} }
buff.append("\nFROM "); buff.append("\nFROM ");
TableFilter filter = topTableFilter; TableFilter filter = topTableFilter;
......
...@@ -43,8 +43,7 @@ public class ConditionExists extends Condition { ...@@ -43,8 +43,7 @@ public class ConditionExists extends Condition {
@Override @Override
public StringBuilder getSQL(StringBuilder builder) { public StringBuilder getSQL(StringBuilder builder) {
builder.append("EXISTS(\n"); builder.append("EXISTS(\n");
builder.append(StringUtils.indent(query.getPlanSQL(), 4, false)); return StringUtils.indent(builder, query.getPlanSQL(), 4, false).append(')');
return builder.append(')');
} }
@Override @Override
......
...@@ -143,7 +143,8 @@ public class ConditionInSelect extends Condition { ...@@ -143,7 +143,8 @@ public class ConditionInSelect extends Condition {
append(" ANY"); append(" ANY");
} }
} }
return builder.append("(\n").append(StringUtils.indent(query.getPlanSQL(), 4, false)).append("))"); builder.append("(\n");
return StringUtils.indent(builder, query.getPlanSQL(), 4, false).append("))");
} }
@Override @Override
......
...@@ -774,7 +774,7 @@ public class TableFilter implements ColumnResolver { ...@@ -774,7 +774,7 @@ public class TableFilter implements ColumnResolver {
if (enclose) { if (enclose) {
buff.append("(\n"); buff.append("(\n");
} }
buff.append(StringUtils.indent(nested, 4, false)); StringUtils.indent(buff, nested, 4, false);
if (enclose) { if (enclose) {
buff.append(')'); buff.append(')');
} }
...@@ -842,7 +842,7 @@ public class TableFilter implements ColumnResolver { ...@@ -842,7 +842,7 @@ public class TableFilter implements ColumnResolver {
if (plan.indexOf('\n') >= 0) { if (plan.indexOf('\n') >= 0) {
plan += "\n"; plan += "\n";
} }
buff.append(StringUtils.indent("/* " + plan + " */", 4, false)); StringUtils.indent(buff, "/* " + plan + " */", 4, false);
} }
if (isJoin) { if (isJoin) {
buff.append("\n ON "); buff.append("\n ON ");
...@@ -858,7 +858,7 @@ public class TableFilter implements ColumnResolver { ...@@ -858,7 +858,7 @@ public class TableFilter implements ColumnResolver {
buff.append('\n'); buff.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*/";
buff.append(StringUtils.indent(condition, 4, false)); StringUtils.indent(buff, condition, 4, false);
} }
if (scanCount > 0) { if (scanCount > 0) {
buff.append("\n /* scanCount: ").append(scanCount).append(" */"); buff.append("\n /* scanCount: ").append(scanCount).append(" */");
......
...@@ -468,7 +468,8 @@ public class TableView extends Table { ...@@ -468,7 +468,8 @@ public class TableView extends Table {
@Override @Override
public String getSQL() { public String getSQL() {
if (isTemporary() && querySQL != null) { if (isTemporary() && querySQL != null) {
return "(\n" + StringUtils.indent(querySQL) + ")"; StringBuilder builder = new StringBuilder(querySQL.length() + 16).append("(\n");
return StringUtils.indent(builder, querySQL, 4, true).append(')').toString();
} }
return super.getSQL(); return super.getSQL();
} }
......
...@@ -560,49 +560,49 @@ public class StringUtils { ...@@ -560,49 +560,49 @@ public class StringUtils {
*/ */
public static String xmlNode(String name, String attributes, public static String xmlNode(String name, String attributes,
String content, boolean indent) { String content, boolean indent) {
String start = attributes == null ? name : name + attributes; StringBuilder builder = new StringBuilder();
builder.append('<').append(name);
if (attributes != null) {
builder.append(attributes);
}
if (content == null) { if (content == null) {
return "<" + start + "/>\n"; builder.append("/>\n");
return builder.toString();
} }
builder.append('>');
if (indent && content.indexOf('\n') >= 0) { if (indent && content.indexOf('\n') >= 0) {
content = "\n" + indent(content); builder.append('\n');
indent(builder, content, 4, true);
} else {
builder.append(content);
} }
return "<" + start + ">" + content + "</" + name + ">\n"; builder.append("</").append(name).append(">\n");
} return builder.toString();
/**
* Indents a string with 4 spaces.
*
* @param s the string
* @return the indented string
*/
public static String indent(String s) {
return indent(s, 4, true);
} }
/** /**
* Indents a string with spaces. * Indents a string with spaces and appends it to a specified builder.
* *
* @param builder string builder to append to
* @param s the string * @param s the string
* @param spaces the number of spaces * @param spaces the number of spaces
* @param newline append a newline if there is none * @param newline append a newline if there is none
* @return the indented string * @return the specified string builder
*/ */
public static String indent(String s, int spaces, boolean newline) { public static StringBuilder indent(StringBuilder builder, String s, int spaces, boolean newline) {
StringBuilder buff = new StringBuilder(s.length() + spaces); for (int i = 0, length = s.length(); i < length;) {
for (int i = 0; i < s.length();) {
for (int j = 0; j < spaces; j++) { for (int j = 0; j < spaces; j++) {
buff.append(' '); builder.append(' ');
} }
int n = s.indexOf('\n', i); int n = s.indexOf('\n', i);
n = n < 0 ? s.length() : n + 1; n = n < 0 ? length : n + 1;
buff.append(s, i, n); builder.append(s, i, n);
i = n; i = n;
} }
if (newline && !s.endsWith("\n")) { if (newline && !s.endsWith("\n")) {
buff.append('\n'); builder.append('\n');
} }
return buff.toString(); return builder;
} }
/** /**
...@@ -625,7 +625,8 @@ public class StringUtils { ...@@ -625,7 +625,8 @@ public class StringUtils {
// must have a space at the beginning and at the end, // must have a space at the beginning and at the end,
// otherwise the data must not contain '-' as the first/last character // otherwise the data must not contain '-' as the first/last character
if (data.indexOf('\n') >= 0) { if (data.indexOf('\n') >= 0) {
return "<!--\n" + indent(data) + "-->\n"; StringBuilder builder = new StringBuilder(data.length() + 18).append("<!--\n");
return indent(builder, data, 4, true).append("-->\n").toString();
} }
return "<!-- " + data + " -->\n"; return "<!-- " + data + " -->\n";
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论