Bind a list of objects to JDBI SQL. Kotlin

Viewed 315

I have a list of data object that looks as follows:

data class Detail (
    val type1: Type1
    val type2: Type2
)

val list: List<Detail> = arrayListOf(Detail,Detail,Detail)

How do I bind the the list of details into a JDBI SQL query. I know how to do it with a single data type but just not sure how this works when you are getting properties from the "Detail" data class.

eg:

it.createQuery(
                    """
                    SELECT
                        id
                    FROM
                        tbl_of_something
                    WHERE
                        type1
                    AND
                        type2
                    IN (<detail>)
                    """
                ).bindList("detail", list)
                    .mapTo(String::class.java)
                    .toSet()
                    .toList()
                    .sorted()
1 Answers

I figured it out, this can be solved by using bindBeanList() as follows:

it.createQuery(
                    """
                    SELECT
                        id
                    FROM
                        tbl_of_something
                    WHERE
                        (type1, type2)
                    IN (<detail>)
                    """
                ).bindBeanList("detail", list, listOf("type1", "type2"))
                    .mapTo(String::class.java)
                    .toSet()
                    .toList()
                    .sorted()```
Related