Skip to content

Commit

Permalink
Merge pull request #106 from cvng/feat/type-name
Browse files Browse the repository at this point in the history
feat: add support for type name
  • Loading branch information
psteinroe authored Jan 8, 2024
2 parents 8c3e3d3 + 3aba5fb commit 92a8f2b
Show file tree
Hide file tree
Showing 3 changed files with 1,141 additions and 50 deletions.
45 changes: 45 additions & 0 deletions crates/codegen/src/get_node_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,51 @@ fn custom_handlers(node: &Node) -> TokenStream {
tokens.push(TokenProperty::from(Token::Function));
}
},
"TypeName" => quote! {
let names = n.names
.iter()
.filter_map(|n| if let Some(NodeEnum::String(s)) = &n.node { Some(s.sval.clone()) } else { None })
.collect::<Vec<_>>();

if names.len() == 2 && names[0] == "pg_catalog" {
match names[1].as_str() {
"interval" => {
// Adapted from https://github.com/postgres/postgres/blob/REL_15_STABLE/src/backend/utils/adt/timestamp.c#L1103
const HOUR: i32 = 10;
const MINUTE: i32 = 11;
const SECOND: i32 = 12;

let fields = &n.typmods.first()
.and_then(|node| node.node.as_ref())
.and_then(|node| if let NodeEnum::AConst(n) = node { n.val.clone() } else { None })
.and_then(|node| if let protobuf::a_const::Val::Ival(n) = node { Some(n.ival) } else { None });

if let Some(fields) = fields {
match fields.clone() {
i if i == 1 << HOUR | 1 << MINUTE | 1 << SECOND => {
tokens.push(TokenProperty::from(Token::To));
tokens.push(TokenProperty::from(Token::SecondP));
},
_ => panic!("Unknown Interval fields {:#?}", fields),
}
}
},
"timestamptz" => {
tokens.push(TokenProperty::from(Token::Timestamp));
tokens.push(TokenProperty::from(Token::With));
tokens.push(TokenProperty::from(Token::Time));
tokens.push(TokenProperty::from(Token::Zone));
}
"timetz" => {
tokens.push(TokenProperty::from(Token::Time));
tokens.push(TokenProperty::from(Token::With));
tokens.push(TokenProperty::from(Token::Time));
tokens.push(TokenProperty::from(Token::Zone));
}
_ => {}
}
}
},
_ => quote! {},
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/parser/tests/data/statements/valid/0043.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CREATE UNLOGGED TABLE cities (name text, population real, altitude double, identifier smallint, postal_code int, foreign_id bigint);
/* 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;
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));
CREATE TABLE types (a real, b double precision, c numeric(2, 3), d char(4), e char(5), f varchar(6), g varchar(7));
CREATE TABLE types (a geometry(point) NOT NULL);
CREATE TABLE tablename (colname int NOT NULL DEFAULT nextval('tablename_colname_seq'));
Expand Down
Loading

0 comments on commit 92a8f2b

Please sign in to comment.