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

Can't bind list named parameter if the alias contains a dot #2471

Closed
uraimo opened this issue Aug 17, 2023 · 1 comment · Fixed by #2481
Closed

Can't bind list named parameter if the alias contains a dot #2471

uraimo opened this issue Aug 17, 2023 · 1 comment · Fixed by #2481
Assignees
Labels

Comments

@uraimo
Copy link

uraimo commented Aug 17, 2023

Hi and thanks for JDBI,
I've noticed that bindList doesn't work correctly if the name of a list named parameters contains a dot, e.g. <like.this>, it seems that when building the query the parameter list is not being replaced in the query in this case. Normal named parameters with a dot work without any issues.

For example, this snippet works:

    Jdbi jdbi = Jdbi.create("jdbc:h2:mem:test;Mode=Oracle;DB_CLOSE_DELAY=-1"); // (H2 in-memory database)
    jdbi.setSqlLogger(new Slf4JSqlLogger());

    List<String> names = jdbi.withHandle(handle -> {
        handle.execute("CREATE TABLE \"user\" (id INTEGER PRIMARY KEY, name_id VARCHAR)");

        handle.createUpdate("INSERT INTO \"user\" (id, name_id) VALUES (?, ?)")
                .bind(0, 1) // 0-based parameter indexes
                .bind(1, "Bob")
                .execute();

        handle.createUpdate("INSERT INTO \"user\" (id, name_id) VALUES (:id, :name)")
                .bind("id", 2)
                .bind("name", "Clarice")
                .execute();


        List<String> res = handle.createQuery("SELECT name_id FROM \"user\" WHERE ((id >:dot.id) AND name_id IN (<names>) AND id >:dot.id)")
                .bindList("names",List.of("1","2","Bob","Clarice"))
                .bind("dot.id",-1)
                .mapTo(String.class)
                .list();

But when I rename names to nam.es:

        List<String> res = handle.createQuery("SELECT name_id FROM \"user\" WHERE ((id >:dot.id) AND name_id IN (<nam.es>) AND id >:dot.id)")
                .bindList("nam.es",List.of("1","2","Bob","Clarice"))
                .bind("dot.id",-1)
                .mapTo(String.class)
                .list();

The example above fails with an exception (generic H2 exception):

  org.jdbi.v3.core.statement.UnableToCreateStatementException: org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement "SELECT name_id FROM ""user"" WHERE ((id >?) AND name_id IN ([*]<nam.es>) AND id >?)"; expected "INTERSECTS (, NOT, EXISTS, UNIQUE, INTERSECTS"; SQL statement:
  SELECT name_id FROM "user" WHERE ((id >?) AND name_id IN (<nam.es>) AND id >?) [42001-220] [statement:"SELECT name_id FROM "user" WHERE ((id >:dot.id) AND name_id IN (<nam.es>) AND id >:dot.id)", arguments:{positional:{}, named:{dot.id:-1,__nam.es_1:2,__nam.es_2:Bob,__nam.es_3:Clarice,__nam.es_0:1}, finder:[]}]
  
      at org.jdbi.v3.core.statement.SqlStatement.internalExecute(SqlStatement.java:1789)
      at org.jdbi.v3.core.result.ResultProducers.lambda$createResultBearing$3(ResultProducers.java:94)
      at org.jdbi.v3.core.result.internal.ResultSetResultIterable.iterator(ResultSetResultIterable.java:51)
      at org.jdbi.v3.core.result.ResultIterable.stream(ResultIterable.java:266)
      at org.jdbi.v3.core.result.ResultIterable.collect(ResultIterable.java:340)

I would expect to see something like AND name_id IN (__nam.es_0,__nam.es_1, __nam.es_2,__nam.es_3) but it seems that the replacement is not taking place.
Luckily, just not using dots for those aliases works as a workaround.

@hgschmie
Copy link
Contributor

hgschmie commented Sep 1, 2023

This is a lexer limitation. Technically not a bug but just a limitation. I will add a fix that will allow the . to be a valid character in the default ("DefinedAttributeTemplateEngine") template engine.

hgschmie added a commit to hgschmie/jdbi that referenced this issue Sep 1, 2023
@hgschmie hgschmie self-assigned this Sep 1, 2023
@hgschmie hgschmie added the bug label Sep 1, 2023
hgschmie added a commit to hgschmie/jdbi that referenced this issue Sep 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging a pull request may close this issue.

2 participants