https://kotlinlang.org logo
#exposed
Title
# exposed
b

bjonnh

01/03/2019, 7:58 PM
is there a better way to write that:
Copy code
kotlin
fun getCitation(code: String, offset: Int = 0): Citation? {
        var result: Citation? = null
        transaction(db) {
            result = Citation.wrapRows(Citations
                .select { Citations.code eq code }.limit(1)).toList().getOrNull(0)
        }
        return result
    }
t

tKw

01/04/2019, 1:28 AM
Without knowing more about your code, I created a generalized solution that is similar to ones we are using in one of our current projects. This may give you some ideas for how to structure it. Of course, there are most likely other ways, and I have only been using Kotlin for 4-5 months.
Copy code
object Citations : IntIdTable("citations") {
    data class Citation(val id: Int, val code: String, val citation: String)

    val code = text("code").uniqueIndex()
    val citation = text("citation")

    private fun toCitation(row: ResultRow): Citation =
        Citation(
            row[Citations.id].value,
            row[Citations.code],
            row[Citations.citation]
        )

    fun getCitation(code: String): Citation? =
        transaction {
            Citations.select { Citations.code eq code }
                .mapNotNull { toCitation(it) }
                .singleOrNull()
        }
}
👍 1
t

tapac

01/09/2019, 1:55 PM
@bjonnh, maybe
Copy code
fun getCitation(code: String, offset: Int = 0): Citation? 
= transaction(db) {
    Citation.find { Citations.code eq code }.limit(1)).toList().singleOrNull()
    }
2 Views