Skip to content

Queries

Select

Standard select query:

kotlin
val userFromDatabase = database.select(UserTable)
    .where { UserTable.uuid eq insertedUser.uuid }
    .map { it.toUser() }
    .first()

Other parameters:

ParameterExampleDescription
Distinctdistinct()Only return distinct rows
Limitlimit(10)Limit the number of rows returned
Limit with offsetlimit(10, offset = 5)Limit the number of rows returned with an offset
Order byorderBy(UserTable.name to SortOrder.ASC)Order the results by a column
Group bygroupBy(UserTable.name)Group the results by a column
Havinghaving { UserTable.name eq "Panda" }Filter the results after grouping
JoininnerJoin(INNER/LEFT/RIGHT/FULL, GuildTable.owner, UserTable.id)Join two tables
Sliceslice(UserTable.name, UserTable.age)Only return specific columns

Insert

Standard insert query:

kotlin
val insertedUser = database
    .insert(UserTable)
    .values(UnidentifiedUser(name = "Panda", age = 20))
    .map { userToInsert.withId(id = it[UserTable.id]) }
    .first()

You can also insert individual rows:

kotlin
val insertedUser = database
    .insert(UserTable)
    .values {
        it[UserTable.name] = "Panda"
        it[UserTable.age] = 20
    }
    .map { userToInsert.withId(id = it[UserTable.id]) }
    .first()

Update

Standard update query:

kotlin
val updatedRecords = database
    .update(UserTable) {
        it[UserTable.name] = "Giant Panda"
        it[UserTable.age] = UserTable.age * 2
    }
    .where { UserTable.id eq insertedUser.id }
    .execute()

Delete

Standard delete query:

kotlin
database
    .delete(UserTable)
    .where { UserTable.id eq id }
    .execute()