-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🧑💻 Teknikk for å kunne la DAO-er ta inn enten DataSource eller Session
- Loading branch information
Showing
14 changed files
with
100 additions
and
155 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
spesialist-felles/src/main/kotlin/no/nav/helse/db/QueryRunner.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package no.nav.helse.db | ||
|
||
import io.ktor.utils.io.core.use | ||
import kotliquery.Query | ||
import kotliquery.Row | ||
import kotliquery.Session | ||
import kotliquery.action.QueryAction | ||
import kotliquery.sessionOf | ||
import javax.sql.DataSource | ||
|
||
interface QueryRunner { | ||
fun <T> Query.single(mapping: (Row) -> T?): T? | ||
|
||
fun <T> Query.list(mapping: (Row) -> T?): List<T> | ||
|
||
fun Query.update(): Int | ||
} | ||
|
||
class MedSession(private val session: Session) : QueryRunner { | ||
override fun <T> Query.single(mapping: (Row) -> T?) = session.run(map(mapping).asSingle) | ||
|
||
override fun <T> Query.list(mapping: (Row) -> T?) = session.run(map(mapping).asList) | ||
|
||
override fun Query.update() = session.run(asUpdate) | ||
} | ||
|
||
class MedDataSource(private val dataSource: DataSource) : QueryRunner { | ||
private fun <T> QueryAction<T>.runInSession() = sessionOf(dataSource).use(::runWithSession) | ||
|
||
override fun <T> Query.single(mapping: (Row) -> T?) = map(mapping).asSingle.runInSession() | ||
|
||
override fun <T> Query.list(mapping: (Row) -> T?) = map(mapping).asList.runInSession() | ||
|
||
override fun Query.update() = asUpdate.runInSession() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.