Skip to content

Commit

Permalink
Raise exception if a positional query param is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
angusholder authored and stevenschlansker committed Jun 28, 2024
1 parent 57a93ae commit 2513227
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,10 @@ void bind(Binding binding) {
}

void bindPositional(Binding binding) {
boolean moreArgumentsProvidedThanDeclared = binding.positionals.size() != params.getParameterCount();
if (moreArgumentsProvidedThanDeclared && !ctx.getConfig(SqlStatements.class).isUnusedBindingAllowed()) {
throw new UnableToCreateStatementException("Superfluous positional param at (0 based) position " + params.getParameterCount(), ctx);
}
for (int index = 0; index < params.getParameterCount(); index++) {
if (!binding.positionals.containsKey(index)) {
throw new UnableToCreateStatementException(format("Missing positional parameter %d in binding:%s", index, binding), ctx);
}
QualifiedType<?> type = typeOf(binding.positionals.get(index));
try {
argumentFactoryForType(type)
Expand All @@ -84,6 +83,10 @@ void bindPositional(Binding binding) {
throw new UnableToCreateStatementException("Exception while binding positional param at (0 based) position " + index, e, ctx);
}
}
boolean moreArgumentsProvidedThanDeclared = binding.positionals.size() != params.getParameterCount();
if (moreArgumentsProvidedThanDeclared && !ctx.getConfig(SqlStatements.class).isUnusedBindingAllowed()) {
throw new UnableToCreateStatementException("Superfluous positional param at (0 based) position " + params.getParameterCount(), ctx);
}
}

void bindNamed(Binding binding) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.junit.jupiter.api.extension.RegisterExtension;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class TestArgumentBinder {

Expand Down Expand Up @@ -283,6 +284,36 @@ public void testNullBatchArgument() {
}
}

@Test
public void testMissingParamThrowsError() {
try (Handle h = pgDatabaseExtension.openHandle()) {
assertThatThrownBy(() -> {
// Right number of params, but indexes are non-contiguous by mistake.
h.createQuery("SELECT COUNT(1) from binder_test WHERE i = ? OR i = ? OR i = ?")
.bind(0, 100)
.bind(1, 101)
.bind(3, 102)
.mapTo(Integer.class)
.one();
}).isInstanceOf(UnableToCreateStatementException.class)
.hasMessage("Param at (0 based) position 2 was not provided");
}
}

@Test
public void testOneBasedIndexingThrowsError() {
try (Handle h = pgDatabaseExtension.openHandle()) {
assertThatThrownBy(() -> {
// Mistakenly thought the params are 1-based.
h.createQuery("SELECT COUNT(1) from binder_test WHERE i = ?")
.bind(1, 100)
.mapTo(Integer.class)
.one();
}).isInstanceOf(UnableToCreateStatementException.class)
.hasMessage("Param at (0 based) position 0 was not provided");
}
}

public static class TestBean {

private final int i;
Expand Down

0 comments on commit 2513227

Please sign in to comment.