MyGit

v2.7

kotlin-orm/ktorm

版本发布时间: 2020-02-02 19:03:08

kotlin-orm/ktorm最新发布版本:v4.1.1(2024-09-03 11:39:09)

In Ktorm 2.7, we did a refactoring of the code. This refactoring deprecated Database.global and a series of functions implemented based on it, making users explicitly specify the Database instances to use while performing database operations, instead of implicitly use Database.global. More details can be found at https://ktorm.liuwj.me/en/about-deprecating-database-global.html

Note that these APIs are still available in version 2.7, but they have been marked as @Deprecated and will be completely removed in the future.

In previous versions, although Database.connect returns a new created Database object, we usually ignore it because Ktorm automatically saves it to an internal global variable. But now, we have to define a variable by ourselves to hold the return value:

val database = Database.connect("jdbc:mysql://localhost:3306/ktorm?user=root&password=***")

We used to create queries by the extension function Table.select before:

// Old API
for (row in Employees.select()) {
    println(row[Employees.name])
}

This query uses Database.global, obtaining all records from Employees table, which is indeed very implicit as you can see. Now we have to specify the database instance explicitly and use the syntax of database.from(..).select(..) to create queries:

for (row in database.from(Employees).select()) {
    println(row[Employees.name])
}

Here is another example:

val t = Employees.aliased("t")
database
    .from(t)
    .select(t.departmentId, avg(t.salary))
    .groupBy(t.departmentId)
    .having { avg(t.salary) greater 100.0 }
    .forEach { row -> 
        println("${row.getInt(1)}:${row.getDouble(2)}")
    }

As for sequence APIs, we used to create sequence objects via asSequence before, and now we just need to change it to sequenceOf. For example:

val employees = database.sequenceOf(Employees).toList()

Another example using sequenceOf:

val employees = database
    .sequenceOf(Employees)
    .filter { it.departmentId eq 1 }
    .filter { it.managerId.isNotNull() }
    .sortedBy { it.salary }
    .toList()

These are the two most significant changes in this refactoring. The documents on Ktorm's official website have now been updated for version 2.7. You can refer to the latest documents for what you are interested in. https://ktorm.liuwj.me/

Feel free to raise issues on GitHub if you have any questions.

相关地址:原始地址 下载(tar) 下载(zip)

查看:2020-02-02发行的版本