dataSource.connection.use { conn ->
conn.prepareStatement("""
insert into items (feed_id, title, link, category, description, author, pub_date, updated, guid, contentText, contentHTML)
- select ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
- where ? not in (select guid from items)
+ select ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? from dual
+ left join items I on I.guid = ? and I.feed_id = ? and coalesce(I.updated, I.pub_date, ?) = ?
+ where I.item_id is null
""".trimIndent()).use { stmt ->
items.forEach { item ->
stmt.setInt(1, item.feedId)
stmt.setString(10, item.contentText)
stmt.setString(11, item.contentHtml)
stmt.setString(12, item.guid)
+ stmt.setInt(13, item.feedId)
+ val itemDate = java.sql.Timestamp.valueOf(item.updated ?: item.pubDate ?: java.time.LocalDateTime.MIN)
+ stmt.setTimestamp(14, itemDate)
+ stmt.setTimestamp(15, itemDate)
stmt.addBatch()
}
stmt.executeBatch()