Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for type name #106

Merged
merged 3 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: add other fields from intervaltypmodout

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