-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Use JSqlParser for parsing SQL #2417
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
Use JSqlParser for parsing SQL #2417
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this PR. I highly appreciate it.
I found a few things that we should change before we merge it though.
Please take a look at my comments.
src/main/java/org/springframework/data/jpa/repository/query/AbstractJpaQuery.java
Outdated
Show resolved
Hide resolved
src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java
Outdated
Show resolved
Hide resolved
src/main/java/org/springframework/data/jpa/repository/query/JSqlParserQueryUtils.java
Outdated
Show resolved
Hide resolved
...test/java/org/springframework/data/jpa/repository/query/ParameterBindingParserUnitTests.java
Outdated
Show resolved
Hide resolved
My suggestion is to leave |
@mp911de |
The original |
The new introduced API for all the "enhancing" of a query (that would require to parse and modify the query) is now called
The first one ( Additionally there is a factory class
|
The factory approach makes a lot of sense to be created for a query object. The actual factory implementation would already decide whether to use I think that |
This is now fixed.
This is now fixed.
This is now fixed. |
@DiegoKrupitza care to squash your commits into one or at least a few ones so that we can get rid of merge commits? |
This PR introduces the usage of JSqlParser in spring-data-jpa for native queries. As discussed in spring-projects#2409 the current parser has few SQL related bugs. Closes spring-projects#2409
65c42a4
to
a3e5084
Compare
Done! @mp911de |
@DiegoKrupitza I'll be shepherding in the last bits of your valiant PR. I spotted that private static final String QUERY = "select u from User u";
@Test
void queryEnhancerGetExistsQueryShouldReturnBackExistsQuery() {
QueryEnhancer enhancer = getEnhancer(new StringQuery(QUERY, true));
String existsQueryString = enhancer.getExistsQueryString("USER", "firstname",
Collections.singletonList("firstname"));
assertThat(existsQueryString).isEqualTo("SELECT count(firstname) FROM USER AS x WHERE x.firstname = :firstname");
} The thing returns back a COUNT query. Is this what's expected with crafting an EXISTS query in JPQL? (Having a hard time finding what's proper here). |
That is wonderful to hear!
That is true. I somehow forgot to add a testcase for this method. The
When I implemented the JSqlParser version of As far as I understand I now even checked if QueryUtils.getExistsQueryString("USER", "firstname", Collections.singletonList("firstname"));
// = select count(firstname) from USER x WHERE x.firstname = :firstname QueryEnhancer enhancer = getEnhancer(new StringQuery(QUERY, true));
String existsQueryString = enhancer.getExistsQueryString("USER", "firstname", Collections.singletonList("firstname"));
// = SELECT count(firstname) FROM USER AS x WHERE x.firstname = :firstname PS: I just realised that the method |
I think a COUNT query is not a bad idea here, since we only work with SQL here (when native queries are used) and not JPQL. Additionally as far as I know there are not many ways to check with pure SQL for existence. (Beside performance optimized SQL queries see here) |
I just realised that Note: |
@gregturn since the changes are now in main, can I close this PR? |
This is fixed by 1fe4cf5 |
This PR introduces the usage of JSqlParser in
QueryUtils
. As discussed in #2409 the current parser has few SQL related bugs. In this PR we focus on the most "important" functions of QueryUtils.This PR is currently just a draft and its main purpose is to get a review of the current state.
Closes: #2409