Skip to content

Instantly share code, notes, and snippets.

fun main(args: Array<String>) {
Database.connect("jdbc:h2:mem:test", driver = "org.h2.Driver")
transaction {
// insert new city. SQL: INSERT INTO Cities (name) VALUES ('St. Petersburg')
val stPeteId = Cities.insert {
it[name] = "St. Petersburg"
} get Cities.id
// 'select *' SQL: SELECT Cities.id, Cities.name FROM Cities
@oshai
oshai / dsl.kt
Created February 19, 2018 19:00
object StarWarsFilms : IntIdTable() {
val sequelId: Column<Int> = integer("sequel_id").uniqueIndex()
val name: Column<String> = varchar("name", 50)
val director: Column<String> = varchar("director", 50)
}
data class StarWarsFilm(val sequelId: Int,
val name: String,
val director: String)
@oshai
oshai / ExposedLogging.kt
Created December 5, 2017 20:50
exposed logging
// Usage
transaction {
logger.addLogger(KotlinLoggingSqlLogger)
...
}
// Declaration
object KotlinLoggingSqlLogger : SqlLogger {
private val logger = KotlinLogging.logger { }
override
@oshai
oshai / upsert.kt
Last active December 6, 2017 08:24
The update or insert definition
fun execute(cities: List<City>) {
transaction {
Cities.batchInsertOnDuplicateKeyUpdate( cities, listOf(id, name) ) { batch, city ->
batch[Cities.id] = city.id
batch[Cities.name] = city.name
}
}
}
// The below code is just a copy-paste that should actually be in the lib
@oshai
oshai / table.kt
Last active December 5, 2017 20:44
definition of schema
// Table definition
object Cities : Table() {
val id = integer("id").autoIncrement().primaryKey()
val name = varchar("name", 50)
}
// Entity definition
data class City(
val id: Int,
val name: String
)