From f5f2097d147369b1ed2d2a1b6b36435fcbf0bead Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A1=8C=E8=80=85?= <1243463730@qq.com> Date: Sun, 26 Dec 2021 21:56:04 +0800 Subject: [PATCH] =?UTF-8?q?Updated=20MySQL=E7=94=9F=E6=88=90=E8=A1=A8?= =?UTF-8?q?=E7=BB=93=E6=9E=84=E4=BD=93=E9=81=87=E5=88=B0=E5=85=B3=E9=94=AE?= =?UTF-8?q?=E5=AD=97db=E9=83=A8=E5=88=86=E4=BF=9D=E6=8C=81=E5=8E=9F?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=E5=90=8D=E5=AE=9A=E4=B9=89=20(#1369)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tools/goctl/model/sql/gen/field.go | 2 +- tools/goctl/model/sql/parser/parser.go | 21 +++++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/tools/goctl/model/sql/gen/field.go b/tools/goctl/model/sql/gen/field.go index e319bb33..c4a96ffd 100644 --- a/tools/goctl/model/sql/gen/field.go +++ b/tools/goctl/model/sql/gen/field.go @@ -24,7 +24,7 @@ func genFields(fields []*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 { return "", err } diff --git a/tools/goctl/model/sql/parser/parser.go b/tools/goctl/model/sql/parser/parser.go index eb46882c..e220dd06 100644 --- a/tools/goctl/model/sql/parser/parser.go +++ b/tools/goctl/model/sql/parser/parser.go @@ -36,6 +36,7 @@ type ( // Field describes a table field Field struct { + NameOriginal string Name stringx.String DataType string Comment string @@ -47,6 +48,19 @@ type ( 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 func Parse(filename, database string) ([]*Table, error) { p := parser.NewParser() @@ -55,6 +69,8 @@ func Parse(filename, database string) ([]*Table, error) { return nil, err } + nameOriginals := parseNameOriginal(ts) + tables := GetSafeTables(ts) indexNameGen := func(column ...string) string { return strings.Join(column, "_") @@ -62,7 +78,7 @@ func Parse(filename, database string) ([]*Table, error) { prefix := filepath.Base(filename) var list []*Table - for _, e := range tables { + for indexTable, e := range tables { columns := e.Columns var ( @@ -120,9 +136,10 @@ func Parse(filename, database string) ([]*Table, error) { var fields []*Field // sort - for _, c := range columns { + for indexColumn, c := range columns { field, ok := fieldM[c.Name] if ok { + field.NameOriginal = nameOriginals[indexTable][indexColumn] fields = append(fields, field) } }