github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/sem/tree/scrub.go (about) 1 // Copyright 2017 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 "fmt" 14 15 // ScrubType describes the SCRUB statement operation. 16 type ScrubType int 17 18 const ( 19 // ScrubTable describes the SCRUB operation SCRUB TABLE. 20 ScrubTable = iota 21 // ScrubDatabase describes the SCRUB operation SCRUB DATABASE. 22 ScrubDatabase = iota 23 ) 24 25 // Scrub represents a SCRUB statement. 26 type Scrub struct { 27 Typ ScrubType 28 Options ScrubOptions 29 // Table is only set during SCRUB TABLE statements. 30 Table *UnresolvedObjectName 31 // Database is only set during SCRUB DATABASE statements. 32 Database Name 33 AsOf AsOfClause 34 } 35 36 // Format implements the NodeFormatter interface. 37 func (n *Scrub) Format(ctx *FmtCtx) { 38 ctx.WriteString("EXPERIMENTAL SCRUB ") 39 switch n.Typ { 40 case ScrubTable: 41 ctx.WriteString("TABLE ") 42 n.Table.Format(ctx) 43 case ScrubDatabase: 44 ctx.WriteString("DATABASE ") 45 ctx.FormatNode(&n.Database) 46 default: 47 panic("Unhandled ScrubType") 48 } 49 50 if n.AsOf.Expr != nil { 51 ctx.WriteByte(' ') 52 ctx.FormatNode(&n.AsOf) 53 } 54 55 if len(n.Options) > 0 { 56 ctx.WriteString(" WITH OPTIONS ") 57 ctx.FormatNode(&n.Options) 58 } 59 } 60 61 // ScrubOptions corresponds to a comma-delimited list of scrub options. 62 type ScrubOptions []ScrubOption 63 64 // Format implements the NodeFormatter interface. 65 func (n *ScrubOptions) Format(ctx *FmtCtx) { 66 for i, option := range *n { 67 if i > 0 { 68 ctx.WriteString(", ") 69 } 70 ctx.FormatNode(option) 71 } 72 } 73 74 func (n *ScrubOptions) String() string { return AsString(n) } 75 76 // ScrubOption represents a scrub option. 77 type ScrubOption interface { 78 fmt.Stringer 79 NodeFormatter 80 81 scrubOptionType() 82 } 83 84 // scrubOptionType implements the ScrubOption interface 85 func (*ScrubOptionIndex) scrubOptionType() {} 86 func (*ScrubOptionPhysical) scrubOptionType() {} 87 func (*ScrubOptionConstraint) scrubOptionType() {} 88 89 func (n *ScrubOptionIndex) String() string { return AsString(n) } 90 func (n *ScrubOptionPhysical) String() string { return AsString(n) } 91 func (n *ScrubOptionConstraint) String() string { return AsString(n) } 92 93 // ScrubOptionIndex represents an INDEX scrub check. 94 type ScrubOptionIndex struct { 95 IndexNames NameList 96 } 97 98 // Format implements the NodeFormatter interface. 99 func (n *ScrubOptionIndex) Format(ctx *FmtCtx) { 100 ctx.WriteString("INDEX ") 101 if n.IndexNames != nil { 102 ctx.WriteByte('(') 103 ctx.FormatNode(&n.IndexNames) 104 ctx.WriteByte(')') 105 } else { 106 ctx.WriteString("ALL") 107 } 108 } 109 110 // ScrubOptionPhysical represents a PHYSICAL scrub check. 111 type ScrubOptionPhysical struct{} 112 113 // Format implements the NodeFormatter interface. 114 func (n *ScrubOptionPhysical) Format(ctx *FmtCtx) { 115 ctx.WriteString("PHYSICAL") 116 } 117 118 // ScrubOptionConstraint represents a CONSTRAINT scrub check. 119 type ScrubOptionConstraint struct { 120 ConstraintNames NameList 121 } 122 123 // Format implements the NodeFormatter interface. 124 func (n *ScrubOptionConstraint) Format(ctx *FmtCtx) { 125 ctx.WriteString("CONSTRAINT ") 126 if n.ConstraintNames != nil { 127 ctx.WriteByte('(') 128 ctx.FormatNode(&n.ConstraintNames) 129 ctx.WriteByte(')') 130 } else { 131 ctx.WriteString("ALL") 132 } 133 }