Co-authored-by: anqiansong <anqiansong@bytedance.com>
master
anqiansong 3 years ago committed by GitHub
parent 9fe868ade9
commit 8dd764679c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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;

@ -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,
})

@ -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,
})

@ -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 {

@ -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())

@ -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)

@ -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
}

Loading…
Cancel
Save