api-docs / org.ktorm.dsl / Query
Query
class Query
(source code)Query is an abstraction of query operations and the core class of Ktorm’s query DSL.
The constructor of this class accepts two parameters: database is the database instance that this query
is running on; expression is the abstract representation of the executing SQL statement. Usually, we don’t
use the constructor to create Query objects but use the database.from(..).select(..)
syntax instead.
Query provides a built-in iterator, so we can iterate the results by a for-each loop:
for (row in database.from(Employees).select()) {
println(row[Employees.name])
}
Moreover, there are many extension functions that can help us easily process the query results, such as
Query.map, Query.flatMap, Query.associate, Query.fold, etc. With the help of these functions, we can
obtain rows from a query just like it’s a common Kotlin collection.
Query objects are immutable. Query DSL functions are provided as its extension functions normally. We can
call these functions in chaining style to modify them and create new query objects. Here is a simple example:
val query = database
.from(Employees)
.select(Employees.salary)
.where { (Employees.departmentId eq 1) and (Employees.name like "%vince%") }
Easy to know that the query obtains the salary of an employee named vince in department 1. The generated
SQL is obviously:
select t_employee.salary as t_employee_salary
from t_employee
where (t_employee.department_id = ?) and (t_employee.name like ?)
More usages can be found in the documentations of those DSL functions.
Constructors
Name | Summary |
---|---|
Query(database: Database, expression: QueryExpression) Query is an abstraction of query operations and the core class of Ktorm’s query DSL. |
Properties
Name | Summary |
---|---|
val database: Database the Database instance that this query is running on. | |
val expression: QueryExpression the underlying SQL expression of this query object. | |
val rowSet: QueryRowSet The ResultSet object of this query, lazy initialized after first access, obtained from the database by | |
val sql: String The executable SQL string of this query. | |
val The total record count of this query ignoring the pagination params. | |
val totalRecordsInAllPages: Int The total record count of this query ignoring the pagination params. |
Functions
Name | Summary |
---|---|
operator fun iterator(): Iterator<QueryRowSet> Return an iterator over the rows of this query. | |
fun withExpression(expression: QueryExpression): Query Return a copy of this Query with the expression modified. |
Extension Functions
Name | Summary |
---|---|
fun Query.asIterable(): Iterable<QueryRowSet> Wrap this query as Iterable. | |
Return a Map containing key-value pairs provided by transform function applied to rows of the query. | |
fun <K, V> Query.associateBy( Return a Map containing the values provided by valueTransform and indexed by keySelector functions applied to | |
fun <K, V, M : MutableMap<in K, in V>> Query.associateByTo( Populate and return the destination mutable map with key-value pairs, where key is provided by the keySelector | |
fun <K, V, M : MutableMap<in K, in V>> Query.associateTo( Populate and return the destination mutable map with key-value pairs provided by transform function applied to | |
infix fun <T : Any> T.eq( Equal operator, translated to | |
Return a single list of all elements yielded from results of transform function being invoked on each row | |
Return a single list of all elements yielded from results of transform function being invoked on each row | |
fun <R, C : MutableCollection<in R>> Query.flatMapIndexedTo( Append all elements yielded from results of transform function being invoked on each row and its index | |
fun <R, C : MutableCollection<in R>> Query.flatMapTo( Append all elements yielded from results of transform function being invoked on each row of the query, | |
Accumulate value starting with initial value and applying operation to current accumulator value and each row. | |
Accumulate value starting with initial value and applying operation to current accumulator value and each row | |
fun Query.forEach(action: (row: QueryRowSet) -> Unit): Unit Perform the given action on each row of the query. | |
fun Query.forEachIndexed( Perform the given action on each row of the query, providing sequential index with the row. | |
fun Query.groupBy( fun Query.groupBy(vararg columns: ColumnDeclaring<*>): Query Specify the | |
fun Query.having(condition: ColumnDeclaring<Boolean>): Query Specify the fun Query.having( Specify the | |
Insert the current Query‘s results into the given table, useful when transfer data from a table to another table. | |
fun <A : Appendable> Query.joinTo( Append the string from all rows separated using separator and using the given prefix and postfix if supplied. | |
fun Query.joinToString( Create a string from all rows separated using separator and using the given prefix and postfix if supplied. | |
Specify the pagination limit parameter of this query. Specify the pagination parameters of this query. | |
fun Query.locking( Specify the locking clause of this query, an example generated SQL could be: | |
fun Query.locking( Specify the locking clause of this query, an example generated SQL could be: | |
fun <R> Query.map( Return a list containing the results of applying the given transform function to each row of the query. | |
Return a list containing the results of applying the given transform function to each row and its index. | |
Return a list containing only the non-null results of applying the given transform function to each row | |
fun <R : Any, C : MutableCollection<in R>> Query.mapIndexedNotNullTo( Apply the given transform function to each row and its index and append only the non-null results to | |
fun <R, C : MutableCollection<in R>> Query.mapIndexedTo( Apply the given transform function to each row and its index and append the results to the given destination. | |
Return a list containing only the non-null results of applying the given transform function to each row of | |
fun <R : Any, C : MutableCollection<in R>> Query.mapNotNullTo( Apply the given transform function to each row of the query and append only the non-null results to | |
fun <R, C : MutableCollection<in R>> Query.mapTo( Apply the given transform function to each row of the query and append the results to the given destination. | |
infix fun <T : Any> T.neq( Not-equal operator, translated to | |
infix fun <T : Any> T.notEq( Not-equal operator, translated to | |
Specify the pagination offset parameter of this query. | |
fun Query.orderBy( fun Query.orderBy(vararg orders: OrderByExpression): Query Specify the | |
Union this query with the given one, corresponding to the | |
Union this query with the given one, corresponding to the | |
fun Query.where(condition: ColumnDeclaring<Boolean>): Query Specify the fun Query.where( Specify the | |
fun Query.whereWithConditions( Create a mutable list, then add filter conditions to the list in the given callback function, finally combine | |
fun Query.whereWithOrConditions( Create a mutable list, then add filter conditions to the list in the given callback function, finally combine | |
fun Query.withIndex(): Iterable<IndexedValue<QueryRowSet>> Return a lazy Iterable that wraps each row of the query into an IndexedValue containing the index of |