Skip to content

Commit

Permalink
Register individual bindings for IN bindings if parameter is already …
Browse files Browse the repository at this point in the history
…bound differently.

We now register a new parameter binding if the named/positional parameter is already bound in an incompatible style.

Closes #3126
  • Loading branch information
mp911de committed Aug 25, 2023
1 parent 50de4d7 commit a9279c7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -288,36 +288,28 @@ private String parseParameterBindingsOfQueryIntoBindingsAndReturnCleanedQuery(St
: ParameterOrigin.ofExpression(expression);

BindingIdentifier targetBinding = queryParameter;
Function<BindingIdentifier, ParameterBinding> bindingFactory;
switch (ParameterBindingType.of(typeSource)) {

case LIKE:

Type likeType = LikeParameterBinding.getLikeTypeFrom(matcher.group(2));

if (origin.isExpression()) {
parameterBindings.register(new LikeParameterBinding(queryParameter, origin, likeType));
} else {
targetBinding = parameterBindings.register(queryParameter, origin,
(identifier) -> new LikeParameterBinding(identifier, origin, likeType));
}

bindingFactory = (identifier) -> new LikeParameterBinding(identifier, origin, likeType);
break;

case IN:

parameterBindings.register(new InParameterBinding(queryParameter, origin));

bindingFactory = (identifier) -> new InParameterBinding(identifier, origin);
break;

case AS_IS: // fall-through we don't need a special parameter queryParameter for the given parameter.
default:
bindingFactory = (identifier) -> new ParameterBinding(identifier, origin);
}

if (origin.isExpression()) {
parameterBindings.register(new ParameterBinding(queryParameter, origin));
} else {
targetBinding = parameterBindings.register(queryParameter, origin,
(identifier) -> new ParameterBinding(identifier, origin));
}
if (origin.isExpression()) {
parameterBindings.register(bindingFactory.apply(queryParameter));
} else {
targetBinding = parameterBindings.register(queryParameter, origin, bindingFactory);
}

replacement = targetBinding.hasName() ? ":" + targetBinding.getName()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,6 @@ void detectsNamedInParameterBindings() {
assertThat(bindings).hasSize(1);

assertNamedBinding(InParameterBinding.class, "ids", bindings.get(0));

softly.assertAll();
}

@Test // DATAJPA-461
Expand Down Expand Up @@ -282,8 +280,37 @@ void detectsPositionalInParameterBindings() {
assertThat(bindings).hasSize(1);

assertPositionalBinding(InParameterBinding.class, 1, bindings.get(0));
}

softly.assertAll();
@Test // GH-3126
void allowsReuseOfParameterWithInAndRegularBinding() {

StringQuery query = new StringQuery(
"select u from User u where COALESCE(?1) is null OR u.id in ?1 OR COALESCE(?1) is null OR u.id in ?1", true);

assertThat(query.hasParameterBindings()).isTrue();
assertThat(query.getQueryString()).isEqualTo(
"select u from User u where COALESCE(?1) is null OR u.id in ?2 OR COALESCE(?1) is null OR u.id in ?2");

List<ParameterBinding> bindings = query.getParameterBindings();
assertThat(bindings).hasSize(2);

assertPositionalBinding(ParameterBinding.class, 1, bindings.get(0));
assertPositionalBinding(InParameterBinding.class, 2, bindings.get(1));

query = new StringQuery(
"select u from User u where COALESCE(:foo) is null OR u.id in :foo OR COALESCE(:foo) is null OR u.id in :foo",
true);

assertThat(query.hasParameterBindings()).isTrue();
assertThat(query.getQueryString()).isEqualTo(
"select u from User u where COALESCE(:foo) is null OR u.id in :foo_1 OR COALESCE(:foo) is null OR u.id in :foo_1");

bindings = query.getParameterBindings();
assertThat(bindings).hasSize(2);

assertNamedBinding(ParameterBinding.class, "foo", bindings.get(0));
assertNamedBinding(InParameterBinding.class, "foo_1", bindings.get(1));
}

@Test // DATAJPA-461
Expand Down Expand Up @@ -360,8 +387,6 @@ void detectsInBindingWithSpecialFrenchCharactersInParentheses() {

assertThat(bindings).hasSize(1);
assertNamedBinding(InParameterBinding.class, "abonnés", bindings.get(0));

softly.assertAll();
}

@Test // DATAJPA-545
Expand Down

0 comments on commit a9279c7

Please sign in to comment.