github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/sql/sem/tree/drop.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 // DropBehavior represents options for dropping schema elements. 23 type DropBehavior int 24 25 // DropBehavior values. 26 const ( 27 DropDefault DropBehavior = iota 28 DropRestrict 29 DropCascade 30 ) 31 32 var dropBehaviorName = [...]string{ 33 DropDefault: "", 34 DropRestrict: "RESTRICT", 35 DropCascade: "CASCADE", 36 } 37 38 func (d DropBehavior) String() string { 39 return dropBehaviorName[d] 40 } 41 42 // DropDatabase represents a DROP DATABASE statement. 43 type DropDatabase struct { 44 Name Name 45 IfExists bool 46 DropBehavior DropBehavior 47 } 48 49 // Format implements the NodeFormatter interface. 50 func (node *DropDatabase) Format(ctx *FmtCtx) { 51 ctx.WriteString("DROP DATABASE ") 52 if node.IfExists { 53 ctx.WriteString("IF EXISTS ") 54 } 55 ctx.FormatNode(&node.Name) 56 if node.DropBehavior != DropDefault { 57 ctx.WriteByte(' ') 58 ctx.WriteString(node.DropBehavior.String()) 59 } 60 } 61 62 // DropIndex represents a DROP INDEX statement. 63 type DropIndex struct { 64 IndexList TableIndexNames 65 IfExists bool 66 DropBehavior DropBehavior 67 Concurrently bool 68 } 69 70 // Format implements the NodeFormatter interface. 71 func (node *DropIndex) Format(ctx *FmtCtx) { 72 ctx.WriteString("DROP INDEX ") 73 if node.Concurrently { 74 ctx.WriteString("CONCURRENTLY ") 75 } 76 if node.IfExists { 77 ctx.WriteString("IF EXISTS ") 78 } 79 ctx.FormatNode(&node.IndexList) 80 if node.DropBehavior != DropDefault { 81 ctx.WriteByte(' ') 82 ctx.WriteString(node.DropBehavior.String()) 83 } 84 } 85 86 // DropTable represents a DROP TABLE statement. 87 type DropTable struct { 88 Names TableNames 89 IfExists bool 90 DropBehavior DropBehavior 91 } 92 93 // Format implements the NodeFormatter interface. 94 func (node *DropTable) Format(ctx *FmtCtx) { 95 ctx.WriteString("DROP TABLE ") 96 if node.IfExists { 97 ctx.WriteString("IF EXISTS ") 98 } 99 ctx.FormatNode(&node.Names) 100 if node.DropBehavior != DropDefault { 101 ctx.WriteByte(' ') 102 ctx.WriteString(node.DropBehavior.String()) 103 } 104 } 105 106 // DropView represents a DROP VIEW statement. 107 type DropView struct { 108 Names TableNames 109 IfExists bool 110 DropBehavior DropBehavior 111 IsMaterialized bool 112 } 113 114 // Format implements the NodeFormatter interface. 115 func (node *DropView) Format(ctx *FmtCtx) { 116 ctx.WriteString("DROP ") 117 if node.IsMaterialized { 118 ctx.WriteString("MATERIALIZED ") 119 } 120 ctx.WriteString("VIEW ") 121 if node.IfExists { 122 ctx.WriteString("IF EXISTS ") 123 } 124 ctx.FormatNode(&node.Names) 125 if node.DropBehavior != DropDefault { 126 ctx.WriteByte(' ') 127 ctx.WriteString(node.DropBehavior.String()) 128 } 129 } 130 131 // DropSequence represents a DROP SEQUENCE statement. 132 type DropSequence struct { 133 Names TableNames 134 IfExists bool 135 DropBehavior DropBehavior 136 } 137 138 // Format implements the NodeFormatter interface. 139 func (node *DropSequence) Format(ctx *FmtCtx) { 140 ctx.WriteString("DROP SEQUENCE ") 141 if node.IfExists { 142 ctx.WriteString("IF EXISTS ") 143 } 144 ctx.FormatNode(&node.Names) 145 if node.DropBehavior != DropDefault { 146 ctx.WriteByte(' ') 147 ctx.WriteString(node.DropBehavior.String()) 148 } 149 } 150 151 // DropRole represents a DROP ROLE statement 152 type DropRole struct { 153 Names RoleSpecList 154 IsRole bool 155 IfExists bool 156 } 157 158 // Format implements the NodeFormatter interface. 159 func (node *DropRole) Format(ctx *FmtCtx) { 160 ctx.WriteString("DROP") 161 if node.IsRole { 162 ctx.WriteString(" ROLE ") 163 } else { 164 ctx.WriteString(" USER ") 165 } 166 if node.IfExists { 167 ctx.WriteString("IF EXISTS ") 168 } 169 ctx.FormatNode(&node.Names) 170 } 171 172 // DropType represents a DROP TYPE command. 173 type DropType struct { 174 Names []*UnresolvedObjectName 175 IfExists bool 176 DropBehavior DropBehavior 177 } 178 179 var _ Statement = &DropType{} 180 181 // Format implements the NodeFormatter interface. 182 func (node *DropType) Format(ctx *FmtCtx) { 183 ctx.WriteString("DROP TYPE ") 184 if node.IfExists { 185 ctx.WriteString("IF EXISTS ") 186 } 187 for i := range node.Names { 188 if i > 0 { 189 ctx.WriteString(", ") 190 } 191 ctx.FormatNode(node.Names[i]) 192 } 193 if node.DropBehavior != DropDefault { 194 ctx.WriteByte(' ') 195 ctx.WriteString(node.DropBehavior.String()) 196 } 197 } 198 199 // DropSchema represents a DROP SCHEMA command. 200 type DropSchema struct { 201 Names ObjectNamePrefixList 202 IfExists bool 203 DropBehavior DropBehavior 204 } 205 206 var _ Statement = &DropSchema{} 207 208 // Format implements the NodeFormatter interface. 209 func (node *DropSchema) Format(ctx *FmtCtx) { 210 ctx.WriteString("DROP SCHEMA ") 211 if node.IfExists { 212 ctx.WriteString("IF EXISTS ") 213 } 214 ctx.FormatNode(&node.Names) 215 if node.DropBehavior != DropDefault { 216 ctx.WriteString(" ") 217 ctx.WriteString(node.DropBehavior.String()) 218 } 219 } 220 221 // DropExternalConnection represents a DROP EXTERNAL CONNECTION statement. 222 type DropExternalConnection struct { 223 ConnectionLabel Expr 224 } 225 226 var _ Statement = &DropExternalConnection{} 227 228 // Format implements the Statement interface. 229 func (node *DropExternalConnection) Format(ctx *FmtCtx) { 230 ctx.WriteString("DROP EXTERNAL CONNECTION") 231 232 if node.ConnectionLabel != nil { 233 ctx.WriteString(" ") 234 ctx.FormatNode(node.ConnectionLabel) 235 } 236 } 237 238 // DropTenant represents a DROP VIRTUAL CLUSTER command. 239 type DropTenant struct { 240 TenantSpec *TenantSpec 241 IfExists bool 242 Immediate bool 243 } 244 245 var _ Statement = &DropTenant{} 246 247 // Format implements the NodeFormatter interface. 248 func (node *DropTenant) Format(ctx *FmtCtx) { 249 ctx.WriteString("DROP VIRTUAL CLUSTER ") 250 if node.IfExists { 251 ctx.WriteString("IF EXISTS ") 252 } 253 ctx.FormatNode(node.TenantSpec) 254 if node.Immediate { 255 ctx.WriteString(" IMMEDIATE") 256 } 257 }