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