Skip to content

Commit

Permalink
Support using clause in DELETE statement (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
ppputtyo authored Nov 1, 2023
1 parent 2d255b8 commit 3128fb8
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 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.

37 changes: 36 additions & 1 deletion crates/uroborosql-fmt/src/visitor/statement/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ use tree_sitter::TreeCursor;
use crate::{
cst::*,
error::UroboroSQLFmtError,
visitor::{create_clause, ensure_kind, error_annotation_from_cursor, Visitor, COMMENT},
visitor::{
create_clause, ensure_kind, error_annotation_from_cursor,
expr::{ComplementConfig, ComplementKind},
Visitor, COMMENT,
},
};

impl Visitor {
Expand Down Expand Up @@ -45,6 +49,10 @@ impl Visitor {

while cursor.goto_next_sibling() {
match cursor.node().kind() {
"using_table_list" => {
let clause = self.visit_using_table_list(cursor, src)?;
statement.add_clause(clause);
}
"where_clause" => {
let clause = self.visit_where_clause(cursor, src)?;
statement.add_clause(clause);
Expand Down Expand Up @@ -72,4 +80,31 @@ impl Visitor {

Ok(statement)
}

pub(crate) fn visit_using_table_list(
&mut self,
cursor: &mut TreeCursor,
src: &str,
) -> Result<Clause, UroboroSQLFmtError> {
// using_table_listは必ずUSINGを子供に持つ
cursor.goto_first_child();

// cursor -> USING
let mut clause = create_clause(cursor, src, "USING")?;
cursor.goto_next_sibling();
self.consume_comment_in_clause(cursor, src, &mut clause)?;

// ASがあれば除去する
// エイリアス補完は現状行わない
let complement_config = ComplementConfig::new(ComplementKind::TableName, true, false);
let body = self.visit_comma_sep_alias(cursor, src, Some(&complement_config))?;

clause.set_body(body);

// cursorをusing_table_listに戻す
cursor.goto_parent();
ensure_kind(cursor, "using_table_list", src)?;

Ok(clause)
}
}
11 changes: 11 additions & 0 deletions crates/uroborosql-fmt/testfiles/dst/delete/using.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
delete
from
tbl_a a
using
tbl_b b
, tbl_c c
where
a.col1 = b.col1
and b.col2 > 10
and c.col3 = 'abc'
;
3 changes: 3 additions & 0 deletions crates/uroborosql-fmt/testfiles/src/delete/using.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
delete from tbl_a a
using tbl_b b, tbl_c c
where a.col1 = b.col1 and b.col2 > 10 and c.col3 = 'abc';

0 comments on commit 3128fb8

Please sign in to comment.