@@ -535,3 +535,149 @@ impl From<Revoke> for crate::ast::Statement {
535535 crate :: ast:: Statement :: Revoke ( v)
536536 }
537537}
538+
539+ /// `ALTER DEFAULT PRIVILEGES`, which applies to objects created later rather
540+ /// than to existing ones.
541+ ///
542+ /// [PostgreSQL](https://www.postgresql.org/docs/current/sql-alterdefaultprivileges.html)
543+ #[ derive( Debug , Clone , PartialEq , PartialOrd , Eq , Ord , Hash ) ]
544+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
545+ #[ cfg_attr( feature = "visitor" , derive( Visit , VisitMut ) ) ]
546+ pub struct AlterDefaultPrivileges {
547+ /// `FOR ROLE <role> [, ...]`, empty when the clause is absent.
548+ pub for_roles : Vec < Ident > ,
549+ /// `IN SCHEMA <schema> [, ...]`, empty when the clause is absent.
550+ pub in_schemas : Vec < ObjectName > ,
551+ /// The abbreviated `GRANT` or `REVOKE` that follows.
552+ pub operation : AlterDefaultPrivilegesOperation ,
553+ }
554+
555+ impl fmt:: Display for AlterDefaultPrivileges {
556+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
557+ f. write_str ( "ALTER DEFAULT PRIVILEGES" ) ?;
558+ if !self . for_roles . is_empty ( ) {
559+ write ! ( f, " FOR ROLE {}" , display_comma_separated( & self . for_roles) ) ?;
560+ }
561+ if !self . in_schemas . is_empty ( ) {
562+ write ! (
563+ f,
564+ " IN SCHEMA {}" ,
565+ display_comma_separated( & self . in_schemas)
566+ ) ?;
567+ }
568+ write ! ( f, " {}" , self . operation)
569+ }
570+ }
571+
572+ impl From < AlterDefaultPrivileges > for crate :: ast:: Statement {
573+ fn from ( v : AlterDefaultPrivileges ) -> Self {
574+ crate :: ast:: Statement :: AlterDefaultPrivileges ( v)
575+ }
576+ }
577+
578+ /// The abbreviated `GRANT` or `REVOKE` of an [`AlterDefaultPrivileges`], naming
579+ /// an object type rather than named objects and so unable to reuse [`Grant`].
580+ #[ derive( Debug , Clone , PartialEq , PartialOrd , Eq , Ord , Hash ) ]
581+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
582+ #[ cfg_attr( feature = "visitor" , derive( Visit , VisitMut ) ) ]
583+ pub enum AlterDefaultPrivilegesOperation {
584+ /// `GRANT <privileges> ON <object_type> TO <grantees> [WITH GRANT OPTION]`
585+ Grant {
586+ /// The privileges being granted.
587+ privileges : Privileges ,
588+ /// The kind of future object the privileges apply to.
589+ object_type : DefaultPrivilegesObjectType ,
590+ /// The roles receiving the privileges.
591+ grantees : Vec < Grantee > ,
592+ /// Whether `WITH GRANT OPTION` is present.
593+ with_grant_option : bool ,
594+ } ,
595+ /// `REVOKE [GRANT OPTION FOR] <privileges> ON <object_type> FROM <grantees> [CASCADE | RESTRICT]`
596+ Revoke {
597+ /// Whether `GRANT OPTION FOR` is present.
598+ grant_option_for : bool ,
599+ /// The privileges being revoked.
600+ privileges : Privileges ,
601+ /// The kind of future object the privileges apply to.
602+ object_type : DefaultPrivilegesObjectType ,
603+ /// The roles losing the privileges.
604+ grantees : Vec < Grantee > ,
605+ /// Optional `CASCADE`/`RESTRICT` behaviour.
606+ cascade : Option < CascadeOption > ,
607+ } ,
608+ }
609+
610+ impl fmt:: Display for AlterDefaultPrivilegesOperation {
611+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
612+ match self {
613+ AlterDefaultPrivilegesOperation :: Grant {
614+ privileges,
615+ object_type,
616+ grantees,
617+ with_grant_option,
618+ } => {
619+ write ! (
620+ f,
621+ "GRANT {privileges} ON {object_type} TO {}" ,
622+ display_comma_separated( grantees)
623+ ) ?;
624+ if * with_grant_option {
625+ f. write_str ( " WITH GRANT OPTION" ) ?;
626+ }
627+ }
628+ AlterDefaultPrivilegesOperation :: Revoke {
629+ grant_option_for,
630+ privileges,
631+ object_type,
632+ grantees,
633+ cascade,
634+ } => {
635+ f. write_str ( "REVOKE " ) ?;
636+ if * grant_option_for {
637+ f. write_str ( "GRANT OPTION FOR " ) ?;
638+ }
639+ write ! (
640+ f,
641+ "{privileges} ON {object_type} FROM {}" ,
642+ display_comma_separated( grantees)
643+ ) ?;
644+ if let Some ( cascade) = cascade {
645+ write ! ( f, " {cascade}" ) ?;
646+ }
647+ }
648+ }
649+ Ok ( ( ) )
650+ }
651+ }
652+
653+ /// The kind of future object an [`AlterDefaultPrivileges`] applies to.
654+ #[ derive( Debug , Clone , Copy , PartialEq , PartialOrd , Eq , Ord , Hash ) ]
655+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
656+ #[ cfg_attr( feature = "visitor" , derive( Visit , VisitMut ) ) ]
657+ pub enum DefaultPrivilegesObjectType {
658+ /// `TABLES`, which also covers views and foreign tables.
659+ Tables ,
660+ /// `SEQUENCES`
661+ Sequences ,
662+ /// `FUNCTIONS`
663+ Functions ,
664+ /// `ROUTINES`, a synonym of `FUNCTIONS`.
665+ Routines ,
666+ /// `TYPES`
667+ Types ,
668+ /// `SCHEMAS`
669+ Schemas ,
670+ }
671+
672+ impl fmt:: Display for DefaultPrivilegesObjectType {
673+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
674+ f. write_str ( match self {
675+ DefaultPrivilegesObjectType :: Tables => "TABLES" ,
676+ DefaultPrivilegesObjectType :: Sequences => "SEQUENCES" ,
677+ DefaultPrivilegesObjectType :: Functions => "FUNCTIONS" ,
678+ DefaultPrivilegesObjectType :: Routines => "ROUTINES" ,
679+ DefaultPrivilegesObjectType :: Types => "TYPES" ,
680+ DefaultPrivilegesObjectType :: Schemas => "SCHEMAS" ,
681+ } )
682+ }
683+ }
0 commit comments