uf-valence-core 0.1.2

Valence ports: DatabaseBackend, router, builder, host injectable traits
Documentation
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
//! Dialect-specific compiled queries for deletion DAG and ownership helpers.

use serde_json::Value;

use crate::compiled_query::CompiledQuery;
use crate::error::{Error, Result};
use crate::known_engines::KnownEngines;

fn is_sql_family(engine_id: &str) -> bool {
    matches!(
        engine_id,
        KnownEngines::SQLITE
            | KnownEngines::POSTGRES
            | KnownEngines::INMEMORY_MEM
            | KnownEngines::MONGODB
            | KnownEngines::REDIS
            | KnownEngines::INDRADB
            // Hybrid delegates non-hop compiled queries to its SQL primary,
            // so deletion DAG queries use the SQL dialect.
            | KnownEngines::HYBRID_INDRA_SQL
    )
}

fn is_surreal(engine_id: &str) -> bool {
    engine_id == KnownEngines::SURREALDB
}

#[allow(clippy::unnecessary_wraps)] // fallible when compiler-* features are disabled
fn require_compiler(engine_id: &str, family: &str) -> Result<()> {
    let _ = family;
    if is_sql_family(engine_id) {
        #[cfg(not(feature = "compiler-sql"))]
        return Err(Error::Internal(format!(
            "deletion queries for `{engine_id}` require valence-core/compiler-sql ({family})"
        )));
    } else if is_surreal(engine_id) {
        #[cfg(not(feature = "compiler-surreal"))]
        return Err(Error::Internal(format!(
            "deletion queries for surrealdb require valence-core/compiler-surreal ({family})"
        )));
    }
    Ok(())
}

/// Count M2M edge rows where `in` points at `(root_table, bare_root_id)`.
/// # Errors
///
/// Returns an error when the requested operation cannot be completed.
pub fn count_m2m_edges_from_root(
    engine_id: &str,
    edge_table: &str,
    root_table: &str,
    bare_root_id: &str,
) -> Result<CompiledQuery> {
    require_compiler(engine_id, "count_m2m_edges")?;
    if is_surreal(engine_id) {
        let q = format!(
            "SELECT VALUE count FROM (SELECT count() AS count FROM {edge_table} \
             WHERE `in` = type::record($tb, $rid) GROUP ALL)"
        );
        return Ok(CompiledQuery::new(
            q,
            vec![
                ("tb".to_string(), Value::String(root_table.to_string())),
                ("rid".to_string(), Value::String(bare_root_id.to_string())),
            ],
        ));
    }
    if is_sql_family(engine_id) {
        let q = "SELECT COUNT(*) AS count FROM valence_edges \
             WHERE edge_type = $edge_type AND from_table = $tb AND from_id = $rid"
            .to_string();
        return Ok(CompiledQuery::new(
            q,
            vec![
                (
                    "edge_type".to_string(),
                    Value::String(edge_table.to_string()),
                ),
                ("tb".to_string(), Value::String(root_table.to_string())),
                ("rid".to_string(), Value::String(bare_root_id.to_string())),
            ],
        ));
    }
    Ok(CompiledQuery::new(
        format!("/* count_m2m_edges_from_root unsupported for {engine_id} */"),
        vec![],
    ))
}

/// Count rows in `from_table` whose FK equals the target record.
/// # Errors
///
/// Returns an error when the requested operation cannot be completed.
pub fn count_where_thing_eq(
    engine_id: &str,
    from_table: &str,
    fk_field: &str,
    target_table: &str,
    bare_target_id: &str,
) -> Result<CompiledQuery> {
    require_compiler(engine_id, "count_where_thing_eq")?;
    if is_surreal(engine_id) {
        let parent_rid = format!("{target_table}:{bare_target_id}");
        let q = format!(
            "SELECT VALUE count FROM (SELECT count() AS count FROM {from_table} \
             WHERE {fk_field} = $parent_rid OR {fk_field} = type::record($ptb, $prid) GROUP ALL)"
        );
        return Ok(CompiledQuery::new(
            q,
            vec![
                ("parent_rid".to_string(), Value::String(parent_rid)),
                ("ptb".to_string(), Value::String(target_table.to_string())),
                (
                    "prid".to_string(),
                    Value::String(bare_target_id.to_string()),
                ),
            ],
        ));
    }
    if is_sql_family(engine_id) {
        let q = format!(
            "SELECT COUNT(*) AS count FROM {from_table} \
             WHERE json_extract(body, '$.{fk_field}') = $bare_id \
                OR json_extract(body, '$.{fk_field}.id') = $bare_id \
                OR json_extract(body, '$.{fk_field}') = $parent_rid"
        );
        let parent_rid = format!("{target_table}:{bare_target_id}");
        return Ok(CompiledQuery::new(
            q,
            vec![
                (
                    "bare_id".to_string(),
                    Value::String(bare_target_id.to_string()),
                ),
                ("parent_rid".to_string(), Value::String(parent_rid)),
            ],
        ));
    }
    Ok(CompiledQuery::new(
        format!("/* count_where_thing_eq unsupported for {engine_id} */"),
        vec![],
    ))
}

