import java.sql.Statement
object Database {
- val connection: Connection
val dataSource: HikariDataSource
init {
- connection = DriverManager.getConnection("jdbc:h2:~/.rssreader/feeds")
-
val config = HikariConfig()
config.jdbcUrl = "jdbc:h2:~/.rssreader/feeds"
config.maximumPoolSize = 16
dataSource = HikariDataSource(config)
- ensureSchema(connection)
+ ensureSchema(dataSource.connection)
}
private fun ensureSchema(conn: Connection) {
public fun getFeedTree(context: Context) : MutableList<FeedGroup> {
val groups = mutableListOf<FeedGroup>()
- connection.createStatement().use { stmt ->
+ dataSource.connection.createStatement().use { stmt ->
val rs = stmt.executeQuery("""
select
g.group_id,
public fun newFeedGroup(context: Context, name: String) : FeedGroup {
var groupId = 0
- connection.prepareStatement("""
+ dataSource.connection.prepareStatement("""
insert into groups (pos, name) select coalesce(max(pos), 0)+1, ? from groups
""".trimIndent(), Statement.RETURN_GENERATED_KEYS).use { stmt ->
stmt.setString(1, name)
cert: String? = null) : FeedCollection
{
var feedcollectionId = -1
+ val connection = dataSource.connection
connection.prepareStatement("""
insert into feedcollections (group_id, pos, name) select ?, coalesce(max(pos), 0)+1, ? from groups
""".trimIndent(), Statement.RETURN_GENERATED_KEYS).use { stmt ->
public fun getItems(feedCollection: FeedCollection, maxItems: Int) : MutableList<Item> {
val items = mutableListOf<Item>()
- connection.prepareStatement("""
+ dataSource.connection.prepareStatement("""
select I.*, F.URL from items I
inner join feeds F on I.feed_id = F.feed_id
where F.feedcollection_id = ? order by pub_date desc limit ?
public fun getAllFeeds() : MutableList<Feed> {
val feeds = mutableListOf<Feed>()
- connection.prepareStatement("""
+ dataSource.connection.prepareStatement("""
select * from feeds
""".trimIndent()).use { stmt ->
stmt.executeQuery().use { rs ->