]> uap-core.de Git - rssreader.git/commitdiff
use merge into to add items
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Sat, 6 Sep 2025 14:35:25 +0000 (16:35 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Sat, 6 Sep 2025 14:35:25 +0000 (16:35 +0200)
rss-application/src/main/kotlin/de/unixwork/rssreader/Database.kt

index de2816879efe75962e3c44b1a14a587dfa518164..e6677895e0acef61c85be9b4cf4deeda563ad34f 100644 (file)
@@ -327,10 +327,23 @@ object Database {
     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)
@@ -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()