1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 03:01:00 +00:00

orm add operator between #518

This commit is contained in:
slene 2014-03-10 20:18:57 +08:00 committed by asta.xie
parent d0949b64c6
commit cacdb3228d
2 changed files with 17 additions and 4 deletions

View File

@ -35,7 +35,7 @@ var (
"istartswith": true,
"iendswith": true,
"in": true,
// "range": true,
"between": true,
// "year": true,
// "month": true,
// "day": true,
@ -916,13 +916,19 @@ func (d *dbBase) GenerateOperatorSql(mi *modelInfo, fi *fieldInfo, operator stri
}
arg := params[0]
if operator == "in" {
switch operator {
case "in":
marks := make([]string, len(params))
for i, _ := range marks {
marks[i] = "?"
}
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 {
panic(fmt.Errorf("operator `%s` need 1 args not %d", operator, len(params)))
}

View File

@ -653,6 +653,14 @@ func TestOperators(t *testing.T) {
num, err = qs.Filter("status__in", []*int{&n1}, &n2).Count()
throwFail(t, err)
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) {
@ -1611,7 +1619,6 @@ func TestDelete(t *testing.T) {
throwFail(t, err)
throwFail(t, AssertIs(num, 4))
fmt.Println("...")
qs = dORM.QueryTable("comment")
num, err = qs.Filter("Post__User", 3).Delete()
throwFail(t, err)