Skip to main content

uqa_engine/capabilities/
truncate.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Bind TRUNCATE metadata, live trigger state and the existing table-clear transaction boundary.
8
9use crate::Engine;
10use uqa_execution::schema::truncate::{
11    TruncateAccess, TruncateContext, TruncateStorage, TruncateTransactions, TruncateTriggers,
12    TruncateWrite,
13};
14use uqa_sql::{
15    ast::{TriggerEvent, TriggerTiming},
16    catalog::security::table::TableAclPrivilege,
17    schema::truncate::TruncateCatalog,
18    SQLError,
19};
20
21impl Engine {
22    pub(crate) fn truncate_context(&self) -> TruncateContext<'_> {
23        TruncateContext {
24            catalog: self,
25            access: self,
26            triggers: self,
27            storage: self,
28            transactions: self,
29            notices: &self.runtime.notices,
30        }
31    }
32}
33impl TruncateCatalog for Engine {
34    fn try_resolve_visible_relation_kind(
35        &self,
36        name: &str,
37    ) -> Result<Option<(String, &'static str)>, SQLError> {
38        Engine::try_resolve_visible_relation_kind(self, name)
39    }
40    fn is_partitioned(&self, table: &str) -> Result<bool, String> {
41        self.try_table_hierarchy(table)
42            .map(|hierarchy| hierarchy.partition_spec.is_some())
43            .map_err(|error| error.to_string())
44    }
45    fn hierarchy_scan_tables(
46        &self,
47        table: &str,
48        descendants: bool,
49    ) -> Result<Vec<String>, SQLError> {
50        Engine::hierarchy_scan_tables(self, table, descendants)
51    }
52    fn referrers_to(&self, table: &str) -> Result<Vec<String>, String> {
53        Engine::referrers_to(self, table)
54            .map(|references| references.into_iter().map(|(name, _)| name).collect())
55            .map_err(|error| error.to_string())
56    }
57    fn partition_ancestor_tables(&self, table: &str) -> Result<Vec<String>, SQLError> {
58        uqa_sql::semantics::partition::partition_ancestor_tables(self, table)
59    }
60}
61impl TruncateAccess for Engine {
62    fn ensure_truncate_privilege(&self, table: &str) -> Result<(), SQLError> {
63        self.ensure_table_privilege(table, TableAclPrivilege::Truncate)
64    }
65}
66impl TruncateTriggers for Engine {
67    fn ensure_no_pending_trigger_events(
68        &self,
69        table: &str,
70        operation: &str,
71    ) -> Result<(), SQLError> {
72        Engine::ensure_no_pending_trigger_events(self, table, operation)
73    }
74    fn fire_statement_trigger(&self, table: &str, timing: TriggerTiming) -> Result<(), SQLError> {
75        uqa_execution::mutation::triggers::fire_statement_triggers(
76            &self.trigger_execution_context(),
77            table,
78            timing,
79            TriggerEvent::Truncate,
80            &[],
81        )
82    }
83}
84impl TruncateStorage for Engine {
85    fn truncate_tables_with_identity(
86        &self,
87        tables: &[String],
88        restart_identity: bool,
89    ) -> Result<(), SQLError> {
90        Engine::truncate_tables_with_identity(self, tables, restart_identity)
91    }
92}
93impl TruncateTransactions for Engine {
94    fn transaction_depth(&self) -> usize {
95        Engine::transaction_depth(self)
96    }
97    fn with_transaction(&self, operation: TruncateWrite<'_>) -> Result<(), SQLError> {
98        self.transaction(|engine| operation(&engine.truncate_context()))
99    }
100}