English | 简体中文

api-docs / org.ktorm.dsl

Package org.ktorm.dsl

Constructs strong-typed SQL DSL.

Types

NameSummary

AssignmentsBuilder

open class AssignmentsBuilder

Base class of DSL builders, provide basic functions used to build assignments for insert or update DSL.

BatchInsertStatementBuilder

class BatchInsertStatementBuilder<T : BaseTable<*>>

DSL builder for batch insert statements.

BatchUpdateStatementBuilder

class BatchUpdateStatementBuilder<T : BaseTable<*>>

DSL builder for batch update statements.

Query

class Query

Query is an abstraction of query operations and the core class of Ktorm’s query DSL.

QueryRowSet

class QueryRowSet : CachedRowSet

Special implementation of ResultSet, used to hold the Query results for Ktorm.

QuerySource

data class QuerySource

Represents a query source, used in the from clause of a query.

UpdateStatementBuilder

class UpdateStatementBuilder : AssignmentsBuilder

DSL builder for update statements.

Annotations

NameSummary

KtormDsl

annotation class KtormDsl

Marker annotation for Ktorm DSL builder classes.

Extensions for External Classes

NameSummary

kotlin.Boolean

kotlin.collections.Iterable

Functions

NameSummary

and


infix fun ColumnDeclaring<Boolean>.and(
    value: Boolean
): BinaryExpression<Boolean>

And operator, translated to the and keyword in SQL.

asc

Order this column or expression in ascending order.

asIterable

fun Query.asIterable(): Iterable<QueryRowSet>

Wrap this query as Iterable.

associate

fun <K, V> Query.associate(
    transform: (row: QueryRowSet) -> Pair<K, V>
): Map<K, V>

Return a Map containing key-value pairs provided by transform function applied to rows of the query.

associateBy

fun <K, V> Query.associateBy(
    keySelector: (row: QueryRowSet) -> K,
    valueTransform: (row: QueryRowSet) -> V
): Map<K, V>

Return a Map containing the values provided by valueTransform and indexed by keySelector functions applied to
rows of the query.

associateByTo

fun <K, V, M : MutableMap<in K, in V>> Query.associateByTo(
    destination: M,
    keySelector: (row: QueryRowSet) -> K,
    valueTransform: (row: QueryRowSet) -> V
): M

Populate and return the destination mutable map with key-value pairs, where key is provided by the keySelector
function and value is provided by the valueTransform function applied to rows of the query.

associateTo

fun <K, V, M : MutableMap<in K, in V>> Query.associateTo(
    destination: M,
    transform: (row: QueryRowSet) -> Pair<K, V>
): M

Populate and return the destination mutable map with key-value pairs provided by transform function applied to
each row of the query.

avg

fun <C : Number> avg(
    column: ColumnDeclaring<C>
): AggregateExpression<Double>

The avg function, translated to avg(column) in SQL.

avgDistinct

fun <C : Number> avgDistinct(
    column: ColumnDeclaring<C>
): AggregateExpression<Double>

The avg function with distinct, translated to avg(distinct column) in SQL.

batchInsert

fun <T : BaseTable<*>> Database.batchInsert(
    table: T,
    block: BatchInsertStatementBuilder<T>.() -> Unit
): IntArray

Construct insert expressions in the given closure, then batch execute them and return the effected
row counts for each expression.

batchUpdate

fun <T : BaseTable<*>> Database.batchUpdate(
    table: T,
    block: BatchUpdateStatementBuilder<T>.() -> Unit
): IntArray

Construct update expressions in the given closure, then batch execute them and return the effected
row counts for each expression.

between

infix fun <T : Comparable<T>> ColumnDeclaring<T>.between(
    range: ClosedRange<T>
): BetweenExpression<T>

Between operator, translated to between .. and .. in SQL.

cast

fun <T : Any> ColumnDeclaring<*>.cast(
    sqlType: SqlType<T>
): CastingExpression<T>

Cast the current column or expression to the given SqlType.

count

fun count(
    column: ColumnDeclaring<*>? = null
): AggregateExpression<Int>

The count function, translated to count(column) in SQL.

countDistinct

fun countDistinct(
    column: ColumnDeclaring<*>? = null
): AggregateExpression<Int>

The count function with distinct, translated to count(distinct column) in SQL.

crossJoin

fun QuerySource.crossJoin(
    right: BaseTable<*>,
    on: ColumnDeclaring<Boolean>? = null
): QuerySource