/// Select child ids for HasMany reverse lookup.
/// # Errors
///
/// Returns an error when the requested operation cannot be completed.
pub fn select_child_ids_hasmany(
    engine_id: &str,
    child_table: &str,
    reverse_field: &str,
    parent_table: &str,
    bare_parent_id: &str,
) -> Result<CompiledQuery> {
    require_compiler(engine_id, "select_child_ids_hasmany")?;
    if is_surreal(engine_id) {
        let parent_rid = format!("{parent_table}:{bare_parent_id}");
        let q = format!(
            "SELECT VALUE id FROM {child_table} \
             WHERE {reverse_field} = $parent_rid OR {reverse_field} = type::record($ptb, $prid)"
        );
        return Ok(CompiledQuery::new(
            q,
            vec![
                ("parent_rid".to_string(), Value::String(parent_rid)),
                ("ptb".to_string(), Value::String(parent_table.to_string())),
                (
                    "prid".to_string(),
                    Value::String(bare_parent_id.to_string()),
                ),
            ],
        ));
    }
    if is_sql_family(engine_id) {
        let q = format!(
            "SELECT id FROM {child_table} \
             WHERE json_extract(body, '$.{reverse_field}') = $parent_rid \
                OR json_extract(body, '$.{reverse_field}.id') = $bare_id \
                OR json_extract(body, '$.{reverse_field}') = $bare_id"
        );
        let parent_rid = format!("{parent_table}:{bare_parent_id}");
        return Ok(CompiledQuery::new(
            q,
            vec![
                ("parent_rid".to_string(), Value::String(parent_rid)),
                (
                    "bare_id".to_string(),
                    Value::String(bare_parent_id.to_string()),
                ),
            ],
        ));
    }
    // Third-party / stub engines without a dialect compiler: empty child set.
    Ok(CompiledQuery::new(
        format!("/* select_child_ids_hasmany unsupported for {engine_id} */"),
        vec![],
    ))
}

/// Select HasOne cascade children ids.
/// # Errors
///
/// Returns an error when the requested operation cannot be completed.
pub fn select_hasone_cascade_children(
    engine_id: &str,
    other: &str,
    from_field: &str,
    parent_table: &str,
    bare_parent_id: &str,
) -> Result<CompiledQuery> {
    require_compiler(engine_id, "select_hasone_cascade_children")?;
    if is_surreal(engine_id) {
        let parent_rid = format!("{parent_table}:{bare_parent_id}");
        let q = format!(
            "SELECT VALUE id FROM {other} \
             WHERE {from_field} = $parent_rid OR {from_field} = type::record($tb, $rid)"
        );
        return Ok(CompiledQuery::new(
            q,
            vec![
                ("parent_rid".to_string(), Value::String(parent_rid)),
                ("tb".to_string(), Value::String(parent_table.to_string())),
                ("rid".to_string(), Value::String(bare_parent_id.to_string())),
            ],
        ));
    }
    if is_sql_family(engine_id) {
        let q = format!(
            "SELECT id FROM {other} \
             WHERE json_extract(body, '$.{from_field}') = $parent_rid \
                OR json_extract(body, '$.{from_field}.id') = $bare_id \
                OR json_extract(body, '$.{from_field}') = $bare_id"
        );
        let parent_rid = format!("{parent_table}:{bare_parent_id}");
        return Ok(CompiledQuery::new(
            q,
            vec![
                ("parent_rid".to_string(), Value::String(parent_rid)),
                (
                    "bare_id".to_string(),
                    Value::String(bare_parent_id.to_string()),
                ),
            ],
        ));
    }
    // Third-party / stub engines without a dialect compiler: empty child set.
    Ok(CompiledQuery::new(
        format!("/* select_hasone_cascade_children unsupported for {engine_id} */"),
        vec![],
    ))
}

/// Count ownership sidecar rows for one schema / owner / status.
///
/// # Errors
///
/// Returns an error when the requested operation cannot be completed.
pub fn count_ownership_rows_for_schema(
    engine_id: &str,
    valence_model: &str,
    owner_ids: &[String],
    owner_type: &str,
    status: &str,
) -> Result<CompiledQuery> {
    require_compiler(engine_id, "count_ownership_rows_for_schema")?;
    if is_surreal(engine_id) {
        let q = concat!(
            "SELECT count() AS n FROM valence_data_ownership ",
            "WHERE valence_model = $model AND owner_id IN $owner_ids ",
            "AND owner_type = $owner_type AND status = $status GROUP ALL"
        );
        return Ok(CompiledQuery::new(
            q.to_string(),
            vec![
                (
                    "model".to_string(),
                    Value::String(valence_model.to_string()),
                ),
                (
                    "owner_ids".to_string(),
                    Value::Array(owner_ids.iter().cloned().map(Value::String).collect()),
                ),
                (
                    "owner_type".to_string(),
                    Value::String(owner_type.to_string()),
                ),
                ("status".to_string(), Value::String(status.to_string())),
            ],
        ));
    }
    if is_sql_family(engine_id) {
        let mut params = vec![
            (
                "model".to_string(),
                Value::String(valence_model.to_string()),
            ),
            (
                "owner_type".to_string(),
                Value::String(owner_type.to_string()),
            ),
            ("status".to_string(), Value::String(status.to_string())),
        ];
        let mut owner_ors = Vec::with_capacity(owner_ids.len());
        for (i, owner_id) in owner_ids.iter().enumerate() {
            let key = format!("owner_id_{i}");
            owner_ors.push(format!("json_extract(body, '$.owner_id') = ${key}"));
            params.push((key, Value::String(owner_id.clone())));
        }
        let owner_clause = match owner_ors.as_slice() {
            [] => "0".to_string(),
            [only] => only.clone(),
            many => format!("({})", many.join(" OR ")),
        };
        let q = format!(
            "SELECT COUNT(*) AS n FROM valence_data_ownership \
             WHERE json_extract(body, '$.valence_model') = $model \
               AND {owner_clause} \
               AND json_extract(body, '$.owner_type') = $owner_type \
               AND json_extract(body, '$.status') = $status"
        );
        return Ok(CompiledQuery::new(q, params));
    }
    Ok(CompiledQuery::new(
        format!("/* count_ownership_rows_for_schema unsupported for {engine_id} */"),
        vec![],
    ))
}

