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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JdbcStatement named parameters should be usable more than once in a statement #2599

Closed
joev0000 opened this issue Dec 13, 2020 · 2 comments · Fixed by #2693
Closed

JdbcStatement named parameters should be usable more than once in a statement #2599

joev0000 opened this issue Dec 13, 2020 · 2 comments · Fixed by #2693
Assignees
Labels
DB client Helidon DB Client enhancement New feature or request P2 SE

Comments

@joev0000
Copy link

(or, it would be nice if named parameters in a statement didn't have to be unique)

I am trying to build a statement that uses the same value in multiple positions:

SELECT ID FROM MY_TABLE WHERE (FOO = :foo) OR (FOO > :foo AND BAR = :bar)

When executed with a map containing { foo=1, bar=2 }, this causes an exception

Caused by: io.helidon.dbclient.DbClientException: Query parameters missing in Map:
    at io.helidon.dbclient.jdbc@2.1.0/io.helidon.dbclient.jdbc.JdbcStatement.prepareNamedStatement(JdbcStatement.java:121)

There is a check in that method that makes sure the number of entries in the parameter map is the same as the number of parameter names as they appear in the query:

// SQL statement and provided parameters integrity check
if (namesOrder.size() > parameters.size()) {
    throw new DbClientException(namedStatementErrorMessage(namesOrder, parameters));
}

The debugger here tells me that the namesOrder contains ["foo", "foo", "bar"] but parameters contains {foo=1, bar=2}. and so the exception is raised.

Environment Details

  • Helidon Version: 2.1.0
  • Helidon SE
  • JDK version: 11.0.8
  • OS: Oracle Linux 7.8
  • Docker version (if applicable): N/A

Problem Description

Expected Behavior

JdbcStatement allows the use of a named parameter more than once in a statement.

Current Behavior

The exception is raised: DbClientException: Query parameters missing in Map

Caused by: io.helidon.dbclient.DbClientException: Query parameters missing in Map:
        at io.helidon.dbclient.jdbc@2.1.0/io.helidon.dbclient.jdbc.JdbcStatement.prepareNamedStatement(JdbcStatement.java:121)
        at io.helidon.dbclient.jdbc@2.1.0/io.helidon.dbclient.jdbc.JdbcStatement.lambda$build$3(JdbcStatement.java:76)
        at java.base/java.util.Optional.map(Optional.java:265)
        at io.helidon.dbclient.jdbc@2.1.0/io.helidon.dbclient.jdbc.JdbcStatement.build(JdbcStatement.java:76)
        at io.helidon.dbclient.jdbc@2.1.0/io.helidon.dbclient.jdbc.JdbcStatementQuery.lambda$doExecute$2(JdbcStatementQuery.java:95)
        ... 7 more

Steps to reproduce

Put this code in a web Service, set up a router to call it:

private void test(ServerRequest request, ServerResponse response) {
    db.execute(e -> e.createNamedQuery("test", "SELECT ID FROM MY_TABLE WHERE (FOO = :foo) OR (FOO > :foo AND BAR = :bar)
            .addParam("foo", 1)
            .addParam("bar", 2)
            .execute())
        .collectList()
        .thenAccept(l -> response.send(l.toString()))
        .exceptionallyAccept(t -> respondWithStackTrace(response, t));
}
@m0mus m0mus added DB client Helidon DB Client SE enhancement New feature or request P2 labels Dec 17, 2020
@Tomas-Kraus
Copy link
Member

Yes, this change makes sense for all namedQuery/DML methods. Maybe I made the check too much strict.

@Tomas-Kraus Tomas-Kraus linked a pull request Jan 28, 2021 that will close this issue
Tomas-Kraus added a commit to Tomas-Kraus/helidon that referenced this issue Feb 1, 2021
Signed-off-by: Tomas Kraus <Tomas.Kraus@oracle.com>
@Tomas-Kraus
Copy link
Member

Please let me know whether current master build works fine for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DB client Helidon DB Client enhancement New feature or request P2 SE
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants