Skip to content

Commit b9c9262

Browse files
authored
Merge pull request #1 from Zxc4wonder/codex/fix-oracle-fetch-offset-row
fix: support ROW in Oracle FETCH and OFFSET
2 parents 4a436b3 + ece9651 commit b9c9262

2 files changed

Lines changed: 57 additions & 2 deletions

File tree

core/src/main/java/com/alibaba/druid/sql/parser/SQLSelectParser.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2139,7 +2139,10 @@ public void parseFetchClause(SQLSelectQueryBlock queryBlock) {
21392139
lexer.nextToken();
21402140
SQLExpr offset = this.exprParser.expr();
21412141
queryBlock.setOffset(offset);
2142-
if (lexer.identifierEquals(FnvHash.Constants.ROW) || lexer.identifierEquals(FnvHash.Constants.ROWS)) {
2142+
if (lexer.token == Token.ROW
2143+
|| lexer.token == Token.ROWS
2144+
|| lexer.identifierEquals(FnvHash.Constants.ROW)
2145+
|| lexer.identifierEquals(FnvHash.Constants.ROWS)) {
21432146
lexer.nextToken();
21442147
}
21452148
}
@@ -2155,7 +2158,10 @@ public void parseFetchClause(SQLSelectQueryBlock queryBlock) {
21552158
}
21562159
SQLExpr first = this.exprParser.primary();
21572160
queryBlock.setFirst(first);
2158-
if (lexer.identifierEquals(FnvHash.Constants.ROW) || lexer.identifierEquals(FnvHash.Constants.ROWS)) {
2161+
if (lexer.token == Token.ROW
2162+
|| lexer.token == Token.ROWS
2163+
|| lexer.identifierEquals(FnvHash.Constants.ROW)
2164+
|| lexer.identifierEquals(FnvHash.Constants.ROWS)) {
21592165
lexer.nextToken();
21602166
}
21612167

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 1999-2017 Alibaba Group Holding Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.alibaba.druid.bvt.sql.oracle.select;
17+
18+
import com.alibaba.druid.DbType;
19+
import com.alibaba.druid.sql.SQLUtils;
20+
import com.alibaba.druid.sql.ast.SQLStatement;
21+
import junit.framework.TestCase;
22+
23+
public class OracleSelectTest_fetch_offset_row extends TestCase {
24+
public void test_fetch_offset_row() {
25+
assertFormatted(
26+
"SELECT * FROM t FETCH FIRST 1 ROW ONLY;",
27+
"SELECT *\nFROM t\nFETCH FIRST 1 ROWS ONLY;");
28+
assertFormatted(
29+
"SELECT * FROM t FETCH FIRST 2 ROWS ONLY;",
30+
"SELECT *\nFROM t\nFETCH FIRST 2 ROWS ONLY;");
31+
assertFormatted(
32+
"SELECT * FROM t OFFSET 1 ROW;",
33+
"SELECT *\nFROM t\nOFFSET 1 ROWS;");
34+
assertFormatted(
35+
"SELECT * FROM t OFFSET 2 ROWS;",
36+
"SELECT *\nFROM t\nOFFSET 2 ROWS;");
37+
assertFormatted(
38+
"SELECT * FROM t OFFSET 1 ROW FETCH NEXT 1 ROW ONLY;",
39+
"SELECT *\nFROM t\nOFFSET 1 ROWS FETCH FIRST 1 ROWS ONLY;");
40+
}
41+
42+
private void assertFormatted(String sql, String expected) {
43+
SQLStatement statement = SQLUtils.parseSingleStatement(sql, DbType.oracle);
44+
String formatted = SQLUtils.toSQLString(statement, DbType.oracle);
45+
46+
assertEquals(expected, formatted);
47+
assertNotNull(SQLUtils.parseSingleStatement(formatted, DbType.oracle));
48+
}
49+
}

0 commit comments

Comments
 (0)