Query
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.
Properties
the underlying SQL expression of this query object.
The ResultSet object of this query, lazy initialized after first access, obtained from the database by executing the generated SQL.
The total record count of this query ignoring the pagination params.
The total record count of this query ignoring the pagination params.
Functions
Wrap this query as Iterable.
Return a Map containing the values provided by valueTransform and indexed by keySelector functions applied to rows of the query.
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.
Populate and return the destination mutable map with key-value pairs provided by transform function applied to each row of the query.
Return a single list of all elements yielded from results of transform function being invoked on each row and its index in the query.
Append all elements yielded from results of transform function being invoked on each row and its index in the query, to the given destination.
Append all elements yielded from results of transform function being invoked on each row of the query, to the given destination.
Perform the given action on each row of the query, providing sequential index with the row.
Specify the group by
clause of this query using the given columns or expressions.
Specify the having
clause of this query using the expression returned by the given callback function.
Specify the having
clause of this query using the given condition expression.
Return an iterator over the rows of this 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 and its index.
Apply the given transform function to each row and its index and append only the non-null results to the given destination.
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 the query.
Apply the given transform function to each row of the query and append only the non-null results to the given destination.
Apply the given transform function to each row of the query and append the results to the given destination.
Specify the order by
clause of this query using the given order-by expressions.
Specify the where
clause of this query using the expression returned by the given callback function.
Specify the where
clause of this query using the given condition expression.
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.
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.
Return a copy of this Query with the expression modified.
Return a lazy Iterable that wraps each row of the query into an IndexedValue containing the index of that row and the row itself.