Skip to content

Commit d8c7b3e

Browse files
authored
Merge pull request #113 from pert5432/support-truncate
2 parents 5ce7116 + 62c833b commit d8c7b3e

File tree

9 files changed

+328
-0
lines changed

9 files changed

+328
-0
lines changed

crates/codegen/src/get_node_properties.rs

+18
Original file line numberDiff line numberDiff line change
@@ -953,6 +953,24 @@ fn custom_handlers(node: &Node) -> TokenStream {
953953
}
954954
}
955955
},
956+
"TruncateStmt" => quote! {
957+
tokens.push(TokenProperty::from(Token::Truncate));
958+
tokens.push(TokenProperty::from(Token::Table));
959+
if n.restart_seqs {
960+
tokens.push(TokenProperty::from(Token::Restart));
961+
tokens.push(TokenProperty::from(Token::IdentityP));
962+
} else {
963+
tokens.push(TokenProperty::from(Token::ContinueP));
964+
tokens.push(TokenProperty::from(Token::IdentityP));
965+
}
966+
match n.behavior {
967+
// DropRestrict
968+
1 => tokens.push(TokenProperty::from(Token::Restrict)),
969+
// DropCascade
970+
2 => tokens.push(TokenProperty::from(Token::Cascade)),
971+
_ => {}
972+
}
973+
},
956974
_ => quote! {},
957975
}
958976
}

crates/parser/src/codegen.rs

