Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ON UPDATE with two many blank formatted incorrectly #46

Merged
merged 2 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl<'a> Formatter<'a> {

let previous_token = self.previous_token(1);
if previous_token.is_some()
&& previous_token.unwrap().value.contains("\n")
&& previous_token.unwrap().value.contains('\n')
&& is_whitespace_followed_by_special_token
{
self.add_new_line(query);
Expand Down Expand Up @@ -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(' ');
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! This crate is a port of https://github.com/kufii/sql-formatter-plus

Check warning on line 1 in src/lib.rs

View workflow job for this annotation

GitHub Actions / build-test-unix (latest-stable)

this URL is not a hyperlink
//! written in Rust. It is intended to be usable as a pure-Rust library
//! for formatting SQL queries.

Expand Down Expand Up @@ -1621,7 +1621,7 @@
#[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!(
Expand Down
3 changes: 2 additions & 1 deletion src/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,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,
Expand Down
Loading