const ACTIVE_DELETION_STATUSES: &[&str] = &["queued", "scanning", "processing"];

/// Count in-flight deletion runs for a requester JSON string.
///
/// # Errors
///
/// Returns an error when the requested operation cannot be completed.
pub fn count_active_deletion_runs_for_requester(
    engine_id: &str,
    requested_by: &str,
) -> Result<CompiledQuery> {
    require_compiler(engine_id, "count_active_deletion_runs_for_requester")?;
    if is_surreal(engine_id) {
        let q = concat!(
            "SELECT count() AS n FROM valence_deletion_run ",
            "WHERE requested_by = $requested_by ",
            "AND status IN ['queued', 'scanning', 'processing'] GROUP ALL"
        );
        return Ok(CompiledQuery::new(
            q.to_string(),
            vec![(
                "requested_by".to_string(),
                Value::String(requested_by.to_string()),
            )],
        ));
    }
    if is_sql_family(engine_id) {
        let mut params = vec![(
            "requested_by".to_string(),
            Value::String(requested_by.to_string()),
        )];
        let mut status_ors = Vec::with_capacity(ACTIVE_DELETION_STATUSES.len());
        for (i, status) in ACTIVE_DELETION_STATUSES.iter().enumerate() {
            let key = format!("status_{i}");
            status_ors.push(format!("json_extract(body, '$.status') = ${key}"));
            params.push((key, Value::String((*status).to_string())));
        }
        let status_clause = status_ors.join(" OR ");
        let q = format!(
            "SELECT COUNT(*) AS n FROM valence_deletion_run \
             WHERE json_extract(body, '$.requested_by') = $requested_by \
               AND ({status_clause})"
        );
        return Ok(CompiledQuery::new(q, params));
    }
    Ok(CompiledQuery::new(
        format!("/* count_active_deletion_runs_for_requester unsupported for {engine_id} */"),
        vec![],
    ))
}

#[cfg(test)]
mod tests {
    #![allow(clippy::expect_used, clippy::unwrap_used)]

    use super::*;
    use crate::known_engines::KnownEngines;

    #[test]
    fn ownership_count_sql_uses_count_star_and_json_extract() {
        let cq = count_ownership_rows_for_schema(
            KnownEngines::SQLITE,
            "task",
            &["user:abc".into(), "abc".into()],
            "user",
            "active",
        )
        .expect("compile");
        assert!(cq.query_string.contains("COUNT(*)"), "{}", cq.query_string);
        assert!(
            !cq.query_string.contains("GROUP ALL"),
            "{}",
            cq.query_string
        );
        assert!(cq.query_string.contains("json_extract(body, '$.owner_id')"));
        assert_eq!(cq.params.len(), 5);
    }

    #[test]
    #[cfg(feature = "compiler-surreal")]
    fn ownership_count_surreal_keeps_group_all() {
        let cq = count_ownership_rows_for_schema(
            KnownEngines::SURREALDB,
            "task",
            &["user:abc".into()],
            "user",
            "active",
        )
        .expect("compile");
        assert!(cq.query_string.contains("count()"));
        assert!(cq.query_string.contains("GROUP ALL"));
        assert!(cq.query_string.contains("IN $owner_ids"));
    }

    #[test]
    fn active_deletion_count_sql_avoids_surreal_in_list() {
        let cq = count_active_deletion_runs_for_requester(
            KnownEngines::SQLITE,
            r#"{"User":{"user_id":"x"}}"#,
        )
        .expect("compile");
        assert!(cq.query_string.contains("COUNT(*)"), "{}", cq.query_string);
        assert!(
            !cq.query_string.contains("GROUP ALL"),
            "{}",
            cq.query_string
        );
        assert!(!cq.query_string.contains("IN ["), "{}", cq.query_string);
        assert!(cq.query_string.contains("json_extract(body, '$.status')"));
    }
}