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
108impl PLpgSQLExpression {
109 pub fn parse_statement(&self) -> Result<Statement, SQLError> {
110 self.parse()
111 .map(super::variable_conflicts::expression_query)
112 }
113 pub fn parse(&self) -> Result<Expr, SQLError> {
114 super::lowering_expression::compile_expression_source(&self.source)
115 }
116}
117impl PLpgSQLStatement {
118 pub fn parse(&self) -> Result<Statement, SQLError> {
119 super::lowering_expression::compile_statement_source(&self.source)
120 }
121}
122impl PLpgSQLCursorArguments {
123 pub fn parse_statement(&self) -> Result<Statement, SQLError> {
124 let arguments = self.parse()?;
125 let Statement::Select(mut query) =
126 super::variable_conflicts::expression_query(Expr::Literal(uqa_core::Value::Null))
127 else {
128 unreachable!()
129 };
130 query.projections = arguments
131 .into_iter()
132 .map(|argument| crate::ast::Projection {
133 expr: argument.expr,
134 alias: argument.name,
135 })
136 .collect();
137 Ok(Statement::Select(query))
138 }
139 pub fn parse(&self) -> Result<Vec<PLpgSQLCursorArgument>, SQLError> {
140 super::lowering_expression::compile_cursor_arguments_source(&self.source)
141 }
142 pub fn empty() -> Self {
143 Self::new(
144 Arc::new(PLpgSQLSource {
145 query: Arc::from(""),
146 mode: PLpgSQLParseMode::Expression,
147 }),
148 Some(Vec::new()),
149 )
150 }
151}
152
153#[derive(Debug, Clone, Default)]
156pub struct PLpgSQLCompilationIdentity(Arc<()>);
157impl PLpgSQLCompilationIdentity {
158 pub fn same(&self, other: &Self) -> bool {
159 Arc::ptr_eq(&self.0, &other.0)
160 }
161}
162
163#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
164pub enum PLpgSQLCompileMode {
165 #[default]
166 Validate,
167 Runtime,
168}
169impl PLpgSQLCompileMode {
170 pub(super) const fn validates(self) -> bool {
171 matches!(self, Self::Validate)
172 }
173}
174
175#[cfg(test)]
176mod tests;