Skip to content

Commit fbaf2bf

Browse files
committed
feat: add support for create table like
1 parent 9c694d7 commit fbaf2bf

File tree

13 files changed

+1725
-0
lines changed

13 files changed

+1725
-0
lines changed

crates/codegen/src/get_node_properties.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,11 @@ fn custom_handlers(node: &Node) -> TokenStream {
365365
tokens.push(TokenProperty::from(Token::Key));
366366
},
367367
protobuf::ConstrType::ConstrForeign => tokens.push(TokenProperty::from(Token::References)),
368+
protobuf::ConstrType::ConstrUnique => tokens.push(TokenProperty::from(Token::Unique)),
368369
_ => panic!("Unknown Constraint {:#?}", n.contype()),
370+
};
371+
if n.options.len() > 0 {
372+
tokens.push(TokenProperty::from(Token::With));
369373
}
370374
},
371375
"PartitionSpec" => quote! {
@@ -439,6 +443,29 @@ fn custom_handlers(node: &Node) -> TokenStream {
439443
tokens.push(TokenProperty::from(Token::For));
440444
tokens.push(TokenProperty::from(Token::Values));
441445
}
446+
if let Some(n) = &n.relation {
447+
match n.relpersistence.as_str() {
448+
// Unlogged
449+
"u" => tokens.push(TokenProperty::from(Token::Unlogged)),
450+
// Temporary
451+
"t" => tokens.push(TokenProperty::from(Token::Temporary)),
452+
_ => {},
453+
}
454+
if n.inh {
455+
tokens.push(TokenProperty::from(Token::Inherits));
456+
}
457+
}
458+
},
459+
"TableLikeClause" => quote! {
460+
tokens.push(TokenProperty::from(Token::Like));
461+
// CREATE_TABLE_LIKE_ALL
462+
if n.options == 0x7FFFFFFF {
463+
tokens.push(TokenProperty::from(Token::Including));
464+
tokens.push(TokenProperty::from(Token::All));
465+
} else {
466+
tokens.push(TokenProperty::from(Token::Excluding));
467+
tokens.push(TokenProperty::from(Token::All));
468+
}
442469
},
443470
"PartitionBoundSpec" => quote! {
444471
tokens.push(TokenProperty::from(Token::From));
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CREATE UNLOGGED TABLE cities (name text, population real, altitude double, identifier smallint, postal_code int, foreign_id bigint);
2+
/* TODO: CREATE TABLE IF NOT EXISTS distributors (name varchar(40) DEFAULT 'Luso Films', len interval hour to second(3), name varchar(40) DEFAULT 'Luso Films', did int DEFAULT nextval('distributors_serial'), stamp timestamp DEFAULT now() NOT NULL, stamptz timestamp with time zone, "time" time NOT NULL, timetz time with time zone, CONSTRAINT name_len PRIMARY KEY (name, len)); */ SELECT 1;
3+
/* TODO: CREATE TABLE types (a real, b double precision, c numeric(2, 3), d char(4), e char(5), f varchar(6), g varchar(7)); */ SELECT 1;
4+
/* TODO: CREATE TABLE types (a geometry(point) NOT NULL); */ SELECT 1;
5+
CREATE TABLE tablename (colname int NOT NULL DEFAULT nextval('tablename_colname_seq'));
6+
CREATE TABLE capitals (state char(2)) INHERITS (cities);
7+
/* TODO: CREATE TEMPORARY TABLE temp AS SELECT c FROM t; */ SELECT 1;
8+
/* TODO: CREATE TABLE films2 AS SELECT * FROM films; */ SELECT 1;
9+
/* TODO: CREATE TEMPORARY TABLE films_recent ON COMMIT DROP AS SELECT * FROM films WHERE date_prod > $1; */ SELECT 1;
10+
CREATE TABLE like_constraint_rename_cache (LIKE constraint_rename_cache INCLUDING ALL);
11+
CREATE TABLE distributors (did int, name varchar(40), UNIQUE (name) WITH (fillfactor=70)) WITH (fillfactor=70);

crates/parser/tests/snapshots/statements/valid/0043@1.snap

Lines changed: 412 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
---
2+
source: crates/parser/tests/statement_parser_test.rs
3+
description: CREATE TABLE like_constraint_rename_cache (LIKE constraint_rename_cache INCLUDING ALL);
4+
---
5+
Parse {
6+
cst: SourceFile@0..87
7+
CreateStmt@0..87
8+
Create@0..6 "CREATE"
9+
Whitespace@6..7 " "
10+
Table@7..12 "TABLE"
11+
Whitespace@12..13 " "
12+
RangeVar@13..41
13+
Ident@13..41 "like_constraint_renam ..."
14+
Whitespace@41..42 " "
15+
Ascii40@42..43 "("
16+
TableLikeClause@43..85
17+
Like@43..47 "LIKE"
18+
Whitespace@47..48 " "
19+
RangeVar@48..71
20+
Ident@48..71 "constraint_rename_cache"
21+
Whitespace@71..72 " "
22+
Including@72..81 "INCLUDING"
23+
Whitespace@81..82 " "
24+
All@82..85 "ALL"
25+
Ascii41@85..86 ")"
26+
Ascii59@86..87 ";"
27+
,
28+
errors: [],
29+
stmts: [
30+
RawStmt {
31+
stmt: CreateStmt(
32+
CreateStmt {
33+
relation: Some(
34+
RangeVar {
35+
catalogname: "",
36+
schemaname: "",
37+
relname: "like_constraint_rename_cache",
38+
inh: true,
39+
relpersistence: "p",
40+
alias: None,
41+
location: 13,
42+
},
43+
),
44+
table_elts: [
45+
Node {
46+
node: Some(
47+
TableLikeClause(
48+
TableLikeClause {
49+
relation: Some(
50+
RangeVar {
51+
catalogname: "",
52+
schemaname: "",
53+
relname: "constraint_rename_cache",
54+
inh: true,
55+
relpersistence: "p",
56+
alias: None,
57+
location: 48,
58+
},
59+
),
60+
options: 2147483647,
61+
relation_oid: 0,
62+
},
63+
),
64+
),
65+
},
66+
],
67+
inh_relations: [],
68+
partbound: None,
69+
partspec: None,
70+
of_typename: None,
71+
constraints: [],
72+
options: [],
73+
oncommit: OncommitNoop,
74+
tablespacename: "",
75+
access_method: "",
76+
if_not_exists: false,
77+
},
78+
),
79+
range: 0..86,
80+
},
81+
],
82+
}

0 commit comments

Comments
 (0)