/
stream_updates.dart
177 lines (148 loc) · 5.62 KB
/
stream_updates.dart
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
part of 'runtime_api.dart';
/// Collects a set of [UpdateRule]s which can be used to express how a set of
/// direct updates to a table affects other updates.
///
/// This is used to implement query streams in databases that have triggers.
class StreamQueryUpdateRules {
/// All rules active in a database.
final List<UpdateRule> rules;
/// Creates a [StreamQueryUpdateRules] from the underlying [rules].
const StreamQueryUpdateRules(this.rules);
/// The default implementation, which doesn't have any rules.
const StreamQueryUpdateRules.none() : this(const []);
/// Obtain a set of all tables that might be affected by direct updates in
/// [input].
Set<TableUpdate> apply(Iterable<TableUpdate> input) {
// Most users don't have any update rules, and this check is much faster
// than crawling through all updates.
if (rules.isEmpty) return input.toSet();
final pending = List.of(input);
final seen = <TableUpdate>{};
while (pending.isNotEmpty) {
final update = pending.removeLast();
seen.add(update);
for (final rule in rules) {
if (rule is WritePropagation && rule.on.matches(update)) {
pending.addAll(rule.result.where((u) => !seen.contains(u)));
}
}
}
return seen;
}
}
/// A common rule that describes how a [TableUpdate] has other [TableUpdate]s.
///
/// Users should not extend or implement this class.
abstract class UpdateRule {
/// Common const constructor so that subclasses can be const.
const UpdateRule._();
}
/// An [UpdateRule] for triggers that exist in a database.
///
/// An update on [on] implicitly triggers updates on [result].
///
/// This class is for use by generated or drift-internal code only. It does not
/// adhere to Semantic Versioning and should not be used manually.
class WritePropagation extends UpdateRule {
/// The updates that cause further writes in [result].
final TableUpdateQuery on;
/// All updates that will be performed by the trigger listening on [on].
final List<TableUpdate> result;
/// Default constructor. See [WritePropagation] for details.
const WritePropagation({required this.on, required this.result}) : super._();
}
/// Classifies a [TableUpdate] by what kind of write happened - an insert, an
/// update or a delete operation.
///
/// This information is used by drift to determine which triggers might be
/// invoked by the write. For instance, an `AFTER UPDATE ON table` trigger would
/// only be considered for [UpdateKind.update].
enum UpdateKind {
/// An insert statement ran on the affected table.
///
/// This will also be used for upserts.
insert,
/// An update statement ran on the affected table.
update,
/// A delete statement ran on the affected table.
delete
}
/// Contains information on how a table was updated, which can be used to find
/// queries that are affected by this.
class TableUpdate {
/// What kind of update was applied to the [table].
///
/// Can be null, which indicates that the update is not known.
final UpdateKind? kind;
/// Name of the table that was updated.
final String table;
/// Default constant constructor.
const TableUpdate(this.table, {this.kind});
/// Creates a [TableUpdate] instance based on a [TableInfo] instead of the raw
/// name.
factory TableUpdate.onTable(TableInfo table, {UpdateKind? kind}) {
return TableUpdate(table.actualTableName, kind: kind);
}
@override
int get hashCode => Object.hash(kind, table);
@override
bool operator ==(Object other) {
return other is TableUpdate && other.kind == kind && other.table == table;
}
@override
String toString() {
return 'TableUpdate($table, kind: $kind)';
}
}
/// A table update query describes information to listen for [TableUpdate]s.
///
/// Users should not extend implement this class.
abstract class TableUpdateQuery {
/// Default const constructor so that subclasses can have constant
/// constructors.
const TableUpdateQuery();
/// A query that listens for all table updates in a database.
const factory TableUpdateQuery.any() = AnyUpdateQuery;
/// A query that listens for all updates that match any query in [queries].
const factory TableUpdateQuery.allOf(List<TableUpdateQuery> queries) =
MultipleUpdateQuery;
/// A query that listens for all updates on a specific [table] by its name.
///
/// The optional [limitUpdateKind] parameter can be used to limit the updates
/// to a certain kind.
const factory TableUpdateQuery.onTableName(String table,
{UpdateKind? limitUpdateKind}) = SpecificUpdateQuery;
/// A query that listens for all updates on a specific [table].
///
/// The optional [limitUpdateKind] parameter can be used to limit the updates
/// to a certain kind.
factory TableUpdateQuery.onTable(ResultSetImplementation table,
{UpdateKind? limitUpdateKind}) {
if (table is ViewInfo) {
return TableUpdateQuery.allOf([
for (final table in table.readTables)
TableUpdateQuery.onTableName(table)
]);
}
return TableUpdateQuery.onTableName(
table.entityName,
limitUpdateKind: limitUpdateKind,
);
}
/// A query that listens for any change on any table in [tables].
factory TableUpdateQuery.onAllTables(
Iterable<ResultSetImplementation> tables) {
return TableUpdateQuery.allOf(
[
for (final table in tables)
if (table is ViewInfo)
for (final table in table.readTables)
TableUpdateQuery.onTableName(table)
else
TableUpdateQuery.onTable(table),
],
);
}
/// Determines whether the [update] would be picked up by this query.
bool matches(TableUpdate update);
}