Skip to content

Commit 55d2ba5

Browse files
wugeershssoichiro
authored andcommitted
fix: REFERENCES xyz ON UPDATE .. causes formatter to treat the remaining as an UPDATE statement
1 parent cec5a6f commit 55d2ba5

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

src/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,4 +1617,20 @@ mod tests {
16171617

16181618
assert_eq!(format(input, &QueryParams::None, options), expected);
16191619
}
1620+
1621+
#[test]
1622+
fn it_recognizes_on_update_clause() {
1623+
let input = indoc!(
1624+
"CREATE TABLE a (b integer REFERENCES c (id) ON UPDATE RESTRICT, other integer);"
1625+
);
1626+
let options = FormatOptions::default();
1627+
let expected = indoc!(
1628+
"
1629+
CREATE TABLE a (
1630+
b integer REFERENCES c (id) ON UPDATE RESTRICT,
1631+
other integer
1632+
);"
1633+
);
1634+
assert_eq!(format(input, &QueryParams::None, options), expected);
1635+
}
16201636
}

src/tokenizer.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -574,8 +574,10 @@ fn get_top_level_reserved_token_no_indent(input: &str) -> IResult<&str, Token<'_
574574
Err(Err::Error(Error::new(input, ErrorKind::Alt)))
575575
}
576576
}
577-
578577
fn get_plain_reserved_token(input: &str) -> IResult<&str, Token<'_>> {
578+
alt((get_plain_reserved_two_token, get_plain_reserved_one_token))(input)
579+
}
580+
fn get_plain_reserved_one_token(input: &str) -> IResult<&str, Token<'_>> {
579581
let uc_input = get_uc_words(input, 1);
580582
let result: IResult<&str, &str> = alt((
581583
terminated(tag("ACCESSIBLE"), end_of_word),
@@ -600,7 +602,6 @@ fn get_plain_reserved_token(input: &str) -> IResult<&str, Token<'_>> {
600602
alt((
601603
terminated(tag("CHANGE"), end_of_word),
602604
terminated(tag("CHANGED"), end_of_word),
603-
terminated(tag("CHARACTER SET"), end_of_word),
604605
terminated(tag("CHARSET"), end_of_word),
605606
terminated(tag("CHECK"), end_of_word),
606607
terminated(tag("CHECKSUM"), end_of_word),
@@ -741,8 +742,6 @@ fn get_plain_reserved_token(input: &str) -> IResult<&str, Token<'_>> {
741742
terminated(tag("NOW()"), end_of_word),
742743
terminated(tag("NULL"), end_of_word),
743744
terminated(tag("OFFSET"), end_of_word),
744-
terminated(tag("ON DELETE"), end_of_word),
745-
terminated(tag("ON UPDATE"), end_of_word),
746745
alt((
747746
terminated(tag("ON"), end_of_word),
748747
terminated(tag("ONLY"), end_of_word),
@@ -934,6 +933,29 @@ fn get_plain_reserved_token(input: &str) -> IResult<&str, Token<'_>> {
934933
}
935934
}
936935

936+
fn get_plain_reserved_two_token(input: &str) -> IResult<&str, Token<'_>> {
937+
let uc_input = get_uc_words(input, 2);
938+
let result: IResult<&str, &str> = alt((
939+
terminated(tag("CHARACTER SET"), end_of_word),
940+
terminated(tag("ON DELETE"), end_of_word),
941+
terminated(tag("ON UPDATE"), end_of_word),
942+
))(&uc_input);
943+
if let Ok((_, token)) = result {
944+
let input_end_pos = token.len();
945+
let (token, input) = input.split_at(input_end_pos);
946+
Ok((
947+
input,
948+
Token {
949+
kind: TokenKind::Reserved,
950+
value: token,
951+
key: None,
952+
},
953+
))
954+
} else {
955+
Err(Err::Error(Error::new(input, ErrorKind::Alt)))
956+
}
957+
}
958+
937959
fn get_word_token(input: &str) -> IResult<&str, Token<'_>> {
938960
take_while1(is_word_character)(input).map(|(input, token)| {
939961
(

0 commit comments

Comments
 (0)