diff --git a/crates/codegen/src/get_node_properties.rs b/crates/codegen/src/get_node_properties.rs index d95347ae..a80aba26 100644 --- a/crates/codegen/src/get_node_properties.rs +++ b/crates/codegen/src/get_node_properties.rs @@ -952,6 +952,24 @@ 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)); + } 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..7905e271 100644 --- a/crates/parser/src/codegen.rs +++ b/crates/parser/src/codegen.rs @@ -358,4 +358,19 @@ 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::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 new file mode 100644 index 00000000..12906a21 --- /dev/null +++ b/crates/parser/tests/data/statements/valid/0057.sql @@ -0,0 +1,11 @@ +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@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, + }, + ], +} 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, + }, + ], +}