github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/sql/sem/tree/alter_default_privileges.go (about) 1 // Copyright 2021 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package tree 12 13 import "github.com/cockroachdb/cockroachdb-parser/pkg/sql/privilege" 14 15 // AlterDefaultPrivileges represents an ALTER DEFAULT PRIVILEGES statement. 16 type AlterDefaultPrivileges struct { 17 Roles RoleSpecList 18 // True if `ALTER DEFAULT PRIVILEGES FOR ALL ROLES` is executed. 19 ForAllRoles bool 20 // If Schema is not specified, ALTER DEFAULT PRIVILEGES is being 21 // run on the current database. 22 Schemas ObjectNamePrefixList 23 24 // Database is only used when converting a granting / revoking incompatible 25 // database privileges to an alter default privileges statement. 26 // If it is not set, the current database is used. 27 Database *Name 28 29 // Only one of Grant or Revoke should be set. IsGrant is used to determine 30 // which one is set. 31 IsGrant bool 32 Grant AbbreviatedGrant 33 Revoke AbbreviatedRevoke 34 } 35 36 // Format implements the NodeFormatter interface. 37 func (n *AlterDefaultPrivileges) Format(ctx *FmtCtx) { 38 ctx.WriteString("ALTER DEFAULT PRIVILEGES ") 39 if n.ForAllRoles { 40 ctx.WriteString("FOR ALL ROLES ") 41 } else if len(n.Roles) > 0 { 42 ctx.WriteString("FOR ROLE ") 43 for i := range n.Roles { 44 if i > 0 { 45 ctx.WriteString(", ") 46 } 47 ctx.FormatNode(&n.Roles[i]) 48 } 49 ctx.WriteString(" ") 50 } 51 if len(n.Schemas) > 0 { 52 ctx.WriteString("IN SCHEMA ") 53 ctx.FormatNode(n.Schemas) 54 ctx.WriteString(" ") 55 } 56 if n.IsGrant { 57 n.Grant.Format(ctx) 58 } else { 59 n.Revoke.Format(ctx) 60 } 61 } 62 63 // AbbreviatedGrant represents the GRANT part of an 64 // ALTER DEFAULT PRIVILEGES statement. 65 type AbbreviatedGrant struct { 66 Privileges privilege.List 67 Target privilege.TargetObjectType 68 Grantees RoleSpecList 69 WithGrantOption bool 70 } 71 72 // Format implements the NodeFormatter interface. 73 func (n *AbbreviatedGrant) Format(ctx *FmtCtx) { 74 ctx.WriteString("GRANT ") 75 n.Privileges.FormatNames(&ctx.Buffer) 76 ctx.WriteString(" ON ") 77 switch n.Target { 78 case privilege.Tables: 79 ctx.WriteString("TABLES ") 80 case privilege.Sequences: 81 ctx.WriteString("SEQUENCES ") 82 case privilege.Types: 83 ctx.WriteString("TYPES ") 84 case privilege.Schemas: 85 ctx.WriteString("SCHEMAS ") 86 case privilege.Routines: 87 ctx.WriteString("FUNCTIONS ") 88 } 89 ctx.WriteString("TO ") 90 n.Grantees.Format(ctx) 91 if n.WithGrantOption { 92 ctx.WriteString(" WITH GRANT OPTION") 93 } 94 } 95 96 // AbbreviatedRevoke represents the REVOKE part of an 97 // ALTER DEFAULT PRIVILEGES statement. 98 type AbbreviatedRevoke struct { 99 Privileges privilege.List 100 Target privilege.TargetObjectType 101 Grantees RoleSpecList 102 GrantOptionFor bool 103 } 104 105 // Format implements the NodeFormatter interface. 106 func (n *AbbreviatedRevoke) Format(ctx *FmtCtx) { 107 ctx.WriteString("REVOKE ") 108 if n.GrantOptionFor { 109 ctx.WriteString("GRANT OPTION FOR ") 110 } 111 n.Privileges.FormatNames(&ctx.Buffer) 112 ctx.WriteString(" ON ") 113 switch n.Target { 114 case privilege.Tables: 115 ctx.WriteString("TABLES ") 116 case privilege.Sequences: 117 ctx.WriteString("SEQUENCES ") 118 case privilege.Types: 119 ctx.WriteString("TYPES ") 120 case privilege.Schemas: 121 ctx.WriteString("SCHEMAS ") 122 case privilege.Routines: 123 ctx.WriteString("FUNCTIONS ") 124 } 125 ctx.WriteString(" FROM ") 126 n.Grantees.Format(ctx) 127 }