mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-02-03 00:38:40 +08:00
Feature model fix (#362)
* fix sql builderx adding raw string quotation marks incompatibility bug * add unit test * remove comments * fix sql builderx adding raw string quotation marks incompatibility bug
This commit is contained in:
parent
57b73d8b49
commit
6c624a6ed0
@ -46,6 +46,7 @@ func ToMap(in interface{}) map[string]interface{} {
|
|||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// deprecated: use RawFieldNames instead automaticly while model generating after goctl version v1.1.0
|
||||||
func FieldNames(in interface{}) []string {
|
func FieldNames(in interface{}) []string {
|
||||||
out := make([]string, 0)
|
out := make([]string, 0)
|
||||||
v := reflect.ValueOf(in)
|
v := reflect.ValueOf(in)
|
||||||
@ -61,9 +62,32 @@ func FieldNames(in interface{}) []string {
|
|||||||
// gets us a StructField
|
// gets us a StructField
|
||||||
fi := typ.Field(i)
|
fi := typ.Field(i)
|
||||||
if tagv := fi.Tag.Get(dbTag); tagv != "" {
|
if tagv := fi.Tag.Get(dbTag); tagv != "" {
|
||||||
out = append(out, fmt.Sprintf("`%v`", tagv))
|
out = append(out, tagv)
|
||||||
} else {
|
} else {
|
||||||
out = append(out, fmt.Sprintf("`%v`", fi.Name))
|
out = append(out, fi.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func RawFieldNames(in interface{}) []string {
|
||||||
|
out := make([]string, 0)
|
||||||
|
v := reflect.ValueOf(in)
|
||||||
|
if v.Kind() == reflect.Ptr {
|
||||||
|
v = v.Elem()
|
||||||
|
}
|
||||||
|
// we only accept structs
|
||||||
|
if v.Kind() != reflect.Struct {
|
||||||
|
panic(fmt.Errorf("ToMap only accepts structs; got %T", v))
|
||||||
|
}
|
||||||
|
typ := v.Type()
|
||||||
|
for i := 0; i < v.NumField(); i++ {
|
||||||
|
// gets us a StructField
|
||||||
|
fi := typ.Field(i)
|
||||||
|
if tagv := fi.Tag.Get(dbTag); tagv != "" {
|
||||||
|
out = append(out, fmt.Sprintf("`%s`", tagv))
|
||||||
|
} else {
|
||||||
|
out = append(out, fmt.Sprintf(`"%s"`, fi.Name))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return out
|
return out
|
||||||
|
@ -23,13 +23,23 @@ type (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
var userFields = FieldNames(User{})
|
var userFieldsWithRawStringQuote = RawFieldNames(User{})
|
||||||
|
var userFieldsWithoutRawStringQuote = FieldNames(User{})
|
||||||
|
|
||||||
func TestFieldNames(t *testing.T) {
|
func TestFieldNames(t *testing.T) {
|
||||||
var u User
|
t.Run("old", func(t *testing.T) {
|
||||||
out := FieldNames(&u)
|
var u User
|
||||||
actual := []string{"`id`", "`user_name`", "`sex`", "`uuid`", "`age`"}
|
out := FieldNames(&u)
|
||||||
assert.Equal(t, out, actual)
|
expected := []string{"id", "user_name", "sex", "uuid", "age"}
|
||||||
|
assert.Equal(t, expected, out)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("new", func(t *testing.T) {
|
||||||
|
var u User
|
||||||
|
out := RawFieldNames(&u)
|
||||||
|
expected := []string{"`id`", "`user_name`", "`sex`", "`uuid`", "`age`"}
|
||||||
|
assert.Equal(t, expected, out)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNewEq(t *testing.T) {
|
func TestNewEq(t *testing.T) {
|
||||||
@ -48,7 +58,7 @@ func TestBuilderSql(t *testing.T) {
|
|||||||
u := &User{
|
u := &User{
|
||||||
Id: "123123",
|
Id: "123123",
|
||||||
}
|
}
|
||||||
fields := FieldNames(u)
|
fields := RawFieldNames(u)
|
||||||
eq := NewEq(u)
|
eq := NewEq(u)
|
||||||
sql, args, err := builder.Select(fields...).From("user").Where(eq).ToSQL()
|
sql, args, err := builder.Select(fields...).From("user").Where(eq).ToSQL()
|
||||||
fmt.Println(sql, args, err)
|
fmt.Println(sql, args, err)
|
||||||
@ -64,13 +74,25 @@ func TestBuildSqlDefaultValue(t *testing.T) {
|
|||||||
eq["age"] = 0
|
eq["age"] = 0
|
||||||
eq["user_name"] = ""
|
eq["user_name"] = ""
|
||||||
|
|
||||||
sql, args, err := builder.Select(userFields...).From("user").Where(eq).ToSQL()
|
t.Run("raw", func(t *testing.T) {
|
||||||
fmt.Println(sql, args, err)
|
sql, args, err := builder.Select(userFieldsWithRawStringQuote...).From("user").Where(eq).ToSQL()
|
||||||
|
fmt.Println(sql, args, err)
|
||||||
|
|
||||||
actualSql := "SELECT `id`,`user_name`,`sex`,`uuid`,`age` FROM user WHERE age=? AND user_name=?"
|
actualSql := "SELECT `id`,`user_name`,`sex`,`uuid`,`age` FROM user WHERE age=? AND user_name=?"
|
||||||
actualArgs := []interface{}{0, ""}
|
actualArgs := []interface{}{0, ""}
|
||||||
assert.Equal(t, sql, actualSql)
|
assert.Equal(t, sql, actualSql)
|
||||||
assert.Equal(t, args, actualArgs)
|
assert.Equal(t, args, actualArgs)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("withour raw quote", func(t *testing.T) {
|
||||||
|
sql, args, err := builder.Select(userFieldsWithoutRawStringQuote...).From("user").Where(eq).ToSQL()
|
||||||
|
fmt.Println(sql, args, err)
|
||||||
|
|
||||||
|
actualSql := "SELECT id,user_name,sex,uuid,age FROM user WHERE age=? AND user_name=?"
|
||||||
|
actualArgs := []interface{}{0, ""}
|
||||||
|
assert.Equal(t, sql, actualSql)
|
||||||
|
assert.Equal(t, args, actualArgs)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBuilderSqlIn(t *testing.T) {
|
func TestBuilderSqlIn(t *testing.T) {
|
||||||
@ -79,7 +101,7 @@ func TestBuilderSqlIn(t *testing.T) {
|
|||||||
}
|
}
|
||||||
gtU := NewGt(u)
|
gtU := NewGt(u)
|
||||||
in := builder.In("id", []string{"1", "2", "3"})
|
in := builder.In("id", []string{"1", "2", "3"})
|
||||||
sql, args, err := builder.Select(userFields...).From("user").Where(in).And(gtU).ToSQL()
|
sql, args, err := builder.Select(userFieldsWithRawStringQuote...).From("user").Where(in).And(gtU).ToSQL()
|
||||||
fmt.Println(sql, args, err)
|
fmt.Println(sql, args, err)
|
||||||
|
|
||||||
actualSql := "SELECT `id`,`user_name`,`sex`,`uuid`,`age` FROM user WHERE id IN (?,?,?) AND age>?"
|
actualSql := "SELECT `id`,`user_name`,`sex`,`uuid`,`age` FROM user WHERE id IN (?,?,?) AND age>?"
|
||||||
@ -90,7 +112,7 @@ func TestBuilderSqlIn(t *testing.T) {
|
|||||||
|
|
||||||
func TestBuildSqlLike(t *testing.T) {
|
func TestBuildSqlLike(t *testing.T) {
|
||||||
like := builder.Like{"name", "wang"}
|
like := builder.Like{"name", "wang"}
|
||||||
sql, args, err := builder.Select(userFields...).From("user").Where(like).ToSQL()
|
sql, args, err := builder.Select(userFieldsWithRawStringQuote...).From("user").Where(like).ToSQL()
|
||||||
fmt.Println(sql, args, err)
|
fmt.Println(sql, args, err)
|
||||||
|
|
||||||
actualSql := "SELECT `id`,`user_name`,`sex`,`uuid`,`age` FROM user WHERE name LIKE ?"
|
actualSql := "SELECT `id`,`user_name`,`sex`,`uuid`,`age` FROM user WHERE name LIKE ?"
|
||||||
|
@ -2,12 +2,12 @@
|
|||||||
|
|
||||||
# generate model with cache from ddl
|
# generate model with cache from ddl
|
||||||
fromDDLWithCache:
|
fromDDLWithCache:
|
||||||
goctl template clean;
|
goctl template clean
|
||||||
goctl model mysql ddl -src="./sql/*.sql" -dir="./sql/model/cache/user" -cache;
|
goctl model mysql ddl -src="./sql/*.sql" -dir="./sql/model/cache" -cache
|
||||||
|
|
||||||
fromDDLWithoutCache:
|
fromDDLWithoutCache:
|
||||||
goctl template clean;
|
goctl template clean;
|
||||||
goctl model mysql ddl -src="./sql/*.sql" -dir="./sql/model/nocache/user";
|
goctl model mysql ddl -src="./sql/*.sql" -dir="./sql/model/nocache"
|
||||||
|
|
||||||
|
|
||||||
# generate model with cache from data source
|
# generate model with cache from data source
|
||||||
@ -17,5 +17,5 @@ datasource=127.0.0.1:3306
|
|||||||
database=gozero
|
database=gozero
|
||||||
|
|
||||||
fromDataSource:
|
fromDataSource:
|
||||||
goctl template clean;
|
goctl template clean
|
||||||
goctl model mysql datasource -url="$(user):$(password)@tcp($(datasource))/$(database)" -table="*" -dir ./model/cache -c -style gozero;
|
goctl model mysql datasource -url="$(user):$(password)@tcp($(datasource))/$(database)" -table="*" -dir ./model/cache -c -style gozero
|
||||||
|
@ -102,7 +102,7 @@ func TestFields(t *testing.T) {
|
|||||||
UpdateTime sql.NullTime `db:"update_time"`
|
UpdateTime sql.NullTime `db:"update_time"`
|
||||||
}
|
}
|
||||||
var (
|
var (
|
||||||
studentFieldNames = builderx.FieldNames(&Student{})
|
studentFieldNames = builderx.RawFieldNames(&Student{})
|
||||||
studentRows = strings.Join(studentFieldNames, ",")
|
studentRows = strings.Join(studentFieldNames, ",")
|
||||||
studentRowsExpectAutoSet = strings.Join(stringx.Remove(studentFieldNames, "`id`", "`create_time`", "`update_time`"), ",")
|
studentRowsExpectAutoSet = strings.Join(stringx.Remove(studentFieldNames, "`id`", "`create_time`", "`update_time`"), ",")
|
||||||
studentRowsWithPlaceHolder = strings.Join(stringx.Remove(studentFieldNames, "`id`", "`create_time`", "`update_time`"), "=?,") + "=?"
|
studentRowsWithPlaceHolder = strings.Join(stringx.Remove(studentFieldNames, "`id`", "`create_time`", "`update_time`"), "=?,") + "=?"
|
||||||
|
@ -4,7 +4,7 @@ import "fmt"
|
|||||||
|
|
||||||
var Vars = fmt.Sprintf(`
|
var Vars = fmt.Sprintf(`
|
||||||
var (
|
var (
|
||||||
{{.lowerStartCamelObject}}FieldNames = builderx.FieldNames(&{{.upperStartCamelObject}}{})
|
{{.lowerStartCamelObject}}FieldNames = builderx.RawFieldNames(&{{.upperStartCamelObject}}{})
|
||||||
{{.lowerStartCamelObject}}Rows = strings.Join({{.lowerStartCamelObject}}FieldNames, ",")
|
{{.lowerStartCamelObject}}Rows = strings.Join({{.lowerStartCamelObject}}FieldNames, ",")
|
||||||
{{.lowerStartCamelObject}}RowsExpectAutoSet = strings.Join(stringx.Remove({{.lowerStartCamelObject}}FieldNames, {{if .autoIncrement}}"{{.originalPrimaryKey}}",{{end}} "%screate_time%s", "%supdate_time%s"), ",")
|
{{.lowerStartCamelObject}}RowsExpectAutoSet = strings.Join(stringx.Remove({{.lowerStartCamelObject}}FieldNames, {{if .autoIncrement}}"{{.originalPrimaryKey}}",{{end}} "%screate_time%s", "%supdate_time%s"), ",")
|
||||||
{{.lowerStartCamelObject}}RowsWithPlaceHolder = strings.Join(stringx.Remove({{.lowerStartCamelObject}}FieldNames, "{{.originalPrimaryKey}}", "%screate_time%s", "%supdate_time%s"), "=?,") + "=?"
|
{{.lowerStartCamelObject}}RowsWithPlaceHolder = strings.Join(stringx.Remove({{.lowerStartCamelObject}}FieldNames, "{{.originalPrimaryKey}}", "%screate_time%s", "%supdate_time%s"), "=?,") + "=?"
|
||||||
|
@ -14,7 +14,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
studentFieldNames = builderx.FieldNames(&Student{})
|
studentFieldNames = builderx.RawFieldNames(&Student{})
|
||||||
studentRows = strings.Join(studentFieldNames, ",")
|
studentRows = strings.Join(studentFieldNames, ",")
|
||||||
studentRowsExpectAutoSet = strings.Join(stringx.Remove(studentFieldNames, "`id`", "`create_time`", "`update_time`"), ",")
|
studentRowsExpectAutoSet = strings.Join(stringx.Remove(studentFieldNames, "`id`", "`create_time`", "`update_time`"), ",")
|
||||||
studentRowsWithPlaceHolder = strings.Join(stringx.Remove(studentFieldNames, "`id`", "`create_time`", "`update_time`"), "=?,") + "=?"
|
studentRowsWithPlaceHolder = strings.Join(stringx.Remove(studentFieldNames, "`id`", "`create_time`", "`update_time`"), "=?,") + "=?"
|
||||||
|
@ -15,8 +15,8 @@ import (
|
|||||||
var (
|
var (
|
||||||
userFieldNames = builderx.FieldNames(&User{})
|
userFieldNames = builderx.FieldNames(&User{})
|
||||||
userRows = strings.Join(userFieldNames, ",")
|
userRows = strings.Join(userFieldNames, ",")
|
||||||
userRowsExpectAutoSet = strings.Join(stringx.Remove(userFieldNames, "`id`", "`create_time`", "`update_time`"), ",")
|
userRowsExpectAutoSet = strings.Join(stringx.Remove(userFieldNames, "id", "create_time", "update_time"), ",")
|
||||||
userRowsWithPlaceHolder = strings.Join(stringx.Remove(userFieldNames, "`id`", "`create_time`", "`update_time`"), "=?,") + "=?"
|
userRowsWithPlaceHolder = strings.Join(stringx.Remove(userFieldNames, "id", "create_time", "update_time"), "=?,") + "=?"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
|
Loading…
Reference in New Issue
Block a user