From 8dd764679c95853409b5e4ced99c8b4fce7b31d2 Mon Sep 17 00:00:00 2001 From: anqiansong Date: Thu, 7 Apr 2022 10:40:21 +0800 Subject: [PATCH] Fix #1765 (#1767) Co-authored-by: anqiansong --- tools/goctl/model/sql/example/sql/user.sql | 4 +- tools/goctl/model/sql/gen/delete.go | 4 +- tools/goctl/model/sql/gen/findone.go | 4 +- tools/goctl/model/sql/gen/findonebyfield.go | 4 +- tools/goctl/model/sql/gen/keys.go | 14 +++---- .../goctl/model/sql/model/infoschemamodel.go | 6 +-- tools/goctl/model/sql/parser/parser.go | 38 +------------------ 7 files changed, 19 insertions(+), 55 deletions(-) diff --git a/tools/goctl/model/sql/example/sql/user.sql b/tools/goctl/model/sql/example/sql/user.sql index 0e705744..48531dc3 100644 --- a/tools/goctl/model/sql/example/sql/user.sql +++ b/tools/goctl/model/sql/example/sql/user.sql @@ -21,7 +21,7 @@ CREATE TABLE `user` CREATE TABLE `student` ( - `id` bigint NOT NULL, + `type` bigint NOT NULL, `class` varchar(255) COLLATE utf8mb4_bin NOT NULL DEFAULT '', `name` varchar(255) COLLATE utf8mb4_bin NOT NULL DEFAULT '', `age` tinyint DEFAULT NULL, @@ -29,6 +29,6 @@ CREATE TABLE `student` ) DEFAULT NULL, `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `update_time` timestamp NULL DEFAULT NULL, - PRIMARY KEY (`id`) USING BTREE, + PRIMARY KEY (`type`) USING BTREE, UNIQUE KEY `class_name_index` (`class`,`name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; \ No newline at end of file diff --git a/tools/goctl/model/sql/gen/delete.go b/tools/goctl/model/sql/gen/delete.go index 559741ea..55af120d 100644 --- a/tools/goctl/model/sql/gen/delete.go +++ b/tools/goctl/model/sql/gen/delete.go @@ -32,7 +32,7 @@ func genDelete(table Table, withCache, postgreSql bool) (string, string, error) "upperStartCamelObject": camel, "withCache": withCache, "containsIndexCache": table.ContainsUniqueCacheKey, - "lowerStartCamelPrimaryKey": stringx.From(table.PrimaryKey.Name.ToCamel()).Untitle(), + "lowerStartCamelPrimaryKey": util.EscapeGolangKeyword(stringx.From(table.PrimaryKey.Name.ToCamel()).Untitle()), "dataType": table.PrimaryKey.DataType, "keys": strings.Join(keySet.KeysStr(), "\n"), "originalPrimaryKey": wrapWithRawString(table.PrimaryKey.Name.Source(), postgreSql), @@ -53,7 +53,7 @@ func genDelete(table Table, withCache, postgreSql bool) (string, string, error) deleteMethodOut, err := util.With("deleteMethod"). Parse(text). Execute(map[string]interface{}{ - "lowerStartCamelPrimaryKey": stringx.From(table.PrimaryKey.Name.ToCamel()).Untitle(), + "lowerStartCamelPrimaryKey": util.EscapeGolangKeyword(stringx.From(table.PrimaryKey.Name.ToCamel()).Untitle()), "dataType": table.PrimaryKey.DataType, "data": table, }) diff --git a/tools/goctl/model/sql/gen/findone.go b/tools/goctl/model/sql/gen/findone.go index 814a8f7f..a2063e85 100644 --- a/tools/goctl/model/sql/gen/findone.go +++ b/tools/goctl/model/sql/gen/findone.go @@ -21,7 +21,7 @@ func genFindOne(table Table, withCache, postgreSql bool) (string, string, error) "upperStartCamelObject": camel, "lowerStartCamelObject": stringx.From(camel).Untitle(), "originalPrimaryKey": wrapWithRawString(table.PrimaryKey.Name.Source(), postgreSql), - "lowerStartCamelPrimaryKey": stringx.From(table.PrimaryKey.Name.ToCamel()).Untitle(), + "lowerStartCamelPrimaryKey": util.EscapeGolangKeyword(stringx.From(table.PrimaryKey.Name.ToCamel()).Untitle()), "dataType": table.PrimaryKey.DataType, "cacheKey": table.PrimaryCacheKey.KeyExpression, "cacheKeyVariable": table.PrimaryCacheKey.KeyLeft, @@ -41,7 +41,7 @@ func genFindOne(table Table, withCache, postgreSql bool) (string, string, error) Parse(text). Execute(map[string]interface{}{ "upperStartCamelObject": camel, - "lowerStartCamelPrimaryKey": stringx.From(table.PrimaryKey.Name.ToCamel()).Untitle(), + "lowerStartCamelPrimaryKey": util.EscapeGolangKeyword(stringx.From(table.PrimaryKey.Name.ToCamel()).Untitle()), "dataType": table.PrimaryKey.DataType, "data": table, }) diff --git a/tools/goctl/model/sql/gen/findonebyfield.go b/tools/goctl/model/sql/gen/findonebyfield.go index 1d21d741..97abf4cf 100644 --- a/tools/goctl/model/sql/gen/findonebyfield.go +++ b/tools/goctl/model/sql/gen/findonebyfield.go @@ -59,7 +59,7 @@ func genFindOneByField(table Table, withCache, postgreSql bool) (*findOneCode, e for _, key := range table.UniqueCacheKey { var inJoin, paramJoin Join for _, f := range key.Fields { - param := stringx.From(f.Name.ToCamel()).Untitle() + param := util.EscapeGolangKeyword(stringx.From(f.Name.ToCamel()).Untitle()) inJoin = append(inJoin, fmt.Sprintf("%s %s", param, f.DataType)) paramJoin = append(paramJoin, param) } @@ -115,7 +115,7 @@ func genFindOneByField(table Table, withCache, postgreSql bool) (*findOneCode, e func convertJoin(key Key, postgreSql bool) (in, paramJoinString, originalFieldString string) { var inJoin, paramJoin, argJoin Join for index, f := range key.Fields { - param := stringx.From(f.Name.ToCamel()).Untitle() + param := util.EscapeGolangKeyword(stringx.From(f.Name.ToCamel()).Untitle()) inJoin = append(inJoin, fmt.Sprintf("%s %s", param, f.DataType)) paramJoin = append(paramJoin, param) if postgreSql { diff --git a/tools/goctl/model/sql/gen/keys.go b/tools/goctl/model/sql/gen/keys.go index 17c71e16..2a19ff47 100644 --- a/tools/goctl/model/sql/gen/keys.go +++ b/tools/goctl/model/sql/gen/keys.go @@ -53,8 +53,8 @@ func genCacheKeys(table parser.Table) (Key, []Key) { func genCacheKey(db, table stringx.String, in []*parser.Field) Key { var ( - varLeftJoin, varRightJon, fieldNameJoin Join - varLeft, varRight, varExpression string + varLeftJoin, varRightJoin, fieldNameJoin Join + varLeft, varRight, varExpression string keyLeftJoin, keyRightJoin, keyRightArgJoin, dataRightJoin Join keyLeft, keyRight, dataKeyRight, keyExpression, dataKeyExpression string @@ -63,19 +63,19 @@ func genCacheKey(db, table stringx.String, in []*parser.Field) Key { dbName, tableName := util.SafeString(db.Source()), util.SafeString(table.Source()) if len(dbName) > 0 { varLeftJoin = append(varLeftJoin, "cache", dbName, tableName) - varRightJon = append(varRightJon, "cache", dbName, tableName) + varRightJoin = append(varRightJoin, "cache", dbName, tableName) keyLeftJoin = append(keyLeftJoin, dbName, tableName) } else { varLeftJoin = append(varLeftJoin, "cache", tableName) - varRightJon = append(varRightJon, "cache", tableName) + varRightJoin = append(varRightJoin, "cache", tableName) keyLeftJoin = append(keyLeftJoin, tableName) } for _, each := range in { varLeftJoin = append(varLeftJoin, each.Name.Source()) - varRightJon = append(varRightJon, each.Name.Source()) + varRightJoin = append(varRightJoin, each.Name.Source()) keyLeftJoin = append(keyLeftJoin, each.Name.Source()) - keyRightJoin = append(keyRightJoin, stringx.From(each.Name.ToCamel()).Untitle()) + keyRightJoin = append(keyRightJoin, util.EscapeGolangKeyword(stringx.From(each.Name.ToCamel()).Untitle())) keyRightArgJoin = append(keyRightArgJoin, "%v") dataRightJoin = append(dataRightJoin, "data."+each.Name.ToCamel()) fieldNameJoin = append(fieldNameJoin, each.Name.Source()) @@ -84,7 +84,7 @@ func genCacheKey(db, table stringx.String, in []*parser.Field) Key { keyLeftJoin = append(keyLeftJoin, "key") varLeft = util.SafeString(varLeftJoin.Camel().With("").Untitle()) - varRight = fmt.Sprintf(`"%s"`, varRightJon.Camel().Untitle().With(":").Source()+":") + varRight = fmt.Sprintf(`"%s"`, varRightJoin.Camel().Untitle().With(":").Source()+":") varExpression = fmt.Sprintf(`%s = %s`, varLeft, varRight) keyLeft = util.SafeString(keyLeftJoin.Camel().With("").Untitle()) diff --git a/tools/goctl/model/sql/model/infoschemamodel.go b/tools/goctl/model/sql/model/infoschemamodel.go index 6480951a..8a204939 100644 --- a/tools/goctl/model/sql/model/infoschemamodel.go +++ b/tools/goctl/model/sql/model/infoschemamodel.go @@ -6,7 +6,6 @@ import ( "github.com/zeromicro/go-zero/core/stores/sqlx" "github.com/zeromicro/go-zero/tools/goctl/model/sql/util" - su "github.com/zeromicro/go-zero/tools/goctl/util" ) const indexPri = "PRIMARY" @@ -145,15 +144,14 @@ func (m *InformationSchemaModel) FindIndex(db, table, column string) ([]*DbIndex // Convert converts column data into Table func (c *ColumnData) Convert() (*Table, error) { var table Table - table.Table = su.EscapeGolangKeyword(c.Table) - table.Db = su.EscapeGolangKeyword(c.Db) + table.Table = c.Table + table.Db = c.Db table.Columns = c.Columns table.UniqueIndex = map[string][]*Column{} table.NormalIndex = map[string][]*Column{} m := make(map[string][]*Column) for _, each := range c.Columns { - each.Name = su.EscapeGolangKeyword(each.Name) each.Comment = util.TrimNewLine(each.Comment) if each.Index != nil { m[each.Index.IndexName] = append(m[each.Index.IndexName], each) diff --git a/tools/goctl/model/sql/parser/parser.go b/tools/goctl/model/sql/parser/parser.go index ab992083..37db2690 100644 --- a/tools/goctl/model/sql/parser/parser.go +++ b/tools/goctl/model/sql/parser/parser.go @@ -11,7 +11,6 @@ import ( "github.com/zeromicro/go-zero/tools/goctl/model/sql/converter" "github.com/zeromicro/go-zero/tools/goctl/model/sql/model" "github.com/zeromicro/go-zero/tools/goctl/model/sql/util" - su "github.com/zeromicro/go-zero/tools/goctl/util" "github.com/zeromicro/go-zero/tools/goctl/util/console" "github.com/zeromicro/go-zero/tools/goctl/util/stringx" ) @@ -64,14 +63,13 @@ func parseNameOriginal(ts []*parser.Table) (nameOriginals [][]string) { // Parse parses ddl into golang structure func Parse(filename, database string) ([]*Table, error) { p := parser.NewParser() - ts, err := p.From(filename) + tables, err := p.From(filename) if err != nil { return nil, err } - nameOriginals := parseNameOriginal(ts) + nameOriginals := parseNameOriginal(tables) - tables := GetSafeTables(ts) indexNameGen := func(column ...string) string { return strings.Join(column, "_") } @@ -371,35 +369,3 @@ func getTableFields(table *model.Table) (map[string]*Field, error) { } return fieldM, nil } - -// GetSafeTables escapes the golang keywords from sql tables. -func GetSafeTables(tables []*parser.Table) []*parser.Table { - var list []*parser.Table - for _, t := range tables { - table := GetSafeTable(t) - list = append(list, table) - } - - return list -} - -// GetSafeTable escapes the golang keywords from sql table. -func GetSafeTable(table *parser.Table) *parser.Table { - table.Name = su.EscapeGolangKeyword(table.Name) - for _, c := range table.Columns { - c.Name = su.EscapeGolangKeyword(c.Name) - } - - for _, e := range table.Constraints { - var uniqueKeys, primaryKeys []string - for _, u := range e.ColumnUniqueKey { - uniqueKeys = append(uniqueKeys, su.EscapeGolangKeyword(u)) - } - for _, p := range e.ColumnPrimaryKey { - primaryKeys = append(primaryKeys, su.EscapeGolangKeyword(p)) - } - e.ColumnUniqueKey = uniqueKeys - e.ColumnPrimaryKey = primaryKeys - } - return table -}