Join the right table and return a new QuerySource, translated to cross join in SQL.

delete

fun <T : BaseTable<*>> Database.delete(
    table: T,
    predicate: (T) -> ColumnDeclaring<Boolean>
): Int

Delete the records in the table that matches the given predicate.

deleteAll

fun Database.deleteAll(table: BaseTable<*>): Int

Delete all the records in the table.

desc

Order this column or expression in descending order, corresponding to the desc keyword in SQL.

div

infix operator fun <T : Number> ColumnDeclaring<T>.div(
    expr: ColumnDeclaring<T>
): BinaryExpression<T>

infix operator fun <T : Number> ColumnDeclaring<T>.div(
    value: T
): BinaryExpression<T>

infix operator fun <T : Number> T.div(
    expr: ColumnDeclaring<T>
): BinaryExpression<T>

Divide operator, translated to / in SQL.

eq

infix fun <T : Any> ColumnDeclaring<T>.eq(
    expr: ColumnDeclaring<T>
): BinaryExpression<Boolean>

infix fun <T : Any> ColumnDeclaring<T>.eq(
    value: T
): BinaryExpression<Boolean>

Equal operator, translated to = in SQL.

exists

fun exists(query: Query): ExistsExpression

Check if the given query has at least one result, translated to the exists keyword in SQL.

flatMap

fun <R> Query.flatMap(
    transform: (row: QueryRowSet) -> Iterable<R>
): List<R>

Return a single list of all elements yielded from results of transform function being invoked on each row
of the query.

flatMapIndexed

fun <R> Query.flatMapIndexed(
    transform: (index: Int, row: QueryRowSet) -> Iterable<R>
): List<R>

Return a single list of all elements yielded from results of transform function being invoked on each row
and its index in the query.

flatMapIndexedTo

fun <R, C : MutableCollection<in R>> Query.flatMapIndexedTo(
    destination: C,
    transform: (index: Int, row: QueryRowSet) -> Iterable<R>
): C

Append all elements yielded from results of transform function being invoked on each row and its index
in the query, to the given destination.

flatMapTo

fun <R, C : MutableCollection<in R>> Query.flatMapTo(
    destination: C,
    transform: (row: QueryRowSet) -> Iterable<R>
): C

Append all elements yielded from results of transform function being invoked on each row of the query,
to the given destination.

fold

fun <R> Query.fold(
    initial: R,
    operation: (acc: R, row: QueryRowSet) -> R
): R

Accumulate value starting with initial value and applying operation to current accumulator value and each row.

foldIndexed

fun <R> Query.foldIndexed(
    initial: R,
    operation: (index: Int, acc: R, row: QueryRowSet) -> R
): R

Accumulate value starting with initial value and applying operation to current accumulator value and each row
with its index in the original query results.

forEach

fun Query.forEach(action: (row: QueryRowSet) -> Unit): Unit

Perform the given action on each row of the query.

forEachIndexed

fun Query.forEachIndexed(
    action: (index: Int, row: QueryRowSet) -> Unit
): Unit

Perform the given action on each row of the query, providing sequential index with the row.

forUpdate

fun Query.forUpdate(): Query

Indicate that this query should aquire the record-lock, the generated SQL would be select ... for update.

from

fun Database.from(table: BaseTable<*>): QuerySource

Wrap the specific table as a QuerySource.

greater

infix fun <T : Comparable<T>> ColumnDeclaring<T>.greater(
    expr: ColumnDeclaring<T>
): BinaryExpression<Boolean>

infix fun <T : Comparable<T>> ColumnDeclaring<T>.greater(
    value: T
): BinaryExpression<Boolean>

infix fun <T : Comparable<T>> T.greater(
    expr: ColumnDeclaring<T>
): BinaryExpression<Boolean>

Greater operator, translated to > in SQL.

greaterEq

infix fun <T : Comparable<T>> ColumnDeclaring<T>.greaterEq(
    expr: ColumnDeclaring<T>
): BinaryExpression<Boolean>

infix fun <T : Comparable<T>> ColumnDeclaring<T>.greaterEq(
    value: T
): BinaryExpression<Boolean>

infix fun <T : Comparable<T>> T.greaterEq(
    expr: ColumnDeclaring<T>
): BinaryExpression<Boolean>

Greater-eq operator, translated to >= in SQL.

groupBy

fun Query.groupBy(vararg columns: ColumnDeclaring<*>): Query

