Skip to content
Open
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
34 changes: 34 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7779,6 +7779,16 @@ pub enum GrantObjects {
/// The target schema names.
schemas: Vec<ObjectName>,
},
/// Grant privileges on `ALL PROCEDURES IN SCHEMA <schema_name> [, ...]`
AllProceduresInSchema {
/// The target schema names.
schemas: Vec<ObjectName>,
},
/// Grant privileges on `ALL ROUTINES IN SCHEMA <schema_name> [, ...]`
AllRoutinesInSchema {
/// The target schema names.
schemas: Vec<ObjectName>,
},
/// Grant privileges on `FUTURE SCHEMAS IN DATABASE <database_name> [, ...]`
FutureSchemasInDatabase {
/// The target database names.
Expand Down Expand Up @@ -7847,6 +7857,13 @@ pub enum GrantObjects {
/// For example:
/// `GRANT EXECUTE ON FUNCTION foo(p_id uuid), bar() TO role1`
Functions(Vec<FunctionDesc>),
/// Grant privileges on routines, which name functions and procedures alike.
///
/// For example:
/// `GRANT EXECUTE ON ROUTINE foo(p_id uuid), bar() TO role1`
///
/// [PostgreSQL](https://www.postgresql.org/docs/current/sql-grant.html)
Routines(Vec<FunctionDesc>),
}

impl fmt::Display for GrantObjects {
Expand Down Expand Up @@ -7915,6 +7932,20 @@ impl fmt::Display for GrantObjects {
display_comma_separated(schemas)
)
}
GrantObjects::AllProceduresInSchema { schemas } => {
write!(
f,
"ALL PROCEDURES IN SCHEMA {}",
display_comma_separated(schemas)
)
}
GrantObjects::AllRoutinesInSchema { schemas } => {
write!(
f,
"ALL ROUTINES IN SCHEMA {}",
display_comma_separated(schemas)
)
}
GrantObjects::FutureSchemasInDatabase { databases } => {
write!(
f,
Expand Down Expand Up @@ -7984,6 +8015,9 @@ impl fmt::Display for GrantObjects {
GrantObjects::Functions(functions) => {
write!(f, "FUNCTION {}", display_comma_separated(functions))
}
GrantObjects::Routines(routines) => {
write!(f, "ROUTINE {}", display_comma_separated(routines))
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,7 @@ define_keywords!(
PRIVILEGES,
PROC,
PROCEDURE,
PROCEDURES,
PROCESSLIST,
PROFILE,
PROGRAM,
Expand Down Expand Up @@ -934,6 +935,8 @@ define_keywords!(
ROLLBACK,
ROLLUP,
ROOT,
ROUTINE,
ROUTINES,
ROW,
ROWGROUPSIZE,
ROWID,
Expand Down
22 changes: 22 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18687,6 +18687,24 @@ impl<'a> Parser<'a> {
Some(GrantObjects::AllFunctionsInSchema {
schemas: self.parse_comma_separated(|p| p.parse_object_name(false))?,
})
} else if self.parse_keywords(&[
Keyword::ALL,
Keyword::PROCEDURES,
Keyword::IN,
Keyword::SCHEMA,
]) {
Some(GrantObjects::AllProceduresInSchema {
schemas: self.parse_comma_separated(|p| p.parse_object_name(false))?,
})
} else if self.parse_keywords(&[
Keyword::ALL,
Keyword::ROUTINES,
Keyword::IN,
Keyword::SCHEMA,
]) {
Some(GrantObjects::AllRoutinesInSchema {
schemas: self.parse_comma_separated(|p| p.parse_object_name(false))?,
})
} else if self.parse_keywords(&[
Keyword::FUTURE,
Keyword::SCHEMAS,
Expand Down Expand Up @@ -18780,6 +18798,10 @@ impl<'a> Parser<'a> {
Some(GrantObjects::Procedures(
self.parse_comma_separated(Parser::parse_function_desc)?,
))
} else if self.parse_keyword(Keyword::ROUTINE) {
Some(GrantObjects::Routines(
self.parse_comma_separated(Parser::parse_function_desc)?,
))
} else {
let object_type = self.parse_one_of_keywords(&[
Keyword::SEQUENCE,
Expand Down
54 changes: 54 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22744,3 +22744,57 @@ fn display_create_procedure_parameter_list_propagates_write_errors() {
let mut writer = RejectingWriter(" (");
assert_eq!(write!(writer, "{statement}"), Err(fmt::Error));
}

#[test]
fn parse_grant_routines() {
let objects = |sql: &str| match verified_stmt(sql) {
Statement::Grant(grant) => grant.objects,
Statement::Revoke(revoke) => revoke.objects,
stmt => panic!("unexpected statement: {stmt:?}"),
};
let name = |name: &str| ObjectName::from(vec![Ident::new(name)]);

assert_eq!(
objects("GRANT EXECUTE ON ROUTINE f(INT) TO bob"),
Some(GrantObjects::Routines(vec![FunctionDesc {
name: name("f"),
args: Some(vec![OperateFunctionArg::unnamed(DataType::Int(None))]),
}]))
);
assert_eq!(
objects("REVOKE ALL ON ROUTINE f, g() FROM bob"),
Some(GrantObjects::Routines(vec![
FunctionDesc {
name: name("f"),
args: None,
},
FunctionDesc {
name: name("g"),
args: Some(vec![]),
},
]))
);
assert_eq!(
objects("GRANT EXECUTE ON ALL PROCEDURES IN SCHEMA s TO bob"),
Some(GrantObjects::AllProceduresInSchema {
schemas: vec![name("s")],
})
);
assert_eq!(
objects("REVOKE EXECUTE ON ALL ROUTINES IN SCHEMA s, t FROM bob"),
Some(GrantObjects::AllRoutinesInSchema {
schemas: vec![name("s"), name("t")],
})
);

verified_stmt("REVOKE ALL ON ROUTINE public.f(p_id UUID), g() FROM bob");
verified_stmt("REVOKE EXECUTE ON ALL PROCEDURES IN SCHEMA s, t FROM bob");
verified_stmt("GRANT EXECUTE ON ALL ROUTINES IN SCHEMA s TO bob");

assert_eq!(
all_dialects()
.parse_sql_statements("GRANT EXECUTE ON ROUTINE f(INT")
.unwrap_err(),
ParserError::ParserError("Expected: ), found: EOF".to_string())
);
}
Loading