+15
Original file line numberDiff line numberDiff line change
@@ -358,4 +358,19 @@ mod tests {
358358
],
359359
)
360360
}
361+
362+
#[test]
363+
fn test_truncate() {
364+
test_get_node_properties(
365+
"TRUNCATE TABLE users CONTINUE IDENTITY RESTRICT",
366+
SyntaxKind::TruncateStmt,
367+
vec![
368+
TokenProperty::from(SyntaxKind::Truncate),
369+
TokenProperty::from(SyntaxKind::Table),
370+
TokenProperty::from(SyntaxKind::ContinueP),
371+
TokenProperty::from(SyntaxKind::IdentityP),
372+
TokenProperty::from(SyntaxKind::Restrict),
373+
],
374+
)
375+
}
361376
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
TRUNCATE users CONTINUE IDENTITY RESTRICT;
2+
3+
TRUNCATE TABLE users RESTART IDENTITY CASCADE;
4+
5+
TRUNCATE users;
6+
7+
TRUNCATE accounts CASCADE;
8+
9+
TRUNCATE accounts RESTRICT;
10+
11+
TRUNCATE TABLE users;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
source: crates/parser/tests/statement_parser_test.rs
3+
assertion_line: 62
4+
description: TRUNCATE users CONTINUE IDENTITY RESTRICT;
5+
---
6+
Parse {
7+
cst: SourceFile@0..42
8+
TruncateStmt@0..42
9+
Truncate@0..8 "TRUNCATE"
10+
Whitespace@8..9 " "
11+
RangeVar@9..14
12+
Ident@9..14 "users"
13+
Whitespace@14..15 " "
14+
ContinueP@15..23 "CONTINUE"
15+
Whitespace@23..24 " "
16+
IdentityP@24..32 "IDENTITY"
17+
Whitespace@32..33 " "
18+
Restrict@33..41 "RESTRICT"
19+
Ascii59@41..42 ";"
20+
,
21+
errors: [],
22+
stmts: [
23+
RawStmt {
24+
stmt: TruncateStmt(
25+
TruncateStmt {
26+
relations: [
27+
Node {
28+
node: Some(
29+
RangeVar(
30+
RangeVar {
31+
catalogname: "",
32+
schemaname: "",
33+
relname: "users",
34+
inh: true,
35+
relpersistence: "p",
36+
alias: None,
37+
location: 9,
38+
},
39+
),
40+
),
41+
},
42+
],
43+
restart_seqs: false,
44+
behavior: DropRestrict,
45+
},
46+
),
47+
range: 0..41,
48+
},
49+
],
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
source: crates/parser/tests/statement_parser_test.rs
3+
assertion_line: 62
4+
description: TRUNCATE TABLE users RESTART IDENTITY CASCADE;
5+
---
6+
Parse {
7+
cst: SourceFile@0..46
8+
TruncateStmt@0..46
9+
Truncate@0..8 "TRUNCATE"
10+
Whitespace@8..9 " "
11+
Table@9..14 "TABLE"
12+
Whitespace@14..15 " "
13+
RangeVar@15..20
14+
Ident@15..20 "users"
15+
Whitespace@20..21 " "
16+
Restart@21..28 "RESTART"
17+
Whitespace@28..29 " "
18+
IdentityP@29..37 "IDENTITY"
19+
Whitespace@37..38 " "
20+
Cascade@38..45 "CASCADE"
21+
Ascii59@45..46 ";"
22+
,
23+
errors: [],
24+
stmts: [
25+
RawStmt {
26+
stmt: TruncateStmt(
27+
TruncateStmt {
28+
relations: [
29+
Node {
30+
node: Some(
31+
RangeVar(
32+
RangeVar {
33+
catalogname: "",
34+
schemaname: "",
35+
relname: "users",
36+
inh: true,
37+
relpersistence: "p",
38+
alias: None,
39+
location: 15,
40+
},
41+
),
42+
),
43+
},
44+
],
45+
restart_seqs: true,
46+
behavior: DropCascade,
47+
},
48+
),
49+
range: 0..45,
50+
},
51+
],
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
source: crates/parser/tests/statement_parser_test.rs
3+
assertion_line: 62
4+
description: TRUNCATE users;
5+
---
6+
Parse {
7+
cst: SourceFile@0..15
8+
TruncateStmt@0..15
9+
Truncate@0..8 "TRUNCATE"
10+
Whitespace@8..9 " "
11+
RangeVar@9..14
12+
Ident@9..14 "users"
13+
Ascii59@14..15 ";"
14+
,
15+
errors: [],
16+
stmts: [
17+
RawStmt {
18+
stmt: TruncateStmt(
19+
TruncateStmt {
20+
relations: [
21+
Node {
22+
node: Some(
23+
RangeVar(
24+
RangeVar {
25+
catalogname: "",
26+
schemaname: "",
27+
relname: "users",
28+
inh: true,
29+
relpersistence: "p",
30+
alias: None,
31+
location: 9,
32+
},
33+
),
34+
),
35+
},
36+
],
37+
restart_seqs: false,
38+
behavior: DropRestrict,
39+
},
40+
),
41+
range: 0..14,
42+
},
43+
],
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
source: crates/parser/tests/statement_parser_test.rs
3+
assertion_line: 62
4+
description: TRUNCATE accounts CASCADE;
5+
---
6+
Parse {
7+
cst: SourceFile@0..26
8+
TruncateStmt@0..26
9+
Truncate@0..8 "TRUNCATE"
10+
Whitespace@8..9 " "
11+
RangeVar@9..17
12+
Ident@9..17 "accounts"
13+
Whitespace@17..18 " "
14+
Cascade@18..25 "CASCADE"
15+
Ascii59@25..26 ";"
16+
,
17+
errors: [],
18+
stmts: [
19+
RawStmt {
20+
stmt: TruncateStmt(
21+
TruncateStmt {
22+
relations: [
23+
Node {
24+
node: Some(
25+
RangeVar(
26+
RangeVar {
27+
catalogname: "",
28+
schemaname: "",
29+
relname: "accounts",
30+
inh: true,
31+
relpersistence: "p",
32+
alias: None,
33+
location: 9,
34+
},
35+
),
36+
),
37+
},
38+
],
39+
restart_seqs: false,
40+
behavior: DropCascade,
41+
},
42+
),
43+
range: 0..25,
44+
},
45+
],
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
source: crates/parser/tests/statement_parser_test.rs
3+
assertion_line: 62
4+
description: TRUNCATE accounts RESTRICT;
5+
---
6+
Parse {
7+
cst: SourceFile@0..27
8+
TruncateStmt@0..27
9+
Truncate@0..8 "TRUNCATE"
10+
Whitespace@8..9 " "
11+
RangeVar@9..17
12+
Ident@9..17 "accounts"
13+
Whitespace@17..18 " "
14+
Restrict@18..26 "RESTRICT"
15+
Ascii59@26..27 ";"
16+
,
17+
errors: [],
18+
stmts: [
19+
RawStmt {
20+
stmt: TruncateStmt(
21+
TruncateStmt {
22+
relations: [
23+
Node {
24+
node: Some(
25+
RangeVar(
26+
RangeVar {
27+
catalogname: "",
28+
schemaname: "",
29+
relname: "accounts",
30+
inh: true,
31+
relpersistence: "p",
32+
alias: None,
33+
location: 9,
34+
},
35+
),
36+
),
37+
},
38+
],
39+
restart_seqs: false,
40+
behavior: DropRestrict,
41+
},
42+
),
43+
range: 0..26,
44+
},
45+
],
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
source: crates/parser/tests/statement_parser_test.rs
3+
assertion_line: 62
4+
description: TRUNCATE TABLE users;
5+
---
6+
Parse {
7+
cst: SourceFile@0..21
8+
TruncateStmt@0..21
9+
Truncate@0..8 "TRUNCATE"
10+
Whitespace@8..9 " "
11+
Table@9..14 "TABLE"
12+
Whitespace@14..15 " "
13+
RangeVar@15..20
14+
Ident@15..20 "users"
15+
Ascii59@20..21 ";"
16+
,
17+
errors: [],
18+
stmts: [
19+
RawStmt {
20+
stmt: TruncateStmt(
21+
TruncateStmt {
22+
relations: [
23+
Node {
24+
node: Some(
25+
RangeVar(
26+
RangeVar {
27+
catalogname: "",
28+
schemaname: "",
29+
relname: "users",
30+
inh: true,
31+
relpersistence: "p",
32+
alias: None,
33+
location: 15,
34+
},
35+
),
36+
),
37+
},
38+
],
39+
restart_seqs: false,
40+
behavior: DropRestrict,
41+
},
42+
),
43+
range: 0..20,
44+
},
45+
],
46+
}

0 commit comments

Comments
 (0)