From 1cf54a19aa0f5ce04c7f0e9943be73059df1a089 Mon Sep 17 00:00:00 2001 From: Taishi Naka <62321668+lemonadern@users.noreply.github.com> Date: Thu, 12 Sep 2024 12:46:32 +0900 Subject: [PATCH] Support comments before select_statement in insert_statement (#101) --- .../src/visitor/statement/insert.rs | 28 ++++++++++++++++++- .../testfiles/dst/insert/insert_select.sql | 15 ++++++++++ .../testfiles/src/insert/insert_select.sql | 5 ++++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/crates/uroborosql-fmt/src/visitor/statement/insert.rs b/crates/uroborosql-fmt/src/visitor/statement/insert.rs index bde6532..49aeebe 100644 --- a/crates/uroborosql-fmt/src/visitor/statement/insert.rs +++ b/crates/uroborosql-fmt/src/visitor/statement/insert.rs @@ -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)?; @@ -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)?; diff --git a/crates/uroborosql-fmt/testfiles/dst/insert/insert_select.sql b/crates/uroborosql-fmt/testfiles/dst/insert/insert_select.sql index 14b7565..498a40d 100644 --- a/crates/uroborosql-fmt/testfiles/dst/insert/insert_select.sql +++ b/crates/uroborosql-fmt/testfiles/dst/insert/insert_select.sql @@ -63,3 +63,18 @@ on do nothing ; +insert +into + tbl +( + id +) +-- comments +-- before select +select + id as id +from + tbl2 +where + id = 1 +; diff --git a/crates/uroborosql-fmt/testfiles/src/insert/insert_select.sql b/crates/uroborosql-fmt/testfiles/src/insert/insert_select.sql index 2c6d3af..f8e0b4b 100644 --- a/crates/uroborosql-fmt/testfiles/src/insert/insert_select.sql +++ b/crates/uroborosql-fmt/testfiles/src/insert/insert_select.sql @@ -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;