-
Notifications
You must be signed in to change notification settings - Fork 1.5k
JSqlParser fails for modifying queries #2555
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
Comments
Yeah you are right! The current implementation does not detect if it is a modifying query or not. It assumes that all the queries are "selects". The problems is here: private static Select parseSelectStatement(String query) {
try {
return (Select) CCJSqlParserUtil.parse(query);
} catch (JSQLParserException e) {
throw new IllegalArgumentException("The query you provided is not a valid SQL Query!", e);
}
} |
When I implemented the |
Previously this implementation of `QueryEnhancer` could not handle non Select statements (such as Delete, Update, ...). With this commit we do handle them. Keep in mind that "enhancing" non selects does not have any effect on them (and the current default implementation `QueryUtils` does not care either aka it often just returns the same query, null or empty string). Closes: spring-projects#2555
@DiegoKrupitza I don't know if modified queries is possible/makes sense, or if (at least) we simply need a more complete error message? |
@gregturn Cause of Problem:
String sortedQueryString = QueryEnhancerFactory.forQuery(query) //
.applySorting(accessor.getSort(), query.getAlias()); No matter what kind of query it is, we call Proposed Solutions:
|
Thanks @DiegoKrupitza. To be honest, your proposed PR is along the lines of what I was thinking. Sniff out the query "type", and cut out early from any sorting, etc. |
Thanks @DiegoKrupitza! |
This implementation of QueryEnhancer was originally designed for SELECT statements. This commit now handles DELETE and UPDATE operations by side-stepping any sorting or other changes. Keep in mind that "enhancing" non selects does not have any effect on them (and the current default implementation `QueryUtils` does not care either aka it often just returns the same query, null or empty string). Closes #2555
This implementation of QueryEnhancer was originally designed for SELECT statements. This commit now handles DELETE and UPDATE operations by side-stepping any sorting or other changes. Keep in mind that "enhancing" non selects does not have any effect on them (and the current default implementation `QueryUtils` does not care either aka it often just returns the same query, null or empty string). Closes #2555
Backported to |
I have the same issue with an insert query, using 2.7.2-SNAPSHOT. Thanks. |
@fabriziodelfranco there is an issue (#2593) that covers this problem. I already created a PR (#2595) that fixes this issue |
Thank you @DiegoKrupitza |
Thanks @DiegoKrupitza and @gregturn for quick response, followup and fix. |
Using Spring Boot 2.7 which uses Spring Data JPA 2.7.0 and I have a problem that occurred after upgrading from Spring Boot 2.3.12.
I have a simple modifying query something like this:
@Modifying(clearAutomatically = true) @Query(value = "update userinfo user set user.is_in_treatment = false where user.id = :userId", nativeQuery = true) void setNotInTreatment(@Param("userId") int userId);
We are using JSqlParser for other things in our project, so it is in the classpath, and that parser is therefore used according to the log:
o.s.d.j.r.q.QueryEnhancerFactory - JSqlParser is in classpath. If applicable JSqlParser will be used.
At startup the parsing of the above query fails with the following error message:
Could not create query for public abstract void com.norse.api.repository.UserRepository.setNotInTreatment(int)! Reason: class net.sf.jsqlparser.statement.update.Update cannot be cast to class net.sf.jsqlparser.statement.select.Select
I'm assume the error is related to the fact that I have a modifying query?
The text was updated successfully, but these errors were encountered: