-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathutils.go
More file actions
98 lines (84 loc) · 1.85 KB
/
utils.go
File metadata and controls
98 lines (84 loc) · 1.85 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
89
90
91
92
93
94
95
96
97
98
package dolphin
import (
pcast "github.com/pingcap/parser/ast"
"github.com/kyleconroy/sqlc/internal/sql/ast"
)
type nodeSearch struct {
list []pcast.Node
check func(pcast.Node) bool
}
func (s *nodeSearch) Enter(n pcast.Node) (pcast.Node, bool) {
if s.check(n) {
s.list = append(s.list, n)
}
return n, false // skipChildren
}
func (s *nodeSearch) Leave(n pcast.Node) (pcast.Node, bool) {
return n, true // ok
}
func collect(root pcast.Node, f func(pcast.Node) bool) []pcast.Node {
if root == nil {
return nil
}
ns := &nodeSearch{check: f}
root.Accept(ns)
return ns.list
}
type nodeVisit struct {
fn func(pcast.Node)
}
func (s *nodeVisit) Enter(n pcast.Node) (pcast.Node, bool) {
s.fn(n)
return n, false // skipChildren
}
func (s *nodeVisit) Leave(n pcast.Node) (pcast.Node, bool) {
return n, true // ok
}
func visit(root pcast.Node, f func(pcast.Node)) {
if root == nil {
return
}
ns := &nodeVisit{fn: f}
root.Accept(ns)
}
// Maybe not useful?
func text(nodes []pcast.Node) []string {
str := make([]string, len(nodes))
for i := range nodes {
if nodes[i] == nil {
continue
}
str[i] = nodes[i].Text()
}
return str
}
func parseTableName(n *pcast.TableName) *ast.TableName {
return &ast.TableName{
Schema: identifier(n.Schema.String()),
Name: identifier(n.Name.String()),
}
}
func toList(node pcast.Node) *ast.List {
var items []ast.Node
switch n := node.(type) {
case *pcast.TableName:
if schema := n.Schema.String(); schema != "" {
items = append(items, NewIdentifer(schema))
}
items = append(items, NewIdentifer(n.Name.String()))
default:
return nil
}
return &ast.List{Items: items}
}
func isNotNull(n *pcast.ColumnDef) bool {
for i := range n.Options {
if n.Options[i].Tp == pcast.ColumnOptionNotNull {
return true
}
if n.Options[i].Tp == pcast.ColumnOptionPrimaryKey {
return true
}
}
return false
}