add map key operator support in Where function for gdb

This commit is contained in:
John
2019-03-08 11:12:52 +08:00
parent 3eee95caf2
commit 94bd5da68a
2 changed files with 15 additions and 2 deletions

View File

@ -13,6 +13,7 @@ import (
"github.com/gogf/gf/g/os/glog"
"github.com/gogf/gf/g/os/gtime"
"github.com/gogf/gf/g/text/gregex"
"github.com/gogf/gf/g/text/gstr"
"github.com/gogf/gf/g/util/gconv"
_ "github.com/gogf/gf/third/github.com/go-sql-driver/mysql"
"reflect"
@ -44,7 +45,11 @@ func formatCondition(where interface{}, args []interface{}) (newWhere string, ne
case reflect.Array:
buffer.WriteString(k + " IN(?)")
default:
buffer.WriteString(k + "=?")
if gstr.Pos(k, "<") == -1 && gstr.Pos(k, ">") == -1 && gstr.Pos(k, "=") == -1 {
buffer.WriteString(k + "=?")
} else {
buffer.WriteString(k + "?")
}
}
// 当给定的Where参数为map/struct时args参数必定为空
// 考虑到后续还会对args做处理特别是判断slice类型这里直接给args赋值。

View File

@ -434,6 +434,14 @@ func TestModel_Where(t *testing.T) {
}
gtest.Assert(result["id"].Int(), 3)
})
// map key operator
gtest.Case(t, func() {
result, err := db.Table("user").Where(g.Map{"id>" : 1, "id<" : 3}).One()
if err != nil {
gtest.Fatal(err)
}
gtest.Assert(result["id"].Int(), 2)
})
// struct
gtest.Case(t, func() {
type User struct {
@ -454,7 +462,7 @@ func TestModel_Where(t *testing.T) {
})
// slice single
gtest.Case(t, func() {
result, err := db.Table("user").Where("id IN(?)", g.Slice{1,3}).OrderBy("id ASC").All()
result, err := db.Table("user").Where("id IN(?)", g.Slice{1, 3}).OrderBy("id ASC").All()
if err != nil {
gtest.Fatal(err)
}