mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
fix issue #471 DB2数据库SQL带ORDER BY的分页查询生成的语句不对
This commit is contained in:
@@ -15,6 +15,9 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.db2.visitor;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLObject;
|
||||
import com.alibaba.druid.sql.ast.SQLOrderBy;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelect;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.db2.ast.stmt.DB2SelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.db2.ast.stmt.DB2ValuesStatement;
|
||||
@@ -31,12 +34,26 @@ public class DB2OutputVisitor extends SQLASTOutputVisitor implements DB2ASTVisit
|
||||
this.visit((SQLSelectQueryBlock) x);
|
||||
|
||||
if (x.getFirst() != null) {
|
||||
|
||||
//order by 语句必须在FETCH FIRST ROWS ONLY之前
|
||||
SQLObject parent= x.getParent();
|
||||
if(parent instanceof SQLSelect)
|
||||
{
|
||||
SQLOrderBy orderBy= ((SQLSelect) parent).getOrderBy();
|
||||
if (orderBy!=null&&orderBy.getItems().size() > 0) {
|
||||
println();
|
||||
print("ORDER BY ");
|
||||
printAndAccept(orderBy.getItems(), ", ");
|
||||
((SQLSelect) parent).setOrderBy(null);
|
||||
}
|
||||
}
|
||||
println();
|
||||
print("FETCH FIRST ");
|
||||
x.getFirst().accept(this);
|
||||
print(" ROWS ONLY");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
if (x.isForReadOnly()) {
|
||||
println();
|
||||
print("FOR READ ONLY");
|
||||
@@ -57,6 +74,7 @@ public class DB2OutputVisitor extends SQLASTOutputVisitor implements DB2ASTVisit
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void endVisit(DB2SelectQueryBlock x) {
|
||||
|
||||
|
||||
@@ -50,4 +50,15 @@ public class PagerUtilsTest_Limit_db2_0 extends TestCase {
|
||||
+ "\nWHERE ROWNUM > 20"//
|
||||
+ "\n\tAND ROWNUM <= 30", result);
|
||||
}
|
||||
|
||||
public void test_fetch_order() {
|
||||
String sql="SELECT t.CUSTNAME AS custname\n" +
|
||||
"FROM CPS_LOAN_INFO t WHERE t.DEL_FLAG = 0\n" +
|
||||
"ORDER BY t.CREATE_TIME DESC";
|
||||
|
||||
String limitSql=PagerUtils.limit(sql, JdbcConstants.DB2,0,20) ;
|
||||
int fetchRowIndex=limitSql.indexOf("FETCH FIRST");
|
||||
int orderIndex=limitSql.indexOf("ORDER BY") ;
|
||||
Assert.assertTrue(fetchRowIndex>orderIndex);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user