public fun addItems(items: Collection<Item>) {
dataSource.connection.use { conn ->
conn.prepareStatement("""
- insert into items (feed_id, title, link, category, description, author, pub_date, updated, guid, contentText, contentHTML)
- 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
+ merge into items i
+ using (select cast(? as int) as feed_id, cast(? as varchar) as title, cast(? as varchar) as link, cast(? as varchar) as category, cast(? as varchar) as description, cast(? as varchar) as author, cast(? as timestamp) as pub_date, cast(? as timestamp) as updated, cast(? as varchar) as guid, cast(? as clob) as contentText, cast(? as clob) as contentHtml) v
+ on (i.feed_id = v.feed_id and i.guid = v.guid and coalesce(i.updated, i.pub_date, '1970-01-01') = coalesce(v.updated, v.pub_date, '1970-01-01'))
+ when matched then
+ update set
+ i.title = v.title,
+ i.link = v.link,
+ i.category = v.category,
+ i.description = v.description,
+ i.author = v.author,
+ i.pub_date = v.pub_date,
+ i.updated = v.updated,
+ i.contentText = v.contentText,
+ i.contentHtml = v.contentHtml
+ when not matched then
+ insert (feed_id, title, link, category, description, author, pub_date, updated, guid, contentText, contentHtml)
+ values (v.feed_id, v.title, v.link, v.category, v.description, v.author, v.pub_date, v.updated, v.guid, v.contentText, v.contentHtml)
""".trimIndent()).use { stmt ->
items.forEach { item ->
stmt.setInt(1, item.feedId)
stmt.setString(9, item.guid)
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()