-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPowerSyncDatabase.cs
782 lines (661 loc) · 24.7 KB
/
PowerSyncDatabase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
namespace PowerSync.Common.Client;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Newtonsoft.Json;
using PowerSync.Common.Client.Connection;
using PowerSync.Common.Client.Sync.Bucket;
using PowerSync.Common.Client.Sync.Stream;
using PowerSync.Common.DB;
using PowerSync.Common.DB.Crud;
using PowerSync.Common.DB.Schema;
using PowerSync.Common.MDSQLite;
using PowerSync.Common.Utils;
public class BasePowerSyncDatabaseOptions()
{
/// <summary>
/// Schema used for the local database.
/// </summary>
public Schema Schema { get; set; } = null!;
public ILogger? Logger { get; set; } = null!;
}
public interface IDatabaseSource { }
public class DBAdapterSource(IDBAdapter Adapter) : IDatabaseSource
{
public IDBAdapter Adapter { get; init; } = Adapter;
}
public class OpenFactorySource(ISQLOpenFactory Factory) : IDatabaseSource
{
public ISQLOpenFactory Factory { get; init; } = Factory;
}
public class PowerSyncDatabaseOptions() : BasePowerSyncDatabaseOptions()
{
/// <summary>
/// Source for a SQLite database connection.
/// </summary>
public IDatabaseSource Database { get; set; } = null!;
}
public class PowerSyncDBEvent : StreamingSyncImplementationEvent
{
public bool? Initialized { get; set; }
public Schema? SchemaChanged { get; set; }
}
public interface IPowerSyncDatabase : IEventStream<PowerSyncDBEvent>
{
public Task Connect(IPowerSyncBackendConnector connector, PowerSyncConnectionOptions? options = null);
public Task<string> GetClientId();
public Task<CrudBatch?> GetCrudBatch(int limit);
public Task<CrudTransaction?> GetNextCrudTransaction();
Task<NonQueryResult> Execute(string query, object[]? parameters = null);
Task<T[]> GetAll<T>(string sql, params object[]? parameters);
Task<T?> GetOptional<T>(string sql, params object[]? parameters);
Task<T> Get<T>(string sql, params object[]? parameters);
Task<T> ReadLock<T>(Func<ILockContext, Task<T>> fn, DBLockOptions? options = null);
Task<T> ReadTransaction<T>(Func<ITransaction, Task<T>> fn, DBLockOptions? options = null);
Task WriteLock(Func<ILockContext, Task> fn, DBLockOptions? options = null);
Task<T> WriteLock<T>(Func<ILockContext, Task<T>> fn, DBLockOptions? options = null);
Task WriteTransaction(Func<ITransaction, Task> fn, DBLockOptions? options = null);
Task<T> WriteTransaction<T>(Func<ITransaction, Task<T>> fn, DBLockOptions? options = null);
}
public class PowerSyncDatabase : EventStream<PowerSyncDBEvent>, IPowerSyncDatabase
{
public IDBAdapter Database;
private Schema schema;
private static readonly int DEFAULT_WATCH_THROTTLE_MS = 30;
private static readonly Regex POWERSYNC_TABLE_MATCH = new Regex(@"(^ps_data__|^ps_data_local__)", RegexOptions.Compiled);
public bool Closed;
public bool Ready;
protected Task isReadyTask;
private StreamingSyncImplementation? syncStreamImplementation;
public string SdkVersion;
protected IBucketStorageAdapter BucketStorageAdapter;
protected CancellationTokenSource? syncStreamStatusCts;
protected SyncStatus CurrentStatus;
public ILogger Logger;
public PowerSyncDatabase(PowerSyncDatabaseOptions options)
{
if (options.Database is DBAdapterSource adapterSource)
{
Database = adapterSource.Adapter;
}
else if (options.Database is OpenFactorySource factorySource)
{
Database = factorySource.Factory.OpenDatabase();
}
else if (options.Database is SQLOpenOptions openOptions)
{
Database = new MDSQLiteAdapter(new MDSQLiteAdapterOptions
{
Name = openOptions.DbFilename,
SqliteOptions = null
});
}
else
{
throw new ArgumentException("The provided `Database` option is invalid.");
}
Logger = options.Logger ?? NullLogger.Instance;
CurrentStatus = new SyncStatus(new SyncStatusOptions());
BucketStorageAdapter = generateBucketStorageAdapter();
Closed = false;
Ready = false;
schema = options.Schema;
SdkVersion = "";
isReadyTask = Initialize();
}
protected IBucketStorageAdapter generateBucketStorageAdapter()
{
return new SqliteBucketStorage(Database, Logger);
}
/// <summary>
/// Resolves once initialization is completed.
/// </summary>
public async Task WaitForReady()
{
if (Ready)
{
return;
}
await isReadyTask;
}
public async Task WaitForFirstSync(CancellationToken? cancellationToken = null)
{
if (CurrentStatus.HasSynced == true)
{
return;
}
var tcs = new TaskCompletionSource<bool>();
var cts = new CancellationTokenSource();
var _ = Task.Run(() =>
{
foreach (var update in Listen(cts.Token))
{
if (update.StatusChanged?.HasSynced == true)
{
cts.Cancel();
tcs.SetResult(true);
}
}
});
cancellationToken?.Register(() =>
{
cts.Cancel();
tcs.SetCanceled();
});
await tcs.Task;
}
protected async Task Initialize()
{
await BucketStorageAdapter.Init();
await LoadVersion();
await UpdateSchema(schema);
await UpdateHasSynced();
await Database.Execute("PRAGMA RECURSIVE_TRIGGERS=TRUE");
Ready = true;
Emit(new PowerSyncDBEvent { Initialized = true });
}
private record VersionResult(string version);
private async Task LoadVersion()
{
string sdkVersion = (await Database.Get<VersionResult>("SELECT powersync_rs_version() as version")).version;
SdkVersion = sdkVersion;
int[] versionInts;
try
{
versionInts = [.. sdkVersion
.Split(['.', '/'], StringSplitOptions.RemoveEmptyEntries)
.Take(3)
.Select(n => int.Parse(n))];
}
catch (Exception e)
{
throw new Exception(
$"Unsupported PowerSync extension version. Need >=0.2.0 <1.0.0, got: {sdkVersion}. Details: {e.Message}"
);
}
// Validate version is >= 0.2.0 and < 1.0.0
if (versionInts[0] != 0 || versionInts[1] < 2 || versionInts[2] < 0)
{
throw new Exception($"Unsupported PowerSync extension version. Need >=0.2.0 <1.0.0, got: {sdkVersion}");
}
}
private record LastSyncedResult(string? synced_at);
protected async Task UpdateHasSynced()
{
var result = await Database.Get<LastSyncedResult>("SELECT powersync_last_synced_at() as synced_at");
var hasSynced = result.synced_at != null;
DateTime? syncedAt = result.synced_at != null ? DateTime.Parse(result.synced_at + "Z") : null;
if (hasSynced != CurrentStatus.HasSynced)
{
CurrentStatus = new SyncStatus(new SyncStatusOptions(CurrentStatus.Options)
{
HasSynced = hasSynced,
LastSyncedAt = syncedAt
});
Emit(new PowerSyncDBEvent { StatusChanged = CurrentStatus });
}
}
/// <summary>
/// Replace the schema with a new version. This is for advanced use cases - typically the schema should just be specified once in the constructor.
/// Cannot be used while connected - this should only be called before <see cref="Connect"/>.
/// </summary>
public async Task UpdateSchema(Schema schema)
{
if (syncStreamImplementation != null)
{
throw new Exception("Cannot update schema while connected");
}
try
{
schema.Validate();
}
catch (Exception ex)
{
Logger.LogWarning("Schema validation failed. Unexpected behavior could occur: {Exception}", ex);
}
this.schema = schema;
await Database.Execute("SELECT powersync_replace_schema(?)", [schema.ToJSON()]);
await Database.RefreshSchema();
Emit(new PowerSyncDBEvent { SchemaChanged = schema });
}
/// <summary>
/// Wait for initialization to complete.
/// While initializing is automatic, this helps to catch and report initialization errors.
/// </summary>
public async Task Init()
{
await WaitForReady();
}
private RequiredAdditionalConnectionOptions resolveConnectionOptions(PowerSyncConnectionOptions? options)
{
var defaults = RequiredAdditionalConnectionOptions.DEFAULT_ADDITIONAL_CONNECTION_OPTIONS;
return new RequiredAdditionalConnectionOptions
{
RetryDelayMs = options?.RetryDelayMs ?? defaults.RetryDelayMs,
CrudUploadThrottleMs = options?.CrudUploadThrottleMs ?? defaults.CrudUploadThrottleMs,
};
}
public async Task Connect(IPowerSyncBackendConnector connector, PowerSyncConnectionOptions? options = null)
{
await WaitForReady();
// close connection if one is open
await Disconnect();
if (Closed)
{
throw new Exception("Cannot connect using a closed client");
}
var resolvedOptions = resolveConnectionOptions(options);
syncStreamImplementation = new StreamingSyncImplementation(new StreamingSyncImplementationOptions
{
Adapter = BucketStorageAdapter,
Remote = new Remote(connector),
UploadCrud = async () =>
{
await WaitForReady();
await connector.UploadData(this);
},
RetryDelayMs = resolvedOptions.RetryDelayMs,
CrudUploadThrottleMs = resolvedOptions.CrudUploadThrottleMs,
Logger = Logger
});
syncStreamStatusCts = new CancellationTokenSource();
var _ = Task.Run(() =>
{
foreach (var update in syncStreamImplementation.Listen(syncStreamStatusCts.Token))
{
if (update.StatusChanged != null)
{
CurrentStatus = new SyncStatus(new SyncStatusOptions(update.StatusChanged.Options)
{
HasSynced = CurrentStatus?.HasSynced == true || update.StatusChanged.LastSyncedAt != null,
});
Emit(new PowerSyncDBEvent { StatusChanged = CurrentStatus });
}
}
});
await syncStreamImplementation.WaitForReady();
syncStreamImplementation.TriggerCrudUpload();
await syncStreamImplementation.Connect(options);
}
public async Task Disconnect()
{
await WaitForReady();
if (syncStreamImplementation != null)
{
await syncStreamImplementation.Disconnect();
syncStreamImplementation.Close();
syncStreamImplementation = null;
}
syncStreamStatusCts?.Cancel();
}
/// <summary>
/// Disconnect and clear the database.
/// Use this when logging out.
/// The database can still be queried after this is called, but the tables
/// would be empty.
///
/// To preserve data in local-only tables, set clearLocal to false.
/// </summary>
public async Task DisconnectAndClear(bool clearLocal = true)
{
await Disconnect();
await WaitForReady();
await Database.WriteTransaction(async tx =>
{
await tx.Execute("SELECT powersync_clear(?)", [clearLocal ? 1 : 0]);
});
// The data has been deleted - reset the sync status
CurrentStatus = new SyncStatus(new SyncStatusOptions());
Emit(new PowerSyncDBEvent { StatusChanged = CurrentStatus });
}
/// <summary>
/// Close the database, releasing resources.
///
/// Also disconnects any active connection.
///
/// Once close is called, this connection cannot be used again - a new one
/// must be constructed.
/// </summary>
public new async Task Close()
{
await WaitForReady();
if (Closed) return;
await Disconnect();
base.Close();
syncStreamImplementation?.Close();
BucketStorageAdapter?.Close();
Database.Close();
Closed = true;
}
/// <summary>
/// Get a batch of crud data to upload.
/// <para />
/// Returns null if there is no data to upload.
/// <para />
/// Use this from the <see cref="IPowerSyncBackendConnector.UploadData"/> callback.
///
/// Once the data have been successfully uploaded, call <see cref="CrudBatch.Complete"/> before
/// requesting the next batch.
/// <para />
/// Use <paramref name="limit"/> to specify the maximum number of updates to return in a single
/// batch.
/// <para />
/// This method does include transaction ids in the result, but does not group
/// data by transaction. One batch may contain data from multiple transactions,
/// and a single transaction may be split over multiple batches.
/// </summary>
public async Task<CrudBatch?> GetCrudBatch(int limit = 100)
{
var crudResult = await GetAll<CrudEntryJSON>($"SELECT id, tx_id, data FROM {PSInternalTable.CRUD} ORDER BY id ASC LIMIT ?", [limit + 1]);
var all = crudResult.Select(CrudEntry.FromRow).ToList();
var haveMore = false;
if (all.Count > limit)
{
all.RemoveAt(all.Count - 1);
haveMore = true;
}
if (all.Count == 0)
{
return null;
}
var last = all[all.Count - 1];
return new CrudBatch(
[.. all],
haveMore,
async writeCheckpoint => await HandleCrudCheckpoint(last.ClientId, writeCheckpoint)
);
}
/// <summary>
/// Get the next recorded transaction to upload.
/// <para />
/// Returns null if there is no data to upload.
///
/// Use this from the <see cref="IPowerSyncBackendConnector.UploadData"/> callback.
/// <para />
/// Once the data have been successfully uploaded, call <see cref="CrudTransaction.Complete"/> before
/// requesting the next transaction.
/// <para />
/// Unlike <see cref="GetCrudBatch"/>, this only returns data from a single transaction at a time.
/// All data for the transaction is loaded into memory.
/// </summary>
public async Task<CrudTransaction?> GetNextCrudTransaction()
{
return await ReadTransaction(async tx =>
{
var first = await tx.GetOptional<CrudEntryJSON>(
$"SELECT id, tx_id, data FROM {PSInternalTable.CRUD} ORDER BY id ASC LIMIT 1");
if (first == null)
{
return null;
}
long? txId = first.TransactionId ?? null;
List<CrudEntry> all;
if (txId == null)
{
all = [CrudEntry.FromRow(first)];
}
else
{
var result = await tx.GetAll<CrudEntryJSON>(
$"SELECT id, tx_id, data FROM {PSInternalTable.CRUD} WHERE tx_id = ? ORDER BY id ASC",
[txId]);
all = result.Select(CrudEntry.FromRow).ToList();
}
var last = all.Last();
return new CrudTransaction(
[.. all],
async writeCheckpoint => await HandleCrudCheckpoint(last.ClientId, writeCheckpoint),
txId
);
});
}
public async Task HandleCrudCheckpoint(long lastClientId, string? writeCheckpoint = null)
{
await Database.WriteTransaction(async (tx) =>
{
await tx.Execute($"DELETE FROM {PSInternalTable.CRUD} WHERE id <= ?", [lastClientId]);
if (!string.IsNullOrEmpty(writeCheckpoint))
{
var check = await tx.GetAll<object>($"SELECT 1 FROM {PSInternalTable.CRUD} LIMIT 1");
if (check.Length == 0)
{
await tx.Execute($"UPDATE {PSInternalTable.BUCKETS} SET target_op = CAST(? as INTEGER) WHERE name='$local'", [
writeCheckpoint
]);
}
}
else
{
await tx.Execute(
$"UPDATE {PSInternalTable.BUCKETS} SET target_op = CAST(? as INTEGER) WHERE name = '$local'",
[BucketStorageAdapter.GetMaxOpId()]);
}
});
}
/// <summary>
/// Get an unique client id for this database.
///
/// The id is not reset when the database is cleared, only when the database is deleted.
/// </summary>
public async Task<string> GetClientId()
{
return await BucketStorageAdapter.GetClientId();
}
public async Task<NonQueryResult> Execute(string query, object[]? parameters = null)
{
await WaitForReady();
return await Database.Execute(query, parameters);
}
public async Task<T[]> GetAll<T>(string query, object[]? parameters = null)
{
await WaitForReady();
return await Database.GetAll<T>(query, parameters);
}
public async Task<T?> GetOptional<T>(string query, object[]? parameters = null)
{
await WaitForReady();
return await Database.GetOptional<T>(query, parameters);
}
public async Task<T> Get<T>(string query, object[]? parameters = null)
{
await WaitForReady();
return await Database.Get<T>(query, parameters);
}
public async Task<T> ReadLock<T>(Func<ILockContext, Task<T>> fn, DBLockOptions? options = null)
{
await WaitForReady();
return await Database.ReadLock(fn, options);
}
public async Task WriteLock(Func<ILockContext, Task> fn, DBLockOptions? options = null)
{
await WaitForReady();
await Database.WriteLock(fn, options);
}
public async Task<T> WriteLock<T>(Func<ILockContext, Task<T>> fn, DBLockOptions? options = null)
{
await WaitForReady();
return await Database.WriteLock(fn, options);
}
public async Task<T> ReadTransaction<T>(Func<ITransaction, Task<T>> fn, DBLockOptions? options = null)
{
await WaitForReady();
return await Database.ReadTransaction(fn, options);
}
public async Task WriteTransaction(Func<ITransaction, Task> fn, DBLockOptions? options = null)
{
await WaitForReady();
await Database.WriteTransaction(fn, options);
}
public async Task<T> WriteTransaction<T>(Func<ITransaction, Task<T>> fn, DBLockOptions? options = null)
{
await WaitForReady();
return await Database.WriteTransaction(fn, options);
}
/// <summary>
/// Executes a read query every time the source tables are modified.
/// <para />
/// Use <see cref="SQLWatchOptions.ThrottleMs"/> to specify the minimum interval between queries.
/// Source tables are automatically detected using <c>EXPLAIN QUERY PLAN</c>.
/// </summary>
public Task Watch<T>(string query, object[]? parameters, WatchHandler<T> handler, SQLWatchOptions? options = null)
{
var tcs = new TaskCompletionSource<bool>();
Task.Run(async () =>
{
try
{
var resolvedTables = await ResolveTables(query, parameters, options);
var result = await GetAll<T>(query, parameters);
handler.OnResult(result);
OnChange(new WatchOnChangeHandler
{
OnChange = async (change) =>
{
try
{
var result = await GetAll<T>(query, parameters);
handler.OnResult(result);
}
catch (Exception ex)
{
handler.OnError?.Invoke(ex);
}
},
OnError = handler.OnError
}, new SQLWatchOptions
{
Tables = resolvedTables,
Signal = options?.Signal,
ThrottleMs = options?.ThrottleMs
});
tcs.SetResult(true);
}
catch (Exception ex)
{
handler.OnError?.Invoke(ex);
}
});
return tcs.Task;
}
private record ExplainedResult(string opcode, int p2, int p3);
private record TableSelectResult(string tbl_name);
public async Task<string[]> ResolveTables(string sql, object[]? parameters = null, SQLWatchOptions? options = null)
{
List<string> resolvedTables = options?.Tables != null ? [.. options.Tables] : [];
if (options?.Tables == null)
{
var explained = await GetAll<ExplainedResult>(
$"EXPLAIN {sql}", parameters
);
var rootPages = explained
.Where(row => row.opcode == "OpenRead" && row.p3 == 0)
.Select(row => row.p2)
.ToList();
var tables = await GetAll<TableSelectResult>(
"SELECT DISTINCT tbl_name FROM sqlite_master WHERE rootpage IN (SELECT json_each.value FROM json_each(?))",
[JsonConvert.SerializeObject(rootPages)]
);
foreach (var table in tables)
{
resolvedTables.Add(POWERSYNC_TABLE_MATCH.Replace(table.tbl_name, ""));
}
}
return [.. resolvedTables];
}
/// <summary>
/// Invokes the provided callback whenever any of the specified tables are modified.
/// <para />
/// This is preferred over <see cref="Watch"/> when multiple queries need to be performed
/// together in response to data changes.
/// </summary>
public void OnChange(WatchOnChangeHandler handler, SQLWatchOptions? options = null)
{
var resolvedOptions = options ?? new SQLWatchOptions();
string[] tables = resolvedOptions.Tables ?? [];
HashSet<string> watchedTables = [.. tables.SelectMany(table => new[] { table, $"ps_data__{table}", $"ps_data_local__{table}" })];
var changedTables = new HashSet<string>();
var resolvedThrottleMs = resolvedOptions.ThrottleMs ?? DEFAULT_WATCH_THROTTLE_MS;
void flushTableUpdates()
{
HandleTableChanges(changedTables, watchedTables, (intersection) =>
{
if (resolvedOptions?.Signal?.IsCancellationRequested == true) return;
handler.OnChange(new WatchOnChangeEvent { ChangedTables = intersection });
});
}
var cts = Database.RunListener((update) =>
{
if (update.TablesUpdated != null)
{
try
{
ProcessTableUpdates(update.TablesUpdated, changedTables);
flushTableUpdates();
}
catch (Exception ex)
{
handler?.OnError?.Invoke(ex);
}
}
});
if (options?.Signal.HasValue == true)
{
options.Signal.Value.Register(() =>
{
cts.Cancel();
});
}
}
private static void HandleTableChanges(HashSet<string> changedTables, HashSet<string> watchedTables, Action<string[]> onDetectedChanges)
{
if (changedTables.Count > 0)
{
var intersection = changedTables.Where(watchedTables.Contains).ToArray();
if (intersection.Length > 0)
{
onDetectedChanges(intersection);
}
}
changedTables.Clear();
}
private static void ProcessTableUpdates(INotification updateNotification, HashSet<string> changedTables)
{
string[] tables = [];
if (updateNotification is BatchedUpdateNotification batchedUpdate)
{
tables = batchedUpdate.Tables;
}
else if (updateNotification is UpdateNotification singleUpdate)
{
tables = [singleUpdate.Table];
}
foreach (var table in tables)
{
changedTables.Add(table);
}
}
}
public class SQLWatchOptions
{
public CancellationToken? Signal { get; set; }
public string[]? Tables { get; set; }
/// <summary>
/// The minimum interval between queries in milliseconds.
/// </summary>
public int? ThrottleMs { get; set; }
}
public class WatchHandler<T>
{
public Action<T[]> OnResult { get; set; } = null!;
public Action<Exception>? OnError { get; set; }
}
public class WatchOnChangeEvent
{
public string[] ChangedTables { get; set; } = [];
}
public class WatchOnChangeHandler
{
public Func<WatchOnChangeEvent, Task> OnChange { get; set; } = null!;
public Action<Exception>? OnError { get; set; }
}