forked from sunary/sqlize
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlite.go
More file actions
168 lines (137 loc) · 3.88 KB
/
Copy pathsqlite.go
File metadata and controls
168 lines (137 loc) · 3.88 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
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
package sql_parser
import (
"strings"
"github.com/pingcap/parser/ast"
sqlite "github.com/rqlite/sql"
"github.com/sunary/sqlize/element"
)
// ParserSqlite ...
func (p *Parser) ParserSqlite(sql string) error {
ps := sqlite.NewParser(strings.NewReader(sql))
node, err := ps.ParseStatement()
if err != nil {
return err
}
return sqlite.Walk(p, node)
}
/*
Walk with statements:
CreateTableStatement
CreateIndexStatement
DropTableStatement
DropIndexStatement
AlterTableStatement
sqlite does not support drop column
*/
func (p Parser) Visit(node sqlite.Node) (w sqlite.Visitor, err error) {
switch n := node.(type) {
case *sqlite.CreateTableStatement:
tbName := n.Table.String()
tb := element.NewTableWithAction(tbName, element.MigrateAddAction)
p.Migration.AddTable(*tb)
p.Migration.Using(tbName)
for i := range n.Columns {
col := element.Column{
Node: element.Node{
Name: n.Columns[i].Name.String(),
Action: element.MigrateAddAction,
},
LiteType: n.Columns[i].Type,
Options: p.parseSqliteConstrains(tbName, n.Columns[i].Constraints),
}
p.Migration.AddColumn(tbName, col)
}
for _, cons := range n.Constraints {
switch cons := cons.(type) {
case *sqlite.UniqueConstraint:
indexCol := make([]string, len(cons.Columns))
for i := range cons.Columns {
indexCol[i] = cons.Columns[i].Name
}
p.Migration.AddIndex(tbName, element.Index{
Node: element.Node{
Name: cons.Name.Name,
Action: element.MigrateAddAction,
},
Columns: indexCol,
Typ: ast.IndexKeyTypeUnique,
})
case *sqlite.ForeignKeyConstraint:
// p.Migration.AddForeignKey(tbName, element.ForeignKey{})
}
}
case *sqlite.CreateIndexStatement:
tbName := n.Table.String()
indexCol := make([]string, len(n.Columns))
for i := range n.Columns {
indexCol[i] = n.Columns[i].String()
}
typ := ast.IndexKeyTypeNone
if n.Unique.IsValid() {
typ = ast.IndexKeyTypeUnique
}
p.Migration.AddIndex(tbName, element.Index{
Node: element.Node{
Name: n.Name.Name,
Action: element.MigrateAddAction,
},
Columns: indexCol,
Typ: typ,
})
case *sqlite.DropTableStatement:
tbName := n.Table.String()
p.Migration.RemoveTable(tbName)
case *sqlite.DropIndexStatement:
case *sqlite.AlterTableStatement:
tbName := n.Table.String()
switch {
case n.Rename.IsValid():
p.Migration.RenameTable(tbName, n.NewName.Name)
case n.RenameColumn.IsValid():
p.Migration.RenameColumn(tbName, n.ColumnName.String(), n.NewColumnName.String())
case n.AddColumn.IsValid():
p.Migration.AddColumn(tbName, element.Column{
Node: element.Node{
Name: n.ColumnDef.Name.String(),
Action: element.MigrateAddAction,
},
LiteType: n.ColumnDef.Type,
Options: p.parseSqliteConstrains(tbName, n.ColumnDef.Constraints),
})
}
}
return nil, nil
}
func (p *Parser) parseSqliteConstrains(tbName string, conss []sqlite.Constraint) []*ast.ColumnOption {
opts := []*ast.ColumnOption{}
for _, cons := range conss {
switch cons := cons.(type) {
case *sqlite.PrimaryKeyConstraint:
opts = append(opts, &ast.ColumnOption{Tp: ast.ColumnOptionPrimaryKey})
case *sqlite.NotNullConstraint:
opts = append(opts, &ast.ColumnOption{Tp: ast.ColumnOptionNotNull})
case *sqlite.UniqueConstraint:
indexCol := make([]string, len(cons.Columns))
for i := range cons.Columns {
indexCol[i] = cons.Columns[i].Name
}
p.Migration.AddIndex(tbName, element.Index{
Node: element.Node{
Name: cons.Name.Name,
Action: element.MigrateAddAction,
},
Columns: indexCol,
Typ: ast.IndexKeyTypeUnique,
})
case *sqlite.CheckConstraint:
case *sqlite.DefaultConstraint:
opts = append(opts, &ast.ColumnOption{
Tp: ast.ColumnOptionDefaultValue,
StrValue: cons.Default.String()})
}
}
return opts
}
func (p Parser) VisitEnd(node sqlite.Node) error {
return nil
}