Skip to content

Commit d2bdb2b

Browse files
committed
Reformat and review feedback
1 parent 7935001 commit d2bdb2b

File tree

6 files changed

+39
-10
lines changed

6 files changed

+39
-10
lines changed

integrations/sqldelight-test-database/src/commonMain/sqldelight/com/powersync/integrations/sqldelight/todos.sq

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ SELECT * FROM todos;
99

1010
create:
1111
INSERT INTO todos (id, title, content) VALUES (uuid(), ?, ?);
12+
13+
update:
14+
UPDATE todos SET content = content || title RETURNING *;

integrations/sqldelight/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ There are also some limitations to be aware of:
5353
1. Due to historical reasons, the PowerSync SDK migrates all databases to `user_version` 1 when
5454
created (but it will never downgrade a database).
5555
So if you want to use SQLDelight's schema tools, the first version would have to be `2`.
56-
2. While you can write `CREATE TABLE` statements in your `.sq` files, note that these aren't
57-
actually used - you still have to define your PowerSync schema and the SDK will auto-create the
58-
tables from there.
56+
2. The `CREATE TABLE` statements in your `.sq` files are only used at build time to verify your
57+
queries. At runtime, PowerSync will create tables from your schema as views, the defined
58+
statements are ignored.
5959
If you want to use the schema managed by SQLDelight, configure PowerSync to use
6060
[raw tables](https://docs.powersync.com/usage/use-case-examples/raw-tables).
6161
3. Functions and tables contributed by the PowerSync core extension are not visible to `.sq` files

integrations/sqldelight/src/appleTest/kotlin/com/powersync/integrations/sqldelight/SqlDelightTest.apple.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,4 @@ package com.powersync.integrations.sqldelight
22

33
import com.powersync.DatabaseDriverFactory
44

5-
actual fun databaseDriverFactory(): DatabaseDriverFactory {
6-
return DatabaseDriverFactory()
7-
}
5+
actual fun databaseDriverFactory(): DatabaseDriverFactory = DatabaseDriverFactory()

integrations/sqldelight/src/commonIntegrationTest/kotlin/com/powersync/integrations/sqldelight/SqlDelightTest.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.powersync.integrations.sqldelight
22

33
import app.cash.sqldelight.async.coroutines.awaitAsList
4+
import app.cash.sqldelight.async.coroutines.awaitAsOne
45
import app.cash.sqldelight.coroutines.asFlow
56
import app.cash.sqldelight.coroutines.mapToList
67
import app.cash.turbine.turbineScope
@@ -85,6 +86,28 @@ class SqlDelightTest {
8586
}
8687
}
8788

89+
@Test
90+
fun testReturningQuery() =
91+
databaseTest { powersync ->
92+
val db = TestDatabase(PowerSyncDriver(powersync, this))
93+
turbineScope {
94+
db.todosQueries.create("title", "content")
95+
96+
val query =
97+
db.todosQueries
98+
.all()
99+
.asFlow()
100+
.mapToList(currentCoroutineContext())
101+
.testIn(this)
102+
query.awaitItem() shouldHaveSize 1
103+
104+
val updatedItem = db.todosQueries.update().awaitAsOne()
105+
updatedItem.content shouldBe "contenttitle"
106+
query.awaitItem() shouldBe listOf(updatedItem)
107+
query.cancelAndIgnoreRemainingEvents()
108+
}
109+
}
110+
88111
@Test
89112
fun testTransaction() =
90113
databaseTest { powersync ->

integrations/sqldelight/src/commonMain/kotlin/com/powersync/integrations/sqldelight/PowerSyncDriver.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ public class PowerSyncDriver(
5656
binders: (SqlPreparedStatement.() -> Unit)?,
5757
): QueryResult<R> =
5858
QueryResult.AsyncValue {
59+
// Despite being a query, this is not guaranteed to be read-only: RETURNING statements
60+
// also use this.
61+
// So, always using the write connection is a safe default. In the future we may want to
62+
// analyze the statement to potentially route it to a read connection if possible.
5963
withConnection { connection ->
6064
connection.usePrepared(sql) { stmt ->
6165
val wrapper = StatementWrapper(stmt)
@@ -81,8 +85,11 @@ public class PowerSyncDriver(
8185
while (stmt.step()) {
8286
// Keep stepping through statement
8387
}
88+
}
8489

85-
0L
90+
connection.usePrepared("SELECT changes()") {
91+
check(it.step())
92+
it.getLong(0)
8693
}
8794
}
8895
}

integrations/sqldelight/src/jvmTest/kotlin/com/powersync/integrations/sqldelight/SqlDelightTest.jvm.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,4 @@ package com.powersync.integrations.sqldelight
22

33
import com.powersync.DatabaseDriverFactory
44

5-
actual fun databaseDriverFactory(): DatabaseDriverFactory {
6-
return DatabaseDriverFactory()
7-
}
5+
actual fun databaseDriverFactory(): DatabaseDriverFactory = DatabaseDriverFactory()

0 commit comments

Comments
 (0)