0

I got a SQL Query with a named parameter :featureIds:

const val selectGeoCoordinates = "SELECT gg.feature_id, gg.latitude, gg.longitude, gg.order, gg.group FROM geo.geo_coordinates as gg WHERE gg.feature_id IN (:featureIds);"

When I try to bind a list of Ints to the named parameter

val values: List<Int> = listOf(4138, 1752, 1161, 371, 1860, 5498)
val geoCoordinatesList = geo.withHandle<List<GeoCoordinates>, Exception> { handle ->
            handle.createQuery(selectGeoCoordinates).bindList("featureIds", values)
                    .mapTo<GeoCoordinates>().list()
        }

I get the following exception on execution:

Caused by: org.jdbi.v3.core.statement.UnableToExecuteStatementException: Unable to execute, no named parameter matches 'featureIds'. [statement:"SELECT gg.feature_id, gg.latitude, gg.longitude, gg.order, gg.group FROM geo.geo_coordinates as gg WHERE gg.feature_id IN :featureIds ;", rewritten:"SELECT gg.feature_id, gg.latitude, gg.longitude, gg.order, gg.group FROM geo.geo_coordinates as gg WHERE gg.feature_id IN :featureIds ;", parsed:"ParsedSql{sql='SELECT gg.feature_id, gg.latitude, gg.longitude, gg.order, gg.group FROM geo.geo_coordinates as gg WHERE gg.feature_id IN ? ;', parameters=ParsedParameters{positional=false, parameterNames=[featureIds]}}", arguments:{ positional:{}, named:{__featureIds_0:4138,__featureIds_4:1860,__featureIds_3:371,__featureIds_2:1161,__featureIds_1:1752,__featureIds_5:5498}, finder:[]}]
at org.jdbi.v3.core.statement.ArgumentBinder.lambda$bindNamed$0(ArgumentBinder.java:58)
at java.util.Optional.orElseThrow(Optional.java:290)
at org.jdbi.v3.core.statement.ArgumentBinder.bindNamed(ArgumentBinder.java:57)
at org.jdbi.v3.core.statement.ArgumentBinder.bind(ArgumentBinder.java:27)
at org.jdbi.v3.core.statement.SqlStatement.internalExecute(SqlStatement.java:1443)
at org.jdbi.v3.core.result.ResultProducers.lambda$getResultSet$2(ResultProducers.java:59)
at org.jdbi.v3.core.result.ResultIterable.lambda$of$0(ResultIterable.java:53)
at org.jdbi.v3.core.result.ResultIterable.stream(ResultIterable.java:141)
at org.jdbi.v3.core.result.ResultIterable.collect(ResultIterable.java:197)
at org.jdbi.v3.core.result.ResultIterable.list(ResultIterable.java:186)

Which puzzles me... If I use .bind("featureIds", 4138) it works, therefore the naming is correct.

Johannes Zink
  • 495
  • 4
  • 13

1 Answers1

0

You need to use @UseStringTemplate3StatementLocator to bind list parameters in JDBI. Please see more on the link below:

Jdbi - how to bind a list parameter in Java?

André Barbosa
  • 684
  • 1
  • 6
  • 14
  • 1
    I found a weird and simple solution... When I put the named parameter for list in `` it works. `"SELECT gg.feature_id, gg.latitude, gg.longitude, gg.order, gg.group FROM geo.geo_coordinates as gg WHERE gg.feature_id IN ();"` Your link hinted me in the direction as this is the way the StringTemplate3 does it – Johannes Zink Feb 02 '18 at 06:25