-
Notifications
You must be signed in to change notification settings - Fork 548
Add support for async drivers #3168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 30 commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
7eb9012
Add async codegen and runtime
dellisd d52cff9
Split sync and async runtime modules
dellisd b9dabd0
Merge branch 'master' into async-drivers
ac736a9
Generate suspend functions for queries
dellisd a928067
Add runtime module tasks to gradle plugin tests
dellisd e18423c
Run spotless
dellisd bc032c3
Fix up broken async codegen tests
dellisd 72fbdd5
Pull async gen flag from SqlDelightFile
dellisd 83b0078
Add missing prop in intellij tests
dellisd 68d65e9
Update async mysql integration test
dellisd a998844
Merge branch 'master' into async-drivers
dellisd 8427d74
Add async runtime tests
dellisd 183112a
Fix spotless errors
dellisd 3d64593
Fix broken AsyncTest
dellisd 7500c51
Fix js worker test
dellisd 146ba97
Skip js web worker tests on node
dellisd 4b34ce0
Add driver-async-test
dellisd 87e3619
Disable mingwX86 on driver-async-test
dellisd bf84f44
Run spotless (again)
dellisd b2ab001
Apply suggestions from code review
dellisd a5a1959
Remove mingwX86 from runtime-async entirely
dellisd 7fa7967
Clean things up
dellisd 79ae9d6
Make Closeable.close() suspendable
dellisd 6bccb9e
Add missing suspend modifier
dellisd e2415a9
Clean up driver-async-test to handle suspend funs
dellisd 920e13a
Handle even more suspending test teardowns
dellisd 1b25338
Run spotless
dellisd f96c407
Fix one last close function
dellisd 76fae94
Fix up and enable remaining js worker tests
dellisd 7fb0ae3
Remove driver-async-test
dellisd 7155b04
Apply suggestions from code review
dellisd d4f5d34
Clean up async runtime
dellisd d510ace
Merge branch 'master' into async-drivers
dellisd 289e86c
Delete async runtime tests
dellisd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
plugins { | ||
alias(deps.plugins.kotlin.jvm) | ||
alias(deps.plugins.publish) | ||
alias(deps.plugins.dokka) | ||
} | ||
|
||
archivesBaseName = 'sqldelight-r2dbc-driver' | ||
|
||
dependencies { | ||
api project(':runtime:runtime-async') | ||
implementation deps.r2dbc | ||
implementation deps.kotlin.coroutines.core | ||
implementation deps.kotlin.coroutines.reactive | ||
} | ||
|
||
apply from: "$rootDir/gradle/gradle-mvn-push.gradle" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
POM_ARTIFACT_ID=r2dbc-driver | ||
POM_NAME=SQLDelight R2DBC Driver | ||
POM_DESCRIPTION=R2DBC driver for SQLDelight | ||
POM_PACKAGING=jar |
195 changes: 195 additions & 0 deletions
195
drivers/r2dbc-driver/src/main/kotlin/app/cash/sqldelight/driver/r2dbc/R2dbcDriver.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
package app.cash.sqldelight.driver.r2dbc | ||
|
||
import app.cash.sqldelight.async.AsyncQuery | ||
import app.cash.sqldelight.async.AsyncTransacter | ||
import app.cash.sqldelight.async.db.AsyncSqlCursor | ||
import app.cash.sqldelight.async.db.AsyncSqlDriver | ||
import app.cash.sqldelight.async.db.AsyncSqlPreparedStatement | ||
import io.r2dbc.spi.Connection | ||
import io.r2dbc.spi.Statement | ||
import kotlinx.coroutines.reactive.awaitLast | ||
import kotlinx.coroutines.reactive.awaitSingle | ||
import org.reactivestreams.Publisher | ||
import org.reactivestreams.Subscriber | ||
import org.reactivestreams.Subscription | ||
|
||
class R2dbcDriver(val connection: Connection) : AsyncSqlDriver { | ||
dellisd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
override suspend fun <R> executeQuery( | ||
identifier: Int?, | ||
sql: String, | ||
mapper: (AsyncSqlCursor) -> R, | ||
parameters: Int, | ||
binders: (AsyncSqlPreparedStatement.() -> Unit)? | ||
): R { | ||
val prepared = connection.createStatement(sql).also { statement -> | ||
R2dbcPreparedStatement(statement).apply { if (binders != null) this.binders() } | ||
} | ||
val result = prepared.execute().awaitSingle() | ||
|
||
val rowSet = mutableListOf<Map<Int, Any?>>() | ||
result.map { row, rowMetadata -> | ||
rowSet.add(rowMetadata.columnMetadatas.mapIndexed { index, _ -> index to row.get(index) }.toMap()) | ||
}.awaitLast() | ||
|
||
return mapper(R2dbcCursor(rowSet)) | ||
} | ||
|
||
override suspend fun execute( | ||
identifier: Int?, | ||
sql: String, | ||
parameters: Int, | ||
binders: (AsyncSqlPreparedStatement.() -> Unit)? | ||
): Long { | ||
val prepared = connection.createStatement(sql).also { statement -> | ||
R2dbcPreparedStatement(statement).apply { if (binders != null) this.binders() } | ||
} | ||
|
||
val result = prepared.execute().awaitSingle() | ||
// TODO: r2dbc-mysql emits a java.lang.Integer instead of a java.lang.Long, mysql driver needs to support latest r2dbc-spi | ||
// return result.rowsUpdated.awaitSingle() | ||
return 0L | ||
dellisd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
|
||
private val transactions = ThreadLocal<Transaction>() | ||
var transaction: Transaction? | ||
dellisd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
get() = transactions.get() | ||
set(value) { | ||
transactions.set(value) | ||
} | ||
|
||
override suspend fun newTransaction(): AsyncTransacter.Transaction { | ||
val enclosing = transaction | ||
val transaction = Transaction(enclosing, this.connection) | ||
dellisd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
connection.beginTransaction().awaitSingle() | ||
|
||
return transaction | ||
} | ||
|
||
override fun currentTransaction(): AsyncTransacter.Transaction? = transaction | ||
|
||
override fun addListener(listener: AsyncQuery.Listener, queryKeys: Array<String>) { | ||
} | ||
|
||
override fun removeListener(listener: AsyncQuery.Listener, queryKeys: Array<String>) { | ||
} | ||
|
||
override fun notifyListeners(queryKeys: Array<String>) { | ||
} | ||
dellisd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
override suspend fun close() { | ||
connection.close().awaitSingle() | ||
} | ||
|
||
class Transaction( | ||
dellisd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
override val enclosingTransaction: AsyncTransacter.Transaction?, | ||
private val connection: Connection | ||
) : AsyncTransacter.Transaction() { | ||
override suspend fun endTransaction(successful: Boolean) { | ||
if (enclosingTransaction == null) { | ||
if (successful) connection.commitTransaction().awaitSingle() | ||
} else { | ||
connection.rollbackTransaction().awaitSingle() | ||
} | ||
dellisd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
|
||
open class R2dbcPreparedStatement(private val statement: Statement) : AsyncSqlPreparedStatement { | ||
override fun bindBytes(index: Int, bytes: ByteArray?) { | ||
if (bytes == null) { | ||
statement.bindNull(index - 1, ByteArray::class.java) | ||
} else { | ||
statement.bind(index - 1, bytes) | ||
} | ||
} | ||
|
||
override fun bindLong(index: Int, long: Long?) { | ||
if (long == null) { | ||
statement.bindNull(index - 1, Long::class.java) | ||
} else { | ||
statement.bind(index - 1, long) | ||
} | ||
} | ||
|
||
override fun bindDouble(index: Int, double: Double?) { | ||
if (double == null) { | ||
statement.bindNull(index - 1, Double::class.java) | ||
} else { | ||
statement.bind(index - 1, double) | ||
} | ||
} | ||
|
||
override fun bindString(index: Int, string: String?) { | ||
if (string == null) { | ||
statement.bindNull(index - 1, String::class.java) | ||
} else { | ||
statement.bind(index - 1, string) | ||
} | ||
} | ||
|
||
override fun bindBoolean(index: Int, boolean: Boolean?) { | ||
if (boolean == null) { | ||
statement.bindNull(index - 1, Boolean::class.java) | ||
} else { | ||
statement.bind(index - 1, boolean) | ||
} | ||
} | ||
|
||
fun bindObject(index: Int, any: Any?) { | ||
if (any == null) { | ||
statement.bindNull(index - 1, Any::class.java) | ||
} else { | ||
statement.bind(index - 1, any) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* TODO: Write a better async cursor API | ||
*/ | ||
open class R2dbcCursor(val rowSet: List<Map<Int, Any?>>) : AsyncSqlCursor { | ||
var row = -1 | ||
AlecKazakova marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private set | ||
|
||
override fun next(): Boolean = ++row < rowSet.size | ||
|
||
override fun getString(index: Int): String? = rowSet[row][index] as String? | ||
|
||
override fun getLong(index: Int): Long? = (rowSet[row][index] as Number?)?.toLong() | ||
|
||
override fun getBytes(index: Int): ByteArray? = rowSet[row][index] as ByteArray? | ||
|
||
override fun getDouble(index: Int): Double? = rowSet[row][index] as Double? | ||
|
||
override fun getBoolean(index: Int): Boolean? = rowSet[row][index] as Boolean? | ||
|
||
inline fun <reified T : Any> getObject(index: Int): T? = rowSet[row][index] as T? | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
fun <T> getArray(index: Int): Array<T>? = rowSet[row][index] as Array<T>? | ||
} | ||
|
||
private fun <T> Publisher<T>.subscribe( | ||
dellisd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
next: (T) -> Unit = {}, | ||
error: (Throwable) -> Unit = {}, | ||
complete: () -> Unit = {}, | ||
) = subscribe(object : Subscriber<T> { | ||
private var subscription: Subscription? = null | ||
override fun onSubscribe(s: Subscription) { | ||
subscription = s | ||
s.request(Long.MAX_VALUE) | ||
} | ||
|
||
override fun onNext(t: T) { | ||
next(t) | ||
} | ||
|
||
override fun onError(t: Throwable) { | ||
error(t) | ||
} | ||
|
||
override fun onComplete() { | ||
complete() | ||
subscription?.cancel() | ||
} | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.