github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/sql/sem/tree/alter_role.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  // AlterRole represents an `ALTER ROLE ... WITH options` statement.
    14  type AlterRole struct {
    15  	Name      RoleSpec
    16  	IfExists  bool
    17  	IsRole    bool
    18  	KVOptions KVOptions
    19  }
    20  
    21  // Format implements the NodeFormatter interface.
    22  func (node *AlterRole) Format(ctx *FmtCtx) {
    23  	ctx.WriteString("ALTER")
    24  	if node.IsRole {
    25  		ctx.WriteString(" ROLE ")
    26  	} else {
    27  		ctx.WriteString(" USER ")
    28  	}
    29  	if node.IfExists {
    30  		ctx.WriteString("IF EXISTS ")
    31  	}
    32  	ctx.FormatNode(&node.Name)
    33  
    34  	if len(node.KVOptions) > 0 {
    35  		ctx.WriteString(" WITH")
    36  		node.KVOptions.formatAsRoleOptions(ctx)
    37  	}
    38  }
    39  
    40  // AlterRoleSet represents an `ALTER ROLE ... SET` statement.
    41  type AlterRoleSet struct {
    42  	RoleName     RoleSpec
    43  	IfExists     bool
    44  	IsRole       bool
    45  	AllRoles     bool
    46  	DatabaseName Name
    47  	SetOrReset   *SetVar
    48  }
    49  
    50  // Format implements the NodeFormatter interface.
    51  func (node *AlterRoleSet) Format(ctx *FmtCtx) {
    52  	ctx.WriteString("ALTER")
    53  	if node.IsRole {
    54  		ctx.WriteString(" ROLE ")
    55  	} else {
    56  		ctx.WriteString(" USER ")
    57  	}
    58  	if node.IfExists {
    59  		ctx.WriteString("IF EXISTS ")
    60  	}
    61  	if node.AllRoles {
    62  		ctx.WriteString("ALL ")
    63  	} else {
    64  		ctx.FormatNode(&node.RoleName)
    65  		ctx.WriteString(" ")
    66  	}
    67  	if node.DatabaseName != "" {
    68  		ctx.WriteString("IN DATABASE ")
    69  		ctx.FormatNode(&node.DatabaseName)
    70  		ctx.WriteString(" ")
    71  	}
    72  	ctx.FormatNode(node.SetOrReset)
    73  }