Skip to content

fix: location and add support for create publication #94

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

Merged
merged 1 commit into from
Dec 22, 2023
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
18 changes: 18 additions & 0 deletions crates/codegen/src/get_location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,22 @@ pub fn get_location_mod(proto_file: &ProtoFile) -> proc_macro2::TokenStream {
} else {
Some(n.location)
},
NodeEnum::PublicationObjSpec(n) => {
match &n.pubtable {
Some(pubtable) => match &pubtable.relation {
Some(range_var) => Some(range_var.location),
None => Some(n.location),
},
None => Some(n.location),
}
},
NodeEnum::BooleanTest(n) => {
if n.arg.is_some() {
get_location_internal(&n.arg.as_ref().unwrap().node.as_ref().unwrap())
} else {
Some(n.location)
}
},
Comment on lines +58 to +73
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@cvng fixed location here

#(NodeEnum::#node_identifiers(n) => #location_idents),*
};
if location.is_some() && location.unwrap() < 0 {
Expand All @@ -74,6 +90,8 @@ fn manual_node_names() -> Vec<&'static str> {
"CollateClause",
"TypeCast",
"ColumnDef",
"NullTest",
"PublicationObjSpec",
]
}

Expand Down
68 changes: 68 additions & 0 deletions crates/codegen/src/get_node_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,74 @@ fn custom_handlers(node: &Node) -> TokenStream {
tokens.push(TokenProperty::from(Token::With));
}
},
"CreatePublicationStmt" => quote! {
tokens.push(TokenProperty::from(Token::Create));
tokens.push(TokenProperty::from(Token::Publication));
if n.for_all_tables {
tokens.push(TokenProperty::from(Token::For));
tokens.push(TokenProperty::from(Token::All));
tokens.push(TokenProperty::from(Token::Tables));
}
if let Some(n) = n.options.first() {
tokens.push(TokenProperty::from(Token::With));
}
if let Some(n) = n.pubobjects.first() {
tokens.push(TokenProperty::from(Token::For));
if let Some(NodeEnum::PublicationObjSpec(n)) = &n.node {
match n.pubobjtype() {
protobuf::PublicationObjSpecType::PublicationobjTable => {
tokens.push(TokenProperty::from(Token::Table));
},
protobuf::PublicationObjSpecType::PublicationobjTablesInSchema => {
tokens.push(TokenProperty::from(Token::Tables));
tokens.push(TokenProperty::from(Token::InP));
tokens.push(TokenProperty::from(Token::Schema));
},
_ => panic!("Unknown CreatePublicationStmt {:#?}", n.pubobjtype())
}
}
}
if let Some(n) = n.pubobjects.last() {
if let Some(NodeEnum::PublicationObjSpec(n)) = &n.node {
match n.pubobjtype() {
protobuf::PublicationObjSpecType::PublicationobjTablesInSchema => {
tokens.push(TokenProperty::from(Token::Tables));
tokens.push(TokenProperty::from(Token::InP));
tokens.push(TokenProperty::from(Token::Schema));
},
_ => {}
}
}
Comment on lines +706 to +732
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think this can be implemented safer. we should do .find() for all types and add the respective tokens if at least one can be found. feel free to fix this @cvng

Copy link
Contributor

Choose a reason for hiding this comment

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

sure, let's have this in a next PR with a test_create_publication test

}
},
"PublicationTable" => quote! {
if n.where_clause.is_some() {
tokens.push(TokenProperty::from(Token::Where));
}
},
"BooleanTest" => quote! {
match n.booltesttype() {
protobuf::BoolTestType::IsTrue => {
tokens.push(TokenProperty::from(Token::Is));
tokens.push(TokenProperty::from(Token::TrueP));
},
protobuf::BoolTestType::IsNotTrue => {
tokens.push(TokenProperty::from(Token::Is));
tokens.push(TokenProperty::from(Token::Not));
tokens.push(TokenProperty::from(Token::TrueP));
},
protobuf::BoolTestType::IsFalse => {
tokens.push(TokenProperty::from(Token::Is));
tokens.push(TokenProperty::from(Token::FalseP));
},
protobuf::BoolTestType::IsNotFalse => {
tokens.push(TokenProperty::from(Token::Is));
tokens.push(TokenProperty::from(Token::Not));
tokens.push(TokenProperty::from(Token::FalseP));
},
_ => panic!("Unknown BooleanTest {:#?}", n.booltesttype()),
}
},
"CompositeTypeStmt" => quote! {
tokens.push(TokenProperty::from(Token::Create));
tokens.push(TokenProperty::from(Token::TypeP));
Expand Down
7 changes: 7 additions & 0 deletions crates/parser/tests/data/statements/valid/0053.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE PUBLICATION mypublication FOR TABLE users, departments;
CREATE PUBLICATION active_departments FOR TABLE departments WHERE (active IS TRUE);
CREATE PUBLICATION alltables FOR ALL TABLES;
CREATE PUBLICATION insert_only FOR TABLE mydata WITH (publish = 'insert');
CREATE PUBLICATION production_publication FOR TABLE users, departments, TABLES IN SCHEMA production;
CREATE PUBLICATION sales_publication FOR TABLES IN SCHEMA marketing, sales;
CREATE PUBLICATION users_filtered FOR TABLE users (user_id, firstname);
59 changes: 29 additions & 30 deletions crates/parser/tests/snapshots/statements/valid/[email protected]
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
---
source: crates/parser/tests/statement_parser_test.rs
description: "\nCREATE EXTENSION IF NOT EXISTS x CASCADE VERSION \"1.2\" SCHEMA a;"
description: "CREATE EXTENSION IF NOT EXISTS x CASCADE VERSION \"1.2\" SCHEMA a;"
---
Parse {
cst: [email protected]
[email protected] "\n"
[email protected]
[email protected] "CREATE"
[email protected] " "
[email protected] "EXTENSION"
[email protected] " "
[email protected] "IF"
[email protected] " "
[email protected] "NOT"
[email protected] " "
[email protected] "EXISTS"
[email protected] " "
[email protected] "x"
[email protected] " "
[email protected]
[email protected] "CASCADE"
[email protected] " "
[email protected]
[email protected] "VERSION"
[email protected] " "
[email protected] "\"1.2\""
[email protected] " "
[email protected]
[email protected] "SCHEMA"
[email protected] " "
[email protected] "a"
[email protected] ";"
cst: [email protected]
[email protected]
[email protected] "CREATE"
[email protected] " "
[email protected] "EXTENSION"
[email protected] " "
[email protected] "IF"
[email protected] " "
[email protected] "NOT"
[email protected] " "
[email protected] "EXISTS"
[email protected] " "
[email protected] "x"
[email protected] " "
[email protected]
[email protected] "CASCADE"
[email protected] " "
[email protected]
[email protected] "VERSION"
[email protected] " "
[email protected] "\"1.2\""
[email protected] " "
[email protected]
[email protected] "SCHEMA"
[email protected] " "
[email protected] "a"
[email protected] ";"
,
errors: [],
stmts: [
Expand Down Expand Up @@ -112,7 +111,7 @@ Parse {
],
},
),
range: 0..64,
range: 0..63,
},
],
}
101 changes: 101 additions & 0 deletions crates/parser/tests/snapshots/statements/valid/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
---
source: crates/parser/tests/statement_parser_test.rs
description: "CREATE PUBLICATION mypublication FOR TABLE users, departments;"
---
Parse {
cst: [email protected]
[email protected]
[email protected] "CREATE"
[email protected] " "
[email protected] "PUBLICATION"
[email protected] " "
[email protected] "mypublication"
[email protected] " "
[email protected] "FOR"
[email protected] " "
[email protected] "TABLE"
[email protected] " "
[email protected]
[email protected]
[email protected]
[email protected] "users"
[email protected] ","
[email protected] " "
[email protected]
[email protected]
[email protected]
[email protected] "departments"
[email protected] ";"
,
errors: [],
stmts: [
RawStmt {
stmt: CreatePublicationStmt(
CreatePublicationStmt {
pubname: "mypublication",
options: [],
pubobjects: [
Node {
node: Some(
PublicationObjSpec(
PublicationObjSpec {
pubobjtype: PublicationobjTable,
name: "",
pubtable: Some(
PublicationTable {
relation: Some(
RangeVar {
catalogname: "",
schemaname: "",
relname: "users",
inh: true,
relpersistence: "p",
alias: None,
location: 43,
},
),
where_clause: None,
columns: [],
},
),
location: 0,
},
),
),
},
Node {
node: Some(
PublicationObjSpec(
PublicationObjSpec {
pubobjtype: PublicationobjTable,
name: "",
pubtable: Some(
PublicationTable {
relation: Some(
RangeVar {
catalogname: "",
schemaname: "",
relname: "departments",
inh: true,
relpersistence: "p",
alias: None,
location: 50,
},
),
where_clause: None,
columns: [],
},
),
location: 50,
},
),
),
},
],
for_all_tables: false,
},
),
range: 0..61,
},
],
}
Loading