@@ -8,6 +8,7 @@ use sqlite_nostd::{self as sqlite, Connection, ManagedStmt, ResultCode};
88use crate :: {
99 error:: { PSResult , PowerSyncError } ,
1010 ext:: SafeManagedStmt ,
11+ kv:: client_id,
1112 operations:: delete_bucket,
1213 schema:: Schema ,
1314 state:: DatabaseState ,
@@ -37,6 +38,7 @@ pub struct StorageAdapter {
3738 pub progress_stmt : ManagedStmt ,
3839 time_stmt : ManagedStmt ,
3940 delete_subscription : ManagedStmt ,
41+ update_subscription : ManagedStmt ,
4042}
4143
4244impl StorageAdapter {
@@ -53,11 +55,16 @@ impl StorageAdapter {
5355 let delete_subscription =
5456 db. prepare_v2 ( "DELETE FROM ps_stream_subscriptions WHERE id = ?" ) ?;
5557
58+ // language=SQLite
59+ let update_subscription =
60+ db. prepare_v2 ( "UPDATE ps_stream_subscriptions SET active = ?2, is_default = ?3, ttl = ?, expires_at = ?, last_synced_at = ? WHERE id = ?1" ) ?;
61+
5662 Ok ( Self {
5763 db,
5864 progress_stmt : progress,
5965 time_stmt : time,
6066 delete_subscription,
67+ update_subscription,
6168 } )
6269 }
6370
@@ -283,7 +290,23 @@ impl StorageAdapter {
283290 & self ,
284291 include_defaults : bool ,
285292 ) -> Result < StreamSubscriptionRequest , PowerSyncError > {
293+ self . delete_outdated_subscriptions ( ) ?;
294+
286295 let mut subscriptions: Vec < RequestedStreamSubscription > = Vec :: new ( ) ;
296+ let stmt = self
297+ . db
298+ . prepare_v2 ( "SELECT * FROM ps_stream_subscriptions WHERE NOT is_default;" ) ?;
299+
300+ while let ResultCode :: ROW = stmt. step ( ) ? {
301+ let subscription = Self :: read_stream_subscription ( & stmt) ?;
302+
303+ subscriptions. push ( RequestedStreamSubscription {
304+ stream : subscription. stream_name ,
305+ parameters : subscription. local_params ,
306+ override_priority : subscription. local_priority ,
307+ client_id : subscription. id ,
308+ } ) ;
309+ }
287310
288311 Ok ( StreamSubscriptionRequest {
289312 include_defaults,
@@ -323,6 +346,12 @@ impl StorageAdapter {
323346 } )
324347 }
325348
349+ fn delete_outdated_subscriptions ( & self ) -> Result < ( ) , PowerSyncError > {
350+ self . db
351+ . exec_safe ( "DELETE FROM ps_stream_subscriptions WHERE expires_at < unixepoch()" ) ?;
352+ Ok ( ( ) )
353+ }
354+
326355 pub fn iterate_local_subscriptions < F : FnMut ( LocallyTrackedSubscription ) -> ( ) > (
327356 & self ,
328357 mut action : F ,
@@ -351,6 +380,39 @@ impl StorageAdapter {
351380 }
352381 }
353382
383+ pub fn update_subscription (
384+ & self ,
385+ subscription : & LocallyTrackedSubscription ,
386+ ) -> Result < ( ) , PowerSyncError > {
387+ let _ = self . update_subscription . reset ( ) ;
388+
389+ self . update_subscription . bind_int64 ( 1 , subscription. id ) ?;
390+ self . update_subscription
391+ . bind_int ( 2 , if subscription. active { 1 } else { 0 } ) ?;
392+ self . update_subscription
393+ . bind_int ( 3 , if subscription. is_default { 1 } else { 0 } ) ?;
394+ if let Some ( ttl) = subscription. ttl {
395+ self . update_subscription . bind_int64 ( 4 , ttl) ?;
396+ } else {
397+ self . update_subscription . bind_null ( 4 ) ?;
398+ }
399+
400+ if let Some ( expires_at) = subscription. expires_at {
401+ self . update_subscription . bind_int64 ( 5 , expires_at) ?;
402+ } else {
403+ self . update_subscription . bind_null ( 5 ) ?;
404+ }
405+
406+ if let Some ( last_synced_at) = subscription. last_synced_at {
407+ self . update_subscription . bind_int64 ( 6 , last_synced_at) ?;
408+ } else {
409+ self . update_subscription . bind_null ( 6 ) ?;
410+ }
411+
412+ self . update_subscription . exec ( ) ?;
413+ Ok ( ( ) )
414+ }
415+
354416 pub fn delete_subscription ( & self , id : i64 ) -> Result < ( ) , PowerSyncError > {
355417 let _ = self . delete_subscription . reset ( ) ;
356418 self . delete_subscription . bind_int64 ( 1 , id) ?;
0 commit comments