Ktorm v2.7 Release Notes

Release Date: 2020-02-02 // about 4 years ago
  • 🔨 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 APIfor (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.