Skip to content

Commit

Permalink
Support NAME keyword in Oracle XMLELEMENT function
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhengguanLi committed Apr 22, 2024
1 parent cf165de commit 2dbcc9d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1873,6 +1873,15 @@ protected SQLExpr methodRest(SQLExpr expr, boolean acceptLPAREN) {
SQLExpr contentExpr = expr();
methodInvokeExpr.setContent(contentExpr);
}
if ("XMLELEMENT".equals(methodName) && lexer.identifierEquals("NAME")) {
Lexer.SavePoint mark = lexer.markOut();
lexer.nextToken(); // Skip NAME if it is a keyword
if (lexer.token != Token.IDENTIFIER) {
// No other identifier name comes after NAME, so NAME itself is
// the xml element name. Reset lexer to NAME
lexer.reset(mark);
}
}
if (token != Token.RPAREN && token != Token.FROM) {
exprList(methodInvokeExpr.getArguments(), methodInvokeExpr);
if (lexer.token == Token.RPAREN) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.alibaba.druid.bvt.sql.oracle;

import com.alibaba.druid.sql.OracleTest;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser;
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor;
import org.junit.Assert;

import java.util.List;

public class OracleXmlelementTest extends OracleTest {
public void testOracleXmlelement() {
String sql = "SELECT /* NUSQL.TEST */ XMLELEMENT(NAME foo).getstringval() from dual";

OracleStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement statement = statementList.get(0);
print(statementList);

Assert.assertEquals(1, statementList.size());

OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
statement.accept(visitor);

Assert.assertEquals(0, visitor.getTables().size());
Assert.assertEquals(0, visitor.getColumns().size());
}

public void testOracleXmlelement_WithoutNameKeyword() {
String sql = "SELECT /* NUSQL.TEST */ XMLELEMENT(foo).getstringval() from dual";

OracleStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement statement = statementList.get(0);
print(statementList);

Assert.assertEquals(1, statementList.size());

OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
statement.accept(visitor);

Assert.assertEquals(0, visitor.getTables().size());
Assert.assertEquals(0, visitor.getColumns().size());
}
}

0 comments on commit 2dbcc9d

Please sign in to comment.