Skip to content

Commit

Permalink
Merge pull request #113 from pert5432/support-truncate
Browse files Browse the repository at this point in the history
  • Loading branch information
psteinroe authored Jan 21, 2024
2 parents 5ce7116 + 62c833b commit d8c7b3e
Show file tree
Hide file tree
Showing 9 changed files with 328 additions and 0 deletions.
18 changes: 18 additions & 0 deletions crates/codegen/src/get_node_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,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! {},
}
}
Expand Down
15 changes: 15 additions & 0 deletions crates/parser/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
],
)
}
}
11 changes: 11 additions & 0 deletions crates/parser/tests/data/statements/valid/0057.sql
Original file line number Diff line number Diff line change
@@ -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;
50 changes: 50 additions & 0 deletions crates/parser/tests/snapshots/statements/valid/0057@1.snap
Original file line number Diff line number Diff line change
@@ -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,
},
],
}
52 changes: 52 additions & 0 deletions crates/parser/tests/snapshots/statements/valid/0057@2.snap
Original file line number Diff line number Diff line change
@@ -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,
},
],
}
44 changes: 44 additions & 0 deletions crates/parser/tests/snapshots/statements/valid/0057@3.snap
Original file line number Diff line number Diff line change
@@ -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,
},
],
}
46 changes: 46 additions & 0 deletions crates/parser/tests/snapshots/statements/valid/0057@4.snap
Original file line number Diff line number Diff line change
@@ -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,
},
],
}
46 changes: 46 additions & 0 deletions crates/parser/tests/snapshots/statements/valid/0057@5.snap
Original file line number Diff line number Diff line change
@@ -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,
},
],
}
46 changes: 46 additions & 0 deletions crates/parser/tests/snapshots/statements/valid/0057@6.snap
Original file line number Diff line number Diff line change
@@ -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,
},
],
}

0 comments on commit d8c7b3e

Please sign in to comment.