vibesql_executor/truncate/
mod.rs

1//! TRUNCATE TABLE statement execution
2//!
3//! This module coordinates truncate operations across multiple sub-modules:
4//! - `core`: Core truncate logic and table clearing
5//! - `triggers`: Trigger validation and coordination
6//! - `constraints`: Constraint and foreign key validation
7
8pub mod core;
9pub mod triggers;
10pub mod constraints;
11
12use vibesql_ast::TruncateTableStmt;
13use vibesql_storage::Database;
14
15use crate::errors::ExecutorError;
16use crate::privilege_checker::PrivilegeChecker;
17
18use self::core::{execute_truncate, execute_truncate_cascade};
19use self::constraints::validate_truncate_allowed;
20
21/// Executor for TRUNCATE TABLE statements
22pub struct TruncateTableExecutor;
23
24impl TruncateTableExecutor {
25    /// Execute a TRUNCATE TABLE statement
26    ///
27    /// # Arguments
28    ///
29    /// * `stmt` - The TRUNCATE TABLE statement AST node
30    /// * `database` - The database to truncate the table(s) in
31    ///
32    /// # Returns
33    ///
34    /// Total number of rows deleted from all tables or error
35    ///
36    /// # Behavior
37    ///
38    /// Supports truncating multiple tables in a single statement with all-or-nothing semantics:
39    /// - Validates all tables first (existence, privileges, constraints)
40    /// - Only truncates if all validations pass
41    /// - IF EXISTS: skips non-existent tables, continues with existing ones
42    ///
43    /// # Examples
44    ///
45    /// ```
46    /// use vibesql_ast::{ColumnDef, CreateTableStmt, TruncateTableStmt};
47    /// use vibesql_executor::{CreateTableExecutor, TruncateTableExecutor};
48    /// use vibesql_storage::{Database, Row};
49    /// use vibesql_types::{DataType, SqlValue};
50    ///
51    /// let mut db = Database::new();
52    /// let create_stmt = CreateTableStmt {
53    ///     table_name: "users".to_string(),
54    ///     columns: vec![ColumnDef {
55    ///         name: "id".to_string(),
56    ///         data_type: DataType::Integer,
57    ///         nullable: false,
58    ///         constraints: vec![],
59    ///         default_value: None,
60    ///         comment: None,
61    ///     }],
62    ///     table_constraints: vec![],
63    ///     table_options: vec![],
64    /// };
65    /// CreateTableExecutor::execute(&create_stmt, &mut db).unwrap();
66    ///
67    /// // Insert some rows
68    /// db.insert_row("users", Row::new(vec![SqlValue::Integer(1)])).unwrap();
69    /// db.insert_row("users", Row::new(vec![SqlValue::Integer(2)])).unwrap();
70    ///
71    /// let stmt = TruncateTableStmt { table_names: vec!["users".to_string()], if_exists: false, cascade: None };
72    ///
73    /// let result = TruncateTableExecutor::execute(&stmt, &mut db);
74    /// assert_eq!(result.unwrap(), 2); // 2 rows deleted
75    /// assert_eq!(db.get_table("users").unwrap().row_count(), 0);
76    /// ```
77    pub fn execute(
78        stmt: &TruncateTableStmt,
79        database: &mut Database,
80    ) -> Result<usize, ExecutorError> {
81        // Phase 1: Validation - Check all tables before truncating any
82        // Collect tables that exist and need to be truncated
83        let mut tables_to_truncate = Vec::new();
84
85        for table_name in &stmt.table_names {
86            // Check if table exists
87            if !database.catalog.table_exists(table_name) {
88                if stmt.if_exists {
89                    // IF EXISTS specified and table doesn't exist - skip this table
90                    continue;
91                } else {
92                    return Err(ExecutorError::TableNotFound(table_name.clone()));
93                }
94            }
95
96            tables_to_truncate.push(table_name.as_str());
97        }
98
99        // If no tables to truncate (all were non-existent with IF EXISTS), return 0
100        if tables_to_truncate.is_empty() {
101            return Ok(0);
102        }
103
104        // Check DELETE privilege on all tables
105        for table_name in &tables_to_truncate {
106            PrivilegeChecker::check_delete(database, table_name)?;
107        }
108
109        // Determine CASCADE behavior - check explicit CASCADE, default to RESTRICT
110        let cascade_mode = &stmt.cascade;
111        
112        match cascade_mode {
113            Some(vibesql_ast::TruncateCascadeOption::Cascade) => {
114                // CASCADE mode: recursively truncate dependent tables for each requested table
115                let mut total_rows = 0;
116                for table_name in &tables_to_truncate {
117                    total_rows += execute_truncate_cascade(database, table_name)?;
118                }
119                Ok(total_rows)
120            }
121            _ => {
122                // RESTRICT mode (default): fail if referenced by foreign keys
123                // Check if TRUNCATE is allowed on all tables (no DELETE triggers, no FK references)
124                for table_name in &tables_to_truncate {
125                    validate_truncate_allowed(database, table_name)?;
126                }
127
128                // Phase 2: Execution - All validations passed, now truncate all tables
129                let mut total_rows = 0;
130                for table_name in &tables_to_truncate {
131                    total_rows += execute_truncate(database, table_name)?;
132                }
133
134                Ok(total_rows)
135            }
136        }
137    }
138}
139