Skip to content

Differentiate between JPQL and native queries in count query derivation #2777

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

Closed
wants to merge 3 commits into from

Conversation

mp911de
Copy link
Member

@mp911de mp911de commented Jan 25, 2023

We now consider whether a query is a native one when deriving a count query for pagination. Previously, the generated queries used JPQL syntax that doesn't comply with native SQL syntax rules.

Also, cleanup tests, deduplicate test code, and rearrange test cases into a maintainable form.

Closes #2773

We now consider whether a query is a native one when deriving a count query for pagination. Previously, the generated queries used JPQL syntax that doesn't comply with native SQL syntax rules.
De-duplicate code, use parametrized tests instead of test methods to verify individual fixtures. Ensure all variants are tested with JSQLparser and the default enhancer.
@mp911de mp911de requested review from schauder and gregturn January 25, 2023 14:44
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jan 25, 2023
Copy link
Contributor

@gregturn gregturn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put in some naming suggestions. But apart from that, it looks great. Like the TCK tactic.

}

@Test // GH-2578
void deeplyNestedcomplexSetOperationListWorks() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps make it deeplyNestedComplexSetOperationListWorks?

}

@Test // GH-2578
void valuesStatementsWorks() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps valuesStatementWorks?

}

@Test // GH-2578
void withStatementsWorks() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps withStatementWorks?

}

@Test // GH-2578
void multipleWithStatementsWorks() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps multipleWithStatementsWork?

* @return a query String to be used a count query for pagination. Guaranteed to be not {@literal null}.
* @since 2.7.8
*/
static String createCountQueryFor(String originalQuery, @Nullable String countProjection, boolean nativeQuery) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer two separate methods createCountQueryForNative and createCountQueryForJpql plus extracting the common parts into a new method.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My gut feeling is that we should rethink QueryUtils entirely and eventually eliminate the most methods (or create delegates). Therefore, I would want to refrain from introducing additional methods for the time being unless new methods heavily improve the code.

schauder pushed a commit that referenced this pull request Jan 30, 2023
We now consider whether a query is a native one when deriving a count query for pagination. Previously, the generated queries used JPQL syntax that doesn't comply with native SQL syntax rules.

Closes #2773
Original pull request #2777
schauder pushed a commit that referenced this pull request Jan 30, 2023
De-duplicate code, use parametrized tests instead of test methods to verify individual fixtures. Ensure all variants are tested with JSQLparser and the default enhancer.

See #2773
Original pull request #2777
schauder pushed a commit that referenced this pull request Jan 30, 2023
We now consider whether a query is a native one when deriving a count query for pagination. Previously, the generated queries used JPQL syntax that doesn't comply with native SQL syntax rules.

Closes #2773
Original pull request #2777
schauder pushed a commit that referenced this pull request Jan 30, 2023
De-duplicate code, use parametrized tests instead of test methods to verify individual fixtures. Ensure all variants are tested with JSQLparser and the default enhancer.

See #2773
Original pull request #2777
@schauder schauder added this to the 3.0.2 (2022.0.2) milestone Jan 30, 2023
@schauder
Copy link
Contributor

That's merged and back ported to 3.0.x

@schauder schauder closed this Jan 30, 2023
@schauder schauder assigned mp911de and unassigned schauder Jan 30, 2023
mp911de added a commit that referenced this pull request Feb 15, 2023
We now consider whether a query is a native one when deriving a count query for pagination. Previously, the generated queries used JPQL syntax that doesn't comply with native SQL syntax rules.

Closes #2773
Original pull request #2777
String alias = QueryUtils.detectAlias(originalQuery);
if ("*".equals(variable) && alias != null) {
replacement = alias;
if (nativeQuery && (variable.contains(",") || "*".equals(variable))) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if it is a native query and the variable is null 😬

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you run into a bug or you can reproduce such a case, please file a bug report.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

already present #2812

@ghost
Copy link

ghost commented Feb 23, 2023

will it be back ported to 2.7.x?

@mp911de
Copy link
Member Author

mp911de commented Feb 23, 2023

Yes, we will backport the fix to 2.7.x.

@mp911de mp911de deleted the issue/2773 branch February 23, 2023 15:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Broken count query for native queries with table alias
5 participants