From 07041cd5935479ad8f0e888bc8dc873b386c9bb7 Mon Sep 17 00:00:00 2001 From: DominicGBauer Date: Fri, 20 Sep 2024 13:54:58 +0200 Subject: [PATCH 1/2] chore: add changes to disconnectAndClear --- .../com/powersync/db/PowerSyncDatabaseImpl.kt | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/core/src/commonMain/kotlin/com/powersync/db/PowerSyncDatabaseImpl.kt b/core/src/commonMain/kotlin/com/powersync/db/PowerSyncDatabaseImpl.kt index 97788328..428b71c0 100644 --- a/core/src/commonMain/kotlin/com/powersync/db/PowerSyncDatabaseImpl.kt +++ b/core/src/commonMain/kotlin/com/powersync/db/PowerSyncDatabaseImpl.kt @@ -285,18 +285,9 @@ internal class PowerSyncDatabaseImpl( disconnect() this.writeTransaction { tx -> - tx.execute("DELETE FROM ${InternalTable.OPLOG}") - tx.execute("DELETE FROM ${InternalTable.CRUD}") - tx.execute("DELETE FROM ${InternalTable.BUCKETS}") - tx.execute("DELETE FROM ${InternalTable.UNTYPED}") - - val tableGlob = if (clearLocal) "ps_data_*" else "ps_data__*" - val existingTableRows = internalDb.getExistingTableNames(tableGlob) - - for (row in existingTableRows) { - tx.execute("DELETE FROM ${quoteIdentifier(row)}") - } + tx.execute("select powersync_clear(?)", listOf(if(clearLocal) 1 else 0)) } + currentStatus.update(lastSyncedAt = null, hasSynced = false) } private suspend fun updateHasSynced() { From f1211e5f767a816cc0fe094f4fc533c174a06602 Mon Sep 17 00:00:00 2001 From: DominicGBauer Date: Fri, 20 Sep 2024 15:50:50 +0200 Subject: [PATCH 2/2] chore: add dialect and custom function to sq file --- .../com/powersync/db/PowerSyncDatabaseImpl.kt | 4 +-- dialect/README.md | 27 +++++++++++++++++++ .../com/powersync/sqlite/PowerSyncDialect.kt | 2 +- .../sqldelight/com/persistence/Powersync.sq | 3 +++ 4 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 dialect/README.md diff --git a/core/src/commonMain/kotlin/com/powersync/db/PowerSyncDatabaseImpl.kt b/core/src/commonMain/kotlin/com/powersync/db/PowerSyncDatabaseImpl.kt index 428b71c0..0cddc036 100644 --- a/core/src/commonMain/kotlin/com/powersync/db/PowerSyncDatabaseImpl.kt +++ b/core/src/commonMain/kotlin/com/powersync/db/PowerSyncDatabaseImpl.kt @@ -284,8 +284,8 @@ internal class PowerSyncDatabaseImpl( override suspend fun disconnectAndClear(clearLocal: Boolean) { disconnect() - this.writeTransaction { tx -> - tx.execute("select powersync_clear(?)", listOf(if(clearLocal) 1 else 0)) + this.writeTransaction { + internalDb.queries.powersyncClear(if(clearLocal) "1" else "0").awaitAsOne() } currentStatus.update(lastSyncedAt = null, hasSynced = false) } diff --git a/dialect/README.md b/dialect/README.md new file mode 100644 index 00000000..411682f6 --- /dev/null +++ b/dialect/README.md @@ -0,0 +1,27 @@ +# SQLDelight Custom PowerSync Dialect + +This defines the custom PowerSync SQLite functions to be used in the `PowerSync.sq` file found in the `persistence` module. + +## Example +```kotlin +public class PowerSyncTypeResolver(private val parentResolver: TypeResolver) : + TypeResolver by SqliteTypeResolver(parentResolver) { + override fun functionType(functionExpr: SqlFunctionExpr): IntermediateType? { + when (functionExpr.functionName.text) { + "powersync_replace_schema" -> return IntermediateType( + PrimitiveType.TEXT + ) + } + return parentResolver.functionType(functionExpr) + } +} +``` + +allows + +```sql +replaceSchema: +SELECT powersync_replace_schema(?); +``` + +To be used in the `PowerSync.sq` file in the `persistence` module. \ No newline at end of file diff --git a/dialect/src/main/kotlin/com/powersync/sqlite/PowerSyncDialect.kt b/dialect/src/main/kotlin/com/powersync/sqlite/PowerSyncDialect.kt index 1c7c5f65..904470a0 100644 --- a/dialect/src/main/kotlin/com/powersync/sqlite/PowerSyncDialect.kt +++ b/dialect/src/main/kotlin/com/powersync/sqlite/PowerSyncDialect.kt @@ -16,7 +16,7 @@ public class PowerSyncTypeResolver(private val parentResolver: TypeResolver) : TypeResolver by SqliteTypeResolver(parentResolver) { override fun functionType(functionExpr: SqlFunctionExpr): IntermediateType? { when (functionExpr.functionName.text) { - "sqlite_version", "powersync_rs_version", "powersync_replace_schema" -> return IntermediateType( + "sqlite_version", "powersync_rs_version", "powersync_replace_schema", "powersync_clear" -> return IntermediateType( PrimitiveType.TEXT ) } diff --git a/persistence/src/commonMain/sqldelight/com/persistence/Powersync.sq b/persistence/src/commonMain/sqldelight/com/persistence/Powersync.sq index 34862214..f27815b6 100644 --- a/persistence/src/commonMain/sqldelight/com/persistence/Powersync.sq +++ b/persistence/src/commonMain/sqldelight/com/persistence/Powersync.sq @@ -8,6 +8,9 @@ SELECT powersync_rs_version(); replaceSchema: SELECT powersync_replace_schema(?); +powersyncClear: +SELECT powersync_clear(?); + -- CRUD operations hasCrud: SELECT 1 FROM ps_crud LIMIT 1;