Specify the group by clause of this query using the given columns or expressions.

having

fun Query.having(
    block: () -> ColumnDeclaring<Boolean>
): Query

Specify the having clause of this query using the expression returned by the given callback function.

inList

fun <T : Any> ColumnDeclaring<T>.inList(
    vararg list: T
): InListExpression<T>

infix fun <T : Any> ColumnDeclaring<T>.inList(
    list: Collection<T>
): InListExpression<T>

infix fun <T : Any> ColumnDeclaring<T>.inList(
    query: Query
): InListExpression<T>

In-list operator, translated to the in keyword in SQL.

innerJoin

fun QuerySource.innerJoin(
    right: BaseTable<*>,
    on: ColumnDeclaring<Boolean>? = null
): QuerySource

Join the right table and return a new QuerySource, translated to inner join in SQL.

insert

fun <T : BaseTable<*>> Database.insert(
    table: T,
    block: AssignmentsBuilder.(T) -> Unit
): Int

Construct an insert expression in the given closure, then execute it and return the effected row count.

insertAndGenerateKey

fun <T : BaseTable<*>> Database.insertAndGenerateKey(
    table: T,
    block: AssignmentsBuilder.(T) -> Unit
): Any

Construct an insert expression in the given closure, then execute it and return the auto-generated key.

insertTo

fun Query.insertTo(
    table: BaseTable<*>,
    vararg columns: Column<*>
): Int

Insert the current Query‘s results into the given table, useful when transfer data from a table to another table.

isNotNull

Check if the current column or expression is not null, translated to is not null in SQL.

isNull

Check if the current column or expression is null, translated to is null in SQL.

joinReferencesAndSelect

fun QuerySource.joinReferencesAndSelect(): Query

Return a new-created Query object, left joining all the reference tables, and selecting all columns of them.

joinTo

fun <A : Appendable> Query.joinTo(
    buffer: A,
    separator: CharSequence = ", ",
    prefix: CharSequence = "",
    postfix: CharSequence = "",
    limit: Int = -1,
    truncated: CharSequence = "...",
    transform: (row: QueryRowSet) -> CharSequence
): A

Append the string from all rows separated using separator and using the given prefix and postfix if supplied.

joinToString

fun Query.joinToString(
    separator: CharSequence = ", ",
    prefix: CharSequence = "",
    postfix: CharSequence = "",
    limit: Int = -1,
    truncated: CharSequence = "...",
    transform: (row: QueryRowSet) -> CharSequence
): String

Create a string from all rows separated using separator and using the given prefix and postfix if supplied.

leftJoin

fun QuerySource.leftJoin(
    right: BaseTable<*>,
    on: ColumnDeclaring<Boolean>? = null
): QuerySource

Join the right table and return a new QuerySource, translated to left join in SQL.

less

infix fun <T : Comparable<T>> ColumnDeclaring<T>.less(
    expr: ColumnDeclaring<T>
): BinaryExpression<Boolean>

infix fun <T : Comparable<T>> ColumnDeclaring<T>.less(
    value: T
): BinaryExpression<Boolean>

infix fun <T : Comparable<T>> T.less(
    expr: ColumnDeclaring<T>
): BinaryExpression<Boolean>

Less operator, translated to < in SQL.

lessEq

infix fun <T : Comparable<T>> ColumnDeclaring<T>.lessEq(
    expr: ColumnDeclaring<T>
): BinaryExpression<Boolean>

infix fun <T : Comparable<T>> ColumnDeclaring<T>.lessEq(
    value: T
): BinaryExpression<Boolean>

infix fun <T : Comparable<T>> T.lessEq(
    expr: ColumnDeclaring<T>
): BinaryExpression<Boolean>

Less-eq operator, translated to <= in SQL.

like

infix fun ColumnDeclaring<*>.like(
    expr: ColumnDeclaring<String>
): BinaryExpression<Boolean>

infix fun ColumnDeclaring<*>.like(
    value: String
): BinaryExpression<Boolean>

Like operator, translated to the like keyword in SQL.

limit

fun Query.limit(offset: Int, limit: Int): Query

Specify the pagination parameters of this query.

map

fun <R> Query.map(
    transform: (row: QueryRowSet) -> R
): List<R>

Return a list containing the results of applying the given transform function to each row of the query.

mapIndexed

fun <R> Query.mapIndexed(
    transform: (index: Int, row: QueryRowSet) -> R
): List<R>

