Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

JdbcClient should reject a List as the parameter of the IN clause of an SQL statement when using positional parameters #31215

Closed
hansdesmet opened this issue Sep 13, 2023 · 2 comments
Assignees
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) type: enhancement A general enhancement
Milestone

Comments

@hansdesmet
Copy link

Most of the methods of JdbcClient work with positional parameters in the SQL statement as well as with named parameters 馃憤.

When I use a named parameter, i can use a List as the value for a named parameter in the IN clause of an SQL statement 馃憤:

List<Person> persons=jdbcClient.sql("select id,name from persons where id in (:ids)")
  .param("ids", List.of(1,3))
  .query(Person.class)
  .list();

When I try the same with a positional parameter, I get an exception 馃様:
org.springframework.dao.DataIntegrityViolationException: PreparedStatementCallback; SQL [select id,name from persons where id in (?)]; Data conversion error converting "JAVA_OBJECT to BIGINT"; SQL statement:
select id,name from persons where id in (?)

List<Person> persons=jdbcClient.sql("select id,name from persons where id in (?)")
  .param(List.of(1,3))
  .query(Person.class)
  .list();

I put a project on https://github.com/hansdesmet/jdbcclientproposal2 that shows the problem.

Is it possible to make this work with a positional parameter ?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Sep 13, 2023
@sbrannen sbrannen added the in: data Issues in data modules (jdbc, orm, oxm, tx) label Sep 13, 2023
@sbrannen sbrannen changed the title Make the JdbcClient accept a List as the parameter of the IN clause of an SQL statement when using positional parameters Make JdbcClient accept a List as the parameter of the IN clause of an SQL statement when using positional parameters Sep 13, 2023
@sbrannen
Copy link
Member

Thanks for raising the issue. We'll look into it.

@sbrannen sbrannen added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Sep 13, 2023
@sbrannen sbrannen added this to the 6.1.x milestone Sep 13, 2023
@jhoeller
Copy link
Contributor

I'm afraid this is not idiomatic with positional parameters since the given SQL statement remains unmodified there, without any expansion of placeholders. Just like with direct JDBC usage, the parameter positions need to match the exact "?" placeholders in the SQL statement.

That said, we could detect a provided Collection parameter in param(Object) and raise a meaningful exception, indicating that such parameter expansion is only supported with named parameters.

@jhoeller jhoeller changed the title Make JdbcClient accept a List as the parameter of the IN clause of an SQL statement when using positional parameters JdbcClient should reject a List as the parameter of the IN clause of an SQL statement when using positional parameters Sep 13, 2023
@jhoeller jhoeller self-assigned this Sep 13, 2023
@jhoeller jhoeller modified the milestones: 6.1.x, 6.1.0-M5 Sep 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

4 participants