Skip to main content

limbo_sqlite3_parser/to_sql_string/stmt/
delete.rs

1use crate::{ast, to_sql_string::ToSqlString};
2
3impl ToSqlString for ast::Delete {
4    fn to_sql_string<C: crate::to_sql_string::ToSqlContext>(&self, context: &C) -> String {
5        format!(
6            "{}DELETE FROM {}{}{}{}{}{};",
7            self.with.as_ref().map_or("".to_string(), |with| format!(
8                "{} ",
9                with.to_sql_string(context)
10            )),
11            self.tbl_name.to_sql_string(context),
12            self.indexed
13                .as_ref()
14                .map_or("".to_string(), |indexed| format!(" {}", indexed)),
15            self.where_clause
16                .as_ref()
17                .map_or("".to_string(), |expr| format!(
18                    " WHERE {}",
19                    expr.to_sql_string(context)
20                )),
21            self.returning
22                .as_ref()
23                .map_or("".to_string(), |returning| format!(
24                    " RETURNING {}",
25                    returning
26                        .iter()
27                        .map(|col| col.to_sql_string(context))
28                        .collect::<Vec<_>>()
29                        .join(", ")
30                )),
31            self.order_by
32                .as_ref()
33                .map_or("".to_string(), |order_by| format!(
34                    " ORDER BY {}",
35                    order_by
36                        .iter()
37                        .map(|col| col.to_sql_string(context))
38                        .collect::<Vec<_>>()
39                        .join(", ")
40                )),
41            self.limit.as_ref().map_or("".to_string(), |limit| format!(
42                " {}",
43                limit.to_sql_string(context)
44            ))
45        )
46    }
47}
48
49#[cfg(test)]
50mod tests {
51    use crate::to_sql_string_test;
52
53    // Basic DELETE from a single table
54    to_sql_string_test!(test_delete_all, "DELETE FROM employees;");
55
56    // DELETE with a simple WHERE clause
57    to_sql_string_test!(
58        test_delete_with_where,
59        "DELETE FROM employees WHERE id = 1;"
60    );
61
62    // DELETE with multiple WHERE conditions
63    to_sql_string_test!(
64        test_delete_with_multi_where,
65        "DELETE FROM employees WHERE salary < 50000 AND department_id = 3;"
66    );
67
68    // DELETE with IN clause
69    to_sql_string_test!(
70        test_delete_with_in,
71        "DELETE FROM employees WHERE id IN (1, 2, 3);"
72    );
73
74    // DELETE with subquery in WHERE
75    to_sql_string_test!(
76        test_delete_with_subquery,
77        "DELETE FROM employees WHERE department_id IN (SELECT id FROM departments WHERE name = 'Sales');"
78    );
79
80    // DELETE with EXISTS clause
81    to_sql_string_test!(
82        test_delete_with_exists,
83        "DELETE FROM employees WHERE EXISTS (SELECT 1 FROM orders WHERE orders.employee_id = employees.id AND orders.status = 'pending');"
84    );
85
86    // DELETE with RETURNING clause
87    to_sql_string_test!(
88        test_delete_with_returning,
89        "DELETE FROM employees WHERE salary < 30000 RETURNING id, name;"
90    );
91
92    // DELETE with LIMIT clause
93    to_sql_string_test!(
94        test_delete_with_limit,
95        "DELETE FROM employees WHERE salary < 40000 LIMIT 5;"
96    );
97
98    // DELETE with ORDER BY and LIMIT
99    to_sql_string_test!(
100        test_delete_with_order_by_limit,
101        "DELETE FROM employees WHERE salary < 40000 ORDER BY id DESC LIMIT 5;"
102    );
103
104    // DELETE from schema-qualified table
105    to_sql_string_test!(
106        test_delete_schema_qualified,
107        "DELETE FROM main.employees WHERE id = 1;"
108    );
109
110    // DELETE with BETWEEN clause
111    to_sql_string_test!(
112        test_delete_with_between,
113        "DELETE FROM employees WHERE salary BETWEEN 30000 AND 50000;"
114    );
115
116    // DELETE with NULL check
117    to_sql_string_test!(
118        test_delete_with_null,
119        "DELETE FROM employees WHERE department_id IS NULL;"
120    );
121
122    // DELETE with LIKE clause
123    to_sql_string_test!(
124        test_delete_with_like,
125        "DELETE FROM employees WHERE name LIKE 'J%';"
126    );
127
128    // DELETE with complex expression in WHERE
129    to_sql_string_test!(
130        test_delete_with_complex_expression,
131        "DELETE FROM employees WHERE (salary * 1.1) > 60000 AND department_id != 1;"
132    );
133}