Kwery: An SQL library for Kotlin

Hi all,

I’ve just released Kwery, an SQL library for Kotlin at https://github.com/andrewoma/kwery

The core is a thin wrapper over JDBC. e.g.

val sql = "select * from actor where first_name = :first_name"

val actors = session.select(sql, mapOf(first_name to Brad)) { row ->
  Actor(row.string(first_name), row.string(last_name), row.timestamp(last_update))
}



It also includes mapping functionality, including standard CRUD operations via DAOs. e.g.

object actorTable : Table<Actor, Int>("actor", tableConfig), VersionedWithTimestamp {
    val ActorId    by col(Actor::id,                     id = true)
    val FirstName  by col(Name::firstName, { it.name },  notNull = true)
    val LastName   by col(Name::lastName,  { it.name },  notNull = true)
    val LastUpdate by col(Actor::lastUpdate,             version = true)

  override fun idColumns(id: Int) = setOf(ActorId of id)

  override fun create(value: Value<Actor>) = Actor(value of ActorId,
           Name(value of FirstName, value of LastName), value of LastUpdate)
}

class ActorDao(session: Session) : AbstractDao<Actor, Int>(session, actorTable, { it.id })



Finally, there’s a module for efficient fetching of object graphs - see the github README (https://github.com/andrewoma/kwery) for more information.

The usual warnings apply - it’s under active development, unstable and probably bug ridden.

However, I’m fairly happy with the direction it’s heading. The table definition is fairly terse yet flexible. The design supports immutable objects (key for me) and partial selects.

Kwery uses a bunch of Kotlin features (reified generics, delegated properties and callable references) and it all seems to work - kudos to the Kotlin devs!

Anyway, I thought others might find it useful or interesting - any feedback is welcome.

Cheers,
Andrew

looks like a great lib! what is the development status?
Where can I find more info about establishing a connection? and how to “bind” it to the session?

Hi,

Development is pretty much on hold at the moment as I’ve recently had a baby and free time is scarce. :wink:

I will however fix bugs and respond to queries etc.

As to establishing a connection see: kwery/README.md at master · andrewoma/kwery · GitHub

ThreadLocalSession is usually the best choice for obtaining a session from a DataSource.

Cheers,
Andrew