bug fixed for sql wall. for issue #2411

This commit is contained in:
wenshao
2018-03-22 21:32:02 +08:00
parent 720a857913
commit 23e68d13cb
4 changed files with 112 additions and 10 deletions
@@ -93,15 +93,17 @@ public class PagerUtils {
return limitSQLServer(select, dbType, offset, count, check);
}
if (query instanceof SQLSelectQueryBlock) {
return limitQueryBlock(select, dbType, offset, count, check);
}
throw new UnsupportedOperationException();
return limitQueryBlock(select, dbType, offset, count, check);
}
private static boolean limitQueryBlock(SQLSelect select, String dbType, int offset, int count, boolean check) {
SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) select.getQuery();
SQLSelectQuery query = select.getQuery();
if (query instanceof SQLUnionQuery) {
SQLUnionQuery union = (SQLUnionQuery) query;
return limitUnion(union, dbType, offset, count, check);
}
SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) query;
if (JdbcConstants.MYSQL.equals(dbType) || //
JdbcConstants.MARIADB.equals(dbType) || //
JdbcConstants.H2.equals(dbType)) {
@@ -400,6 +402,37 @@ public class PagerUtils {
return true;
}
private static boolean limitUnion(SQLUnionQuery queryBlock, String dbType, int offset, int count, boolean check) {
SQLLimit limit = queryBlock.getLimit();
if (limit != null) {
if (offset > 0) {
limit.setOffset(new SQLIntegerExpr(offset));
}
if (check && limit.getRowCount() instanceof SQLNumericLiteralExpr) {
int rowCount = ((SQLNumericLiteralExpr) limit.getRowCount()).getNumber().intValue();
if (rowCount <= count && offset <= 0) {
return false;
}
} else if (check && limit.getRowCount() instanceof SQLVariantRefExpr) {
return false;
}
limit.setRowCount(new SQLIntegerExpr(count));
}
if (limit == null) {
limit = new SQLLimit();
if (offset > 0) {
limit.setOffset(new SQLIntegerExpr(offset));
}
limit.setRowCount(new SQLIntegerExpr(count));
queryBlock.setLimit(limit);
}
return true;
}
private static String count(SQLSelect select, String dbType) {
if (select.getOrderBy() != null) {
select.setOrderBy(null);
@@ -2721,7 +2721,8 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
println();
SQLSelectQuery right = x.getRight();
boolean needParen = x.getOrderBy() != null && !right.isBracket();
SQLOrderBy orderBy = x.getOrderBy();
boolean needParen = orderBy != null && !right.isBracket();
if (needParen) {
print('(');
@@ -2731,9 +2732,15 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
right.accept(this);
}
if (x.getOrderBy() != null) {
if (orderBy != null) {
println();
x.getOrderBy().accept(this);
orderBy.accept(this);
}
SQLLimit limit = x.getLimit();
if (limit != null) {
println();
limit.accept(this);
}
if (bracket) {