From 3818bbc9d89eb6c9f1ec443289f9b2810c15554b Mon Sep 17 00:00:00 2001 From: MikeRotchip Date: Sat, 13 Jan 2024 16:15:20 +0100 Subject: [PATCH 1/3] feat: add support for truncate statement --- crates/codegen/src/get_node_properties.rs | 17 +++++++++++++++++ crates/parser/src/codegen.rs | 14 ++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/crates/codegen/src/get_node_properties.rs b/crates/codegen/src/get_node_properties.rs index d95347ae..6df9ad7a 100644 --- a/crates/codegen/src/get_node_properties.rs +++ b/crates/codegen/src/get_node_properties.rs @@ -952,6 +952,23 @@ fn custom_handlers(node: &Node) -> TokenStream { } } }, + "TruncateStmt" => quote! { + tokens.push(TokenProperty::from(Token::Truncate)); + if n.restart_seqs { + tokens.push(TokenProperty::from(Token::Restart)); + tokens.push(TokenProperty::from(Token::IdentityP)); + } else { + tokens.push(TokenProperty::from(Token::ContinueP)); + tokens.push(TokenProperty::from(Token::IdentityP)); + } + match n.behavior { + // DropRestrict + 1 => tokens.push(TokenProperty::from(Token::Restrict)), + // DropCascade + 2 => tokens.push(TokenProperty::from(Token::Cascade)), + _ => {} + } + }, _ => quote! {}, } } diff --git a/crates/parser/src/codegen.rs b/crates/parser/src/codegen.rs index f0b631f2..df992e20 100644 --- a/crates/parser/src/codegen.rs +++ b/crates/parser/src/codegen.rs @@ -358,4 +358,18 @@ mod tests { ], ) } + + #[test] + fn test_truncate() { + test_get_node_properties( + "TRUNCATE TABLE users CONTINUE IDENTITY RESTRICT", + SyntaxKind::TruncateStmt, + vec![ + TokenProperty::from(SyntaxKind::Truncate), + TokenProperty::from(SyntaxKind::ContinueP), + TokenProperty::from(SyntaxKind::IdentityP), + TokenProperty::from(SyntaxKind::Restrict), + ], + ) + } } From c2b05c77351557fcc5b8d58a434ab212764a1985 Mon Sep 17 00:00:00 2001 From: MikeRotchip Date: Sat, 20 Jan 2024 14:23:12 +0100 Subject: [PATCH 2/3] add snapshot test --- .../tests/data/statements/valid/0057.sql | 1 + .../snapshots/statements/valid/0057@1.snap | 50 +++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 crates/parser/tests/data/statements/valid/0057.sql create mode 100644 crates/parser/tests/snapshots/statements/valid/0057@1.snap diff --git a/crates/parser/tests/data/statements/valid/0057.sql b/crates/parser/tests/data/statements/valid/0057.sql new file mode 100644 index 00000000..97415a72 --- /dev/null +++ b/crates/parser/tests/data/statements/valid/0057.sql @@ -0,0 +1 @@ +TRUNCATE users CONTINUE IDENTITY RESTRICT \ No newline at end of file diff --git a/crates/parser/tests/snapshots/statements/valid/0057@1.snap b/crates/parser/tests/snapshots/statements/valid/0057@1.snap new file mode 100644 index 00000000..2f3f3705 --- /dev/null +++ b/crates/parser/tests/snapshots/statements/valid/0057@1.snap @@ -0,0 +1,50 @@ +--- +source: crates/parser/tests/statement_parser_test.rs +assertion_line: 62 +description: TRUNCATE users CONTINUE IDENTITY RESTRICT; +--- +Parse { + cst: SourceFile@0..42 + TruncateStmt@0..42 + Truncate@0..8 "TRUNCATE" + Whitespace@8..9 " " + RangeVar@9..14 + Ident@9..14 "users" + Whitespace@14..15 " " + ContinueP@15..23 "CONTINUE" + Whitespace@23..24 " " + IdentityP@24..32 "IDENTITY" + Whitespace@32..33 " " + Restrict@33..41 "RESTRICT" + Ascii59@41..42 ";" + , + errors: [], + stmts: [ + RawStmt { + stmt: TruncateStmt( + TruncateStmt { + relations: [ + Node { + node: Some( + RangeVar( + RangeVar { + catalogname: "", + schemaname: "", + relname: "users", + inh: true, + relpersistence: "p", + alias: None, + location: 9, + }, + ), + ), + }, + ], + restart_seqs: false, + behavior: DropRestrict, + }, + ), + range: 0..41, + }, + ], +} From 62c833b0985cbef1c3a1c65914a01f46f3048411 Mon Sep 17 00:00:00 2001 From: MikeRotchip Date: Sun, 21 Jan 2024 17:32:45 +0100 Subject: [PATCH 3/3] emit TABLE token, add more tests --- crates/codegen/src/get_node_properties.rs | 1 + crates/parser/src/codegen.rs | 1 + .../tests/data/statements/valid/0057.sql | 12 ++++- .../snapshots/statements/valid/0057@2.snap | 52 +++++++++++++++++++ .../snapshots/statements/valid/0057@3.snap | 44 ++++++++++++++++ .../snapshots/statements/valid/0057@4.snap | 46 ++++++++++++++++ .../snapshots/statements/valid/0057@5.snap | 46 ++++++++++++++++ .../snapshots/statements/valid/0057@6.snap | 46 ++++++++++++++++ 8 files changed, 247 insertions(+), 1 deletion(-) create mode 100644 crates/parser/tests/snapshots/statements/valid/0057@2.snap create mode 100644 crates/parser/tests/snapshots/statements/valid/0057@3.snap create mode 100644 crates/parser/tests/snapshots/statements/valid/0057@4.snap create mode 100644 crates/parser/tests/snapshots/statements/valid/0057@5.snap create mode 100644 crates/parser/tests/snapshots/statements/valid/0057@6.snap diff --git a/crates/codegen/src/get_node_properties.rs b/crates/codegen/src/get_node_properties.rs index 6df9ad7a..a80aba26 100644 --- a/crates/codegen/src/get_node_properties.rs +++ b/crates/codegen/src/get_node_properties.rs @@ -954,6 +954,7 @@ fn custom_handlers(node: &Node) -> TokenStream { }, "TruncateStmt" => quote! { tokens.push(TokenProperty::from(Token::Truncate)); + tokens.push(TokenProperty::from(Token::Table)); if n.restart_seqs { tokens.push(TokenProperty::from(Token::Restart)); tokens.push(TokenProperty::from(Token::IdentityP)); diff --git a/crates/parser/src/codegen.rs b/crates/parser/src/codegen.rs index df992e20..7905e271 100644 --- a/crates/parser/src/codegen.rs +++ b/crates/parser/src/codegen.rs @@ -366,6 +366,7 @@ mod tests { SyntaxKind::TruncateStmt, vec![ TokenProperty::from(SyntaxKind::Truncate), + TokenProperty::from(SyntaxKind::Table), TokenProperty::from(SyntaxKind::ContinueP), TokenProperty::from(SyntaxKind::IdentityP), TokenProperty::from(SyntaxKind::Restrict), diff --git a/crates/parser/tests/data/statements/valid/0057.sql b/crates/parser/tests/data/statements/valid/0057.sql index 97415a72..12906a21 100644 --- a/crates/parser/tests/data/statements/valid/0057.sql +++ b/crates/parser/tests/data/statements/valid/0057.sql @@ -1 +1,11 @@ -TRUNCATE users CONTINUE IDENTITY RESTRICT \ No newline at end of file +TRUNCATE users CONTINUE IDENTITY RESTRICT; + +TRUNCATE TABLE users RESTART IDENTITY CASCADE; + +TRUNCATE users; + +TRUNCATE accounts CASCADE; + +TRUNCATE accounts RESTRICT; + +TRUNCATE TABLE users; \ No newline at end of file diff --git a/crates/parser/tests/snapshots/statements/valid/0057@2.snap b/crates/parser/tests/snapshots/statements/valid/0057@2.snap new file mode 100644 index 00000000..1072629c --- /dev/null +++ b/crates/parser/tests/snapshots/statements/valid/0057@2.snap @@ -0,0 +1,52 @@ +--- +source: crates/parser/tests/statement_parser_test.rs +assertion_line: 62 +description: TRUNCATE TABLE users RESTART IDENTITY CASCADE; +--- +Parse { + cst: SourceFile@0..46 + TruncateStmt@0..46 + Truncate@0..8 "TRUNCATE" + Whitespace@8..9 " " + Table@9..14 "TABLE" + Whitespace@14..15 " " + RangeVar@15..20 + Ident@15..20 "users" + Whitespace@20..21 " " + Restart@21..28 "RESTART" + Whitespace@28..29 " " + IdentityP@29..37 "IDENTITY" + Whitespace@37..38 " " + Cascade@38..45 "CASCADE" + Ascii59@45..46 ";" + , + errors: [], + stmts: [ + RawStmt { + stmt: TruncateStmt( + TruncateStmt { + relations: [ + Node { + node: Some( + RangeVar( + RangeVar { + catalogname: "", + schemaname: "", + relname: "users", + inh: true, + relpersistence: "p", + alias: None, + location: 15, + }, + ), + ), + }, + ], + restart_seqs: true, + behavior: DropCascade, + }, + ), + range: 0..45, + }, + ], +} diff --git a/crates/parser/tests/snapshots/statements/valid/0057@3.snap b/crates/parser/tests/snapshots/statements/valid/0057@3.snap new file mode 100644 index 00000000..cbc0dbad --- /dev/null +++ b/crates/parser/tests/snapshots/statements/valid/0057@3.snap @@ -0,0 +1,44 @@ +--- +source: crates/parser/tests/statement_parser_test.rs +assertion_line: 62 +description: TRUNCATE users; +--- +Parse { + cst: SourceFile@0..15 + TruncateStmt@0..15 + Truncate@0..8 "TRUNCATE" + Whitespace@8..9 " " + RangeVar@9..14 + Ident@9..14 "users" + Ascii59@14..15 ";" + , + errors: [], + stmts: [ + RawStmt { + stmt: TruncateStmt( + TruncateStmt { + relations: [ + Node { + node: Some( + RangeVar( + RangeVar { + catalogname: "", + schemaname: "", + relname: "users", + inh: true, + relpersistence: "p", + alias: None, + location: 9, + }, + ), + ), + }, + ], + restart_seqs: false, + behavior: DropRestrict, + }, + ), + range: 0..14, + }, + ], +} diff --git a/crates/parser/tests/snapshots/statements/valid/0057@4.snap b/crates/parser/tests/snapshots/statements/valid/0057@4.snap new file mode 100644 index 00000000..524f90d1 --- /dev/null +++ b/crates/parser/tests/snapshots/statements/valid/0057@4.snap @@ -0,0 +1,46 @@ +--- +source: crates/parser/tests/statement_parser_test.rs +assertion_line: 62 +description: TRUNCATE accounts CASCADE; +--- +Parse { + cst: SourceFile@0..26 + TruncateStmt@0..26 + Truncate@0..8 "TRUNCATE" + Whitespace@8..9 " " + RangeVar@9..17 + Ident@9..17 "accounts" + Whitespace@17..18 " " + Cascade@18..25 "CASCADE" + Ascii59@25..26 ";" + , + errors: [], + stmts: [ + RawStmt { + stmt: TruncateStmt( + TruncateStmt { + relations: [ + Node { + node: Some( + RangeVar( + RangeVar { + catalogname: "", + schemaname: "", + relname: "accounts", + inh: true, + relpersistence: "p", + alias: None, + location: 9, + }, + ), + ), + }, + ], + restart_seqs: false, + behavior: DropCascade, + }, + ), + range: 0..25, + }, + ], +} diff --git a/crates/parser/tests/snapshots/statements/valid/0057@5.snap b/crates/parser/tests/snapshots/statements/valid/0057@5.snap new file mode 100644 index 00000000..974597ad --- /dev/null +++ b/crates/parser/tests/snapshots/statements/valid/0057@5.snap @@ -0,0 +1,46 @@ +--- +source: crates/parser/tests/statement_parser_test.rs +assertion_line: 62 +description: TRUNCATE accounts RESTRICT; +--- +Parse { + cst: SourceFile@0..27 + TruncateStmt@0..27 + Truncate@0..8 "TRUNCATE" + Whitespace@8..9 " " + RangeVar@9..17 + Ident@9..17 "accounts" + Whitespace@17..18 " " + Restrict@18..26 "RESTRICT" + Ascii59@26..27 ";" + , + errors: [], + stmts: [ + RawStmt { + stmt: TruncateStmt( + TruncateStmt { + relations: [ + Node { + node: Some( + RangeVar( + RangeVar { + catalogname: "", + schemaname: "", + relname: "accounts", + inh: true, + relpersistence: "p", + alias: None, + location: 9, + }, + ), + ), + }, + ], + restart_seqs: false, + behavior: DropRestrict, + }, + ), + range: 0..26, + }, + ], +} diff --git a/crates/parser/tests/snapshots/statements/valid/0057@6.snap b/crates/parser/tests/snapshots/statements/valid/0057@6.snap new file mode 100644 index 00000000..ff4635f5 --- /dev/null +++ b/crates/parser/tests/snapshots/statements/valid/0057@6.snap @@ -0,0 +1,46 @@ +--- +source: crates/parser/tests/statement_parser_test.rs +assertion_line: 62 +description: TRUNCATE TABLE users; +--- +Parse { + cst: SourceFile@0..21 + TruncateStmt@0..21 + Truncate@0..8 "TRUNCATE" + Whitespace@8..9 " " + Table@9..14 "TABLE" + Whitespace@14..15 " " + RangeVar@15..20 + Ident@15..20 "users" + Ascii59@20..21 ";" + , + errors: [], + stmts: [ + RawStmt { + stmt: TruncateStmt( + TruncateStmt { + relations: [ + Node { + node: Some( + RangeVar( + RangeVar { + catalogname: "", + schemaname: "", + relname: "users", + inh: true, + relpersistence: "p", + alias: None, + location: 15, + }, + ), + ), + }, + ], + restart_seqs: false, + behavior: DropRestrict, + }, + ), + range: 0..20, + }, + ], +}