Skip to content

Commit

Permalink
Handle comments after select_statement at the end of insert_statement (
Browse files Browse the repository at this point in the history
  • Loading branch information
lemonadern authored Sep 3, 2024
1 parent ee7fed8 commit cb71681
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 8 deletions.
59 changes: 52 additions & 7 deletions crates/uroborosql-fmt/src/cst/body/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,14 +375,59 @@ impl InsertBody {
comment: Comment,
) -> Result<(), UroboroSQLFmtError> {
// 下から順番に見ていく

// table_nameの直後に現れる
if comment.is_block_comment() || !self.table_name.loc().is_same_line(&comment.loc()) {
// 行末コメントではない場合は未対応
unimplemented!()
// 1. on_conflict
// 2. values_or_query
// 3. columns
// else: table_name

if self.on_conflict.is_some() {
// on_conflict 句の後にコメントが来る場合
return Err(UroboroSQLFmtError::Unimplemented(format!(
"add_comment_to_child(): Comments after on_conflict clause is not implemented: {comment:?}"
)));
} else if let Some(values_or_query) = self.values_or_query.as_mut() {
// values 句 か query の後にコメントが来る場合
match values_or_query {
ValuesOrQuery::Values(_) => {
// values 句の場合
return Err(UroboroSQLFmtError::Unimplemented(format!(
"add_comment_to_child(): Comments after values_clause are not implemented: {comment:?}"
)));
}
ValuesOrQuery::Query(query) => match query {
Query::Normal(statement) => {
// select 文のあとにコメントが来る場合
statement.add_comment_to_child(comment)?;
}
Query::Paren(_) => {
// 括弧付き select で、閉じ括弧の後にコメントが来る場合
return Err(UroboroSQLFmtError::Unimplemented(format!(
"add_comment_to_child(): Comments after select queries enclosed in parentheses are not implemented: {comment:?}"
)));
}
},
}
} else if self.columns.is_some() {
// カラム名の後 または columns の後にコメントが来る場合
return Err(UroboroSQLFmtError::Unimplemented(format!(
"add_comment_to_child(): Comments after column name is not implemented: {comment:?}"
)));
} else {
// 行末コメントである場合、table_nameに追加する
self.table_name.set_trailing_comment(comment)?;
// table_name 直後のコメント
if comment.is_block_comment() {
// ブロックコメントの場合
return Err(UroboroSQLFmtError::Unimplemented(format!(
"add_comment_to_child(): Block comment after table_name is not implemented: {comment:?}"
)));
} else if self.table_name.loc().is_same_line(&comment.loc()) {
// 行末コメントである場合、table_nameに追加する
self.table_name.set_trailing_comment(comment)?;
} else {
// それ以外は未対応
return Err(UroboroSQLFmtError::Unimplemented(format!(
"add_comment_to_child(): Comments for this location is not implemented: {comment:?}"
)));
}
}

Ok(())
Expand Down
7 changes: 7 additions & 0 deletions crates/uroborosql-fmt/src/visitor/statement/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,13 @@ impl Visitor {
_ => {}
}

// values か query の後のコメント
while cursor.node().kind() == COMMENT {
let comment = Comment::new(cursor.node(), src);
insert_body.add_comment_to_child(comment)?;
cursor.goto_next_sibling();
}

// on_conflict句
if cursor.node().kind() == "on_conflict_clause" {
let on_conflict = self.visit_on_conflict(cursor, src)?;
Expand Down
30 changes: 30 additions & 0 deletions crates/uroborosql-fmt/testfiles/dst/insert/insert_select.sql
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,33 @@ into
where
flag = 'TRUE'
);
insert
into
tbl
(
id
)
select
id as id
from
tbl2
where
id = 1 -- trailing comment
;
insert
into
tbl
(
id
)
select
id as id
from
tbl2
where
id = 1 -- trailing comment
on
conflict
do
nothing
;
11 changes: 10 additions & 1 deletion crates/uroborosql-fmt/testfiles/src/insert/insert_select.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,13 @@ insert into stafflist (name, address, staff_cls)
select name, address, /*#CLS_STAFF_CLS_NEW_COMER*/'0' from newcomer where flag = 'TRUE';

insert into stafflist (name, address, staff_cls)
(select name, address, /*#CLS_STAFF_CLS_NEW_COMER*/'0' from newcomer where flag = 'TRUE');
(select name, address, /*#CLS_STAFF_CLS_NEW_COMER*/'0' from newcomer where flag = 'TRUE');

insert into tbl (id)
select id from tbl2 where id = 1 -- trailing comment
;

insert into tbl (id)
select id from tbl2 where id = 1 -- trailing comment
on conflict do nothing
;

0 comments on commit cb71681

Please sign in to comment.