Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] JSQLParser Version : 5.1 解析sqlserver数据库 stuff函数,当查询语句添加order by ,解析会报错 #2173

Open
rayliang123 opened this issue Feb 21, 2025 · 2 comments
Labels
MS SQL Server MS SQL Server specific feature Sponsor needed

Comments

@rayliang123
Copy link

SQL Example:

public static void main(String[] args) {
String sql = "SELECT STUFF((SELECT ',' + name FROM class order by id FOR XML PATH('')),1,1,'') AS names from " +
"users";
try {
Statement statement = CCJSqlParserUtil.parse(sql);
} catch (JSQLParserException e) {
throw new RuntimeException(e);
}
}
CCJSqlParserUtil解析sql错误,错误信息如下

as expecting one of:

<EOF>
<ST_SEMICOLON>

at com.aciplaw.patas.operation.service.impl.CaseInfoServiceImpl.main(CaseInfoServiceImpl.java:1154)

Caused by: net.sf.jsqlparser.JSQLParserException: net.sf.jsqlparser.parser.ParseException: Encountered unexpected token: "(" "("
at line 1, column 13.

Was expecting one of:

<EOF>
<ST_SEMICOLON>

at net.sf.jsqlparser.parser.CCJSqlParserUtil.parseStatement(CCJSqlParserUtil.java:352)
at net.sf.jsqlparser.parser.CCJSqlParserUtil.parse(CCJSqlParserUtil.java:125)
at net.sf.jsqlparser.parser.CCJSqlParserUtil.parse(CCJSqlParserUtil.java:91)
at net.sf.jsqlparser.parser.CCJSqlParserUtil.parse(CCJSqlParserUtil.java:64)
at com.aciplaw.patas.operation.service.impl.CaseInfoServiceImpl.main(CaseInfoServiceImpl.java:1152)

Caused by: java.util.concurrent.ExecutionException: net.sf.jsqlparser.parser.ParseException: Encountered unexpected token: "(" "("
at line 1, column 13.

Was expecting one of:

<EOF>
<ST_SEMICOLON>

at java.base/java.util.concurrent.FutureTask.report(FutureTask.java:122)
at java.base/java.util.concurrent.FutureTask.get(FutureTask.java:205)
at net.sf.jsqlparser.parser.CCJSqlParserUtil.parseStatement(CCJSqlParserUtil.java:345)
... 4 more

Caused by: net.sf.jsqlparser.parser.ParseException: Encountered unexpected token: "(" "("
at line 1, column 13.

Was expecting one of:

<EOF>
<ST_SEMICOLON>

at net.sf.jsqlparser.parser.CCJSqlParser.generateParseException(CCJSqlParser.java:48520)
at net.sf.jsqlparser.parser.CCJSqlParser.jj_consume_token(CCJSqlParser.java:48341)
at net.sf.jsqlparser.parser.CCJSqlParser.Statement(CCJSqlParser.java:345)
at net.sf.jsqlparser.parser.CCJSqlParserUtil$1.call(CCJSqlParserUtil.java:341)
at net.sf.jsqlparser.parser.CCJSqlParserUtil$1.call(CCJSqlParserUtil.java:338)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:317)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
at java.base/java.lang.Thread.run(Thread.java:1583)

如果去掉order by ,解析就会正确,程序如何
public static void main(String[] args) {
String sql = "SELECT STUFF((SELECT ',' + name FROM class FOR XML PATH('')),1,1,'') AS names from " +
"users";
try {
Statement statement = CCJSqlParserUtil.parse(sql);
} catch (JSQLParserException e) {
throw new RuntimeException(e);
}
}

估计是 CCJSqlParser 类解析时出错,应该时一个bug,期望下个版本可以解决

@manticore-projects
Copy link
Contributor

Greetings.

FOR XML PATH('') is not supported.

@manticore-projects manticore-projects added Sponsor needed MS SQL Server MS SQL Server specific feature labels Feb 21, 2025
@rayliang123
Copy link
Author

@manticore-projects
When the STUFF function is used in combination with FOR XML
PATH, it can implement a function similar to GROUP CONCAT, co
mbining strings in multiple rows into one string. This is very uset
ul when you need to merge the content of multiple lines into on
e result.
can you supported this function?????????

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
MS SQL Server MS SQL Server specific feature Sponsor needed
Projects
None yet
Development

No branches or pull requests

2 participants