From 563ab2fdf3ab06701cfa741baf4e8390177dc923 Mon Sep 17 00:00:00 2001 From: pedrocarlo Date: Wed, 28 May 2025 12:21:27 -0300 Subject: [PATCH] impl ToSqlString for CREATE VIEW stmt + tests --- .../src/to_sql_string/stmt/mod.rs | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/vendored/sqlite3-parser/src/to_sql_string/stmt/mod.rs b/vendored/sqlite3-parser/src/to_sql_string/stmt/mod.rs index 9006129c6..3062817db 100644 --- a/vendored/sqlite3-parser/src/to_sql_string/stmt/mod.rs +++ b/vendored/sqlite3-parser/src/to_sql_string/stmt/mod.rs @@ -92,6 +92,29 @@ impl ToSqlString for ast::Stmt { ) } Self::CreateTrigger(trigger) => trigger.to_sql_string(context), + Self::CreateView { + temporary, + if_not_exists, + view_name, + columns, + select, + } => { + format!( + "CREATE{} VIEW {}{}{} AS {}", + temporary.then_some(" TEMP").unwrap_or(""), + if_not_exists.then_some("IF NOT EXISTS ").unwrap_or(""), + view_name.to_sql_string(context), + columns.as_ref().map_or("".to_string(), |columns| format!( + " ({})", + columns + .iter() + .map(|col| col.to_sql_string(context)) + .collect::>() + .join(", ") + )), + select.to_sql_string(context) + ) + } Self::Select(select) => format!("{};", select.to_sql_string(context)), _ => todo!(), } @@ -219,4 +242,64 @@ mod tests { test_create_index_mixed_order, "CREATE INDEX idx_name_asc_salary_desc ON employees (last_name ASC, salary DESC);" ); + + // Test 1: View with DISTINCT keyword + to_sql_string_test!( + test_create_view_distinct, + "CREATE VIEW view_distinct AS SELECT DISTINCT name FROM employees" + ); + + // Test 2: View with LIMIT clause + to_sql_string_test!( + test_create_view_limit, + "CREATE VIEW view_limit AS SELECT id, name FROM employees LIMIT 10" + ); + + // Test 3: View with CASE expression + to_sql_string_test!( + test_create_view_case, + "CREATE VIEW view_case AS SELECT name, CASE WHEN salary > 70000 THEN 'High' ELSE 'Low' END AS salary_level FROM employees" + ); + + // Test 4: View with LEFT JOIN + to_sql_string_test!( + test_create_view_left_join, + "CREATE VIEW view_left_join AS SELECT e.name, d.name AS department FROM employees e LEFT JOIN departments d ON e.department_id = d.id" + ); + + // Test 5: View with HAVING clause + to_sql_string_test!( + test_create_view_having, + "CREATE VIEW view_having AS SELECT department_id, AVG(salary) AS avg_salary FROM employees GROUP BY department_id HAVING AVG(salary) > 55000" + ); + + // Test 6: View with CTE (Common Table Expression) + to_sql_string_test!( + test_create_view_cte, + "CREATE VIEW view_cte AS WITH high_earners AS (SELECT * FROM employees WHERE salary > 80000) SELECT id, name FROM high_earners" + ); + + // Test 7: View with multiple conditions in WHERE + to_sql_string_test!( + test_create_view_multi_where, + "CREATE VIEW view_multi_where AS SELECT id, name FROM employees WHERE salary > 50000 AND department_id = 3" + ); + + // Test 8: View with NULL handling + to_sql_string_test!( + test_create_view_null, + "CREATE VIEW view_null AS SELECT name, COALESCE(salary, 0) AS salary FROM employees" + ); + + // Test 9: View with subquery in WHERE clause + to_sql_string_test!( + test_create_view_subquery_where, + "CREATE VIEW view_subquery_where AS SELECT name FROM employees WHERE department_id IN (SELECT id FROM departments WHERE name = 'Sales')" + ); + + // Test 10: View with arithmetic expression + to_sql_string_test!( + test_create_view_arithmetic, + "CREATE VIEW view_arithmetic AS SELECT name, salary * 1.1 AS adjusted_salary FROM employees" + ); }