1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
use crate::{
ast::{CreateProcedureStmt, FunctionAccess, ObjectName, SecurityMode, SqlOption},
common::symbol::Symbol,
lexer::TokenKind,
parser::{parser::Parser, parser_error::ParserError},
};
impl<'a> Parser<'a> {
/// Parses a `CREATE PROCEDURE` statement.
///
/// Syntax:
/// ```sql
/// CREATE [OR REPLACE] PROCEDURE [IF NOT EXISTS] name (params)
/// LANGUAGE sql | plpgsql | ...
/// [SECURITY DEFINER | SECURITY INVOKER]
/// [SET param = value]
/// [ACCESS PUBLIC | PRIVATE | RESTRICTED]
/// [RAISES exception, ...]
/// [TRANSACTION CONTROL]
/// [TIMEOUT n]
/// [IDEMPOTENT]
/// [RETRIES n]
/// AS $$ body $$ | BEGIN...END
/// ```
///
/// Key differences from `CREATE FUNCTION`:
/// - No `RETURNS` clause — procedures never return values
/// - No `VOLATILE`/`STABLE`/`IMMUTABLE` — not applicable
/// - No `STRICT`/`PARALLEL`/`COST`/`ROWS` — not applicable
/// - Can manage transactions via `COMMIT`/`ROLLBACK` in body
/// - Called with `CALL proc()` not `SELECT proc()`
///
/// `or_replace` is passed from `parse_create_modifiers` in `create.rs`.
/// The `PROCEDURE` keyword is consumed by the caller.
pub fn parse_create_procedure(
&mut self,
or_replace: bool,
) -> Result<CreateProcedureStmt, ParserError> {
// Optional IF NOT EXISTS clause
let if_not_exists = self.parse_if_not_exist()?;
// Qualified procedure name — required
let name = ObjectName(self.parse_qualified_name()?);
// Parameter list — required even if empty: `proc()`
self.expect(TokenKind::LParen)?;
let params = self.parse_function_params()?;
self.expect(TokenKind::RParen)?;
// LANGUAGE clause — required
let language = self.parse_function_language()?;
// ── Optional clauses — all have defaults, order independent ──
// SECURITY INVOKER is the default per SQL standard
let mut security = SecurityMode::Invoker;
// Runtime configuration options set for duration of procedure call
let mut set_options: Vec<SqlOption> = vec![];
// PUBLIC access by default — our extension
let mut access = FunctionAccess::Public;
// No declared exceptions by default — our extension
let mut raises: Vec<Symbol> = vec![];
// false by default — procedure does not manage transactions
let mut transaction_control = false;
// No timeout by default — our extension
let mut timeout: Option<u64> = None;
// false by default — procedure is not marked idempotent
let mut idempotent = false;
// No auto-retry by default — our extension
let mut retries: Option<u32> = None;
loop {
match self.current_token().clone() {
// SECURITY DEFINER | SECURITY INVOKER
// DEFINER — runs with privileges of the procedure owner.
// INVOKER — runs with privileges of the caller (default).
TokenKind::Security => {
self.advance();
security = match self.current_token() {
TokenKind::Definer => {
self.advance();
SecurityMode::Definer
}
TokenKind::Invoker => {
self.advance();
SecurityMode::Invoker
}
_ => {
return Err(ParserError::new(
format!(
"Expected DEFINER or INVOKER after SECURITY, got {:?}",
self.current_token()
),
self.current.span.clone(),
));
}
};
}
// SET param = value — sets a GUC parameter for the duration
// of the procedure call, restored on exit.
TokenKind::Set => {
self.advance();
let param_name = self.expect_identifier()?;
self.expect(TokenKind::Eq)?;
let value = self.parse_expr()?;
set_options.push(SqlOption {
name: param_name,
value,
});
}
// ACCESS PUBLIC | PRIVATE | RESTRICTED
// Our extension — declare visibility at creation time.
// Avoids a separate GRANT EXECUTE ON PROCEDURE statement.
TokenKind::Access => {
self.advance();
access = match self.current_token() {
TokenKind::Public => {
self.advance();
FunctionAccess::Public
}
TokenKind::Private => {
self.advance();
FunctionAccess::Private
}
TokenKind::Restricted => {
self.advance();
FunctionAccess::Restricted
}
_ => {
return Err(ParserError::new(
format!(
"Expected PUBLIC, PRIVATE or RESTRICTED after ACCESS, got {:?}",
self.current_token()
),
self.current.span.clone(),
));
}
};
}
// RAISES exception1, exception2
// Our extension — declares which exceptions this procedure
// can raise. Useful for static analysis and documentation.
TokenKind::Raises => {
self.advance();
loop {
raises.push(self.expect_identifier()?);
if !self.consume(&TokenKind::Comma) {
break;
}
}
}
// TRANSACTION CONTROL — two tokens: TRANSACTION then CONTROL.
// Our extension — explicitly declares that this procedure
// manages its own transactions via COMMIT/ROLLBACK in the body.
// PostgreSQL has no way to declare this upfront.
// Useful for static analysis, tooling, and documentation.
TokenKind::Transaction => {
self.advance();
self.expect(TokenKind::Control)?;
transaction_control = true;
}
// TIMEOUT n — maximum execution time in milliseconds.
// Our extension — procedure is killed if it exceeds this limit.
// PostgreSQL requires setting statement_timeout GUC separately.
TokenKind::Timeout => {
self.advance();
timeout = Some(self.expect_int_literal()?);
}
// IDEMPOTENT — marks the procedure as safe to call multiple
// times with the same arguments without unintended side effects.
// Our extension — useful for retry logic and documentation.
TokenKind::Idempotent => {
self.advance();
idempotent = true;
}
// RETRIES n — number of times to automatically retry this
// procedure on serialization failure or deadlock.
// Our extension — PostgreSQL requires application-level retries.
TokenKind::Retries => {
self.advance();
retries = Some(self.expect_int_literal()? as u32);
}
// No more recognized clauses — stop parsing options
_ => break,
}
}
// Procedure body — required, must come last.
// Reuses parse_function_body from function.rs — supports both
// PostgreSQL AS $$ ... $$ and our cleaner BEGIN...END syntax.
let body = self.parse_function_body()?;
Ok(CreateProcedureStmt {
name,
or_replace,
if_not_exists,
params,
language,
security,
set_options,
access,
raises,
transaction_control,
timeout,
idempotent,
retries,
body,
})
}
}