From 0dc5ca668c9db7a526e23843e0ac7628bb0a113a Mon Sep 17 00:00:00 2001 From: pedrocarlo Date: Wed, 28 May 2025 13:25:31 -0300 Subject: [PATCH] test for DELETE + fixes --- .../src/to_sql_string/stmt/create_trigger.rs | 2 +- .../src/to_sql_string/stmt/delete.rs | 90 ++++++++++++++++++- 2 files changed, 89 insertions(+), 3 deletions(-) diff --git a/vendored/sqlite3-parser/src/to_sql_string/stmt/create_trigger.rs b/vendored/sqlite3-parser/src/to_sql_string/stmt/create_trigger.rs index 88c244976..84f996989 100644 --- a/vendored/sqlite3-parser/src/to_sql_string/stmt/create_trigger.rs +++ b/vendored/sqlite3-parser/src/to_sql_string/stmt/create_trigger.rs @@ -77,7 +77,7 @@ impl ToSqlString for ast::TriggerCmdDelete { self.where_clause .as_ref() .map_or("".to_string(), |expr| format!( - " {}", + " WHERE {}", expr.to_sql_string(context) )) ) diff --git a/vendored/sqlite3-parser/src/to_sql_string/stmt/delete.rs b/vendored/sqlite3-parser/src/to_sql_string/stmt/delete.rs index af6158ac1..8968e1647 100644 --- a/vendored/sqlite3-parser/src/to_sql_string/stmt/delete.rs +++ b/vendored/sqlite3-parser/src/to_sql_string/stmt/delete.rs @@ -18,13 +18,13 @@ impl ToSqlString for ast::Delete { self.where_clause .as_ref() .map_or("".to_string(), |expr| format!( - " {}", + " WHERE {}", expr.to_sql_string(context) )), self.returning .as_ref() .map_or("".to_string(), |returning| format!( - " {}", + " RETURNING {}", returning .iter() .map(|col| col.to_sql_string(context)) @@ -48,3 +48,89 @@ impl ToSqlString for ast::Delete { ) } } + +#[cfg(test)] +mod tests { + use crate::to_sql_string_test; + + // Basic DELETE from a single table + to_sql_string_test!(test_delete_all, "DELETE FROM employees;"); + + // DELETE with a simple WHERE clause + to_sql_string_test!( + test_delete_with_where, + "DELETE FROM employees WHERE id = 1;" + ); + + // DELETE with multiple WHERE conditions + to_sql_string_test!( + test_delete_with_multi_where, + "DELETE FROM employees WHERE salary < 50000 AND department_id = 3;" + ); + + // DELETE with IN clause + to_sql_string_test!( + test_delete_with_in, + "DELETE FROM employees WHERE id IN (1, 2, 3);" + ); + + // DELETE with subquery in WHERE + to_sql_string_test!( + test_delete_with_subquery, + "DELETE FROM employees WHERE department_id IN (SELECT id FROM departments WHERE name = 'Sales');" + ); + + // DELETE with EXISTS clause + to_sql_string_test!( + test_delete_with_exists, + "DELETE FROM employees WHERE EXISTS (SELECT 1 FROM orders WHERE orders.employee_id = employees.id AND orders.status = 'pending');" + ); + + // DELETE with RETURNING clause + to_sql_string_test!( + test_delete_with_returning, + "DELETE FROM employees WHERE salary < 30000 RETURNING id, name;" + ); + + // DELETE with LIMIT clause + to_sql_string_test!( + test_delete_with_limit, + "DELETE FROM employees WHERE salary < 40000 LIMIT 5;" + ); + + // DELETE with ORDER BY and LIMIT + to_sql_string_test!( + test_delete_with_order_by_limit, + "DELETE FROM employees WHERE salary < 40000 ORDER BY id DESC LIMIT 5;" + ); + + // DELETE from schema-qualified table + to_sql_string_test!( + test_delete_schema_qualified, + "DELETE FROM main.employees WHERE id = 1;" + ); + + // DELETE with BETWEEN clause + to_sql_string_test!( + test_delete_with_between, + "DELETE FROM employees WHERE salary BETWEEN 30000 AND 50000;" + ); + + // DELETE with NULL check + to_sql_string_test!( + test_delete_with_null, + "DELETE FROM employees WHERE department_id IS NULL;" + ); + + // DELETE with LIKE clause + to_sql_string_test!( + test_delete_with_like, + "DELETE FROM employees WHERE name LIKE 'J%';" + ); + + // DELETE with complex expression in WHERE + to_sql_string_test!( + test_delete_with_complex_expression, + "DELETE FROM employees WHERE (salary * 1.1) > 60000 AND department_id != 1;" + ); +}