From 4b5f09c23e3759412b88b31472f20208a13e3369 Mon Sep 17 00:00:00 2001 From: Dominic Gunther Bauer <46312751+DominicGBauer@users.noreply.github.com> Date: Fri, 20 Sep 2024 20:22:38 +0200 Subject: [PATCH] chore: add changes to disconnectAndClear (#58) * chore: add changes to disconnectAndClear * chore: add dialect and custom function to sq file --------- Co-authored-by: DominicGBauer --- .../com/powersync/db/PowerSyncDatabaseImpl.kt | 15 +++-------- dialect/README.md | 27 +++++++++++++++++++ .../com/powersync/sqlite/PowerSyncDialect.kt | 2 +- .../sqldelight/com/persistence/Powersync.sq | 3 +++ 4 files changed, 34 insertions(+), 13 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 97788328..0cddc036 100644 --- a/core/src/commonMain/kotlin/com/powersync/db/PowerSyncDatabaseImpl.kt +++ b/core/src/commonMain/kotlin/com/powersync/db/PowerSyncDatabaseImpl.kt @@ -284,19 +284,10 @@ internal class PowerSyncDatabaseImpl( override suspend fun disconnectAndClear(clearLocal: Boolean) { 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)}") - } + this.writeTransaction { + internalDb.queries.powersyncClear(if(clearLocal) "1" else "0").awaitAsOne() } + currentStatus.update(lastSyncedAt = null, hasSynced = false) } private suspend fun updateHasSynced() { 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;