-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstmt.go
102 lines (89 loc) · 2.52 KB
/
stmt.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
package ddlm2s
import (
"fmt"
"github.com/k0kubun/pp"
"github.com/knocknote/vitess-sqlparser/sqlparser"
)
type CreateStatements []*CreateStatement
func (all CreateStatements) findBy(tableName string) *CreateStatement {
for _, c := range all {
if fmt.Sprintf("%s", c.stmt.DDL.NewName.Name) == tableName {
return c
}
}
panic("cant find stmt by tablename")
}
type CreateStatement struct {
stmt *sqlparser.CreateTable
indices []Index
}
func (c *CreateStatement) Print(debug bool) {
if debug {
fmt.Println("------------------------after-----------------------------")
pp.Print(c.stmt)
}
tbuf := sqlparser.NewTrackedBuffer(func(buf *sqlparser.TrackedBuffer, node sqlparser.SQLNode) {})
c.stmt.SpannerFormat(tbuf)
fmt.Printf("%s;\n", string(tbuf.Buffer.String()))
for _, index := range c.indices {
fmt.Println(index.CreateDdl())
}
}
func (c *CreateStatement) InterLeaveDepth(all CreateStatements) int {
depth := 0
parent := c.GetInterleaveParentStatement(all)
if parent != nil {
depth += 1
depth += parent.InterLeaveDepth(all)
}
return depth
}
func (c *CreateStatement) GetInterleaveParentStatement(all CreateStatements) *CreateStatement {
for _, constraint := range c.stmt.Constraints {
if constraint.Type == sqlparser.ConstraintInterleave {
return all.findBy(constraint.Name)
}
}
return nil
}
func (c *CreateStatement) GetPrimaryKey() *sqlparser.Constraint {
for _, constraint := range c.stmt.Constraints {
if constraint.Type == sqlparser.ConstraintPrimaryKey {
return constraint
}
}
return nil
}
func (c *CreateStatement) GetColumns(colKeys []sqlparser.ColIdent) []*sqlparser.ColumnDef {
var defs []*sqlparser.ColumnDef
for _, key := range colKeys {
strKey := fmt.Sprintf("%v", key)
for _, c := range c.stmt.Columns {
if c.Name == strKey {
defs = append(defs, c)
continue
}
}
}
return defs
}
func (c *CreateStatement) updatePrimaryKeyByInterleaveParent(all CreateStatements) {
parent := c.GetInterleaveParentStatement(all)
if parent == nil {
return
}
var newConstraints []*sqlparser.Constraint
for _, constraint := range c.stmt.Constraints {
if constraint.Type == sqlparser.ConstraintPrimaryKey {
constraint.Keys = append(parent.GetPrimaryKey().Keys, constraint.Keys...)
}
newConstraints = append(newConstraints, constraint)
}
c.stmt.Constraints = newConstraints
if c.InterLeaveDepth(all) > 1 {
pks := c.GetPrimaryKey().Keys
lackedPks := pks[:len(pks)-2]
lackedColumns := parent.GetColumns(lackedPks)
c.stmt.Columns = append(lackedColumns, c.stmt.Columns...)
}
}