提交 2c0a1fd5 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Pass StringBuilder to SelectOrderBy.getSQL()

上级 10ce4998
...@@ -48,7 +48,6 @@ import org.h2.table.TableFilter.TableFilterVisitor; ...@@ -48,7 +48,6 @@ import org.h2.table.TableFilter.TableFilterVisitor;
import org.h2.table.TableType; import org.h2.table.TableType;
import org.h2.table.TableView; import org.h2.table.TableView;
import org.h2.util.ColumnNamer; import org.h2.util.ColumnNamer;
import org.h2.util.StatementBuilder;
import org.h2.util.StringUtils; 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;
...@@ -1463,7 +1462,7 @@ public class Select extends Query { ...@@ -1463,7 +1462,7 @@ public class Select extends Query {
// indexes may be incorrect: ? may be in fact ?2 for a subquery // indexes may be incorrect: ? may be in fact ?2 for a subquery
// but indexes may be set manually as well // but indexes may be set manually as well
Expression[] exprList = expressions.toArray(new Expression[0]); Expression[] exprList = expressions.toArray(new Expression[0]);
StatementBuilder buff = new StatementBuilder(); StringBuilder builder = new StringBuilder();
for (TableFilter f : topFilters) { for (TableFilter f : topFilters) {
Table t = f.getTable(); Table t = f.getTable();
TableView tableView = t.isView() ? (TableView) t : null; TableView tableView = t.isView() ? (TableView) t : null;
...@@ -1474,121 +1473,124 @@ public class Select extends Query { ...@@ -1474,121 +1473,124 @@ public class Select extends Query {
// since using a with statement will re-create the common table expression // since using a with statement will re-create the common table expression
// views. // views.
} else { } else {
buff.append("WITH RECURSIVE "); builder.append("WITH RECURSIVE ");
t.getSchema().getSQL(buff.builder()).append('.'); t.getSchema().getSQL(builder).append('.');
Parser.quoteIdentifier(buff.builder(), t.getName()) Parser.quoteIdentifier(builder, t.getName()).append('(');
.append('('); Column.writeColumns(builder, t.getColumns());
Column.writeColumns(buff.builder(), t.getColumns()); builder.append(") AS ");
buff.append(") AS "); t.getSQL(builder).append('\n');
t.getSQL(buff.builder()).append('\n');
} }
} }
} }
buff.append("SELECT"); builder.append("SELECT");
if (isAnyDistinct()) { if (isAnyDistinct()) {
buff.append(" DISTINCT"); builder.append(" DISTINCT");
if (distinctExpressions != null) { if (distinctExpressions != null) {
buff.append(" ON("); builder.append(" ON(");
Expression.writeExpressions(buff.builder(), distinctExpressions); Expression.writeExpressions(builder, distinctExpressions);
buff.append(')'); builder.append(')');
} }
} }
for (int i = 0; i < visibleColumnCount; i++) { for (int i = 0; i < visibleColumnCount; i++) {
buff.appendExceptFirst(","); if (i > 0) {
buff.append('\n'); builder.append(", ");
StringUtils.indent(buff.builder(), exprList[i].getSQL(), 4, false);
} }
buff.append("\nFROM "); builder.append('\n');
StringUtils.indent(builder, exprList[i].getSQL(), 4, false);
}
builder.append("\nFROM ");
TableFilter filter = topTableFilter; TableFilter filter = topTableFilter;
if (filter != null) { if (filter != null) {
buff.resetCount();
int i = 0; int i = 0;
do { do {
buff.appendExceptFirst("\n"); if (i > 0) {
filter.getPlanSQL(buff.builder(), i++ > 0); builder.append('\n');
}
filter.getPlanSQL(builder, i++ > 0);
filter = filter.getJoin(); filter = filter.getJoin();
} while (filter != null); } while (filter != null);
} else { } else {
buff.resetCount();
int i = 0; int i = 0;
for (TableFilter f : topFilters) { for (TableFilter f : topFilters) {
do { do {
buff.appendExceptFirst("\n"); if (i > 0) {
f.getPlanSQL(buff.builder(), i++ > 0); builder.append('\n');
}
f.getPlanSQL(builder, i++ > 0);
f = f.getJoin(); f = f.getJoin();
} while (f != null); } while (f != null);
} }
} }
if (condition != null) { if (condition != null) {
buff.append("\nWHERE "); builder.append("\nWHERE ");
condition.getUnenclosedSQL(buff.builder()); condition.getUnenclosedSQL(builder);
} }
if (groupIndex != null) { if (groupIndex != null) {
buff.append("\nGROUP BY "); builder.append("\nGROUP BY ");
buff.resetCount(); for (int i = 0, l = groupIndex.length; i < l; i++) {
for (int gi : groupIndex) { if (i > 0) {
Expression g = exprList[gi]; builder.append(", ");
g = g.getNonAliasExpression();
buff.appendExceptFirst(", ");
g.getUnenclosedSQL(buff.builder());
} }
exprList[groupIndex[i]].getNonAliasExpression().getUnenclosedSQL(builder);
} }
if (group != null) { } else if (group != null) {
buff.append("\nGROUP BY "); builder.append("\nGROUP BY ");
buff.resetCount(); for (int i = 0, l = group.size(); i < l; i++) {
for (Expression g : group) { if (i > 0) {
buff.appendExceptFirst(", "); builder.append(", ");
g.getUnenclosedSQL(buff.builder()); }
group.get(i).getUnenclosedSQL(builder);
} }
} }
getFilterSQL(buff, "\nHAVING ", exprList, having, havingIndex); getFilterSQL(builder, "\nHAVING ", exprList, having, havingIndex);
getFilterSQL(buff, "\nQUALIFY ", exprList, qualify, qualifyIndex); getFilterSQL(builder, "\nQUALIFY ", exprList, qualify, qualifyIndex);
if (sort != null) { if (sort != null) {
buff.append("\nORDER BY ").append( builder.append("\nORDER BY ").append(
sort.getSQL(exprList, visibleColumnCount)); sort.getSQL(exprList, visibleColumnCount));
} }
if (orderList != null) { if (orderList != null) {
buff.append("\nORDER BY "); builder.append("\nORDER BY ");
buff.resetCount(); for (int i = 0, l = orderList.size(); i < l; i++) {
for (SelectOrderBy o : orderList) { if (i > 0) {
buff.appendExceptFirst(", "); builder.append(", ");
buff.append(StringUtils.unEnclose(o.getSQL())); }
orderList.get(i).getSQL(builder);
} }
} }
appendLimitToSQL(buff.builder()); appendLimitToSQL(builder);
if (sampleSizeExpr != null) { if (sampleSizeExpr != null) {
buff.append("\nSAMPLE_SIZE "); builder.append("\nSAMPLE_SIZE ");
sampleSizeExpr.getUnenclosedSQL(buff.builder()); sampleSizeExpr.getUnenclosedSQL(builder);
} }
if (isForUpdate) { if (isForUpdate) {
buff.append("\nFOR UPDATE"); builder.append("\nFOR UPDATE");
} }
if (isQuickAggregateQuery) { if (isQuickAggregateQuery) {
buff.append("\n/* direct lookup */"); builder.append("\n/* direct lookup */");
} }
if (isDistinctQuery) { if (isDistinctQuery) {
buff.append("\n/* distinct */"); builder.append("\n/* distinct */");
} }
if (sortUsingIndex) { if (sortUsingIndex) {
buff.append("\n/* index sorted */"); builder.append("\n/* index sorted */");
} }
if (isGroupQuery) { if (isGroupQuery) {
if (isGroupSortedQuery) { if (isGroupSortedQuery) {
buff.append("\n/* group sorted */"); builder.append("\n/* group sorted */");
} }
} }
// buff.append("\n/* cost: " + cost + " */"); // buff.append("\n/* cost: " + cost + " */");
return buff.toString(); return builder.toString();
} }
private static void getFilterSQL(StatementBuilder buff, String sql, Expression[] exprList, Expression condition, private static void getFilterSQL(StringBuilder builder, String sql, Expression[] exprList, Expression condition,
int conditionIndex) { int conditionIndex) {
if (condition != null) { if (condition != null) {
buff.append(sql); builder.append(sql);
condition.getUnenclosedSQL(buff.builder()); condition.getUnenclosedSQL(builder);
} else if (conditionIndex >= 0) { } else if (conditionIndex >= 0) {
buff.append(sql); builder.append(sql);
exprList[conditionIndex].getUnenclosedSQL(buff.builder()); exprList[conditionIndex].getUnenclosedSQL(builder);
} }
} }
......
...@@ -30,16 +30,14 @@ public class SelectOrderBy { ...@@ -30,16 +30,14 @@ public class SelectOrderBy {
*/ */
public int sortType; public int sortType;
public String getSQL() { public void getSQL(StringBuilder builder) {
StringBuilder buff = new StringBuilder();
if (expression != null) { if (expression != null) {
buff.append('='); builder.append('=');
expression.getSQL(buff); expression.getSQL(builder);
} else { } else {
columnIndexExpr.getSQL(buff); columnIndexExpr.getUnenclosedSQL(builder);
} }
SortOrder.typeToString(buff, sortType); SortOrder.typeToString(builder, sortType);
return buff.toString();
} }
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论