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

Use shared code for sort types parsing and formatting

上级 3321a9f9
......@@ -882,24 +882,30 @@ public class Parser {
do {
IndexColumn column = new IndexColumn();
column.columnName = readColumnIdentifier();
column.sortType = parseSortType();
columns.add(column);
if (readIf("ASC")) {
// ignore
} else if (readIf("DESC")) {
column.sortType = SortOrder.DESCENDING;
}
if (readIf("NULLS")) {
if (readIf("FIRST")) {
column.sortType |= SortOrder.NULLS_FIRST;
} else {
read("LAST");
column.sortType |= SortOrder.NULLS_LAST;
}
}
} while (readIfMore(true));
return columns.toArray(new IndexColumn[0]);
}
private int parseSortType() {
int sortType = 0;
if (readIf("ASC")) {
// ignore
} else if (readIf("DESC")) {
sortType = SortOrder.DESCENDING;
}
if (readIf("NULLS")) {
if (readIf("FIRST")) {
sortType |= SortOrder.NULLS_FIRST;
} else {
read("LAST");
sortType |= SortOrder.NULLS_LAST;
}
}
return sortType;
}
private String[] parseColumnList() {
ArrayList<String> columns = Utils.newSmallArrayList();
do {
......@@ -2029,19 +2035,7 @@ public class Parser {
} else {
order.expression = expr;
}
if (readIf("DESC")) {
order.descending = true;
} else {
readIf("ASC");
}
if (readIf("NULLS")) {
if (readIf("FIRST")) {
order.nullsFirst = true;
} else {
read("LAST");
order.nullsLast = true;
}
}
order.sortType = parseSortType();
orderList.add(order);
} while (readIf(","));
command.setOrder(orderList);
......@@ -2701,11 +2695,7 @@ public class Parser {
do {
SelectOrderBy order = new SelectOrderBy();
order.expression = readExpression();
if (readIf("DESC")) {
order.descending = true;
} else {
readIf("ASC");
}
order.sortType = parseSortType();
orderList.add(order);
} while (readIf(","));
return orderList;
......
......@@ -524,15 +524,14 @@ public abstract class Query extends Prepared {
}
}
index[i] = idx;
boolean desc = o.descending;
int type = o.sortType;
if (reverse) {
desc = !desc;
}
int type = desc ? SortOrder.DESCENDING : SortOrder.ASCENDING;
if (o.nullsFirst) {
type += SortOrder.NULLS_FIRST;
} else if (o.nullsLast) {
type += SortOrder.NULLS_LAST;
// TODO NULLS FIRST / LAST should be inverted too?
if ((type & SortOrder.DESCENDING) != 0) {
type &= ~SortOrder.DESCENDING;
} else {
type |= SortOrder.DESCENDING;
}
}
sortType[i] = type;
}
......
......@@ -6,6 +6,7 @@
package org.h2.command.dml;
import org.h2.expression.Expression;
import org.h2.result.SortOrder;
/**
* Describes one element of the ORDER BY clause of a query.
......@@ -25,19 +26,9 @@ public class SelectOrderBy {
public Expression columnIndexExpr;
/**
* If the column should be sorted descending.
* Sort type for this column.
*/
public boolean descending;
/**
* If NULL should be appear first.
*/
public boolean nullsFirst;
/**
* If NULL should be appear at the end.
*/
public boolean nullsLast;
public int sortType;
public String getSQL() {
StringBuilder buff = new StringBuilder();
......@@ -46,14 +37,7 @@ public class SelectOrderBy {
} else {
buff.append(columnIndexExpr.getSQL());
}
if (descending) {
buff.append(" DESC");
}
if (nullsFirst) {
buff.append(" NULLS FIRST");
} else if (nullsLast) {
buff.append(" NULLS LAST");
}
SortOrder.typeToString(buff, sortType);
return buff.toString();
}
......
......@@ -252,8 +252,7 @@ public class Aggregate extends Expression {
for (int i = 0; i < size; i++) {
SelectOrderBy o = orderByList.get(i);
index[i] = i + 1;
int order = o.descending ? SortOrder.DESCENDING : SortOrder.ASCENDING;
sortType[i] = order;
sortType[i] = o.sortType;
}
return new SortOrder(session.getDatabase(), index, sortType, null);
}
......@@ -590,9 +589,7 @@ public class Aggregate extends Expression {
for (SelectOrderBy o : orderByList) {
buff.appendExceptFirst(", ");
buff.append(o.expression.getSQL());
if (o.descending) {
buff.append(" DESC");
}
SortOrder.typeToString(buff.builder(), o.sortType);
}
}
if (groupConcatSeparator != null) {
......@@ -616,9 +613,7 @@ public class Aggregate extends Expression {
for (SelectOrderBy o : orderByList) {
buff.appendExceptFirst(", ");
buff.append(o.expression.getSQL());
if (o.descending) {
buff.append(" DESC");
}
SortOrder.typeToString(buff.builder(), o.sortType);
}
}
buff.append(')');
......
......@@ -127,19 +127,27 @@ public class SortOrder implements Comparator<Value[]> {
} else {
buff.append('=').append(StringUtils.unEnclose(list[idx].getSQL()));
}
int type = sortTypes[i++];
if ((type & DESCENDING) != 0) {
buff.append(" DESC");
}
if ((type & NULLS_FIRST) != 0) {
buff.append(" NULLS FIRST");
} else if ((type & NULLS_LAST) != 0) {
buff.append(" NULLS LAST");
}
typeToString(buff.builder(), sortTypes[i++]);
}
return buff.toString();
}
/**
* Appends type information (DESC, NULLS FIRST, NULLS LAST) to the specified statement builder.
* @param builder statement builder
* @param type sort type
*/
public static void typeToString(StringBuilder builder, int type) {
if ((type & DESCENDING) != 0) {
builder.append(" DESC");
}
if ((type & NULLS_FIRST) != 0) {
builder.append(" NULLS FIRST");
} else if ((type & NULLS_LAST) != 0) {
builder.append(" NULLS LAST");
}
}
/**
* Compare two expressions where one of them is NULL.
*
......
......@@ -36,14 +36,7 @@ public class IndexColumn {
*/
public String getSQL() {
StringBuilder buff = new StringBuilder(column.getSQL());
if ((sortType & SortOrder.DESCENDING) != 0) {
buff.append(" DESC");
}
if ((sortType & SortOrder.NULLS_FIRST) != 0) {
buff.append(" NULLS FIRST");
} else if ((sortType & SortOrder.NULLS_LAST) != 0) {
buff.append(" NULLS LAST");
}
SortOrder.typeToString(buff, sortType);
return buff.toString();
}
......
......@@ -127,4 +127,12 @@ public class StatementBuilder {
return builder.length();
}
/**
* Return underlying builder.
* @return underlying builder
*/
public StringBuilder builder() {
return builder;
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论