github.com/dolthub/go-mysql-server@v0.18.0/sql/plan/tablealias.go (about) 1 // Copyright 2020-2021 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package plan 16 17 import ( 18 "github.com/dolthub/go-mysql-server/sql" 19 ) 20 21 // TableAlias is a node that acts as a table with a given name. 22 type TableAlias struct { 23 *UnaryNode 24 name string 25 comment string 26 id sql.TableId 27 cols sql.ColSet 28 } 29 30 var _ sql.RenameableNode = (*TableAlias)(nil) 31 var _ sql.CommentedNode = (*TableAlias)(nil) 32 var _ sql.CollationCoercible = (*TableAlias)(nil) 33 34 // NewTableAlias returns a new Table alias node. 35 func NewTableAlias(name string, node sql.Node) *TableAlias { 36 ret := &TableAlias{UnaryNode: &UnaryNode{Child: node}, name: name} 37 if tin, ok := node.(TableIdNode); ok { 38 ret.id = tin.Id() 39 ret.cols = tin.Columns() 40 } 41 return ret 42 } 43 44 // WithId implements sql.TableIdNode 45 func (t *TableAlias) WithId(id sql.TableId) TableIdNode { 46 ret := *t 47 ret.id = id 48 return &ret 49 } 50 51 // Id implements sql.TableIdNode 52 func (t *TableAlias) Id() sql.TableId { 53 return t.id 54 } 55 56 // WithColumns implements sql.TableIdNode 57 func (t *TableAlias) WithColumns(set sql.ColSet) TableIdNode { 58 ret := *t 59 ret.cols = set 60 return &ret 61 } 62 63 // Columns implements sql.TableIdNode 64 func (t *TableAlias) Columns() sql.ColSet { 65 return t.cols 66 } 67 68 // Name implements the Nameable interface. 69 func (t *TableAlias) Name() string { 70 return t.name 71 } 72 73 func (t *TableAlias) IsReadOnly() bool { 74 return t.Child.IsReadOnly() 75 } 76 77 func (t *TableAlias) WithComment(s string) sql.Node { 78 ret := *t 79 ret.comment = s 80 return &ret 81 } 82 83 func (t *TableAlias) Comment() string { 84 return t.comment 85 } 86 87 // Schema implements the Node interface. TableAlias alters the schema of its child element to rename the source of 88 // columns to the alias. 89 func (t *TableAlias) Schema() sql.Schema { 90 childSchema := t.Child.Schema() 91 copy := make(sql.Schema, len(childSchema)) 92 for i, col := range childSchema { 93 colCopy := *col 94 colCopy.Source = t.name 95 copy[i] = &colCopy 96 } 97 return copy 98 } 99 100 // WithChildren implements the Node interface. 101 func (t *TableAlias) WithChildren(children ...sql.Node) (sql.Node, error) { 102 if len(children) != 1 { 103 return nil, sql.ErrInvalidChildrenNumber.New(t, len(children), 1) 104 } 105 106 ret := NewTableAlias(t.name, children[0]) 107 ret.comment = t.comment 108 ret.cols = t.cols 109 ret.id = t.id 110 return ret, nil 111 } 112 113 // CheckPrivileges implements the interface sql.Node. 114 func (t *TableAlias) CheckPrivileges(ctx *sql.Context, opChecker sql.PrivilegedOperationChecker) bool { 115 if t.UnaryNode != nil { 116 return t.UnaryNode.Child.CheckPrivileges(ctx, opChecker) 117 } 118 return true 119 } 120 121 // CollationCoercibility implements the interface sql.CollationCoercible. 122 func (t *TableAlias) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) { 123 if t.UnaryNode != nil { 124 return sql.GetCoercibility(ctx, t.UnaryNode.Child) 125 } 126 return sql.Collation_binary, 7 127 } 128 129 func (t TableAlias) String() string { 130 pr := sql.NewTreePrinter() 131 _ = pr.WriteNode("TableAlias(%s)", t.name) 132 _ = pr.WriteChildren(t.Child.String()) 133 return pr.String() 134 } 135 136 func (t TableAlias) DebugString() string { 137 pr := sql.NewTreePrinter() 138 _ = pr.WriteNode("TableAlias(%s)", t.name) 139 _ = pr.WriteChildren(sql.DebugString(t.Child)) 140 return pr.String() 141 } 142 143 func (t TableAlias) WithName(name string) sql.Node { 144 t.name = name 145 return &t 146 }