github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/sql/sem/tree/update.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 // Update represents an UPDATE statement. 23 type Update struct { 24 With *With 25 Table TableExpr 26 Exprs UpdateExprs 27 From TableExprs 28 Where *Where 29 OrderBy OrderBy 30 Limit *Limit 31 Returning ReturningClause 32 } 33 34 // Format implements the NodeFormatter interface. 35 func (node *Update) Format(ctx *FmtCtx) { 36 ctx.FormatNode(node.With) 37 ctx.WriteString("UPDATE ") 38 ctx.FormatNode(node.Table) 39 ctx.WriteString(" SET ") 40 ctx.FormatNode(&node.Exprs) 41 if len(node.From) > 0 { 42 ctx.WriteString(" FROM ") 43 ctx.FormatNode(&node.From) 44 } 45 if node.Where != nil { 46 ctx.WriteByte(' ') 47 ctx.FormatNode(node.Where) 48 } 49 if len(node.OrderBy) > 0 { 50 ctx.WriteByte(' ') 51 ctx.FormatNode(&node.OrderBy) 52 } 53 if node.Limit != nil { 54 ctx.WriteByte(' ') 55 ctx.FormatNode(node.Limit) 56 } 57 if HasReturningClause(node.Returning) { 58 ctx.WriteByte(' ') 59 ctx.FormatNode(node.Returning) 60 } 61 } 62 63 // UpdateExprs represents a list of update expressions. 64 type UpdateExprs []*UpdateExpr 65 66 // Format implements the NodeFormatter interface. 67 func (node *UpdateExprs) Format(ctx *FmtCtx) { 68 for i, n := range *node { 69 if i > 0 { 70 ctx.WriteString(", ") 71 } 72 ctx.FormatNode(n) 73 } 74 } 75 76 // UpdateExpr represents an update expression. 77 type UpdateExpr struct { 78 Tuple bool 79 Names NameList 80 Expr Expr 81 } 82 83 // Format implements the NodeFormatter interface. 84 func (node *UpdateExpr) Format(ctx *FmtCtx) { 85 open, close := "", "" 86 if node.Tuple { 87 open, close = "(", ")" 88 } 89 ctx.WriteString(open) 90 ctx.FormatNode(&node.Names) 91 ctx.WriteString(close) 92 ctx.WriteString(" = ") 93 ctx.FormatNode(node.Expr) 94 }