Version v1.18 of the documentation is no longer actively maintained. The site that you are currently viewing is an archived snapshot. For up-to-date documentation, see the latest version.

Query Builders

Overview

A function that builds part of a query is called a builder.

All builder functions are defined in org.komapper.core.dsl.query.

where

The where function builds a Where declaration.

val salaryWhere = where {
  e.salary greater BigDecimal(1_000)
}
val query: Query<List<Employee>> = QueryDsl.from(e).where(salaryWhere)

on

The on function builds a On declaration.

val departmentIdOn = on {
    e.departmentId eq d.departmentId
}
val query: Query<List<Employee>> = QueryDsl.from(e).innerJoin(d, departmentIdOn)

having

The having function builds a Having declaration.

val countHaving = having {
    count() greater 3
}
val query: Query<List<Int?>> = QueryDsl.from(e)
    .groupBy(e.departmentId)
    .having(countHaving)
    .select(e.departmentId)

set

The set function builds an Assignment declaration.

val addressAssignment = set(a) {
    a.street eq "STREET 16"
}
val query: Query<Int> = QueryDsl.update(a).set(addressAssignment).where {
    a.addressId eq 1
}

values

The values function builds an Assignment declaration.

val addressAssignment = values(a) {
    a.street eq "STREET 16"
}
val query: Query<Pair<Int, Int?>> = QueryDsl.insert(a).values(addressAssignment)

join

The join function builds a Join element.

The Join element can be passed to the innerJoin or leftJoin functions:

val departmentJoin = join(d) {
    e.departmentId eq d.departmentId
}

val query1: Query<List<Employee>> = QueryDsl.from(e).innerJoin(departmentJoin)
val query2: Query<List<Employee>> = QueryDsl.from(e).leftJoin(departmentJoin)

groupBy

The groupBy function builds a list of column expressions.

val groupByDepartmentId = groupBy(e.departmentId)
val query: Query<List<Int?>> = QueryDsl.from(e)
    .groupBy(groupByDepartmentId)
    .having {
        count() greater 3
    }
    .select(e.departmentId)

orderBy

The orderBy function builds a list of sort expressions.

val orderBySalaryAndNo = orderBy(e.salary, e.employeeNo)
val query: Query<List<Employee>> = QueryDsl.from(e).orderBy(orderBySalaryAndNo)
Last modified March 11, 2022: Update the title and linkTitle (cf9ed4f)