github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/sem/tree/table_pattern.go (about) 1 // Copyright 2016 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 // Table patterns are used by e.g. GRANT statements, to designate 16 // zero, one or more table names. For example: 17 // GRANT ... ON foo ... 18 // GRANT ... ON * ... 19 // GRANT ... ON db.* ... 20 // 21 // The other syntax nodes hold a TablePattern reference. This is 22 // initially populated during parsing with an UnresolvedName, which 23 // can be transformed to either a TableName (single name) or 24 // AllTablesSelector instance (all tables of a given database) using 25 // NormalizeTablePattern(). 26 27 // TablePattern is the common interface to UnresolvedName, TableName 28 // and AllTablesSelector. 29 type TablePattern interface { 30 fmt.Stringer 31 NodeFormatter 32 33 // NormalizeTablePattern() guarantees to return a pattern that is 34 // not an UnresolvedName. This converts the UnresolvedName to an 35 // AllTablesSelector or TableName as necessary. 36 NormalizeTablePattern() (TablePattern, error) 37 } 38 39 var _ TablePattern = &UnresolvedName{} 40 var _ TablePattern = &TableName{} 41 var _ TablePattern = &AllTablesSelector{} 42 43 // NormalizeTablePattern resolves an UnresolvedName to either a 44 // TableName or AllTablesSelector. 45 func (n *UnresolvedName) NormalizeTablePattern() (TablePattern, error) { 46 return classifyTablePattern(n) 47 } 48 49 // NormalizeTablePattern implements the TablePattern interface. 50 func (t *TableName) NormalizeTablePattern() (TablePattern, error) { return t, nil } 51 52 // AllTablesSelector corresponds to a selection of all 53 // tables in a database, e.g. when used with GRANT. 54 type AllTablesSelector struct { 55 ObjectNamePrefix 56 } 57 58 // Format implements the NodeFormatter interface. 59 func (at *AllTablesSelector) Format(ctx *FmtCtx) { 60 at.ObjectNamePrefix.Format(ctx) 61 if at.ExplicitSchema || ctx.alwaysFormatTablePrefix() { 62 ctx.WriteByte('.') 63 } 64 ctx.WriteByte('*') 65 } 66 func (at *AllTablesSelector) String() string { return AsString(at) } 67 68 // NormalizeTablePattern implements the TablePattern interface. 69 func (at *AllTablesSelector) NormalizeTablePattern() (TablePattern, error) { return at, nil } 70 71 // TablePatterns implement a comma-separated list of table patterns. 72 // Used by e.g. the GRANT statement. 73 type TablePatterns []TablePattern 74 75 // Format implements the NodeFormatter interface. 76 func (tt *TablePatterns) Format(ctx *FmtCtx) { 77 for i, t := range *tt { 78 if i > 0 { 79 ctx.WriteString(", ") 80 } 81 ctx.FormatNode(t) 82 } 83 }