Skip to content

Commit

Permalink
chore: add changes to disconnectAndClear (#58)
Browse files Browse the repository at this point in the history
* chore: add changes to disconnectAndClear

* chore: add dialect and custom function to sq file

---------

Co-authored-by: DominicGBauer <[email protected]>
  • Loading branch information
DominicGBauer and DominicGBauer authored Sep 20, 2024
1 parent 984f47a commit 4b5f09c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
27 changes: 27 additions & 0 deletions dialect/README.md
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 4b5f09c

Please sign in to comment.