update gdb.Model.Where, orverwrite args if multiple calls Where function

This commit is contained in:
John
2019-03-08 10:18:38 +08:00
parent 374c70c0e3
commit a6ec9d7a1c
2 changed files with 4 additions and 2 deletions

View File

@ -66,6 +66,7 @@ func formatCondition(where interface{}, args []interface{}) (newWhere string, ne
for i := 0; i < rv.Len(); i++ {
newArgs = append(newArgs, rv.Index(i).Interface())
}
// counter用于匹配该参数的位置(与index对应)
counter := 0
newWhere, _ = gregex.ReplaceStringFunc(`\?`, newWhere, func(s string) string {
counter++

View File

@ -136,12 +136,13 @@ func (md *Model) Filter() (*Model) {
return model
}
// 链式操作condition支持string & gdb.Map
// 链式操作condition支持string & gdb.Map.
// 注意多个Where调用时会相互覆盖只有最后一个Where语句生效。
func (md *Model) Where(where interface{}, args ...interface{}) (*Model) {
model := md.getModel()
newWhere, newArgs := formatCondition(where, args)
model.where = newWhere
model.whereArgs = append(model.whereArgs, newArgs...)
model.whereArgs = newArgs
// 支持 Where("uid", 1)这种格式
if len(args) == 1 && strings.Index(model.where , "?") < 0 {
model.where += "=?"