Skip to content

Commit

Permalink
Support arithmetic expressions in limit clause (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
lemonadern authored Aug 23, 2024
1 parent c794048 commit d4b56ae
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/uroborosql-fmt/src/cst/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ impl Expr {
Expr::Boolean(boolean) => boolean.set_head_comment(comment),
Expr::ColumnList(col_list) => col_list.set_head_comment(comment),
// primary, aligned, boolean以外の式は現状、バインドパラメータがつくことはない
Expr::ExprSeq(expr_seq) => expr_seq.set_head_comment_to_first_child(comment),
_ => unimplemented!(),
}
}
Expand Down
11 changes: 10 additions & 1 deletion crates/uroborosql-fmt/src/cst/expr/expr_seq.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
cst::{Location, Position},
cst::{Comment, Location, Position},
error::UroboroSQLFmtError,
util::{tab_size, to_tab_num},
};
Expand Down Expand Up @@ -34,6 +34,15 @@ impl ExprSeq {
self.loc.clone()
}

/// 先頭の Expr にバインドパラメータをセットする
pub(crate) fn set_head_comment_to_first_child(&mut self, comment: Comment) {
if let Some(first_expr) = self.exprs.first_mut() {
first_expr.set_head_comment(comment);
} else {
unreachable!()
}
}

pub(crate) fn is_multi_line(&self) -> bool {
self.exprs.iter().any(|e| e.is_multi_line())
}
Expand Down
17 changes: 4 additions & 13 deletions crates/uroborosql-fmt/src/visitor/clause/limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use tree_sitter::TreeCursor;
use crate::{
cst::*,
error::UroboroSQLFmtError,
visitor::{ensure_kind, error_annotation_from_cursor, Visitor, COMMENT},
visitor::{ensure_kind, Visitor, COMMENT},
};

impl Visitor {
Expand All @@ -28,13 +28,6 @@ impl Visitor {
}

match cursor.node().kind() {
"number" => {
// numberをExprに格納
let number = self.visit_expr(cursor, src)?;
// numberからBody::SingleLineを作成
let body = Body::SingleLine(Box::new(SingleLine::new(number)));
limit_clause.set_body(body);
}
"ALL" => {
// "LIMIT ALL"というキーワードと捉えて構造体に格納
let all_kw = PrimaryExpr::with_node(cursor.node(), src, PrimaryExprKind::Keyword);
Expand All @@ -43,11 +36,9 @@ impl Visitor {
limit_clause.set_body(body);
}
_ => {
return Err(UroboroSQLFmtError::UnexpectedSyntax(format!(
r#"visit_limit_clause(): expected node is number or ALL, but actual {}\n{}"#,
cursor.node().kind(),
error_annotation_from_cursor(cursor, src)
)));
let expr = self.visit_expr(cursor, src)?;
let body = Body::SingleLine(Box::new(SingleLine::new(expr)));
limit_clause.set_body(body);
}
}

Expand Down
18 changes: 18 additions & 0 deletions crates/uroborosql-fmt/testfiles/dst/select/limit_offset.sql
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,21 @@ order by
limit /*$hoge*/all
offset 5
;
select
tbl1.column1 as column1
from
table1 tbl1
order by
tbl1.column2 desc
limit 1 + 2
offset 5
;
select
tbl1.column1 as column1
from
table1 tbl1
order by
tbl1.column2 desc
limit /*$hoge*/100 + 1
offset 5
;
22 changes: 22 additions & 0 deletions crates/uroborosql-fmt/testfiles/src/select/limit_offset.sql
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,25 @@ ORDER BY
DESC
limit /*$hoge*/all
OFFSET 5;

SELECT
TBL1.COLUMN1
AS COLUMN1
FROM
TABLE1 TBL1
ORDER BY
TBL1.COLUMN2
DESC
limit 1 + 2
OFFSET 5;

SELECT
TBL1.COLUMN1
AS COLUMN1
FROM
TABLE1 TBL1
ORDER BY
TBL1.COLUMN2
DESC
limit /*$hoge*/100 + 1
OFFSET 5;

0 comments on commit d4b56ae

Please sign in to comment.