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