bug fixed for sql server parser. for issue #1795

This commit is contained in:
wenshao
2017-07-12 01:21:47 +08:00
parent cdcc4ed07c
commit a288d1014b
7 changed files with 195 additions and 28 deletions
@@ -404,7 +404,8 @@ public class PagerUtils {
SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) query;
if (queryBlock.getGroupBy() != null && queryBlock.getGroupBy().getItems().size() > 0) {
if (queryBlock.getGroupBy() != null
&& queryBlock.getGroupBy().getItems().size() > 0) {
return createCountUseSubQuery(select, dbType);
}
@@ -489,16 +490,8 @@ public class PagerUtils {
private static void clearOrderBy(SQLSelectQuery query) {
if (query instanceof SQLSelectQueryBlock) {
SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) query;
if (queryBlock instanceof MySqlSelectQueryBlock) {
MySqlSelectQueryBlock mysqlQueryBlock = (MySqlSelectQueryBlock) queryBlock;
if (mysqlQueryBlock.getOrderBy() != null) {
mysqlQueryBlock.setOrderBy(null);
}
} else if (queryBlock instanceof PGSelectQueryBlock) {
PGSelectQueryBlock pgQueryBlock = (PGSelectQueryBlock) queryBlock;
if (pgQueryBlock.getOrderBy() != null) {
pgQueryBlock.setOrderBy(null);
}
if (queryBlock.getOrderBy() != null) {
queryBlock.setOrderBy(null);
}
return;
}
@@ -163,6 +163,8 @@ public class SQLServerSelectParser extends SQLSelectParser {
parseGroupBy(queryBlock);
queryBlock.setOrderBy(this.exprParser.parseOrderBy());
parseFetchClause(queryBlock);
return queryRest(queryBlock);
@@ -16,6 +16,7 @@
package com.alibaba.druid.sql.dialect.sqlserver.visitor;
import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.SQLOrderBy;
import com.alibaba.druid.sql.ast.SQLSetQuantifier;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.statement.*;
@@ -82,9 +83,16 @@ public class SQLServerOutputVisitor extends SQLASTOutputVisitor implements SQLSe
x.getWhere().accept(this);
}
if (x.getGroupBy() != null) {
SQLSelectGroupByClause groupBy = x.getGroupBy();
if (groupBy != null) {
println();
x.getGroupBy().accept(this);
groupBy.accept(this);
}
SQLOrderBy orderBy = x.getOrderBy();
if (orderBy != null) {
println();
orderBy.accept(this);
}
printFetchFirst(x);
@@ -620,7 +620,8 @@ public class SQLSelectParser extends SQLParser {
if (lexer.token() == Token.FETCH) {
lexer.nextToken();
if (lexer.token() == Token.FIRST) {
if (lexer.token() == Token.FIRST
|| identifierEquals("NEXT")) {
lexer.nextToken();
} else {
acceptIdentifier("FIRST");
@@ -1370,14 +1370,16 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
printHierarchical(x);
if (x.getGroupBy() != null) {
SQLSelectGroupByClause groupBy = x.getGroupBy();
if (groupBy != null) {
println();
x.getGroupBy().accept(this);
groupBy.accept(this);
}
if (x.getOrderBy() != null) {
SQLOrderBy orderBy = x.getOrderBy();
if (orderBy != null) {
println();
x.getOrderBy().accept(this);
orderBy.accept(this);
}
if (!JdbcConstants.INFORMIX.equals(dbType)) {
@@ -1430,11 +1432,21 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
if (offset != null) {
print0(ucase ? "OFFSET " : "offset ");
offset.accept(this);
print0(ucase ? " ROWS " : " rows ");
print0(ucase ? " ROWS" : " rows");
}
if (first != null) {
if (offset != null) {
print(' ');
}
if (JdbcConstants.SQL_SERVER.equals(dbType) && offset != null) {
print0(ucase ? "FETCH NEXT " : "fetch next ");
} else {
print0(ucase ? "FETCH FIRST " : "fetch first ");
}
first.accept(this);
print0(ucase ? " ROWS ONLY" : " rows only");
}
print0(ucase ? "FETCH FIRST " : "fetch first ");
first.accept(this);
print0(ucase ? " ROWS ONLY" : " rows only");
} else {
println();
limit.accept(this);