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

Support comments before select_statement in insert_statement #101

Merged
merged 1 commit into from
Sep 12, 2024
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: 27 additions & 1 deletion crates/uroborosql-fmt/src/visitor/statement/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,24 @@ impl Visitor {

cursor.goto_next_sibling();

// values か query の前のコメント
// selectの場合のみ対応している(括弧付きselectとvalues句の場合は未対応)
let mut comments_before_values_or_query = vec![];
while cursor.node().kind() == COMMENT {
comments_before_values_or_query.push(Comment::new(cursor.node(), src));
cursor.goto_next_sibling();
}

// {VALUES ( { expression | DEFAULT } [, ...] ) [, ...] | query }
match cursor.node().kind() {
"values_clause" => {
if !comments_before_values_or_query.is_empty() {
return Err(UroboroSQLFmtError::Unimplemented(format!(
"visit_insert_stmt(): Comments before values clause are not implemented. \nComment: {:?}",
comments_before_values_or_query.first().unwrap()
)));
}

cursor.goto_first_child();
ensure_kind(cursor, "VALUES", src)?;

Expand Down Expand Up @@ -148,13 +163,24 @@ impl Visitor {
}
"select_statement" => {
// select文
let stmt = self.visit_select_stmt(cursor, src)?;
let mut stmt = self.visit_select_stmt(cursor, src)?;

// select 文の前にあったコメントを付与
for comment in comments_before_values_or_query {
stmt.add_comment(comment);
}

insert_body.set_query(stmt);

cursor.goto_next_sibling();
}
"select_subexpression" => {
if !comments_before_values_or_query.is_empty() {
return Err(UroboroSQLFmtError::Unimplemented(format!(
"visit_insert_stmt(): Comments before parenthesized subquery are not implemented. \nComment: {:?}",
comments_before_values_or_query.first().unwrap()
)));
}
// 括弧付きSELECT
let selct_sub = self.visit_select_subexpr(cursor, src)?;

Expand Down
15 changes: 15 additions & 0 deletions crates/uroborosql-fmt/testfiles/dst/insert/insert_select.sql
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,18 @@ on
do
nothing
;
insert
into
tbl
(
id
)
-- comments
-- before select
select
id as id
from
tbl2
where
id = 1
;
5 changes: 5 additions & 0 deletions crates/uroborosql-fmt/testfiles/src/insert/insert_select.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ insert into tbl (id)
select id from tbl2 where id = 1 -- trailing comment
on conflict do nothing
;

insert into tbl (id)
-- comments
-- before select
select id from tbl2 where id = 1;
Loading