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: REFERENCES xyz ON UPDATE .. causes formatter to treat the remaining as an UPDATE statement #43

Merged
merged 1 commit into from
Aug 29, 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
16 changes: 16 additions & 0 deletions 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 @@ -1616,4 +1616,20 @@

assert_eq!(format(input, &QueryParams::None, options), expected);
}

#[test]
fn it_recognizes_on_update_clause() {
let input = indoc!(
"CREATE TABLE a (b integer REFERENCES c (id) ON UPDATE RESTRICT, other integer);"
);
let options = FormatOptions::default();
let expected = indoc!(
"
CREATE TABLE a (
b integer REFERENCES c (id) ON UPDATE RESTRICT,
other integer
);"
);
assert_eq!(format(input, &QueryParams::None, options), expected);
}
}
30 changes: 26 additions & 4 deletions src/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,8 +569,10 @@ fn get_top_level_reserved_token_no_indent(input: &str) -> IResult<&str, Token<'_
Err(Err::Error(Error::new(input, ErrorKind::Alt)))
}
}

fn get_plain_reserved_token(input: &str) -> IResult<&str, Token<'_>> {
alt((get_plain_reserved_two_token, get_plain_reserved_one_token))(input)
}
fn get_plain_reserved_one_token(input: &str) -> IResult<&str, Token<'_>> {
let uc_input = get_uc_words(input, 1);
let result: IResult<&str, &str> = alt((
terminated(tag("ACCESSIBLE"), end_of_word),
Expand All @@ -595,7 +597,6 @@ fn get_plain_reserved_token(input: &str) -> IResult<&str, Token<'_>> {
alt((
terminated(tag("CHANGE"), end_of_word),
terminated(tag("CHANGED"), end_of_word),
terminated(tag("CHARACTER SET"), end_of_word),
terminated(tag("CHARSET"), end_of_word),
terminated(tag("CHECK"), end_of_word),
terminated(tag("CHECKSUM"), end_of_word),
Expand Down Expand Up @@ -736,8 +737,6 @@ fn get_plain_reserved_token(input: &str) -> IResult<&str, Token<'_>> {
terminated(tag("NOW()"), end_of_word),
terminated(tag("NULL"), end_of_word),
terminated(tag("OFFSET"), end_of_word),
terminated(tag("ON DELETE"), end_of_word),
terminated(tag("ON UPDATE"), end_of_word),
alt((
terminated(tag("ON"), end_of_word),
terminated(tag("ONLY"), end_of_word),
Expand Down Expand Up @@ -929,6 +928,29 @@ fn get_plain_reserved_token(input: &str) -> IResult<&str, Token<'_>> {
}
}

fn get_plain_reserved_two_token(input: &str) -> IResult<&str, Token<'_>> {
let uc_input = get_uc_words(input, 2);
let result: IResult<&str, &str> = alt((
terminated(tag("CHARACTER SET"), end_of_word),
terminated(tag("ON DELETE"), end_of_word),
terminated(tag("ON UPDATE"), end_of_word),
))(&uc_input);
if let Ok((_, token)) = result {
let input_end_pos = token.len();
let (token, input) = input.split_at(input_end_pos);
Ok((
input,
Token {
kind: TokenKind::Reserved,
value: token,
key: None,
},
))
} else {
Err(Err::Error(Error::new(input, ErrorKind::Alt)))
}
}

fn get_word_token(input: &str) -> IResult<&str, Token<'_>> {
take_while1(is_word_character)(input).map(|(input, token)| {
(
Expand Down
Loading