Skip to content

Commit

Permalink
Support comments before select_statement in insert_statement (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
lemonadern authored Sep 12, 2024
1 parent d3688b2 commit 1cf54a1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
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;

0 comments on commit 1cf54a1

Please sign in to comment.