feature model fix (#296)
* add raw stirng quote for sql field * remove unused code
This commit is contained in:
@@ -36,7 +36,7 @@ func genDelete(table Table, withCache bool) (string, string, error) {
|
||||
"lowerStartCamelPrimaryKey": stringx.From(table.PrimaryKey.Name.ToCamel()).Untitle(),
|
||||
"dataType": table.PrimaryKey.DataType,
|
||||
"keys": strings.Join(keySet.KeysStr(), "\n"),
|
||||
"originalPrimaryKey": table.PrimaryKey.Name.Source(),
|
||||
"originalPrimaryKey": wrapWithRawString(table.PrimaryKey.Name.Source()),
|
||||
"keyValues": strings.Join(keyVariableSet.KeysStr(), ", "),
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
@@ -19,7 +19,7 @@ func genFindOne(table Table, withCache bool) (string, string, error) {
|
||||
"withCache": withCache,
|
||||
"upperStartCamelObject": camel,
|
||||
"lowerStartCamelObject": stringx.From(camel).Untitle(),
|
||||
"originalPrimaryKey": table.PrimaryKey.Name.Source(),
|
||||
"originalPrimaryKey": wrapWithRawString(table.PrimaryKey.Name.Source()),
|
||||
"lowerStartCamelPrimaryKey": stringx.From(table.PrimaryKey.Name.ToCamel()).Untitle(),
|
||||
"dataType": table.PrimaryKey.DataType,
|
||||
"cacheKey": table.CacheKey[table.PrimaryKey.Name.Source()].KeyExpression,
|
||||
|
||||
@@ -39,7 +39,7 @@ func genFindOneByField(table Table, withCache bool) (*findOneCode, error) {
|
||||
"lowerStartCamelObject": stringx.From(camelTableName).Untitle(),
|
||||
"lowerStartCamelField": stringx.From(camelFieldName).Untitle(),
|
||||
"upperStartCamelPrimaryKey": table.PrimaryKey.Name.ToCamel(),
|
||||
"originalField": field.Name.Source(),
|
||||
"originalField": wrapWithRawString(field.Name.Source()),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -82,7 +82,7 @@ func genFindOneByField(table Table, withCache bool) (*findOneCode, error) {
|
||||
"upperStartCamelObject": camelTableName,
|
||||
"primaryKeyLeft": table.CacheKey[table.PrimaryKey.Name.Source()].Left,
|
||||
"lowerStartCamelObject": stringx.From(camelTableName).Untitle(),
|
||||
"originalPrimaryField": table.PrimaryKey.Name.Source(),
|
||||
"originalPrimaryField": wrapWithRawString(table.PrimaryKey.Name.Source()),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -21,9 +21,6 @@ import (
|
||||
const (
|
||||
pwd = "."
|
||||
createTableFlag = `(?m)^(?i)CREATE\s+TABLE` // ignore case
|
||||
NamingLower = "lower"
|
||||
NamingCamel = "camel"
|
||||
NamingSnake = "snake"
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -280,3 +277,20 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, er
|
||||
|
||||
return output.String(), nil
|
||||
}
|
||||
|
||||
func wrapWithRawString(v string) string {
|
||||
if v == "`" {
|
||||
return v
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(v, "`") {
|
||||
v = "`" + v
|
||||
}
|
||||
|
||||
if !strings.HasSuffix(v, "`") {
|
||||
v = v + "`"
|
||||
} else if len(v) == 1 {
|
||||
v = v + "`"
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
@@ -1,13 +1,18 @@
|
||||
package gen
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/tal-tech/go-zero/core/logx"
|
||||
"github.com/tal-tech/go-zero/core/stringx"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/config"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/model/sql/builderx"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -79,3 +84,32 @@ func TestNamingModel(t *testing.T) {
|
||||
return err == nil
|
||||
}())
|
||||
}
|
||||
|
||||
func TestWrapWithRawString(t *testing.T) {
|
||||
assert.Equal(t, "``", wrapWithRawString(""))
|
||||
assert.Equal(t, "``", wrapWithRawString("``"))
|
||||
assert.Equal(t, "`a`", wrapWithRawString("a"))
|
||||
assert.Equal(t, "` `", wrapWithRawString(" "))
|
||||
}
|
||||
|
||||
func TestFields(t *testing.T) {
|
||||
type Student struct {
|
||||
Id int64 `db:"id"`
|
||||
Name string `db:"name"`
|
||||
Age sql.NullInt64 `db:"age"`
|
||||
Score sql.NullFloat64 `db:"score"`
|
||||
CreateTime time.Time `db:"create_time"`
|
||||
UpdateTime sql.NullTime `db:"update_time"`
|
||||
}
|
||||
var (
|
||||
studentFieldNames = builderx.FieldNames(&Student{})
|
||||
studentRows = strings.Join(studentFieldNames, ",")
|
||||
studentRowsExpectAutoSet = strings.Join(stringx.Remove(studentFieldNames, "`id`", "`create_time`", "`update_time`"), ",")
|
||||
studentRowsWithPlaceHolder = strings.Join(stringx.Remove(studentFieldNames, "`id`", "`create_time`", "`update_time`"), "=?,") + "=?"
|
||||
)
|
||||
|
||||
assert.Equal(t, []string{"`id`", "`name`", "`age`", "`score`", "`create_time`", "`update_time`"}, studentFieldNames)
|
||||
assert.Equal(t, "`id`,`name`,`age`,`score`,`create_time`,`update_time`", studentRows)
|
||||
assert.Equal(t, "`name`,`age`,`score`", studentRowsExpectAutoSet)
|
||||
assert.Equal(t, "`name`=?,`age`=?,`score`=?", studentRowsWithPlaceHolder)
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ func genNew(table Table, withCache bool) (string, error) {
|
||||
output, err := util.With("new").
|
||||
Parse(text).
|
||||
Execute(map[string]interface{}{
|
||||
"table": table.Name.Source(),
|
||||
"table": wrapWithRawString(table.Name.Source()),
|
||||
"withCache": withCache,
|
||||
"upperStartCamelObject": table.Name.ToCamel(),
|
||||
})
|
||||
|
||||
@@ -35,7 +35,7 @@ func genUpdate(table Table, withCache bool) (string, string, error) {
|
||||
"primaryCacheKey": table.CacheKey[table.PrimaryKey.Name.Source()].DataKeyExpression,
|
||||
"primaryKeyVariable": table.CacheKey[table.PrimaryKey.Name.Source()].Variable,
|
||||
"lowerStartCamelObject": stringx.From(camelTableName).Untitle(),
|
||||
"originalPrimaryKey": table.PrimaryKey.Name.Source(),
|
||||
"originalPrimaryKey": wrapWithRawString(table.PrimaryKey.Name.Source()),
|
||||
"expressionValues": strings.Join(expressionValues, ", "),
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
@@ -27,7 +27,7 @@ func genVars(table Table, withCache bool) (string, error) {
|
||||
"upperStartCamelObject": camel,
|
||||
"cacheKeys": strings.Join(keys, "\n"),
|
||||
"autoIncrement": table.PrimaryKey.AutoIncrement,
|
||||
"originalPrimaryKey": table.PrimaryKey.Name.Source(),
|
||||
"originalPrimaryKey": wrapWithRawString(table.PrimaryKey.Name.Source()),
|
||||
"withCache": withCache,
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user