Return a list containing the results of applying the given transform function to each row and its index.

mapIndexedNotNull

fun <R : Any> Query.mapIndexedNotNull(
    transform: (index: Int, row: QueryRowSet) -> R?
): List<R>

Return a list containing only the non-null results of applying the given transform function to each row
and its index.

mapIndexedNotNullTo

fun <R : Any, C : MutableCollection<in R>> Query.mapIndexedNotNullTo(
    destination: C,
    transform: (index: Int, row: QueryRowSet) -> R?
): C

Apply the given transform function the each row and its index and append only the non-null results to
the given destination.

mapIndexedTo

fun <R, C : MutableCollection<in R>> Query.mapIndexedTo(
    destination: C,
    transform: (index: Int, row: QueryRowSet) -> R
): C

Apply the given transform function the each row and its index and append the results to the given destination.

mapNotNull

fun <R : Any> Query.mapNotNull(
    transform: (row: QueryRowSet) -> R?
): List<R>

Return a list containing only the non-null results of applying the given transform function to each row of
the query.

mapNotNullTo

fun <R : Any, C : MutableCollection<in R>> Query.mapNotNullTo(
    destination: C,
    transform: (row: QueryRowSet) -> R?
): C

Apply the given transform function to each row of the query and append only the non-null results to
the given destination.

mapTo

fun <R, C : MutableCollection<in R>> Query.mapTo(
    destination: C,
    transform: (row: QueryRowSet) -> R
): C

Apply the given transform function to each row of the query and append the results to the given destination.

max

fun <C : Comparable<C>> max(
    column: ColumnDeclaring<C>
): AggregateExpression<C>

The max function, translated to max(column) in SQL.

maxDistinct

fun <C : Comparable<C>> maxDistinct(
    column: ColumnDeclaring<C>
): AggregateExpression<C>

The max function with distinct, translated to max(distinct column) in SQL.

min

fun <C : Comparable<C>> min(
    column: ColumnDeclaring<C>
): AggregateExpression<C>

The min function, translated to min(column) in SQL.

minDistinct

fun <C : Comparable<C>> minDistinct(
    column: ColumnDeclaring<C>
): AggregateExpression<C>

The min function with distinct, translated to min(distinct column) in SQL.

minus

infix operator fun <T : Number> ColumnDeclaring<T>.minus(
    expr: ColumnDeclaring<T>
): BinaryExpression<T>

infix operator fun <T : Number> ColumnDeclaring<T>.minus(
    value: T
): BinaryExpression<T>

infix operator fun <T : Number> T.minus(
    expr: ColumnDeclaring<T>
): BinaryExpression<T>

Minus operator, translated to - in SQL.

not

Negative operator, translated to the not keyword in SQL.

notBetween

infix fun <T : Comparable<T>> ColumnDeclaring<T>.notBetween(
    range: ClosedRange<T>
): BetweenExpression<T>

Not-between operator, translated to not between .. and .. in SQL.

notEq

infix fun <T : Any> ColumnDeclaring<T>.notEq(
    expr: ColumnDeclaring<T>
): BinaryExpression<Boolean>

infix fun <T : Any> ColumnDeclaring<T>.notEq(
    value: T
): BinaryExpression<Boolean>

Not-equal operator, translated to <> in SQL.

notExists

fun notExists(query: Query): ExistsExpression

Check if the given query doesn’t have any results, translated to the not exists keyword in SQL.

notInList

fun <T : Any> ColumnDeclaring<T>.notInList(
    vararg list: T
): InListExpression<T>

infix fun <T : Any> ColumnDeclaring<T>.notInList(
    list: Collection<T>
): InListExpression<T>

infix fun <T : Any> ColumnDeclaring<T>.notInList(
    query: Query
): InListExpression<T>

Not-in-list operator, translated to the not in keyword in SQL.

notLike

infix fun ColumnDeclaring<*>.notLike(
    expr: ColumnDeclaring<String>
): BinaryExpression<Boolean>

infix fun ColumnDeclaring<*>.notLike(
    value: String
): BinaryExpression<Boolean>

Not like operator, translated to the not like keyword in SQL.

or


infix fun ColumnDeclaring<Boolean>.or(
    value: Boolean
): BinaryExpression<Boolean>

Or operator, translated to the or keyword in SQL.

orderBy

fun Query.orderBy(vararg orders: OrderByExpression): Query

Specify the order by clause of this query using the given order-by expressions.

plus

