1use crate::Engine;
8use uqa_core::Value;
9use uqa_execution::routines::{
10 context::{
11 RoutineExpressions, RoutinePortals, RoutineStatements, RoutineTransactions,
12 StatementResultCheck,
13 },
14 transaction::RoutineSessionId,
15 RoutineContext,
16};
17use uqa_sql::{
18 assignment::routines::RoutineValueContext,
19 ast::{ColumnType, Expr, FetchCursorStmt, Statement},
20 plan::{ExpressionPlan, UnifiedPlan},
21 SQLError, SQLParam, SQLResult,
22};
23impl Engine {
24 pub(crate) fn routine_session_id(&self) -> RoutineSessionId {
25 RoutineSessionId(std::sync::Arc::as_ptr(&self.session) as usize)
26 }
27 pub(crate) fn routine_execution_context(&self) -> RoutineContext<'_> {
28 RoutineContext {
29 expressions: self,
30 statements: self,
31 transactions: self,
32 portals: self,
33 session: self.routine_session_id(),
34 runtime: self.query_runtime_view(),
35 }
36 }
37}
38impl RoutineValueContext for Engine {
39 fn catalog_column_type(&self, name: &str) -> Option<ColumnType> {
40 uqa_execution::catalog::projection::resolve_catalog_column_type(
41 &self.catalog_execution(),
42 name,
43 )
44 }
45}
46impl RoutineExpressions for Engine {
47 fn column_type_name(&self, name: &str) -> Result<ColumnType, SQLError> {
48 uqa_execution::catalog::projection::resolve_catalog_column_type_name(
49 &self.catalog_execution(),
50 name,
51 )
52 }
53 fn evaluate(&self, expression: &Expr) -> Result<Value, SQLError> {
54 crate::capabilities::query_expressions::eval_lowered_expression(self, expression, None, &[])
55 }
56 fn evaluate_with_type(
57 &self,
58 expression: &Expr,
59 ) -> Result<(Value, Option<ColumnType>), SQLError> {
60 crate::capabilities::query_expressions::eval_lowered_expression_with_type(
61 self,
62 expression,
63 None,
64 &[],
65 )
66 }
67 fn expression_type(
68 &self,
69 plan: &ExpressionPlan,
70 params: &[SQLParam],
71 ) -> Result<Option<ColumnType>, SQLError> {
72 let mut scope = super::query_scope::new_for_current_routine(self);
73 scope.scalar_subqueries.clone_from(&plan.subqueries);
74 let hook = uqa_execution::query::relational::context::QueryExpressionFactory::bind_scope(
75 self, scope,
76 );
77 uqa_execution::scalar_type_with_resolver(
78 &plan.scalar,
79 &uqa_execution::RowSchema::default(),
80 params,
81 hook.as_ref(),
82 )
83 }
84}
85impl RoutineStatements for Engine {
86 fn body_input_context(
87 &self,
88 ) -> Option<uqa_execution::routines::sql_body::inputs::SQLRoutineInputContext<'_>> {
89 Some(
90 uqa_execution::routines::sql_body::inputs::SQLRoutineInputContext {
91 cache: self.session.routine_bodies.sql_inputs(),
92 analysis: self.prepared_definition_context(),
93 },
94 )
95 }
96
97 fn plpgsql_preparations(
98 &self,
99 definition: &uqa_sql::ast::CreateFunction,
100 parsed: &uqa_sql::plpgsql::PLpgSQLFunction,
101 ) -> std::sync::Arc<uqa_execution::routines::preparation::PLpgSQLPreparations> {
102 self.session
103 .routine_bodies
104 .plpgsql_preparations(definition, parsed)
105 }
106 fn analyze_static_plan(
107 &self,
108 plan: &mut UnifiedPlan,
109 parameters: &[SQLParam],
110 ) -> Result<uqa_sql::binding::statements::ProceduralPlanAnalysis, SQLError> {
111 uqa_sql::binding::statements::analyze_procedural_plan(
112 &self.statement_planning_context().analysis,
113 &uqa_sql::routines::resolution::RoutineOverloadContext { catalog: self },
114 self,
115 plan,
116 parameters,
117 )
118 }
119 fn parser_settings(&self) -> uqa_sql::parser::ParserSettings {
120 Engine::parser_settings(self)
121 }
122 fn execute_body_statement(
123 &self,
124 plan: UnifiedPlan,
125 params: &[SQLParam],
126 check: Option<StatementResultCheck<'_>>,
127 ) -> Result<SQLResult, SQLError> {
128 let context = self.compiled_statement_context();
129 match check {
130 Some(check) => uqa_execution::statement::compiled::execute_checked_plan(
131 &context, plan, params, check,
132 ),
133 None => uqa_execution::statement::compiled::execute_plan(&context, plan, params),
134 }
135 }
136
137 fn select_body_plan(
138 &self,
139 entry: &uqa_sql::prepared::entry::PreparedStatementPlan,
140 params: &[SQLParam],
141 ) -> Result<uqa_sql::prepared::planning::PreparedPlanSelection, SQLError> {
142 let _temporal_input = uqa_sql::expr::TemporalInputContext {
143 transaction_clock_micros: Some(self.transaction_timestamp_micros()),
144 interval_style: self.session.interval_style(),
145 date_order: uqa_sql::semantics::parameters::datestyle::date_order(
146 &self.session.setting("DateStyle"),
147 ),
148 }
149 .enter();
150 uqa_planner::statement_planning::prepared::selection::select_analyzed_entry(
151 &uqa_planner::statement_planning::prepared::selection::PreparedPlanningContext {
152 session: self,
153 analysis: self.prepared_definition_context(),
154 optimization: self,
155 },
156 entry,
157 params,
158 )
159 }
160 fn execute_selected_body_plan(
161 &self,
162 plan: &UnifiedPlan,
163 params: &[SQLParam],
164 ) -> Result<SQLResult, SQLError> {
165 uqa_execution::statement::compiled::execute_selected_plan(
166 &self.compiled_statement_context(),
167 plan,
168 params,
169 )
170 }
171 fn execute_bound(
172 &self,
173 statement: Statement,
174 params: &[SQLParam],
175 ) -> Result<SQLResult, SQLError> {
176 uqa_execution::statement::compiled::execute(
177 &self.compiled_statement_context(),
178 statement,
179 params,
180 )
181 }
182 fn execute_text(&self, text: &str, params: &[SQLParam]) -> Result<SQLResult, SQLError> {
183 uqa_execution::statement::batch::execute_nested(
184 &self.batch_execution_context(),
185 text,
186 params,
187 )
188 }
189 fn optimize_plan(&self, plan: UnifiedPlan) -> Result<UnifiedPlan, SQLError> {
190 crate::capabilities::statement_planning::optimize_engine_plan(self, plan)
191 }
192 fn assertions_enabled(&self) -> bool {
193 self.plpgsql_asserts_enabled()
194 }
195 fn load_language_library(&self, language: &str) {
196 self.load_language(language);
197 }
198 fn with_statement_scope(
199 &self,
200 analyze: uqa_execution::routines::context::StatementScopeOperation<'_>,
201 ) -> Result<(), SQLError> {
202 let scope = super::query_scope::new_for_current_routine(self);
203 analyze(
204 self,
205 &uqa_execution::query::binding::binding_context(&scope)?,
206 )
207 }
208}
209impl RoutineTransactions for Engine {
210 fn depth(&self) -> usize {
211 self.transaction_depth()
212 }
213 fn begin(&self) -> Result<(), SQLError> {
214 self.begin()
215 }
216 fn commit(&self) -> Result<(), SQLError> {
217 self.commit()
218 }
219 fn rollback(&self) -> Result<(), SQLError> {
220 self.rollback()
221 }
222 fn finish_procedural_transaction(&self, commit: bool, chain: bool) -> Result<(), SQLError> {
223 self.finish_procedural_transaction(commit, chain)
224 }
225}
226impl RoutinePortals for Engine {
227 fn ensure_available(&self, name: &str) -> Result<(), SQLError> {
228 uqa_execution::statement::portal::declaration::ensure_plpgsql_session_portal_available(
229 self, name,
230 )
231 }
232 fn open(
233 &self,
234 params: &[SQLParam],
235 name: &str,
236 scroll: Option<bool>,
237 plan: &UnifiedPlan,
238 source_sql: &str,
239 ) -> Result<(), SQLError> {
240 uqa_execution::statement::portal::declaration::open_plpgsql_session_portal(
241 &self
242 .portal_execution_context()
243 .with_source_sql(Some(source_sql)),
244 params,
245 name,
246 scroll,
247 plan,
248 )
249 }
250 fn fetch(&self, request: &FetchCursorStmt) -> Result<SQLResult, SQLError> {
251 self.fetch_session_portal(request)
252 }
253 fn close(&self, name: &str) -> Result<(), SQLError> {
254 self.close_session_portal(name)
255 }
256 fn allocate_name(&self) -> String {
257 self.allocate_session_portal_name()
258 }
259 fn pin(&self, name: &str) -> Result<(), SQLError> {
260 self.pin_session_portal(name)
261 }
262 fn unpin(&self, name: &str) -> Result<(), SQLError> {
263 self.unpin_session_portal(name)
264 }
265}