|
1 | 1 | package com.powersync.integrations.room |
2 | 2 |
|
3 | | -import androidx.room.RoomDatabase |
4 | | -import androidx.room.Transactor |
5 | | -import androidx.room.execSQL |
6 | | -import androidx.room.useReaderConnection |
7 | | -import androidx.room.useWriterConnection |
| 3 | +import androidx.room3.RoomDatabase |
| 4 | +import androidx.room3.Transactor |
| 5 | +import androidx.room3.executeSQL |
| 6 | +import androidx.room3.useReaderConnection |
| 7 | +import androidx.room3.useWriterConnection |
8 | 8 | import androidx.sqlite.SQLiteException |
9 | 9 | import androidx.sqlite.SQLiteStatement |
| 10 | +import androidx.sqlite.async.step |
10 | 11 | import com.powersync.db.driver.SQLiteConnectionLease |
11 | 12 | import com.powersync.db.driver.SQLiteConnectionPool |
12 | 13 | import com.powersync.db.schema.Schema |
13 | | -import kotlinx.coroutines.currentCoroutineContext |
14 | 14 | import kotlinx.coroutines.flow.MutableSharedFlow |
15 | 15 | import kotlinx.coroutines.flow.SharedFlow |
16 | 16 | import kotlinx.coroutines.launch |
17 | | -import kotlinx.coroutines.runBlocking |
18 | | -import kotlinx.serialization.json.Json |
19 | | -import kotlin.coroutines.CoroutineContext |
20 | 17 |
|
21 | 18 | /** |
22 | 19 | * A [SQLiteConnectionPool] implementation for the PowerSync SDK that is backed by a [RoomDatabase]. |
@@ -53,7 +50,7 @@ public class RoomConnectionPool( |
53 | 50 |
|
54 | 51 | override suspend fun <T> read(callback: suspend (SQLiteConnectionLease) -> T): T = |
55 | 52 | db.useReaderConnection { |
56 | | - callback(RoomTransactionLease(it, currentCoroutineContext())) |
| 53 | + callback(it.asConnectionLease()) |
57 | 54 | } |
58 | 55 |
|
59 | 56 | /** |
@@ -94,11 +91,11 @@ public class RoomConnectionPool( |
94 | 91 | db.useWriterConnection { |
95 | 92 | if (!hasInstalledUpdateHook) { |
96 | 93 | hasInstalledUpdateHook = true |
97 | | - it.execSQL("SELECT powersync_update_hooks('install')") |
| 94 | + it.executeSQL("SELECT powersync_update_hooks('install')") |
98 | 95 | } |
99 | 96 |
|
100 | 97 | try { |
101 | | - callback(RoomTransactionLease(it, currentCoroutineContext())) |
| 98 | + callback(it.asConnectionLease()) |
102 | 99 | } finally { |
103 | 100 | // List changed tables, excluding virtual and shadow tables for e.g. fts5 |
104 | 101 | val statement = |
@@ -139,43 +136,15 @@ public class RoomConnectionPool( |
139 | 136 | } |
140 | 137 | } |
141 | 138 |
|
142 | | -private class RoomTransactionLease( |
143 | | - private val transactor: Transactor, |
144 | | - /** |
145 | | - * The context to use for [runBlocking] calls to avoid the "Attempted to use connection on a |
146 | | - * different coroutine" error. |
147 | | - */ |
148 | | - private val context: CoroutineContext, |
| 139 | +internal expect suspend fun Transactor.asConnectionLease(): SQLiteConnectionLease |
| 140 | + |
| 141 | +internal abstract class RoomTransactionLease( |
| 142 | + protected val transactor: Transactor, |
149 | 143 | ) : SQLiteConnectionLease { |
150 | 144 | override suspend fun isInTransaction(): Boolean = transactor.inTransaction() |
151 | 145 |
|
152 | | - override suspend fun <R> usePrepared( |
153 | | - sql: String, |
154 | | - block: (SQLiteStatement) -> R, |
155 | | - ): R = transactor.usePrepared(sql, block) |
156 | | - |
157 | 146 | override suspend fun <R> usePreparedAsync( |
158 | 147 | sql: String, |
159 | 148 | block: suspend (SQLiteStatement) -> R, |
160 | | - ): R = |
161 | | - transactor.usePrepared(sql) { |
162 | | - // TODO: This is suspending in Room3, where we can avoid the runBlocking here. |
163 | | - stmt -> |
164 | | - // Don't use the context here, Room2 does that for us. We're not allowed to use the |
165 | | - // connection from different threads. |
166 | | - runBlocking { block(stmt) } |
167 | | - } |
168 | | - |
169 | | - override fun isInTransactionSync(): Boolean = |
170 | | - runBlocking(context) { |
171 | | - isInTransaction() |
172 | | - } |
173 | | - |
174 | | - override fun <R> usePreparedSync( |
175 | | - sql: String, |
176 | | - block: (SQLiteStatement) -> R, |
177 | | - ): R = |
178 | | - runBlocking(context) { |
179 | | - usePrepared(sql, block) |
180 | | - } |
| 149 | + ): R = transactor.usePrepared(sql, block) |
181 | 150 | } |
0 commit comments