Skip to content

Commit 92a8f2b

Browse files
authored
Merge pull request #106 from cvng/feat/type-name
feat: add support for type name
2 parents 8c3e3d3 + 3aba5fb commit 92a8f2b

File tree

3 files changed

+1141
-50
lines changed

3 files changed

+1141
-50
lines changed

crates/codegen/src/get_node_properties.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,51 @@ fn custom_handlers(node: &Node) -> TokenStream {
869869
tokens.push(TokenProperty::from(Token::Function));
870870
}
871871
},
872+
"TypeName" => quote! {
873+
let names = n.names
874+
.iter()
875+
.filter_map(|n| if let Some(NodeEnum::String(s)) = &n.node { Some(s.sval.clone()) } else { None })
876+
.collect::<Vec<_>>();
877+
878+
if names.len() == 2 && names[0] == "pg_catalog" {
879+
match names[1].as_str() {
880+
"interval" => {
881+
// Adapted from https://github.com/postgres/postgres/blob/REL_15_STABLE/src/backend/utils/adt/timestamp.c#L1103
882+
const HOUR: i32 = 10;
883+
const MINUTE: i32 = 11;
884+
const SECOND: i32 = 12;
885+
886+
let fields = &n.typmods.first()
887+
.and_then(|node| node.node.as_ref())
888+
.and_then(|node| if let NodeEnum::AConst(n) = node { n.val.clone() } else { None })
889+
.and_then(|node| if let protobuf::a_const::Val::Ival(n) = node { Some(n.ival) } else { None });
890+
891+
if let Some(fields) = fields {
892+
match fields.clone() {
893+
i if i == 1 << HOUR | 1 << MINUTE | 1 << SECOND => {
894+
tokens.push(TokenProperty::from(Token::To));
895+
tokens.push(TokenProperty::from(Token::SecondP));
896+
},
897+
_ => panic!("Unknown Interval fields {:#?}", fields),
898+
}
899+
}
900+
},
901+
"timestamptz" => {
902+
tokens.push(TokenProperty::from(Token::Timestamp));
903+
tokens.push(TokenProperty::from(Token::With));
904+
tokens.push(TokenProperty::from(Token::Time));
905+
tokens.push(TokenProperty::from(Token::Zone));
906+
}
907+
"timetz" => {
908+
tokens.push(TokenProperty::from(Token::Time));
909+
tokens.push(TokenProperty::from(Token::With));
910+
tokens.push(TokenProperty::from(Token::Time));
911+
tokens.push(TokenProperty::from(Token::Zone));
912+
}
913+
_ => {}
914+
}
915+
}
916+
},
872917
_ => quote! {},
873918
}
874919
}

crates/parser/tests/data/statements/valid/0043.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
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;
2+
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));
33
CREATE TABLE types (a real, b double precision, c numeric(2, 3), d char(4), e char(5), f varchar(6), g varchar(7));
44
CREATE TABLE types (a geometry(point) NOT NULL);
55
CREATE TABLE tablename (colname int NOT NULL DEFAULT nextval('tablename_colname_seq'));

0 commit comments

Comments
 (0)