mirror of
https://github.com/jlengrand/exposed-wiki.git
synced 2026-03-10 08:11:18 +00:00
1.6 KiB
1.6 KiB
The DSL (Domain Specific Language) API of Exposed, is similar to actual SQL statements with type safety that Kotlin offers.
A DB table is represented by an object inherited from org.jetbrains.exposed.sql.Table like that:
object StarWarsFilms : Table() {
val id: Column<Int> = integer("id").autoIncrement().primaryKey()
val sequelId: Column<Int> = integer("sequel_id").uniqueIndex()
val name: Column<String> = varchar("name", 50)
}
Tables that contains Int id with the name id can be declared like that:
object StarWarsFilms : IntIdTable() {
val sequelId: Column<Int> = integer("sequel_id").uniqueIndex()
val name: Column<String> = varchar("name", 50)
}
Basic CRUD operations
Create
val id = StarWarsFilms.insert {
it[name] = "The Last Jedi"
it[sequelId] = 8
} get StarWarsFilms.id
Read
val query: Query = StarWarsFilms.select { StarWarsFilms.sequelId eq 8 }
Query inherit Iterable so it is possible to traverse it with map/foreach etc'
Update
StarWarsFilms.update ({ StarWarsFilms.sequelId eq 8 }) {
it[name] = "Episode VIII – The Last Jedi"
}
Delete
StarWarsFilms.deleteWhere { StarWarsFilms.sequelId eq 8 }
Where expression
Query expression (where) expects a boolean operator (ie: Op<Boolean>).
Allowed conditions are:
eq - (==)
neq - (!=)
isNull()
isNotNull()
less - (<)
lessEq - (<=)
greater - (>)
greaterEq - (>=)
like - (=~)
notLike - (!~)
regexp
notRegexp
inList
notInList
between
match (MySQL MATCH AGAINST)
Allowed logical conditions are:
and
or