Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading