add support for map[string]interface{} as condition on Update and Where (#764)
This commit is contained in:
parent
43222cbcaf
commit
865979f716
2
error.go
2
error.go
|
@ -23,4 +23,6 @@ var (
|
||||||
ErrNeedDeletedCond = errors.New("Delete need at least one condition")
|
ErrNeedDeletedCond = errors.New("Delete need at least one condition")
|
||||||
// ErrNotImplemented not implemented
|
// ErrNotImplemented not implemented
|
||||||
ErrNotImplemented = errors.New("Not implemented")
|
ErrNotImplemented = errors.New("Not implemented")
|
||||||
|
// ErrConditionType condition type unsupported
|
||||||
|
ErrConditionType = errors.New("Unsupported conditon type")
|
||||||
)
|
)
|
||||||
|
|
|
@ -242,10 +242,23 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6
|
||||||
|
|
||||||
var autoCond builder.Cond
|
var autoCond builder.Cond
|
||||||
if !session.statement.noAutoCondition && len(condiBean) > 0 {
|
if !session.statement.noAutoCondition && len(condiBean) > 0 {
|
||||||
var err error
|
if c, ok := condiBean[0].(map[string]interface{}); ok {
|
||||||
autoCond, err = session.statement.buildConds(session.statement.RefTable, condiBean[0], true, true, false, true, false)
|
autoCond = builder.Eq(c)
|
||||||
if err != nil {
|
} else {
|
||||||
return 0, err
|
ct := reflect.TypeOf(condiBean[0])
|
||||||
|
k := ct.Kind()
|
||||||
|
if k == reflect.Ptr {
|
||||||
|
k = ct.Elem().Kind()
|
||||||
|
}
|
||||||
|
if k == reflect.Struct {
|
||||||
|
var err error
|
||||||
|
autoCond, err = session.statement.buildConds(session.statement.RefTable, condiBean[0], true, true, false, true, false)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return 0, ErrConditionType
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1215,3 +1215,34 @@ func TestCreatedUpdated2(t *testing.T) {
|
||||||
assert.True(t, s2.UpdateAt.Unix() > s.UpdateAt.Unix())
|
assert.True(t, s2.UpdateAt.Unix() > s.UpdateAt.Unix())
|
||||||
assert.True(t, s2.UpdateAt.Unix() > s2.CreateAt.Unix())
|
assert.True(t, s2.UpdateAt.Unix() > s2.CreateAt.Unix())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUpdateMapCondition(t *testing.T) {
|
||||||
|
assert.NoError(t, prepareEngine())
|
||||||
|
|
||||||
|
type UpdateMapCondition struct {
|
||||||
|
Id int64
|
||||||
|
String string
|
||||||
|
}
|
||||||
|
|
||||||
|
assertSync(t, new(UpdateMapCondition))
|
||||||
|
|
||||||
|
var c = UpdateMapCondition{
|
||||||
|
String: "string",
|
||||||
|
}
|
||||||
|
_, err := testEngine.Insert(&c)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
cnt, err := testEngine.Update(&UpdateMapCondition{
|
||||||
|
String: "string1",
|
||||||
|
}, map[string]interface{}{
|
||||||
|
"id": c.Id,
|
||||||
|
})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.EqualValues(t, 1, cnt)
|
||||||
|
|
||||||
|
var c2 UpdateMapCondition
|
||||||
|
has, err := testEngine.ID(c.Id).Get(&c2)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.True(t, has)
|
||||||
|
assert.EqualValues(t, "string1", c2.String)
|
||||||
|
}
|
||||||
|
|
|
@ -160,6 +160,9 @@ func (statement *Statement) And(query interface{}, args ...interface{}) *Stateme
|
||||||
case string:
|
case string:
|
||||||
cond := builder.Expr(query.(string), args...)
|
cond := builder.Expr(query.(string), args...)
|
||||||
statement.cond = statement.cond.And(cond)
|
statement.cond = statement.cond.And(cond)
|
||||||
|
case map[string]interface{}:
|
||||||
|
cond := builder.Eq(query.(map[string]interface{}))
|
||||||
|
statement.cond = statement.cond.And(cond)
|
||||||
case builder.Cond:
|
case builder.Cond:
|
||||||
cond := query.(builder.Cond)
|
cond := query.(builder.Cond)
|
||||||
statement.cond = statement.cond.And(cond)
|
statement.cond = statement.cond.And(cond)
|
||||||
|
@ -181,6 +184,9 @@ func (statement *Statement) Or(query interface{}, args ...interface{}) *Statemen
|
||||||
case string:
|
case string:
|
||||||
cond := builder.Expr(query.(string), args...)
|
cond := builder.Expr(query.(string), args...)
|
||||||
statement.cond = statement.cond.Or(cond)
|
statement.cond = statement.cond.Or(cond)
|
||||||
|
case map[string]interface{}:
|
||||||
|
cond := builder.Eq(query.(map[string]interface{}))
|
||||||
|
statement.cond = statement.cond.Or(cond)
|
||||||
case builder.Cond:
|
case builder.Cond:
|
||||||
cond := query.(builder.Cond)
|
cond := query.(builder.Cond)
|
||||||
statement.cond = statement.cond.Or(cond)
|
statement.cond = statement.cond.Or(cond)
|
||||||
|
|
Loading…
Reference in New Issue