| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 16 |
pub enum ConflictStrategy { |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
HybridLogicalClock, |
| 21 |
|
| 22 |
|
| 23 |
ServerOrder, |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
#[derive(Debug, Clone, PartialEq, Eq)] |
| 28 |
pub enum SyncMode { |
| 29 |
|
| 30 |
|
| 31 |
Full, |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
PartialUpdate { set: &'static [&'static str] }, |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
#[derive(Debug, Clone, PartialEq, Eq)] |
| 41 |
pub enum DeleteMode { |
| 42 |
|
| 43 |
Hard, |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
Ignore, |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
Tombstone { column: &'static str }, |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 58 |
pub enum RowIdScheme { |
| 59 |
|
| 60 |
|
| 61 |
PrimaryKey, |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
Hashed, |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
#[derive(Debug, Clone)] |
| 75 |
pub struct SyncTable { |
| 76 |
pub(crate) name: &'static str, |
| 77 |
pub(crate) columns: &'static [&'static str], |
| 78 |
pub(crate) pk: &'static [&'static str], |
| 79 |
pub(crate) row_id: RowIdScheme, |
| 80 |
pub(crate) mode: SyncMode, |
| 81 |
pub(crate) deletes: DeleteMode, |
| 82 |
pub(crate) preserve_local: &'static [&'static str], |
| 83 |
pub(crate) insert_defaults: &'static [(&'static str, &'static str)], |
| 84 |
pub(crate) references_unsynced: bool, |
| 85 |
pub(crate) exclude_where: Option<&'static str>, |
| 86 |
pub(crate) group_scope: Option<&'static str>, |
| 87 |
} |
| 88 |
|
| 89 |
impl SyncTable { |
| 90 |
|
| 91 |
pub fn name(&self) -> &'static str { |
| 92 |
self.name |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
pub fn group_scope(&self) -> Option<&'static str> { |
| 99 |
self.group_scope |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
pub fn full(name: &'static str, columns: &'static [&'static str]) -> Self { |
| 106 |
Self { |
| 107 |
name, |
| 108 |
columns, |
| 109 |
pk: &["id"], |
| 110 |
row_id: RowIdScheme::PrimaryKey, |
| 111 |
mode: SyncMode::Full, |
| 112 |
deletes: DeleteMode::Hard, |
| 113 |
preserve_local: &[], |
| 114 |
insert_defaults: &[], |
| 115 |
references_unsynced: false, |
| 116 |
exclude_where: None, |
| 117 |
group_scope: None, |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
#[must_use] |
| 124 |
pub fn pk(mut self, pk: &'static [&'static str]) -> Self { |
| 125 |
self.pk = pk; |
| 126 |
self |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
#[must_use] |
| 132 |
pub fn hashed(mut self) -> Self { |
| 133 |
self.row_id = RowIdScheme::Hashed; |
| 134 |
self |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
#[must_use] |
| 140 |
pub fn tombstone(mut self, column: &'static str) -> Self { |
| 141 |
self.deletes = DeleteMode::Tombstone { column }; |
| 142 |
self |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
#[must_use] |
| 147 |
pub fn ignore_deletes(mut self) -> Self { |
| 148 |
self.deletes = DeleteMode::Ignore; |
| 149 |
self |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
#[must_use] |
| 154 |
pub fn partial_update(mut self, set: &'static [&'static str]) -> Self { |
| 155 |
self.mode = SyncMode::PartialUpdate { set }; |
| 156 |
self |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
#[must_use] |
| 161 |
pub fn preserve_local(mut self, cols: &'static [&'static str]) -> Self { |
| 162 |
self.preserve_local = cols; |
| 163 |
self |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
#[must_use] |
| 169 |
pub fn insert_defaults(mut self, defaults: &'static [(&'static str, &'static str)]) -> Self { |
| 170 |
self.insert_defaults = defaults; |
| 171 |
self |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
#[must_use] |
| 177 |
pub fn references_unsynced(mut self) -> Self { |
| 178 |
self.references_unsynced = true; |
| 179 |
self |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
#[must_use] |
| 188 |
pub fn exclude_where(mut self, predicate: &'static str) -> Self { |
| 189 |
self.exclude_where = Some(predicate); |
| 190 |
self |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
#[must_use] |
| 202 |
pub fn group_scoped(mut self, col: &'static str) -> Self { |
| 203 |
self.group_scope = Some(col); |
| 204 |
self |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
pub fn emitted_columns(&self) -> Vec<&'static str> { |
| 213 |
match &self.mode { |
| 214 |
SyncMode::Full => self.columns.to_vec(), |
| 215 |
SyncMode::PartialUpdate { set } => { |
| 216 |
let mut cols: Vec<&'static str> = self.pk.to_vec(); |
| 217 |
for c in *set { |
| 218 |
if !cols.contains(c) { |
| 219 |
cols.push(c); |
| 220 |
} |
| 221 |
} |
| 222 |
cols |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
#[derive(Debug, Clone)] |
| 234 |
pub struct SyncSchema { |
| 235 |
pub(crate) tables: Vec<SyncTable>, |
| 236 |
pub(crate) conflict: ConflictStrategy, |
| 237 |
} |
| 238 |
|
| 239 |
impl SyncSchema { |
| 240 |
|
| 241 |
|
| 242 |
pub fn new(tables: Vec<SyncTable>) -> Self { |
| 243 |
Self { |
| 244 |
tables, |
| 245 |
conflict: ConflictStrategy::HybridLogicalClock, |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
#[must_use] |
| 251 |
pub fn conflict_strategy(mut self, conflict: ConflictStrategy) -> Self { |
| 252 |
self.conflict = conflict; |
| 253 |
self |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
pub fn conflict(&self) -> ConflictStrategy { |
| 258 |
self.conflict |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
pub fn tables(&self) -> &[SyncTable] { |
| 263 |
&self.tables |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
pub(crate) fn any_hashed(&self) -> bool { |
| 269 |
self.tables.iter().any(|t| t.row_id == RowIdScheme::Hashed) |
| 270 |
} |
| 271 |
} |
| 272 |
|