druid PagerUtils.limit 对oracle语句处理错误

eeq64g8w  于 4个月前  发布在  Druid
关注(0)|答案(6)|浏览(76)

版本: 1.1.13
语句:
SELECT TO_CHAR(ADD_MONTHS(TO_DATE('2014', 'yyyy'), (ROWNUM) * 12), 'yyyy') as YEAR
FROM DUAL
CONNECT BY ROWNUM <=
months_between(to_date(to_char(sysdate,'yyyy'),'yyyy') ,
to_date('2014', 'yyyy')) / 12

执行以下语句:
PagerUtils.limit(sql, "oracle", 0, 10);

得到结果:
SELECT TO_CHAR(ADD_MONTHS(TO_DATE('2014', 'yyyy'), ROWNUM * 12), 'yyyy') AS YEAR
FROM DUAL
WHERE ROWNUM <= 10
CONNECT BY ROWNUM <= months_between(to_date(to_char(SYSDATE, 'yyyy'), 'yyyy'), to_date('2014', 'yyyy')) / 12

结果服务器执行语句失败

正确语法应该是
SELECT TO_CHAR(ADD_MONTHS(TO_DATE('2014', 'yyyy'), ROWNUM * 12), 'yyyy') AS YEAR
FROM DUAL
CONNECT BY ROWNUM <= months_between(to_date(to_char(SYSDATE, 'yyyy'), 'yyyy'), to_date('2014', 'yyyy')) / 12 and ROWNUM <= 10

x8diyxa7

x8diyxa71#

我看到生成的SQL是这个:

SELECT *
FROM (
	SELECT XX.*, ROWNUM AS RN
	FROM (
		SELECT TO_CHAR(ADD_MONTHS(TO_DATE('2014', 'yyyy'), ROWNUM * 12), 'yyyy') AS YEAR
		FROM DUAL
		CONNECT BY ROWNUM <= months_between(to_date(to_char(SYSDATE, 'yyyy'), 'yyyy'), to_date('2014', 'yyyy')) / 12
	) XX
	WHERE ROWNUM <= 30
) XXX
WHERE RN > 20
r8xiu3jd

r8xiu3jd3#

MF文件内容如下:
Manifest-Version: 1.0
Implementation-Title: druid
Implementation-Version: 1.1.13
Archiver-Version: Plexus Archiver
Built-By: wenshao
Implementation-Build: 2019-01-30 21:26:09
Implementation-Vendor-Id: com.alibaba
Created-By: Apache Maven 3.5.0
Build-Jdk: 1.8.0_181
Implementation-Vendor: Alibaba Group

cbjzeqam

cbjzeqam4#

经过尝试发现,当offset大于0时确实正常,但当offset等于0时就会出现以上bug。

PagerUtils.java:309
if ((queryBlock.getGroupBy() == null) && (orderBy == null) && (offset <= 0))

enyaitl3

enyaitl35#

看上去bug还在,我看下怎么修复

vwhgwdsa

vwhgwdsa6#

备份一下测试代码

@Test
    public void test_limit() throws Exception {
            String sql = "SELECT TO_CHAR(ADD_MONTHS(TO_DATE('2014', 'yyyy'), (ROWNUM) * 12), 'yyyy') as YEAR\n"
                + "FROM DUAL\n"
                + "CONNECT BY ROWNUM <=\n"
                + "months_between(to_date(to_char(sysdate,'yyyy'),'yyyy') ,\n"
                + "to_date('2014', 'yyyy')) / 12";
            System.out.println("原始的sql===" + sql);
            String newSql = PagerUtils.limit(sql, DbType.oracle, 0, 10);;
            System.out.println("生成的sql===" + newSql);
         newSql = PagerUtils.limit(sql, DbType.oracle, 10, 20);;
        System.out.println("生成的sql===" + newSql);

    }

相关问题