mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
@@ -403,6 +403,7 @@ public class PagerUtils {
|
||||
SQLSelectItem countItem = createCountItem(dbType);
|
||||
|
||||
SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) query;
|
||||
List<SQLSelectItem> selectList = queryBlock.getSelectList();
|
||||
|
||||
if (queryBlock.getGroupBy() != null
|
||||
&& queryBlock.getGroupBy().getItems().size() > 0) {
|
||||
@@ -410,15 +411,18 @@ public class PagerUtils {
|
||||
}
|
||||
|
||||
int option = queryBlock.getDistionOption();
|
||||
if (option == SQLSetQuantifier.DISTINCT && queryBlock.getSelectList().size() == 1) {
|
||||
SQLSelectItem firstItem = queryBlock.getSelectList().get(0);
|
||||
SQLAggregateExpr exp = new SQLAggregateExpr("COUNT", SQLAggregateOption.DISTINCT);
|
||||
exp.addArgument(firstItem.getExpr());
|
||||
firstItem.setExpr(exp);
|
||||
if (option == SQLSetQuantifier.DISTINCT
|
||||
&& selectList.size() >= 1) {
|
||||
SQLAggregateExpr countExpr = new SQLAggregateExpr("COUNT", SQLAggregateOption.DISTINCT);
|
||||
for (int i = 0; i < selectList.size(); ++i) {
|
||||
countExpr.addArgument(selectList.get(i).getExpr());
|
||||
}
|
||||
selectList.clear();
|
||||
queryBlock.setDistionOption(0);
|
||||
queryBlock.addSelectItem(countExpr);
|
||||
} else {
|
||||
queryBlock.getSelectList().clear();
|
||||
queryBlock.getSelectList().add(countItem);
|
||||
selectList.clear();
|
||||
selectList.add(countItem);
|
||||
}
|
||||
return SQLUtils.toSQLString(select, dbType);
|
||||
} else if (query instanceof SQLUnionQuery) {
|
||||
|
||||
@@ -40,7 +40,7 @@ public class PagerUtilsTest_Count_MySql_0 extends TestCase {
|
||||
public void test_mysql_4() throws Exception {
|
||||
String sql = "select distinct a.col1,a.col2 from test a";
|
||||
String result = PagerUtils.count(sql, JdbcConstants.MYSQL);
|
||||
Assert.assertEquals("SELECT DISTINCT COUNT(*)\n" +
|
||||
assertEquals("SELECT COUNT(DISTINCT a.col1, a.col2)\n" +
|
||||
"FROM test a", result);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user