Vertx - How to pass ArrayList to SELECT query with IN

563 views Asked by At

I have a SELECT statement like SELECT * FROM table where id=? and key IN (?,?..?). For INSERT,UPDATE we have this batchWithParams, how can I do this for SELECT. I am using JDBC driver for MySQL database. The array list is like so,

 BATCH [[100,4,11], [150,4,12]]
1

There are 1 answers

0
Felipe Desiderati On

Why not just convert the ArrayList into String? Like this:

 suspend fun getUsersByEmail(emails: Iterable<String>): List<User> {
    return mysqlPool.getConnectionAwait().use { conn ->
        conn.query(
            """
                SELECT * FROM users
                WHERE email IN (${emails.joinToString(separator = ", ") { "'$it'" }})
            """
        ).executeAwait().map { row ->
            User(
                row.getLong("id"),
                row.getString("email")
            )
        }
    }
}