fix issue in Counter of package gdb

This commit is contained in:
John Guo
2021-07-13 19:37:16 +08:00
parent 3d4d3a763a
commit fbfc23211c
4 changed files with 33 additions and 23 deletions

View File

@ -510,29 +510,34 @@ func (c *Core) DoUpdate(ctx context.Context, link Link, table string, data inter
switch kind {
case reflect.Map, reflect.Struct:
var (
fields []string
dataMap = ConvertDataForTableRecord(data)
fields []string
dataMap = ConvertDataForTableRecord(data)
counterHandler = func(column string, counter Counter) {
if counter.Value != 0 {
var (
column = c.QuoteWord(column)
columnRef = c.QuoteWord(counter.Field)
columnVal = counter.Value
operator = "+"
)
if columnVal < 0 {
operator = "-"
columnVal = -columnVal
}
fields = append(fields, fmt.Sprintf("%s=%s%s?", column, columnRef, operator))
params = append(params, columnVal)
}
}
)
for k, v := range dataMap {
switch value := v.(type) {
case *Counter:
if value.Value != 0 {
column := k
if value.Field != "" {
column = c.QuoteWord(value.Field)
}
fields = append(fields, fmt.Sprintf("%s=%s+?", column, column))
params = append(params, value.Value)
}
counterHandler(k, *value)
case Counter:
if value.Value != 0 {
column := k
if value.Field != "" {
column = c.QuoteWord(value.Field)
}
fields = append(fields, fmt.Sprintf("%s=%s+?", column, column))
params = append(params, value.Value)
}
counterHandler(k, value)
default:
if s, ok := v.(Raw); ok {
fields = append(fields, c.QuoteWord(k)+"="+gconv.String(s))
@ -543,6 +548,7 @@ func (c *Core) DoUpdate(ctx context.Context, link Link, table string, data inter
}
}
updates = strings.Join(fields, ",")
default:
updates = gconv.String(data)
}

View File

@ -814,7 +814,9 @@ func formatError(err error, sql string, args ...interface{}) error {
func FormatSqlWithArgs(sql string, args []interface{}) string {
index := -1
newQuery, _ := gregex.ReplaceStringFunc(
`(\?|:v\d+|\$\d+|@p\d+)`, sql, func(s string) string {
`(\?|:v\d+|\$\d+|@p\d+)`,
sql,
func(s string) string {
index++
if len(args) > index {
if args[index] == nil {
@ -834,6 +836,7 @@ func FormatSqlWithArgs(sql string, args []interface{}) string {
switch kind {
case reflect.String, reflect.Map, reflect.Slice, reflect.Array:
return `'` + gstr.QuoteMeta(gconv.String(args[index]), `'`) + `'`
case reflect.Struct:
if t, ok := args[index].(time.Time); ok {
return `'` + t.Format(`2006-01-02 15:04:05`) + `'`

View File

@ -1436,7 +1436,7 @@ func Test_DB_UpdateCounter(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
gdbCounter := &gdb.Counter{
Field: "views",
Field: "id",
Value: 1,
}
updateData := g.Map{
@ -1449,7 +1449,7 @@ func Test_DB_UpdateCounter(t *testing.T) {
one, err := db.Model(tableName).Where("id", 1).One()
t.AssertNil(err)
t.Assert(one["id"].Int(), 1)
t.Assert(one["views"].Int(), 1)
t.Assert(one["views"].Int(), 2)
})
gtest.C(t, func(t *gtest.T) {
@ -1468,7 +1468,7 @@ func Test_DB_UpdateCounter(t *testing.T) {
one, err := db.Model(tableName).Where("id", 1).One()
t.AssertNil(err)
t.Assert(one["id"].Int(), 1)
t.Assert(one["views"].Int(), 0)
t.Assert(one["views"].Int(), 1)
})
}

View File

@ -71,8 +71,9 @@ func doPrint(ctx context.Context, content string, stack bool) {
buffer.WriteString(now())
buffer.WriteString(" [INTE] ")
buffer.WriteString(file())
buffer.WriteString(" ")
if s := traceIdStr(ctx); s != "" {
buffer.WriteString(" " + s)
buffer.WriteString(s + " ")
}
buffer.WriteString(content)
buffer.WriteString("\n")