Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Kotlin API

Ohad Shai edited this page Nov 7, 2018 · 9 revisions

Although jasync-sql is mostly written in Kotlin, most of the API's are written so it will be easily used from Java as well as Kotlin.
This page describes API's that are intended to use from Kotlin code.

  • ResultSet - operator invoke.
  • RowData - operator get and invoke, generic casting.
  • Configuration and PoolConfiguration are data classes, so copy can be easily used.

Kotlin coroutines support

Support for suspending invocation is done via SuspendingConnection interface. This interface is identical to Connection in the method it has but works with suspend API instead of CompletableFuture callback. For Example:

fun sendQuery(query: String): CompletableFuture<QueryResult>

Has its suspending flavor:

suspend fun sendQuery(query: String): QueryResult

In order to 'move' to coroutines world, use the following extension property on Connection:

val suspendingConnection: SuspendingConnection = connection.asSuspending

Behind the scenes, SuspendingConnection is a wrapper that delegates all calls to the underlying Connection and converts the future to suspend method using coroutines extension from the kotlinx.coroutines.

More details here. More examples can be found on Executing Statements.

In case you want to know there was also previous api.

An example Kotlin project (not up to date) can be found here: https://github.com/jasync-sql/jasync-sql/tree/master/samples/ktor

Clone this wiki locally