vibesql_executor/procedural/
mod.rs

1//! Stored Procedures and User-Defined Functions
2//!
3//! This module implements SQL:1999 Feature P001 (stored procedures) and related
4//! functionality for user-defined functions. It provides a complete procedural
5//! execution environment with local variables, control flow, and parameter binding.
6//!
7//! # Overview
8//!
9//! Procedures and functions allow storing SQL logic in the database for reuse and
10//! encapsulation. Procedures are invoked with CALL and can modify data, while
11//! functions return values and are read-only (cannot modify database state).
12//!
13//! ## Key Features
14//!
15//! - **Parameter modes**: IN (input), OUT (output), INOUT (both)
16//! - **Local variables**: DECLARE and SET for variable management
17//! - **Control flow**: IF, WHILE, LOOP, REPEAT statements
18//! - **Labels**: Named blocks for LEAVE and ITERATE
19//! - **Recursion**: Function calls with depth limiting (max 100)
20//! - **Read-only functions**: Functions cannot modify database tables
21//!
22//! # Examples
23//!
24//! ## Creating a Simple Procedure
25//!
26//! ```sql
27//! CREATE PROCEDURE greet(IN name VARCHAR(50))
28//! BEGIN
29//!   SELECT CONCAT('Hello, ', name);
30//! END;
31//!
32//! CALL greet('Alice');
33//! -- Output: Hello, Alice
34//! ```
35//!
36//! ## Using OUT Parameters
37//!
38//! ```sql
39//! CREATE PROCEDURE calculate_stats(
40//!   IN input_value INT,
41//!   OUT sum_result INT,
42//!   OUT count_result INT
43//! )
44//! BEGIN
45//!   SELECT SUM(value), COUNT(*)
46//!   INTO sum_result, count_result
47//!   FROM data_table
48//!   WHERE value > input_value;
49//! END;
50//!
51//! CALL calculate_stats(10, @sum, @count);
52//! SELECT @sum, @count;
53//! ```
54//!
55//! ## Creating a Function
56//!
57//! ```sql
58//! CREATE FUNCTION factorial(n INT) RETURNS INT
59//!   DETERMINISTIC
60//!   COMMENT 'Calculate factorial of n'
61//! BEGIN
62//!   DECLARE result INT DEFAULT 1;
63//!   DECLARE i INT DEFAULT 2;
64//!
65//!   WHILE i <= n DO
66//!     SET result = result * i;
67//!     SET i = i + 1;
68//!   END WHILE;
69//!
70//!   RETURN result;
71//! END;
72//!
73//! SELECT factorial(5);
74//! -- Output: 120
75//! ```
76//!
77//! ## Control Flow with Labels
78//!
79//! ```sql
80//! CREATE FUNCTION find_first(search_val INT) RETURNS INT
81//! BEGIN
82//!   DECLARE i INT DEFAULT 1;
83//!   DECLARE found INT DEFAULT 0;
84//!
85//!   search_loop: WHILE i <= 100 DO
86//!     IF i = search_val THEN
87//!       SET found = 1;
88//!       LEAVE search_loop;
89//!     END IF;
90//!     SET i = i + 1;
91//!   END WHILE search_loop;
92//!
93//!   RETURN found;
94//! END;
95//! ```
96//!
97//! # Module Structure
98//!
99//! - [`context`]: Execution context with variables, parameters, and scope management
100//! - [`executor`]: Execute individual procedural statements
101//! - [`control_flow`]: Control flow execution (IF, WHILE, LOOP, REPEAT)
102//! - [`function`]: User-defined function execution and recursion handling
103//!
104//! # Function Characteristics
105//!
106//! Functions support the following characteristics (from Phase 6):
107//!
108//! - **DETERMINISTIC**: Same input always produces same output
109//! - **SQL SECURITY DEFINER**: Execute with definer's privileges (requires privilege system)
110//! - **SQL SECURITY INVOKER**: Execute with caller's privileges (default)
111//! - **COMMENT**: Documentation string for the function
112//! - **LANGUAGE SQL**: Indicates SQL language (only SQL is currently supported)
113//!
114//! Example:
115//! ```sql
116//! CREATE FUNCTION add_ten(x INT) RETURNS INT
117//!   DETERMINISTIC
118//!   SQL SECURITY INVOKER
119//!   COMMENT 'Adds 10 to the input value'
120//!   LANGUAGE SQL
121//!   RETURN x + 10;
122//! ```
123//!
124//! # Limitations
125//!
126//! Current implementation has these limitations:
127//!
128//! - Cursors not yet supported
129//! - Exception handlers not yet supported
130//! - Only SQL language supported (no external languages like PLpgSQL)
131//! - Privilege enforcement for SQL SECURITY requires full privilege system
132//! - Maximum recursion depth is 100 levels
133//!
134//! # See Also
135//!
136//! - SQL:1999 Standard, Feature P001 (Stored Modules)
137//! - [`crate::advanced_objects`]: CREATE/DROP PROCEDURE and FUNCTION statements
138//! - [`vibesql_catalog::Procedure`]: Procedure catalog storage
139//! - [`vibesql_catalog::Function`]: Function catalog storage
140
141pub mod context;
142pub mod control_flow;
143pub mod executor;
144pub mod function;
145
146pub use context::{ControlFlow, ExecutionContext};
147pub use executor::execute_procedural_statement;
148pub use function::execute_user_function;