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::{visit_ctes, CommandPlan, QueryPlan, ScalarExpr};
10fn subqueries(values: &[QueryPlan], visit: &mut dyn FnMut(&ScalarExpr)) {
11    for query in values {
12        query.visit_scalar_expressions(visit);
13    }
14}
15
16impl CommandPlan {
17    /// 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.
18    pub fn visit_scalar_expressions(&self, visit: &mut dyn FnMut(&ScalarExpr)) {
19        match self {
20            Self::Insert(_) | Self::Update(_) | Self::Delete(_) | Self::Merge(_) => {
21                visit_ctes(self.ctes(), visit);
22                for expression in self.expressions() {
23                    visit(expression);
24                }
25                for query in self.query_inputs() {
26                    query.visit_scalar_expressions(visit);
27                }
28                if let Some(source) = self.source_input() {
29                    source.visit_scalar_expressions(visit);
30                }
31            }
32            Self::CreateView { query, .. }
33            | Self::CreateMaterializedView { query, .. }
34            | Self::CreateTableAs { query, .. }
35            | Self::DeclareCursor { query, .. } => query.visit_scalar_expressions(visit),
36            Self::Explain { body, .. } | Self::Prepare { body, .. } => {
37                body.visit_scalar_expressions(visit);
38            }
39            Self::Execute { params, .. } | Self::Call { args: params, .. } => {
40                for expression in params {
41                    visit(&expression.scalar);
42                    subqueries(&expression.subqueries, visit);
43                }
44            }
45            Self::CreateTable(_)
46            | Self::CreateTableIfNotExists(_)
47            | Self::CreateIndex(_)
48            | Self::RenameIndex(_)
49            | Self::Drop(_)
50            | Self::AlterTable(_)
51            | Self::AlterForeignTable(_)
52            | Self::AlterView(_)
53            | Self::RefreshMaterializedView { .. }
54            | Self::CreateSchema { .. }
55            | Self::AlterSchemaOwner { .. }
56            | Self::RenameSchema { .. }
57            | Self::Notify { .. }
58            | Self::Listen { .. }
59            | Self::Unlisten { .. }
60            | Self::SetVariable { .. }
61            | Self::ResetVariable { .. }
62            | Self::ResetAllVariables
63            | Self::SetConstraints { .. }
64            | Self::ShowVariable { .. }
65            | Self::Discard { .. }
66            | Self::Load { .. }
67            | Self::Analyze { .. }
68            | Self::Vacuum(_)
69            | Self::LockTable(_)
70            | Self::Truncate { .. }
71            | Self::Transaction(_)
72            | Self::FetchCursor(_)
73            | Self::CloseCursor { .. }
74            | Self::CreateSequence(_)
75            | Self::CreateDomain(_)
76            | Self::AlterDomain(_)
77            | Self::CommentType(_)
78            | Self::CreateShellType(_)
79            | Self::CreateEnum(_)
80            | Self::CreateCompositeType(_)
81            | Self::AlterEnum(_)
82            | Self::AlterTypeObject(_)
83            | Self::GrantType(_)
84            | Self::AlterSequence(_)
85            | Self::Deallocate { .. }
86            | Self::CreateForeignWrapper(_)
87            | Self::CreateForeignServer(_)
88            | Self::CreateForeignTable(_)
89            | Self::CreateForeignTableDefinition(_)
90            | Self::CreateFunction(_)
91            | Self::DropFunction(_)
92            | Self::AlterRoutine(_)
93            | Self::AlterRoutineOwner(_)
94            | Self::RenameRoutine(_)
95            | Self::GrantRoutine(_)
96            | Self::GrantTable(_)
97            | Self::GrantSequence(_)
98            | Self::GrantDatabase(_)
99            | Self::GrantSchema(_)
100            | Self::GrantRole(_)
101            | Self::CreateRole(_)
102            | Self::AlterRole(_)
103            | Self::RenameRole(_)
104            | Self::DropRole(_)
105            | Self::CreateTrigger(_)
106            | Self::DropTrigger(_)
107            | Self::CreateRule(_)
108            | Self::DropRule(_)
109            | Self::DoBlock { .. }
110            | Self::Empty => {}
111        }
112    }
113}