createStmt.addBatch("""
CREATE TABLE groups (
group_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
+ pos INT default 0,
name VARCHAR
)
""".trimIndent())
CREATE TABLE feedcollections (
feedcollection_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
group_id INT NOT NULL REFERENCES groups(group_id) ON DELETE CASCADE,
+ pos INT default 0,
name VARCHAR,
update_interval INT
)
println("Database schema already exists.")
}
}
+
+ public fun getFeedTree() : List<FeedGroup> {
+ val groups = mutableListOf<FeedGroup>()
+
+ connection.createStatement().use { stmt ->
+ val rs = stmt.executeQuery("""
+ select
+ g.group_id,
+ g.name as group_name,
+ f.feedcollection_id,
+ f.name as feed_name
+ from groups g
+ left join feedcollections f on g.group_id = f.group_id
+ order by g.pos, f.pos
+ """.trimIndent())
+
+ var currentGroup: FeedGroup? = null
+ while(rs.next()) {
+ val groupId = rs.getInt("group_id")
+ val groupName = rs.getString("group_name")
+ val feedId = rs.getInt("feedcollection_id")
+ val feedName = rs.getString("feed_name")
+
+ if(currentGroup == null || currentGroup.id != groupId) {
+ currentGroup = FeedGroup(groupId, groupName)
+ groups.add(currentGroup)
+ }
+
+ currentGroup.feeds.add(FeedCollection(feedId, feedName))
+ }
+ rs.close()
+ }
+
+ return groups
+ }
}
init {
val db = Database
- // testcode
- val sublist1 = SubList<FeedCollection>()
- sublist1.header = "Test 1"
- val sublist1data = list<FeedCollection>("sublist1data")
- val col1 = FeedCollection("Test Feed")
- sublist1data.add(col1)
- sublist1.value = sublist1data
- feeds.add(sublist1)
+ val groups = db.getFeedTree()
+ groups.forEach {
+ val sublist = SubList<FeedCollection>()
+ sublist.header = it.name
+ val feedlist = list<FeedCollection>()
+ it.feeds.forEach { fc ->
+ feedlist.add(fc)
+ }
+ sublist.value = feedlist
+ feeds.add(sublist)
+ }
}
}
\ No newline at end of file