Skip to main content

uqa_sql/plan/visit/
command.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Borrow command expressions directly, including plans retained by utility commands.
8
9use super::{try_visit_ctes, CommandPlan, QueryPlan, ScalarExpr};
10fn subqueries<'a, E>(
11    values: &'a [QueryPlan],
12    visit: &mut dyn FnMut(&'a ScalarExpr) -> Result<(), E>,
13) -> Result<(), E> {
14    for query in values {
15        query.try_visit_scalar_expressions(visit)?;
16    }
17    Ok(())
18}
19
20impl CommandPlan {
21    /// Visit scalar roots without copying the plan or collecting its children. Mutations retain their analysis order: CTEs, direct expressions, query inputs, then source inputs. Query-bearing utilities share the scalar rewrite's ownership boundary.
22    pub fn visit_scalar_expressions(&self, visit: &mut dyn FnMut(&ScalarExpr)) {
23        let Ok(()) = self.try_visit_scalar_expressions(&mut |expression| {
24            visit(expression);
25            Ok::<_, std::convert::Infallible>(())
26        });
27    }
28
29    /// Stop at the first callback error, without copying syntax or visiting later inputs.
30    pub fn try_visit_scalar_expressions<'a, E>(
31        &'a self,
32        visit: &mut dyn FnMut(&'a ScalarExpr) -> Result<(), E>,
33    ) -> Result<(), E> {
34        match self {
35            Self::Insert(_) | Self::Update(_) | Self::Delete(_) | Self::Merge(_) => {
36                try_visit_ctes(self.ctes(), visit)?;
37                for expression in self.expressions() {
38                    visit(expression)?;
39                }
40                for query in self.query_inputs() {
41                    query.try_visit_scalar_expressions(visit)?;
42                }
43                if let Some(source) = self.source_input() {
44                    source.try_visit_scalar_expressions(visit)?;
45                }
46            }
47            Self::CreateView { query, .. }
48            | Self::CreateMaterializedView { query, .. }
49            | Self::CreateTableAs { query, .. }
50            | Self::DeclareCursor { query, .. } => query.try_visit_scalar_expressions(visit)?,
51            Self::Explain { body, .. } | Self::Prepare { body, .. } => {
52                body.try_visit_scalar_expressions(visit)?;
53            }
54            Self::Execute { params, .. } | Self::Call { args: params, .. } => {
55                for expression in params {
56                    visit(&expression.scalar)?;
57                    subqueries(&expression.subqueries, visit)?;
58                }
59            }
60            Self::CreateTable(_)
61            | Self::CreateTableIfNotExists(_)
62            | Self::CreateIndex(_)
63            | Self::RenameIndex(_)
64            | Self::Drop(_)
65            | Self::AlterTable(_)
66            | Self::AlterForeignTable(_)
67            | Self::AlterView(_)
68            | Self::RefreshMaterializedView { .. }
69            | Self::CreateSchema { .. }
70            | Self::AlterSchemaOwner { .. }
71            | Self::RenameSchema { .. }
72            | Self::Notify { .. }
73            | Self::Listen { .. }
74            | Self::Unlisten { .. }
75            | Self::SetVariable { .. }
76            | Self::ResetVariable { .. }
77            | Self::ResetAllVariables
78            | Self::SetConstraints { .. }
79            | Self::ShowVariable { .. }
80            | Self::Discard { .. }
81            | Self::Load { .. }
82            | Self::Analyze { .. }
83            | Self::Vacuum(_)
84            | Self::LockTable(_)
85            | Self::Truncate { .. }
86            | Self::Transaction(_)
87            | Self::FetchCursor(_)
88            | Self::CloseCursor { .. }
89            | Self::CreateSequence(_)
90            | Self::CreateDomain(_)
91            | Self::AlterDomain(_)
92            | Self::CommentType(_)
93            | Self::CreateShellType(_)
94            | Self::CreateEnum(_)
95            | Self::CreateCompositeType(_)
96            | Self::AlterEnum(_)
97            | Self::AlterTypeObject(_)
98            | Self::GrantType(_)
99            | Self::AlterSequence(_)
100            | Self::Deallocate { .. }
101            | Self::CreateForeignWrapper(_)
102            | Self::CreateForeignServer(_)
103            | Self::CreateForeignTable(_)
104            | Self::CreateForeignTableDefinition(_)
105            | Self::CreateFunction(_)
106            | Self::DropFunction(_)
107            | Self::AlterRoutine(_)
108            | Self::AlterRoutineOwner(_)
109            | Self::RenameRoutine(_)
110            | Self::GrantRoutine(_)
111            | Self::GrantTable(_)
112            | Self::GrantSequence(_)
113            | Self::GrantDatabase(_)
114            | Self::GrantSchema(_)
115            | Self::GrantRole(_)
116            | Self::CreateRole(_)
117            | Self::AlterRole(_)
118            | Self::RenameRole(_)
119            | Self::DropRole(_)
120            | Self::CreateTrigger(_)
121            | Self::DropTrigger(_)
122            | Self::CreateRule(_)
123            | Self::DropRule(_)
124            | Self::DoBlock { .. }
125            | Self::Empty => {}
126        }
127        Ok(())
128    }
129}