Updated MySQL生成表结构体遇到关键字db部分保持原字段名定义 (#1369)

master
行者 3 years ago committed by GitHub
parent cfcfb87fd4
commit f5f2097d14
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -24,7 +24,7 @@ func genFields(fields []*parser.Field) (string, error) {
} }
func genField(field *parser.Field) (string, error) { func genField(field *parser.Field) (string, error) {
tag, err := genTag(field.Name.Source()) tag, err := genTag(field.NameOriginal)
if err != nil { if err != nil {
return "", err return "", err
} }

@ -36,6 +36,7 @@ type (
// Field describes a table field // Field describes a table field
Field struct { Field struct {
NameOriginal string
Name stringx.String Name stringx.String
DataType string DataType string
Comment string Comment string
@ -47,6 +48,19 @@ type (
KeyType int KeyType int
) )
func parseNameOriginal(ts []*parser.Table) (nameOriginals [][]string) {
var columns []string
for _, t := range ts {
columns = []string{}
for _, c := range t.Columns {
columns = append(columns, c.Name)
}
nameOriginals = append(nameOriginals, columns)
}
return
}
// Parse parses ddl into golang structure // Parse parses ddl into golang structure
func Parse(filename, database string) ([]*Table, error) { func Parse(filename, database string) ([]*Table, error) {
p := parser.NewParser() p := parser.NewParser()
@ -55,6 +69,8 @@ func Parse(filename, database string) ([]*Table, error) {
return nil, err return nil, err
} }
nameOriginals := parseNameOriginal(ts)
tables := GetSafeTables(ts) tables := GetSafeTables(ts)
indexNameGen := func(column ...string) string { indexNameGen := func(column ...string) string {
return strings.Join(column, "_") return strings.Join(column, "_")
@ -62,7 +78,7 @@ func Parse(filename, database string) ([]*Table, error) {
prefix := filepath.Base(filename) prefix := filepath.Base(filename)
var list []*Table var list []*Table
for _, e := range tables { for indexTable, e := range tables {
columns := e.Columns columns := e.Columns
var ( var (
@ -120,9 +136,10 @@ func Parse(filename, database string) ([]*Table, error) {
var fields []*Field var fields []*Field
// sort // sort
for _, c := range columns { for indexColumn, c := range columns {
field, ok := fieldM[c.Name] field, ok := fieldM[c.Name]
if ok { if ok {
field.NameOriginal = nameOriginals[indexTable][indexColumn]
fields = append(fields, field) fields = append(fields, field)
} }
} }

Loading…
Cancel
Save