/
showcolumns.go
192 lines (166 loc) · 4.75 KB
/
showcolumns.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright 2020-2021 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package plan
import (
"fmt"
"strings"
"github.com/Ciyfly/FakerMysql/sql"
)
// ShowColumns shows the columns details of a table.
type ShowColumns struct {
UnaryNode
Full bool
Indexes []sql.Index
}
var (
showColumnsSchema = sql.Schema{
{Name: "Field", Type: sql.LongText},
{Name: "Type", Type: sql.LongText},
{Name: "Null", Type: sql.LongText},
{Name: "Key", Type: sql.LongText},
{Name: "Default", Type: sql.LongText, Nullable: true},
{Name: "Extra", Type: sql.LongText},
}
showColumnsFullSchema = sql.Schema{
{Name: "Field", Type: sql.LongText},
{Name: "Type", Type: sql.LongText},
{Name: "Collation", Type: sql.LongText, Nullable: true},
{Name: "Null", Type: sql.LongText},
{Name: "Key", Type: sql.LongText},
{Name: "Default", Type: sql.LongText, Nullable: true},
{Name: "Extra", Type: sql.LongText},
{Name: "Privileges", Type: sql.LongText},
{Name: "Comment", Type: sql.LongText},
}
)
// NewShowColumns creates a new ShowColumns node.
func NewShowColumns(full bool, child sql.Node) *ShowColumns {
return &ShowColumns{UnaryNode: UnaryNode{Child: child}, Full: full}
}
var _ sql.Node = (*ShowColumns)(nil)
// Schema implements the sql.Node interface.
func (s *ShowColumns) Schema() sql.Schema {
if s.Full {
return showColumnsFullSchema
}
return showColumnsSchema
}
// RowIter creates a new ShowColumns node.
func (s *ShowColumns) RowIter(ctx *sql.Context, row sql.Row) (sql.RowIter, error) {
span, _ := ctx.Span("plan.ShowColumns")
schema := s.Child.Schema()
var rows = make([]sql.Row, len(schema))
for i, col := range schema {
var row sql.Row
var collation interface{}
if sql.IsTextOnly(col.Type) {
collation = sql.Collation_Default.String()
}
var null = "NO"
if col.Nullable {
null = "YES"
}
node := s.Child
if exchange, ok := node.(*Exchange); ok {
node = exchange.Child
}
key := ""
switch table := node.(type) {
case *ResolvedTable:
if col.PrimaryKey {
key = "PRI"
} else if s.isFirstColInUniqueKey(col, table) {
key = "UNI"
} else if s.isFirstColInNonUniqueKey(col, table) {
key = "MUL"
}
case *SubqueryAlias:
// no key info for views
default:
panic(fmt.Sprintf("unexpected type %T", s.Child))
}
var defaultVal string
if col.Default != nil {
defaultVal = col.Default.String()
}
// TODO: rather than lower-casing here, we should lower-case the String() method of types
if s.Full {
row = sql.Row{
col.Name,
strings.ToLower(col.Type.String()),
collation,
null,
key,
defaultVal,
col.Extra,
"", // Privileges
col.Comment,
}
} else {
row = sql.Row{
col.Name,
strings.ToLower(col.Type.String()),
null,
key,
defaultVal,
col.Extra,
}
}
rows[i] = row
}
return sql.NewSpanIter(span, sql.RowsToRowIter(rows...)), nil
}
// WithChildren implements the Node interface.
func (s *ShowColumns) WithChildren(children ...sql.Node) (sql.Node, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(s, len(children), 1)
}
showColumns := NewShowColumns(s.Full, children[0])
showColumns.Indexes = s.Indexes
return showColumns, nil
}
func (s *ShowColumns) String() string {
tp := sql.NewTreePrinter()
if s.Full {
_ = tp.WriteNode("ShowColumns(full)")
} else {
_ = tp.WriteNode("ShowColumns")
}
_ = tp.WriteChildren(s.Child.String())
return tp.String()
}
func (s *ShowColumns) isFirstColInUniqueKey(col *sql.Column, table sql.Table) bool {
for _, idx := range s.Indexes {
if !idx.IsUnique() {
continue
}
firstIndexCol := GetColumnFromIndexExpr(idx.Expressions()[0], table)
if firstIndexCol != nil && firstIndexCol.Name == col.Name {
return true
}
}
return false
}
func (s *ShowColumns) isFirstColInNonUniqueKey(col *sql.Column, table sql.Table) bool {
for _, idx := range s.Indexes {
if idx.IsUnique() {
continue
}
firstIndexCol := GetColumnFromIndexExpr(idx.Expressions()[0], table)
if firstIndexCol != nil && firstIndexCol.Name == col.Name {
return true
}
}
return false
}