English | 简体中文

api-docs / org.ktorm.entity / EntitySequence / <init>

<init>

EntitySequence(
    database: Database,
    sourceTable: T,
    expression: SelectExpression,
    entityExtractor: (row: QueryRowSet) -> E)

Represents a sequence of entity objects. As the name implies, the style and use pattern of Ktorm’s entity sequence
APIs are highly similar to kotlin.sequences.Sequence and the extension functions in Kotlin standard lib, as it
provides many extension functions with the same names, such as filter, map, reduce, etc.

To create an EntitySequence, we can use the extension function sequenceOf:

val sequence = database.sequenceOf(Employees)

Now we got a default sequence, which can obtain all employees from the table. Please know that Ktorm doesn’t execute
the query right now. The sequence provides an iterator of type Iterator<Employee>, only when we iterate the
sequence using the iterator, the query is executed. The following code prints all employees using a for-each loop:

for (employee in sequence) {
println(employee)
}

This class wraps a Query object, and it’s iterator exactly wraps the query’s iterator. When an entity sequence is
iterated, its internal query is executed, and the entityExtractor function is applied to create an entity object
for each row.

Most of the entity sequence APIs are provided as extension functions, which can be divided into two groups:

  • Intermediate operations: these functions don’t execute the internal queries but return new-created sequence
    objects applying some modifications. For example, the filter function creates a new sequence object with the filter
    condition given by its parameter. The return types of intermediate operations are usually EntitySequence, so we
    can call other sequence functions continuously in chaining style.
  • Terminal operations: the return types of these functions are usually a collection or a computed result, as they execute the queries right now, obtain their results and perform some calculations on them. E.g. toList, reduce, etc.

For the list of sequence operations available, see the extension functions below.