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
//! Stored Procedures and User-Defined Functions
//!
//! This module implements SQL:1999 Feature P001 (stored procedures) and related
//! functionality for user-defined functions. It provides a complete procedural
//! execution environment with local variables, control flow, and parameter binding.
//!
//! # Overview
//!
//! Procedures and functions allow storing SQL logic in the database for reuse and
//! encapsulation. Procedures are invoked with CALL and can modify data, while
//! functions return values and are read-only (cannot modify database state).
//!
//! ## Key Features
//!
//! - **Parameter modes**: IN (input), OUT (output), INOUT (both)
//! - **Local variables**: DECLARE and SET for variable management
//! - **Control flow**: IF, WHILE, LOOP, REPEAT statements
//! - **Labels**: Named blocks for LEAVE and ITERATE
//! - **Recursion**: Function calls with depth limiting (max 100)
//! - **Read-only functions**: Functions cannot modify database tables
//!
//! # Examples
//!
//! ## Creating a Simple Procedure
//!
//! ```sql
//! CREATE PROCEDURE greet(IN name VARCHAR(50))
//! BEGIN
//! SELECT CONCAT('Hello, ', name);
//! END;
//!
//! CALL greet('Alice');
//! -- Output: Hello, Alice
//! ```
//!
//! ## Using OUT Parameters
//!
//! ```sql
//! CREATE PROCEDURE calculate_stats(
//! IN input_value INT,
//! OUT sum_result INT,
//! OUT count_result INT
//! )
//! BEGIN
//! SELECT SUM(value), COUNT(*)
//! INTO sum_result, count_result
//! FROM data_table
//! WHERE value > input_value;
//! END;
//!
//! CALL calculate_stats(10, @sum, @count);
//! SELECT @sum, @count;
//! ```
//!
//! ## Creating a Function
//!
//! ```sql
//! CREATE FUNCTION factorial(n INT) RETURNS INT
//! DETERMINISTIC
//! COMMENT 'Calculate factorial of n'
//! BEGIN
//! DECLARE result INT DEFAULT 1;
//! DECLARE i INT DEFAULT 2;
//!
//! WHILE i <= n DO
//! SET result = result * i;
//! SET i = i + 1;
//! END WHILE;
//!
//! RETURN result;
//! END;
//!
//! SELECT factorial(5);
//! -- Output: 120
//! ```
//!
//! ## Control Flow with Labels
//!
//! ```sql
//! CREATE FUNCTION find_first(search_val INT) RETURNS INT
//! BEGIN
//! DECLARE i INT DEFAULT 1;
//! DECLARE found INT DEFAULT 0;
//!
//! search_loop: WHILE i <= 100 DO
//! IF i = search_val THEN
//! SET found = 1;
//! LEAVE search_loop;
//! END IF;
//! SET i = i + 1;
//! END WHILE search_loop;
//!
//! RETURN found;
//! END;
//! ```
//!
//! # Module Structure
//!
//! - [`context`]: Execution context with variables, parameters, and scope management
//! - [`executor`]: Execute individual procedural statements
//! - [`control_flow`]: Control flow execution (IF, WHILE, LOOP, REPEAT)
//! - [`function`]: User-defined function execution and recursion handling
//!
//! # Function Characteristics
//!
//! Functions support the following characteristics (from Phase 6):
//!
//! - **DETERMINISTIC**: Same input always produces same output
//! - **SQL SECURITY DEFINER**: Execute with definer's privileges (requires privilege system)
//! - **SQL SECURITY INVOKER**: Execute with caller's privileges (default)
//! - **COMMENT**: Documentation string for the function
//! - **LANGUAGE SQL**: Indicates SQL language (only SQL is currently supported)
//!
//! Example:
//! ```sql
//! CREATE FUNCTION add_ten(x INT) RETURNS INT
//! DETERMINISTIC
//! SQL SECURITY INVOKER
//! COMMENT 'Adds 10 to the input value'
//! LANGUAGE SQL
//! RETURN x + 10;
//! ```
//!
//! # Limitations
//!
//! Current implementation has these limitations:
//!
//! - Cursors not yet supported
//! - Exception handlers not yet supported
//! - Only SQL language supported (no external languages like PLpgSQL)
//! - Privilege enforcement for SQL SECURITY requires full privilege system
//! - Maximum recursion depth is 100 levels
//!
//! # See Also
//!
//! - SQL:1999 Standard, Feature P001 (Stored Modules)
//! - [`crate::advanced_objects`]: CREATE/DROP PROCEDURE and FUNCTION statements
//! - [`vibesql_catalog::Procedure`]: Procedure catalog storage
//! - [`vibesql_catalog::Function`]: Function catalog storage
pub use ;
pub use execute_procedural_statement;
pub use execute_user_function;