How can I implement a saveAll
method like in ReactiveCrudRepository
using DatabaseClient
?
That's my implementation - Sadly it doesn't work.
@Repository
class AppStartRepo(val client: DatabaseClient) {
suspend fun saveAll(starts: List<AppStart>) {
val builder = StringBuilder()
builder.append("INSERT INTO app_start (device_id, platform, last_update) VALUES ")
for (index in starts.indices) {
builder.append("(?")
builder.append(index * 3)
builder.append(",?")
builder.append(index * 3 + 1)
builder.append(",?")
builder.append(index * 3 + 2)
builder.append(")")
}
var querySpec = client.sql(builder.toString())
for (index in starts.indices) {
val start = starts[index]
querySpec = querySpec.bind(index * 3, start.deviceId)
.bind(index * 3 + 1, start.platform.toString())
.bind(index * 3 + 2, start.lastUpdate)
}
querySpec.await()
}
}
Use
Statement.add
method to bind multiple parameters.See my example in this post.