github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/sql/sem/tree/set.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 // SetVar represents a SET or RESET statement. 23 type SetVar struct { 24 Name string 25 Local bool 26 Values Exprs 27 Reset bool 28 ResetAll bool 29 SetRow bool 30 } 31 32 // Format implements the NodeFormatter interface. 33 func (node *SetVar) Format(ctx *FmtCtx) { 34 if node.ResetAll { 35 ctx.WriteString("RESET ALL") 36 return 37 } 38 if node.Reset { 39 ctx.WriteString("RESET ") 40 ctx.WithFlags(ctx.flags & ^FmtAnonymize & ^FmtMarkRedactionNode, func() { 41 // Session var names never contain PII and should be distinguished 42 // for feature tracking purposes. 43 ctx.FormatNameP(&node.Name) 44 }) 45 return 46 } 47 ctx.WriteString("SET ") 48 if node.Local { 49 ctx.WriteString("LOCAL ") 50 } 51 if node.SetRow { 52 ctx.WriteString("ROW (") 53 ctx.FormatNode(&node.Values) 54 ctx.WriteString(")") 55 } else { 56 ctx.WithFlags(ctx.flags & ^FmtAnonymize & ^FmtMarkRedactionNode, func() { 57 // Session var names never contain PII and should be distinguished 58 // for feature tracking purposes. 59 ctx.FormatNameP(&node.Name) 60 }) 61 62 ctx.WriteString(" = ") 63 ctx.FormatNode(&node.Values) 64 } 65 } 66 67 // SetClusterSetting represents a SET CLUSTER SETTING statement. 68 type SetClusterSetting struct { 69 Name string 70 Value Expr 71 } 72 73 // Format implements the NodeFormatter interface. 74 func (node *SetClusterSetting) Format(ctx *FmtCtx) { 75 ctx.WriteString("SET CLUSTER SETTING ") 76 77 // Cluster setting names never contain PII and should be distinguished 78 // for feature tracking purposes. 79 ctx.WithFlags(ctx.flags & ^FmtAnonymize & ^FmtMarkRedactionNode, func() { 80 ctx.FormatNameP(&node.Name) 81 }) 82 83 ctx.WriteString(" = ") 84 85 switch v := node.Value.(type) { 86 case *DBool, *DInt: 87 ctx.WithFlags(ctx.flags & ^FmtAnonymize & ^FmtMarkRedactionNode, func() { 88 ctx.FormatNode(v) 89 }) 90 default: 91 ctx.FormatNode(v) 92 } 93 } 94 95 // SetTransaction represents a SET TRANSACTION statement. 96 type SetTransaction struct { 97 Modes TransactionModes 98 } 99 100 // Format implements the NodeFormatter interface. 101 func (node *SetTransaction) Format(ctx *FmtCtx) { 102 ctx.WriteString("SET TRANSACTION") 103 ctx.FormatNode(&node.Modes) 104 } 105 106 // SetSessionAuthorizationDefault represents a SET SESSION AUTHORIZATION DEFAULT 107 // statement. This can be extended (and renamed) if we ever support names in the 108 // last position. 109 type SetSessionAuthorizationDefault struct{} 110 111 // Format implements the NodeFormatter interface. 112 func (node *SetSessionAuthorizationDefault) Format(ctx *FmtCtx) { 113 ctx.WriteString("SET SESSION AUTHORIZATION DEFAULT") 114 } 115 116 // SetSessionCharacteristics represents a SET SESSION CHARACTERISTICS AS TRANSACTION statement. 117 type SetSessionCharacteristics struct { 118 Modes TransactionModes 119 } 120 121 // Format implements the NodeFormatter interface. 122 func (node *SetSessionCharacteristics) Format(ctx *FmtCtx) { 123 ctx.WriteString("SET SESSION CHARACTERISTICS AS TRANSACTION") 124 ctx.FormatNode(&node.Modes) 125 } 126 127 // SetTracing represents a SET TRACING statement. 128 type SetTracing struct { 129 Values Exprs 130 } 131 132 // Format implements the NodeFormatter interface. 133 func (node *SetTracing) Format(ctx *FmtCtx) { 134 ctx.WriteString("SET TRACING = ") 135 // Set tracing values never contain PII and should be distinguished 136 // for feature tracking purposes. 137 ctx.WithFlags(ctx.flags&^FmtMarkRedactionNode, func() { 138 ctx.FormatNode(&node.Values) 139 }) 140 }