Skip to content

Commit

Permalink
fixed #320
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Aug 9, 2017
1 parent be88226 commit 60d396a
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 4 deletions.
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
1. [ISSUE #308](https://github.com/dangdangdotcom/sharding-jdbc/issues/308) 数据库原生的自增GeneratedKey的返回无效
1. [ISSUE #309](https://github.com/dangdangdotcom/sharding-jdbc/issues/310) 子查询中的orderby和groupby不列入解析上下文
1. [ISSUE #313](https://github.com/dangdangdotcom/sharding-jdbc/issues/313) 支持<>操作符
1. [ISSUE #320](https://github.com/dangdangdotcom/sharding-jdbc/issues/320) GROUP BY + LIMIT的SQL改写错误

## 1.5.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ protected void parseGroupBy() {
if (getSqlParser().skipIfEqual(DefaultKeyword.HAVING)) {
throw new UnsupportedOperationException("Cannot support Having");
}
getSelectStatement().setGroupByLastPosition(getSqlParser().getLexer().getCurrentToken().getEndPosition());
getSelectStatement().setGroupByLastPosition(getSqlParser().getLexer().getCurrentToken().getEndPosition() - getSqlParser().getLexer().getCurrentToken().getLiterals().length());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ protected void parseGroupBy() {
if (sqlParser.skipIfEqual(DefaultKeyword.HAVING)) {
throw new UnsupportedOperationException("Cannot support Having");
}
selectStatement.setGroupByLastPosition(sqlParser.getLexer().getCurrentToken().getEndPosition());
selectStatement.setGroupByLastPosition(sqlParser.getLexer().getCurrentToken().getEndPosition() - getSqlParser().getLexer().getCurrentToken().getLiterals().length());
} else if (sqlParser.skipIfEqual(DefaultKeyword.HAVING)) {
throw new UnsupportedOperationException("Cannot support Having");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public SQLBuilder rewrite(final boolean isRewriteLimit) {
} else if (each instanceof OffsetToken) {
appendLimitOffsetToken(result, (OffsetToken) each, count, sqlTokens, isRewriteLimit);
} else if (each instanceof OrderByToken) {
appendOrderByToken(result);
appendOrderByToken(result, count, sqlTokens);
}
count++;
}
Expand Down Expand Up @@ -147,7 +147,7 @@ private void appendLimitOffsetToken(final SQLBuilder sqlBuilder, final OffsetTok
sqlBuilder.appendLiterals(originalSQL.substring(beginPosition, endPosition));
}

private void appendOrderByToken(final SQLBuilder sqlBuilder) {
private void appendOrderByToken(final SQLBuilder sqlBuilder, final int count, final List<SQLToken> sqlTokens) {
SelectStatement selectStatement = (SelectStatement) sqlStatement;
StringBuilder orderByLiterals = new StringBuilder(" ORDER BY ");
int i = 0;
Expand All @@ -161,6 +161,9 @@ private void appendOrderByToken(final SQLBuilder sqlBuilder) {
}
orderByLiterals.append(" ");
sqlBuilder.appendLiterals(orderByLiterals.toString());
int beginPosition = ((SelectStatement) sqlStatement).getGroupByLastPosition();
int endPosition = sqlTokens.size() - 1 == count ? originalSQL.length() : sqlTokens.get(count + 1).getBeginPosition();
sqlBuilder.appendLiterals(originalSQL.substring(beginPosition, endPosition));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,10 @@
<data parameter="1,9,1000,1909"/>
</sharding-rule>
</sql>
<sql id="assertSelectWithGroupByAndLimit">
<sharding-rule>
<data parameter="1" expected="select_group_by/SelectLimit.xml" />
<data parameter="0" />
</sharding-rule>
</sql>
</sqls>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<dataset>
<t_order user_id="10" />
</dataset>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<dataset>
<t_order user_id="10" />
</dataset>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<dataset>
<t_order user_id="10" />
</dataset>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<dataset>
<t_order user_id="10" />
</dataset>
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
<sql id="assertSelectOrderByDescWithGroupBy" value="SELECT SUM(order_id) AS orders_sum, user_id FROM t_order GROUP BY user_id ORDER BY orders_sum DESC" />
<sql id="assertSelectCountWithoutGroupedColumn" value="SELECT count(*) as items_count FROM t_order o JOIN t_order_item i ON o.user_id = i.user_id AND o.order_id = i.order_id WHERE o.user_id IN (%s, %s) AND o.order_id BETWEEN %s AND %s GROUP BY o.user_id" />
<sql id="assertSelectCountWithGroupByBindingTable" value="SELECT count(*) as items_count, o.user_id FROM t_order o JOIN t_order_item i ON o.user_id = i.user_id AND o.order_id = i.order_id WHERE o.user_id IN (%s, %s) AND o.order_id BETWEEN %s AND %s GROUP BY o.user_id" />
<sql id="assertSelectWithGroupByAndLimit" value="SELECT user_id FROM t_order GROUP BY user_id LIMIT %s" type="H2,MySQL,PostgreSQL" />
</sqls>

0 comments on commit 60d396a

Please sign in to comment.