From 3825b7f3345d5f9ed452b92a9260c7b8071a09ec Mon Sep 17 00:00:00 2001 From: wugeer <1284057728@qq.com> Date: Fri, 6 Sep 2024 19:33:20 +0800 Subject: [PATCH] fix: ON UPDATE with two many blank formatted incorrectly (#46) --- src/formatter.rs | 6 +++--- src/lib.rs | 2 +- src/tokenizer.rs | 3 ++- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/formatter.rs b/src/formatter.rs index c18cc04..66126d4 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -137,11 +137,11 @@ impl<'a> Formatter<'a> { fn format_with_spaces(&self, token: &Token<'_>, query: &mut String) { let value = if token.kind == TokenKind::Reserved { - self.format_reserved_word(token.value) + &self.equalize_whitespace(&self.format_reserved_word(token.value)) } else { - Cow::Borrowed(token.value) + token.value }; - query.push_str(&value); + query.push_str(value); query.push(' '); } diff --git a/src/lib.rs b/src/lib.rs index 1800bf9..ad5b166 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1621,7 +1621,7 @@ mod tests { #[test] fn it_recognizes_on_update_clause() { let input = indoc!( - "CREATE TABLE a (b integer REFERENCES c (id) ON UPDATE RESTRICT, other integer);" + "CREATE TABLE a (b integer REFERENCES c (id) ON UPDATE RESTRICT, other integer);" ); let options = FormatOptions::default(); let expected = indoc!( diff --git a/src/tokenizer.rs b/src/tokenizer.rs index e0de6de..47f81e9 100644 --- a/src/tokenizer.rs +++ b/src/tokenizer.rs @@ -968,7 +968,8 @@ fn get_plain_reserved_two_token(input: &str) -> IResult<&str, Token<'_>> { terminated(tag("ON UPDATE"), end_of_word), ))(&uc_input); if let Ok((_, token)) = result { - let input_end_pos = token.len(); + let final_word = token.split(' ').last().unwrap(); + let input_end_pos = input.to_ascii_uppercase().find(final_word).unwrap() + final_word.len(); let (token, input) = input.split_at(input_end_pos); Ok(( input,