infix operator fun <T : Number> ColumnDeclaring<T>.plus(
    expr: ColumnDeclaring<T>
): BinaryExpression<T>

infix operator fun <T : Number> ColumnDeclaring<T>.plus(
    value: T
): BinaryExpression<T>

infix operator fun <T : Number> T.plus(
    expr: ColumnDeclaring<T>
): BinaryExpression<T>

Plus operator, translated to + in SQL.

rem

infix operator fun <T : Number> ColumnDeclaring<T>.rem(
    expr: ColumnDeclaring<T>
): BinaryExpression<T>

infix operator fun <T : Number> ColumnDeclaring<T>.rem(
    value: T
): BinaryExpression<T>

infix operator fun <T : Number> T.rem(
    expr: ColumnDeclaring<T>
): BinaryExpression<T>

Mod operator, translated to % in SQL.

rightJoin

fun QuerySource.rightJoin(
    right: BaseTable<*>,
    on: ColumnDeclaring<Boolean>? = null
): QuerySource

Join the right table and return a new QuerySource, translated to right join in SQL.

select

fun QuerySource.select(
    columns: Collection<ColumnDeclaring<*>>
): Query

fun QuerySource.select(
    vararg columns: ColumnDeclaring<*>
): Query

Create a query object, selecting the specific columns or expressions from this QuerySource.

selectDistinct

fun QuerySource.selectDistinct(
    columns: Collection<ColumnDeclaring<*>>
): Query

fun QuerySource.selectDistinct(
    vararg columns: ColumnDeclaring<*>
): Query

Create a query object, selecting the specific columns or expressions from this QuerySource distinctly.

sum

fun <C : Number> sum(
    column: ColumnDeclaring<C>
): AggregateExpression<C>

The sum function, translated to sum(column) in SQL.

sumDistinct

fun <C : Number> sumDistinct(
    column: ColumnDeclaring<C>
): AggregateExpression<C>

The sum function with distinct, translated to sum(distinct column) in SQL.

times

infix operator fun <T : Number> ColumnDeclaring<T>.times(
    expr: ColumnDeclaring<T>
): BinaryExpression<T>

infix operator fun <T : Number> ColumnDeclaring<T>.times(
    value: T
): BinaryExpression<T>

infix operator fun <T : Number> T.times(
    expr: ColumnDeclaring<T>
): BinaryExpression<T>

Multiply operator, translated to * in SQL.

toDouble

Cast the current column or expression’s type to Double.

toFloat

Cast the current column or expression’s type to Float.

toInt


Cast the current column or expression’s type to Int.

toLong

Cast the current column or expression’s type to Long.

toShort

Cast the current column or expression’s type to Short.

unaryMinus

operator fun <T : Number> ColumnDeclaring<T>.unaryMinus(): UnaryExpression<T>

Unary minus operator, translated to - in SQL.

unaryPlus

operator fun <T : Number> ColumnDeclaring<T>.unaryPlus(): UnaryExpression<T>

Unary plus operator, translated to + in SQL.

union

fun Query.union(right: Query): Query

Union this query with the given one, corresponding to the union keyword in SQL.

unionAll

fun Query.unionAll(right: Query): Query

Union this query with the given one, corresponding to the union all keyword in SQL.

update

fun <T : BaseTable<*>> Database.update(
    table: T,
    block: UpdateStatementBuilder.(T) -> Unit
): Int

Construct an update expression in the given closure, then execute it and return the effected row count.

where

fun Query.where(block: () -> ColumnDeclaring<Boolean>): Query

Specify the where clause of this query using the expression returned by the given callback function.

whereWithConditions

fun Query.whereWithConditions(
    block: (MutableList<ColumnDeclaring<Boolean>>) -> Unit
): Query

Create a mutable list, then add filter conditions to the list in the given callback function, finally combine
them with the and operator and set the combined condition as the where clause of this query.

whereWithOrConditions

fun Query.whereWithOrConditions(
    block: (MutableList<ColumnDeclaring<Boolean>>) -> Unit
): Query

Create a mutable list, then add filter conditions to the list in the given callback function, finally combine
them with the or operator and set the combined condition as the where clause of this query.

withIndex

Return a lazy Iterable that wraps each row of the query into an IndexedValue containing the index of
that row and the row itself.

xor


infix fun ColumnDeclaring<Boolean>.xor(
    value: Boolean
): BinaryExpression<Boolean>

Xor operator, translated to the xor keyword in SQL.