From: Olaf Wintermann Date: Sat, 6 Sep 2025 14:35:25 +0000 (+0200) Subject: use merge into to add items X-Git-Url: https://uap-core.de/gitweb/?a=commitdiff_plain;h=8d221d09cbb80d507fc52c02a73092b23a230387;p=rssreader.git use merge into to add items --- diff --git a/rss-application/src/main/kotlin/de/unixwork/rssreader/Database.kt b/rss-application/src/main/kotlin/de/unixwork/rssreader/Database.kt index de28168..e667789 100644 --- a/rss-application/src/main/kotlin/de/unixwork/rssreader/Database.kt +++ b/rss-application/src/main/kotlin/de/unixwork/rssreader/Database.kt @@ -327,10 +327,23 @@ object Database { public fun addItems(items: Collection) { 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) @@ -344,11 +357,6 @@ object Database { 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()