-
Notifications
You must be signed in to change notification settings - Fork 218
Expand file tree
/
Copy pathcommon.go
More file actions
88 lines (76 loc) · 2.04 KB
/
common.go
File metadata and controls
88 lines (76 loc) · 2.04 KB
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
/*
* Radon
*
* Copyright 2018 The Radon Authors.
* Code is licensed under the GPLv3.
*
*/
package builder
import (
"strings"
"router"
"github.com/pkg/errors"
"github.com/xelabs/go-mysqlstack/sqlparser"
)
func checkTbName(tbInfos map[string]*tableInfo, node sqlparser.SQLNode) error {
return sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) {
if col, ok := node.(*sqlparser.ColName); ok {
tableName := col.Qualifier.Name.String()
if tableName != "" {
if _, ok := tbInfos[tableName]; !ok {
buf := sqlparser.NewTrackedBuffer(nil)
col.Format(buf)
return false, errors.Errorf("unsupported: unknown.column.'%s'.in.exprs", buf.String())
}
}
}
return true, nil
}, node)
}
// checkTbInNode used to check whether the filter's referTables in the tbInfos.
func checkTbInNode(referTables []string, tbInfos map[string]*tableInfo) bool {
if len(referTables) == 0 {
return true
}
for _, tb := range referTables {
if _, ok := tbInfos[tb]; !ok {
return false
}
}
return true
}
func isContainKey(a []string, b string) bool {
for _, c := range a {
if c == b {
return true
}
}
return false
}
// fetchIndex used to fetch index from router.
func fetchIndex(tbInfo *tableInfo, val *sqlparser.SQLVal, router *router.Router) error {
idx, err := router.GetIndex(tbInfo.database, tbInfo.tableName, val)
if err != nil {
return err
}
tbInfo.parent.indexes = append(tbInfo.parent.indexes, idx)
return nil
}
// checkShard used to check whether the col is shardkey.
func checkShard(table, col string, tbInfos map[string]*tableInfo, router *router.Router) (bool, error) {
tbInfo, ok := tbInfos[table]
if !ok {
return false, errors.Errorf("unsupported: unknown.column.'%s.%s'.in.field.list", table, col)
}
if tbInfo.shardKey != "" && strings.EqualFold(tbInfo.shardKey, col) {
return true, nil
}
return false, nil
}
// getOneTableInfo get a tableInfo.
func getOneTableInfo(tbInfos map[string]*tableInfo) (string, *tableInfo) {
for tb, tbInfo := range tbInfos {
return tb, tbInfo
}
return "", nil
}