-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
48 lines (41 loc) · 1.09 KB
/
parser.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
package parser
import (
"github.com/blastrain/vitess-sqlparser/sqlparser"
"github.com/hopeio/cherry/utils/dao/database/mysql"
dbi "github.com/hopeio/cherry/utils/dao/database/toentity"
)
func MysqlConvertByTable(sql string) {
gen := NewMysqlGen(sql)
dbi.ConvertByTable(gen, gen.Tables()[0])
}
type mysqlgen struct {
*sqlparser.CreateTable
}
func NewMysqlGen(sql string) *mysqlgen {
statement, _ := sqlparser.Parse(sql)
return &mysqlgen{statement.(*sqlparser.CreateTable)}
}
func (m *mysqlgen) Tables() []string {
return []string{m.DDL.NewName.Name.String()}
}
func (m *mysqlgen) Fields(tableName string) []*dbi.Field {
var dbfields []*dbi.Field
for i, column := range m.Columns {
dbfields = append(dbfields, &dbi.Field{
Field: column.Name,
Type: column.Type,
Comment: "",
GoTYpe: "",
})
for _, option := range column.Options {
if option.Type == sqlparser.ColumnOptionComment {
dbfields[i].Comment = option.Value[1 : len(option.Value)-1]
break
}
}
}
return dbfields
}
func (m *mysqlgen) TypeToGoTYpe(typ string) string {
return mysql.MySqlTypeToGoType(typ)
}