forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
result_field.go
291 lines (247 loc) · 7.57 KB
/
result_field.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Copyright 2013 The ql Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSES/QL-LICENSE file.
// Copyright 2015 PingCAP, 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package field
import (
"fmt"
"strings"
"github.com/juju/errors"
"github.com/pingcap/tidb/column"
mysql "github.com/pingcap/tidb/mysqldef"
"github.com/pingcap/tidb/util/types"
)
const (
// DefaultFieldFlag is `FieldNameFlag`.
DefaultFieldFlag = FieldNameFlag
// CheckFieldFlag includes `FieldNameFlag` and `OrgFieldNameFlag`.
CheckFieldFlag = FieldNameFlag | OrgFieldNameFlag
)
const (
// OrgFieldNameFlag is origin filed name flag.
OrgFieldNameFlag uint32 = 1 << iota
// FieldNameFlag is filed name flag.
FieldNameFlag
)
// ResultField provides meta data of table column.
type ResultField struct {
column.Col // Col.Name is OrgName.
Name string
TableName string
OrgTableName string
DBName string
}
// String implements fmt.Stringer interface.
func (rf *ResultField) String() string {
return JoinQualifiedName(rf.DBName, rf.TableName, rf.Name)
}
// Clone clones a new ResultField from old ResultField.
func (rf *ResultField) Clone() *ResultField {
r := *rf
return &r
}
// RFQNames gets all ResultField names.
func RFQNames(l []*ResultField) []string {
r := make([]string, len(l))
for i, v := range l {
r[i] = fmt.Sprintf("%q", v.Name)
}
return r
}
// ColsToResultFields converts Cols to ResultFields.
func ColsToResultFields(cols []*column.Col, tableName string) []*ResultField {
// TODO: add DBName
r := make([]*ResultField, len(cols))
for i, v := range cols {
r[i] = ColToResultField(v, tableName)
}
return r
}
// ColToResultField converts Col to ResultField.
func ColToResultField(col *column.Col, tableName string) *ResultField {
// TODO: add DBName.
rf := &ResultField{
Col: *col,
Name: col.Name.O,
TableName: tableName,
OrgTableName: tableName,
}
if rf.Col.Flen == types.UnspecifiedLength {
rf.Col.Flen = 0
}
if rf.Col.Decimal == types.UnspecifiedLength {
rf.Col.Decimal = 0
}
// Keep things compatible for old clients.
// Refer to mysql-server/sql/protocol.cc send_result_set_metadata()
if rf.Tp == mysql.TypeVarchar {
rf.Tp = mysql.TypeVarString
}
return rf
}
// CheckAmbiguousField checks whether name is an ambiguous field in ResultFields.
func CheckAmbiguousField(name string, fields []*ResultField, flag uint32) error {
indices := GetResultFieldIndex(name, fields, flag)
if len(indices) > 1 {
return errors.Errorf("ambiguous field %s", name)
}
return nil
}
// CheckFieldName checks whether name is an unknown or ambiguous field in ResultFields.
func CheckFieldName(name string, fields []*ResultField, flag uint32) error {
if !ContainFieldName(name, fields, flag) {
return errors.Errorf("unknown field %s", name)
}
return CheckAmbiguousField(name, fields, OrgFieldNameFlag)
}
// CheckAllFieldNames checks whether names has unknown or ambiguous field in ResultFields.
func CheckAllFieldNames(names []string, fields []*ResultField, flag uint32) error {
for _, name := range names {
if err := CheckFieldName(name, fields, flag); err != nil {
return errors.Trace(err)
}
}
return nil
}
// ContainFieldName checks whether name is in ResultFields.
func ContainFieldName(name string, fields []*ResultField, flag uint32) bool {
indices := GetResultFieldIndex(name, fields, flag)
return len(indices) > 0
}
// ContainAllFieldNames checks whether names are all in ResultFields.
// TODO: add alias table name support
func ContainAllFieldNames(names []string, fields []*ResultField, flag uint32) bool {
for _, name := range names {
if !ContainFieldName(name, fields, flag) {
return false
}
}
return true
}
func splitQualifiedName(name string) (db string, table string, field string) {
seps := strings.Split(name, ".")
l := len(seps)
switch l {
case 1:
// `name` is field.
field = seps[0]
case 2:
// `name` is `table.field`.
table, field = seps[0], seps[1]
case 3:
// `name` is `db.table.field`.
db, table, field = seps[0], seps[1], seps[2]
default:
// `name` is `db.table.field`.
db, table, field = seps[l-3], seps[l-2], seps[l-1]
}
return
}
// JoinQualifiedName converts db, table, field to a qualified name.
func JoinQualifiedName(db string, table string, field string) string {
if len(db) > 0 {
return fmt.Sprintf("%s.%s.%s", db, table, field)
} else if len(table) > 0 {
return fmt.Sprintf("%s.%s", table, field)
} else {
return field
}
}
// GetResultFieldIndex gets name index in ResultFields.
func GetResultFieldIndex(name string, fields []*ResultField, flag uint32) []int {
var indices []int
db, table, field := splitQualifiedName(name)
// Check origin field name.
if flag&OrgFieldNameFlag > 0 {
for i, f := range fields {
if checkFieldsEqual(db, table, field, f.DBName, f.TableName, f.ColumnInfo.Name.L) {
indices = append(indices, i)
continue
}
}
}
// Check alias field name.
if flag&FieldNameFlag > 0 {
for i, f := range fields {
if checkFieldsEqual(db, table, field, f.DBName, f.TableName, f.Name) {
indices = append(indices, i)
continue
}
}
}
return indices
}
// GetFieldIndex gets name index in Fields.
func GetFieldIndex(name string, fields []*Field, flag uint32) []int {
var indices []int
// Check origin field name.
if flag&OrgFieldNameFlag > 0 {
for i, f := range fields {
if CheckFieldsEqual(name, f.Expr.String()) {
indices = append(indices, i)
continue
}
}
}
// Check alias field name.
if flag&FieldNameFlag > 0 {
for i, f := range fields {
if CheckFieldsEqual(name, f.Name) {
indices = append(indices, i)
continue
}
}
}
return indices
}
// CheckFieldsEqual checks if xname and yname is equal.
// xname/yname in the pattern dbname.tablename.fieldname
// If any part of any argument is missing, it will ignore it and
// continue check other parts. So a.b.c equals b.c
func CheckFieldsEqual(xname, yname string) bool {
xdb, xtable, xfield := splitQualifiedName(xname)
ydb, ytable, yfield := splitQualifiedName(yname)
return checkFieldsEqual(xdb, xtable, xfield, ydb, ytable, yfield)
}
// See: https://dev.mysql.com/doc/refman/5.0/en/identifier-case-sensitivity.html
// TODO: check `lower_case_table_names` system variable.
func checkFieldsEqual(xdb, xtable, xfield, ydb, ytable, yfield string) bool {
if !strings.EqualFold(xfield, yfield) {
return false
}
if len(xtable) > 0 && len(ytable) > 0 {
if !strings.EqualFold(xtable, ytable) {
return false
}
}
if len(xdb) > 0 && len(ydb) > 0 {
if !strings.EqualFold(xdb, ydb) {
return false
}
}
return true
}
// CloneFieldByName clones a ResultField in ResultFields according to name.
func CloneFieldByName(name string, fields []*ResultField, flag uint32) (*ResultField, error) {
indices := GetResultFieldIndex(name, fields, flag)
if len(indices) == 0 {
return nil, errors.Errorf("unknown field %s", name)
}
return fields[indices[0]].Clone(), nil
}
// CheckWildcardField checks wildcard field, like `*` or `table.*` or `db.table.*`.
func CheckWildcardField(name string) (string, bool, error) {
_, table, field := splitQualifiedName(name)
return table, field == "*", nil
}