]> uap-core.de Git - rssreader.git/commitdiff
add db schema
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Mon, 11 Aug 2025 19:25:53 +0000 (21:25 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Mon, 11 Aug 2025 19:25:53 +0000 (21:25 +0200)
rss-application/pom.xml
rss-application/src/main/kotlin/de/unixwork/rssreader/Database.kt [new file with mode: 0644]
rss-application/src/main/kotlin/de/unixwork/rssreader/FeedSourceList.kt

index 2599412f6b4c82634d551d521f6423adb1326088..5ef160aa76fec0590535e0dd1c53ac52354d5d3d 100644 (file)
             <artifactId>ui-kotlin</artifactId>
             <version>1.0-SNAPSHOT</version>
         </dependency>
+
+        <dependency>
+            <groupId>com.h2database</groupId>
+            <artifactId>h2</artifactId>
+            <version>2.3.232</version>
+        </dependency>
     </dependencies>
 
 </project>
diff --git a/rss-application/src/main/kotlin/de/unixwork/rssreader/Database.kt b/rss-application/src/main/kotlin/de/unixwork/rssreader/Database.kt
new file mode 100644 (file)
index 0000000..97d9935
--- /dev/null
@@ -0,0 +1,67 @@
+package de.unixwork.rssreader
+
+import java.sql.Connection
+import java.sql.DriverManager
+
+object Database {
+    val connection: Connection
+
+    init {
+        connection = DriverManager.getConnection("jdbc:h2:~/.rssreader/feeds")
+        ensureSchema(connection)
+    }
+
+    private fun ensureSchema(conn: Connection) {
+        val stmt = conn.createStatement()
+        val rs = stmt.executeQuery(
+            "SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'FEEDCOLLECTIONS'"
+        )
+
+        var tableExists = false
+        if (rs.next()) {
+            tableExists = rs.getInt(1) > 0
+        }
+        rs.close()
+        stmt.close()
+
+        if (!tableExists) {
+            println("Database empty — creating tables...")
+            conn.createStatement().use { createStmt ->
+                createStmt.addBatch("""
+                    CREATE TABLE feedcollections (
+                        feedcollection_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
+                        name VARCHAR,
+                        update_interval INT
+                    )
+                """.trimIndent())
+
+                createStmt.addBatch("""
+                    CREATE TABLE feeds(
+                        feed_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
+                        feedcollection_id INT NOT NULL REFERENCES feedcollections(feedcollection_id) ON DELETE CASCADE,
+                        url VARCHAR NOT NULL,
+                        last_update TIMESTAMP
+                    )
+                """.trimIndent())
+
+                createStmt.addBatch("""
+                    CREATE TABLE items (
+                        item_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
+                        feed_id INT NOT NULL REFERENCES feeds(feed_id),
+                        title VARCHAR NOT NULL,
+                        link VARCHAR NOT NULL,
+                        description CLOB,
+                        author VARCHAR,
+                        pub_date TIMESTAMP,
+                        guid VARCHAR UNIQUE,
+                        content CLOB
+                    )
+                """.trimIndent())
+
+                createStmt.executeBatch()
+            }
+        } else {
+            println("Database schema already exists.")
+        }
+    }
+}
index 2aca7ae04da54487cb52ff8673e2f26caee48793..fb5c2da9532bf0e7fef2519541c3f0608f2ea5e8 100644 (file)
@@ -7,6 +7,8 @@ class FeedSourceList : Document() {
     val feeds = this.sourcelist("feeds")
 
     init {
+        val db = Database
+
         // testcode
         val sublist1 = SubList<FeedCollection>()
         sublist1.header = "Test 1"