Skip to content

Commit

Permalink
support bind_param in first col of select clause
Browse files Browse the repository at this point in the history
  • Loading branch information
ppputtyo committed Nov 4, 2023
1 parent 3128fb8 commit dae400c
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 2 deletions.
2 changes: 1 addition & 1 deletion crates/uroborosql-fmt/src/cst/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}
}
14 changes: 14 additions & 0 deletions crates/uroborosql-fmt/src/cst/body/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
4 changes: 4 additions & 0 deletions crates/uroborosql-fmt/testfiles/dst/select/bind_param.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 5 additions & 1 deletion crates/uroborosql-fmt/testfiles/src/select/bind_param.sql
Original file line number Diff line number Diff line change
@@ -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
159 changes: 159 additions & 0 deletions err.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
Compiling uroborosql-fmt v0.1.0 (/Users/ppputtyo/Documents/Future/uroborosql-fmt/crates/uroborosql-fmt)
Compiling uroborosql-fmt-cli v0.1.0 (/Users/ppputtyo/Documents/Future/uroborosql-fmt/crates/uroborosql-fmt-cli)
Compiling uroborosql-fmt-napi v0.0.0 (/Users/ppputtyo/Documents/Future/uroborosql-fmt/crates/uroborosql-fmt-napi)
Compiling uroborosql-fmt-wasm v0.1.0 (/Users/ppputtyo/Documents/Future/uroborosql-fmt/crates/uroborosql-fmt-wasm)
Finished dev [unoptimized + debuginfo] target(s) in 7.35s
Running `target/debug/uroborosql-fmt-cli test.sql out.sql`

==================== normal mode ====================

source_file [(0, 0)-(1, 14)]
select_statement [(0, 0)-(1, 14)]
select_clause [(0, 0)-(1, 14)]
SELECT [(0, 0)-(0, 6)]
comment [(0, 7)-(0, 21)]
comment [(1, 1)-(1, 6)]
select_clause_body [(1, 6)-(1, 14)]
alias [(1, 6)-(1, 14)]
string [(1, 6)-(1, 9)]
' [(1, 6)-(1, 7)]
content [(1, 7)-(1, 8)]
' [(1, 8)-(1, 9)]
AS [(1, 10)-(1, 12)]
identifier [(1, 13)-(1, 14)]
[
Statement {
clauses: [
Clause {
keyword: "select",
body: Some(
Select(
SelectBody {
loc: Some(
Location {
start_position: Position {
row: 1,
col: 6,
},
end_position: Position {
row: 1,
col: 14,
},
},
),
all_distinct: None,
select_clause_body: Some(
SepLines(
SeparatedLines {
contents: [
SepLinesContent {
sep: None,
preceding_comments: [],
expr: AlignedExpr {
lhs: Primary(
PrimaryExpr {
element: "'x'",
loc: Location {
start_position: Position {
row: 1,
col: 1,
},
end_position: Position {
row: 1,
col: 9,
},
},
head_comment: Some(
"/*x*/",
),
},
),
rhs: Some(
Primary(
PrimaryExpr {
element: "x",
loc: Location {
start_position: Position {
row: 1,
col: 13,
},
end_position: Position {
row: 1,
col: 14,
},
},
head_comment: None,
},
),
),
op: Some(
"as",
),
loc: Location {
start_position: Position {
row: 1,
col: 6,
},
end_position: Position {
row: 1,
col: 14,
},
},
trailing_comment: None,
lhs_trailing_comment: None,
},
following_comments: [],
},
],
loc: Some(
Location {
start_position: Position {
row: 1,
col: 6,
},
end_position: Position {
row: 1,
col: 14,
},
},
),
},
),
),
},
),
),
loc: Location {
start_position: Position {
row: 0,
col: 0,
},
end_position: Position {
row: 1,
col: 14,
},
},
sql_id: Some(
SqlID {
sql_id: "/* _SQL_ID_ */",
},
),
comments: [],
},
],
loc: Some(
Location {
start_position: Position {
row: 0,
col: 0,
},
end_position: Position {
row: 1,
col: 14,
},
},
),
comments: [],
has_semi: false,
},
]
2 changes: 2 additions & 0 deletions out.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
select /* _SQL_ID_ */
/*x*/'x' as x
2 changes: 2 additions & 0 deletions test.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
select /* _SQL_ID_ */
/*x*/'x' as x

0 comments on commit dae400c

Please sign in to comment.