Polishing.

This commit is contained in:
Greg L. Turnquist
2022-12-21 13:23:36 -06:00
parent 6ce2dd8895
commit 525cf6abb0
3 changed files with 19 additions and 7 deletions

View File

@@ -308,10 +308,12 @@ public class JSqlParserQueryEnhancer implements QueryEnhancer {
private String detectAlias(String query) {
if (ParsedType.MERGE.equals(this.parsedType)) {
Merge mergeStatement = parseSelectStatement(query, Merge.class);
return detectAlias(mergeStatement);
} else if (ParsedType.SELECT.equals(this.parsedType)) {
Select selectStatement = parseSelectStatement(query);
/*
@@ -357,6 +359,7 @@ public class JSqlParserQueryEnhancer implements QueryEnhancer {
*/
@Nullable
private String detectAlias(Merge mergeStatement) {
Alias alias = mergeStatement.getUsingAlias();
return alias == null ? null : alias.getName();
}
@@ -385,6 +388,7 @@ public class JSqlParserQueryEnhancer implements QueryEnhancer {
selectBody.setOrderByElements(null);
if (StringUtils.hasText(countProjection)) {
Function jSqlCount = getJSqlCount(Collections.singletonList(countProjection), false);
selectBody.setSelectItems(Collections.singletonList(new SelectExpressionItem(jSqlCount)));
return selectBody.toString();
@@ -399,6 +403,7 @@ public class JSqlParserQueryEnhancer implements QueryEnhancer {
List<SelectItem> selectItems = selectBody.getSelectItems();
if (onlyASingleColumnProjection(selectItems)) {
SelectExpressionItem singleProjection = (SelectExpressionItem) selectItems.get(0);
Column column = (Column) singleProjection.getExpression();
@@ -443,6 +448,7 @@ public class JSqlParserQueryEnhancer implements QueryEnhancer {
SelectBody selectBody = selectStatement.getSelectBody();
if (selectStatement.getSelectBody() instanceof SetOperationList) {
SetOperationList setOperationList = (SetOperationList) selectStatement.getSelectBody();
// using the first one since for setoperations the projection has to be the same
selectBody = setOperationList.getSelects().get(0);

View File

@@ -2816,15 +2816,17 @@ public class UserRepositoryTests {
flushTestUsers();
Optional<User> byIdUser = repository.findById(firstUser.getId());
assertThat(byIdUser).isPresent().map(User::getAge).get().isEqualTo(28);
assertThat(repository.findById(firstUser.getId())) //
.isPresent() //
.map(User::getAge).contains(28);
// when
repository.mergeNativeStatement();
// then
Optional<User> afterUpdate = repository.findById(firstUser.getId());
assertThat(afterUpdate).isPresent().map(User::getAge).get().isEqualTo(30);
assertThat(repository.findById(firstUser.getId())) //
.isPresent() //
.map(User::getAge).contains(30);
}
private Page<User> executeSpecWithSort(Sort sort) {

View File

@@ -926,6 +926,7 @@ class QueryEnhancerUnitTests {
@ParameterizedTest // GH-2641
@MethodSource("mergeStatementWorksWithJSqlParserSource")
void mergeStatementWorksWithJSqlParser(String query, String alias) {
StringQuery stringQuery = new StringQuery(query, true);
QueryEnhancer queryEnhancer = QueryEnhancerFactory.forQuery(stringQuery);
@@ -939,6 +940,7 @@ class QueryEnhancerUnitTests {
}
public static Stream<Arguments> insertStatementIsProcessedSameAsDefaultSource() {
return Stream.of( //
Arguments.of("INSERT INTO FOO(A) VALUES('A')"), //
Arguments.of("INSERT INTO randomsecondTable(A,B,C,D) VALUES('A','B','C','D')") //
@@ -946,9 +948,11 @@ class QueryEnhancerUnitTests {
}
public static Stream<Arguments> mergeStatementWorksWithJSqlParserSource() {
return Stream.of(Arguments.of(
"merge into a using (select id, value from b) query on (a.id = query.id) when matched then update set a.value = value",
"query"),
return Stream.of( //
Arguments.of(
"merge into a using (select id, value from b) query on (a.id = query.id) when matched then update set a.value = value",
"query"),
Arguments.of(
"merge into a using (select id2, value from b) on (id = id2) when matched then update set a.value = value",
null));