mirror of
https://github.com/aljazceru/turso.git
synced 2025-12-24 19:44:21 +01:00
impl ToSqlString for CREATE VIEW stmt + tests
This commit is contained in:
@@ -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::<Vec<_>>()
|
||||
.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"
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user