github kotlin-orm/ktorm v3.1.0

latest releases: v4.1.1, v4.1.0, v4.0.0...
4 years ago

Upgrade to Kotlin 1.4

To use the new features of Kotlin, we've upgraded its version to 1.4.10, which brings some changes to us:

  • Explicit API mode to ensure we don't expose some internal things accidentally.

  • Contract support for database.useConnection { .. } & database.useTransaction { .. } to let the compiler know our callback functions are called in place exactly once.

  • Deprecation of mapColumnsN & aggregateColumnsN functions. As Kotlin 1.4 supports overload resolution by lambda return type, we don't need these functions anymore, mapColumns & aggregateColumns is enough.

    database.employees
        .filter { it.departmentId eq 1 }
        .mapColumns { tupleOf(it.id, it.name) }
        .forEach { (id, name) ->
            println("$id:$name")
        }

Entity Based Update Function

In Ktorm 3.1, we provide an update function that can update all the non-null properties of an entity object to the database. Using this function, the entity object is not required to be associated with a table first. That means, comparing to flushChanges, we don’t have to obtain an entity object from the database first before performing the update. The usage is as follows:

val employee = Employee {
    id = 5
    job = "engineer"
    salary = 100
}

database.employees.update(employee)

Generated SQL:

update t_employee set job = ?, salary = ? where id = ?

Syntax Refactoring of Insert & Update DSL

Previously, we inserted a record into the table like this:

database.insert(Employees) {
    it.name to "jerry"
    it.job to "trainee"
    it.managerId to 1
    it.hireDate to LocalDate.now()
    it.salary to 50
    it.departmentId to 1
}

Here, we used it.name to "jerry" to set the name to jerry in the closure. And the to function is a member of AssignmentsBuilder, but not the to function used to create Pair instances of Kotlin standard lib.

It is very easy for users to get confused with these two functions, so in Ktorm 3.1, we provide another set function as an alternative. The to function is marked deprecated and will be removed in the future. You can learn the new syntax here https://ktorm.liuwj.me/en/dml.html

database.insert(Employees) {
    set(it.name, "jerry")
    set(it.job, "trainee")
    set(it.managerId, 1)
    set(it.hireDate, LocalDate.now())
    set(it.salary, 50)
    set(it.departmentId, 1)
}

Other Optimizations and Bug Fixes

  • Add ShortSqlType. #160
  • Add MySQL IF function. #163
  • Support mixed-case column names & auto case transform. #175
  • Allow specify PostgreSQL insertOrUpdate conflict columns. #181
  • Support select .. for update. #69
  • Support catalog & schema in table definition. #89, #154, #183
  • Check max column name length. #122
  • Fix timestamp fraction bug. #130
  • Update the syntax of defining JSON columns from json("foo", typeRef<List<Int>>) to json<List<Int>>("foo").

Don't miss a new ktorm release

NewReleases is sending notifications on new releases.