diff --git a/crates/uroborosql-fmt/src/cst/body.rs b/crates/uroborosql-fmt/src/cst/body.rs index f394711..759a8b0 100644 --- a/crates/uroborosql-fmt/src/cst/body.rs +++ b/crates/uroborosql-fmt/src/cst/body.rs @@ -107,7 +107,7 @@ impl Body { Body::Insert(_) => false, Body::With(_) => false, Body::SingleLine(single_line) => single_line.try_set_head_comment(comment), - Body::Select(_) => false, + Body::Select(select) => select.try_set_head_comment(comment), } } } diff --git a/crates/uroborosql-fmt/src/cst/body/select.rs b/crates/uroborosql-fmt/src/cst/body/select.rs index 017f5e8..0748cd8 100644 --- a/crates/uroborosql-fmt/src/cst/body/select.rs +++ b/crates/uroborosql-fmt/src/cst/body/select.rs @@ -56,6 +56,20 @@ impl SelectBody { Ok(()) } + pub(crate) fn try_set_head_comment(&mut self, comment: Comment) -> bool { + // select_clause_bodyが存在し、空ではなく、先頭とコメントが隣接している場合 + // select_clause_bodyの先頭要素にバインドパラメータをセットする + if let Some(select_clause_body) = &mut self.select_clause_body { + if let Some(select_clause_body_loc) = select_clause_body.loc() { + if comment.loc().is_next_to(&select_clause_body_loc) { + return select_clause_body.try_set_head_comment(comment); + } + } + } + + false + } + pub(crate) fn is_empty(&self) -> bool { self.all_distinct.is_none() && self.select_clause_body.is_none() } diff --git a/crates/uroborosql-fmt/testfiles/dst/select/bind_param.sql b/crates/uroborosql-fmt/testfiles/dst/select/bind_param.sql index 8860fc2..379d0ca 100644 --- a/crates/uroborosql-fmt/testfiles/dst/select/bind_param.sql +++ b/crates/uroborosql-fmt/testfiles/dst/select/bind_param.sql @@ -6,3 +6,7 @@ from where t.a = /*var*/1 and /*var2*/1 = t.c +; +select /* _SQL_ID_ */ + /*x*/'x' as x +, /*y*/'y' as y diff --git a/crates/uroborosql-fmt/testfiles/src/select/bind_param.sql b/crates/uroborosql-fmt/testfiles/src/select/bind_param.sql index f3b5b3e..154b355 100644 --- a/crates/uroborosql-fmt/testfiles/src/select/bind_param.sql +++ b/crates/uroborosql-fmt/testfiles/src/select/bind_param.sql @@ -1,4 +1,8 @@ select a, /*param*/'1' as b from t where t.a = /*var*/1 -and /*var2*/1 = t.c +and /*var2*/1 = t.c; + +select /* _SQL_ID_ */ +/*x*/'x' as x +, /*y*/'y' as y