Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

集合演算(union, intersect, except) に対応 #143

Merged
merged 1 commit into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions crates/uroborosql-fmt/src/new_visitor/statement/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,34 @@ impl Visitor {
let comment = Comment::pg_new(cursor.node());
statement.add_comment_to_child(comment)?;
}
SyntaxKind::UNION | SyntaxKind::INTERSECT | SyntaxKind::EXCEPT => {
// (UNION | INTERSECT | EXCEPT) set_quantifier? (select_clause)

let mut combining_clause = Clause::from_pg_node(cursor.node());
cursor.goto_next_sibling();

// cursor -> set_quantifier?
if cursor.node().kind() == SyntaxKind::set_quantifier {
// set_quantifier
// - ALL
// - DISTINCT
combining_clause.pg_extend_kw(cursor.node());
cursor.goto_next_sibling();
}

// 演算子のみからなる句を追加
statement.add_clause(combining_clause);

while cursor.node().is_comment() {
let comment = Comment::pg_new(cursor.node());
statement.add_comment_to_child(comment)?;
cursor.goto_next_sibling();
}

// cursor -> (select_clause)
let select_clause = self.visit_select_clause(cursor, src)?;
statement.add_clause(select_clause);
}
SyntaxKind::into_clause => {
return Err(UroboroSQLFmtError::Unimplemented(format!(
"visit_select_stmt(): into_clause is not implemented\n{}",
Expand Down
42 changes: 42 additions & 0 deletions crates/uroborosql-fmt/test_normal_cases/dst/038_set_operations.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
-- union
select
a as a
from
b
/* select - union */
union
-- union
/* union - subselect */
select
c as c
from
b
;
-- intersect
select
a as a
from
b
/* select - intersect */
intersect
-- intersect
/* intersect - subselect */
select
c as c
from
b
;
-- except
select
a as a
from
b
/* select - except */
except
-- except
/* except - subselect */
select
c as c
from
b
;
27 changes: 27 additions & 0 deletions crates/uroborosql-fmt/test_normal_cases/src/038_set_operations.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-- union
SELECT A
FROM B
/* select - union */
UNION -- union
/* union - subselect */
SELECT C
FROM B
;
-- intersect
SELECT A
FROM B
/* select - intersect */
INTERSECT -- intersect
/* intersect - subselect */
SELECT C
FROM B
;
-- except
SELECT A
FROM B
/* select - except */
EXCEPT -- except
/* except - subselect */
SELECT C
FROM B
;
Loading