Skip to main content

nodedb_sql/engine_rules/
document_strict.rs

1// SPDX-License-Identifier: Apache-2.0
2
3//! Engine rules for strict document collections.
4
5use crate::engine_rules::*;
6use crate::error::{Result, SqlError};
7use crate::types::*;
8
9pub struct StrictRules;
10
11impl EngineRules for StrictRules {
12    fn plan_insert(&self, p: InsertParams) -> Result<Vec<SqlPlan>> {
13        Ok(vec![SqlPlan::Insert {
14            collection: p.collection,
15            engine: EngineType::DocumentStrict,
16            rows: p.rows,
17            column_defaults: p.column_defaults,
18            if_absent: p.if_absent,
19            column_schema: vec![],
20        }])
21    }
22
23    fn plan_upsert(&self, p: UpsertParams) -> Result<Vec<SqlPlan>> {
24        Ok(vec![SqlPlan::Upsert {
25            collection: p.collection,
26            engine: EngineType::DocumentStrict,
27            rows: p.rows,
28            column_defaults: p.column_defaults,
29            on_conflict_updates: p.on_conflict_updates,
30            column_schema: vec![],
31        }])
32    }
33
34    fn plan_scan(&self, p: ScanParams) -> Result<SqlPlan> {
35        if p.temporal.is_temporal() && !p.bitemporal {
36            return Err(SqlError::Unsupported {
37                detail: format!(
38                    "FOR SYSTEM_TIME / FOR VALID_TIME requires a bitemporal \
39                     collection; '{}' was not created WITH bitemporal = true",
40                    p.collection
41                ),
42            });
43        }
44        if let Some(plan) =
45            crate::engine_rules::try_document_index_lookup(&p, EngineType::DocumentStrict)
46        {
47            return Ok(plan);
48        }
49        Ok(SqlPlan::Scan {
50            collection: p.collection,
51            alias: p.alias,
52            engine: EngineType::DocumentStrict,
53            filters: p.filters,
54            projection: p.projection,
55            sort_keys: p.sort_keys,
56            limit: p.limit,
57            offset: p.offset,
58            distinct: p.distinct,
59            window_functions: p.window_functions,
60            temporal: p.temporal,
61        })
62    }
63
64    fn plan_point_get(&self, p: PointGetParams) -> Result<SqlPlan> {
65        Ok(SqlPlan::PointGet {
66            collection: p.collection,
67            alias: p.alias,
68            engine: EngineType::DocumentStrict,
69            key_column: p.key_column,
70            key_value: p.key_value,
71        })
72    }
73
74    fn plan_update(&self, p: UpdateParams) -> Result<Vec<SqlPlan>> {
75        Ok(vec![SqlPlan::Update {
76            collection: p.collection,
77            engine: EngineType::DocumentStrict,
78            assignments: p.assignments,
79            filters: p.filters,
80            target_keys: p.target_keys,
81            returning: p.returning,
82        }])
83    }
84
85    fn plan_update_from(&self, p: UpdateFromParams) -> Result<Vec<SqlPlan>> {
86        Ok(vec![SqlPlan::UpdateFrom {
87            collection: p.collection,
88            engine: EngineType::DocumentStrict,
89            source: p.source,
90            target_join_col: p.target_join_col,
91            source_join_col: p.source_join_col,
92            assignments: p.assignments,
93            target_filters: p.target_filters,
94            returning: p.returning,
95        }])
96    }
97
98    fn plan_delete(&self, p: DeleteParams) -> Result<Vec<SqlPlan>> {
99        Ok(vec![SqlPlan::Delete {
100            collection: p.collection,
101            engine: EngineType::DocumentStrict,
102            filters: p.filters,
103            target_keys: p.target_keys,
104        }])
105    }
106
107    fn plan_merge(&self, p: MergeParams) -> Result<Vec<SqlPlan>> {
108        Ok(vec![SqlPlan::Merge {
109            target: p.collection,
110            engine: EngineType::DocumentStrict,
111            source: p.source,
112            target_join_col: p.target_join_col,
113            source_join_col: p.source_join_col,
114            source_alias: p.source_alias,
115            clauses: p.clauses,
116            returning: p.returning,
117        }])
118    }
119
120    fn plan_aggregate(&self, p: AggregateParams) -> Result<SqlPlan> {
121        let base_scan = SqlPlan::Scan {
122            collection: p.collection,
123            alias: p.alias,
124            engine: EngineType::DocumentStrict,
125            filters: p.filters,
126            projection: Vec::new(),
127            sort_keys: Vec::new(),
128            limit: None,
129            offset: 0,
130            distinct: false,
131            window_functions: Vec::new(),
132            temporal: p.temporal,
133        };
134        Ok(SqlPlan::Aggregate {
135            input: Box::new(base_scan),
136            group_by: p.group_by,
137            aggregates: p.aggregates,
138            having: p.having,
139            limit: p.limit,
140            grouping_sets: None,
141            sort_keys: Vec::new(),
142        })
143    }
144}