github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/sql/sem/tree/grant.go (about)

     1  // Copyright 2012, Google Inc. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in licenses/BSD-vitess.txt.
     4  
     5  // Portions of this file are additionally subject to the following
     6  // license and copyright.
     7  //
     8  // Copyright 2015 The Cockroach Authors.
     9  //
    10  // Use of this software is governed by the Business Source License
    11  // included in the file licenses/BSL.txt.
    12  //
    13  // As of the Change Date specified in that file, in accordance with
    14  // the Business Source License, use of this software will be governed
    15  // by the Apache License, Version 2.0, included in the file
    16  // licenses/APL.txt.
    17  
    18  // This code was derived from https://github.com/youtube/vitess.
    19  
    20  package tree
    21  
    22  import "github.com/cockroachdb/cockroachdb-parser/pkg/sql/privilege"
    23  
    24  // Grant represents a GRANT statement.
    25  type Grant struct {
    26  	Privileges      privilege.List
    27  	Targets         GrantTargetList
    28  	Grantees        RoleSpecList
    29  	WithGrantOption bool
    30  }
    31  
    32  // GrantTargetList represents a list of targets.
    33  // Only one field may be non-nil.
    34  type GrantTargetList struct {
    35  	Databases  NameList
    36  	Schemas    ObjectNamePrefixList
    37  	Tables     TableAttrs
    38  	Types      []*UnresolvedObjectName
    39  	Functions  RoutineObjs
    40  	Procedures RoutineObjs
    41  	// If the target is for all sequences in a set of schemas.
    42  	AllSequencesInSchema bool
    43  	// If the target is for all tables in a set of schemas.
    44  	AllTablesInSchema bool
    45  	// If the target is for all functions in a set of schemas.
    46  	AllFunctionsInSchema bool
    47  	// If the target is for all procedures in a set of schemas.
    48  	AllProceduresInSchema bool
    49  	// If the target is system.
    50  	System bool
    51  	// If the target is External Connection.
    52  	ExternalConnections NameList
    53  
    54  	// ForRoles and Roles are used internally in the parser and not used
    55  	// in the AST. Therefore they do not participate in pretty-printing,
    56  	// etc.
    57  	ForRoles bool
    58  	Roles    RoleSpecList
    59  }
    60  
    61  // Format implements the NodeFormatter interface.
    62  func (tl *GrantTargetList) Format(ctx *FmtCtx) {
    63  	if tl.Databases != nil {
    64  		ctx.WriteString("DATABASE ")
    65  		ctx.FormatNode(&tl.Databases)
    66  	} else if tl.AllSequencesInSchema {
    67  		ctx.WriteString("ALL SEQUENCES IN SCHEMA ")
    68  		ctx.FormatNode(&tl.Schemas)
    69  	} else if tl.AllTablesInSchema {
    70  		ctx.WriteString("ALL TABLES IN SCHEMA ")
    71  		ctx.FormatNode(&tl.Schemas)
    72  	} else if tl.AllFunctionsInSchema {
    73  		ctx.WriteString("ALL FUNCTIONS IN SCHEMA ")
    74  		ctx.FormatNode(&tl.Schemas)
    75  	} else if tl.AllProceduresInSchema {
    76  		ctx.WriteString("ALL PROCEDURES IN SCHEMA ")
    77  		ctx.FormatNode(&tl.Schemas)
    78  	} else if tl.Schemas != nil {
    79  		ctx.WriteString("SCHEMA ")
    80  		ctx.FormatNode(&tl.Schemas)
    81  	} else if tl.Types != nil {
    82  		ctx.WriteString("TYPE ")
    83  		for i, typ := range tl.Types {
    84  			if i != 0 {
    85  				ctx.WriteString(", ")
    86  			}
    87  			ctx.FormatNode(typ)
    88  		}
    89  	} else if tl.ExternalConnections != nil {
    90  		ctx.WriteString("EXTERNAL CONNECTION ")
    91  		ctx.FormatNode(&tl.ExternalConnections)
    92  	} else if tl.Functions != nil {
    93  		ctx.WriteString("FUNCTION ")
    94  		ctx.FormatNode(tl.Functions)
    95  	} else if tl.Procedures != nil {
    96  		ctx.WriteString("PROCEDURE ")
    97  		ctx.FormatNode(tl.Procedures)
    98  	} else {
    99  		if tl.Tables.SequenceOnly {
   100  			ctx.WriteString("SEQUENCE ")
   101  		} else {
   102  			ctx.WriteString("TABLE ")
   103  		}
   104  		ctx.FormatNode(&tl.Tables.TablePatterns)
   105  	}
   106  }
   107  
   108  // Format implements the NodeFormatter interface.
   109  func (node *Grant) Format(ctx *FmtCtx) {
   110  	ctx.WriteString("GRANT ")
   111  	if node.Targets.System {
   112  		ctx.WriteString(" SYSTEM ")
   113  	}
   114  	node.Privileges.FormatNames(&ctx.Buffer)
   115  	if !node.Targets.System {
   116  		ctx.WriteString(" ON ")
   117  		ctx.FormatNode(&node.Targets)
   118  	}
   119  	ctx.WriteString(" TO ")
   120  	ctx.FormatNode(&node.Grantees)
   121  }
   122  
   123  // GrantRole represents a GRANT <role> statement.
   124  type GrantRole struct {
   125  	Roles       NameList
   126  	Members     RoleSpecList
   127  	AdminOption bool
   128  }
   129  
   130  // Format implements the NodeFormatter interface.
   131  func (node *GrantRole) Format(ctx *FmtCtx) {
   132  	ctx.WriteString("GRANT ")
   133  	ctx.FormatNode(&node.Roles)
   134  	ctx.WriteString(" TO ")
   135  	ctx.FormatNode(&node.Members)
   136  	if node.AdminOption {
   137  		ctx.WriteString(" WITH ADMIN OPTION")
   138  	}
   139  }