mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 13:00:54 +00:00
orm add operator between
#518
This commit is contained in:
parent
03b17a2ca9
commit
6ca30386b8
12
orm/db.go
12
orm/db.go
@ -35,7 +35,7 @@ var (
|
|||||||
"istartswith": true,
|
"istartswith": true,
|
||||||
"iendswith": true,
|
"iendswith": true,
|
||||||
"in": true,
|
"in": true,
|
||||||
// "range": true,
|
"between": true,
|
||||||
// "year": true,
|
// "year": true,
|
||||||
// "month": true,
|
// "month": true,
|
||||||
// "day": true,
|
// "day": true,
|
||||||
@ -916,13 +916,19 @@ func (d *dbBase) GenerateOperatorSql(mi *modelInfo, fi *fieldInfo, operator stri
|
|||||||
}
|
}
|
||||||
arg := params[0]
|
arg := params[0]
|
||||||
|
|
||||||
if operator == "in" {
|
switch operator {
|
||||||
|
case "in":
|
||||||
marks := make([]string, len(params))
|
marks := make([]string, len(params))
|
||||||
for i, _ := range marks {
|
for i, _ := range marks {
|
||||||
marks[i] = "?"
|
marks[i] = "?"
|
||||||
}
|
}
|
||||||
sql = fmt.Sprintf("IN (%s)", strings.Join(marks, ", "))
|
sql = fmt.Sprintf("IN (%s)", strings.Join(marks, ", "))
|
||||||
} else {
|
case "between":
|
||||||
|
if len(params) != 2 {
|
||||||
|
panic(fmt.Errorf("operator `%s` need 2 args not %d", operator, len(params)))
|
||||||
|
}
|
||||||
|
sql = "BETWEEN ? AND ?"
|
||||||
|
default:
|
||||||
if len(params) > 1 {
|
if len(params) > 1 {
|
||||||
panic(fmt.Errorf("operator `%s` need 1 args not %d", operator, len(params)))
|
panic(fmt.Errorf("operator `%s` need 1 args not %d", operator, len(params)))
|
||||||
}
|
}
|
||||||
|
@ -653,6 +653,14 @@ func TestOperators(t *testing.T) {
|
|||||||
num, err = qs.Filter("status__in", []*int{&n1}, &n2).Count()
|
num, err = qs.Filter("status__in", []*int{&n1}, &n2).Count()
|
||||||
throwFail(t, err)
|
throwFail(t, err)
|
||||||
throwFail(t, AssertIs(num, 2))
|
throwFail(t, AssertIs(num, 2))
|
||||||
|
|
||||||
|
num, err = qs.Filter("id__between", 2, 3).Count()
|
||||||
|
throwFail(t, err)
|
||||||
|
throwFail(t, AssertIs(num, 2))
|
||||||
|
|
||||||
|
num, err = qs.Filter("id__between", []int{2, 3}).Count()
|
||||||
|
throwFail(t, err)
|
||||||
|
throwFail(t, AssertIs(num, 2))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSetCond(t *testing.T) {
|
func TestSetCond(t *testing.T) {
|
||||||
@ -1611,7 +1619,6 @@ func TestDelete(t *testing.T) {
|
|||||||
throwFail(t, err)
|
throwFail(t, err)
|
||||||
throwFail(t, AssertIs(num, 4))
|
throwFail(t, AssertIs(num, 4))
|
||||||
|
|
||||||
fmt.Println("...")
|
|
||||||
qs = dORM.QueryTable("comment")
|
qs = dORM.QueryTable("comment")
|
||||||
num, err = qs.Filter("Post__User", 3).Delete()
|
num, err = qs.Filter("Post__User", 3).Delete()
|
||||||
throwFail(t, err)
|
throwFail(t, err)
|
||||||
|
Loading…
Reference in New Issue
Block a user