uqa_sql/plpgsql/
fragments.rs1use super::PLpgSQLCursorArgument;
10use crate::{
11 ast::{Expr, Statement},
12 SQLError,
13};
14use std::sync::Arc;
15
16#[derive(Debug, Clone, Copy, PartialEq, Eq)]
18pub enum PLpgSQLParseMode {
19 Statement,
20 Expression,
21 Assignment1,
22 Assignment2,
23 Assignment3,
24}
25impl TryFrom<i64> for PLpgSQLParseMode {
26 type Error = SQLError;
27 fn try_from(mode: i64) -> Result<Self, SQLError> {
28 match mode {
29 0 => Ok(Self::Statement),
30 2 => Ok(Self::Expression),
31 3 => Ok(Self::Assignment1),
32 4 => Ok(Self::Assignment2),
33 5 => Ok(Self::Assignment3),
34 _ => Err(SQLError::Internal(format!(
35 "invalid PL/pgSQL parse mode {mode}"
36 ))),
37 }
38 }
39}
40impl PLpgSQLParseMode {
41 pub(super) fn parser_mode(self) -> pg_query::ParseMode {
42 match self {
43 Self::Statement => pg_query::ParseMode::Default,
44 Self::Expression => pg_query::ParseMode::PlPgSqlExpr,
45 Self::Assignment1 => pg_query::ParseMode::PlPgSqlAssign1,
46 Self::Assignment2 => pg_query::ParseMode::PlPgSqlAssign2,
47 Self::Assignment3 => pg_query::ParseMode::PlPgSqlAssign3,
48 }
49 }
50}
51
52#[derive(Debug)]
55pub struct PLpgSQLSource {
56 pub query: Arc<str>,
57 pub mode: PLpgSQLParseMode,
58}
59
60#[derive(Debug, Clone)]
63pub struct PLpgSQLFragment<T> {
64 source: Arc<PLpgSQLSource>,
65 validation: Option<Arc<T>>,
66}
67impl<T> PLpgSQLFragment<T> {
68 pub(super) fn new(source: Arc<PLpgSQLSource>, validation: Option<T>) -> Self {
69 Self {
70 source,
71 validation: validation.map(Arc::new),
72 }
73 }
74 pub fn source(&self) -> &Arc<PLpgSQLSource> {
75 &self.source
76 }
77 pub fn validation(&self) -> Option<&T> {
78 self.validation.as_deref()
79 }
80 pub fn site(&self) -> usize {
82 Arc::as_ptr(&self.source) as usize
83 }
84}
85pub type PLpgSQLExpression = PLpgSQLFragment<Expr>;
86pub type PLpgSQLStatement = PLpgSQLFragment<Statement>;
87pub type PLpgSQLCursorArguments = PLpgSQLFragment<Vec<PLpgSQLCursorArgument>>;
88
89pub fn is_simple_expression_plan(
91 catalog: &dyn crate::routines::RoutineResolution,
92 plan: &crate::plan::UnifiedPlan,
93) -> bool {
94 let crate::plan::UnifiedPlan::Query(query) = plan else {
95 return false;
96 };
97 let crate::plan::RelationalPlan::QueryBlock(block) = &query.root else {
98 return false;
99 };
100 query.ctes.is_empty()
101 && block.is_single_expression()
102 && !crate::semantics::sets::static_setness::expression_may_return_set_statically(
103 catalog,
104 &block.projections[0].expr,
105 )
106}
107
108pub fn is_simple_expression_plan_with_control(
109 catalog: &dyn crate::routines::RoutineResolution,
110 plan: &crate::plan::UnifiedPlan,
111 control: &uqa_core::memory::ProductionControl<'_>,
112) -> Result<bool, SQLError> {
113 control.check()?;
114 let crate::plan::UnifiedPlan::Query(query) = plan else {
115 return Ok(false);
116 };
117 let crate::plan::RelationalPlan::QueryBlock(block) = &query.root else {
118 return Ok(false);
119 };
120 Ok(query.ctes.is_empty() && block.is_single_expression() && !crate::semantics::sets::static_setness::expression_may_return_set_statically_with_control(catalog, &block.projections[0].expr, control)?)
121}
122
123impl PLpgSQLExpression {
124 pub fn parse_statement(&self) -> Result<Statement, SQLError> {
125 self.parse()
126 .map(super::variable_conflicts::expression_query)
127 }
128 pub fn parse(&self) -> Result<Expr, SQLError> {
129 super::lowering_expression::compile_expression_source(&self.source)
130 }
131}
132impl PLpgSQLStatement {
133 pub fn parse(&self) -> Result<Statement, SQLError> {
134 super::lowering_expression::compile_statement_source(&self.source)
135 }
136}
137impl PLpgSQLCursorArguments {
138 pub fn parse_statement(&self) -> Result<Statement, SQLError> {
139 let arguments = self.parse()?;
140 let Statement::Select(mut query) =
141 super::variable_conflicts::expression_query(Expr::Literal(uqa_core::Value::Null))
142 else {
143 unreachable!()
144 };
145 query.projections = arguments
146 .into_iter()
147 .map(|argument| crate::ast::Projection {
148 expr: argument.expr,
149 alias: argument.name,
150 })
151 .collect();
152 Ok(Statement::Select(query))
153 }
154 pub fn parse(&self) -> Result<Vec<PLpgSQLCursorArgument>, SQLError> {
155 super::lowering_expression::compile_cursor_arguments_source(&self.source)
156 }
157 pub fn empty() -> Self {
158 Self::new(
159 Arc::new(PLpgSQLSource {
160 query: Arc::from(""),
161 mode: PLpgSQLParseMode::Expression,
162 }),
163 Some(Vec::new()),
164 )
165 }
166}
167
168#[derive(Debug, Clone, Default)]
171pub struct PLpgSQLCompilationIdentity(Arc<()>);
172impl PLpgSQLCompilationIdentity {
173 pub fn same(&self, other: &Self) -> bool {
174 Arc::ptr_eq(&self.0, &other.0)
175 }
176}
177
178#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
179pub enum PLpgSQLCompileMode {
180 #[default]
181 Validate,
182 Runtime,
183}
184impl PLpgSQLCompileMode {
185 pub(super) const fn validates(self) -> bool {
186 matches!(self, Self::Validate)
187 }
188}
189
190#[cfg(test)]
191mod tests;