/
table_pattern.go
83 lines (70 loc) · 2.64 KB
/
table_pattern.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright 2016 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package ast
import "fmt"
// Table patterns are used by e.g. GRANT statements, to designate
// zero, one or more table names. For example:
// GRANT ... ON foo ...
// GRANT ... ON * ...
// GRANT ... ON db.* ...
//
// The other syntax nodes hold a TablePattern reference. This is
// initially populated during parsing with an UnresolvedName, which
// can be transformed to either a TableName (single name) or
// AllTablesSelector instance (all tables of a given database) using
// NormalizeTablePattern().
// TablePattern is the common interface to UnresolvedName, TableName
// and AllTablesSelector.
type TablePattern interface {
fmt.Stringer
NodeFormatter
// NormalizeTablePattern() guarantees to return a pattern that is
// not an UnresolvedName. This converts the UnresolvedName to an
// AllTablesSelector or TableName as necessary.
NormalizeTablePattern() (TablePattern, error)
}
var _ TablePattern = &UnresolvedName{}
var _ TablePattern = &TableName{}
var _ TablePattern = &AllTablesSelector{}
// NormalizeTablePattern resolves an UnresolvedName to either a
// TableName or AllTablesSelector.
func (n *UnresolvedName) NormalizeTablePattern() (TablePattern, error) {
return classifyTablePattern(n)
}
// NormalizeTablePattern implements the TablePattern interface.
func (t *TableName) NormalizeTablePattern() (TablePattern, error) { return t, nil }
// AllTablesSelector corresponds to a selection of all
// tables in a database, e.g. when used with GRANT.
type AllTablesSelector struct {
ObjectNamePrefix
}
// Format implements the NodeFormatter interface.
func (at *AllTablesSelector) Format(ctx *FmtCtx) {
at.ObjectNamePrefix.Format(ctx)
if at.ExplicitSchema || ctx.alwaysFormatTablePrefix() {
ctx.WriteByte('.')
}
ctx.WriteByte('*')
}
func (at *AllTablesSelector) String() string { return AsString(at) }
// NormalizeTablePattern implements the TablePattern interface.
func (at *AllTablesSelector) NormalizeTablePattern() (TablePattern, error) { return at, nil }
// TablePatterns implement a comma-separated list of table patterns.
// Used by e.g. the GRANT statement.
type TablePatterns []TablePattern
// Format implements the NodeFormatter interface.
func (tt *TablePatterns) Format(ctx *FmtCtx) {
for i, t := range *tt {
if i > 0 {
ctx.WriteString(", ")
}
ctx.FormatNode(t)
}
}