-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtable_info.rs
More file actions
336 lines (288 loc) · 9.53 KB
/
table_info.rs
File metadata and controls
336 lines (288 loc) · 9.53 KB
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
use alloc::string::ToString;
use alloc::vec;
use alloc::{collections::btree_set::BTreeSet, format, string::String, vec::Vec};
use serde::{Deserialize, de::Visitor};
#[derive(Deserialize)]
pub struct Table {
pub name: String,
#[serde(rename = "view_name")]
pub view_name_override: Option<String>,
pub columns: Vec<Column>,
#[serde(default)]
pub indexes: Vec<Index>,
#[serde(
default,
rename = "include_old",
deserialize_with = "deserialize_include_old"
)]
pub diff_include_old: Option<DiffIncludeOld>,
#[serde(flatten)]
pub flags: TableInfoFlags,
}
#[derive(Deserialize)]
pub struct RawTable {
pub name: String,
pub put: PendingStatement,
pub delete: PendingStatement,
#[serde(default)]
pub clear: Option<String>,
}
impl Table {
pub fn view_name(&self) -> &str {
self.view_name_override
.as_deref()
.unwrap_or(self.name.as_str())
}
pub fn local_only(&self) -> bool {
self.flags.local_only()
}
pub fn internal_name(&self) -> String {
if self.local_only() {
format!("ps_data_local__{:}", self.name)
} else {
format!("ps_data__{:}", self.name)
}
}
pub fn filtered_columns<'a>(
&'a self,
names: impl Iterator<Item = &'a str>,
) -> impl Iterator<Item = &'a Column> {
// First, sort all columns by name for faster lookups by name.
let mut sorted_by_name: Vec<&Column> = self.columns.iter().collect();
sorted_by_name.sort_by_key(|c| &*c.name);
names.filter_map(move |name| {
let index = sorted_by_name
.binary_search_by_key(&name, |c| c.name.as_str())
.ok()?;
Some(sorted_by_name[index])
})
}
}
#[derive(Deserialize)]
pub struct Column {
pub name: String,
#[serde(rename = "type")]
pub type_name: String,
}
#[derive(Deserialize)]
pub struct Index {
pub name: String,
pub columns: Vec<IndexedColumn>,
}
#[derive(Deserialize)]
pub struct IndexedColumn {
pub name: String,
pub ascending: bool,
#[serde(rename = "type")]
pub type_name: String,
}
pub enum DiffIncludeOld {
OnlyForColumns { columns: Vec<String> },
ForAllColumns,
}
fn deserialize_include_old<'de, D: serde::Deserializer<'de>>(
deserializer: D,
) -> Result<Option<DiffIncludeOld>, D::Error> {
struct IncludeOldVisitor;
impl<'de> Visitor<'de> for IncludeOldVisitor {
type Value = Option<DiffIncludeOld>;
fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(formatter, "an array of columns, or true")
}
fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where
D: serde::Deserializer<'de>,
{
deserializer.deserialize_any(self)
}
fn visit_none<E>(self) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
return Ok(None);
}
fn visit_bool<E>(self, v: bool) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(if v {
Some(DiffIncludeOld::ForAllColumns)
} else {
None
})
}
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: serde::de::SeqAccess<'de>,
{
let mut elements: Vec<String> = vec![];
while let Some(next) = seq.next_element::<String>()? {
elements.push(next);
}
Ok(Some(DiffIncludeOld::OnlyForColumns { columns: elements }))
}
}
deserializer.deserialize_option(IncludeOldVisitor)
}
#[derive(Clone, Copy)]
#[repr(transparent)]
pub struct TableInfoFlags(pub u32);
impl TableInfoFlags {
pub const LOCAL_ONLY: u32 = 1;
pub const INSERT_ONLY: u32 = 2;
pub const INCLUDE_METADATA: u32 = 4;
pub const INCLUDE_OLD_ONLY_WHEN_CHANGED: u32 = 8;
pub const IGNORE_EMPTY_UPDATE: u32 = 16;
pub const fn local_only(self) -> bool {
self.0 & Self::LOCAL_ONLY != 0
}
pub const fn insert_only(self) -> bool {
// Note: insert_only is incompatible with local_only. For backwards compatibility, we want
// to silently ignore insert_only if local_only is set.
if self.local_only() {
return false;
}
self.0 & Self::INSERT_ONLY != 0
}
pub const fn include_metadata(self) -> bool {
self.0 & Self::INCLUDE_METADATA != 0
}
pub const fn include_old_only_when_changed(self) -> bool {
self.0 & Self::INCLUDE_OLD_ONLY_WHEN_CHANGED != 0
}
pub const fn ignore_empty_update(self) -> bool {
self.0 & Self::IGNORE_EMPTY_UPDATE != 0
}
const fn with_flag(self, flag: u32) -> Self {
Self(self.0 | flag)
}
const fn without_flag(self, flag: u32) -> Self {
Self(self.0 & !flag)
}
const fn set_flag(self, flag: u32, enable: bool) -> Self {
if enable {
self.with_flag(flag)
} else {
self.without_flag(flag)
}
}
}
impl Default for TableInfoFlags {
fn default() -> Self {
Self(0)
}
}
impl<'de> Deserialize<'de> for TableInfoFlags {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
struct FlagsVisitor;
impl<'de> Visitor<'de> for FlagsVisitor {
type Value = TableInfoFlags;
fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(formatter, "an object with table flags")
}
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
where
A: serde::de::MapAccess<'de>,
{
let mut flags = TableInfoFlags::default();
while let Some((key, value)) = map.next_entry::<&'de str, bool>()? {
flags = flags.set_flag(
match key {
"local_only" => TableInfoFlags::LOCAL_ONLY,
"insert_only" => TableInfoFlags::INSERT_ONLY,
"include_metadata" => TableInfoFlags::INCLUDE_METADATA,
"include_old_only_when_changed" => {
TableInfoFlags::INCLUDE_OLD_ONLY_WHEN_CHANGED
}
"ignore_empty_update" => TableInfoFlags::IGNORE_EMPTY_UPDATE,
_ => continue,
},
value,
);
}
Ok(flags)
}
}
deserializer.deserialize_struct(
"TableInfoFlags",
&[
"local_only",
"insert_only",
"include_metadata",
"include_old_only_when_changed",
"ignore_empty_update",
],
FlagsVisitor,
)
}
}
pub struct PendingStatement {
pub sql: String,
/// This vec should contain an entry for each parameter in [sql].
pub params: Vec<PendingStatementValue>,
/// Present if this statement has a [PendingStatementValue::Rest] parameter.
pub named_parameters_index: Option<RestColumnIndex>,
}
pub struct RestColumnIndex {
/// All column names referenced by this statement.
pub named_parameters: BTreeSet<String>,
/// Parameter indices that should be bound to a JSON object containing those values from the
/// source row that haven't been referenced by [PendingStatementValue::Column].
pub rest_parameter_positions: Vec<usize>,
}
impl<'de> Deserialize<'de> for PendingStatement {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
#[derive(Deserialize)]
struct PendingStatementSource {
pub sql: String,
/// This vec should contain an entry for each parameter in [sql].
pub params: Vec<PendingStatementValue>,
}
let source = PendingStatementSource::deserialize(deserializer)?;
let mut named_parameters_index = None;
if source
.params
.iter()
.any(|s| matches!(s, PendingStatementValue::Rest))
{
let mut set = BTreeSet::new();
let mut rest_parameter_positions = vec![];
for (i, column) in source.params.iter().enumerate() {
set.insert(match column {
PendingStatementValue::Id => "id".to_string(),
PendingStatementValue::Column(name) => name.clone(),
PendingStatementValue::Rest => {
rest_parameter_positions.push(i);
continue;
}
});
}
named_parameters_index = Some(RestColumnIndex {
named_parameters: set,
rest_parameter_positions,
});
}
return Ok(Self {
sql: source.sql,
params: source.params,
named_parameters_index,
});
}
}
#[derive(Deserialize)]
pub enum PendingStatementValue {
/// Bind to the PowerSync row id of the affected row.
Id,
/// Bind to the value of column in the synced row.
Column(String),
/// Bind to a JSON object containing all columns from the synced row that haven't been matched
/// by other statement values.
Rest,
// TODO: Stuff like a raw object of put data?
}