Skip to content

Support for async drivers #1942

@janvladimirmostert

Description

@janvladimirmostert

Currently SQL Delight is using JDBC or other blocking drivers

Jasync has async MySQL / PostgreSQL drivers which although it's using annoying Joda time, works great otherwise
(there is a ticket there somewhere to replace Joda Time with Java Time)

Converting Joda Time is not difficult either

		when (value) {
			is java.lang.Integer -> {
				return value.toInt() as T
			}
			is java.lang.Long -> {
				return value.toLong() as T
			}
			is org.joda.time.LocalDateTime -> {
				val utc = value.toDateTime(DateTimeZone.UTC);
				val secondsSinceEpoch: Long = utc.millis / 1000
				val milliSeconds: Long = utc.millis - (secondsSinceEpoch * 1000)

				val javaTime = LocalDateTime.ofEpochSecond(
					secondsSinceEpoch,
					milliSeconds.toInt() * 1000000,
					ZoneOffset.UTC
				)
				return javaTime as T
			}

Jasync returns a Future and can easily be converted to a Coroutine allowing non-blocking queries

suspend fun Connection.execute(query: String, vararg values: Any?): QueryResult {
	return if (values.isEmpty()) {
		this.sendQuery(query).await()
	} else {
		this.sendPreparedStatement(query, values.asList().toMutableList()).await()
	}
}

suspend fun ConnectionPool<*>.executeAsync(query: String, vararg values: Any?): Deferred<QueryResult> {
	return if (values.isEmpty()) {
		this.sendQuery(query).asDeferred()
	} else {
		this.sendPreparedStatement(query, values.asList().toMutableList()).asDeferred()
	}
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions